root / hw / mac_nvram.c @ 5439779e
History | View | Annotate | Download (3.8 kB)
1 | 3cbee15b | j_mayer | /*
|
---|---|---|---|
2 | 3cbee15b | j_mayer | * PowerMac NVRAM emulation
|
3 | 3cbee15b | j_mayer | *
|
4 | 3cbee15b | j_mayer | * Copyright (c) 2005-2007 Fabrice Bellard
|
5 | 3cbee15b | j_mayer | * Copyright (c) 2007 Jocelyn Mayer
|
6 | 3cbee15b | j_mayer | *
|
7 | 3cbee15b | j_mayer | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | 3cbee15b | j_mayer | * of this software and associated documentation files (the "Software"), to deal
|
9 | 3cbee15b | j_mayer | * in the Software without restriction, including without limitation the rights
|
10 | 3cbee15b | j_mayer | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | 3cbee15b | j_mayer | * copies of the Software, and to permit persons to whom the Software is
|
12 | 3cbee15b | j_mayer | * furnished to do so, subject to the following conditions:
|
13 | 3cbee15b | j_mayer | *
|
14 | 3cbee15b | j_mayer | * The above copyright notice and this permission notice shall be included in
|
15 | 3cbee15b | j_mayer | * all copies or substantial portions of the Software.
|
16 | 3cbee15b | j_mayer | *
|
17 | 3cbee15b | j_mayer | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | 3cbee15b | j_mayer | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | 3cbee15b | j_mayer | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | 3cbee15b | j_mayer | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | 3cbee15b | j_mayer | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | 3cbee15b | j_mayer | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | 3cbee15b | j_mayer | * THE SOFTWARE.
|
24 | 3cbee15b | j_mayer | */
|
25 | 87ecb68b | pbrook | #include "hw.h" |
26 | 3cbee15b | j_mayer | #include "ppc_mac.h" |
27 | 3cbee15b | j_mayer | |
28 | 3cbee15b | j_mayer | struct MacIONVRAMState {
|
29 | 74e91155 | j_mayer | target_phys_addr_t mem_base; |
30 | 74e91155 | j_mayer | target_phys_addr_t size; |
31 | 74e91155 | j_mayer | int mem_index;
|
32 | 3cbee15b | j_mayer | uint8_t data[0x2000];
|
33 | 3cbee15b | j_mayer | }; |
34 | 3cbee15b | j_mayer | |
35 | 3cbee15b | j_mayer | /* Direct access to NVRAM */
|
36 | 3cbee15b | j_mayer | uint32_t macio_nvram_read (void *opaque, uint32_t addr)
|
37 | 3cbee15b | j_mayer | { |
38 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
39 | 3cbee15b | j_mayer | uint32_t ret; |
40 | 3cbee15b | j_mayer | |
41 | 3cbee15b | j_mayer | // printf("%s: %p addr %04x\n", __func__, s, addr);
|
42 | 3cbee15b | j_mayer | if (addr < 0x2000) |
43 | 3cbee15b | j_mayer | ret = s->data[addr]; |
44 | 3cbee15b | j_mayer | else
|
45 | 3cbee15b | j_mayer | ret = -1;
|
46 | 3cbee15b | j_mayer | |
47 | 3cbee15b | j_mayer | return ret;
|
48 | 3cbee15b | j_mayer | } |
49 | 3cbee15b | j_mayer | |
50 | 3cbee15b | j_mayer | void macio_nvram_write (void *opaque, uint32_t addr, uint32_t val) |
51 | 3cbee15b | j_mayer | { |
52 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
53 | 3cbee15b | j_mayer | |
54 | 3cbee15b | j_mayer | // printf("%s: %p addr %04x val %02x\n", __func__, s, addr, val);
|
55 | 3cbee15b | j_mayer | if (addr < 0x2000) |
56 | 3cbee15b | j_mayer | s->data[addr] = val; |
57 | 3cbee15b | j_mayer | } |
58 | 3cbee15b | j_mayer | |
59 | 3cbee15b | j_mayer | /* macio style NVRAM device */
|
60 | 3cbee15b | j_mayer | static void macio_nvram_writeb (void *opaque, |
61 | 3cbee15b | j_mayer | target_phys_addr_t addr, uint32_t value) |
62 | 3cbee15b | j_mayer | { |
63 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
64 | 74e91155 | j_mayer | |
65 | 74e91155 | j_mayer | addr -= s->mem_base; |
66 | 3cbee15b | j_mayer | addr = (addr >> 4) & 0x1fff; |
67 | 3cbee15b | j_mayer | s->data[addr] = value; |
68 | 3cbee15b | j_mayer | // printf("macio_nvram_writeb %04x = %02x\n", addr, value);
|
69 | 3cbee15b | j_mayer | } |
70 | 3cbee15b | j_mayer | |
71 | 3cbee15b | j_mayer | static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr) |
72 | 3cbee15b | j_mayer | { |
73 | 3cbee15b | j_mayer | MacIONVRAMState *s = opaque; |
74 | 3cbee15b | j_mayer | uint32_t value; |
75 | 3cbee15b | j_mayer | |
76 | 74e91155 | j_mayer | addr -= s->mem_base; |
77 | 3cbee15b | j_mayer | addr = (addr >> 4) & 0x1fff; |
78 | 3cbee15b | j_mayer | value = s->data[addr]; |
79 | 3cbee15b | j_mayer | // printf("macio_nvram_readb %04x = %02x\n", addr, value);
|
80 | 3cbee15b | j_mayer | |
81 | 3cbee15b | j_mayer | return value;
|
82 | 3cbee15b | j_mayer | } |
83 | 3cbee15b | j_mayer | |
84 | 3cbee15b | j_mayer | static CPUWriteMemoryFunc *nvram_write[] = {
|
85 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
86 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
87 | 3cbee15b | j_mayer | &macio_nvram_writeb, |
88 | 3cbee15b | j_mayer | }; |
89 | 3cbee15b | j_mayer | |
90 | 3cbee15b | j_mayer | static CPUReadMemoryFunc *nvram_read[] = {
|
91 | 3cbee15b | j_mayer | &macio_nvram_readb, |
92 | 3cbee15b | j_mayer | &macio_nvram_readb, |
93 | 3cbee15b | j_mayer | &macio_nvram_readb, |
94 | 3cbee15b | j_mayer | }; |
95 | 3cbee15b | j_mayer | |
96 | 74e91155 | j_mayer | MacIONVRAMState *macio_nvram_init (int *mem_index, target_phys_addr_t size)
|
97 | 3cbee15b | j_mayer | { |
98 | 3cbee15b | j_mayer | MacIONVRAMState *s; |
99 | 74e91155 | j_mayer | |
100 | 3cbee15b | j_mayer | s = qemu_mallocz(sizeof(MacIONVRAMState));
|
101 | 3cbee15b | j_mayer | if (!s)
|
102 | 3cbee15b | j_mayer | return NULL; |
103 | 74e91155 | j_mayer | s->size = size; |
104 | 74e91155 | j_mayer | s->mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
|
105 | 74e91155 | j_mayer | *mem_index = s->mem_index; |
106 | 3cbee15b | j_mayer | |
107 | 3cbee15b | j_mayer | return s;
|
108 | 3cbee15b | j_mayer | } |
109 | 3cbee15b | j_mayer | |
110 | 74e91155 | j_mayer | void macio_nvram_map (void *opaque, target_phys_addr_t mem_base) |
111 | 74e91155 | j_mayer | { |
112 | 74e91155 | j_mayer | MacIONVRAMState *s; |
113 | 74e91155 | j_mayer | |
114 | 74e91155 | j_mayer | s = opaque; |
115 | 74e91155 | j_mayer | s->mem_base = mem_base; |
116 | 74e91155 | j_mayer | cpu_register_physical_memory(mem_base, s->size, s->mem_index); |
117 | 74e91155 | j_mayer | } |
118 | 74e91155 | j_mayer | |
119 | 3cbee15b | j_mayer | static uint8_t nvram_chksum (const uint8_t *buf, int n) |
120 | 3cbee15b | j_mayer | { |
121 | 3cbee15b | j_mayer | int sum, i;
|
122 | 3cbee15b | j_mayer | sum = 0;
|
123 | 3cbee15b | j_mayer | for(i = 0; i < n; i++) |
124 | 3cbee15b | j_mayer | sum += buf[i]; |
125 | 3cbee15b | j_mayer | return (sum & 0xff) + (sum >> 8); |
126 | 3cbee15b | j_mayer | } |
127 | 3cbee15b | j_mayer | |
128 | 3cbee15b | j_mayer | /* set a free Mac OS NVRAM partition */
|
129 | 3cbee15b | j_mayer | void pmac_format_nvram_partition (MacIONVRAMState *nvr, int len) |
130 | 3cbee15b | j_mayer | { |
131 | 3cbee15b | j_mayer | uint8_t *buf; |
132 | 3cbee15b | j_mayer | char partition_name[12] = "wwwwwwwwwwww"; |
133 | 3cbee15b | j_mayer | |
134 | 3cbee15b | j_mayer | buf = nvr->data; |
135 | 3cbee15b | j_mayer | buf[0] = 0x7f; /* free partition magic */ |
136 | 3cbee15b | j_mayer | buf[1] = 0; /* checksum */ |
137 | 3cbee15b | j_mayer | buf[2] = len >> 8; |
138 | 3cbee15b | j_mayer | buf[3] = len;
|
139 | 3cbee15b | j_mayer | memcpy(buf + 4, partition_name, 12); |
140 | 3cbee15b | j_mayer | buf[1] = nvram_chksum(buf, 16); |
141 | 3cbee15b | j_mayer | } |