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
32 #define NVR_DPRINTF(fmt, args...) \
33 do { printf("NVR: " fmt , ##args); } while (0)
35 #define NVR_DPRINTF(fmt, args...)
38 struct MacIONVRAMState {
39 target_phys_addr_t size;
44 /* Direct access to NVRAM */
45 uint32_t macio_nvram_read (void *opaque, uint32_t addr)
47 MacIONVRAMState *s = opaque;
54 NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
59 void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val)
61 MacIONVRAMState *s = opaque;
63 NVR_DPRINTF("write addr %04x val %x\n", addr, val);
68 /* macio style NVRAM device */
69 static void macio_nvram_writeb (void *opaque,
70 target_phys_addr_t addr, uint32_t value)
72 MacIONVRAMState *s = opaque;
74 addr = (addr >> 4) & 0x1fff;
75 s->data[addr] = value;
76 NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value);
79 static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
81 MacIONVRAMState *s = opaque;
84 addr = (addr >> 4) & 0x1fff;
85 value = s->data[addr];
86 NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
91 static CPUWriteMemoryFunc *nvram_write[] = {
97 static CPUReadMemoryFunc *nvram_read[] = {
103 MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size)
107 s = qemu_mallocz(sizeof(MacIONVRAMState));
111 s->mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
112 *mem_index = s->mem_index;
117 void macio_nvram_map (void *opaque, target_phys_addr_t mem_base)
122 cpu_register_physical_memory(mem_base, s->size, s->mem_index);
125 static uint8_t nvram_chksum (const uint8_t *buf, int n)
129 for(i = 0; i < n; i++)
131 return (sum & 0xff) + (sum >> 8);
134 /* set a free Mac OS NVRAM partition */
135 void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
138 char partition_name[12] = "wwwwwwwwwwww";
141 buf[0] = 0x7f; /* free partition magic */
142 buf[1] = 0; /* checksum */
145 memcpy(buf + 4, partition_name, 12);
146 buf[1] = nvram_chksum(buf, 16);