target-ppc: fix PowerMAC NVRAM emulation
[qemu] / hw / mac_nvram.c
1 /*
2  * PowerMac NVRAM emulation
3  *
4  * Copyright (c) 2005-2007 Fabrice Bellard
5  * Copyright (c) 2007 Jocelyn Mayer
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  */
25 #include "hw.h"
26 #include "firmware_abi.h"
27 #include "sysemu.h"
28 #include "ppc_mac.h"
29
30 /* debug NVR */
31 //#define DEBUG_NVR
32
33 #ifdef DEBUG_NVR
34 #define NVR_DPRINTF(fmt, args...) \
35 do { printf("NVR: " fmt , ##args); } while (0)
36 #else
37 #define NVR_DPRINTF(fmt, args...)
38 #endif
39
40 struct MacIONVRAMState {
41     target_phys_addr_t size;
42     int mem_index;
43     uint8_t *data;
44 };
45
46 /* Direct access to NVRAM */
47 uint32_t macio_nvram_read (void *opaque, uint32_t addr)
48 {
49     MacIONVRAMState *s = opaque;
50     uint32_t ret;
51
52     if (addr < s->size)
53         ret = s->data[addr];
54     else
55         ret = -1;
56     NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
57
58     return ret;
59 }
60
61 void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val)
62 {
63     MacIONVRAMState *s = opaque;
64
65     NVR_DPRINTF("write addr %04x val %x\n", addr, val);
66     if (addr < s->size)
67         s->data[addr] = val;
68 }
69
70 /* macio style NVRAM device */
71 static void macio_nvram_writeb (void *opaque,
72                                 target_phys_addr_t addr, uint32_t value)
73 {
74     MacIONVRAMState *s = opaque;
75
76     addr = (addr >> 4) & (s->size - 1);
77     s->data[addr] = value;
78     NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value);
79 }
80
81 static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
82 {
83     MacIONVRAMState *s = opaque;
84     uint32_t value;
85
86     addr = (addr >> 4) & (s->size - 1);
87     value = s->data[addr];
88     NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
89
90     return value;
91 }
92
93 static CPUWriteMemoryFunc *nvram_write[] = {
94     &macio_nvram_writeb,
95     &macio_nvram_writeb,
96     &macio_nvram_writeb,
97 };
98
99 static CPUReadMemoryFunc *nvram_read[] = {
100     &macio_nvram_readb,
101     &macio_nvram_readb,
102     &macio_nvram_readb,
103 };
104
105 MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size)
106 {
107     MacIONVRAMState *s;
108
109     s = qemu_mallocz(sizeof(MacIONVRAMState));
110     if (!s)
111         return NULL;
112     s->data = qemu_mallocz(size);
113     if (!s->data) {
114         qemu_free(s);
115         return NULL;
116     }
117     s->size = size;
118
119     s->mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
120     *mem_index = s->mem_index;
121
122     return s;
123 }
124
125 void macio_nvram_map (void *opaque, target_phys_addr_t mem_base)
126 {
127     MacIONVRAMState *s;
128
129     s = opaque;
130     cpu_register_physical_memory(mem_base, s->size << 4, s->mem_index);
131 }
132
133 /* Set up a system OpenBIOS NVRAM partition */
134 void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
135 {
136     unsigned int i;
137     uint32_t start = 0, end;
138     struct OpenBIOS_nvpart_v1 *part_header;
139
140     // OpenBIOS nvram variables
141     // Variable partition
142     part_header = (struct OpenBIOS_nvpart_v1 *)nvr->data;
143     part_header->signature = OPENBIOS_PART_SYSTEM;
144     pstrcpy(part_header->name, sizeof(part_header->name), "system");
145
146     end = start + sizeof(struct OpenBIOS_nvpart_v1);
147     for (i = 0; i < nb_prom_envs; i++)
148         end = OpenBIOS_set_var(nvr->data, end, prom_envs[i]);
149
150     // End marker
151     nvr->data[end++] = '\0';
152
153     end = start + ((end - start + 15) & ~15);
154     OpenBIOS_finish_partition(part_header, end - start);
155
156     // free partition
157     start = end;
158     part_header = (struct OpenBIOS_nvpart_v1 *)&nvr->data[start];
159     part_header->signature = OPENBIOS_PART_FREE;
160     pstrcpy(part_header->name, sizeof(part_header->name), "free");
161
162     end = len;
163     OpenBIOS_finish_partition(part_header, end - start);
164 }