25
#include "qemu/osdep.h"
26
#include "hw/qdev-properties.h"
28
#include "migration/vmstate.h"
30
#include "qemu/error-report.h"
31
#include "qemu/module.h"
32
#include "qom/object.h"
42
static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
44
NvRamState *s = opaque;
47
val = s->contents[addr];
48
trace_nvram_read(addr, val);
52
static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
55
NvRamState *s = opaque;
58
trace_nvram_write(addr, s->contents[addr], val);
60
s->contents[addr] = val;
62
fseek(s->file, addr, SEEK_SET);
68
static const MemoryRegionOps nvram_ops = {
75
.endianness = DEVICE_LITTLE_ENDIAN,
78
static int nvram_post_load(void *opaque, int version_id)
80
NvRamState *s = opaque;
88
s->file = s->filename ? fopen(s->filename, "wb") : NULL;
91
if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {
92
printf("nvram_post_load: short write\n");
100
static const VMStateDescription vmstate_nvram = {
103
.minimum_version_id = 0,
104
.post_load = nvram_post_load,
105
.fields = (const VMStateField[]) {
106
VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0,
107
vmstate_info_uint8, uint8_t),
108
VMSTATE_END_OF_LIST()
112
#define TYPE_DS1225Y "ds1225y"
113
OBJECT_DECLARE_SIMPLE_TYPE(SysBusNvRamState, DS1225Y)
115
struct SysBusNvRamState {
116
SysBusDevice parent_obj;
121
static void nvram_sysbus_realize(DeviceState *dev, Error **errp)
123
SysBusNvRamState *sys = DS1225Y(dev);
124
NvRamState *s = &sys->nvram;
127
s->contents = g_malloc0(s->chip_size);
129
memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s,
130
"nvram", s->chip_size);
131
sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
134
file = s->filename ? fopen(s->filename, "rb") : NULL;
137
if (fread(s->contents, s->chip_size, 1, file) != 1) {
138
error_report("nvram_sysbus_realize: short read");
142
nvram_post_load(s, 0);
145
static Property nvram_sysbus_properties[] = {
146
DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000),
147
DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename),
148
DEFINE_PROP_END_OF_LIST(),
151
static void nvram_sysbus_class_init(ObjectClass *klass, void *data)
153
DeviceClass *dc = DEVICE_CLASS(klass);
155
dc->realize = nvram_sysbus_realize;
156
dc->vmsd = &vmstate_nvram;
157
device_class_set_props(dc, nvram_sysbus_properties);
160
static const TypeInfo nvram_sysbus_info = {
161
.name = TYPE_DS1225Y,
162
.parent = TYPE_SYS_BUS_DEVICE,
163
.instance_size = sizeof(SysBusNvRamState),
164
.class_init = nvram_sysbus_class_init,
167
static void nvram_register_types(void)
169
type_register_static(&nvram_sysbus_info);
172
type_init(nvram_register_types)