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