1 /* 2 * QEMU NVRAM emulation for DS1225Y chip 3 * 4 * Copyright (c) 2007-2008 Hervé Poussineau 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 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 22 * THE SOFTWARE. 23 */ 24 25 #include "hw/sysbus.h" 26 #include "trace.h" 27 28 typedef struct { 29 MemoryRegion iomem; 30 uint32_t chip_size; 31 char *filename; 32 FILE *file; 33 uint8_t *contents; 34 } NvRamState; 35 36 static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size) 37 { 38 NvRamState *s = opaque; 39 uint32_t val; 40 41 val = s->contents[addr]; 42 trace_nvram_read(addr, val); 43 return val; 44 } 45 46 static void nvram_write(void *opaque, hwaddr addr, uint64_t val, 47 unsigned size) 48 { 49 NvRamState *s = opaque; 50 51 val &= 0xff; 52 trace_nvram_write(addr, s->contents[addr], val); 53 54 s->contents[addr] = val; 55 if (s->file) { 56 fseek(s->file, addr, SEEK_SET); 57 fputc(val, s->file); 58 fflush(s->file); 59 } 60 } 61 62 static const MemoryRegionOps nvram_ops = { 63 .read = nvram_read, 64 .write = nvram_write, 65 .impl = { 66 .min_access_size = 1, 67 .max_access_size = 1, 68 }, 69 .endianness = DEVICE_LITTLE_ENDIAN, 70 }; 71 72 static int nvram_post_load(void *opaque, int version_id) 73 { 74 NvRamState *s = opaque; 75 76 /* Close file, as filename may has changed in load/store process */ 77 if (s->file) { 78 fclose(s->file); 79 } 80 81 /* Write back nvram contents */ 82 s->file = fopen(s->filename, "wb"); 83 if (s->file) { 84 /* Write back contents, as 'wb' mode cleaned the file */ 85 if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) { 86 printf("nvram_post_load: short write\n"); 87 } 88 fflush(s->file); 89 } 90 91 return 0; 92 } 93 94 static const VMStateDescription vmstate_nvram = { 95 .name = "nvram", 96 .version_id = 0, 97 .minimum_version_id = 0, 98 .post_load = nvram_post_load, 99 .fields = (VMStateField[]) { 100 VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0, 101 vmstate_info_uint8, uint8_t), 102 VMSTATE_END_OF_LIST() 103 } 104 }; 105 106 #define TYPE_DS1225Y "ds1225y" 107 #define DS1225Y(obj) OBJECT_CHECK(SysBusNvRamState, (obj), TYPE_DS1225Y) 108 109 typedef struct { 110 SysBusDevice parent_obj; 111 112 NvRamState nvram; 113 } SysBusNvRamState; 114 115 static int nvram_sysbus_initfn(SysBusDevice *dev) 116 { 117 SysBusNvRamState *sys = DS1225Y(dev); 118 NvRamState *s = &sys->nvram; 119 FILE *file; 120 121 s->contents = g_malloc0(s->chip_size); 122 123 memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s, 124 "nvram", s->chip_size); 125 sysbus_init_mmio(dev, &s->iomem); 126 127 /* Read current file */ 128 file = fopen(s->filename, "rb"); 129 if (file) { 130 /* Read nvram contents */ 131 if (fread(s->contents, s->chip_size, 1, file) != 1) { 132 printf("nvram_sysbus_initfn: short read\n"); 133 } 134 fclose(file); 135 } 136 nvram_post_load(s, 0); 137 138 return 0; 139 } 140 141 static Property nvram_sysbus_properties[] = { 142 DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000), 143 DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename), 144 DEFINE_PROP_END_OF_LIST(), 145 }; 146 147 static void nvram_sysbus_class_init(ObjectClass *klass, void *data) 148 { 149 DeviceClass *dc = DEVICE_CLASS(klass); 150 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 151 152 k->init = nvram_sysbus_initfn; 153 dc->vmsd = &vmstate_nvram; 154 dc->props = nvram_sysbus_properties; 155 } 156 157 static const TypeInfo nvram_sysbus_info = { 158 .name = TYPE_DS1225Y, 159 .parent = TYPE_SYS_BUS_DEVICE, 160 .instance_size = sizeof(SysBusNvRamState), 161 .class_init = nvram_sysbus_class_init, 162 }; 163 164 static void nvram_register_types(void) 165 { 166 type_register_static(&nvram_sysbus_info); 167 } 168 169 type_init(nvram_register_types) 170