aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU NVRAM emulation for DS1225Y chip |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 3 | * |
Stefan Weil | bcc4e41 | 2011-12-02 10:30:41 +0100 | [diff] [blame] | 4 | * Copyright (c) 2007-2008 Hervé Poussineau |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 5 | * |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 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 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 25 | #include "hw/sysbus.h" |
Hervé Poussineau | d43ed9e | 2011-07-18 23:34:21 +0200 | [diff] [blame] | 26 | #include "trace.h" |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 27 | |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 28 | typedef struct { |
Avi Kivity | 871321a | 2011-11-10 11:24:24 +0200 | [diff] [blame] | 29 | MemoryRegion iomem; |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 30 | uint32_t chip_size; |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 31 | char *filename; |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 32 | FILE *file; |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 33 | uint8_t *contents; |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 34 | } NvRamState; |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 35 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 36 | static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size) |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 37 | { |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 38 | NvRamState *s = opaque; |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 39 | uint32_t val; |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 40 | |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 41 | val = s->contents[addr]; |
Hervé Poussineau | d43ed9e | 2011-07-18 23:34:21 +0200 | [diff] [blame] | 42 | trace_nvram_read(addr, val); |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 43 | return val; |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 46 | static void nvram_write(void *opaque, hwaddr addr, uint64_t val, |
Avi Kivity | 871321a | 2011-11-10 11:24:24 +0200 | [diff] [blame] | 47 | unsigned size) |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 48 | { |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 49 | NvRamState *s = opaque; |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 50 | |
Hervé Poussineau | d43ed9e | 2011-07-18 23:34:21 +0200 | [diff] [blame] | 51 | val &= 0xff; |
| 52 | trace_nvram_write(addr, s->contents[addr], val); |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 53 | |
Hervé Poussineau | d43ed9e | 2011-07-18 23:34:21 +0200 | [diff] [blame] | 54 | s->contents[addr] = val; |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 55 | if (s->file) { |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 56 | fseek(s->file, addr, SEEK_SET); |
| 57 | fputc(val, s->file); |
| 58 | fflush(s->file); |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Avi Kivity | 871321a | 2011-11-10 11:24:24 +0200 | [diff] [blame] | 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, |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 72 | static int nvram_post_load(void *opaque, int version_id) |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 73 | { |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 74 | NvRamState *s = opaque; |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 75 | |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 76 | /* Close file, as filename may has changed in load/store process */ |
| 77 | if (s->file) { |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 78 | fclose(s->file); |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 79 | } |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 80 | |
| 81 | /* Write back nvram contents */ |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 82 | s->file = fopen(s->filename, "wb"); |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 83 | if (s->file) { |
| 84 | /* Write back contents, as 'wb' mode cleaned the file */ |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 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); |
aurel32 | 02cb158 | 2008-03-13 19:23:00 +0000 | [diff] [blame] | 89 | } |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 90 | |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 91 | return 0; |
aurel32 | 30aa5c0 | 2008-03-13 01:19:15 +0000 | [diff] [blame] | 92 | } |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 93 | |
| 94 | static const VMStateDescription vmstate_nvram = { |
| 95 | .name = "nvram", |
| 96 | .version_id = 0, |
| 97 | .minimum_version_id = 0, |
| 98 | .minimum_version_id_old = 0, |
| 99 | .post_load = nvram_post_load, |
| 100 | .fields = (VMStateField[]) { |
| 101 | VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0, |
| 102 | vmstate_info_uint8, uint8_t), |
| 103 | VMSTATE_END_OF_LIST() |
| 104 | } |
| 105 | }; |
| 106 | |
Andreas Färber | 8c1892c | 2013-07-27 12:50:29 +0200 | [diff] [blame] | 107 | #define TYPE_DS1225Y "ds1225y" |
| 108 | #define DS1225Y(obj) OBJECT_CHECK(SysBusNvRamState, (obj), TYPE_DS1225Y) |
| 109 | |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 110 | typedef struct { |
Andreas Färber | 8c1892c | 2013-07-27 12:50:29 +0200 | [diff] [blame] | 111 | SysBusDevice parent_obj; |
| 112 | |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 113 | NvRamState nvram; |
| 114 | } SysBusNvRamState; |
| 115 | |
| 116 | static int nvram_sysbus_initfn(SysBusDevice *dev) |
| 117 | { |
Andreas Färber | 8c1892c | 2013-07-27 12:50:29 +0200 | [diff] [blame] | 118 | SysBusNvRamState *sys = DS1225Y(dev); |
| 119 | NvRamState *s = &sys->nvram; |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 120 | FILE *file; |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 121 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 122 | s->contents = g_malloc0(s->chip_size); |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 123 | |
Paolo Bonzini | eedfac6 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 124 | memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s, |
| 125 | "nvram", s->chip_size); |
Avi Kivity | 750ecd4 | 2011-11-27 11:38:10 +0200 | [diff] [blame] | 126 | sysbus_init_mmio(dev, &s->iomem); |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 127 | |
| 128 | /* Read current file */ |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 129 | file = fopen(s->filename, "rb"); |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 130 | if (file) { |
| 131 | /* Read nvram contents */ |
Juan Quintela | 3a23025 | 2011-09-13 14:41:18 +0200 | [diff] [blame] | 132 | if (fread(s->contents, s->chip_size, 1, file) != 1) { |
| 133 | printf("nvram_sysbus_initfn: short read\n"); |
| 134 | } |
| 135 | fclose(file); |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 136 | } |
| 137 | nvram_post_load(s, 0); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 142 | static Property nvram_sysbus_properties[] = { |
| 143 | DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000), |
| 144 | DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename), |
| 145 | DEFINE_PROP_END_OF_LIST(), |
| 146 | }; |
| 147 | |
| 148 | static void nvram_sysbus_class_init(ObjectClass *klass, void *data) |
| 149 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 150 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 151 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
| 152 | |
| 153 | k->init = nvram_sysbus_initfn; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 154 | dc->vmsd = &vmstate_nvram; |
| 155 | dc->props = nvram_sysbus_properties; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 156 | } |
| 157 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 158 | static const TypeInfo nvram_sysbus_info = { |
Andreas Färber | 8c1892c | 2013-07-27 12:50:29 +0200 | [diff] [blame] | 159 | .name = TYPE_DS1225Y, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 160 | .parent = TYPE_SYS_BUS_DEVICE, |
| 161 | .instance_size = sizeof(SysBusNvRamState), |
| 162 | .class_init = nvram_sysbus_class_init, |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 165 | static void nvram_register_types(void) |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 166 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 167 | type_register_static(&nvram_sysbus_info); |
Hervé Poussineau | cd3e240 | 2011-07-18 23:34:22 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 170 | type_init(nvram_register_types) |