SPARC64: introduce a convenience function for getting physical addresses
[qemu.git] / hw / mac_nvram.c
blob61e53d28b4d2bdaeec422f4b05d63e92a450ccf6
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.h"
26 #include "firmware_abi.h"
27 #include "sysemu.h"
28 #include "ppc_mac.h"
30 /* debug NVR */
31 //#define DEBUG_NVR
33 #ifdef DEBUG_NVR
34 #define NVR_DPRINTF(fmt, ...) \
35 do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0)
36 #else
37 #define NVR_DPRINTF(fmt, ...)
38 #endif
40 struct MacIONVRAMState {
41 uint32_t size;
42 int mem_index;
43 unsigned int it_shift;
44 uint8_t *data;
47 #define DEF_SYSTEM_SIZE 0xc10
49 /* Direct access to NVRAM */
50 uint32_t macio_nvram_read (void *opaque, uint32_t addr)
52 MacIONVRAMState *s = opaque;
53 uint32_t ret;
55 if (addr < s->size)
56 ret = s->data[addr];
57 else
58 ret = -1;
59 NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
61 return ret;
64 void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val)
66 MacIONVRAMState *s = opaque;
68 NVR_DPRINTF("write addr %04x val %x\n", addr, val);
69 if (addr < s->size)
70 s->data[addr] = val;
73 /* macio style NVRAM device */
74 static void macio_nvram_writeb (void *opaque,
75 target_phys_addr_t addr, uint32_t value)
77 MacIONVRAMState *s = opaque;
79 addr = (addr >> s->it_shift) & (s->size - 1);
80 s->data[addr] = value;
81 NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value);
84 static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
86 MacIONVRAMState *s = opaque;
87 uint32_t value;
89 addr = (addr >> s->it_shift) & (s->size - 1);
90 value = s->data[addr];
91 NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
93 return value;
96 static CPUWriteMemoryFunc * const nvram_write[] = {
97 &macio_nvram_writeb,
98 &macio_nvram_writeb,
99 &macio_nvram_writeb,
102 static CPUReadMemoryFunc * const nvram_read[] = {
103 &macio_nvram_readb,
104 &macio_nvram_readb,
105 &macio_nvram_readb,
108 static const VMStateDescription vmstate_macio_nvram = {
109 .name = "macio_nvram",
110 .version_id = 1,
111 .minimum_version_id = 1,
112 .minimum_version_id_old = 1,
113 .fields = (VMStateField[]) {
114 VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size),
115 VMSTATE_END_OF_LIST()
120 static void macio_nvram_reset(void *opaque)
124 MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size,
125 unsigned int it_shift)
127 MacIONVRAMState *s;
129 s = qemu_mallocz(sizeof(MacIONVRAMState));
130 s->data = qemu_mallocz(size);
131 s->size = size;
132 s->it_shift = it_shift;
134 s->mem_index = cpu_register_io_memory(nvram_read, nvram_write, s,
135 DEVICE_NATIVE_ENDIAN);
136 *mem_index = s->mem_index;
137 vmstate_register(NULL, -1, &vmstate_macio_nvram, s);
138 qemu_register_reset(macio_nvram_reset, s);
140 return s;
143 void macio_nvram_map (void *opaque, target_phys_addr_t mem_base)
145 MacIONVRAMState *s;
147 s = opaque;
148 cpu_register_physical_memory(mem_base, s->size << s->it_shift,
149 s->mem_index);
152 /* Set up a system OpenBIOS NVRAM partition */
153 void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
155 unsigned int i;
156 uint32_t start = 0, end;
157 struct OpenBIOS_nvpart_v1 *part_header;
159 // OpenBIOS nvram variables
160 // Variable partition
161 part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
162 part_header->signature = OPENBIOS_PART_SYSTEM;
163 pstrcpy(part_header->name, sizeof(part_header->name), "system");
165 end = start + sizeof(struct OpenBIOS_nvpart_v1);
166 for (i = 0; i < nb_prom_envs; i++)
167 end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]);
169 // End marker
170 nvr->data[end++] = '\0';
172 end = start + ((end - start + 15) & ~15);
173 /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
174 new variables. */
175 if (end < DEF_SYSTEM_SIZE)
176 end = DEF_SYSTEM_SIZE;
177 OpenBIOS_finish_partition(part_header, end - start);
179 // free partition
180 start = end;
181 part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
182 part_header->signature = OPENBIOS_PART_FREE;
183 pstrcpy(part_header->name, sizeof(part_header->name), "free");
185 end = len;
186 OpenBIOS_finish_partition(part_header, end - start);