Fix PreP PCI IRQ mapping.
[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 "vl.h"
26 #include "ppc_mac.h"
27
28 struct MacIONVRAMState {
29     uint8_t data[0x2000];
30 };
31
32 /* Direct access to NVRAM */
33 uint32_t macio_nvram_read (void *opaque, uint32_t addr)
34 {
35     MacIONVRAMState *s = opaque;
36     uint32_t ret;
37
38     //    printf("%s: %p addr %04x\n", __func__, s, addr);
39     if (addr < 0x2000)
40         ret = s->data[addr];
41     else
42         ret = -1;
43
44     return ret;
45 }
46
47 void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val)
48 {
49     MacIONVRAMState *s = opaque;
50
51     //    printf("%s: %p addr %04x val %02x\n", __func__, s, addr, val);
52     if (addr < 0x2000)
53         s->data[addr] = val;
54 }
55
56 /* macio style NVRAM device */
57 static void macio_nvram_writeb (void *opaque,
58                                 target_phys_addr_t addr, uint32_t value)
59 {
60     MacIONVRAMState *s = opaque;
61     addr = (addr >> 4) & 0x1fff;
62     s->data[addr] = value;
63     //    printf("macio_nvram_writeb %04x = %02x\n", addr, value);
64 }
65
66 static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
67 {
68     MacIONVRAMState *s = opaque;
69     uint32_t value;
70
71     addr = (addr >> 4) & 0x1fff;
72     value = s->data[addr];
73     //    printf("macio_nvram_readb %04x = %02x\n", addr, value);
74
75     return value;
76 }
77
78 static CPUWriteMemoryFunc *nvram_write[] = {
79     &macio_nvram_writeb,
80     &macio_nvram_writeb,
81     &macio_nvram_writeb,
82 };
83
84 static CPUReadMemoryFunc *nvram_read[] = {
85     &macio_nvram_readb,
86     &macio_nvram_readb,
87     &macio_nvram_readb,
88 };
89
90 MacIONVRAMState *macio_nvram_init (int *mem_index)
91 {
92     MacIONVRAMState *s;
93     s = qemu_mallocz(sizeof(MacIONVRAMState));
94     if (!s)
95         return NULL;
96     *mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
97
98     return s;
99 }
100
101 static uint8_t nvram_chksum (const uint8_t *buf, int n)
102 {
103     int sum, i;
104     sum = 0;
105     for(i = 0; i < n; i++)
106         sum += buf[i];
107     return (sum & 0xff) + (sum >> 8);
108 }
109
110 /* set a free Mac OS NVRAM partition */
111 void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len)
112 {
113     uint8_t *buf;
114     char partition_name[12] = "wwwwwwwwwwww";
115
116     buf = nvr->data;
117     buf[0] = 0x7f; /* free partition magic */
118     buf[1] = 0; /* checksum */
119     buf[2] = len >> 8;
120     buf[3] = len;
121     memcpy(buf + 4, partition_name, 12);
122     buf[1] = nvram_chksum(buf, 16);
123 }