Rev 1764 | Rev 1772 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1764 | Rev 1771 | ||
---|---|---|---|
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 <printf.h> |
30 | #include <printf.h> |
31 | #include <asm.h> |
31 | #include <asm.h> |
32 | 32 | ||
33 | #define MAX_OFW_ARGS 10 |
33 | #define MAX_OFW_ARGS 10 |
34 | #define BUF_SIZE 1024 |
- | |
35 | - | ||
36 | typedef unsigned int ofw_arg_t; |
- | |
37 | typedef unsigned int ihandle; |
- | |
38 | typedef unsigned int phandle; |
- | |
39 | 34 | ||
40 | /** OpenFirmware command structure |
35 | /** OpenFirmware command structure |
41 | * |
36 | * |
42 | */ |
37 | */ |
43 | typedef struct { |
38 | typedef struct { |
44 | const char *service; /**< Command name */ |
39 | const char *service; /**< Command name */ |
45 | unsigned int nargs; /**< Number of in arguments */ |
40 | unsigned int nargs; /**< Number of in arguments */ |
46 | unsigned int nret; /**< Number of out arguments */ |
41 | unsigned int nret; /**< Number of out arguments */ |
47 | ofw_arg_t args[MAX_OFW_ARGS]; /**< List of arguments */ |
42 | ofw_arg_t args[MAX_OFW_ARGS]; /**< List of arguments */ |
48 | } ofw_args_t; |
43 | } ofw_args_t; |
49 | 44 | ||
50 | typedef void (*ofw_entry)(ofw_args_t *); |
45 | typedef void (*ofw_entry)(ofw_args_t *); |
51 | 46 | ||
52 | - | ||
53 | typedef struct { |
- | |
54 | unsigned int info; |
- | |
55 | unsigned int addr_hi; |
- | |
56 | unsigned int addr_lo; |
- | |
57 | } pci_addr_t; |
- | |
58 | - | ||
59 | typedef struct { |
- | |
60 | pci_addr_t addr; |
- | |
61 | unsigned int size_hi; |
- | |
62 | unsigned int size_lo; |
- | |
63 | } pci_reg_t; |
- | |
64 | - | ||
65 | - | ||
66 | ofw_entry ofw; |
47 | ofw_entry ofw; |
67 | 48 | ||
68 | phandle ofw_chosen; |
49 | phandle ofw_chosen; |
69 | ihandle ofw_stdout; |
50 | ihandle ofw_stdout; |
70 | phandle ofw_root; |
51 | phandle ofw_root; |
71 | ihandle ofw_mmu; |
52 | ihandle ofw_mmu; |
72 | phandle ofw_memory; |
53 | phandle ofw_memory; |
73 | phandle ofw_aliases; |
54 | phandle ofw_aliases; |
74 | 55 | ||
75 | static int ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...) |
56 | static int ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...) |
76 | { |
57 | { |
77 | va_list list; |
58 | va_list list; |
78 | ofw_args_t args; |
59 | ofw_args_t args; |
79 | int i; |
60 | int i; |
80 | 61 | ||
81 | args.service = service; |
62 | args.service = service; |
82 | args.nargs = nargs; |
63 | args.nargs = nargs; |
83 | args.nret = nret; |
64 | args.nret = nret; |
84 | 65 | ||
85 | va_start(list, rets); |
66 | va_start(list, rets); |
86 | for (i = 0; i < nargs; i++) |
67 | for (i = 0; i < nargs; i++) |
87 | args.args[i] = va_arg(list, ofw_arg_t); |
68 | args.args[i] = va_arg(list, ofw_arg_t); |
88 | va_end(list); |
69 | va_end(list); |
89 | 70 | ||
90 | for (i = 0; i < nret; i++) |
71 | for (i = 0; i < nret; i++) |
91 | args.args[i + nargs] = 0; |
72 | args.args[i + nargs] = 0; |
92 | 73 | ||
93 | ofw(&args); |
74 | ofw(&args); |
94 | 75 | ||
95 | for (i = 1; i < nret; i++) |
76 | for (i = 1; i < nret; i++) |
96 | rets[i - 1] = args.args[i + nargs]; |
77 | rets[i - 1] = args.args[i + nargs]; |
97 | 78 | ||
98 | return args.args[nargs]; |
79 | return args.args[nargs]; |
99 | } |
80 | } |
100 | 81 | ||
101 | 82 | ||
102 | static phandle ofw_find_device(const char *name) |
83 | phandle ofw_find_device(const char *name) |
103 | { |
84 | { |
104 | return ofw_call("finddevice", 1, 1, NULL, name); |
85 | return ofw_call("finddevice", 1, 1, NULL, name); |
105 | } |
86 | } |
106 | 87 | ||
107 | 88 | ||
108 | static int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen) |
89 | int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen) |
109 | { |
90 | { |
110 | return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen); |
91 | return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen); |
111 | } |
92 | } |
112 | 93 | ||
113 | 94 | ||
114 | static unsigned int ofw_get_address_cells(const phandle device) |
95 | static unsigned int ofw_get_address_cells(const phandle device) |
115 | { |
96 | { |
116 | unsigned int ret; |
97 | unsigned int ret; |
117 | 98 | ||
118 | if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0) |
99 | if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0) |
119 | if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0) |
100 | if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0) |
120 | ret = 1; |
101 | ret = 1; |
121 | 102 | ||
122 | return ret; |
103 | return ret; |
123 | } |
104 | } |
124 | 105 | ||
125 | 106 | ||
126 | static unsigned int ofw_get_size_cells(const phandle device) |
107 | static unsigned int ofw_get_size_cells(const phandle device) |
127 | { |
108 | { |
128 | unsigned int ret; |
109 | unsigned int ret; |
129 | 110 | ||
130 | if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0) |
111 | if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0) |
131 | if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0) |
112 | if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0) |
132 | ret = 1; |
113 | ret = 1; |
133 | 114 | ||
134 | return ret; |
115 | return ret; |
135 | } |
116 | } |
136 | 117 | ||
137 | 118 | ||
138 | static ihandle ofw_open(const char *name) |
119 | static ihandle ofw_open(const char *name) |
139 | { |
120 | { |
140 | return ofw_call("open", 1, 1, NULL, name); |
121 | return ofw_call("open", 1, 1, NULL, name); |
141 | } |
122 | } |
142 | 123 | ||
143 | 124 | ||
144 | void init(void) |
125 | void init(void) |
145 | { |
126 | { |
146 | ofw_chosen = ofw_find_device("/chosen"); |
127 | ofw_chosen = ofw_find_device("/chosen"); |
147 | if (ofw_chosen == -1) |
128 | if (ofw_chosen == -1) |
148 | halt(); |
129 | halt(); |
149 | 130 | ||
150 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0) |
131 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0) |
151 | ofw_stdout = 0; |
132 | ofw_stdout = 0; |
152 | 133 | ||
153 | ofw_root = ofw_find_device("/"); |
134 | ofw_root = ofw_find_device("/"); |
154 | if (ofw_root == -1) { |
135 | if (ofw_root == -1) { |
155 | puts("\r\nError: Unable to find / device, halted.\r\n"); |
136 | puts("\r\nError: Unable to find / device, halted.\r\n"); |
156 | halt(); |
137 | halt(); |
157 | } |
138 | } |
158 | 139 | ||
159 | if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu, sizeof(ofw_mmu)) <= 0) { |
140 | if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu, sizeof(ofw_mmu)) <= 0) { |
160 | puts("\r\nError: Unable to get mmu property, halted.\r\n"); |
141 | puts("\r\nError: Unable to get mmu property, halted.\r\n"); |
161 | halt(); |
142 | halt(); |
162 | } |
143 | } |
163 | 144 | ||
164 | ofw_memory = ofw_find_device("/memory"); |
145 | ofw_memory = ofw_find_device("/memory"); |
165 | if (ofw_memory == -1) { |
146 | if (ofw_memory == -1) { |
166 | puts("\r\nError: Unable to find /memory device, halted.\r\n"); |
147 | puts("\r\nError: Unable to find /memory device, halted.\r\n"); |
167 | halt(); |
148 | halt(); |
168 | } |
149 | } |
169 | 150 | ||
170 | ofw_aliases = ofw_find_device("/aliases"); |
151 | ofw_aliases = ofw_find_device("/aliases"); |
171 | if (ofw_aliases == -1) { |
152 | if (ofw_aliases == -1) { |
172 | puts("\r\nError: Unable to find /aliases device, halted.\r\n"); |
153 | puts("\r\nError: Unable to find /aliases device, halted.\r\n"); |
173 | halt(); |
154 | halt(); |
174 | } |
155 | } |
175 | } |
156 | } |
176 | 157 | ||
177 | 158 | ||
178 | void ofw_write(const char *str, const int len) |
159 | void ofw_write(const char *str, const int len) |
179 | { |
160 | { |
180 | if (ofw_stdout == 0) |
161 | if (ofw_stdout == 0) |
181 | return; |
162 | return; |
182 | 163 | ||
183 | ofw_call("write", 3, 1, NULL, ofw_stdout, str, len); |
164 | ofw_call("write", 3, 1, NULL, ofw_stdout, str, len); |
184 | } |
165 | } |
185 | 166 | ||
186 | 167 | ||
187 | void *ofw_translate(const void *virt) |
168 | void *ofw_translate(const void *virt) |
188 | { |
169 | { |
189 | ofw_arg_t result[3]; |
170 | ofw_arg_t result[3]; |
190 | 171 | ||
191 | if (ofw_call("call-method", 4, 4, result, "translate", ofw_mmu, virt, 1) != 0) { |
172 | if (ofw_call("call-method", 4, 4, result, "translate", ofw_mmu, virt, 1) != 0) { |
192 | puts("Error: MMU method translate() failed, halting.\n"); |
173 | puts("Error: MMU method translate() failed, halting.\n"); |
193 | halt(); |
174 | halt(); |
194 | } |
175 | } |
195 | return (void *) result[2]; |
176 | return (void *) result[2]; |
196 | } |
177 | } |
197 | 178 | ||
198 | 179 | ||
199 | int ofw_map(const void *phys, const void *virt, const int size, const int mode) |
180 | int ofw_map(const void *phys, const void *virt, const int size, const int mode) |
200 | { |
181 | { |
201 | return ofw_call("call-method", 6, 1, NULL, "map", ofw_mmu, mode, size, virt, phys); |
182 | return ofw_call("call-method", 6, 1, NULL, "map", ofw_mmu, mode, size, virt, phys); |
202 | } |
183 | } |
203 | 184 | ||
204 | 185 | ||
205 | int ofw_memmap(memmap_t *map) |
186 | int ofw_memmap(memmap_t *map) |
206 | { |
187 | { |
207 | unsigned int buf[BUF_SIZE]; |
188 | unsigned long buf[BUF_SIZE]; |
208 | int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(unsigned int) * BUF_SIZE); |
189 | int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf)); |
209 | if (ret <= 0) |
190 | if (ret <= 0) |
210 | return false; |
191 | return false; |
211 | 192 | ||
212 | unsigned int ac = ofw_get_address_cells(ofw_memory); |
193 | unsigned int ac = ofw_get_address_cells(ofw_memory); |
213 | unsigned int sc = ofw_get_size_cells(ofw_memory); |
194 | unsigned int sc = ofw_get_size_cells(ofw_memory); |
214 | 195 | ||
215 | int pos; |
196 | int pos; |
216 | map->total = 0; |
197 | map->total = 0; |
217 | map->count = 0; |
198 | map->count = 0; |
218 | for (pos = 0; (pos < ret / sizeof(unsigned int)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) { |
199 | for (pos = 0; (pos < ret / sizeof(unsigned long)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) { |
219 | void * start = (void *) buf[pos + ac - 1]; |
200 | void * start = (void *) buf[pos + ac - 1]; |
220 | unsigned int size = buf[pos + ac + sc - 1]; |
201 | unsigned int size = buf[pos + ac + sc - 1]; |
221 | 202 | ||
222 | if (size > 0) { |
203 | if (size > 0) { |
223 | map->zones[map->count].start = start; |
204 | map->zones[map->count].start = start; |
224 | map->zones[map->count].size = size; |
205 | map->zones[map->count].size = size; |
225 | map->count++; |
206 | map->count++; |
226 | map->total += size; |
207 | map->total += size; |
227 | } |
208 | } |
228 | } |
209 | } |
229 | } |
210 | } |
230 | 211 | ||
231 | 212 | ||
232 | int ofw_screen(screen_t *screen) |
213 | int ofw_screen(screen_t *screen) |
233 | { |
214 | { |
234 | char device_name[BUF_SIZE]; |
215 | char device_name[BUF_SIZE]; |
235 | 216 | ||
236 | if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(char) * BUF_SIZE) <= 0) |
217 | if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(device_name)) <= 0) |
237 | return false; |
218 | return false; |
238 | 219 | ||
239 | phandle device = ofw_find_device(device_name); |
220 | phandle device = ofw_find_device(device_name); |
240 | if (device == -1) |
221 | if (device == -1) |
241 | return false; |
222 | return false; |
242 | 223 | ||
243 | if (ofw_get_property(device, "address", &screen->addr, sizeof(screen->addr)) <= 0) |
224 | if (ofw_get_property(device, "address", &screen->addr, sizeof(screen->addr)) <= 0) |
244 | return false; |
225 | return false; |
245 | 226 | ||
246 | if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0) |
227 | if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0) |
247 | return false; |
228 | return false; |
248 | 229 | ||
249 | if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0) |
230 | if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0) |
250 | return false; |
231 | return false; |
251 | 232 | ||
252 | if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0) |
233 | if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0) |
253 | return false; |
234 | return false; |
254 | 235 | ||
255 | if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0) |
236 | if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0) |
256 | return false; |
237 | return false; |
257 | 238 | ||
258 | return true; |
239 | return true; |
259 | } |
240 | } |
260 | 241 | ||
261 | - | ||
262 | int ofw_keyboard(keyboard_t *keyboard) |
- | |
263 | { |
- | |
264 | char device_name[BUF_SIZE]; |
- | |
265 | - | ||
266 | if (ofw_get_property(ofw_aliases, "macio", device_name, sizeof(char) * BUF_SIZE) <= 0) |
- | |
267 | return false; |
- | |
268 | - | ||
269 | phandle device = ofw_find_device(device_name); |
- | |
270 | if (device == -1) |
- | |
271 | return false; |
- | |
272 | - | ||
273 | pci_reg_t macio; |
- | |
274 | if (ofw_get_property(device, "assigned-addresses", &macio, sizeof(macio)) <= 0) |
- | |
275 | return false; |
- | |
276 | - | ||
277 | keyboard->addr = (void *) macio.addr.addr_lo; |
- | |
278 | keyboard->size = macio.size_lo; |
- | |
279 | - | ||
280 | return true; |
- | |
281 | } |
- | |
282 | 242 |