root / hw / mac_nvram.c @ e219dea2
History | View | Annotate | Download (5.1 kB)
1 | 3cbee15b | j_mayer | /*
|
---|---|---|---|
2 | 3cbee15b | j_mayer | * PowerMac NVRAM emulation
|
3 | 3cbee15b | j_mayer | *
|
4 | 3cbee15b | j_mayer | * Copyright (c) 2005-2007 Fabrice Bellard
|
5 | 3cbee15b | j_mayer | * Copyright (c) 2007 Jocelyn Mayer
|
6 | 3cbee15b | j_mayer | *
|
7 | 3cbee15b | j_mayer | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | 3cbee15b | j_mayer | * of this software and associated documentation files (the "Software"), to deal
|
9 | 3cbee15b | j_mayer | * in the Software without restriction, including without limitation the rights
|
10 | 3cbee15b | j_mayer | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | 3cbee15b | j_mayer | * copies of the Software, and to permit persons to whom the Software is
|
12 | 3cbee15b | j_mayer | * furnished to do so, subject to the following conditions:
|
13 | 3cbee15b | j_mayer | *
|
14 | 3cbee15b | j_mayer | * The above copyright notice and this permission notice shall be included in
|
15 | 3cbee15b | j_mayer | * all copies or substantial portions of the Software.
|
16 | 3cbee15b | j_mayer | *
|
17 | 3cbee15b | j_mayer | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | 3cbee15b | j_mayer | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | 3cbee15b | j_mayer | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | 3cbee15b | j_mayer | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | 3cbee15b | j_mayer | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | 3cbee15b | j_mayer | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | 3cbee15b | j_mayer | * THE SOFTWARE.
|
24 | 3cbee15b | j_mayer | */
|
25 | 87ecb68b | pbrook | #include "hw.h" |
26 | 95efd11c | blueswir1 | #include "firmware_abi.h" |
27 | 95efd11c | blueswir1 | #include "sysemu.h" |
28 | 3cbee15b | j_mayer | #include "ppc_mac.h" |
29 | 3cbee15b | j_mayer | |
30 | ea026b2f | blueswir1 | /* debug NVR */
|
31 | ea026b2f | blueswir1 | //#define DEBUG_NVR
|
32 | ea026b2f | blueswir1 | |
33 | ea026b2f | blueswir1 | #ifdef DEBUG_NVR
|
34 | 001faf32 | Blue Swirl | #define NVR_DPRINTF(fmt, ...) \
|
35 | 001faf32 | Blue Swirl | do { printf("NVR: " fmt , ## __VA_ARGS__); } while (0) |
36 | ea026b2f | blueswir1 | #else
|
37 | 001faf32 | Blue Swirl | #define NVR_DPRINTF(fmt, ...)
|
38 | ea026b2f | blueswir1 | #endif
|
39 | ea026b2f | blueswir1 | |
40 | 3cbee15b | j_mayer | struct MacIONVRAMState {
|
41 | 8a11f43b | Juan Quintela | uint32_t size; |
42 | 23c5e4ca | Avi Kivity | MemoryRegion mem; |
43 | 68af3f24 | blueswir1 | unsigned int it_shift; |
44 | 3f7cbbbd | aurel32 | uint8_t *data; |
45 | 3cbee15b | j_mayer | }; |
46 | 3cbee15b | j_mayer | |
47 | bd89f43f | aurel32 | #define DEF_SYSTEM_SIZE 0xc10 |
48 | bd89f43f | aurel32 | |
49 | 3cbee15b | j_mayer | /* Direct access to NVRAM */
|
50 | 3cbee15b | j_mayer | uint32_t macio_nvram_read (void *opaque, uint32_t addr)
|
51 | 3cbee15b | j_mayer | { |
52 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
53 | 3cbee15b | j_mayer | uint32_t ret; |
54 | 3cbee15b | j_mayer | |
55 | 3f7cbbbd | aurel32 | if (addr < s->size)
|
56 | 3cbee15b | j_mayer | ret = s->data[addr]; |
57 | 3cbee15b | j_mayer | else
|
58 | 3cbee15b | j_mayer | ret = -1;
|
59 | ea026b2f | blueswir1 | NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
|
60 | 3cbee15b | j_mayer | |
61 | 3cbee15b | j_mayer | return ret;
|
62 | 3cbee15b | j_mayer | } |
63 | 3cbee15b | j_mayer | |
64 | 3cbee15b | j_mayer | void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val) |
65 | 3cbee15b | j_mayer | { |
66 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
67 | 3cbee15b | j_mayer | |
68 | ea026b2f | blueswir1 | NVR_DPRINTF("write addr %04x val %x\n", addr, val);
|
69 | 3f7cbbbd | aurel32 | if (addr < s->size)
|
70 | 3cbee15b | j_mayer | s->data[addr] = val; |
71 | 3cbee15b | j_mayer | } |
72 | 3cbee15b | j_mayer | |
73 | 3cbee15b | j_mayer | /* macio style NVRAM device */
|
74 | 23c5e4ca | Avi Kivity | static void macio_nvram_writeb(void *opaque, target_phys_addr_t addr, |
75 | 23c5e4ca | Avi Kivity | uint64_t value, unsigned size)
|
76 | 3cbee15b | j_mayer | { |
77 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
78 | 74e91155 | j_mayer | |
79 | 68af3f24 | blueswir1 | addr = (addr >> s->it_shift) & (s->size - 1);
|
80 | 3cbee15b | j_mayer | s->data[addr] = value; |
81 | ea026b2f | blueswir1 | NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value); |
82 | 3cbee15b | j_mayer | } |
83 | 3cbee15b | j_mayer | |
84 | 23c5e4ca | Avi Kivity | static uint64_t macio_nvram_readb(void *opaque, target_phys_addr_t addr, |
85 | 23c5e4ca | Avi Kivity | unsigned size)
|
86 | 3cbee15b | j_mayer | { |
87 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
88 | 3cbee15b | j_mayer | uint32_t value; |
89 | 3cbee15b | j_mayer | |
90 | 68af3f24 | blueswir1 | addr = (addr >> s->it_shift) & (s->size - 1);
|
91 | 3cbee15b | j_mayer | value = s->data[addr]; |
92 | ea026b2f | blueswir1 | NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value); |
93 | 3cbee15b | j_mayer | |
94 | 3cbee15b | j_mayer | return value;
|
95 | 3cbee15b | j_mayer | } |
96 | 3cbee15b | j_mayer | |
97 | 23c5e4ca | Avi Kivity | static const MemoryRegionOps macio_nvram_ops = { |
98 | 23c5e4ca | Avi Kivity | .read = macio_nvram_readb, |
99 | 23c5e4ca | Avi Kivity | .write = macio_nvram_writeb, |
100 | 23c5e4ca | Avi Kivity | .endianness = DEVICE_NATIVE_ENDIAN, |
101 | 3cbee15b | j_mayer | }; |
102 | 3cbee15b | j_mayer | |
103 | 8e470f8a | Juan Quintela | static const VMStateDescription vmstate_macio_nvram = { |
104 | 8e470f8a | Juan Quintela | .name = "macio_nvram",
|
105 | 8e470f8a | Juan Quintela | .version_id = 1,
|
106 | 8e470f8a | Juan Quintela | .minimum_version_id = 1,
|
107 | 8e470f8a | Juan Quintela | .minimum_version_id_old = 1,
|
108 | 8e470f8a | Juan Quintela | .fields = (VMStateField[]) { |
109 | 8e470f8a | Juan Quintela | VMSTATE_VBUFFER_UINT32(data, MacIONVRAMState, 0, NULL, 0, size), |
110 | 8e470f8a | Juan Quintela | VMSTATE_END_OF_LIST() |
111 | 8e470f8a | Juan Quintela | } |
112 | 8e470f8a | Juan Quintela | }; |
113 | 9b64997f | blueswir1 | |
114 | 9b64997f | blueswir1 | |
115 | 6e6b7363 | blueswir1 | static void macio_nvram_reset(void *opaque) |
116 | 6e6b7363 | blueswir1 | { |
117 | 6e6b7363 | blueswir1 | } |
118 | 6e6b7363 | blueswir1 | |
119 | 23c5e4ca | Avi Kivity | MacIONVRAMState *macio_nvram_init (target_phys_addr_t size, |
120 | 68af3f24 | blueswir1 | unsigned int it_shift) |
121 | 3cbee15b | j_mayer | { |
122 | 3cbee15b | j_mayer | MacIONVRAMState *s; |
123 | 74e91155 | j_mayer | |
124 | 7267c094 | Anthony Liguori | s = g_malloc0(sizeof(MacIONVRAMState));
|
125 | 7267c094 | Anthony Liguori | s->data = g_malloc0(size); |
126 | 74e91155 | j_mayer | s->size = size; |
127 | 68af3f24 | blueswir1 | s->it_shift = it_shift; |
128 | 3f7cbbbd | aurel32 | |
129 | 23c5e4ca | Avi Kivity | memory_region_init_io(&s->mem, &macio_nvram_ops, s, "macio-nvram",
|
130 | 23c5e4ca | Avi Kivity | size << it_shift); |
131 | 8e470f8a | Juan Quintela | vmstate_register(NULL, -1, &vmstate_macio_nvram, s); |
132 | a08d4367 | Jan Kiszka | qemu_register_reset(macio_nvram_reset, s); |
133 | 3cbee15b | j_mayer | |
134 | 3cbee15b | j_mayer | return s;
|
135 | 3cbee15b | j_mayer | } |
136 | 3cbee15b | j_mayer | |
137 | 23c5e4ca | Avi Kivity | void macio_nvram_setup_bar(MacIONVRAMState *s, MemoryRegion *bar,
|
138 | 23c5e4ca | Avi Kivity | target_phys_addr_t mem_base) |
139 | 74e91155 | j_mayer | { |
140 | 23c5e4ca | Avi Kivity | memory_region_add_subregion(bar, mem_base, &s->mem); |
141 | 74e91155 | j_mayer | } |
142 | 74e91155 | j_mayer | |
143 | 95efd11c | blueswir1 | /* Set up a system OpenBIOS NVRAM partition */
|
144 | 3cbee15b | j_mayer | void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len) |
145 | 3cbee15b | j_mayer | { |
146 | 95efd11c | blueswir1 | unsigned int i; |
147 | 95efd11c | blueswir1 | uint32_t start = 0, end;
|
148 | 95efd11c | blueswir1 | struct OpenBIOS_nvpart_v1 *part_header;
|
149 | 95efd11c | blueswir1 | |
150 | 95efd11c | blueswir1 | // OpenBIOS nvram variables
|
151 | 95efd11c | blueswir1 | // Variable partition
|
152 | 95efd11c | blueswir1 | part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
|
153 | 95efd11c | blueswir1 | part_header->signature = OPENBIOS_PART_SYSTEM; |
154 | 95efd11c | blueswir1 | pstrcpy(part_header->name, sizeof(part_header->name), "system"); |
155 | 95efd11c | blueswir1 | |
156 | 95efd11c | blueswir1 | end = start + sizeof(struct OpenBIOS_nvpart_v1); |
157 | 95efd11c | blueswir1 | for (i = 0; i < nb_prom_envs; i++) |
158 | 95efd11c | blueswir1 | end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]); |
159 | 95efd11c | blueswir1 | |
160 | 95efd11c | blueswir1 | // End marker
|
161 | 95efd11c | blueswir1 | nvr->data[end++] = '\0';
|
162 | 95efd11c | blueswir1 | |
163 | 95efd11c | blueswir1 | end = start + ((end - start + 15) & ~15); |
164 | bd89f43f | aurel32 | /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
|
165 | bd89f43f | aurel32 | new variables. */
|
166 | bd89f43f | aurel32 | if (end < DEF_SYSTEM_SIZE)
|
167 | bd89f43f | aurel32 | end = DEF_SYSTEM_SIZE; |
168 | 95efd11c | blueswir1 | OpenBIOS_finish_partition(part_header, end - start); |
169 | 95efd11c | blueswir1 | |
170 | 95efd11c | blueswir1 | // free partition
|
171 | 95efd11c | blueswir1 | start = end; |
172 | 95efd11c | blueswir1 | part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
|
173 | 95efd11c | blueswir1 | part_header->signature = OPENBIOS_PART_FREE; |
174 | 95efd11c | blueswir1 | pstrcpy(part_header->name, sizeof(part_header->name), "free"); |
175 | 95efd11c | blueswir1 | |
176 | 95efd11c | blueswir1 | end = len; |
177 | 95efd11c | blueswir1 | OpenBIOS_finish_partition(part_header, end - start); |
178 | 3cbee15b | j_mayer | } |