2 * QEMU NVRAM emulation for DS1225Y chip
4 * Copyright (c) 2007-2008 Hervé Poussineau
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
38 NvRamState
*s
= opaque
;
41 val
= s
->contents
[addr
];
42 trace_nvram_read(addr
, val
);
46 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
49 v
= nvram_readb(opaque
, addr
);
50 v
|= nvram_readb(opaque
, addr
+ 1) << 8;
54 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
57 v
= nvram_readb(opaque
, addr
);
58 v
|= nvram_readb(opaque
, addr
+ 1) << 8;
59 v
|= nvram_readb(opaque
, addr
+ 2) << 16;
60 v
|= nvram_readb(opaque
, addr
+ 3) << 24;
64 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
66 NvRamState
*s
= opaque
;
69 trace_nvram_write(addr
, s
->contents
[addr
], val
);
71 s
->contents
[addr
] = val
;
73 qemu_fseek(s
->file
, addr
, SEEK_SET
);
74 qemu_put_byte(s
->file
, (int)val
);
79 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
81 nvram_writeb(opaque
, addr
, val
& 0xff);
82 nvram_writeb(opaque
, addr
+ 1, (val
>> 8) & 0xff);
85 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t val
)
87 nvram_writeb(opaque
, addr
, val
& 0xff);
88 nvram_writeb(opaque
, addr
+ 1, (val
>> 8) & 0xff);
89 nvram_writeb(opaque
, addr
+ 2, (val
>> 16) & 0xff);
90 nvram_writeb(opaque
, addr
+ 3, (val
>> 24) & 0xff);
93 static CPUReadMemoryFunc
* const nvram_read
[] = {
99 static CPUWriteMemoryFunc
* const nvram_write
[] = {
105 static int nvram_post_load(void *opaque
, int version_id
)
107 NvRamState
*s
= opaque
;
109 /* Close file, as filename may has changed in load/store process */
111 qemu_fclose(s
->file
);
114 /* Write back nvram contents */
115 s
->file
= qemu_fopen(s
->filename
, "wb");
117 /* Write back contents, as 'wb' mode cleaned the file */
118 qemu_put_buffer(s
->file
, s
->contents
, s
->chip_size
);
119 qemu_fflush(s
->file
);
125 static const VMStateDescription vmstate_nvram
= {
128 .minimum_version_id
= 0,
129 .minimum_version_id_old
= 0,
130 .post_load
= nvram_post_load
,
131 .fields
= (VMStateField
[]) {
132 VMSTATE_VARRAY_UINT32(contents
, NvRamState
, chip_size
, 0,
133 vmstate_info_uint8
, uint8_t),
134 VMSTATE_END_OF_LIST()
143 static int nvram_sysbus_initfn(SysBusDevice
*dev
)
145 NvRamState
*s
= &FROM_SYSBUS(SysBusNvRamState
, dev
)->nvram
;
149 s
->contents
= qemu_mallocz(s
->chip_size
);
151 s_io
= cpu_register_io_memory(nvram_read
, nvram_write
, s
,
152 DEVICE_NATIVE_ENDIAN
);
153 sysbus_init_mmio(dev
, s
->chip_size
, s_io
);
155 /* Read current file */
156 file
= qemu_fopen(s
->filename
, "rb");
158 /* Read nvram contents */
159 qemu_get_buffer(file
, s
->contents
, s
->chip_size
);
162 nvram_post_load(s
, 0);
167 static SysBusDeviceInfo nvram_sysbus_info
= {
168 .qdev
.name
= "ds1225y",
169 .qdev
.size
= sizeof(SysBusNvRamState
),
170 .qdev
.vmsd
= &vmstate_nvram
,
171 .init
= nvram_sysbus_initfn
,
172 .qdev
.props
= (Property
[]) {
173 DEFINE_PROP_UINT32("size", SysBusNvRamState
, nvram
.chip_size
, 0x2000),
174 DEFINE_PROP_STRING("filename", SysBusNvRamState
, nvram
.filename
),
175 DEFINE_PROP_END_OF_LIST(),
179 static void nvram_register(void)
181 sysbus_register_withprop(&nvram_sysbus_info
);
184 device_init(nvram_register
)