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