1 /* dv-nvram.c -- Generic driver for a non volatile ram (battery saved)
2 Copyright (C) 1999, 2000, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 (From a driver model Contributed by Cygnus Solutions.)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "sim-assert.h"
34 nvram - Non Volatile Ram
39 Implements a generic battery saved CMOS ram. This ram device does
40 not contain any realtime clock and does not generate any interrupt.
41 The ram content is loaded from a file and saved when it is changed.
42 It is intended to be generic.
49 Base and size of the non-volatile ram bank.
53 Path where the memory must be saved or loaded when we start.
55 mode {map | save-modified | save-all}
57 Controls how to load and save the memory content.
59 map The file is mapped in memory
60 save-modified The simulator keeps an open file descriptor to
61 the file and saves portion of memory which are
63 save-all The simulator saves the complete memory each time
64 it's modified (it does not keep an open file
75 This device is independent of the Motorola 68hc11.
81 /* static functions */
83 /* Control of how to access the ram and save its content. */
87 /* Save the complete ram block each time it's changed.
88 We don't keep an open file descriptor. This should be
89 ok for small memory banks. */
92 /* Save only the memory bytes which are modified.
93 This mode means that we have to keep an open file
94 descriptor (O_RDWR). It's good for middle sized memory banks. */
97 /* Map file in memory (not yet implemented).
98 This mode is suitable for large memory banks. We don't allocate
99 a buffer to represent the ram, instead it's mapped in memory
106 address_word base_address; /* Base address of ram. */
107 unsigned size; /* Size of ram. */
108 unsigned8 *data; /* Pointer to ram memory. */
109 const char *file_name; /* Path of ram file. */
110 int fd; /* File description of opened ram file. */
111 enum nvram_mode mode; /* How load/save ram file. */
116 /* Finish off the partially created hw device. Attach our local
117 callbacks. Wire up our port names etc. */
119 static hw_io_read_buffer_method nvram_io_read_buffer;
120 static hw_io_write_buffer_method nvram_io_write_buffer;
125 attach_nvram_regs (struct hw *me, struct nvram *controller)
127 unsigned_word attach_address;
129 unsigned attach_size;
130 reg_property_spec reg;
133 /* Get ram bank description (base and size). */
134 if (hw_find_property (me, "reg") == NULL)
135 hw_abort (me, "Missing \"reg\" property");
137 if (!hw_find_reg_array_property (me, "reg", 0, ®))
138 hw_abort (me, "\"reg\" property must contain one addr/size entry");
140 hw_unit_address_to_attach_address (hw_parent (me),
145 hw_unit_size_to_attach_size (hw_parent (me),
149 hw_attach_address (hw_parent (me), 0,
150 attach_space, attach_address, attach_size,
153 controller->mode = NVRAM_SAVE_ALL;
154 controller->base_address = attach_address;
155 controller->size = attach_size;
158 /* Get the file where the ram content must be loaded/saved. */
159 if(hw_find_property (me, "file") == NULL)
160 hw_abort (me, "Missing \"file\" property");
162 controller->file_name = hw_find_string_property (me, "file");
164 /* Get the mode which defines how to save the memory. */
165 if(hw_find_property (me, "mode") != NULL)
167 const char *value = hw_find_string_property (me, "mode");
169 if (strcmp (value, "map") == 0)
170 controller->mode = NVRAM_MAP_FILE;
171 else if (strcmp (value, "save-modified") == 0)
172 controller->mode = NVRAM_SAVE_MODIFIED;
173 else if (strcmp (value, "save-all") == 0)
174 controller->mode = NVRAM_SAVE_ALL;
176 hw_abort (me, "illegal value for mode parameter `%s': "
177 "use map, save-modified or save-all", value);
180 /* Initialize the ram by loading/mapping the file in memory.
181 If the file does not exist, create and give it some content. */
182 switch (controller->mode)
185 hw_abort (me, "'map' mode is not yet implemented, use 'save-modified'");
188 case NVRAM_SAVE_MODIFIED:
190 controller->data = (char*) hw_malloc (me, attach_size);
191 if (controller->data == 0)
192 hw_abort (me, "Not enough memory, try to use the mode 'map'");
194 memset (controller->data, 0, attach_size);
195 controller->fd = open (controller->file_name, O_RDWR);
196 if (controller->fd < 0)
198 controller->fd = open (controller->file_name,
199 O_RDWR | O_CREAT, 0644);
200 if (controller->fd < 0)
201 hw_abort (me, "Cannot open or create file '%s'",
202 controller->file_name);
203 result = write (controller->fd, controller->data, attach_size);
204 if (result != attach_size)
207 hw_free (me, controller->data);
208 close (controller->fd);
210 hw_abort (me, "Failed to save the ram content");
215 result = read (controller->fd, controller->data, attach_size);
216 if (result != attach_size)
219 hw_free (me, controller->data);
220 close (controller->fd);
222 hw_abort (me, "Failed to load the ram content");
225 if (controller->mode == NVRAM_SAVE_ALL)
227 close (controller->fd);
239 nvram_finish (struct hw *me)
241 struct nvram *controller;
243 controller = HW_ZALLOC (me, struct nvram);
245 set_hw_data (me, controller);
246 set_hw_io_read_buffer (me, nvram_io_read_buffer);
247 set_hw_io_write_buffer (me, nvram_io_write_buffer);
249 /* Attach ourself to our parent bus. */
250 attach_nvram_regs (me, controller);
255 /* generic read/write */
258 nvram_io_read_buffer (struct hw *me,
264 struct nvram *controller = hw_data (me);
266 HW_TRACE ((me, "read 0x%08lx %d [%ld]",
267 (long) base, (int) nr_bytes,
268 (long) (base - controller->base_address)));
270 base -= controller->base_address;
271 if (base + nr_bytes > controller->size)
272 nr_bytes = controller->size - base;
274 memcpy (dest, &controller->data[base], nr_bytes);
281 nvram_io_write_buffer (struct hw *me,
287 struct nvram *controller = hw_data (me);
289 HW_TRACE ((me, "write 0x%08lx %d [%ld]",
290 (long) base, (int) nr_bytes,
291 (long) (base - controller->base_address)));
293 base -= controller->base_address;
294 if (base + nr_bytes > controller->size)
295 nr_bytes = controller->size - base;
297 switch (controller->mode)
301 int fd, result, oerrno;
303 fd = open (controller->file_name, O_WRONLY, 0644);
309 memcpy (&controller->data[base], source, nr_bytes);
310 result = write (fd, controller->data, controller->size);
315 if (result != controller->size)
322 case NVRAM_SAVE_MODIFIED:
327 pos = lseek (controller->fd, (off_t) base, SEEK_SET);
328 if (pos != (off_t) base)
331 result = write (controller->fd, source, nr_bytes);
342 memcpy (&controller->data[base], source, nr_bytes);
347 const struct hw_descriptor dv_nvram_descriptor[] = {
348 { "nvram", nvram_finish, },