Rev 1790 | Rev 1893 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
885 | decky | 1 | /* |
2 | * Copyright (C) 2005 Martin Decky |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1789 | jermar | 29 | #include <ofw.h> |
30 | #include <ofwarch.h> |
||
1764 | jermar | 31 | #include <printf.h> |
32 | #include <asm.h> |
||
1782 | jermar | 33 | #include <types.h> |
885 | decky | 34 | |
1783 | jermar | 35 | uintptr_t ofw_cif; |
964 | decky | 36 | |
885 | decky | 37 | phandle ofw_chosen; |
1146 | decky | 38 | ihandle ofw_stdout; |
39 | phandle ofw_root; |
||
40 | ihandle ofw_mmu; |
||
41 | phandle ofw_memory; |
||
1130 | decky | 42 | phandle ofw_aliases; |
885 | decky | 43 | |
1789 | jermar | 44 | void ofw_init(void) |
45 | { |
||
46 | ofw_chosen = ofw_find_device("/chosen"); |
||
47 | if (ofw_chosen == -1) |
||
48 | halt(); |
||
49 | |||
50 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0) |
||
51 | ofw_stdout = 0; |
||
52 | |||
53 | ofw_root = ofw_find_device("/"); |
||
54 | if (ofw_root == -1) { |
||
55 | puts("\r\nError: Unable to find / device, halted.\r\n"); |
||
56 | halt(); |
||
57 | } |
||
58 | |||
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"); |
||
61 | halt(); |
||
62 | } |
||
63 | |||
64 | ofw_memory = ofw_find_device("/memory"); |
||
65 | if (ofw_memory == -1) { |
||
66 | puts("\r\nError: Unable to find /memory device, halted.\r\n"); |
||
67 | halt(); |
||
68 | } |
||
69 | |||
70 | ofw_aliases = ofw_find_device("/aliases"); |
||
71 | if (ofw_aliases == -1) { |
||
72 | puts("\r\nError: Unable to find /aliases device, halted.\r\n"); |
||
73 | halt(); |
||
74 | } |
||
75 | } |
||
76 | |||
1790 | jermar | 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. |
||
83 | * |
||
84 | * @return Return value returned by the client interface. |
||
85 | */ |
||
1772 | jermar | 86 | static unsigned long ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...) |
885 | decky | 87 | { |
88 | va_list list; |
||
89 | ofw_args_t args; |
||
90 | int i; |
||
91 | |||
1790 | jermar | 92 | args.service = (ofw_arg_t) service; |
885 | decky | 93 | args.nargs = nargs; |
94 | args.nret = nret; |
||
95 | |||
1146 | decky | 96 | va_start(list, rets); |
885 | decky | 97 | for (i = 0; i < nargs; i++) |
98 | args.args[i] = va_arg(list, ofw_arg_t); |
||
99 | va_end(list); |
||
100 | |||
101 | for (i = 0; i < nret; i++) |
||
102 | args.args[i + nargs] = 0; |
||
103 | |||
1783 | jermar | 104 | (void) ofw(&args); |
105 | |||
1146 | decky | 106 | for (i = 1; i < nret; i++) |
107 | rets[i - 1] = args.args[i + nargs]; |
||
1783 | jermar | 108 | |
956 | decky | 109 | return args.args[nargs]; |
885 | decky | 110 | } |
111 | |||
112 | |||
1771 | jermar | 113 | phandle ofw_find_device(const char *name) |
953 | decky | 114 | { |
1146 | decky | 115 | return ofw_call("finddevice", 1, 1, NULL, name); |
964 | decky | 116 | } |
117 | |||
118 | |||
1771 | jermar | 119 | int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen) |
964 | decky | 120 | { |
1146 | decky | 121 | return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen); |
964 | decky | 122 | } |
123 | |||
1146 | decky | 124 | |
1789 | jermar | 125 | unsigned int ofw_get_address_cells(const phandle device) |
1146 | decky | 126 | { |
1789 | jermar | 127 | unsigned int ret = 1; |
1146 | decky | 128 | |
129 | if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0) |
||
130 | if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0) |
||
1789 | jermar | 131 | ret = OFW_ADDRESS_CELLS; |
1146 | decky | 132 | |
133 | return ret; |
||
134 | } |
||
135 | |||
136 | |||
1789 | jermar | 137 | unsigned int ofw_get_size_cells(const phandle device) |
1146 | decky | 138 | { |
139 | unsigned int ret; |
||
140 | |||
141 | if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0) |
||
142 | if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0) |
||
1789 | jermar | 143 | ret = OFW_SIZE_CELLS; |
1146 | decky | 144 | |
145 | return ret; |
||
146 | } |
||
147 | |||
1881 | jermar | 148 | phandle ofw_get_child_node(const phandle node) |
149 | { |
||
150 | return ofw_call("child", 1, 1, NULL, node); |
||
151 | } |
||
1146 | decky | 152 | |
1881 | jermar | 153 | phandle ofw_get_peer_node(const phandle node) |
154 | { |
||
155 | return ofw_call("peer", 1, 1, NULL, node); |
||
156 | } |
||
157 | |||
964 | decky | 158 | static ihandle ofw_open(const char *name) |
159 | { |
||
1146 | decky | 160 | return ofw_call("open", 1, 1, NULL, name); |
953 | decky | 161 | } |
162 | |||
163 | |||
964 | decky | 164 | void ofw_write(const char *str, const int len) |
885 | decky | 165 | { |
964 | decky | 166 | if (ofw_stdout == 0) |
167 | return; |
||
168 | |||
1146 | decky | 169 | ofw_call("write", 3, 1, NULL, ofw_stdout, str, len); |
885 | decky | 170 | } |
171 | |||
172 | |||
956 | decky | 173 | void *ofw_translate(const void *virt) |
885 | decky | 174 | { |
1782 | jermar | 175 | ofw_arg_t result[4]; |
176 | int shift; |
||
177 | |||
178 | if (ofw_call("call-method", 3, 5, result, "translate", ofw_mmu, virt) != 0) { |
||
1146 | decky | 179 | puts("Error: MMU method translate() failed, halting.\n"); |
180 | halt(); |
||
181 | } |
||
1782 | jermar | 182 | |
1783 | jermar | 183 | if (ofw_translate_failed(result[0])) |
184 | return NULL; |
||
185 | |||
1782 | jermar | 186 | if (sizeof(unative_t) == 8) |
187 | shift = 32; |
||
188 | else |
||
189 | shift = 0; |
||
1789 | jermar | 190 | |
1783 | jermar | 191 | return (void *) ((result[2]<<shift)|result[3]); |
885 | decky | 192 | } |
953 | decky | 193 | |
1783 | jermar | 194 | void *ofw_claim(const void *virt, const int len) |
195 | { |
||
196 | ofw_arg_t retaddr; |
||
197 | int shift; |
||
953 | decky | 198 | |
1783 | jermar | 199 | if (ofw_call("call-method", 5, 2, &retaddr, "claim", ofw_mmu, 0, len, virt) != 0) { |
200 | puts("Error: MMU method claim() failed, halting.\n"); |
||
201 | halt(); |
||
202 | } |
||
203 | |||
204 | return (void *) retaddr; |
||
205 | } |
||
206 | |||
1075 | decky | 207 | int ofw_map(const void *phys, const void *virt, const int size, const int mode) |
208 | { |
||
1782 | jermar | 209 | uintptr_t phys_hi, phys_lo; |
210 | |||
211 | if (sizeof(unative_t) == 8) { |
||
212 | int shift = 32; |
||
213 | phys_hi = (uintptr_t) phys >> shift; |
||
214 | phys_lo = (uintptr_t) phys & 0xffffffff; |
||
215 | } else { |
||
216 | phys_hi = 0; |
||
217 | phys_lo = (uintptr_t) phys; |
||
218 | } |
||
219 | |||
220 | return ofw_call("call-method", 7, 1, NULL, "map", ofw_mmu, mode, size, virt, |
||
221 | phys_hi, phys_lo); |
||
1075 | decky | 222 | } |
223 | |||
224 | |||
964 | decky | 225 | int ofw_memmap(memmap_t *map) |
226 | { |
||
1789 | jermar | 227 | unsigned int ac = ofw_get_address_cells(ofw_memory); |
228 | unsigned int sc = ofw_get_size_cells(ofw_memory); |
||
229 | |||
230 | uint32_t buf[((ac+sc)*MEMMAP_MAX_RECORDS)]; |
||
1771 | jermar | 231 | int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf)); |
1789 | jermar | 232 | if (ret <= 0) /* ret is the number of written bytes */ |
964 | decky | 233 | return false; |
1789 | jermar | 234 | |
1146 | decky | 235 | int pos; |
964 | decky | 236 | map->total = 0; |
237 | map->count = 0; |
||
1789 | jermar | 238 | for (pos = 0; (pos < ret / sizeof(uint32_t)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) { |
239 | void * start = (void *) ((uintptr_t) buf[pos + ac - 1]); |
||
1146 | decky | 240 | unsigned int size = buf[pos + ac + sc - 1]; |
241 | |||
242 | if (size > 0) { |
||
243 | map->zones[map->count].start = start; |
||
244 | map->zones[map->count].size = size; |
||
245 | map->count++; |
||
246 | map->total += size; |
||
247 | } |
||
964 | decky | 248 | } |
1789 | jermar | 249 | |
250 | return true; |
||
964 | decky | 251 | } |
1130 | decky | 252 | |
253 | |||
254 | int ofw_screen(screen_t *screen) |
||
255 | { |
||
1146 | decky | 256 | char device_name[BUF_SIZE]; |
1789 | jermar | 257 | uint32_t virtaddr; |
1130 | decky | 258 | |
1771 | jermar | 259 | if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(device_name)) <= 0) |
1130 | decky | 260 | return false; |
261 | |||
262 | phandle device = ofw_find_device(device_name); |
||
263 | if (device == -1) |
||
264 | return false; |
||
265 | |||
1789 | jermar | 266 | if (ofw_get_property(device, "address", &virtaddr, sizeof(virtaddr)) <= 0) |
1130 | decky | 267 | return false; |
1789 | jermar | 268 | |
269 | screen->addr = (void *) ((uintptr_t) virtaddr); |
||
270 | |||
1130 | decky | 271 | if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0) |
272 | return false; |
||
273 | |||
274 | if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0) |
||
275 | return false; |
||
276 | |||
277 | if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0) |
||
278 | return false; |
||
279 | |||
280 | if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0) |
||
281 | return false; |
||
282 | |||
283 | return true; |
||
284 | } |