1 /* dv-nvram.c -- Generic driver for a non volatile ram (battery saved)
2 Copyright (C) 1999-2023 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"
32 #include "m68hc11-sim.h"
36 nvram - Non Volatile Ram
41 Implements a generic battery saved CMOS ram. This ram device does
42 not contain any realtime clock and does not generate any interrupt.
43 The ram content is loaded from a file and saved when it is changed.
44 It is intended to be generic.
51 Base and size of the non-volatile ram bank.
55 Path where the memory must be saved or loaded when we start.
57 mode {map | save-modified | save-all}
59 Controls how to load and save the memory content.
61 map The file is mapped in memory
62 save-modified The simulator keeps an open file descriptor to
63 the file and saves portion of memory which are
65 save-all The simulator saves the complete memory each time
66 it's modified (it does not keep an open file
77 This device is independent of the Motorola 68hc11.
83 /* static functions */
85 /* Control of how to access the ram and save its content. */
89 /* Save the complete ram block each time it's changed.
90 We don't keep an open file descriptor. This should be
91 ok for small memory banks. */
94 /* Save only the memory bytes which are modified.
95 This mode means that we have to keep an open file
96 descriptor (O_RDWR). It's good for middle sized memory banks. */
99 /* Map file in memory (not yet implemented).
100 This mode is suitable for large memory banks. We don't allocate
101 a buffer to represent the ram, instead it's mapped in memory
108 address_word base_address
; /* Base address of ram. */
109 unsigned size
; /* Size of ram. */
110 uint8_t *data
; /* Pointer to ram memory. */
111 const char *file_name
; /* Path of ram file. */
112 int fd
; /* File description of opened ram file. */
113 enum nvram_mode mode
; /* How load/save ram file. */
118 /* Finish off the partially created hw device. Attach our local
119 callbacks. Wire up our port names etc. */
121 static hw_io_read_buffer_method nvram_io_read_buffer
;
122 static hw_io_write_buffer_method nvram_io_write_buffer
;
127 attach_nvram_regs (struct hw
*me
, struct nvram
*controller
)
129 unsigned_word attach_address
;
131 unsigned attach_size
;
132 reg_property_spec reg
;
135 /* Get ram bank description (base and size). */
136 if (hw_find_property (me
, "reg") == NULL
)
137 hw_abort (me
, "Missing \"reg\" property");
139 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
140 hw_abort (me
, "\"reg\" property must contain one addr/size entry");
142 hw_unit_address_to_attach_address (hw_parent (me
),
147 hw_unit_size_to_attach_size (hw_parent (me
),
151 hw_attach_address (hw_parent (me
), 0,
152 attach_space
, attach_address
, attach_size
,
155 controller
->mode
= NVRAM_SAVE_ALL
;
156 controller
->base_address
= attach_address
;
157 controller
->size
= attach_size
;
160 /* Get the file where the ram content must be loaded/saved. */
161 if(hw_find_property (me
, "file") == NULL
)
162 hw_abort (me
, "Missing \"file\" property");
164 controller
->file_name
= hw_find_string_property (me
, "file");
166 /* Get the mode which defines how to save the memory. */
167 if(hw_find_property (me
, "mode") != NULL
)
169 const char *value
= hw_find_string_property (me
, "mode");
171 if (strcmp (value
, "map") == 0)
172 controller
->mode
= NVRAM_MAP_FILE
;
173 else if (strcmp (value
, "save-modified") == 0)
174 controller
->mode
= NVRAM_SAVE_MODIFIED
;
175 else if (strcmp (value
, "save-all") == 0)
176 controller
->mode
= NVRAM_SAVE_ALL
;
178 hw_abort (me
, "illegal value for mode parameter `%s': "
179 "use map, save-modified or save-all", value
);
182 /* Initialize the ram by loading/mapping the file in memory.
183 If the file does not exist, create and give it some content. */
184 switch (controller
->mode
)
187 hw_abort (me
, "'map' mode is not yet implemented, use 'save-modified'");
190 case NVRAM_SAVE_MODIFIED
:
192 controller
->data
= hw_malloc (me
, attach_size
);
193 if (controller
->data
== 0)
194 hw_abort (me
, "Not enough memory, try to use the mode 'map'");
196 memset (controller
->data
, 0, attach_size
);
197 controller
->fd
= open (controller
->file_name
, O_RDWR
);
198 if (controller
->fd
< 0)
200 controller
->fd
= open (controller
->file_name
,
201 O_RDWR
| O_CREAT
, 0644);
202 if (controller
->fd
< 0)
203 hw_abort (me
, "Cannot open or create file '%s'",
204 controller
->file_name
);
205 result
= write (controller
->fd
, controller
->data
, attach_size
);
206 if (result
!= attach_size
)
209 hw_free (me
, controller
->data
);
210 close (controller
->fd
);
212 hw_abort (me
, "Failed to save the ram content");
217 result
= read (controller
->fd
, controller
->data
, attach_size
);
218 if (result
!= attach_size
)
221 hw_free (me
, controller
->data
);
222 close (controller
->fd
);
224 hw_abort (me
, "Failed to load the ram content");
227 if (controller
->mode
== NVRAM_SAVE_ALL
)
229 close (controller
->fd
);
241 nvram_finish (struct hw
*me
)
243 struct nvram
*controller
;
245 controller
= HW_ZALLOC (me
, struct nvram
);
247 set_hw_data (me
, controller
);
248 set_hw_io_read_buffer (me
, nvram_io_read_buffer
);
249 set_hw_io_write_buffer (me
, nvram_io_write_buffer
);
251 /* Attach ourself to our parent bus. */
252 attach_nvram_regs (me
, controller
);
257 /* generic read/write */
260 nvram_io_read_buffer (struct hw
*me
,
266 struct nvram
*controller
= hw_data (me
);
268 HW_TRACE ((me
, "read 0x%08lx %d [%ld]",
269 (long) base
, (int) nr_bytes
,
270 (long) (base
- controller
->base_address
)));
272 base
-= controller
->base_address
;
273 if (base
+ nr_bytes
> controller
->size
)
274 nr_bytes
= controller
->size
- base
;
276 memcpy (dest
, &controller
->data
[base
], nr_bytes
);
283 nvram_io_write_buffer (struct hw
*me
,
289 struct nvram
*controller
= hw_data (me
);
291 HW_TRACE ((me
, "write 0x%08lx %d [%ld]",
292 (long) base
, (int) nr_bytes
,
293 (long) (base
- controller
->base_address
)));
295 base
-= controller
->base_address
;
296 if (base
+ nr_bytes
> controller
->size
)
297 nr_bytes
= controller
->size
- base
;
299 switch (controller
->mode
)
303 int fd
, result
, oerrno
;
305 fd
= open (controller
->file_name
, O_WRONLY
, 0644);
311 memcpy (&controller
->data
[base
], source
, nr_bytes
);
312 result
= write (fd
, controller
->data
, controller
->size
);
317 if (result
!= controller
->size
)
324 case NVRAM_SAVE_MODIFIED
:
329 pos
= lseek (controller
->fd
, (off_t
) base
, SEEK_SET
);
330 if (pos
!= (off_t
) base
)
333 result
= write (controller
->fd
, source
, nr_bytes
);
344 memcpy (&controller
->data
[base
], source
, nr_bytes
);
349 const struct hw_descriptor dv_nvram_descriptor
[] = {
350 { "nvram", nvram_finish
, },