root / hw / ds1225y.c @ 00c3a05b
History | View | Annotate | Download (4.2 kB)
1 | 30aa5c0d | aurel32 | /*
|
---|---|---|---|
2 | 30aa5c0d | aurel32 | * QEMU NVRAM emulation for DS1225Y chip
|
3 | 02cb1585 | aurel32 | *
|
4 | 02cb1585 | aurel32 | * Copyright (c) 2007-2008 Herv? Poussineau
|
5 | 02cb1585 | aurel32 | *
|
6 | 30aa5c0d | aurel32 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 30aa5c0d | aurel32 | * of this software and associated documentation files (the "Software"), to deal
|
8 | 30aa5c0d | aurel32 | * in the Software without restriction, including without limitation the rights
|
9 | 30aa5c0d | aurel32 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 30aa5c0d | aurel32 | * copies of the Software, and to permit persons to whom the Software is
|
11 | 30aa5c0d | aurel32 | * furnished to do so, subject to the following conditions:
|
12 | 30aa5c0d | aurel32 | *
|
13 | 30aa5c0d | aurel32 | * The above copyright notice and this permission notice shall be included in
|
14 | 30aa5c0d | aurel32 | * all copies or substantial portions of the Software.
|
15 | 30aa5c0d | aurel32 | *
|
16 | 30aa5c0d | aurel32 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 30aa5c0d | aurel32 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 30aa5c0d | aurel32 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 30aa5c0d | aurel32 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 30aa5c0d | aurel32 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 30aa5c0d | aurel32 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 30aa5c0d | aurel32 | * THE SOFTWARE.
|
23 | 30aa5c0d | aurel32 | */
|
24 | 30aa5c0d | aurel32 | |
25 | cd3e2409 | Hervé Poussineau | #include "sysbus.h" |
26 | d43ed9ec | Hervé Poussineau | #include "trace.h" |
27 | 30aa5c0d | aurel32 | |
28 | cd3e2409 | Hervé Poussineau | typedef struct { |
29 | cd3e2409 | Hervé Poussineau | DeviceState qdev; |
30 | 871321ac | Avi Kivity | MemoryRegion iomem; |
31 | 02cb1585 | aurel32 | uint32_t chip_size; |
32 | cd3e2409 | Hervé Poussineau | char *filename;
|
33 | 3a230256 | Juan Quintela | FILE *file; |
34 | 02cb1585 | aurel32 | uint8_t *contents; |
35 | cd3e2409 | Hervé Poussineau | } NvRamState; |
36 | 30aa5c0d | aurel32 | |
37 | 871321ac | Avi Kivity | static uint64_t nvram_read(void *opaque, target_phys_addr_t addr, unsigned size) |
38 | 30aa5c0d | aurel32 | { |
39 | cd3e2409 | Hervé Poussineau | NvRamState *s = opaque; |
40 | 02cb1585 | aurel32 | uint32_t val; |
41 | 02cb1585 | aurel32 | |
42 | 8da3ff18 | pbrook | val = s->contents[addr]; |
43 | d43ed9ec | Hervé Poussineau | trace_nvram_read(addr, val); |
44 | 02cb1585 | aurel32 | return val;
|
45 | 02cb1585 | aurel32 | } |
46 | 30aa5c0d | aurel32 | |
47 | 871321ac | Avi Kivity | static void nvram_write(void *opaque, target_phys_addr_t addr, uint64_t val, |
48 | 871321ac | Avi Kivity | unsigned size)
|
49 | 30aa5c0d | aurel32 | { |
50 | cd3e2409 | Hervé Poussineau | NvRamState *s = opaque; |
51 | 30aa5c0d | aurel32 | |
52 | d43ed9ec | Hervé Poussineau | val &= 0xff;
|
53 | d43ed9ec | Hervé Poussineau | trace_nvram_write(addr, s->contents[addr], val); |
54 | 02cb1585 | aurel32 | |
55 | d43ed9ec | Hervé Poussineau | s->contents[addr] = val; |
56 | 02cb1585 | aurel32 | if (s->file) {
|
57 | 3a230256 | Juan Quintela | fseek(s->file, addr, SEEK_SET); |
58 | 3a230256 | Juan Quintela | fputc(val, s->file); |
59 | 3a230256 | Juan Quintela | fflush(s->file); |
60 | 30aa5c0d | aurel32 | } |
61 | 30aa5c0d | aurel32 | } |
62 | 30aa5c0d | aurel32 | |
63 | 871321ac | Avi Kivity | static const MemoryRegionOps nvram_ops = { |
64 | 871321ac | Avi Kivity | .read = nvram_read, |
65 | 871321ac | Avi Kivity | .write = nvram_write, |
66 | 871321ac | Avi Kivity | .impl = { |
67 | 871321ac | Avi Kivity | .min_access_size = 1,
|
68 | 871321ac | Avi Kivity | .max_access_size = 1,
|
69 | 871321ac | Avi Kivity | }, |
70 | 871321ac | Avi Kivity | .endianness = DEVICE_LITTLE_ENDIAN, |
71 | 30aa5c0d | aurel32 | }; |
72 | 30aa5c0d | aurel32 | |
73 | cd3e2409 | Hervé Poussineau | static int nvram_post_load(void *opaque, int version_id) |
74 | 30aa5c0d | aurel32 | { |
75 | cd3e2409 | Hervé Poussineau | NvRamState *s = opaque; |
76 | cd3e2409 | Hervé Poussineau | |
77 | cd3e2409 | Hervé Poussineau | /* Close file, as filename may has changed in load/store process */
|
78 | cd3e2409 | Hervé Poussineau | if (s->file) {
|
79 | 3a230256 | Juan Quintela | fclose(s->file); |
80 | cd3e2409 | Hervé Poussineau | } |
81 | cd3e2409 | Hervé Poussineau | |
82 | cd3e2409 | Hervé Poussineau | /* Write back nvram contents */
|
83 | 3a230256 | Juan Quintela | s->file = fopen(s->filename, "wb");
|
84 | cd3e2409 | Hervé Poussineau | if (s->file) {
|
85 | cd3e2409 | Hervé Poussineau | /* Write back contents, as 'wb' mode cleaned the file */
|
86 | 3a230256 | Juan Quintela | if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) { |
87 | 3a230256 | Juan Quintela | printf("nvram_post_load: short write\n");
|
88 | 3a230256 | Juan Quintela | } |
89 | 3a230256 | Juan Quintela | fflush(s->file); |
90 | cd3e2409 | Hervé Poussineau | } |
91 | cd3e2409 | Hervé Poussineau | |
92 | cd3e2409 | Hervé Poussineau | return 0; |
93 | cd3e2409 | Hervé Poussineau | } |
94 | cd3e2409 | Hervé Poussineau | |
95 | cd3e2409 | Hervé Poussineau | static const VMStateDescription vmstate_nvram = { |
96 | cd3e2409 | Hervé Poussineau | .name = "nvram",
|
97 | cd3e2409 | Hervé Poussineau | .version_id = 0,
|
98 | cd3e2409 | Hervé Poussineau | .minimum_version_id = 0,
|
99 | cd3e2409 | Hervé Poussineau | .minimum_version_id_old = 0,
|
100 | cd3e2409 | Hervé Poussineau | .post_load = nvram_post_load, |
101 | cd3e2409 | Hervé Poussineau | .fields = (VMStateField[]) { |
102 | cd3e2409 | Hervé Poussineau | VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0,
|
103 | cd3e2409 | Hervé Poussineau | vmstate_info_uint8, uint8_t), |
104 | cd3e2409 | Hervé Poussineau | VMSTATE_END_OF_LIST() |
105 | cd3e2409 | Hervé Poussineau | } |
106 | cd3e2409 | Hervé Poussineau | }; |
107 | cd3e2409 | Hervé Poussineau | |
108 | cd3e2409 | Hervé Poussineau | typedef struct { |
109 | cd3e2409 | Hervé Poussineau | SysBusDevice busdev; |
110 | cd3e2409 | Hervé Poussineau | NvRamState nvram; |
111 | cd3e2409 | Hervé Poussineau | } SysBusNvRamState; |
112 | cd3e2409 | Hervé Poussineau | |
113 | cd3e2409 | Hervé Poussineau | static int nvram_sysbus_initfn(SysBusDevice *dev) |
114 | cd3e2409 | Hervé Poussineau | { |
115 | cd3e2409 | Hervé Poussineau | NvRamState *s = &FROM_SYSBUS(SysBusNvRamState, dev)->nvram; |
116 | 3a230256 | Juan Quintela | FILE *file; |
117 | 30aa5c0d | aurel32 | |
118 | 7267c094 | Anthony Liguori | s->contents = g_malloc0(s->chip_size); |
119 | 02cb1585 | aurel32 | |
120 | 871321ac | Avi Kivity | memory_region_init_io(&s->iomem, &nvram_ops, s, "nvram", s->chip_size);
|
121 | 750ecd44 | Avi Kivity | sysbus_init_mmio(dev, &s->iomem); |
122 | cd3e2409 | Hervé Poussineau | |
123 | 02cb1585 | aurel32 | /* Read current file */
|
124 | 3a230256 | Juan Quintela | file = fopen(s->filename, "rb");
|
125 | 02cb1585 | aurel32 | if (file) {
|
126 | 02cb1585 | aurel32 | /* Read nvram contents */
|
127 | 3a230256 | Juan Quintela | if (fread(s->contents, s->chip_size, 1, file) != 1) { |
128 | 3a230256 | Juan Quintela | printf("nvram_sysbus_initfn: short read\n");
|
129 | 3a230256 | Juan Quintela | } |
130 | 3a230256 | Juan Quintela | fclose(file); |
131 | 02cb1585 | aurel32 | } |
132 | cd3e2409 | Hervé Poussineau | nvram_post_load(s, 0);
|
133 | 30aa5c0d | aurel32 | |
134 | cd3e2409 | Hervé Poussineau | return 0; |
135 | 30aa5c0d | aurel32 | } |
136 | cd3e2409 | Hervé Poussineau | |
137 | cd3e2409 | Hervé Poussineau | static SysBusDeviceInfo nvram_sysbus_info = {
|
138 | cd3e2409 | Hervé Poussineau | .qdev.name = "ds1225y",
|
139 | cd3e2409 | Hervé Poussineau | .qdev.size = sizeof(SysBusNvRamState),
|
140 | cd3e2409 | Hervé Poussineau | .qdev.vmsd = &vmstate_nvram, |
141 | cd3e2409 | Hervé Poussineau | .init = nvram_sysbus_initfn, |
142 | cd3e2409 | Hervé Poussineau | .qdev.props = (Property[]) { |
143 | cd3e2409 | Hervé Poussineau | DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000), |
144 | cd3e2409 | Hervé Poussineau | DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename),
|
145 | cd3e2409 | Hervé Poussineau | DEFINE_PROP_END_OF_LIST(), |
146 | cd3e2409 | Hervé Poussineau | }, |
147 | cd3e2409 | Hervé Poussineau | }; |
148 | cd3e2409 | Hervé Poussineau | |
149 | cd3e2409 | Hervé Poussineau | static void nvram_register(void) |
150 | cd3e2409 | Hervé Poussineau | { |
151 | cd3e2409 | Hervé Poussineau | sysbus_register_withprop(&nvram_sysbus_info); |
152 | cd3e2409 | Hervé Poussineau | } |
153 | cd3e2409 | Hervé Poussineau | |
154 | cd3e2409 | Hervé Poussineau | device_init(nvram_register) |