1 /* dv-nvram.c -- Generic driver for a non volatile ram (battery saved)
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 Written by Stephane Carrez (stcarrez@worldnet.fr)
4 (From a driver model Contributed by Cygnus Solutions.)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "sim-assert.h"
33 nvram - Non Volatile Ram
38 Implements a generic battery saved CMOS ram. This ram device does
39 not contain any realtime clock and does not generate any interrupt.
40 The ram content is loaded from a file and saved when it is changed.
41 It is intended to be generic.
48 Base and size of the non-volatile ram bank.
52 Path where the memory must be saved or loaded when we start.
54 mode {map | save-modified | save-all}
56 Controls how to load and save the memory content.
58 map The file is mapped in memory
59 save-modified The simulator keeps an open file descriptor to
60 the file and saves portion of memory which are
62 save-all The simulator saves the complete memory each time
63 it's modified (it does not keep an open file
74 This device is independent of the Motorola 68hc11.
80 /* static functions */
82 /* Control of how to access the ram and save its content. */
86 /* Save the complete ram block each time it's changed.
87 We don't keep an open file descriptor. This should be
88 ok for small memory banks. */
91 /* Save only the memory bytes which are modified.
92 This mode means that we have to keep an open file
93 descriptor (O_RDWR). It's good for middle sized memory banks. */
96 /* Map file in memory (not yet implemented).
97 This mode is suitable for large memory banks. We don't allocate
98 a buffer to represent the ram, instead it's mapped in memory
105 address_word base_address; /* Base address of ram. */
106 unsigned size; /* Size of ram. */
107 unsigned8 *data; /* Pointer to ram memory. */
108 const char *file_name; /* Path of ram file. */
109 int fd; /* File description of opened ram file. */
110 enum nvram_mode mode; /* How load/save ram file. */
115 /* Finish off the partially created hw device. Attach our local
116 callbacks. Wire up our port names etc. */
118 static hw_io_read_buffer_method nvram_io_read_buffer;
119 static hw_io_write_buffer_method nvram_io_write_buffer;
124 attach_nvram_regs (struct hw *me, struct nvram *controller)
126 unsigned_word attach_address;
128 unsigned attach_size;
129 reg_property_spec reg;
132 /* Get ram bank description (base and size). */
133 if (hw_find_property (me, "reg") == NULL)
134 hw_abort (me, "Missing \"reg\" property");
136 if (!hw_find_reg_array_property (me, "reg", 0, ®))
137 hw_abort (me, "\"reg\" property must contain one addr/size entry");
139 hw_unit_address_to_attach_address (hw_parent (me),
144 hw_unit_size_to_attach_size (hw_parent (me),
148 hw_attach_address (hw_parent (me), 0,
149 attach_space, attach_address, attach_size,
152 controller->mode = NVRAM_SAVE_ALL;
153 controller->base_address = attach_address;
154 controller->size = attach_size;
157 /* Get the file where the ram content must be loaded/saved. */
158 if(hw_find_property (me, "file") == NULL)
159 hw_abort (me, "Missing \"file\" property");
161 controller->file_name = hw_find_string_property (me, "file");
163 /* Get the mode which defines how to save the memory. */
164 if(hw_find_property (me, "mode") != NULL)
166 const char *value = hw_find_string_property (me, "mode");
168 if (strcmp (value, "map") == 0)
169 controller->mode = NVRAM_MAP_FILE;
170 else if (strcmp (value, "save-modified") == 0)
171 controller->mode = NVRAM_SAVE_MODIFIED;
172 else if (strcmp (value, "save-all") == 0)
173 controller->mode = NVRAM_SAVE_ALL;
175 hw_abort (me, "illegal value for mode parameter `%s': "
176 "use map, save-modified or save-all", value);
179 /* Initialize the ram by loading/mapping the file in memory.
180 If the file does not exist, create and give it some content. */
181 switch (controller->mode)
184 hw_abort (me, "'map' mode is not yet implemented, use 'save-modified'");
187 case NVRAM_SAVE_MODIFIED:
189 controller->data = hw_malloc (me, attach_size);
190 if (controller->data == 0)
191 hw_abort (me, "Not enough memory, try to use the mode 'map'");
193 memset (controller->data, 0, attach_size);
194 controller->fd = open (controller->file_name, O_RDWR);
195 if (controller->fd < 0)
197 controller->fd = open (controller->file_name,
198 O_RDWR | O_CREAT, 0644);
199 if (controller->fd < 0)
200 hw_abort (me, "Cannot open or create file '%s'",
201 controller->file_name);
202 result = write (controller->fd, controller->data, attach_size);
203 if (result != attach_size)
206 hw_free (me, controller->data);
207 close (controller->fd);
209 hw_abort (me, "Failed to save the ram content");
214 result = read (controller->fd, controller->data, attach_size);
215 if (result != attach_size)
218 hw_free (me, controller->data);
219 close (controller->fd);
221 hw_abort (me, "Failed to load the ram content");
224 if (controller->mode == NVRAM_SAVE_ALL)
226 close (controller->fd);
238 nvram_finish (struct hw *me)
240 struct nvram *controller;
242 controller = HW_ZALLOC (me, struct nvram);
244 set_hw_data (me, controller);
245 set_hw_io_read_buffer (me, nvram_io_read_buffer);
246 set_hw_io_write_buffer (me, nvram_io_write_buffer);
248 /* Attach ourself to our parent bus. */
249 attach_nvram_regs (me, controller);
254 /* generic read/write */
257 nvram_io_read_buffer (struct hw *me,
263 struct nvram *controller = hw_data (me);
265 HW_TRACE ((me, "read 0x%08lx %d [%ld]",
266 (long) base, (int) nr_bytes,
267 (long) (base - controller->base_address)));
269 base -= controller->base_address;
270 if (base + nr_bytes > controller->size)
271 nr_bytes = controller->size - base;
273 memcpy (dest, &controller->data[base], nr_bytes);
280 nvram_io_write_buffer (struct hw *me,
286 struct nvram *controller = hw_data (me);
288 HW_TRACE ((me, "write 0x%08lx %d [%ld]",
289 (long) base, (int) nr_bytes,
290 (long) (base - controller->base_address)));
292 base -= controller->base_address;
293 if (base + nr_bytes > controller->size)
294 nr_bytes = controller->size - base;
296 switch (controller->mode)
300 int fd, result, oerrno;
302 fd = open (controller->file_name, O_WRONLY, 0644);
308 memcpy (&controller->data[base], source, nr_bytes);
309 result = write (fd, controller->data, controller->size);
314 if (result != controller->size)
321 case NVRAM_SAVE_MODIFIED:
326 pos = lseek (controller->fd, (off_t) base, SEEK_SET);
327 if (pos != (off_t) base)
330 result = write (controller->fd, source, nr_bytes);
341 memcpy (&controller->data[base], source, nr_bytes);
346 const struct hw_descriptor dv_nvram_descriptor[] = {
347 { "nvram", nvram_finish, },