source: mainline/boot/genarch/ofw.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was e0565005, checked in by Martin Decky <martin@…>, 16 years ago

initialize and setup all displays which can be detected in OFW tree (not only stdin)
reduce the number of newlines during boot (speedups especially in simulators)

  • Property mode set to 100644
File size: 11.2 KB
RevLine 
[ce8725be]1/*
[df4ed85]2 * Copyright (c) 2005 Martin Decky
[ce8725be]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 */
[fd375a8d]28
[63cda71]29#include <ofw.h>
30#include <ofwarch.h>
[ce8725be]31#include <printf.h>
32#include <asm.h>
[822b64e]33#include <types.h>
[e0565005]34#include <string.h>
[ce8725be]35
[e731b0d]36#define RED(i) (((i) >> 5) & ((1 << 3) - 1))
37#define GREEN(i) (((i) >> 3) & ((1 << 2) - 1))
38#define BLUE(i) ((i) & ((1 << 3) - 1))
39#define CLIP(i) ((i) <= 255 ? (i) : 255)
40
[2e672fd]41uintptr_t ofw_cif;
[ce8725be]42
43phandle ofw_chosen;
44ihandle ofw_stdout;
45phandle ofw_root;
46ihandle ofw_mmu;
[95b47c82]47ihandle ofw_memory_prop;
[ce8725be]48phandle ofw_memory;
49
[63cda71]50void ofw_init(void)
51{
52 ofw_chosen = ofw_find_device("/chosen");
53 if (ofw_chosen == -1)
54 halt();
55
[965dc18]56 if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout,
57 sizeof(ofw_stdout)) <= 0)
[63cda71]58 ofw_stdout = 0;
59
60 ofw_root = ofw_find_device("/");
61 if (ofw_root == -1) {
62 puts("\r\nError: Unable to find / device, halted.\r\n");
63 halt();
64 }
65
[965dc18]66 if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu,
67 sizeof(ofw_mmu)) <= 0) {
[63cda71]68 puts("\r\nError: Unable to get mmu property, halted.\r\n");
69 halt();
70 }
[965dc18]71 if (ofw_get_property(ofw_chosen, "memory", &ofw_memory_prop,
72 sizeof(ofw_memory_prop)) <= 0) {
[95b47c82]73 puts("\r\nError: Unable to get memory property, halted.\r\n");
74 halt();
75 }
[e0565005]76
[63cda71]77 ofw_memory = ofw_find_device("/memory");
78 if (ofw_memory == -1) {
79 puts("\r\nError: Unable to find /memory device, halted.\r\n");
80 halt();
81 }
82}
83
[94d614e]84/** Perform a call to OpenFirmware client interface.
85 *
[e731b0d]86 * @param service String identifying the service requested.
87 * @param nargs Number of input arguments.
88 * @param nret Number of output arguments. This includes the return
89 * value.
90 * @param rets Buffer for output arguments or NULL. The buffer must
91 * accommodate nret - 1 items.
92 *
93 * @return Return value returned by the client interface.
[94d614e]94 *
95 */
[965dc18]96unsigned long
97ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets,
98 ...)
[ce8725be]99{
100 va_list list;
101 ofw_args_t args;
102 int i;
103
[94d614e]104 args.service = (ofw_arg_t) service;
[ce8725be]105 args.nargs = nargs;
106 args.nret = nret;
107
108 va_start(list, rets);
109 for (i = 0; i < nargs; i++)
110 args.args[i] = va_arg(list, ofw_arg_t);
111 va_end(list);
112
113 for (i = 0; i < nret; i++)
114 args.args[i + nargs] = 0;
115
[2e672fd]116 (void) ofw(&args);
117
[ce8725be]118 for (i = 1; i < nret; i++)
119 rets[i - 1] = args.args[i + nargs];
[2e672fd]120
[ce8725be]121 return args.args[nargs];
122}
123
[b7b5f83]124phandle ofw_find_device(const char *name)
[ce8725be]125{
126 return ofw_call("finddevice", 1, 1, NULL, name);
127}
128
[e0565005]129int ofw_get_property(const phandle device, const char *name, void *buf,
[965dc18]130 const int buflen)
[ce8725be]131{
132 return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
133}
134
[3abe07f5]135int ofw_get_proplen(const phandle device, const char *name)
136{
137 return ofw_call("getproplen", 2, 1, NULL, device, name);
138}
139
140int ofw_next_property(const phandle device, char *previous, char *buf)
141{
142 return ofw_call("nextprop", 3, 1, NULL, device, previous, buf);
143}
[ce8725be]144
[16529d5]145int ofw_package_to_path(const phandle device, char *buf, const int buflen)
146{
147 return ofw_call("package-to-path", 3, 1, NULL, device, buf, buflen);
148}
149
[63cda71]150unsigned int ofw_get_address_cells(const phandle device)
[ce8725be]151{
[63cda71]152 unsigned int ret = 1;
[ce8725be]153
154 if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0)
[965dc18]155 if (ofw_get_property(ofw_root, "#address-cells", &ret,
156 sizeof(ret)) <= 0)
[63cda71]157 ret = OFW_ADDRESS_CELLS;
[ce8725be]158
159 return ret;
160}
161
[63cda71]162unsigned int ofw_get_size_cells(const phandle device)
[ce8725be]163{
164 unsigned int ret;
165
166 if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0)
[965dc18]167 if (ofw_get_property(ofw_root, "#size-cells", &ret,
168 sizeof(ret)) <= 0)
[63cda71]169 ret = OFW_SIZE_CELLS;
[ce8725be]170
171 return ret;
172}
173
[9a5b556]174phandle ofw_get_child_node(const phandle node)
175{
176 return ofw_call("child", 1, 1, NULL, node);
177}
178
179phandle ofw_get_peer_node(const phandle node)
180{
181 return ofw_call("peer", 1, 1, NULL, node);
182}
[ce8725be]183
184static ihandle ofw_open(const char *name)
185{
186 return ofw_call("open", 1, 1, NULL, name);
187}
188
189
190void ofw_write(const char *str, const int len)
191{
192 if (ofw_stdout == 0)
193 return;
194
195 ofw_call("write", 3, 1, NULL, ofw_stdout, str, len);
196}
197
198
199void *ofw_translate(const void *virt)
200{
[822b64e]201 ofw_arg_t result[4];
202 int shift;
203
[4bc73fa]204 if (ofw_call("call-method", 4, 5, result, "translate", ofw_mmu,
205 virt, 0) != 0) {
[ce8725be]206 puts("Error: MMU method translate() failed, halting.\n");
207 halt();
208 }
[822b64e]209
[2e672fd]210 if (ofw_translate_failed(result[0]))
211 return NULL;
212
[822b64e]213 if (sizeof(unative_t) == 8)
214 shift = 32;
215 else
216 shift = 0;
[63cda71]217
[95b47c82]218 return (void *) ((result[2] << shift) | result[3]);
[ce8725be]219}
220
[e731b0d]221void *ofw_claim_virt(const void *virt, const unsigned int len)
[2e672fd]222{
223 ofw_arg_t retaddr;
224
[965dc18]225 if (ofw_call("call-method", 5, 2, &retaddr, "claim", ofw_mmu, 0, len,
226 virt) != 0) {
[2e672fd]227 puts("Error: MMU method claim() failed, halting.\n");
228 halt();
229 }
230
231 return (void *) retaddr;
232}
[ce8725be]233
[e731b0d]234static void *ofw_claim_phys_internal(const void *phys, const unsigned int len, const unsigned int alignment)
[95b47c82]235{
[e731b0d]236 /*
237 * Note that the return value check will help
238 * us to discover conflicts between OpenFirmware
239 * allocations and our use of physical memory.
240 * It is better to detect collisions here
241 * than to cope with weird errors later.
242 *
243 * So this is really not to make the loader
244 * more generic; it is here for debugging
245 * purposes.
246 */
247
[95b47c82]248 if (sizeof(unative_t) == 8) {
[e731b0d]249 ofw_arg_t retaddr[2];
250 int shift = 32;
251
[95b47c82]252 if (ofw_call("call-method", 6, 3, retaddr, "claim",
[e731b0d]253 ofw_memory_prop, alignment, len, ((uintptr_t) phys) >> shift,
[95b47c82]254 ((uintptr_t) phys) & ((uint32_t) -1)) != 0) {
255 puts("Error: memory method claim() failed, halting.\n");
256 halt();
257 }
[e731b0d]258
259 return (void *) ((retaddr[0] << shift) | retaddr[1]);
[95b47c82]260 } else {
[e731b0d]261 ofw_arg_t retaddr[1];
262
263 if (ofw_call("call-method", 5, 2, retaddr, "claim",
264 ofw_memory_prop, alignment, len, (uintptr_t) phys) != 0) {
265 puts("Error: memory method claim() failed, halting.\n");
266 halt();
267 }
268
269 return (void *) retaddr[0];
[95b47c82]270 }
[e731b0d]271}
[95b47c82]272
[e731b0d]273void *ofw_claim_phys(const void *phys, const unsigned int len)
274{
275 return ofw_claim_phys_internal(phys, len, 0);
[95b47c82]276}
277
[e731b0d]278void *ofw_claim_phys_any(const unsigned int len, const unsigned int alignment)
279{
280 return ofw_claim_phys_internal(NULL, len, alignment);
281}
282
283int ofw_map(const void *phys, const void *virt, const unsigned int size, const int mode)
[ce8725be]284{
[822b64e]285 uintptr_t phys_hi, phys_lo;
286
287 if (sizeof(unative_t) == 8) {
288 int shift = 32;
289 phys_hi = (uintptr_t) phys >> shift;
290 phys_lo = (uintptr_t) phys & 0xffffffff;
291 } else {
292 phys_hi = 0;
293 phys_lo = (uintptr_t) phys;
294 }
295
[965dc18]296 return ofw_call("call-method", 7, 1, NULL, "map", ofw_mmu, mode, size,
297 virt, phys_hi, phys_lo);
[ce8725be]298}
299
[771cd22]300/** Save OpenFirmware physical memory map.
301 *
302 * @param map Memory map structure where the map will be saved.
303 *
304 * @return Zero on failure, non-zero on success.
305 */
[ce8725be]306int ofw_memmap(memmap_t *map)
307{
[965dc18]308 unsigned int ac = ofw_get_address_cells(ofw_memory) /
309 (sizeof(uintptr_t) / sizeof(uint32_t));
310 unsigned int sc = ofw_get_size_cells(ofw_memory) /
311 (sizeof(uintptr_t) / sizeof(uint32_t));
[63cda71]312
[965dc18]313 uintptr_t buf[((ac + sc) * MEMMAP_MAX_RECORDS)];
[63cda71]314 int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf));
315 if (ret <= 0) /* ret is the number of written bytes */
316 return false;
317
[ce8725be]318 int pos;
319 map->total = 0;
320 map->count = 0;
[965dc18]321 for (pos = 0; (pos < ret / sizeof(uintptr_t)) &&
[95b47c82]322 (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) {
[965dc18]323 void *start = (void *) (buf[pos + ac - 1]);
[ce8725be]324 unsigned int size = buf[pos + ac + sc - 1];
[965dc18]325
326 /*
[e731b0d]327 * This is a hot fix of the issue which occurs on machines
328 * where there are holes in the physical memory (such as
329 * SunBlade 1500). Should we detect a hole in the physical
330 * memory, we will ignore any memory detected behind
331 * the hole and pretend the hole does not exist.
[965dc18]332 */
333 if ((map->count > 0) && (map->zones[map->count - 1].start +
334 map->zones[map->count - 1].size < start))
335 break;
336
[ce8725be]337 if (size > 0) {
338 map->zones[map->count].start = start;
339 map->zones[map->count].size = size;
340 map->count++;
341 map->total += size;
342 }
343 }
[63cda71]344
345 return true;
[ce8725be]346}
347
[e0565005]348static void ofw_setup_screen(phandle handle)
[2b1f860]349{
[e0565005]350 /* Check for device type */
351 char device_type[OFW_TREE_PROPERTY_MAX_VALUELEN];
352 if (ofw_get_property(handle, "device_type", device_type, OFW_TREE_PROPERTY_MAX_VALUELEN) <= 0)
353 return;
[2b1f860]354
[e0565005]355 device_type[OFW_TREE_PROPERTY_MAX_VALUELEN - 1] = '\0';
356 if (strcmp(device_type, "display") != 0)
357 return;
[fd375a8d]358
[e0565005]359 /* Check for 8 bit depth */
[2b1f860]360 uint32_t depth;
[e0565005]361 if (ofw_get_property(handle, "depth", &depth, sizeof(uint32_t)) <= 0)
362 depth = 0;
363
364 /* Get device path */
365 static char path[OFW_TREE_PATH_MAX_LEN + 1];
366 size_t len = ofw_package_to_path(handle, path, OFW_TREE_PATH_MAX_LEN);
367 if (len == -1)
368 return;
[2b1f860]369
[e0565005]370 path[len] = '\0';
371
372 /* Open the display to initialize it */
373 ihandle screen = ofw_open(path);
[2b1f860]374 if (screen == -1)
[e0565005]375 return;
[fd375a8d]376
[e0565005]377 if (depth == 8) {
378 /* Setup the palette so that the (inverted) 3:2:3 scheme is usable */
379 unsigned int i;
380 for (i = 0; i < 256; i++) {
381 ofw_call("call-method", 6, 1, NULL, "color!", screen,
382 255 - i, CLIP(BLUE(i) * 37), GREEN(i) * 85, CLIP(RED(i) * 37));
383 }
384 }
385}
386
387static void ofw_setup_screens_internal(phandle current)
388{
389 while ((current != 0) && (current != -1)) {
390 ofw_setup_screen(current);
391
392 /*
393 * Recursively process the potential child node.
394 */
395 phandle child = ofw_get_child_node(current);
396 if ((child != 0) && (child != -1))
397 ofw_setup_screens_internal(child);
398
399 /*
400 * Iteratively process the next peer node.
401 * Note that recursion is a bad idea here.
402 * Due to the topology of the OpenFirmware device tree,
403 * the nesting of peer nodes could be to wide and the
404 * risk of overflowing the stack is too real.
405 */
406 phandle peer = ofw_get_peer_node(current);
407 if ((peer != 0) && (peer != -1)) {
408 current = peer;
409 /*
410 * Process the peer in next iteration.
411 */
412 continue;
413 }
414
415 /*
416 * No more peers on this level.
417 */
418 break;
419 }
420}
421
422/** Setup all screens which can be detected.
423 *
424 * Open all screens which can be detected and set up the palette for the 8-bit
425 * color depth configuration so that the 3:2:3 color scheme can be used.
426 * Check that setting the palette makes sense (the color depth is not greater
427 * than 8).
428 *
429 */
430void ofw_setup_screens(void)
431{
432 ofw_setup_screens_internal(ofw_root);
[2b1f860]433}
[deb14fb]434
435void ofw_quiesce(void)
436{
[282f2c9c]437 ofw_call("quiesce", 0, 0, NULL);
[deb14fb]438}
Note: See TracBrowser for help on using the repository browser.