root / hw / mac_nvram.c @ 59d94130
History | View | Annotate | Download (5.3 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 | ea026b2f | blueswir1 | #define NVR_DPRINTF(fmt, args...) \
|
35 | ea026b2f | blueswir1 | do { printf("NVR: " fmt , ##args); } while (0) |
36 | ea026b2f | blueswir1 | #else
|
37 | ea026b2f | blueswir1 | #define NVR_DPRINTF(fmt, args...)
|
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 | 3f7cbbbd | aurel32 | uint8_t *data; |
44 | 3cbee15b | j_mayer | }; |
45 | 3cbee15b | j_mayer | |
46 | bd89f43f | aurel32 | #define DEF_SYSTEM_SIZE 0xc10 |
47 | bd89f43f | aurel32 | |
48 | 3cbee15b | j_mayer | /* Direct access to NVRAM */
|
49 | 3cbee15b | j_mayer | uint32_t macio_nvram_read (void *opaque, uint32_t addr)
|
50 | 3cbee15b | j_mayer | { |
51 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
52 | 3cbee15b | j_mayer | uint32_t ret; |
53 | 3cbee15b | j_mayer | |
54 | 3f7cbbbd | aurel32 | if (addr < s->size)
|
55 | 3cbee15b | j_mayer | ret = s->data[addr]; |
56 | 3cbee15b | j_mayer | else
|
57 | 3cbee15b | j_mayer | ret = -1;
|
58 | ea026b2f | blueswir1 | NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
|
59 | 3cbee15b | j_mayer | |
60 | 3cbee15b | j_mayer | return ret;
|
61 | 3cbee15b | j_mayer | } |
62 | 3cbee15b | j_mayer | |
63 | 3cbee15b | j_mayer | void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val) |
64 | 3cbee15b | j_mayer | { |
65 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
66 | 3cbee15b | j_mayer | |
67 | ea026b2f | blueswir1 | NVR_DPRINTF("write addr %04x val %x\n", addr, val);
|
68 | 3f7cbbbd | aurel32 | if (addr < s->size)
|
69 | 3cbee15b | j_mayer | s->data[addr] = val; |
70 | 3cbee15b | j_mayer | } |
71 | 3cbee15b | j_mayer | |
72 | 3cbee15b | j_mayer | /* macio style NVRAM device */
|
73 | 3cbee15b | j_mayer | static void macio_nvram_writeb (void *opaque, |
74 | 3cbee15b | j_mayer | target_phys_addr_t addr, uint32_t value) |
75 | 3cbee15b | j_mayer | { |
76 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
77 | 74e91155 | j_mayer | |
78 | 3f7cbbbd | aurel32 | addr = (addr >> 4) & (s->size - 1); |
79 | 3cbee15b | j_mayer | s->data[addr] = value; |
80 | ea026b2f | blueswir1 | NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value); |
81 | 3cbee15b | j_mayer | } |
82 | 3cbee15b | j_mayer | |
83 | 3cbee15b | j_mayer | static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr) |
84 | 3cbee15b | j_mayer | { |
85 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
86 | 3cbee15b | j_mayer | uint32_t value; |
87 | 3cbee15b | j_mayer | |
88 | 3f7cbbbd | aurel32 | addr = (addr >> 4) & (s->size - 1); |
89 | 3cbee15b | j_mayer | value = s->data[addr]; |
90 | ea026b2f | blueswir1 | NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value); |
91 | 3cbee15b | j_mayer | |
92 | 3cbee15b | j_mayer | return value;
|
93 | 3cbee15b | j_mayer | } |
94 | 3cbee15b | j_mayer | |
95 | 3cbee15b | j_mayer | static CPUWriteMemoryFunc *nvram_write[] = {
|
96 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
97 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
98 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
99 | 3cbee15b | j_mayer | }; |
100 | 3cbee15b | j_mayer | |
101 | 3cbee15b | j_mayer | static CPUReadMemoryFunc *nvram_read[] = {
|
102 | 3cbee15b | j_mayer | &macio_nvram_readb, |
103 | 3cbee15b | j_mayer | &macio_nvram_readb, |
104 | 3cbee15b | j_mayer | &macio_nvram_readb, |
105 | 3cbee15b | j_mayer | }; |
106 | 3cbee15b | j_mayer | |
107 | 9b64997f | blueswir1 | static void macio_nvram_save(QEMUFile *f, void *opaque) |
108 | 9b64997f | blueswir1 | { |
109 | 9b64997f | blueswir1 | MacIONVRAMState *s = (MacIONVRAMState *)opaque; |
110 | 9b64997f | blueswir1 | |
111 | 9b64997f | blueswir1 | qemu_put_buffer(f, s->data, s->size); |
112 | 9b64997f | blueswir1 | } |
113 | 9b64997f | blueswir1 | |
114 | 9b64997f | blueswir1 | static int macio_nvram_load(QEMUFile *f, void *opaque, int version_id) |
115 | 9b64997f | blueswir1 | { |
116 | 9b64997f | blueswir1 | MacIONVRAMState *s = (MacIONVRAMState *)opaque; |
117 | 9b64997f | blueswir1 | |
118 | 9b64997f | blueswir1 | if (version_id != 1) |
119 | 9b64997f | blueswir1 | return -EINVAL;
|
120 | 9b64997f | blueswir1 | |
121 | 9b64997f | blueswir1 | qemu_get_buffer(f, s->data, s->size); |
122 | 9b64997f | blueswir1 | |
123 | 9b64997f | blueswir1 | return 0; |
124 | 9b64997f | blueswir1 | } |
125 | 9b64997f | blueswir1 | |
126 | 6e6b7363 | blueswir1 | static void macio_nvram_reset(void *opaque) |
127 | 6e6b7363 | blueswir1 | { |
128 | 6e6b7363 | blueswir1 | } |
129 | 6e6b7363 | blueswir1 | |
130 | 74e91155 | j_mayer | MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size)
|
131 | 3cbee15b | j_mayer | { |
132 | 3cbee15b | j_mayer | MacIONVRAMState *s; |
133 | 74e91155 | j_mayer | |
134 | 3cbee15b | j_mayer | s = qemu_mallocz(sizeof(MacIONVRAMState));
|
135 | 3cbee15b | j_mayer | if (!s)
|
136 | 3cbee15b | j_mayer | return NULL; |
137 | 3f7cbbbd | aurel32 | s->data = qemu_mallocz(size); |
138 | 3f7cbbbd | aurel32 | if (!s->data) {
|
139 | 3f7cbbbd | aurel32 | qemu_free(s); |
140 | 3f7cbbbd | aurel32 | return NULL; |
141 | 3f7cbbbd | aurel32 | } |
142 | 74e91155 | j_mayer | s->size = size; |
143 | 3f7cbbbd | aurel32 | |
144 | 74e91155 | j_mayer | s->mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
|
145 | 74e91155 | j_mayer | *mem_index = s->mem_index; |
146 | 9b64997f | blueswir1 | register_savevm("macio_nvram", -1, 1, macio_nvram_save, macio_nvram_load, |
147 | 9b64997f | blueswir1 | s); |
148 | 6e6b7363 | blueswir1 | qemu_register_reset(macio_nvram_reset, s); |
149 | 6e6b7363 | blueswir1 | macio_nvram_reset(s); |
150 | 3cbee15b | j_mayer | |
151 | 3cbee15b | j_mayer | return s;
|
152 | 3cbee15b | j_mayer | } |
153 | 3cbee15b | j_mayer | |
154 | 74e91155 | j_mayer | void macio_nvram_map (void *opaque, target_phys_addr_t mem_base) |
155 | 74e91155 | j_mayer | { |
156 | 74e91155 | j_mayer | MacIONVRAMState *s; |
157 | 74e91155 | j_mayer | |
158 | 74e91155 | j_mayer | s = opaque; |
159 | 3f7cbbbd | aurel32 | cpu_register_physical_memory(mem_base, s->size << 4, s->mem_index);
|
160 | 74e91155 | j_mayer | } |
161 | 74e91155 | j_mayer | |
162 | 95efd11c | blueswir1 | /* Set up a system OpenBIOS NVRAM partition */
|
163 | 3cbee15b | j_mayer | void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len) |
164 | 3cbee15b | j_mayer | { |
165 | 95efd11c | blueswir1 | unsigned int i; |
166 | 95efd11c | blueswir1 | uint32_t start = 0, end;
|
167 | 95efd11c | blueswir1 | struct OpenBIOS_nvpart_v1 *part_header;
|
168 | 95efd11c | blueswir1 | |
169 | 95efd11c | blueswir1 | // OpenBIOS nvram variables
|
170 | 95efd11c | blueswir1 | // Variable partition
|
171 | 95efd11c | blueswir1 | part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
|
172 | 95efd11c | blueswir1 | part_header->signature = OPENBIOS_PART_SYSTEM; |
173 | 95efd11c | blueswir1 | pstrcpy(part_header->name, sizeof(part_header->name), "system"); |
174 | 95efd11c | blueswir1 | |
175 | 95efd11c | blueswir1 | end = start + sizeof(struct OpenBIOS_nvpart_v1); |
176 | 95efd11c | blueswir1 | for (i = 0; i < nb_prom_envs; i++) |
177 | 95efd11c | blueswir1 | end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]); |
178 | 95efd11c | blueswir1 | |
179 | 95efd11c | blueswir1 | // End marker
|
180 | 95efd11c | blueswir1 | nvr->data[end++] = '\0';
|
181 | 95efd11c | blueswir1 | |
182 | 95efd11c | blueswir1 | end = start + ((end - start + 15) & ~15); |
183 | bd89f43f | aurel32 | /* XXX: OpenBIOS is not able to grow up a partition. Leave some space for
|
184 | bd89f43f | aurel32 | new variables. */
|
185 | bd89f43f | aurel32 | if (end < DEF_SYSTEM_SIZE)
|
186 | bd89f43f | aurel32 | end = DEF_SYSTEM_SIZE; |
187 | 95efd11c | blueswir1 | OpenBIOS_finish_partition(part_header, end - start); |
188 | 95efd11c | blueswir1 | |
189 | 95efd11c | blueswir1 | // free partition
|
190 | 95efd11c | blueswir1 | start = end; |
191 | 95efd11c | blueswir1 | part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
|
192 | 95efd11c | blueswir1 | part_header->signature = OPENBIOS_PART_FREE; |
193 | 95efd11c | blueswir1 | pstrcpy(part_header->name, sizeof(part_header->name), "free"); |
194 | 95efd11c | blueswir1 | |
195 | 95efd11c | blueswir1 | end = len; |
196 | 95efd11c | blueswir1 | OpenBIOS_finish_partition(part_header, end - start); |
197 | 3cbee15b | j_mayer | } |