]> git.proxmox.com Git - mirror_qemu.git/blame - hw/mac_nvram.c
Use OpenBIOS for g3bw machine
[mirror_qemu.git] / hw / mac_nvram.c
CommitLineData
3cbee15b
JM
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 */
87ecb68b 25#include "hw.h"
3cbee15b
JM
26#include "ppc_mac.h"
27
ea026b2f
BS
28/* debug NVR */
29//#define DEBUG_NVR
30
31#ifdef DEBUG_NVR
32#define NVR_DPRINTF(fmt, args...) \
33do { printf("NVR: " fmt , ##args); } while (0)
34#else
35#define NVR_DPRINTF(fmt, args...)
36#endif
37
3cbee15b 38struct MacIONVRAMState {
74e91155
JM
39 target_phys_addr_t size;
40 int mem_index;
3cbee15b
JM
41 uint8_t data[0x2000];
42};
43
44/* Direct access to NVRAM */
45uint32_t macio_nvram_read (void *opaque, uint32_t addr)
46{
47 MacIONVRAMState *s = opaque;
48 uint32_t ret;
49
3cbee15b
JM
50 if (addr < 0x2000)
51 ret = s->data[addr];
52 else
53 ret = -1;
ea026b2f 54 NVR_DPRINTF("read addr %04x val %x\n", addr, ret);
3cbee15b
JM
55
56 return ret;
57}
58
59void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val)
60{
61 MacIONVRAMState *s = opaque;
62
ea026b2f 63 NVR_DPRINTF("write addr %04x val %x\n", addr, val);
3cbee15b
JM
64 if (addr < 0x2000)
65 s->data[addr] = val;
66}
67
68/* macio style NVRAM device */
69static void macio_nvram_writeb (void *opaque,
70 target_phys_addr_t addr, uint32_t value)
71{
72 MacIONVRAMState *s = opaque;
74e91155 73
3cbee15b
JM
74 addr = (addr >> 4) & 0x1fff;
75 s->data[addr] = value;
ea026b2f 76 NVR_DPRINTF("writeb addr %04x val %x\n", (int)addr, value);
3cbee15b
JM
77}
78
79static 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];
ea026b2f 86 NVR_DPRINTF("readb addr %04x val %x\n", (int)addr, value);
3cbee15b
JM
87
88 return value;
89}
90
91static CPUWriteMemoryFunc *nvram_write[] = {
92 &macio_nvram_writeb,
93 &macio_nvram_writeb,
94 &macio_nvram_writeb,
95};
96
97static CPUReadMemoryFunc *nvram_read[] = {
98 &macio_nvram_readb,
99 &macio_nvram_readb,
100 &macio_nvram_readb,
101};
102
74e91155 103MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size)
3cbee15b
JM
104{
105 MacIONVRAMState *s;
74e91155 106
3cbee15b
JM
107 s = qemu_mallocz(sizeof(MacIONVRAMState));
108 if (!s)
109 return NULL;
74e91155
JM
110 s->size = size;
111 s->mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
112 *mem_index = s->mem_index;
3cbee15b
JM
113
114 return s;
115}
116
74e91155
JM
117void macio_nvram_map (void *opaque, target_phys_addr_t mem_base)
118{
119 MacIONVRAMState *s;
120
121 s = opaque;
74e91155
JM
122 cpu_register_physical_memory(mem_base, s->size, s->mem_index);
123}
124
3cbee15b
JM
125static 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 */
135void 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}