Rev 1755 | Rev 1771 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1755 | Rev 1764 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Martin Decky |
2 | * Copyright (C) 2005 Martin Decky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include "ofw.h" |
29 | #include "ofw.h" |
30 | #include "asm.h" |
30 | #include <printf.h> |
31 | #include "printf.h" |
31 | #include <asm.h> |
32 | 32 | ||
33 | #define MAX_OFW_ARGS 10 |
33 | #define MAX_OFW_ARGS 10 |
34 | #define BUF_SIZE 1024 |
34 | #define BUF_SIZE 1024 |
35 | 35 | ||
36 | typedef unsigned int ofw_arg_t; |
36 | typedef unsigned int ofw_arg_t; |
37 | typedef unsigned int ihandle; |
37 | typedef unsigned int ihandle; |
38 | typedef unsigned int phandle; |
38 | typedef unsigned int phandle; |
39 | 39 | ||
40 | /** OpenFirmware command structure |
40 | /** OpenFirmware command structure |
41 | * |
41 | * |
42 | */ |
42 | */ |
43 | typedef struct { |
43 | typedef struct { |
44 | const char *service; /**< Command name */ |
44 | const char *service; /**< Command name */ |
45 | unsigned int nargs; /**< Number of in arguments */ |
45 | unsigned int nargs; /**< Number of in arguments */ |
46 | unsigned int nret; /**< Number of out arguments */ |
46 | unsigned int nret; /**< Number of out arguments */ |
47 | ofw_arg_t args[MAX_OFW_ARGS]; /**< List of arguments */ |
47 | ofw_arg_t args[MAX_OFW_ARGS]; /**< List of arguments */ |
48 | } ofw_args_t; |
48 | } ofw_args_t; |
49 | 49 | ||
50 | typedef void (*ofw_entry)(ofw_args_t *); |
50 | typedef void (*ofw_entry)(ofw_args_t *); |
51 | 51 | ||
52 | 52 | ||
53 | typedef struct { |
53 | typedef struct { |
54 | unsigned int info; |
54 | unsigned int info; |
55 | unsigned int addr_hi; |
55 | unsigned int addr_hi; |
56 | unsigned int addr_lo; |
56 | unsigned int addr_lo; |
57 | } pci_addr_t; |
57 | } pci_addr_t; |
58 | 58 | ||
59 | typedef struct { |
59 | typedef struct { |
60 | pci_addr_t addr; |
60 | pci_addr_t addr; |
61 | unsigned int size_hi; |
61 | unsigned int size_hi; |
62 | unsigned int size_lo; |
62 | unsigned int size_lo; |
63 | } pci_reg_t; |
63 | } pci_reg_t; |
64 | 64 | ||
65 | 65 | ||
66 | ofw_entry ofw; |
66 | ofw_entry ofw; |
67 | 67 | ||
68 | phandle ofw_chosen; |
68 | phandle ofw_chosen; |
69 | ihandle ofw_stdout; |
69 | ihandle ofw_stdout; |
70 | phandle ofw_root; |
70 | phandle ofw_root; |
71 | ihandle ofw_mmu; |
71 | ihandle ofw_mmu; |
72 | phandle ofw_memory; |
72 | phandle ofw_memory; |
73 | phandle ofw_aliases; |
73 | phandle ofw_aliases; |
74 | 74 | ||
75 | - | ||
76 | static int ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...) |
75 | static int ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...) |
77 | { |
76 | { |
78 | va_list list; |
77 | va_list list; |
79 | ofw_args_t args; |
78 | ofw_args_t args; |
80 | int i; |
79 | int i; |
81 | 80 | ||
82 | args.service = service; |
81 | args.service = service; |
83 | args.nargs = nargs; |
82 | args.nargs = nargs; |
84 | args.nret = nret; |
83 | args.nret = nret; |
85 | 84 | ||
86 | va_start(list, rets); |
85 | va_start(list, rets); |
87 | for (i = 0; i < nargs; i++) |
86 | for (i = 0; i < nargs; i++) |
88 | args.args[i] = va_arg(list, ofw_arg_t); |
87 | args.args[i] = va_arg(list, ofw_arg_t); |
89 | va_end(list); |
88 | va_end(list); |
90 | 89 | ||
91 | for (i = 0; i < nret; i++) |
90 | for (i = 0; i < nret; i++) |
92 | args.args[i + nargs] = 0; |
91 | args.args[i + nargs] = 0; |
93 | 92 | ||
94 | ofw(&args); |
93 | ofw(&args); |
95 | 94 | ||
96 | for (i = 1; i < nret; i++) |
95 | for (i = 1; i < nret; i++) |
97 | rets[i - 1] = args.args[i + nargs]; |
96 | rets[i - 1] = args.args[i + nargs]; |
98 | 97 | ||
99 | return args.args[nargs]; |
98 | return args.args[nargs]; |
100 | } |
99 | } |
101 | 100 | ||
102 | 101 | ||
103 | static phandle ofw_find_device(const char *name) |
102 | static phandle ofw_find_device(const char *name) |
104 | { |
103 | { |
105 | return ofw_call("finddevice", 1, 1, NULL, name); |
104 | return ofw_call("finddevice", 1, 1, NULL, name); |
106 | } |
105 | } |
107 | 106 | ||
108 | 107 | ||
109 | static int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen) |
108 | static int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen) |
110 | { |
109 | { |
111 | return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen); |
110 | return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen); |
112 | } |
111 | } |
113 | 112 | ||
114 | 113 | ||
115 | static unsigned int ofw_get_address_cells(const phandle device) |
114 | static unsigned int ofw_get_address_cells(const phandle device) |
116 | { |
115 | { |
117 | unsigned int ret; |
116 | unsigned int ret; |
118 | 117 | ||
119 | if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0) |
118 | if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0) |
120 | if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0) |
119 | if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0) |
121 | ret = 1; |
120 | ret = 1; |
122 | 121 | ||
123 | return ret; |
122 | return ret; |
124 | } |
123 | } |
125 | 124 | ||
126 | 125 | ||
127 | static unsigned int ofw_get_size_cells(const phandle device) |
126 | static unsigned int ofw_get_size_cells(const phandle device) |
128 | { |
127 | { |
129 | unsigned int ret; |
128 | unsigned int ret; |
130 | 129 | ||
131 | if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0) |
130 | if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0) |
132 | if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0) |
131 | if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0) |
133 | ret = 1; |
132 | ret = 1; |
134 | 133 | ||
135 | return ret; |
134 | return ret; |
136 | } |
135 | } |
137 | 136 | ||
138 | 137 | ||
139 | static ihandle ofw_open(const char *name) |
138 | static ihandle ofw_open(const char *name) |
140 | { |
139 | { |
141 | return ofw_call("open", 1, 1, NULL, name); |
140 | return ofw_call("open", 1, 1, NULL, name); |
142 | } |
141 | } |
143 | 142 | ||
144 | 143 | ||
145 | void init(void) |
144 | void init(void) |
146 | { |
145 | { |
147 | ofw_chosen = ofw_find_device("/chosen"); |
146 | ofw_chosen = ofw_find_device("/chosen"); |
148 | if (ofw_chosen == -1) |
147 | if (ofw_chosen == -1) |
149 | halt(); |
148 | halt(); |
150 | 149 | ||
151 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0) |
150 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0) |
152 | ofw_stdout = 0; |
151 | ofw_stdout = 0; |
153 | 152 | ||
154 | ofw_root = ofw_find_device("/"); |
153 | ofw_root = ofw_find_device("/"); |
155 | if (ofw_root == -1) { |
154 | if (ofw_root == -1) { |
156 | puts("\r\nError: Unable to find / device, halted.\r\n"); |
155 | puts("\r\nError: Unable to find / device, halted.\r\n"); |
157 | halt(); |
156 | halt(); |
158 | } |
157 | } |
159 | 158 | ||
160 | if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu, sizeof(ofw_mmu)) <= 0) { |
159 | if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu, sizeof(ofw_mmu)) <= 0) { |
161 | puts("\r\nError: Unable to get mmu property, halted.\r\n"); |
160 | puts("\r\nError: Unable to get mmu property, halted.\r\n"); |
162 | halt(); |
161 | halt(); |
163 | } |
162 | } |
164 | 163 | ||
165 | ofw_memory = ofw_find_device("/memory"); |
164 | ofw_memory = ofw_find_device("/memory"); |
166 | if (ofw_memory == -1) { |
165 | if (ofw_memory == -1) { |
167 | puts("\r\nError: Unable to find /memory device, halted.\r\n"); |
166 | puts("\r\nError: Unable to find /memory device, halted.\r\n"); |
168 | halt(); |
167 | halt(); |
169 | } |
168 | } |
170 | 169 | ||
171 | ofw_aliases = ofw_find_device("/aliases"); |
170 | ofw_aliases = ofw_find_device("/aliases"); |
172 | if (ofw_aliases == -1) { |
171 | if (ofw_aliases == -1) { |
173 | puts("\r\nError: Unable to find /aliases device, halted.\r\n"); |
172 | puts("\r\nError: Unable to find /aliases device, halted.\r\n"); |
174 | halt(); |
173 | halt(); |
175 | } |
174 | } |
176 | } |
175 | } |
177 | 176 | ||
178 | 177 | ||
179 | void ofw_write(const char *str, const int len) |
178 | void ofw_write(const char *str, const int len) |
180 | { |
179 | { |
181 | if (ofw_stdout == 0) |
180 | if (ofw_stdout == 0) |
182 | return; |
181 | return; |
183 | 182 | ||
184 | ofw_call("write", 3, 1, NULL, ofw_stdout, str, len); |
183 | ofw_call("write", 3, 1, NULL, ofw_stdout, str, len); |
185 | } |
184 | } |
186 | 185 | ||
187 | 186 | ||
188 | void *ofw_translate(const void *virt) |
187 | void *ofw_translate(const void *virt) |
189 | { |
188 | { |
190 | ofw_arg_t result[3]; |
189 | ofw_arg_t result[3]; |
191 | 190 | ||
192 | if (ofw_call("call-method", 4, 4, result, "translate", ofw_mmu, virt, 1) != 0) { |
191 | if (ofw_call("call-method", 4, 4, result, "translate", ofw_mmu, virt, 1) != 0) { |
193 | puts("Error: MMU method translate() failed, halting.\n"); |
192 | puts("Error: MMU method translate() failed, halting.\n"); |
194 | halt(); |
193 | halt(); |
195 | } |
194 | } |
196 | return (void *) result[2]; |
195 | return (void *) result[2]; |
197 | } |
196 | } |
198 | 197 | ||
199 | 198 | ||
200 | int ofw_map(const void *phys, const void *virt, const int size, const int mode) |
199 | int ofw_map(const void *phys, const void *virt, const int size, const int mode) |
201 | { |
200 | { |
202 | return ofw_call("call-method", 6, 1, NULL, "map", ofw_mmu, mode, size, virt, phys); |
201 | return ofw_call("call-method", 6, 1, NULL, "map", ofw_mmu, mode, size, virt, phys); |
203 | } |
202 | } |
204 | 203 | ||
205 | 204 | ||
206 | int ofw_memmap(memmap_t *map) |
205 | int ofw_memmap(memmap_t *map) |
207 | { |
206 | { |
208 | unsigned int buf[BUF_SIZE]; |
207 | unsigned int buf[BUF_SIZE]; |
209 | int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(unsigned int) * BUF_SIZE); |
208 | int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(unsigned int) * BUF_SIZE); |
210 | if (ret <= 0) |
209 | if (ret <= 0) |
211 | return false; |
210 | return false; |
212 | 211 | ||
213 | unsigned int ac = ofw_get_address_cells(ofw_memory); |
212 | unsigned int ac = ofw_get_address_cells(ofw_memory); |
214 | unsigned int sc = ofw_get_size_cells(ofw_memory); |
213 | unsigned int sc = ofw_get_size_cells(ofw_memory); |
215 | 214 | ||
216 | int pos; |
215 | int pos; |
217 | map->total = 0; |
216 | map->total = 0; |
218 | map->count = 0; |
217 | map->count = 0; |
219 | for (pos = 0; (pos < ret / sizeof(unsigned int)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) { |
218 | for (pos = 0; (pos < ret / sizeof(unsigned int)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) { |
220 | void * start = (void *) buf[pos + ac - 1]; |
219 | void * start = (void *) buf[pos + ac - 1]; |
221 | unsigned int size = buf[pos + ac + sc - 1]; |
220 | unsigned int size = buf[pos + ac + sc - 1]; |
222 | 221 | ||
223 | if (size > 0) { |
222 | if (size > 0) { |
224 | map->zones[map->count].start = start; |
223 | map->zones[map->count].start = start; |
225 | map->zones[map->count].size = size; |
224 | map->zones[map->count].size = size; |
226 | map->count++; |
225 | map->count++; |
227 | map->total += size; |
226 | map->total += size; |
228 | } |
227 | } |
229 | } |
228 | } |
230 | } |
229 | } |
231 | 230 | ||
232 | 231 | ||
233 | int ofw_screen(screen_t *screen) |
232 | int ofw_screen(screen_t *screen) |
234 | { |
233 | { |
235 | char device_name[BUF_SIZE]; |
234 | char device_name[BUF_SIZE]; |
236 | 235 | ||
237 | if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(char) * BUF_SIZE) <= 0) |
236 | if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(char) * BUF_SIZE) <= 0) |
238 | return false; |
237 | return false; |
239 | 238 | ||
240 | phandle device = ofw_find_device(device_name); |
239 | phandle device = ofw_find_device(device_name); |
241 | if (device == -1) |
240 | if (device == -1) |
242 | return false; |
241 | return false; |
243 | 242 | ||
244 | if (ofw_get_property(device, "address", &screen->addr, sizeof(screen->addr)) <= 0) |
243 | if (ofw_get_property(device, "address", &screen->addr, sizeof(screen->addr)) <= 0) |
245 | return false; |
244 | return false; |
246 | 245 | ||
247 | if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0) |
246 | if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0) |
248 | return false; |
247 | return false; |
249 | 248 | ||
250 | if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0) |
249 | if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0) |
251 | return false; |
250 | return false; |
252 | 251 | ||
253 | if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0) |
252 | if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0) |
254 | return false; |
253 | return false; |
255 | 254 | ||
256 | if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0) |
255 | if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0) |
257 | return false; |
256 | return false; |
258 | 257 | ||
259 | return true; |
258 | return true; |
260 | } |
259 | } |
261 | 260 | ||
262 | 261 | ||
263 | int ofw_keyboard(keyboard_t *keyboard) |
262 | int ofw_keyboard(keyboard_t *keyboard) |
264 | { |
263 | { |
265 | char device_name[BUF_SIZE]; |
264 | char device_name[BUF_SIZE]; |
266 | 265 | ||
267 | if (ofw_get_property(ofw_aliases, "macio", device_name, sizeof(char) * BUF_SIZE) <= 0) |
266 | if (ofw_get_property(ofw_aliases, "macio", device_name, sizeof(char) * BUF_SIZE) <= 0) |
268 | return false; |
267 | return false; |
269 | 268 | ||
270 | phandle device = ofw_find_device(device_name); |
269 | phandle device = ofw_find_device(device_name); |
271 | if (device == -1) |
270 | if (device == -1) |
272 | return false; |
271 | return false; |
273 | 272 | ||
274 | pci_reg_t macio; |
273 | pci_reg_t macio; |
275 | if (ofw_get_property(device, "assigned-addresses", &macio, sizeof(macio)) <= 0) |
274 | if (ofw_get_property(device, "assigned-addresses", &macio, sizeof(macio)) <= 0) |
276 | return false; |
275 | return false; |
277 | 276 | ||
278 | keyboard->addr = (void *) macio.addr.addr_lo; |
277 | keyboard->addr = (void *) macio.addr.addr_lo; |
279 | keyboard->size = macio.size_lo; |
278 | keyboard->size = macio.size_lo; |
280 | 279 | ||
281 | return true; |
280 | return true; |
282 | } |
281 | } |
283 | 282 |