hw/misc/edu: Convert to realize()
[qemu/ar7.git] / hw / nvram / mac_nvram.c
blob9f165664c610521486081e7c09c56471eca246a5
1 /*
2 * PowerMac NVRAM emulation
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include "hw/hw.h"
26 #include "hw/nvram/openbios_firmware_abi.h"
27 #include "sysemu/sysemu.h"
28 #include "hw/ppc/mac.h"
29 #include <zlib.h>
31 /* debug NVR */
32 //#define DEBUG_NVR
34 #ifdef DEBUG_NVR
35 #define NVR_DPRINTF(fmt, ...) \
36 do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0)
37 #else
38 #define NVR_DPRINTF(fmt, ...)
39 #endif
41 #define DEF_SYSTEM_SIZE 0xc10
43 /* macio style NVRAM device */
44 static void macio_nvram_writeb(void *opaque, hwaddr addr,
45 uint64_t value, unsigned size)
47 MacIONVRAMState *s = opaque;
49 addr = (addr >> s->it_shift) & (s->size - 1);
50 s->data[addr] = value;
51 NVR_DPRINTF("writeb addr %04" PHYS_PRIx " val %" PRIx64 "\n", addr, value);
54 static uint64_t macio_nvram_readb(void *opaque, hwaddr addr,
55 unsigned size)
57 MacIONVRAMState *s = opaque;
58 uint32_t value;
60 addr = (addr >> s->it_shift) & (s->size - 1);
61 value = s->data[addr];
62 NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
64 return value;
67 static const MemoryRegionOps macio_nvram_ops = {
68 .read = macio_nvram_readb,
69 .write = macio_nvram_writeb,
70 .valid.min_access_size = 1,
71 .valid.max_access_size = 4,
72 .impl.min_access_size = 1,
73 .impl.max_access_size = 1,
74 .endianness = DEVICE_BIG_ENDIAN,
77 static const VMStateDescription vmstate_macio_nvram = {
78 .name = "macio_nvram",
79 .version_id = 1,
80 .minimum_version_id = 1,
81 .fields = (VMStateField[]) {
82 VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size),
83 VMSTATE_END_OF_LIST()
88 static void macio_nvram_reset(DeviceState *dev)
92 static void macio_nvram_realizefn(DeviceState *dev, Error **errp)
94 SysBusDevice *d = SYS_BUS_DEVICE(dev);
95 MacIONVRAMState *s = MACIO_NVRAM(dev);
97 s->data = g_malloc0(s->size);
99 memory_region_init_io(&s->mem, OBJECT(s), &macio_nvram_ops, s,
100 "macio-nvram", s->size << s->it_shift);
101 sysbus_init_mmio(d, &s->mem);
104 static void macio_nvram_unrealizefn(DeviceState *dev, Error **errp)
106 MacIONVRAMState *s = MACIO_NVRAM(dev);
108 g_free(s->data);
111 static Property macio_nvram_properties[] = {
112 DEFINE_PROP_UINT32("size", MacIONVRAMState, size, 0),
113 DEFINE_PROP_UINT32("it_shift", MacIONVRAMState, it_shift, 0),
114 DEFINE_PROP_END_OF_LIST()
117 static void macio_nvram_class_init(ObjectClass *oc, void *data)
119 DeviceClass *dc = DEVICE_CLASS(oc);
121 dc->realize = macio_nvram_realizefn;
122 dc->unrealize = macio_nvram_unrealizefn;
123 dc->reset = macio_nvram_reset;
124 dc->vmsd = &vmstate_macio_nvram;
125 dc->props = macio_nvram_properties;
126 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
129 static const TypeInfo macio_nvram_type_info = {
130 .name = TYPE_MACIO_NVRAM,
131 .parent = TYPE_SYS_BUS_DEVICE,
132 .instance_size = sizeof(MacIONVRAMState),
133 .class_init = macio_nvram_class_init,
136 static void macio_nvram_register_types(void)
138 type_register_static(&macio_nvram_type_info);
141 /* Set up a system OpenBIOS NVRAM partition */
142 static void pmac_format_nvram_partition_of(MacIONVRAMState *nvr, int off,
143 int len)
145 unsigned int i;
146 uint32_t start = off, end;
147 struct OpenBIOS_nvpart_v1 *part_header;
149 // OpenBIOS nvram variables
150 // Variable partition
151 part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
152 part_header->signature = OPENBIOS_PART_SYSTEM;
153 pstrcpy(part_header->name, sizeof(part_header->name), "system");
155 end = start + sizeof(struct OpenBIOS_nvpart_v1);
156 for (i = 0; i < nb_prom_envs; i++)
157 end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]);
159 // End marker
160 nvr->data[end++] = '\0';
162 end = start + ((end - start + 15) & ~15);
163 /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
164 new variables. */
165 if (end < DEF_SYSTEM_SIZE)
166 end = DEF_SYSTEM_SIZE;
167 OpenBIOS_finish_partition(part_header, end - start);
169 // free partition
170 start = end;
171 part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
172 part_header->signature = OPENBIOS_PART_FREE;
173 pstrcpy(part_header->name, sizeof(part_header->name), "free");
175 end = len;
176 OpenBIOS_finish_partition(part_header, end - start);
179 #define OSX_NVRAM_SIGNATURE (0x5A)
181 /* Set up a Mac OS X NVRAM partition */
182 static void pmac_format_nvram_partition_osx(MacIONVRAMState *nvr, int off,
183 int len)
185 uint32_t start = off;
186 struct OpenBIOS_nvpart_v1 *part_header;
187 unsigned char *data = &nvr->data[start];
189 /* empty partition */
190 part_header = (struct OpenBIOS_nvpart_v1 *)data;
191 part_header->signature = OSX_NVRAM_SIGNATURE;
192 pstrcpy(part_header->name, sizeof(part_header->name), "wwwwwwwwwwww");
194 OpenBIOS_finish_partition(part_header, len);
196 /* Generation */
197 stl_be_p(&data[20], 2);
199 /* Adler32 checksum */
200 stl_be_p(&data[16], adler32(0, &data[20], len - 20));
203 /* Set up NVRAM with OF and OSX partitions */
204 void pmac_format_nvram_partition(MacIONVRAMState *nvr, int len)
207 * Mac OS X expects side "B" of the flash at the second half of NVRAM,
208 * so we use half of the chip for OF and the other half for a free OSX
209 * partition.
211 pmac_format_nvram_partition_of(nvr, 0, len / 2);
212 pmac_format_nvram_partition_osx(nvr, len / 2, len / 2);
214 type_init(macio_nvram_register_types)