File: /usr/src/linux/arch/ppc/kernel/prom.c
1 /*
2 * BK Id: SCCS/s.prom.c 1.42 09/08/01 15:47:42 paulus
3 */
4 /*
5 * Procedures for interfacing to the Open Firmware PROM on
6 * Power Macintosh computers.
7 *
8 * In particular, we are interested in the device tree
9 * and in using some of its services (exit, write to stdout).
10 *
11 * Paul Mackerras August 1996.
12 * Copyright (C) 1996 Paul Mackerras.
13 */
14 #include <stdarg.h>
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/init.h>
19 #include <linux/version.h>
20 #include <linux/threads.h>
21 #include <linux/spinlock.h>
22
23 #include <asm/sections.h>
24 #include <asm/prom.h>
25 #include <asm/page.h>
26 #include <asm/processor.h>
27 #include <asm/irq.h>
28 #include <asm/io.h>
29 #include <asm/smp.h>
30 #include <asm/bootx.h>
31 #include <asm/system.h>
32 #include <asm/mmu.h>
33 #include <asm/pgtable.h>
34 #include <asm/bitops.h>
35 #include <asm/bootinfo.h>
36 #include <asm/btext.h>
37 #include "open_pic.h"
38
39 #ifdef CONFIG_FB
40 #include <asm/linux_logo.h>
41 #endif
42
43 /*
44 * Properties whose value is longer than this get excluded from our
45 * copy of the device tree. This way we don't waste space storing
46 * things like "driver,AAPL,MacOS,PowerPC" properties. But this value
47 * does need to be big enough to ensure that we don't lose things
48 * like the interrupt-map property on a PCI-PCI bridge.
49 */
50 #define MAX_PROPERTY_LENGTH 4096
51
52 struct prom_args {
53 const char *service;
54 int nargs;
55 int nret;
56 void *args[10];
57 };
58
59 struct pci_address {
60 unsigned a_hi;
61 unsigned a_mid;
62 unsigned a_lo;
63 };
64
65 struct pci_reg_property {
66 struct pci_address addr;
67 unsigned size_hi;
68 unsigned size_lo;
69 };
70
71 struct pci_range {
72 struct pci_address addr;
73 unsigned phys;
74 unsigned size_hi;
75 unsigned size_lo;
76 };
77
78 struct isa_reg_property {
79 unsigned space;
80 unsigned address;
81 unsigned size;
82 };
83
84 struct pci_intr_map {
85 struct pci_address addr;
86 unsigned dunno;
87 phandle int_ctrler;
88 unsigned intr;
89 };
90
91 typedef unsigned long interpret_func(struct device_node *, unsigned long,
92 int, int);
93 static interpret_func interpret_pci_props;
94 static interpret_func interpret_dbdma_props;
95 static interpret_func interpret_isa_props;
96 static interpret_func interpret_macio_props;
97 static interpret_func interpret_root_props;
98
99 #ifndef FB_MAX /* avoid pulling in all of the fb stuff */
100 #define FB_MAX 8
101 #endif
102 char *prom_display_paths[FB_MAX] __initdata = { 0, };
103 phandle prom_display_nodes[FB_MAX] __initdata;
104 unsigned int prom_num_displays __initdata = 0;
105 char *of_stdout_device __initdata = 0;
106 ihandle prom_disp_node __initdata = 0;
107
108 prom_entry prom __initdata = 0;
109 ihandle prom_chosen __initdata = 0;
110 ihandle prom_stdout __initdata = 0;
111
112 extern char *klimit;
113 char *bootpath;
114 char *bootdevice;
115
116 unsigned int rtas_data; /* physical pointer */
117 unsigned int rtas_entry; /* physical pointer */
118 unsigned int rtas_size;
119 unsigned int old_rtas;
120
121 /* Set for a newworld or CHRP machine */
122 int use_of_interrupt_tree;
123 struct device_node *dflt_interrupt_controller;
124 int num_interrupt_controllers;
125
126 int pmac_newworld;
127
128 static struct device_node *allnodes;
129
130 static void *call_prom(const char *service, int nargs, int nret, ...);
131 static void prom_exit(void);
132 static unsigned long copy_device_tree(unsigned long, unsigned long);
133 static unsigned long inspect_node(phandle, struct device_node *, unsigned long,
134 unsigned long, struct device_node ***);
135 static unsigned long finish_node(struct device_node *, unsigned long,
136 interpret_func *, int, int);
137 static unsigned long finish_node_interrupts(struct device_node *, unsigned long);
138 static unsigned long check_display(unsigned long);
139 static int prom_next_node(phandle *);
140 static void *early_get_property(unsigned long, unsigned long, char *);
141 static struct device_node *find_phandle(phandle);
142
143 #ifdef CONFIG_BOOTX_TEXT
144 static void setup_disp_fake_bi(ihandle dp);
145 #endif
146
147 extern void enter_rtas(void *);
148 void phys_call_rtas(int, int, int, ...);
149
150 extern char cmd_line[512]; /* XXX */
151 boot_infos_t *boot_infos;
152 unsigned long dev_tree_size;
153
154 #define ALIGN(x) (((x) + sizeof(unsigned long)-1) & -sizeof(unsigned long))
155
156 /* Is boot-info compatible ? */
157 #define BOOT_INFO_IS_COMPATIBLE(bi) ((bi)->compatible_version <= BOOT_INFO_VERSION)
158 #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
159 #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
160
161 /*
162 * Note that prom_init() and anything called from prom_init() must
163 * use the RELOC/PTRRELOC macros to access any static data in
164 * memory, since the kernel may be running at an address that is
165 * different from the address that it was linked at.
166 * (Note that strings count as static variables.)
167 */
168
169 static void __init
170 prom_exit()
171 {
172 struct prom_args args;
173 unsigned long offset = reloc_offset();
174
175 args.service = "exit";
176 args.nargs = 0;
177 args.nret = 0;
178 RELOC(prom)(&args);
179 for (;;) /* should never get here */
180 ;
181 }
182
183 void __init
184 prom_enter(void)
185 {
186 struct prom_args args;
187 unsigned long offset = reloc_offset();
188
189 args.service = RELOC("enter");
190 args.nargs = 0;
191 args.nret = 0;
192 RELOC(prom)(&args);
193 }
194
195 static void * __init
196 call_prom(const char *service, int nargs, int nret, ...)
197 {
198 va_list list;
199 int i;
200 unsigned long offset = reloc_offset();
201 struct prom_args prom_args;
202
203 prom_args.service = service;
204 prom_args.nargs = nargs;
205 prom_args.nret = nret;
206 va_start(list, nret);
207 for (i = 0; i < nargs; ++i)
208 prom_args.args[i] = va_arg(list, void *);
209 va_end(list);
210 for (i = 0; i < nret; ++i)
211 prom_args.args[i + nargs] = 0;
212 RELOC(prom)(&prom_args);
213 return prom_args.args[nargs];
214 }
215
216 void __init
217 prom_print(const char *msg)
218 {
219 const char *p, *q;
220 unsigned long offset = reloc_offset();
221
222 if (RELOC(prom_stdout) == 0)
223 return;
224
225 for (p = msg; *p != 0; p = q) {
226 for (q = p; *q != 0 && *q != '\n'; ++q)
227 ;
228 if (q > p)
229 call_prom(RELOC("write"), 3, 1, RELOC(prom_stdout),
230 p, q - p);
231 if (*q != 0) {
232 ++q;
233 call_prom(RELOC("write"), 3, 1, RELOC(prom_stdout),
234 RELOC("\r\n"), 2);
235 }
236 }
237 }
238
239 static void __init
240 prom_print_hex(unsigned int v)
241 {
242 char buf[16];
243 int i, c;
244
245 for (i = 0; i < 8; ++i) {
246 c = (v >> ((7-i)*4)) & 0xf;
247 c += (c >= 10)? ('a' - 10): '0';
248 buf[i] = c;
249 }
250 buf[i] = ' ';
251 buf[i+1] = 0;
252 prom_print(buf);
253 }
254
255 unsigned long smp_chrp_cpu_nr __initdata = 0;
256
257 #ifdef CONFIG_SMP
258 /*
259 * With CHRP SMP we need to use the OF to start the other
260 * processors so we can't wait until smp_boot_cpus (the OF is
261 * trashed by then) so we have to put the processors into
262 * a holding pattern controlled by the kernel (not OF) before
263 * we destroy the OF.
264 *
265 * This uses a chunk of high memory, puts some holding pattern
266 * code there and sends the other processors off to there until
267 * smp_boot_cpus tells them to do something. We do that by using
268 * physical address 0x0. The holding pattern checks that address
269 * until its cpu # is there, when it is that cpu jumps to
270 * __secondary_start(). smp_boot_cpus() takes care of setting those
271 * values.
272 *
273 * We also use physical address 0x4 here to tell when a cpu
274 * is in its holding pattern code.
275 *
276 * -- Cort
277 */
278 static void __init
279 prom_hold_cpus(unsigned long mem)
280 {
281 extern void __secondary_hold(void);
282 unsigned long i;
283 int cpu;
284 phandle node;
285 unsigned long offset = reloc_offset();
286 char type[16], *path;
287 unsigned int reg;
288
289 /*
290 * XXX: hack to make sure we're chrp, assume that if we're
291 * chrp we have a device_type property -- Cort
292 */
293 node = call_prom(RELOC("finddevice"), 1, 1, RELOC("/"));
294 if ( (int)call_prom(RELOC("getprop"), 4, 1, node,
295 RELOC("device_type"),type, sizeof(type)) <= 0)
296 return;
297
298 /* copy the holding pattern code to someplace safe (0) */
299 /* the holding pattern is now within the first 0x100
300 bytes of the kernel image -- paulus */
301 memcpy((void *)0, (void *)(KERNELBASE + offset), 0x100);
302 flush_icache_range(0, 0x100);
303
304 /* look for cpus */
305 *(unsigned long *)(0x0) = 0;
306 asm volatile("dcbf 0,%0": : "r" (0) : "memory");
307 for (node = 0; prom_next_node(&node); ) {
308 type[0] = 0;
309 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
310 type, sizeof(type));
311 if (strcmp(type, RELOC("cpu")) != 0)
312 continue;
313 path = (char *) mem;
314 memset(path, 0, 256);
315 if ((int) call_prom(RELOC("package-to-path"), 3, 1,
316 node, path, 255) < 0)
317 continue;
318 reg = -1;
319 call_prom(RELOC("getprop"), 4, 1, node, RELOC("reg"),
320 ®, sizeof(reg));
321 cpu = RELOC(smp_chrp_cpu_nr)++;
322 RELOC(smp_hw_index)[cpu] = reg;
323 /* XXX: hack - don't start cpu 0, this cpu -- Cort */
324 if (cpu == 0)
325 continue;
326 prom_print(RELOC("starting cpu "));
327 prom_print(path);
328 *(ulong *)(0x4) = 0;
329 call_prom(RELOC("start-cpu"), 3, 0, node,
330 __pa(__secondary_hold), cpu);
331 prom_print(RELOC("..."));
332 for ( i = 0 ; (i < 10000) && (*(ulong *)(0x4) == 0); i++ )
333 ;
334 if (*(ulong *)(0x4) == cpu)
335 prom_print(RELOC("ok\n"));
336 else {
337 prom_print(RELOC("failed: "));
338 prom_print_hex(*(ulong *)0x4);
339 prom_print(RELOC("\n"));
340 }
341 }
342 }
343 #endif /* CONFIG_SMP */
344
345 void __init
346 bootx_init(unsigned long r4, unsigned long phys)
347 {
348 boot_infos_t *bi = (boot_infos_t *) r4;
349 unsigned long space;
350 unsigned long ptr, x;
351 char *model;
352 unsigned long offset = reloc_offset();
353
354 RELOC(boot_infos) = PTRUNRELOC(bi);
355 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
356 bi->logicalDisplayBase = 0;
357
358 #ifdef CONFIG_BOOTX_TEXT
359 btext_init(bi);
360
361 /*
362 * Test if boot-info is compatible. Done only in config
363 * CONFIG_BOOTX_TEXT since there is nothing much we can do
364 * with an incompatible version, except display a message
365 * and eventually hang the processor...
366 *
367 * I'll try to keep enough of boot-info compatible in the
368 * future to always allow display of this message;
369 */
370 if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
371 btext_drawstring(RELOC(" !!! WARNING - Incompatible version of BootX !!!\n\n\n"));
372 btext_flushscreen();
373 }
374 #endif /* CONFIG_BOOTX_TEXT */
375
376 /* New BootX enters kernel with MMU off, i/os are not allowed
377 here. This hack will have been done by the boostrap anyway.
378 */
379 if (bi->version < 4) {
380 /*
381 * XXX If this is an iMac, turn off the USB controller.
382 */
383 model = (char *) early_get_property
384 (r4 + bi->deviceTreeOffset, 4, RELOC("model"));
385 if (model
386 && (strcmp(model, RELOC("iMac,1")) == 0
387 || strcmp(model, RELOC("PowerMac1,1")) == 0)) {
388 out_le32((unsigned *)0x80880008, 1); /* XXX */
389 }
390 }
391
392 /* Move klimit to enclose device tree, args, ramdisk, etc... */
393 if (bi->version < 5) {
394 space = bi->deviceTreeOffset + bi->deviceTreeSize;
395 if (bi->ramDisk)
396 space = bi->ramDisk + bi->ramDiskSize;
397 } else
398 space = bi->totalParamsSize;
399 RELOC(klimit) = PTRUNRELOC((char *) bi + space);
400
401 /* New BootX will have flushed all TLBs and enters kernel with
402 MMU switched OFF, so this should not be useful anymore.
403 */
404 if (bi->version < 4) {
405 /*
406 * Touch each page to make sure the PTEs for them
407 * are in the hash table - the aim is to try to avoid
408 * getting DSI exceptions while copying the kernel image.
409 */
410 for (ptr = (KERNELBASE + offset) & PAGE_MASK;
411 ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
412 x = *(volatile unsigned long *)ptr;
413 }
414
415 #ifdef CONFIG_BOOTX_TEXT
416 /*
417 * Note that after we call prepare_disp_BAT, we can't do
418 * prom_draw*, flushscreen or clearscreen until we turn the MMU
419 * on, since prepare_disp_BAT sets disp_bi->logicalDisplayBase
420 * to a virtual address.
421 */
422 btext_prepare_BAT();
423 #endif
424 }
425
426 #ifdef CONFIG_PPC64BRIDGE
427 /*
428 * Set up a hash table with a set of entries in it to map the
429 * first 64MB of RAM. This is used on 64-bit machines since
430 * some of them don't have BATs.
431 * We assume the PTE will fit in the primary PTEG.
432 */
433
434 static inline void make_pte(unsigned long htab, unsigned int hsize,
435 unsigned int va, unsigned int pa, int mode)
436 {
437 unsigned int *pteg;
438 unsigned int hash, i, vsid;
439
440 vsid = ((va >> 28) * 0x111) << 12;
441 hash = ((va ^ vsid) >> 5) & 0x7fff80;
442 pteg = (unsigned int *)(htab + (hash & (hsize - 1)));
443 for (i = 0; i < 8; ++i, pteg += 4) {
444 if ((pteg[1] & 1) == 0) {
445 pteg[1] = vsid | ((va >> 16) & 0xf80) | 1;
446 pteg[3] = pa | mode;
447 break;
448 }
449 }
450 }
451
452 extern unsigned long _SDR1;
453 extern PTE *Hash;
454 extern unsigned long Hash_size;
455
456 static void __init
457 prom_alloc_htab(void)
458 {
459 unsigned int hsize;
460 unsigned long htab;
461 unsigned int addr;
462 unsigned long offset = reloc_offset();
463
464 /*
465 * Because of OF bugs we can't use the "claim" client
466 * interface to allocate memory for the hash table.
467 * This code is only used on 64-bit PPCs, and the only
468 * 64-bit PPCs at the moment are RS/6000s, and their
469 * OF is based at 0xc00000 (the 12M point), so we just
470 * arbitrarily use the 0x800000 - 0xc00000 region for the
471 * hash table.
472 * -- paulus.
473 */
474 #ifdef CONFIG_POWER4
475 hsize = 4 << 20; /* POWER4 has no BATs */
476 #else
477 hsize = 2 << 20;
478 #endif /* CONFIG_POWER4 */
479 htab = (8 << 20);
480 RELOC(Hash) = (void *)(htab + KERNELBASE);
481 RELOC(Hash_size) = hsize;
482 RELOC(_SDR1) = htab + __ilog2(hsize) - 18;
483
484 /*
485 * Put in PTEs for the first 64MB of RAM
486 */
487 cacheable_memzero((void *)htab, hsize);
488 for (addr = 0; addr < 0x4000000; addr += 0x1000)
489 make_pte(htab, hsize, addr + KERNELBASE, addr,
490 _PAGE_ACCESSED | _PAGE_COHERENT | PP_RWXX);
491 }
492 #endif /* CONFIG_PPC64BRIDGE */
493
494 static void __init
495 prom_instantiate_rtas(void)
496 {
497 ihandle prom_rtas;
498 unsigned int i;
499 struct prom_args prom_args;
500 unsigned long offset = reloc_offset();
501
502 prom_rtas = call_prom(RELOC("finddevice"), 1, 1, RELOC("/rtas"));
503 if (prom_rtas == (void *) -1)
504 return;
505
506 RELOC(rtas_size) = 0;
507 call_prom(RELOC("getprop"), 4, 1, prom_rtas,
508 RELOC("rtas-size"), &RELOC(rtas_size), sizeof(rtas_size));
509 prom_print(RELOC("instantiating rtas"));
510 if (RELOC(rtas_size) == 0) {
511 RELOC(rtas_data) = 0;
512 } else {
513 /*
514 * Ask OF for some space for RTAS.
515 * Actually OF has bugs so we just arbitrarily
516 * use memory at the 6MB point.
517 */
518 RELOC(rtas_data) = 6 << 20;
519 prom_print(RELOC(" at "));
520 prom_print_hex(RELOC(rtas_data));
521 }
522
523 prom_rtas = call_prom(RELOC("open"), 1, 1, RELOC("/rtas"));
524 prom_print(RELOC("..."));
525 prom_args.service = RELOC("call-method");
526 prom_args.nargs = 3;
527 prom_args.nret = 2;
528 prom_args.args[0] = RELOC("instantiate-rtas");
529 prom_args.args[1] = prom_rtas;
530 prom_args.args[2] = (void *) RELOC(rtas_data);
531 RELOC(prom)(&prom_args);
532 i = 0;
533 if (prom_args.args[3] == 0)
534 i = (unsigned int)prom_args.args[4];
535 RELOC(rtas_entry) = i;
536 if ((RELOC(rtas_entry) == -1) || (RELOC(rtas_entry) == 0))
537 prom_print(RELOC(" failed\n"));
538 else
539 prom_print(RELOC(" done\n"));
540 }
541
542 /*
543 * We enter here early on, when the Open Firmware prom is still
544 * handling exceptions and the MMU hash table for us.
545 */
546 unsigned long __init
547 prom_init(int r3, int r4, prom_entry pp)
548 {
549 unsigned long mem;
550 ihandle prom_mmu;
551 unsigned long offset = reloc_offset();
552 int l;
553 char *p, *d;
554 unsigned long phys;
555
556 /* Default */
557 phys = offset + KERNELBASE;
558
559 /* First get a handle for the stdout device */
560 RELOC(prom) = pp;
561 RELOC(prom_chosen) = call_prom(RELOC("finddevice"), 1, 1,
562 RELOC("/chosen"));
563 if (RELOC(prom_chosen) == (void *)-1)
564 prom_exit();
565 if ((int) call_prom(RELOC("getprop"), 4, 1, RELOC(prom_chosen),
566 RELOC("stdout"), &RELOC(prom_stdout),
567 sizeof(prom_stdout)) <= 0)
568 prom_exit();
569
570 /* Get the full OF pathname of the stdout device */
571 mem = (unsigned long) RELOC(klimit) + offset;
572 p = (char *) mem;
573 memset(p, 0, 256);
574 call_prom(RELOC("instance-to-path"), 3, 1, RELOC(prom_stdout), p, 255);
575 RELOC(of_stdout_device) = PTRUNRELOC(p);
576 mem += strlen(p) + 1;
577
578 /* Get the boot device and translate it to a full OF pathname. */
579 p = (char *) mem;
580 l = (int) call_prom(RELOC("getprop"), 4, 1, RELOC(prom_chosen),
581 RELOC("bootpath"), p, 1<<20);
582 if (l > 0) {
583 p[l] = 0; /* should already be null-terminated */
584 RELOC(bootpath) = PTRUNRELOC(p);
585 mem += l + 1;
586 d = (char *) mem;
587 *d = 0;
588 call_prom(RELOC("canon"), 3, 1, p, d, 1<<20);
589 RELOC(bootdevice) = PTRUNRELOC(d);
590 mem = ALIGN(mem + strlen(d) + 1);
591 }
592
593 prom_instantiate_rtas();
594
595 #ifdef CONFIG_PPC64BRIDGE
596 /*
597 * Find out how much memory we have and allocate a
598 * suitably-sized hash table.
599 */
600 prom_alloc_htab();
601 #endif
602
603 mem = check_display(mem);
604
605 prom_print(RELOC("copying OF device tree..."));
606 mem = copy_device_tree(mem, mem + (1<<20));
607 prom_print(RELOC("done\n"));
608
609 #ifdef CONFIG_SMP
610 prom_hold_cpus(mem);
611 #endif
612
613 RELOC(klimit) = (char *) (mem - offset);
614
615 /* If we are already running at 0xc0000000, we assume we were loaded by
616 * an OF bootloader which did set a BAT for us. This breaks OF translate
617 * so we force phys to be 0
618 */
619 if (offset == 0)
620 phys = 0;
621 else {
622 if ((int) call_prom(RELOC("getprop"), 4, 1, RELOC(prom_chosen),
623 RELOC("mmu"), &prom_mmu, sizeof(prom_mmu)) <= 0) {
624 prom_print(RELOC(" no MMU found\n"));
625 } else {
626 int nargs;
627 struct prom_args prom_args;
628 nargs = 4;
629 prom_args.service = RELOC("call-method");
630 prom_args.nargs = nargs;
631 prom_args.nret = 4;
632 prom_args.args[0] = RELOC("translate");
633 prom_args.args[1] = prom_mmu;
634 prom_args.args[2] = (void *)(offset + KERNELBASE);
635 prom_args.args[3] = (void *)1;
636 RELOC(prom)(&prom_args);
637
638 /* We assume the phys. address size is 3 cells */
639 if (prom_args.args[nargs] != 0)
640 prom_print(RELOC(" (translate failed)\n"));
641 else
642 phys = (unsigned long)prom_args.args[nargs+3];
643 }
644 }
645
646 #ifdef CONFIG_BOOTX_TEXT
647 if (RELOC(prom_disp_node) != 0)
648 setup_disp_fake_bi(RELOC(prom_disp_node));
649 #endif
650
651 /* Use quiesce call to get OF to shut down any devices it's using */
652 prom_print(RELOC("Calling quiesce ...\n"));
653 call_prom(RELOC("quiesce"), 0, 0);
654
655 #ifdef CONFIG_BOOTX_TEXT
656 btext_prepare_BAT();
657 #endif
658
659 prom_print(RELOC("returning "));
660 prom_print_hex(phys);
661 prom_print(RELOC(" from prom_init\n"));
662 RELOC(prom_stdout) = 0;
663
664 return phys;
665 }
666
667 void phys_call_rtas(int service, int nargs, int nret, ...)
668 {
669 va_list list;
670 union {
671 unsigned long words[16];
672 double align;
673 } u;
674 unsigned long offset = reloc_offset();
675 void (*rtas)(void *, unsigned long);
676 int i;
677
678 u.words[0] = service;
679 u.words[1] = nargs;
680 u.words[2] = nret;
681 va_start(list, nret);
682 for (i = 0; i < nargs; ++i)
683 u.words[i+3] = va_arg(list, unsigned long);
684 va_end(list);
685
686 rtas = (void (*)(void *, unsigned long)) RELOC(rtas_entry);
687 rtas(&u, RELOC(rtas_data));
688 }
689
690 static int __init
691 prom_set_color(ihandle ih, int i, int r, int g, int b)
692 {
693 struct prom_args prom_args;
694 unsigned long offset = reloc_offset();
695
696 prom_args.service = RELOC("call-method");
697 prom_args.nargs = 6;
698 prom_args.nret = 1;
699 prom_args.args[0] = RELOC("color!");
700 prom_args.args[1] = ih;
701 prom_args.args[2] = (void *) i;
702 prom_args.args[3] = (void *) b;
703 prom_args.args[4] = (void *) g;
704 prom_args.args[5] = (void *) r;
705 RELOC(prom)(&prom_args);
706 return (int) prom_args.args[6];
707 }
708
709 /*
710 * If we have a display that we don't know how to drive,
711 * we will want to try to execute OF's open method for it
712 * later. However, OF will probably fall over if we do that
713 * we've taken over the MMU.
714 * So we check whether we will need to open the display,
715 * and if so, open it now.
716 */
717 static unsigned long __init
718 check_display(unsigned long mem)
719 {
720 phandle node;
721 ihandle ih;
722 int i;
723 unsigned long offset = reloc_offset();
724 char type[16], *path;
725 static unsigned char default_colors[] = {
726 0x00, 0x00, 0x00,
727 0x00, 0x00, 0xaa,
728 0x00, 0xaa, 0x00,
729 0x00, 0xaa, 0xaa,
730 0xaa, 0x00, 0x00,
731 0xaa, 0x00, 0xaa,
732 0xaa, 0xaa, 0x00,
733 0xaa, 0xaa, 0xaa,
734 0x55, 0x55, 0x55,
735 0x55, 0x55, 0xff,
736 0x55, 0xff, 0x55,
737 0x55, 0xff, 0xff,
738 0xff, 0x55, 0x55,
739 0xff, 0x55, 0xff,
740 0xff, 0xff, 0x55,
741 0xff, 0xff, 0xff
742 };
743
744 RELOC(prom_disp_node) = 0;
745
746 for (node = 0; prom_next_node(&node); ) {
747 type[0] = 0;
748 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
749 type, sizeof(type));
750 if (strcmp(type, RELOC("display")) != 0)
751 continue;
752 /* It seems OF doesn't null-terminate the path :-( */
753 path = (char *) mem;
754 memset(path, 0, 256);
755 if ((int) call_prom(RELOC("package-to-path"), 3, 1,
756 node, path, 255) < 0)
757 continue;
758
759 /*
760 * If this display is the device that OF is using for stdout,
761 * move it to the front of the list.
762 */
763 mem += strlen(path) + 1;
764 i = RELOC(prom_num_displays)++;
765 if (RELOC(of_stdout_device) != 0 && i > 0
766 && strcmp(PTRRELOC(RELOC(of_stdout_device)), path) == 0) {
767 for (; i > 0; --i) {
768 RELOC(prom_display_paths[i])
769 = RELOC(prom_display_paths[i-1]);
770 RELOC(prom_display_nodes[i])
771 = RELOC(prom_display_nodes[i-1]);
772 }
773 }
774 RELOC(prom_display_paths[i]) = PTRUNRELOC(path);
775 RELOC(prom_display_nodes[i]) = node;
776 if (i == 0)
777 RELOC(prom_disp_node) = node;
778 if (RELOC(prom_num_displays) >= FB_MAX)
779 break;
780 }
781
782 try_again:
783 /*
784 * Open the first display and set its colormap.
785 */
786 if (RELOC(prom_num_displays) > 0) {
787 path = PTRRELOC(RELOC(prom_display_paths[0]));
788 prom_print(RELOC("opening display "));
789 prom_print(path);
790 ih = call_prom(RELOC("open"), 1, 1, path);
791 if (ih == 0 || ih == (ihandle) -1) {
792 prom_print(RELOC("... failed\n"));
793 for (i=1; i<RELOC(prom_num_displays); i++) {
794 RELOC(prom_display_paths[i-1]) = RELOC(prom_display_paths[i]);
795 RELOC(prom_display_nodes[i-1]) = RELOC(prom_display_nodes[i]);
796 }
797 if (--RELOC(prom_num_displays) > 0)
798 RELOC(prom_disp_node) = RELOC(prom_display_nodes[0]);
799 else
800 RELOC(prom_disp_node) = NULL;
801 goto try_again;
802 } else {
803 prom_print(RELOC("... ok\n"));
804 /*
805 * Setup a usable color table when the appropriate
806 * method is available.
807 * Should update this to use set-colors.
808 */
809 for (i = 0; i < 32; i++)
810 if (prom_set_color(ih, i, RELOC(default_colors)[i*3],
811 RELOC(default_colors)[i*3+1],
812 RELOC(default_colors)[i*3+2]) != 0)
813 break;
814
815 #ifdef CONFIG_FB
816 for (i = 0; i < LINUX_LOGO_COLORS; i++)
817 if (prom_set_color(ih, i + 32,
818 RELOC(linux_logo_red)[i],
819 RELOC(linux_logo_green)[i],
820 RELOC(linux_logo_blue)[i]) != 0)
821 break;
822 #endif /* CONFIG_FB */
823 }
824 }
825
826 return ALIGN(mem);
827 }
828
829 /* This function will enable the early boot text when doing OF booting. This
830 * way, xmon output should work too
831 */
832 #ifdef CONFIG_BOOTX_TEXT
833 static void __init
834 setup_disp_fake_bi(ihandle dp)
835 {
836 int width = 640, height = 480, depth = 8, pitch;
837 unsigned address;
838 unsigned long offset = reloc_offset();
839 struct pci_reg_property addrs[8];
840 int i, naddrs;
841 char name[32];
842 char *getprop = RELOC("getprop");
843
844 prom_print(RELOC("Initializing fake screen: "));
845
846 memset(name, 0, sizeof(name));
847 call_prom(getprop, 4, 1, dp, RELOC("name"), name, sizeof(name));
848 name[sizeof(name)-1] = 0;
849 prom_print(name);
850 prom_print(RELOC("\n"));
851 call_prom(getprop, 4, 1, dp, RELOC("width"), &width, sizeof(width));
852 call_prom(getprop, 4, 1, dp, RELOC("height"), &height, sizeof(height));
853 call_prom(getprop, 4, 1, dp, RELOC("depth"), &depth, sizeof(depth));
854 pitch = width * ((depth + 7) / 8);
855 call_prom(getprop, 4, 1, dp, RELOC("linebytes"),
856 &pitch, sizeof(pitch));
857 if (pitch == 1)
858 pitch = 0x1000; /* for strange IBM display */
859 address = 0;
860 call_prom(getprop, 4, 1, dp, RELOC("address"),
861 &address, sizeof(address));
862 if (address == 0) {
863 /* look for an assigned address with a size of >= 1MB */
864 naddrs = (int) call_prom(getprop, 4, 1, dp,
865 RELOC("assigned-addresses"),
866 addrs, sizeof(addrs));
867 naddrs /= sizeof(struct pci_reg_property);
868 for (i = 0; i < naddrs; ++i) {
869 if (addrs[i].size_lo >= (1 << 20)) {
870 address = addrs[i].addr.a_lo;
871 /* use the BE aperture if possible */
872 if (addrs[i].size_lo >= (16 << 20))
873 address += (8 << 20);
874 break;
875 }
876 }
877 if (address == 0) {
878 prom_print(RELOC("Failed to get address\n"));
879 return;
880 }
881 }
882 /* kludge for valkyrie */
883 if (strcmp(name, RELOC("valkyrie")) == 0)
884 address += 0x1000;
885
886 btext_setup_display(width, height, depth, pitch, address);
887 }
888 #endif
889
890 static int __init
891 prom_next_node(phandle *nodep)
892 {
893 phandle node;
894 unsigned long offset = reloc_offset();
895
896 if ((node = *nodep) != 0
897 && (*nodep = call_prom(RELOC("child"), 1, 1, node)) != 0)
898 return 1;
899 if ((*nodep = call_prom(RELOC("peer"), 1, 1, node)) != 0)
900 return 1;
901 for (;;) {
902 if ((node = call_prom(RELOC("parent"), 1, 1, node)) == 0)
903 return 0;
904 if ((*nodep = call_prom(RELOC("peer"), 1, 1, node)) != 0)
905 return 1;
906 }
907 }
908
909 /*
910 * Make a copy of the device tree from the PROM.
911 */
912 static unsigned long __init
913 copy_device_tree(unsigned long mem_start, unsigned long mem_end)
914 {
915 phandle root;
916 unsigned long new_start;
917 struct device_node **allnextp;
918 unsigned long offset = reloc_offset();
919
920 root = call_prom(RELOC("peer"), 1, 1, (phandle)0);
921 if (root == (phandle)0) {
922 prom_print(RELOC("couldn't get device tree root\n"));
923 prom_exit();
924 }
925 allnextp = &RELOC(allnodes);
926 mem_start = ALIGN(mem_start);
927 new_start = inspect_node(root, 0, mem_start, mem_end, &allnextp);
928 *allnextp = 0;
929 return new_start;
930 }
931
932 static unsigned long __init
933 inspect_node(phandle node, struct device_node *dad,
934 unsigned long mem_start, unsigned long mem_end,
935 struct device_node ***allnextpp)
936 {
937 int l;
938 phandle child;
939 struct device_node *np;
940 struct property *pp, **prev_propp;
941 char *prev_name, *namep;
942 unsigned char *valp;
943 unsigned long offset = reloc_offset();
944
945 np = (struct device_node *) mem_start;
946 mem_start += sizeof(struct device_node);
947 memset(np, 0, sizeof(*np));
948 np->node = node;
949 **allnextpp = PTRUNRELOC(np);
950 *allnextpp = &np->allnext;
951 if (dad != 0) {
952 np->parent = PTRUNRELOC(dad);
953 /* we temporarily use the `next' field as `last_child'. */
954 if (dad->next == 0)
955 dad->child = PTRUNRELOC(np);
956 else
957 dad->next->sibling = PTRUNRELOC(np);
958 dad->next = np;
959 }
960
961 /* get and store all properties */
962 prev_propp = &np->properties;
963 prev_name = RELOC("");
964 for (;;) {
965 pp = (struct property *) mem_start;
966 namep = (char *) (pp + 1);
967 pp->name = PTRUNRELOC(namep);
968 if ((int) call_prom(RELOC("nextprop"), 3, 1, node, prev_name,
969 namep) <= 0)
970 break;
971 mem_start = ALIGN((unsigned long)namep + strlen(namep) + 1);
972 prev_name = namep;
973 valp = (unsigned char *) mem_start;
974 pp->value = PTRUNRELOC(valp);
975 pp->length = (int)
976 call_prom(RELOC("getprop"), 4, 1, node, namep,
977 valp, mem_end - mem_start);
978 if (pp->length < 0)
979 continue;
980 #ifdef MAX_PROPERTY_LENGTH
981 if (pp->length > MAX_PROPERTY_LENGTH)
982 continue; /* ignore this property */
983 #endif
984 mem_start = ALIGN(mem_start + pp->length);
985 *prev_propp = PTRUNRELOC(pp);
986 prev_propp = &pp->next;
987 }
988 if (np->node != NULL) {
989 /* Add a "linux,phandle" property" */
990 pp = (struct property *) mem_start;
991 *prev_propp = PTRUNRELOC(pp);
992 prev_propp = &pp->next;
993 namep = (char *) (pp + 1);
994 pp->name = PTRUNRELOC(namep);
995 strcpy(namep, RELOC("linux,phandle"));
996 mem_start = ALIGN((unsigned long)namep + strlen(namep) + 1);
997 pp->value = (unsigned char *) PTRUNRELOC(&np->node);
998 pp->length = sizeof(np->node);
999 }
1000 *prev_propp = NULL;
1001
1002 /* get the node's full name */
1003 l = (int) call_prom(RELOC("package-to-path"), 3, 1, node,
1004 (char *) mem_start, mem_end - mem_start);
1005 if (l >= 0) {
1006 np->full_name = PTRUNRELOC((char *) mem_start);
1007 *(char *)(mem_start + l) = 0;
1008 mem_start = ALIGN(mem_start + l + 1);
1009 }
1010
1011 /* do all our children */
1012 child = call_prom(RELOC("child"), 1, 1, node);
1013 while (child != (void *)0) {
1014 mem_start = inspect_node(child, np, mem_start, mem_end,
1015 allnextpp);
1016 child = call_prom(RELOC("peer"), 1, 1, child);
1017 }
1018
1019 return mem_start;
1020 }
1021
1022 /*
1023 * finish_device_tree is called once things are running normally
1024 * (i.e. with text and data mapped to the address they were linked at).
1025 * It traverses the device tree and fills in the name, type,
1026 * {n_}addrs and {n_}intrs fields of each node.
1027 */
1028 void __init
1029 finish_device_tree(void)
1030 {
1031 unsigned long mem = (unsigned long) klimit;
1032 struct device_node *np;
1033
1034 /* All newworld pmac machines and CHRPs now use the interrupt tree */
1035 for (np = allnodes; np != NULL; np = np->allnext) {
1036 if (get_property(np, "interrupt-parent", 0)) {
1037 use_of_interrupt_tree = 1;
1038 break;
1039 }
1040 }
1041 if (_machine == _MACH_Pmac && use_of_interrupt_tree)
1042 pmac_newworld = 1;
1043
1044 #ifdef CONFIG_BOOTX_TEXT
1045 if (boot_infos && pmac_newworld) {
1046 prom_print("WARNING ! BootX/miBoot booting is not supported on this machine\n");
1047 prom_print(" You should use an Open Firmware bootloader\n");
1048 }
1049 #endif /* CONFIG_BOOTX_TEXT */
1050
1051 if (use_of_interrupt_tree) {
1052 /*
1053 * We want to find out here how many interrupt-controller
1054 * nodes there are, and if we are booted from BootX,
1055 * we need a pointer to the first (and hopefully only)
1056 * such node. But we can't use find_devices here since
1057 * np->name has not been set yet. -- paulus
1058 */
1059 int n = 0;
1060 char *name, *ic;
1061 int iclen;
1062
1063 for (np = allnodes; np != NULL; np = np->allnext) {
1064 ic = get_property(np, "interrupt-controller", &iclen);
1065 name = get_property(np, "name", NULL);
1066 /* checking iclen makes sure we don't get a false
1067 match on /chosen.interrupt_controller */
1068 if ((name != NULL
1069 && strcmp(name, "interrupt-controller") == 0)
1070 || (ic != NULL && iclen == 0)) {
1071 if (n == 0)
1072 dflt_interrupt_controller = np;
1073 ++n;
1074 }
1075 }
1076 num_interrupt_controllers = n;
1077 }
1078
1079 mem = finish_node(allnodes, mem, NULL, 1, 1);
1080 dev_tree_size = mem - (unsigned long) allnodes;
1081 klimit = (char *) mem;
1082 }
1083
1084 /*
1085 * early_get_property is used to access the device tree image prepared
1086 * by BootX very early on, before the pointers in it have been relocated.
1087 */
1088 static void * __init
1089 early_get_property(unsigned long base, unsigned long node, char *prop)
1090 {
1091 struct device_node *np = (struct device_node *)(base + node);
1092 struct property *pp;
1093
1094 for (pp = np->properties; pp != 0; pp = pp->next) {
1095 pp = (struct property *) (base + (unsigned long)pp);
1096 if (strcmp((char *)((unsigned long)pp->name + base),
1097 prop) == 0) {
1098 return (void *)((unsigned long)pp->value + base);
1099 }
1100 }
1101 return 0;
1102 }
1103
1104 static unsigned long __init
1105 finish_node(struct device_node *np, unsigned long mem_start,
1106 interpret_func *ifunc, int naddrc, int nsizec)
1107 {
1108 struct device_node *child;
1109 int *ip;
1110
1111 np->name = get_property(np, "name", 0);
1112 np->type = get_property(np, "device_type", 0);
1113
1114 /* get the device addresses and interrupts */
1115 if (ifunc != NULL) {
1116 mem_start = ifunc(np, mem_start, naddrc, nsizec);
1117 }
1118 if (use_of_interrupt_tree)
1119 mem_start = finish_node_interrupts(np, mem_start);
1120
1121 /* Look for #address-cells and #size-cells properties. */
1122 ip = (int *) get_property(np, "#address-cells", 0);
1123 if (ip != NULL)
1124 naddrc = *ip;
1125 ip = (int *) get_property(np, "#size-cells", 0);
1126 if (ip != NULL)
1127 nsizec = *ip;
1128
1129 /* the f50 sets the name to 'display' and 'compatible' to what we
1130 * expect for the name -- Cort
1131 */
1132 if (!strcmp(np->name, "display"))
1133 np->name = get_property(np, "compatible", 0);
1134
1135 if (np->parent == NULL)
1136 ifunc = interpret_root_props;
1137 else if (np->type == 0)
1138 ifunc = NULL;
1139 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
1140 ifunc = interpret_pci_props;
1141 else if (!strcmp(np->type, "dbdma"))
1142 ifunc = interpret_dbdma_props;
1143 else if (!strcmp(np->type, "mac-io")
1144 || ifunc == interpret_macio_props)
1145 ifunc = interpret_macio_props;
1146 else if (!strcmp(np->type, "isa"))
1147 ifunc = interpret_isa_props;
1148 else if (!((ifunc == interpret_dbdma_props
1149 || ifunc == interpret_macio_props)
1150 && (!strcmp(np->type, "escc")
1151 || !strcmp(np->type, "media-bay"))))
1152 ifunc = NULL;
1153
1154 /* if we were booted from BootX, convert the full name */
1155 if (boot_infos
1156 && strncmp(np->full_name, "Devices:device-tree", 19) == 0) {
1157 if (np->full_name[19] == 0) {
1158 strcpy(np->full_name, "/");
1159 } else if (np->full_name[19] == ':') {
1160 char *p = np->full_name + 19;
1161 np->full_name = p;
1162 for (; *p; ++p)
1163 if (*p == ':')
1164 *p = '/';
1165 }
1166 }
1167
1168 for (child = np->child; child != NULL; child = child->sibling)
1169 mem_start = finish_node(child, mem_start, ifunc,
1170 naddrc, nsizec);
1171
1172 return mem_start;
1173 }
1174
1175 /*
1176 * Find the interrupt parent of a node.
1177 */
1178 static struct device_node * __init
1179 intr_parent(struct device_node *p)
1180 {
1181 phandle *parp;
1182
1183 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
1184 if (parp == NULL)
1185 return p->parent;
1186 p = find_phandle(*parp);
1187 if (p != NULL)
1188 return p;
1189 /*
1190 * On a powermac booted with BootX, we don't get to know the
1191 * phandles for any nodes, so find_phandle will return NULL.
1192 * Fortunately these machines only have one interrupt controller
1193 * so there isn't in fact any ambiguity. -- paulus
1194 */
1195 if (num_interrupt_controllers == 1)
1196 p = dflt_interrupt_controller;
1197 return p;
1198 }
1199
1200 /*
1201 * Find out the size of each entry of the interrupts property
1202 * for a node.
1203 */
1204 static int __init
1205 prom_n_intr_cells(struct device_node *np)
1206 {
1207 struct device_node *p;
1208 unsigned int *icp;
1209
1210 for (p = np; (p = intr_parent(p)) != NULL; ) {
1211 icp = (unsigned int *)
1212 get_property(p, "#interrupt-cells", NULL);
1213 if (icp != NULL)
1214 return *icp;
1215 if (get_property(p, "interrupt-controller", NULL) != NULL
1216 || get_property(p, "interrupt-map", NULL) != NULL) {
1217 printk("oops, node %s doesn't have #interrupt-cells\n",
1218 p->full_name);
1219 return 1;
1220 }
1221 }
1222 printk("prom_n_intr_cells failed for %s\n", np->full_name);
1223 return 1;
1224 }
1225
1226 /*
1227 * Map an interrupt from a device up to the platform interrupt
1228 * descriptor.
1229 */
1230 static int __init
1231 map_interrupt(unsigned int **irq, struct device_node **ictrler,
1232 struct device_node *np, unsigned int *ints, int nintrc)
1233 {
1234 struct device_node *p, *ipar;
1235 unsigned int *imap, *imask, *ip;
1236 int i, imaplen, match;
1237 int newintrc, newaddrc;
1238 unsigned int *reg;
1239 int naddrc;
1240
1241 reg = (unsigned int *) get_property(np, "reg", NULL);
1242 naddrc = prom_n_addr_cells(np);
1243 p = intr_parent(np);
1244 while (p != NULL) {
1245 if (get_property(p, "interrupt-controller", NULL) != NULL)
1246 /* this node is an interrupt controller, stop here */
1247 break;
1248 imap = (unsigned int *)
1249 get_property(p, "interrupt-map", &imaplen);
1250 if (imap == NULL) {
1251 p = intr_parent(p);
1252 continue;
1253 }
1254 imask = (unsigned int *)
1255 get_property(p, "interrupt-map-mask", NULL);
1256 if (imask == NULL) {
1257 printk("oops, %s has interrupt-map but no mask\n",
1258 p->full_name);
1259 return 0;
1260 }
1261 imaplen /= sizeof(unsigned int);
1262 match = 0;
1263 ipar = NULL;
1264 while (imaplen > 0 && !match) {
1265 /* check the child-interrupt field */
1266 match = 1;
1267 for (i = 0; i < naddrc && match; ++i)
1268 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
1269 for (; i < naddrc + nintrc && match; ++i)
1270 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
1271 imap += naddrc + nintrc;
1272 imaplen -= naddrc + nintrc;
1273 /* grab the interrupt parent */
1274 ipar = find_phandle((phandle) *imap++);
1275 --imaplen;
1276 if (ipar == NULL && num_interrupt_controllers == 1)
1277 /* cope with BootX not giving us phandles */
1278 ipar = dflt_interrupt_controller;
1279 if (ipar == NULL) {
1280 printk("oops, no int parent %x in map of %s\n",
1281 imap[-1], p->full_name);
1282 return 0;
1283 }
1284 /* find the parent's # addr and intr cells */
1285 ip = (unsigned int *)
1286 get_property(ipar, "#interrupt-cells", NULL);
1287 if (ip == NULL) {
1288 printk("oops, no #interrupt-cells on %s\n",
1289 ipar->full_name);
1290 return 0;
1291 }
1292 newintrc = *ip;
1293 ip = (unsigned int *)
1294 get_property(ipar, "#address-cells", NULL);
1295 newaddrc = (ip == NULL)? 0: *ip;
1296 imap += newaddrc + newintrc;
1297 imaplen -= newaddrc + newintrc;
1298 }
1299 if (imaplen < 0) {
1300 printk("oops, error decoding int-map on %s, len=%d\n",
1301 p->full_name, imaplen);
1302 return 0;
1303 }
1304 if (!match) {
1305 printk("oops, no match in %s int-map for %s\n",
1306 p->full_name, np->full_name);
1307 return 0;
1308 }
1309 p = ipar;
1310 naddrc = newaddrc;
1311 nintrc = newintrc;
1312 ints = imap - nintrc;
1313 reg = ints - naddrc;
1314 }
1315 if (p == NULL)
1316 printk("hmmm, int tree for %s doesn't have ctrler\n",
1317 np->full_name);
1318 *irq = ints;
1319 *ictrler = p;
1320 return nintrc;
1321 }
1322
1323 /*
1324 * New version of finish_node_interrupts.
1325 */
1326 static unsigned long __init
1327 finish_node_interrupts(struct device_node *np, unsigned long mem_start)
1328 {
1329 unsigned int *ints;
1330 int intlen, intrcells;
1331 int i, j, n, offset;
1332 unsigned int *irq;
1333 struct device_node *ic;
1334
1335 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
1336 if (ints == NULL)
1337 return mem_start;
1338 intrcells = prom_n_intr_cells(np);
1339 intlen /= intrcells * sizeof(unsigned int);
1340 np->n_intrs = intlen;
1341 np->intrs = (struct interrupt_info *) mem_start;
1342 mem_start += intlen * sizeof(struct interrupt_info);
1343
1344 for (i = 0; i < intlen; ++i) {
1345 np->intrs[i].line = 0;
1346 np->intrs[i].sense = 1;
1347 n = map_interrupt(&irq, &ic, np, ints, intrcells);
1348 if (n <= 0)
1349 continue;
1350 offset = 0;
1351 /*
1352 * On a CHRP we have an 8259 which is subordinate to
1353 * the openpic in the interrupt tree, but we want the
1354 * openpic's interrupt numbers offsetted, not the 8259's.
1355 * So we apply the offset if the controller is at the
1356 * root of the interrupt tree, i.e. has no interrupt-parent.
1357 * This doesn't cope with the general case of multiple
1358 * cascaded interrupt controllers, but then neither will
1359 * irq.c at the moment either. -- paulus
1360 */
1361 if (num_interrupt_controllers > 1 && ic != NULL
1362 && get_property(ic, "interrupt-parent", NULL) == NULL)
1363 offset = 16;
1364 np->intrs[i].line = irq[0] + offset;
1365 if (n > 1)
1366 np->intrs[i].sense = irq[1];
1367 if (n > 2) {
1368 printk("hmmm, got %d intr cells for %s:", n,
1369 np->full_name);
1370 for (j = 0; j < n; ++j)
1371 printk(" %d", irq[j]);
1372 printk("\n");
1373 }
1374 ints += intrcells;
1375 }
1376
1377 return mem_start;
1378 }
1379
1380 /*
1381 * When BootX makes a copy of the device tree from the MacOS
1382 * Name Registry, it is in the format we use but all of the pointers
1383 * are offsets from the start of the tree.
1384 * This procedure updates the pointers.
1385 */
1386 void __init
1387 relocate_nodes(void)
1388 {
1389 unsigned long base;
1390 struct device_node *np;
1391 struct property *pp;
1392
1393 #define ADDBASE(x) (x = (x)? ((typeof (x))((unsigned long)(x) + base)): 0)
1394
1395 base = (unsigned long) boot_infos + boot_infos->deviceTreeOffset;
1396 allnodes = (struct device_node *)(base + 4);
1397 for (np = allnodes; np != 0; np = np->allnext) {
1398 ADDBASE(np->full_name);
1399 ADDBASE(np->properties);
1400 ADDBASE(np->parent);
1401 ADDBASE(np->child);
1402 ADDBASE(np->sibling);
1403 ADDBASE(np->allnext);
1404 for (pp = np->properties; pp != 0; pp = pp->next) {
1405 ADDBASE(pp->name);
1406 ADDBASE(pp->value);
1407 ADDBASE(pp->next);
1408 }
1409 }
1410 }
1411
1412 int
1413 prom_n_addr_cells(struct device_node* np)
1414 {
1415 int* ip;
1416 do {
1417 if (np->parent)
1418 np = np->parent;
1419 ip = (int *) get_property(np, "#address-cells", 0);
1420 if (ip != NULL)
1421 return *ip;
1422 } while (np->parent);
1423 /* No #address-cells property for the root node, default to 1 */
1424 return 1;
1425 }
1426
1427 int
1428 prom_n_size_cells(struct device_node* np)
1429 {
1430 int* ip;
1431 do {
1432 if (np->parent)
1433 np = np->parent;
1434 ip = (int *) get_property(np, "#size-cells", 0);
1435 if (ip != NULL)
1436 return *ip;
1437 } while (np->parent);
1438 /* No #size-cells property for the root node, default to 1 */
1439 return 1;
1440 }
1441
1442 static unsigned long __init
1443 interpret_pci_props(struct device_node *np, unsigned long mem_start,
1444 int naddrc, int nsizec)
1445 {
1446 struct address_range *adr;
1447 struct pci_reg_property *pci_addrs;
1448 int i, l, *ip;
1449
1450 pci_addrs = (struct pci_reg_property *)
1451 get_property(np, "assigned-addresses", &l);
1452 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
1453 i = 0;
1454 adr = (struct address_range *) mem_start;
1455 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
1456 /* XXX assumes PCI addresses mapped 1-1 to physical */
1457 adr[i].space = pci_addrs[i].addr.a_hi;
1458 adr[i].address = pci_addrs[i].addr.a_lo;
1459 adr[i].size = pci_addrs[i].size_lo;
1460 ++i;
1461 }
1462 np->addrs = adr;
1463 np->n_addrs = i;
1464 mem_start += i * sizeof(struct address_range);
1465 }
1466
1467 if (use_of_interrupt_tree)
1468 return mem_start;
1469
1470 ip = (int *) get_property(np, "AAPL,interrupts", &l);
1471 if (ip == 0 && np->parent)
1472 ip = (int *) get_property(np->parent, "AAPL,interrupts", &l);
1473 if (ip == 0)
1474 ip = (int *) get_property(np, "interrupts", &l);
1475 if (ip != 0) {
1476 np->intrs = (struct interrupt_info *) mem_start;
1477 np->n_intrs = l / sizeof(int);
1478 mem_start += np->n_intrs * sizeof(struct interrupt_info);
1479 for (i = 0; i < np->n_intrs; ++i) {
1480 np->intrs[i].line = *ip++;
1481 np->intrs[i].sense = 1;
1482 }
1483 }
1484
1485 return mem_start;
1486 }
1487
1488 static unsigned long __init
1489 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
1490 int naddrc, int nsizec)
1491 {
1492 struct reg_property *rp;
1493 struct address_range *adr;
1494 unsigned long base_address;
1495 int i, l, *ip;
1496 struct device_node *db;
1497
1498 base_address = 0;
1499 for (db = np->parent; db != NULL; db = db->parent) {
1500 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
1501 base_address = db->addrs[0].address;
1502 break;
1503 }
1504 }
1505
1506 rp = (struct reg_property *) get_property(np, "reg", &l);
1507 if (rp != 0 && l >= sizeof(struct reg_property)) {
1508 i = 0;
1509 adr = (struct address_range *) mem_start;
1510 while ((l -= sizeof(struct reg_property)) >= 0) {
1511 adr[i].space = 0;
1512 adr[i].address = rp[i].address + base_address;
1513 adr[i].size = rp[i].size;
1514 ++i;
1515 }
1516 np->addrs = adr;
1517 np->n_addrs = i;
1518 mem_start += i * sizeof(struct address_range);
1519 }
1520
1521 if (use_of_interrupt_tree)
1522 return mem_start;
1523
1524 ip = (int *) get_property(np, "AAPL,interrupts", &l);
1525 if (ip == 0)
1526 ip = (int *) get_property(np, "interrupts", &l);
1527 if (ip != 0) {
1528 np->intrs = (struct interrupt_info *) mem_start;
1529 np->n_intrs = l / sizeof(int);
1530 mem_start += np->n_intrs * sizeof(struct interrupt_info);
1531 for (i = 0; i < np->n_intrs; ++i) {
1532 np->intrs[i].line = *ip++;
1533 np->intrs[i].sense = 1;
1534 }
1535 }
1536
1537 return mem_start;
1538 }
1539
1540 static unsigned long __init
1541 interpret_macio_props(struct device_node *np, unsigned long mem_start,
1542 int naddrc, int nsizec)
1543 {
1544 struct reg_property *rp;
1545 struct address_range *adr;
1546 unsigned long base_address;
1547 int i, l, keylargo, *ip;
1548 struct device_node *db;
1549
1550 base_address = 0;
1551 for (db = np->parent; db != NULL; db = db->parent) {
1552 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
1553 base_address = db->addrs[0].address;
1554 keylargo = device_is_compatible(db, "Keylargo");
1555 break;
1556 }
1557 }
1558
1559 rp = (struct reg_property *) get_property(np, "reg", &l);
1560 if (rp != 0 && l >= sizeof(struct reg_property)) {
1561 i = 0;
1562 adr = (struct address_range *) mem_start;
1563 while ((l -= sizeof(struct reg_property)) >= 0) {
1564 adr[i].space = 0;
1565 adr[i].address = rp[i].address + base_address;
1566 adr[i].size = rp[i].size;
1567 ++i;
1568 }
1569 np->addrs = adr;
1570 np->n_addrs = i;
1571 mem_start += i * sizeof(struct address_range);
1572 }
1573
1574 if (use_of_interrupt_tree)
1575 return mem_start;
1576
1577 ip = (int *) get_property(np, "interrupts", &l);
1578 if (ip == 0)
1579 ip = (int *) get_property(np, "AAPL,interrupts", &l);
1580 if (ip != 0) {
1581 np->intrs = (struct interrupt_info *) mem_start;
1582 np->n_intrs = l / sizeof(int);
1583 for (i = 0; i < np->n_intrs; ++i) {
1584 np->intrs[i].line = *ip++;
1585 np->intrs[i].sense = 1;
1586 }
1587 mem_start += np->n_intrs * sizeof(struct interrupt_info);
1588 }
1589
1590 return mem_start;
1591 }
1592
1593 static unsigned long __init
1594 interpret_isa_props(struct device_node *np, unsigned long mem_start,
1595 int naddrc, int nsizec)
1596 {
1597 struct isa_reg_property *rp;
1598 struct address_range *adr;
1599 int i, l, *ip;
1600
1601 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
1602 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
1603 i = 0;
1604 adr = (struct address_range *) mem_start;
1605 while ((l -= sizeof(struct reg_property)) >= 0) {
1606 adr[i].space = rp[i].space;
1607 adr[i].address = rp[i].address
1608 + (adr[i].space? 0: _ISA_MEM_BASE);
1609 adr[i].size = rp[i].size;
1610 ++i;
1611 }
1612 np->addrs = adr;
1613 np->n_addrs = i;
1614 mem_start += i * sizeof(struct address_range);
1615 }
1616
1617 if (use_of_interrupt_tree)
1618 return mem_start;
1619
1620 ip = (int *) get_property(np, "interrupts", &l);
1621 if (ip != 0) {
1622 np->intrs = (struct interrupt_info *) mem_start;
1623 np->n_intrs = l / (2 * sizeof(int));
1624 mem_start += np->n_intrs * sizeof(struct interrupt_info);
1625 for (i = 0; i < np->n_intrs; ++i) {
1626 np->intrs[i].line = *ip++;
1627 np->intrs[i].sense = *ip++;
1628 }
1629 }
1630
1631 return mem_start;
1632 }
1633
1634 static unsigned long __init
1635 interpret_root_props(struct device_node *np, unsigned long mem_start,
1636 int naddrc, int nsizec)
1637 {
1638 struct address_range *adr;
1639 int i, l, *ip;
1640 unsigned int *rp;
1641 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
1642
1643 rp = (unsigned int *) get_property(np, "reg", &l);
1644 if (rp != 0 && l >= rpsize) {
1645 i = 0;
1646 adr = (struct address_range *) mem_start;
1647 while ((l -= rpsize) >= 0) {
1648 adr[i].space = (naddrc >= 2? rp[naddrc-2]: 0);
1649 adr[i].address = rp[naddrc - 1];
1650 adr[i].size = rp[naddrc + nsizec - 1];
1651 ++i;
1652 rp += naddrc + nsizec;
1653 }
1654 np->addrs = adr;
1655 np->n_addrs = i;
1656 mem_start += i * sizeof(struct address_range);
1657 }
1658
1659 if (use_of_interrupt_tree)
1660 return mem_start;
1661
1662 ip = (int *) get_property(np, "AAPL,interrupts", &l);
1663 if (ip == 0)
1664 ip = (int *) get_property(np, "interrupts", &l);
1665 if (ip != 0) {
1666 np->intrs = (struct interrupt_info *) mem_start;
1667 np->n_intrs = l / sizeof(int);
1668 mem_start += np->n_intrs * sizeof(struct interrupt_info);
1669 for (i = 0; i < np->n_intrs; ++i) {
1670 np->intrs[i].line = *ip++;
1671 np->intrs[i].sense = 1;
1672 }
1673 }
1674
1675 return mem_start;
1676 }
1677
1678 /*
1679 * Work out the sense (active-low level / active-high edge)
1680 * of each interrupt from the device tree.
1681 */
1682 void __init
1683 prom_get_irq_senses(unsigned char *senses, int off, int max)
1684 {
1685 struct device_node *np;
1686 int i, j;
1687
1688 /* default to level-triggered */
1689 memset(senses, 1, max - off);
1690 if (!use_of_interrupt_tree)
1691 return;
1692
1693 for (np = allnodes; np != 0; np = np->allnext) {
1694 for (j = 0; j < np->n_intrs; j++) {
1695 i = np->intrs[j].line;
1696 if (i >= off && i < max)
1697 senses[i-off] = np->intrs[j].sense;
1698 }
1699 }
1700 }
1701
1702 /*
1703 * Construct and return a list of the device_nodes with a given name.
1704 */
1705 struct device_node *
1706 find_devices(const char *name)
1707 {
1708 struct device_node *head, **prevp, *np;
1709
1710 prevp = &head;
1711 for (np = allnodes; np != 0; np = np->allnext) {
1712 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1713 *prevp = np;
1714 prevp = &np->next;
1715 }
1716 }
1717 *prevp = 0;
1718 return head;
1719 }
1720
1721 /*
1722 * Construct and return a list of the device_nodes with a given type.
1723 */
1724 struct device_node *
1725 find_type_devices(const char *type)
1726 {
1727 struct device_node *head, **prevp, *np;
1728
1729 prevp = &head;
1730 for (np = allnodes; np != 0; np = np->allnext) {
1731 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1732 *prevp = np;
1733 prevp = &np->next;
1734 }
1735 }
1736 *prevp = 0;
1737 return head;
1738 }
1739
1740 /*
1741 * Returns all nodes linked together
1742 */
1743 struct device_node * __openfirmware
1744 find_all_nodes(void)
1745 {
1746 struct device_node *head, **prevp, *np;
1747
1748 prevp = &head;
1749 for (np = allnodes; np != 0; np = np->allnext) {
1750 *prevp = np;
1751 prevp = &np->next;
1752 }
1753 *prevp = 0;
1754 return head;
1755 }
1756
1757 /* Checks if the given "compat" string matches one of the strings in
1758 * the device's "compatible" property
1759 */
1760 int
1761 device_is_compatible(struct device_node *device, const char *compat)
1762 {
1763 const char* cp;
1764 int cplen, l;
1765
1766 cp = (char *) get_property(device, "compatible", &cplen);
1767 if (cp == NULL)
1768 return 0;
1769 while (cplen > 0) {
1770 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1771 return 1;
1772 l = strlen(cp) + 1;
1773 cp += l;
1774 cplen -= l;
1775 }
1776
1777 return 0;
1778 }
1779
1780
1781 /*
1782 * Indicates whether the root node has a given value in its
1783 * compatible property.
1784 */
1785 int
1786 machine_is_compatible(const char *compat)
1787 {
1788 struct device_node *root;
1789
1790 root = find_path_device("/");
1791 if (root == 0)
1792 return 0;
1793 return device_is_compatible(root, compat);
1794 }
1795
1796 /*
1797 * Construct and return a list of the device_nodes with a given type
1798 * and compatible property.
1799 */
1800 struct device_node *
1801 find_compatible_devices(const char *type, const char *compat)
1802 {
1803 struct device_node *head, **prevp, *np;
1804
1805 prevp = &head;
1806 for (np = allnodes; np != 0; np = np->allnext) {
1807 if (type != NULL
1808 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1809 continue;
1810 if (device_is_compatible(np, compat)) {
1811 *prevp = np;
1812 prevp = &np->next;
1813 }
1814 }
1815 *prevp = 0;
1816 return head;
1817 }
1818
1819 /*
1820 * Find the device_node with a given full_name.
1821 */
1822 struct device_node *
1823 find_path_device(const char *path)
1824 {
1825 struct device_node *np;
1826
1827 for (np = allnodes; np != 0; np = np->allnext)
1828 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1829 return np;
1830 return NULL;
1831 }
1832
1833 /*
1834 * Find the device_node with a given phandle.
1835 */
1836 static struct device_node * __init
1837 find_phandle(phandle ph)
1838 {
1839 struct device_node *np;
1840
1841 for (np = allnodes; np != 0; np = np->allnext)
1842 if (np->node == ph)
1843 return np;
1844 return NULL;
1845 }
1846
1847 /*
1848 * Find a property with a given name for a given node
1849 * and return the value.
1850 */
1851 unsigned char *
1852 get_property(struct device_node *np, const char *name, int *lenp)
1853 {
1854 struct property *pp;
1855
1856 for (pp = np->properties; pp != 0; pp = pp->next)
1857 if (pp->name != NULL && strcmp(pp->name, name) == 0) {
1858 if (lenp != 0)
1859 *lenp = pp->length;
1860 return pp->value;
1861 }
1862 return 0;
1863 }
1864
1865 /*
1866 * Add a property to a node
1867 */
1868 void __openfirmware
1869 prom_add_property(struct device_node* np, struct property* prop)
1870 {
1871 struct property **next = &np->properties;
1872
1873 prop->next = NULL;
1874 while (*next)
1875 next = &(*next)->next;
1876 *next = prop;
1877 }
1878
1879 #if 0
1880 void __openfirmware
1881 print_properties(struct device_node *np)
1882 {
1883 struct property *pp;
1884 char *cp;
1885 int i, n;
1886
1887 for (pp = np->properties; pp != 0; pp = pp->next) {
1888 printk(KERN_INFO "%s", pp->name);
1889 for (i = strlen(pp->name); i < 16; ++i)
1890 printk(" ");
1891 cp = (char *) pp->value;
1892 for (i = pp->length; i > 0; --i, ++cp)
1893 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1894 || (i == 1 && *cp != 0))
1895 break;
1896 if (i == 0 && pp->length > 1) {
1897 /* looks like a string */
1898 printk(" %s\n", (char *) pp->value);
1899 } else {
1900 /* dump it in hex */
1901 n = pp->length;
1902 if (n > 64)
1903 n = 64;
1904 if (pp->length % 4 == 0) {
1905 unsigned int *p = (unsigned int *) pp->value;
1906
1907 n /= 4;
1908 for (i = 0; i < n; ++i) {
1909 if (i != 0 && (i % 4) == 0)
1910 printk("\n ");
1911 printk(" %08x", *p++);
1912 }
1913 } else {
1914 unsigned char *bp = pp->value;
1915
1916 for (i = 0; i < n; ++i) {
1917 if (i != 0 && (i % 16) == 0)
1918 printk("\n ");
1919 printk(" %02x", *bp++);
1920 }
1921 }
1922 printk("\n");
1923 if (pp->length > 64)
1924 printk(" ... (length = %d)\n",
1925 pp->length);
1926 }
1927 }
1928 }
1929 #endif
1930
1931 spinlock_t rtas_lock = SPIN_LOCK_UNLOCKED;
1932
1933 /* this can be called after setup -- Cort */
1934 int __openfirmware
1935 call_rtas(const char *service, int nargs, int nret,
1936 unsigned long *outputs, ...)
1937 {
1938 va_list list;
1939 int i;
1940 unsigned long s;
1941 struct device_node *rtas;
1942 int *tokp;
1943 union {
1944 unsigned long words[16];
1945 double align;
1946 } u;
1947
1948 rtas = find_devices("rtas");
1949 if (rtas == NULL)
1950 return -1;
1951 tokp = (int *) get_property(rtas, service, NULL);
1952 if (tokp == NULL) {
1953 printk(KERN_ERR "No RTAS service called %s\n", service);
1954 return -1;
1955 }
1956 u.words[0] = *tokp;
1957 u.words[1] = nargs;
1958 u.words[2] = nret;
1959 va_start(list, outputs);
1960 for (i = 0; i < nargs; ++i)
1961 u.words[i+3] = va_arg(list, unsigned long);
1962 va_end(list);
1963
1964 /* Shouldn't we enable kernel FP here ? enter_rtas will play
1965 * with MSR_FE0|MSR_FE1|MSR_FP so I assume rtas might use
1966 * floating points. If that's the case, then we need to make
1967 * sure any lazy FP context is backed up
1968 * --BenH
1969 */
1970 spin_lock_irqsave(&rtas_lock, s);
1971 enter_rtas((void *)__pa(&u));
1972 spin_unlock_irqrestore(&rtas_lock, s);
1973
1974 if (nret > 1 && outputs != NULL)
1975 for (i = 0; i < nret-1; ++i)
1976 outputs[i] = u.words[i+nargs+4];
1977 return u.words[nargs+3];
1978 }
1979
1980 void __init
1981 abort()
1982 {
1983 #ifdef CONFIG_XMON
1984 xmon(NULL);
1985 #endif
1986 for (;;)
1987 prom_exit();
1988 }
1989