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