Improve PPC device debugging
[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 "ppc_mac.h"
27
28 /* debug NVR */
29 //#define DEBUG_NVR
30
31 #ifdef DEBUG_NVR
32 #define NVR_DPRINTF(fmt, args...) \
33 do { printf("NVR: " fmt , ##args); } while (0)
34 #else
35 #define NVR_DPRINTF(fmt, args...)
36 #endif
37
38 struct MacIONVRAMState {
39     target_phys_addr_t size;
40     int mem_index;
41     uint8_t data[0x2000];
42 };
43
44 /* Direct access to NVRAM */
45 uint32_t macio_nvram_read (void *opaque, uint32_t addr)
46 {
47     MacIONVRAMState *s = opaque;
48     uint32_t ret;
49
50     if (addr < 0x2000)
51         ret = s->data[addr];
52     else
53         ret = -1;
54     NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
55
56     return ret;
57 }
58
59 void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val)
60 {
61     MacIONVRAMState *s = opaque;
62
63     NVR_DPRINTF("write addr %04x val %x\n", addr, val);
64     if (addr < 0x2000)
65         s->data[addr] = val;
66 }
67
68 /* macio style NVRAM device */
69 static void macio_nvram_writeb (void *opaque,
70                                 target_phys_addr_t addr, uint32_t value)
71 {
72     MacIONVRAMState *s = opaque;
73
74     addr = (addr >> 4) & 0x1fff;
75     s->data[addr] = value;
76     NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value);
77 }
78
79 static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
80 {
81     MacIONVRAMState *s = opaque;
82     uint32_t value;
83
84     addr = (addr >> 4) & 0x1fff;
85     value = s->data[addr];
86     NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
87
88     return value;
89 }
90
91 static CPUWriteMemoryFunc *nvram_write[] = {
92     &macio_nvram_writeb,
93     &macio_nvram_writeb,
94     &macio_nvram_writeb,
95 };
96
97 static CPUReadMemoryFunc *nvram_read[] = {
98     &macio_nvram_readb,
99     &macio_nvram_readb,
100     &macio_nvram_readb,
101 };
102
103 MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size)
104 {
105     MacIONVRAMState *s;
106
107     s = qemu_mallocz(sizeof(MacIONVRAMState));
108     if (!s)
109         return NULL;
110     s->size = size;
111     s->mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
112     *mem_index = s->mem_index;
113
114     return s;
115 }
116
117 void macio_nvram_map (void *opaque, target_phys_addr_t mem_base)
118 {
119     MacIONVRAMState *s;
120
121     s = opaque;
122     cpu_register_physical_memory(mem_base, s->size, s->mem_index);
123 }
124
125 static uint8_t nvram_chksum (const uint8_t *buf, int n)
126 {
127     int sum, i;
128     sum = 0;
129     for(i = 0; i < n; i++)
130         sum += buf[i];
131     return (sum & 0xff) + (sum >> 8);
132 }
133
134 /* set a free Mac OS NVRAM partition */
135 void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
136 {
137     uint8_t *buf;
138     char partition_name[12] = "wwwwwwwwwwww";
139
140     buf = nvr->data;
141     buf[0] = 0x7f; /* free partition magic */
142     buf[1] = 0; /* checksum */
143     buf[2] = len >> 8;
144     buf[3] = len;
145     memcpy(buf + 4, partition_name, 12);
146     buf[1] = nvram_chksum(buf, 16);
147 }