root / hw / mac_nvram.c @ 5122b431
History | View | Annotate | Download (5.4 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 | 74e91155 | j_mayer | target_phys_addr_t size; |
42 | 74e91155 | j_mayer | int mem_index;
|
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 | 3cbee15b | j_mayer | static void macio_nvram_writeb (void *opaque, |
75 | 3cbee15b | j_mayer | target_phys_addr_t addr, uint32_t value) |
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 | 3cbee15b | j_mayer | static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr) |
85 | 3cbee15b | j_mayer | { |
86 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
87 | 3cbee15b | j_mayer | uint32_t value; |
88 | 3cbee15b | j_mayer | |
89 | 68af3f24 | blueswir1 | addr = (addr >> s->it_shift) & (s->size - 1);
|
90 | 3cbee15b | j_mayer | value = s->data[addr]; |
91 | ea026b2f | blueswir1 | NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value); |
92 | 3cbee15b | j_mayer | |
93 | 3cbee15b | j_mayer | return value;
|
94 | 3cbee15b | j_mayer | } |
95 | 3cbee15b | j_mayer | |
96 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const nvram_write[] = { |
97 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
98 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
99 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
100 | 3cbee15b | j_mayer | }; |
101 | 3cbee15b | j_mayer | |
102 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const nvram_read[] = { |
103 | 3cbee15b | j_mayer | &macio_nvram_readb, |
104 | 3cbee15b | j_mayer | &macio_nvram_readb, |
105 | 3cbee15b | j_mayer | &macio_nvram_readb, |
106 | 3cbee15b | j_mayer | }; |
107 | 3cbee15b | j_mayer | |
108 | 9b64997f | blueswir1 | static void macio_nvram_save(QEMUFile *f, void *opaque) |
109 | 9b64997f | blueswir1 | { |
110 | 9b64997f | blueswir1 | MacIONVRAMState *s = (MacIONVRAMState *)opaque; |
111 | 9b64997f | blueswir1 | |
112 | 9b64997f | blueswir1 | qemu_put_buffer(f, s->data, s->size); |
113 | 9b64997f | blueswir1 | } |
114 | 9b64997f | blueswir1 | |
115 | 9b64997f | blueswir1 | static int macio_nvram_load(QEMUFile *f, void *opaque, int version_id) |
116 | 9b64997f | blueswir1 | { |
117 | 9b64997f | blueswir1 | MacIONVRAMState *s = (MacIONVRAMState *)opaque; |
118 | 9b64997f | blueswir1 | |
119 | 9b64997f | blueswir1 | if (version_id != 1) |
120 | 9b64997f | blueswir1 | return -EINVAL;
|
121 | 9b64997f | blueswir1 | |
122 | 9b64997f | blueswir1 | qemu_get_buffer(f, s->data, s->size); |
123 | 9b64997f | blueswir1 | |
124 | 9b64997f | blueswir1 | return 0; |
125 | 9b64997f | blueswir1 | } |
126 | 9b64997f | blueswir1 | |
127 | 6e6b7363 | blueswir1 | static void macio_nvram_reset(void *opaque) |
128 | 6e6b7363 | blueswir1 | { |
129 | 6e6b7363 | blueswir1 | } |
130 | 6e6b7363 | blueswir1 | |
131 | 68af3f24 | blueswir1 | MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size,
|
132 | 68af3f24 | blueswir1 | unsigned int it_shift) |
133 | 3cbee15b | j_mayer | { |
134 | 3cbee15b | j_mayer | MacIONVRAMState *s; |
135 | 74e91155 | j_mayer | |
136 | 3cbee15b | j_mayer | s = qemu_mallocz(sizeof(MacIONVRAMState));
|
137 | 3f7cbbbd | aurel32 | s->data = qemu_mallocz(size); |
138 | 74e91155 | j_mayer | s->size = size; |
139 | 68af3f24 | blueswir1 | s->it_shift = it_shift; |
140 | 3f7cbbbd | aurel32 | |
141 | 1eed09cb | Avi Kivity | s->mem_index = cpu_register_io_memory(nvram_read, nvram_write, s); |
142 | 74e91155 | j_mayer | *mem_index = s->mem_index; |
143 | 9b64997f | blueswir1 | register_savevm("macio_nvram", -1, 1, macio_nvram_save, macio_nvram_load, |
144 | 9b64997f | blueswir1 | s); |
145 | a08d4367 | Jan Kiszka | qemu_register_reset(macio_nvram_reset, s); |
146 | 6e6b7363 | blueswir1 | macio_nvram_reset(s); |
147 | 3cbee15b | j_mayer | |
148 | 3cbee15b | j_mayer | return s;
|
149 | 3cbee15b | j_mayer | } |
150 | 3cbee15b | j_mayer | |
151 | 74e91155 | j_mayer | void macio_nvram_map (void *opaque, target_phys_addr_t mem_base) |
152 | 74e91155 | j_mayer | { |
153 | 74e91155 | j_mayer | MacIONVRAMState *s; |
154 | 74e91155 | j_mayer | |
155 | 74e91155 | j_mayer | s = opaque; |
156 | 68af3f24 | blueswir1 | cpu_register_physical_memory(mem_base, s->size << s->it_shift, |
157 | 68af3f24 | blueswir1 | s->mem_index); |
158 | 74e91155 | j_mayer | } |
159 | 74e91155 | j_mayer | |
160 | 95efd11c | blueswir1 | /* Set up a system OpenBIOS NVRAM partition */
|
161 | 3cbee15b | j_mayer | void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len) |
162 | 3cbee15b | j_mayer | { |
163 | 95efd11c | blueswir1 | unsigned int i; |
164 | 95efd11c | blueswir1 | uint32_t start = 0, end;
|
165 | 95efd11c | blueswir1 | struct OpenBIOS_nvpart_v1 *part_header;
|
166 | 95efd11c | blueswir1 | |
167 | 95efd11c | blueswir1 | // OpenBIOS nvram variables
|
168 | 95efd11c | blueswir1 | // Variable partition
|
169 | 95efd11c | blueswir1 | part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
|
170 | 95efd11c | blueswir1 | part_header->signature = OPENBIOS_PART_SYSTEM; |
171 | 95efd11c | blueswir1 | pstrcpy(part_header->name, sizeof(part_header->name), "system"); |
172 | 95efd11c | blueswir1 | |
173 | 95efd11c | blueswir1 | end = start + sizeof(struct OpenBIOS_nvpart_v1); |
174 | 95efd11c | blueswir1 | for (i = 0; i < nb_prom_envs; i++) |
175 | 95efd11c | blueswir1 | end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]); |
176 | 95efd11c | blueswir1 | |
177 | 95efd11c | blueswir1 | // End marker
|
178 | 95efd11c | blueswir1 | nvr->data[end++] = '\0';
|
179 | 95efd11c | blueswir1 | |
180 | 95efd11c | blueswir1 | end = start + ((end - start + 15) & ~15); |
181 | bd89f43f | aurel32 | /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
|
182 | bd89f43f | aurel32 | new variables. */
|
183 | bd89f43f | aurel32 | if (end < DEF_SYSTEM_SIZE)
|
184 | bd89f43f | aurel32 | end = DEF_SYSTEM_SIZE; |
185 | 95efd11c | blueswir1 | OpenBIOS_finish_partition(part_header, end - start); |
186 | 95efd11c | blueswir1 | |
187 | 95efd11c | blueswir1 | // free partition
|
188 | 95efd11c | blueswir1 | start = end; |
189 | 95efd11c | blueswir1 | part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
|
190 | 95efd11c | blueswir1 | part_header->signature = OPENBIOS_PART_FREE; |
191 | 95efd11c | blueswir1 | pstrcpy(part_header->name, sizeof(part_header->name), "free"); |
192 | 95efd11c | blueswir1 | |
193 | 95efd11c | blueswir1 | end = len; |
194 | 95efd11c | blueswir1 | OpenBIOS_finish_partition(part_header, end - start); |
195 | 3cbee15b | j_mayer | } |