1 /* dv-nvram.c -- Generic driver for a non volatile ram (battery saved)
2 Copyright (C) 1999-2021 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/>.
21 /* This must come before any other includes. */
26 #include "sim-assert.h"
35 nvram - Non Volatile Ram
40 Implements a generic battery saved CMOS ram. This ram device does
41 not contain any realtime clock and does not generate any interrupt.
42 The ram content is loaded from a file and saved when it is changed.
43 It is intended to be generic.
50 Base and size of the non-volatile ram bank.
54 Path where the memory must be saved or loaded when we start.
56 mode {map | save-modified | save-all}
58 Controls how to load and save the memory content.
60 map The file is mapped in memory
61 save-modified The simulator keeps an open file descriptor to
62 the file and saves portion of memory which are
64 save-all The simulator saves the complete memory each time
65 it's modified (it does not keep an open file
76 This device is independent of the Motorola 68hc11.
82 /* static functions */
84 /* Control of how to access the ram and save its content. */
88 /* Save the complete ram block each time it's changed.
89 We don't keep an open file descriptor. This should be
90 ok for small memory banks. */
93 /* Save only the memory bytes which are modified.
94 This mode means that we have to keep an open file
95 descriptor (O_RDWR). It's good for middle sized memory banks. */
98 /* Map file in memory (not yet implemented).
99 This mode is suitable for large memory banks. We don't allocate
100 a buffer to represent the ram, instead it's mapped in memory
107 address_word base_address
; /* Base address of ram. */
108 unsigned size
; /* Size of ram. */
109 unsigned8
*data
; /* Pointer to ram memory. */
110 const char *file_name
; /* Path of ram file. */
111 int fd
; /* File description of opened ram file. */
112 enum nvram_mode mode
; /* How load/save ram file. */
117 /* Finish off the partially created hw device. Attach our local
118 callbacks. Wire up our port names etc. */
120 static hw_io_read_buffer_method nvram_io_read_buffer
;
121 static hw_io_write_buffer_method nvram_io_write_buffer
;
126 attach_nvram_regs (struct hw
*me
, struct nvram
*controller
)
128 unsigned_word attach_address
;
130 unsigned attach_size
;
131 reg_property_spec reg
;
134 /* Get ram bank description (base and size). */
135 if (hw_find_property (me
, "reg") == NULL
)
136 hw_abort (me
, "Missing \"reg\" property");
138 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
139 hw_abort (me
, "\"reg\" property must contain one addr/size entry");
141 hw_unit_address_to_attach_address (hw_parent (me
),
146 hw_unit_size_to_attach_size (hw_parent (me
),
150 hw_attach_address (hw_parent (me
), 0,
151 attach_space
, attach_address
, attach_size
,
154 controller
->mode
= NVRAM_SAVE_ALL
;
155 controller
->base_address
= attach_address
;
156 controller
->size
= attach_size
;
159 /* Get the file where the ram content must be loaded/saved. */
160 if(hw_find_property (me
, "file") == NULL
)
161 hw_abort (me
, "Missing \"file\" property");
163 controller
->file_name
= hw_find_string_property (me
, "file");
165 /* Get the mode which defines how to save the memory. */
166 if(hw_find_property (me
, "mode") != NULL
)
168 const char *value
= hw_find_string_property (me
, "mode");
170 if (strcmp (value
, "map") == 0)
171 controller
->mode
= NVRAM_MAP_FILE
;
172 else if (strcmp (value
, "save-modified") == 0)
173 controller
->mode
= NVRAM_SAVE_MODIFIED
;
174 else if (strcmp (value
, "save-all") == 0)
175 controller
->mode
= NVRAM_SAVE_ALL
;
177 hw_abort (me
, "illegal value for mode parameter `%s': "
178 "use map, save-modified or save-all", value
);
181 /* Initialize the ram by loading/mapping the file in memory.
182 If the file does not exist, create and give it some content. */
183 switch (controller
->mode
)
186 hw_abort (me
, "'map' mode is not yet implemented, use 'save-modified'");
189 case NVRAM_SAVE_MODIFIED
:
191 controller
->data
= hw_malloc (me
, attach_size
);
192 if (controller
->data
== 0)
193 hw_abort (me
, "Not enough memory, try to use the mode 'map'");
195 memset (controller
->data
, 0, attach_size
);
196 controller
->fd
= open (controller
->file_name
, O_RDWR
);
197 if (controller
->fd
< 0)
199 controller
->fd
= open (controller
->file_name
,
200 O_RDWR
| O_CREAT
, 0644);
201 if (controller
->fd
< 0)
202 hw_abort (me
, "Cannot open or create file '%s'",
203 controller
->file_name
);
204 result
= write (controller
->fd
, controller
->data
, attach_size
);
205 if (result
!= attach_size
)
208 hw_free (me
, controller
->data
);
209 close (controller
->fd
);
211 hw_abort (me
, "Failed to save the ram content");
216 result
= read (controller
->fd
, controller
->data
, attach_size
);
217 if (result
!= attach_size
)
220 hw_free (me
, controller
->data
);
221 close (controller
->fd
);
223 hw_abort (me
, "Failed to load the ram content");
226 if (controller
->mode
== NVRAM_SAVE_ALL
)
228 close (controller
->fd
);
240 nvram_finish (struct hw
*me
)
242 struct nvram
*controller
;
244 controller
= HW_ZALLOC (me
, struct nvram
);
246 set_hw_data (me
, controller
);
247 set_hw_io_read_buffer (me
, nvram_io_read_buffer
);
248 set_hw_io_write_buffer (me
, nvram_io_write_buffer
);
250 /* Attach ourself to our parent bus. */
251 attach_nvram_regs (me
, controller
);
256 /* generic read/write */
259 nvram_io_read_buffer (struct hw
*me
,
265 struct nvram
*controller
= hw_data (me
);
267 HW_TRACE ((me
, "read 0x%08lx %d [%ld]",
268 (long) base
, (int) nr_bytes
,
269 (long) (base
- controller
->base_address
)));
271 base
-= controller
->base_address
;
272 if (base
+ nr_bytes
> controller
->size
)
273 nr_bytes
= controller
->size
- base
;
275 memcpy (dest
, &controller
->data
[base
], nr_bytes
);
282 nvram_io_write_buffer (struct hw
*me
,
288 struct nvram
*controller
= hw_data (me
);
290 HW_TRACE ((me
, "write 0x%08lx %d [%ld]",
291 (long) base
, (int) nr_bytes
,
292 (long) (base
- controller
->base_address
)));
294 base
-= controller
->base_address
;
295 if (base
+ nr_bytes
> controller
->size
)
296 nr_bytes
= controller
->size
- base
;
298 switch (controller
->mode
)
302 int fd
, result
, oerrno
;
304 fd
= open (controller
->file_name
, O_WRONLY
, 0644);
310 memcpy (&controller
->data
[base
], source
, nr_bytes
);
311 result
= write (fd
, controller
->data
, controller
->size
);
316 if (result
!= controller
->size
)
323 case NVRAM_SAVE_MODIFIED
:
328 pos
= lseek (controller
->fd
, (off_t
) base
, SEEK_SET
);
329 if (pos
!= (off_t
) base
)
332 result
= write (controller
->fd
, source
, nr_bytes
);
343 memcpy (&controller
->data
[base
], source
, nr_bytes
);
348 const struct hw_descriptor dv_nvram_descriptor
[] = {
349 { "nvram", nvram_finish
, },