]> git.proxmox.com Git - mirror_qemu.git/blame - hw/nvram/ds1225y.c
Merge remote-tracking branch 'remotes/kraxel/tags/input-20190513-v2-pull-request...
[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
0430891c 25#include "qemu/osdep.h"
83c9f4ca 26#include "hw/sysbus.h"
d43ed9ec 27#include "trace.h"
296097f7 28#include "qemu/error-report.h"
30aa5c0d 29
cd3e2409 30typedef struct {
871321ac 31 MemoryRegion iomem;
02cb1585 32 uint32_t chip_size;
cd3e2409 33 char *filename;
3a230256 34 FILE *file;
02cb1585 35 uint8_t *contents;
cd3e2409 36} NvRamState;
30aa5c0d 37
a8170e5e 38static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
30aa5c0d 39{
cd3e2409 40 NvRamState *s = opaque;
02cb1585
AJ
41 uint32_t val;
42
8da3ff18 43 val = s->contents[addr];
d43ed9ec 44 trace_nvram_read(addr, val);
02cb1585
AJ
45 return val;
46}
30aa5c0d 47
a8170e5e 48static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
871321ac 49 unsigned size)
30aa5c0d 50{
cd3e2409 51 NvRamState *s = opaque;
30aa5c0d 52
d43ed9ec
HP
53 val &= 0xff;
54 trace_nvram_write(addr, s->contents[addr], val);
02cb1585 55
d43ed9ec 56 s->contents[addr] = val;
02cb1585 57 if (s->file) {
3a230256
JQ
58 fseek(s->file, addr, SEEK_SET);
59 fputc(val, s->file);
60 fflush(s->file);
30aa5c0d
AJ
61 }
62}
63
871321ac
AK
64static const MemoryRegionOps nvram_ops = {
65 .read = nvram_read,
66 .write = nvram_write,
67 .impl = {
68 .min_access_size = 1,
69 .max_access_size = 1,
70 },
71 .endianness = DEVICE_LITTLE_ENDIAN,
30aa5c0d
AJ
72};
73
cd3e2409 74static int nvram_post_load(void *opaque, int version_id)
30aa5c0d 75{
cd3e2409
HP
76 NvRamState *s = opaque;
77
78 /* Close file, as filename may has changed in load/store process */
79 if (s->file) {
3a230256 80 fclose(s->file);
cd3e2409
HP
81 }
82
83 /* Write back nvram contents */
b7438458 84 s->file = s->filename ? fopen(s->filename, "wb") : NULL;
cd3e2409
HP
85 if (s->file) {
86 /* Write back contents, as 'wb' mode cleaned the file */
3a230256
JQ
87 if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {
88 printf("nvram_post_load: short write\n");
89 }
90 fflush(s->file);
cd3e2409
HP
91 }
92
93 return 0;
94}
95
96static const VMStateDescription vmstate_nvram = {
97 .name = "nvram",
98 .version_id = 0,
99 .minimum_version_id = 0,
cd3e2409
HP
100 .post_load = nvram_post_load,
101 .fields = (VMStateField[]) {
102 VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0,
103 vmstate_info_uint8, uint8_t),
104 VMSTATE_END_OF_LIST()
105 }
106};
107
8c1892cf
AF
108#define TYPE_DS1225Y "ds1225y"
109#define DS1225Y(obj) OBJECT_CHECK(SysBusNvRamState, (obj), TYPE_DS1225Y)
110
cd3e2409 111typedef struct {
8c1892cf
AF
112 SysBusDevice parent_obj;
113
cd3e2409
HP
114 NvRamState nvram;
115} SysBusNvRamState;
116
296097f7 117static void nvram_sysbus_realize(DeviceState *dev, Error **errp)
cd3e2409 118{
8c1892cf
AF
119 SysBusNvRamState *sys = DS1225Y(dev);
120 NvRamState *s = &sys->nvram;
3a230256 121 FILE *file;
30aa5c0d 122
7267c094 123 s->contents = g_malloc0(s->chip_size);
02cb1585 124
eedfac6f
PB
125 memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s,
126 "nvram", s->chip_size);
296097f7 127 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
cd3e2409 128
02cb1585 129 /* Read current file */
b7438458 130 file = s->filename ? fopen(s->filename, "rb") : NULL;
02cb1585
AJ
131 if (file) {
132 /* Read nvram contents */
3a230256 133 if (fread(s->contents, s->chip_size, 1, file) != 1) {
296097f7 134 error_report("nvram_sysbus_realize: short read");
3a230256
JQ
135 }
136 fclose(file);
02cb1585 137 }
cd3e2409 138 nvram_post_load(s, 0);
30aa5c0d 139}
cd3e2409 140
999e12bb
AL
141static 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
147static void nvram_sysbus_class_init(ObjectClass *klass, void *data)
148{
39bffca2 149 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 150
296097f7 151 dc->realize = nvram_sysbus_realize;
39bffca2
AL
152 dc->vmsd = &vmstate_nvram;
153 dc->props = nvram_sysbus_properties;
999e12bb
AL
154}
155
8c43a6f0 156static const TypeInfo nvram_sysbus_info = {
8c1892cf 157 .name = TYPE_DS1225Y,
39bffca2
AL
158 .parent = TYPE_SYS_BUS_DEVICE,
159 .instance_size = sizeof(SysBusNvRamState),
160 .class_init = nvram_sysbus_class_init,
cd3e2409
HP
161};
162
83f7d43a 163static void nvram_register_types(void)
cd3e2409 164{
39bffca2 165 type_register_static(&nvram_sysbus_info);
cd3e2409
HP
166}
167
83f7d43a 168type_init(nvram_register_types)