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