]> git.proxmox.com Git - mirror_qemu.git/blame - hw/nvram/ds1225y.c
Merge remote-tracking branch 'stefanha/block' into staging
[mirror_qemu.git] / hw / nvram / ds1225y.c
CommitLineData
30aa5c0d
AJ
1/*
2 * QEMU NVRAM emulation for DS1225Y chip
02cb1585 3 *
bcc4e41f 4 * Copyright (c) 2007-2008 Hervé Poussineau
02cb1585 5 *
30aa5c0d
AJ
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
83c9f4ca 25#include "hw/sysbus.h"
d43ed9ec 26#include "trace.h"
30aa5c0d 27
cd3e2409 28typedef struct {
871321ac 29 MemoryRegion iomem;
02cb1585 30 uint32_t chip_size;
cd3e2409 31 char *filename;
3a230256 32 FILE *file;
02cb1585 33 uint8_t *contents;
cd3e2409 34} NvRamState;
30aa5c0d 35
a8170e5e 36static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
30aa5c0d 37{
cd3e2409 38 NvRamState *s = opaque;
02cb1585
AJ
39 uint32_t val;
40
8da3ff18 41 val = s->contents[addr];
d43ed9ec 42 trace_nvram_read(addr, val);
02cb1585
AJ
43 return val;
44}
30aa5c0d 45
a8170e5e 46static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
871321ac 47 unsigned size)
30aa5c0d 48{
cd3e2409 49 NvRamState *s = opaque;
30aa5c0d 50
d43ed9ec
HP
51 val &= 0xff;
52 trace_nvram_write(addr, s->contents[addr], val);
02cb1585 53
d43ed9ec 54 s->contents[addr] = val;
02cb1585 55 if (s->file) {
3a230256
JQ
56 fseek(s->file, addr, SEEK_SET);
57 fputc(val, s->file);
58 fflush(s->file);
30aa5c0d
AJ
59 }
60}
61
871321ac
AK
62static 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,
30aa5c0d
AJ
70};
71
cd3e2409 72static int nvram_post_load(void *opaque, int version_id)
30aa5c0d 73{
cd3e2409
HP
74 NvRamState *s = opaque;
75
76 /* Close file, as filename may has changed in load/store process */
77 if (s->file) {
3a230256 78 fclose(s->file);
cd3e2409
HP
79 }
80
81 /* Write back nvram contents */
3a230256 82 s->file = fopen(s->filename, "wb");
cd3e2409
HP
83 if (s->file) {
84 /* Write back contents, as 'wb' mode cleaned the file */
3a230256
JQ
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);
cd3e2409
HP
89 }
90
91 return 0;
92}
93
94static 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
8c1892cf
AF
107#define TYPE_DS1225Y "ds1225y"
108#define DS1225Y(obj) OBJECT_CHECK(SysBusNvRamState, (obj), TYPE_DS1225Y)
109
cd3e2409 110typedef struct {
8c1892cf
AF
111 SysBusDevice parent_obj;
112
cd3e2409
HP
113 NvRamState nvram;
114} SysBusNvRamState;
115
116static int nvram_sysbus_initfn(SysBusDevice *dev)
117{
8c1892cf
AF
118 SysBusNvRamState *sys = DS1225Y(dev);
119 NvRamState *s = &sys->nvram;
3a230256 120 FILE *file;
30aa5c0d 121
7267c094 122 s->contents = g_malloc0(s->chip_size);
02cb1585 123
eedfac6f
PB
124 memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s,
125 "nvram", s->chip_size);
750ecd44 126 sysbus_init_mmio(dev, &s->iomem);
cd3e2409 127
02cb1585 128 /* Read current file */
3a230256 129 file = fopen(s->filename, "rb");
02cb1585
AJ
130 if (file) {
131 /* Read nvram contents */
3a230256
JQ
132 if (fread(s->contents, s->chip_size, 1, file) != 1) {
133 printf("nvram_sysbus_initfn: short read\n");
134 }
135 fclose(file);
02cb1585 136 }
cd3e2409 137 nvram_post_load(s, 0);
30aa5c0d 138
cd3e2409 139 return 0;
30aa5c0d 140}
cd3e2409 141
999e12bb
AL
142static 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
148static void nvram_sysbus_class_init(ObjectClass *klass, void *data)
149{
39bffca2 150 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
151 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
152
153 k->init = nvram_sysbus_initfn;
39bffca2
AL
154 dc->vmsd = &vmstate_nvram;
155 dc->props = nvram_sysbus_properties;
999e12bb
AL
156}
157
8c43a6f0 158static const TypeInfo nvram_sysbus_info = {
8c1892cf 159 .name = TYPE_DS1225Y,
39bffca2
AL
160 .parent = TYPE_SYS_BUS_DEVICE,
161 .instance_size = sizeof(SysBusNvRamState),
162 .class_init = nvram_sysbus_class_init,
cd3e2409
HP
163};
164
83f7d43a 165static void nvram_register_types(void)
cd3e2409 166{
39bffca2 167 type_register_static(&nvram_sysbus_info);
cd3e2409
HP
168}
169
83f7d43a 170type_init(nvram_register_types)