[PATCH] changing CONFIG_LOCALVERSION rebuilds too much, for no good reason
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / ppc / syslib / prom.c
CommitLineData
1da177e4
LT
1/*
2 * Procedures for interfacing to the Open Firmware PROM on
3 * Power Macintosh computers.
4 *
5 * In particular, we are interested in the device tree
6 * and in using some of its services (exit, write to stdout).
7 *
8 * Paul Mackerras August 1996.
9 * Copyright (C) 1996 Paul Mackerras.
10 */
11#include <stdarg.h>
12#include <linux/config.h>
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/init.h>
1da177e4
LT
16#include <linux/threads.h>
17#include <linux/spinlock.h>
18#include <linux/ioport.h>
19#include <linux/pci.h>
20#include <linux/slab.h>
21#include <linux/bitops.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/bootinfo.h>
35#include <asm/btext.h>
36#include <asm/pci-bridge.h>
37#include <asm/open_pic.h>
38
39
40struct pci_address {
41 unsigned a_hi;
42 unsigned a_mid;
43 unsigned a_lo;
44};
45
46struct pci_reg_property {
47 struct pci_address addr;
48 unsigned size_hi;
49 unsigned size_lo;
50};
51
52struct isa_reg_property {
53 unsigned space;
54 unsigned address;
55 unsigned size;
56};
57
58typedef unsigned long interpret_func(struct device_node *, unsigned long,
59 int, int);
60static interpret_func interpret_pci_props;
61static interpret_func interpret_dbdma_props;
62static interpret_func interpret_isa_props;
63static interpret_func interpret_macio_props;
64static interpret_func interpret_root_props;
65
66extern char *klimit;
67
68/* Set for a newworld or CHRP machine */
69int use_of_interrupt_tree;
70struct device_node *dflt_interrupt_controller;
71int num_interrupt_controllers;
72
73int pmac_newworld;
74
75extern unsigned int rtas_entry; /* physical pointer */
76
77extern struct device_node *allnodes;
78
79static unsigned long finish_node(struct device_node *, unsigned long,
80 interpret_func *, int, int);
81static unsigned long finish_node_interrupts(struct device_node *, unsigned long);
82static struct device_node *find_phandle(phandle);
83
84extern void enter_rtas(void *);
85void phys_call_rtas(int, int, int, ...);
86
87extern char cmd_line[512]; /* XXX */
88extern boot_infos_t *boot_infos;
89unsigned long dev_tree_size;
90
f495a8bf 91void
1da177e4
LT
92phys_call_rtas(int service, int nargs, int nret, ...)
93{
94 va_list list;
95 union {
96 unsigned long words[16];
97 double align;
98 } u;
99 void (*rtas)(void *, unsigned long);
100 int i;
101
102 u.words[0] = service;
103 u.words[1] = nargs;
104 u.words[2] = nret;
105 va_start(list, nret);
106 for (i = 0; i < nargs; ++i)
107 u.words[i+3] = va_arg(list, unsigned long);
108 va_end(list);
109
110 rtas = (void (*)(void *, unsigned long)) rtas_entry;
111 rtas(&u, rtas_data);
112}
113
114/*
115 * finish_device_tree is called once things are running normally
116 * (i.e. with text and data mapped to the address they were linked at).
117 * It traverses the device tree and fills in the name, type,
118 * {n_}addrs and {n_}intrs fields of each node.
119 */
120void __init
121finish_device_tree(void)
122{
123 unsigned long mem = (unsigned long) klimit;
124 struct device_node *np;
125
126 /* All newworld pmac machines and CHRPs now use the interrupt tree */
127 for (np = allnodes; np != NULL; np = np->allnext) {
128 if (get_property(np, "interrupt-parent", NULL)) {
129 use_of_interrupt_tree = 1;
130 break;
131 }
132 }
133 if (_machine == _MACH_Pmac && use_of_interrupt_tree)
134 pmac_newworld = 1;
135
136#ifdef CONFIG_BOOTX_TEXT
137 if (boot_infos && pmac_newworld) {
138 prom_print("WARNING ! BootX/miBoot booting is not supported on this machine\n");
139 prom_print(" You should use an Open Firmware bootloader\n");
140 }
141#endif /* CONFIG_BOOTX_TEXT */
142
143 if (use_of_interrupt_tree) {
144 /*
145 * We want to find out here how many interrupt-controller
146 * nodes there are, and if we are booted from BootX,
147 * we need a pointer to the first (and hopefully only)
148 * such node. But we can't use find_devices here since
149 * np->name has not been set yet. -- paulus
150 */
151 int n = 0;
152 char *name, *ic;
153 int iclen;
154
155 for (np = allnodes; np != NULL; np = np->allnext) {
156 ic = get_property(np, "interrupt-controller", &iclen);
157 name = get_property(np, "name", NULL);
158 /* checking iclen makes sure we don't get a false
159 match on /chosen.interrupt_controller */
160 if ((name != NULL
161 && strcmp(name, "interrupt-controller") == 0)
162 || (ic != NULL && iclen == 0 && strcmp(name, "AppleKiwi"))) {
163 if (n == 0)
164 dflt_interrupt_controller = np;
165 ++n;
166 }
167 }
168 num_interrupt_controllers = n;
169 }
170
171 mem = finish_node(allnodes, mem, NULL, 1, 1);
172 dev_tree_size = mem - (unsigned long) allnodes;
173 klimit = (char *) mem;
174}
175
176static unsigned long __init
177finish_node(struct device_node *np, unsigned long mem_start,
178 interpret_func *ifunc, int naddrc, int nsizec)
179{
180 struct device_node *child;
181 int *ip;
182
183 np->name = get_property(np, "name", NULL);
184 np->type = get_property(np, "device_type", NULL);
185
186 if (!np->name)
187 np->name = "<NULL>";
188 if (!np->type)
189 np->type = "<NULL>";
190
191 /* get the device addresses and interrupts */
192 if (ifunc != NULL)
193 mem_start = ifunc(np, mem_start, naddrc, nsizec);
194
195 if (use_of_interrupt_tree)
196 mem_start = finish_node_interrupts(np, mem_start);
197
198 /* Look for #address-cells and #size-cells properties. */
199 ip = (int *) get_property(np, "#address-cells", NULL);
200 if (ip != NULL)
201 naddrc = *ip;
202 ip = (int *) get_property(np, "#size-cells", NULL);
203 if (ip != NULL)
204 nsizec = *ip;
205
206 if (np->parent == NULL)
207 ifunc = interpret_root_props;
208 else if (np->type == 0)
209 ifunc = NULL;
210 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
211 ifunc = interpret_pci_props;
212 else if (!strcmp(np->type, "dbdma"))
213 ifunc = interpret_dbdma_props;
214 else if (!strcmp(np->type, "mac-io")
215 || ifunc == interpret_macio_props)
216 ifunc = interpret_macio_props;
217 else if (!strcmp(np->type, "isa"))
218 ifunc = interpret_isa_props;
219 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
220 ifunc = interpret_root_props;
221 else if (!((ifunc == interpret_dbdma_props
222 || ifunc == interpret_macio_props)
223 && (!strcmp(np->type, "escc")
224 || !strcmp(np->type, "media-bay"))))
225 ifunc = NULL;
226
227 /* if we were booted from BootX, convert the full name */
228 if (boot_infos
229 && strncmp(np->full_name, "Devices:device-tree", 19) == 0) {
230 if (np->full_name[19] == 0) {
231 strcpy(np->full_name, "/");
232 } else if (np->full_name[19] == ':') {
233 char *p = np->full_name + 19;
234 np->full_name = p;
235 for (; *p; ++p)
236 if (*p == ':')
237 *p = '/';
238 }
239 }
240
241 for (child = np->child; child != NULL; child = child->sibling)
242 mem_start = finish_node(child, mem_start, ifunc,
243 naddrc, nsizec);
244
245 return mem_start;
246}
247
248/*
249 * Find the interrupt parent of a node.
250 */
251static struct device_node * __init
252intr_parent(struct device_node *p)
253{
254 phandle *parp;
255
256 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
257 if (parp == NULL)
258 return p->parent;
259 p = find_phandle(*parp);
260 if (p != NULL)
261 return p;
262 /*
263 * On a powermac booted with BootX, we don't get to know the
264 * phandles for any nodes, so find_phandle will return NULL.
265 * Fortunately these machines only have one interrupt controller
266 * so there isn't in fact any ambiguity. -- paulus
267 */
268 if (num_interrupt_controllers == 1)
269 p = dflt_interrupt_controller;
270 return p;
271}
272
273/*
274 * Find out the size of each entry of the interrupts property
275 * for a node.
276 */
277static int __init
278prom_n_intr_cells(struct device_node *np)
279{
280 struct device_node *p;
281 unsigned int *icp;
282
283 for (p = np; (p = intr_parent(p)) != NULL; ) {
284 icp = (unsigned int *)
285 get_property(p, "#interrupt-cells", NULL);
286 if (icp != NULL)
287 return *icp;
288 if (get_property(p, "interrupt-controller", NULL) != NULL
289 || get_property(p, "interrupt-map", NULL) != NULL) {
290 printk("oops, node %s doesn't have #interrupt-cells\n",
291 p->full_name);
292 return 1;
293 }
294 }
295 printk("prom_n_intr_cells failed for %s\n", np->full_name);
296 return 1;
297}
298
299/*
300 * Map an interrupt from a device up to the platform interrupt
301 * descriptor.
302 */
303static int __init
304map_interrupt(unsigned int **irq, struct device_node **ictrler,
305 struct device_node *np, unsigned int *ints, int nintrc)
306{
307 struct device_node *p, *ipar;
308 unsigned int *imap, *imask, *ip;
309 int i, imaplen, match;
310 int newintrc = 1, newaddrc = 1;
311 unsigned int *reg;
312 int naddrc;
313
314 reg = (unsigned int *) get_property(np, "reg", NULL);
315 naddrc = prom_n_addr_cells(np);
316 p = intr_parent(np);
317 while (p != NULL) {
318 if (get_property(p, "interrupt-controller", NULL) != NULL)
319 /* this node is an interrupt controller, stop here */
320 break;
321 imap = (unsigned int *)
322 get_property(p, "interrupt-map", &imaplen);
323 if (imap == NULL) {
324 p = intr_parent(p);
325 continue;
326 }
327 imask = (unsigned int *)
328 get_property(p, "interrupt-map-mask", NULL);
329 if (imask == NULL) {
330 printk("oops, %s has interrupt-map but no mask\n",
331 p->full_name);
332 return 0;
333 }
334 imaplen /= sizeof(unsigned int);
335 match = 0;
336 ipar = NULL;
337 while (imaplen > 0 && !match) {
338 /* check the child-interrupt field */
339 match = 1;
340 for (i = 0; i < naddrc && match; ++i)
341 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
342 for (; i < naddrc + nintrc && match; ++i)
343 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
344 imap += naddrc + nintrc;
345 imaplen -= naddrc + nintrc;
346 /* grab the interrupt parent */
347 ipar = find_phandle((phandle) *imap++);
348 --imaplen;
349 if (ipar == NULL && num_interrupt_controllers == 1)
350 /* cope with BootX not giving us phandles */
351 ipar = dflt_interrupt_controller;
352 if (ipar == NULL) {
353 printk("oops, no int parent %x in map of %s\n",
354 imap[-1], p->full_name);
355 return 0;
356 }
357 /* find the parent's # addr and intr cells */
358 ip = (unsigned int *)
359 get_property(ipar, "#interrupt-cells", NULL);
360 if (ip == NULL) {
361 printk("oops, no #interrupt-cells on %s\n",
362 ipar->full_name);
363 return 0;
364 }
365 newintrc = *ip;
366 ip = (unsigned int *)
367 get_property(ipar, "#address-cells", NULL);
368 newaddrc = (ip == NULL)? 0: *ip;
369 imap += newaddrc + newintrc;
370 imaplen -= newaddrc + newintrc;
371 }
372 if (imaplen < 0) {
373 printk("oops, error decoding int-map on %s, len=%d\n",
374 p->full_name, imaplen);
375 return 0;
376 }
377 if (!match) {
378 printk("oops, no match in %s int-map for %s\n",
379 p->full_name, np->full_name);
380 return 0;
381 }
382 p = ipar;
383 naddrc = newaddrc;
384 nintrc = newintrc;
385 ints = imap - nintrc;
386 reg = ints - naddrc;
387 }
388 if (p == NULL)
389 printk("hmmm, int tree for %s doesn't have ctrler\n",
390 np->full_name);
391 *irq = ints;
392 *ictrler = p;
393 return nintrc;
394}
395
396/*
397 * New version of finish_node_interrupts.
398 */
399static unsigned long __init
400finish_node_interrupts(struct device_node *np, unsigned long mem_start)
401{
402 unsigned int *ints;
403 int intlen, intrcells;
404 int i, j, n, offset;
405 unsigned int *irq;
406 struct device_node *ic;
407
408 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
409 if (ints == NULL)
410 return mem_start;
411 intrcells = prom_n_intr_cells(np);
412 intlen /= intrcells * sizeof(unsigned int);
413 np->n_intrs = intlen;
414 np->intrs = (struct interrupt_info *) mem_start;
415 mem_start += intlen * sizeof(struct interrupt_info);
416
417 for (i = 0; i < intlen; ++i) {
418 np->intrs[i].line = 0;
419 np->intrs[i].sense = 1;
420 n = map_interrupt(&irq, &ic, np, ints, intrcells);
421 if (n <= 0)
422 continue;
423 offset = 0;
424 /*
425 * On a CHRP we have an 8259 which is subordinate to
426 * the openpic in the interrupt tree, but we want the
427 * openpic's interrupt numbers offsetted, not the 8259's.
428 * So we apply the offset if the controller is at the
429 * root of the interrupt tree, i.e. has no interrupt-parent.
430 * This doesn't cope with the general case of multiple
431 * cascaded interrupt controllers, but then neither will
432 * irq.c at the moment either. -- paulus
433 * The G5 triggers that code, I add a machine test. On
434 * those machines, we want to offset interrupts from the
435 * second openpic by 128 -- BenH
436 */
437 if (_machine != _MACH_Pmac && num_interrupt_controllers > 1
438 && ic != NULL
439 && get_property(ic, "interrupt-parent", NULL) == NULL)
440 offset = 16;
441 else if (_machine == _MACH_Pmac && num_interrupt_controllers > 1
442 && ic != NULL && ic->parent != NULL) {
443 char *name = get_property(ic->parent, "name", NULL);
444 if (name && !strcmp(name, "u3"))
445 offset = 128;
446 }
447
448 np->intrs[i].line = irq[0] + offset;
449 if (n > 1)
450 np->intrs[i].sense = irq[1];
451 if (n > 2) {
452 printk("hmmm, got %d intr cells for %s:", n,
453 np->full_name);
454 for (j = 0; j < n; ++j)
455 printk(" %d", irq[j]);
456 printk("\n");
457 }
458 ints += intrcells;
459 }
460
461 return mem_start;
462}
463
464/*
465 * When BootX makes a copy of the device tree from the MacOS
466 * Name Registry, it is in the format we use but all of the pointers
467 * are offsets from the start of the tree.
468 * This procedure updates the pointers.
469 */
470void __init
471relocate_nodes(void)
472{
473 unsigned long base;
474 struct device_node *np;
475 struct property *pp;
476
477#define ADDBASE(x) (x = (typeof (x))((x)? ((unsigned long)(x) + base): 0))
478
479 base = (unsigned long) boot_infos + boot_infos->deviceTreeOffset;
480 allnodes = (struct device_node *)(base + 4);
481 for (np = allnodes; np != 0; np = np->allnext) {
482 ADDBASE(np->full_name);
483 ADDBASE(np->properties);
484 ADDBASE(np->parent);
485 ADDBASE(np->child);
486 ADDBASE(np->sibling);
487 ADDBASE(np->allnext);
488 for (pp = np->properties; pp != 0; pp = pp->next) {
489 ADDBASE(pp->name);
490 ADDBASE(pp->value);
491 ADDBASE(pp->next);
492 }
493 }
494}
495
496int
497prom_n_addr_cells(struct device_node* np)
498{
499 int* ip;
500 do {
501 if (np->parent)
502 np = np->parent;
503 ip = (int *) get_property(np, "#address-cells", NULL);
504 if (ip != NULL)
505 return *ip;
506 } while (np->parent);
507 /* No #address-cells property for the root node, default to 1 */
508 return 1;
509}
510
511int
512prom_n_size_cells(struct device_node* np)
513{
514 int* ip;
515 do {
516 if (np->parent)
517 np = np->parent;
518 ip = (int *) get_property(np, "#size-cells", NULL);
519 if (ip != NULL)
520 return *ip;
521 } while (np->parent);
522 /* No #size-cells property for the root node, default to 1 */
523 return 1;
524}
525
526static unsigned long __init
527map_addr(struct device_node *np, unsigned long space, unsigned long addr)
528{
529 int na;
530 unsigned int *ranges;
531 int rlen = 0;
532 unsigned int type;
533
534 type = (space >> 24) & 3;
535 if (type == 0)
536 return addr;
537
538 while ((np = np->parent) != NULL) {
539 if (strcmp(np->type, "pci") != 0)
540 continue;
541 /* PCI bridge: map the address through the ranges property */
542 na = prom_n_addr_cells(np);
543 ranges = (unsigned int *) get_property(np, "ranges", &rlen);
544 while ((rlen -= (na + 5) * sizeof(unsigned int)) >= 0) {
545 if (((ranges[0] >> 24) & 3) == type
546 && ranges[2] <= addr
547 && addr - ranges[2] < ranges[na+4]) {
548 /* ok, this matches, translate it */
549 addr += ranges[na+2] - ranges[2];
550 break;
551 }
552 ranges += na + 5;
553 }
554 }
555 return addr;
556}
557
558static unsigned long __init
559interpret_pci_props(struct device_node *np, unsigned long mem_start,
560 int naddrc, int nsizec)
561{
562 struct address_range *adr;
563 struct pci_reg_property *pci_addrs;
564 int i, l, *ip;
565
566 pci_addrs = (struct pci_reg_property *)
567 get_property(np, "assigned-addresses", &l);
568 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
569 i = 0;
570 adr = (struct address_range *) mem_start;
571 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
572 adr[i].space = pci_addrs[i].addr.a_hi;
573 adr[i].address = map_addr(np, pci_addrs[i].addr.a_hi,
574 pci_addrs[i].addr.a_lo);
575 adr[i].size = pci_addrs[i].size_lo;
576 ++i;
577 }
578 np->addrs = adr;
579 np->n_addrs = i;
580 mem_start += i * sizeof(struct address_range);
581 }
582
583 if (use_of_interrupt_tree)
584 return mem_start;
585
586 ip = (int *) get_property(np, "AAPL,interrupts", &l);
587 if (ip == 0 && np->parent)
588 ip = (int *) get_property(np->parent, "AAPL,interrupts", &l);
589 if (ip == 0)
590 ip = (int *) get_property(np, "interrupts", &l);
591 if (ip != 0) {
592 np->intrs = (struct interrupt_info *) mem_start;
593 np->n_intrs = l / sizeof(int);
594 mem_start += np->n_intrs * sizeof(struct interrupt_info);
595 for (i = 0; i < np->n_intrs; ++i) {
596 np->intrs[i].line = *ip++;
597 np->intrs[i].sense = 1;
598 }
599 }
600
601 return mem_start;
602}
603
604static unsigned long __init
605interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
606 int naddrc, int nsizec)
607{
608 struct reg_property *rp;
609 struct address_range *adr;
610 unsigned long base_address;
611 int i, l, *ip;
612 struct device_node *db;
613
614 base_address = 0;
615 for (db = np->parent; db != NULL; db = db->parent) {
616 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
617 base_address = db->addrs[0].address;
618 break;
619 }
620 }
621
622 rp = (struct reg_property *) get_property(np, "reg", &l);
623 if (rp != 0 && l >= sizeof(struct reg_property)) {
624 i = 0;
625 adr = (struct address_range *) mem_start;
626 while ((l -= sizeof(struct reg_property)) >= 0) {
627 adr[i].space = 2;
628 adr[i].address = rp[i].address + base_address;
629 adr[i].size = rp[i].size;
630 ++i;
631 }
632 np->addrs = adr;
633 np->n_addrs = i;
634 mem_start += i * sizeof(struct address_range);
635 }
636
637 if (use_of_interrupt_tree)
638 return mem_start;
639
640 ip = (int *) get_property(np, "AAPL,interrupts", &l);
641 if (ip == 0)
642 ip = (int *) get_property(np, "interrupts", &l);
643 if (ip != 0) {
644 np->intrs = (struct interrupt_info *) mem_start;
645 np->n_intrs = l / sizeof(int);
646 mem_start += np->n_intrs * sizeof(struct interrupt_info);
647 for (i = 0; i < np->n_intrs; ++i) {
648 np->intrs[i].line = *ip++;
649 np->intrs[i].sense = 1;
650 }
651 }
652
653 return mem_start;
654}
655
656static unsigned long __init
657interpret_macio_props(struct device_node *np, unsigned long mem_start,
658 int naddrc, int nsizec)
659{
660 struct reg_property *rp;
661 struct address_range *adr;
662 unsigned long base_address;
663 int i, l, *ip;
664 struct device_node *db;
665
666 base_address = 0;
667 for (db = np->parent; db != NULL; db = db->parent) {
668 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
669 base_address = db->addrs[0].address;
670 break;
671 }
672 }
673
674 rp = (struct reg_property *) get_property(np, "reg", &l);
675 if (rp != 0 && l >= sizeof(struct reg_property)) {
676 i = 0;
677 adr = (struct address_range *) mem_start;
678 while ((l -= sizeof(struct reg_property)) >= 0) {
679 adr[i].space = 2;
680 adr[i].address = rp[i].address + base_address;
681 adr[i].size = rp[i].size;
682 ++i;
683 }
684 np->addrs = adr;
685 np->n_addrs = i;
686 mem_start += i * sizeof(struct address_range);
687 }
688
689 if (use_of_interrupt_tree)
690 return mem_start;
691
692 ip = (int *) get_property(np, "interrupts", &l);
693 if (ip == 0)
694 ip = (int *) get_property(np, "AAPL,interrupts", &l);
695 if (ip != 0) {
696 np->intrs = (struct interrupt_info *) mem_start;
697 np->n_intrs = l / sizeof(int);
698 for (i = 0; i < np->n_intrs; ++i) {
699 np->intrs[i].line = *ip++;
700 np->intrs[i].sense = 1;
701 }
702 mem_start += np->n_intrs * sizeof(struct interrupt_info);
703 }
704
705 return mem_start;
706}
707
708static unsigned long __init
709interpret_isa_props(struct device_node *np, unsigned long mem_start,
710 int naddrc, int nsizec)
711{
712 struct isa_reg_property *rp;
713 struct address_range *adr;
714 int i, l, *ip;
715
716 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
717 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
718 i = 0;
719 adr = (struct address_range *) mem_start;
720 while ((l -= sizeof(struct reg_property)) >= 0) {
721 adr[i].space = rp[i].space;
722 adr[i].address = rp[i].address
723 + (adr[i].space? 0: _ISA_MEM_BASE);
724 adr[i].size = rp[i].size;
725 ++i;
726 }
727 np->addrs = adr;
728 np->n_addrs = i;
729 mem_start += i * sizeof(struct address_range);
730 }
731
732 if (use_of_interrupt_tree)
733 return mem_start;
734
735 ip = (int *) get_property(np, "interrupts", &l);
736 if (ip != 0) {
737 np->intrs = (struct interrupt_info *) mem_start;
738 np->n_intrs = l / (2 * sizeof(int));
739 mem_start += np->n_intrs * sizeof(struct interrupt_info);
740 for (i = 0; i < np->n_intrs; ++i) {
741 np->intrs[i].line = *ip++;
742 np->intrs[i].sense = *ip++;
743 }
744 }
745
746 return mem_start;
747}
748
749static unsigned long __init
750interpret_root_props(struct device_node *np, unsigned long mem_start,
751 int naddrc, int nsizec)
752{
753 struct address_range *adr;
754 int i, l, *ip;
755 unsigned int *rp;
756 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
757
758 rp = (unsigned int *) get_property(np, "reg", &l);
759 if (rp != 0 && l >= rpsize) {
760 i = 0;
761 adr = (struct address_range *) mem_start;
762 while ((l -= rpsize) >= 0) {
763 adr[i].space = (naddrc >= 2? rp[naddrc-2]: 2);
764 adr[i].address = rp[naddrc - 1];
765 adr[i].size = rp[naddrc + nsizec - 1];
766 ++i;
767 rp += naddrc + nsizec;
768 }
769 np->addrs = adr;
770 np->n_addrs = i;
771 mem_start += i * sizeof(struct address_range);
772 }
773
774 if (use_of_interrupt_tree)
775 return mem_start;
776
777 ip = (int *) get_property(np, "AAPL,interrupts", &l);
778 if (ip == 0)
779 ip = (int *) get_property(np, "interrupts", &l);
780 if (ip != 0) {
781 np->intrs = (struct interrupt_info *) mem_start;
782 np->n_intrs = l / sizeof(int);
783 mem_start += np->n_intrs * sizeof(struct interrupt_info);
784 for (i = 0; i < np->n_intrs; ++i) {
785 np->intrs[i].line = *ip++;
786 np->intrs[i].sense = 1;
787 }
788 }
789
790 return mem_start;
791}
792
793/*
794 * Work out the sense (active-low level / active-high edge)
795 * of each interrupt from the device tree.
796 */
797void __init
798prom_get_irq_senses(unsigned char *senses, int off, int max)
799{
800 struct device_node *np;
801 int i, j;
802
803 /* default to level-triggered */
804 memset(senses, 1, max - off);
805 if (!use_of_interrupt_tree)
806 return;
807
808 for (np = allnodes; np != 0; np = np->allnext) {
809 for (j = 0; j < np->n_intrs; j++) {
810 i = np->intrs[j].line;
811 if (i >= off && i < max) {
812 if (np->intrs[j].sense == 1)
813 senses[i-off] = (IRQ_SENSE_LEVEL
814 | IRQ_POLARITY_NEGATIVE);
815 else
816 senses[i-off] = (IRQ_SENSE_EDGE
817 | IRQ_POLARITY_POSITIVE);
818 }
819 }
820 }
821}
822
823/*
824 * Construct and return a list of the device_nodes with a given name.
825 */
826struct device_node *
827find_devices(const char *name)
828{
829 struct device_node *head, **prevp, *np;
830
831 prevp = &head;
832 for (np = allnodes; np != 0; np = np->allnext) {
833 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
834 *prevp = np;
835 prevp = &np->next;
836 }
837 }
838 *prevp = NULL;
839 return head;
840}
841
842/*
843 * Construct and return a list of the device_nodes with a given type.
844 */
845struct device_node *
846find_type_devices(const char *type)
847{
848 struct device_node *head, **prevp, *np;
849
850 prevp = &head;
851 for (np = allnodes; np != 0; np = np->allnext) {
852 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
853 *prevp = np;
854 prevp = &np->next;
855 }
856 }
857 *prevp = NULL;
858 return head;
859}
860
861/*
862 * Returns all nodes linked together
863 */
f495a8bf 864struct device_node *
1da177e4
LT
865find_all_nodes(void)
866{
867 struct device_node *head, **prevp, *np;
868
869 prevp = &head;
870 for (np = allnodes; np != 0; np = np->allnext) {
871 *prevp = np;
872 prevp = &np->next;
873 }
874 *prevp = NULL;
875 return head;
876}
877
878/* Checks if the given "compat" string matches one of the strings in
879 * the device's "compatible" property
880 */
881int
882device_is_compatible(struct device_node *device, const char *compat)
883{
884 const char* cp;
885 int cplen, l;
886
887 cp = (char *) get_property(device, "compatible", &cplen);
888 if (cp == NULL)
889 return 0;
890 while (cplen > 0) {
891 if (strncasecmp(cp, compat, strlen(compat)) == 0)
892 return 1;
893 l = strlen(cp) + 1;
894 cp += l;
895 cplen -= l;
896 }
897
898 return 0;
899}
900
901
902/*
903 * Indicates whether the root node has a given value in its
904 * compatible property.
905 */
906int
907machine_is_compatible(const char *compat)
908{
909 struct device_node *root;
910
911 root = find_path_device("/");
912 if (root == 0)
913 return 0;
914 return device_is_compatible(root, compat);
915}
916
917/*
918 * Construct and return a list of the device_nodes with a given type
919 * and compatible property.
920 */
921struct device_node *
922find_compatible_devices(const char *type, const char *compat)
923{
924 struct device_node *head, **prevp, *np;
925
926 prevp = &head;
927 for (np = allnodes; np != 0; np = np->allnext) {
928 if (type != NULL
929 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
930 continue;
931 if (device_is_compatible(np, compat)) {
932 *prevp = np;
933 prevp = &np->next;
934 }
935 }
936 *prevp = NULL;
937 return head;
938}
939
940/*
941 * Find the device_node with a given full_name.
942 */
943struct device_node *
944find_path_device(const char *path)
945{
946 struct device_node *np;
947
948 for (np = allnodes; np != 0; np = np->allnext)
949 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
950 return np;
951 return NULL;
952}
953
954/*******
955 *
956 * New implementation of the OF "find" APIs, return a refcounted
957 * object, call of_node_put() when done. Currently, still lacks
958 * locking as old implementation, this is beeing done for ppc64.
959 *
960 * Note that property management will need some locking as well,
961 * this isn't dealt with yet
962 *
963 *******/
964
965/**
966 * of_find_node_by_name - Find a node by it's "name" property
967 * @from: The node to start searching from or NULL, the node
968 * you pass will not be searched, only the next one
969 * will; typically, you pass what the previous call
970 * returned. of_node_put() will be called on it
971 * @name: The name string to match against
972 *
973 * Returns a node pointer with refcount incremented, use
974 * of_node_put() on it when done.
975 */
976struct device_node *of_find_node_by_name(struct device_node *from,
977 const char *name)
978{
979 struct device_node *np = from ? from->allnext : allnodes;
980
981 for (; np != 0; np = np->allnext)
982 if (np->name != 0 && strcasecmp(np->name, name) == 0)
983 break;
984 if (from)
985 of_node_put(from);
986 return of_node_get(np);
987}
988
989/**
990 * of_find_node_by_type - Find a node by it's "device_type" property
991 * @from: The node to start searching from or NULL, the node
992 * you pass will not be searched, only the next one
993 * will; typically, you pass what the previous call
994 * returned. of_node_put() will be called on it
995 * @name: The type string to match against
996 *
997 * Returns a node pointer with refcount incremented, use
998 * of_node_put() on it when done.
999 */
1000struct device_node *of_find_node_by_type(struct device_node *from,
1001 const char *type)
1002{
1003 struct device_node *np = from ? from->allnext : allnodes;
1004
1005 for (; np != 0; np = np->allnext)
1006 if (np->type != 0 && strcasecmp(np->type, type) == 0)
1007 break;
1008 if (from)
1009 of_node_put(from);
1010 return of_node_get(np);
1011}
1012
1013/**
1014 * of_find_compatible_node - Find a node based on type and one of the
1015 * tokens in it's "compatible" property
1016 * @from: The node to start searching from or NULL, the node
1017 * you pass will not be searched, only the next one
1018 * will; typically, you pass what the previous call
1019 * returned. of_node_put() will be called on it
1020 * @type: The type string to match "device_type" or NULL to ignore
1021 * @compatible: The string to match to one of the tokens in the device
1022 * "compatible" list.
1023 *
1024 * Returns a node pointer with refcount incremented, use
1025 * of_node_put() on it when done.
1026 */
1027struct device_node *of_find_compatible_node(struct device_node *from,
1028 const char *type, const char *compatible)
1029{
1030 struct device_node *np = from ? from->allnext : allnodes;
1031
1032 for (; np != 0; np = np->allnext) {
1033 if (type != NULL
1034 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1035 continue;
1036 if (device_is_compatible(np, compatible))
1037 break;
1038 }
1039 if (from)
1040 of_node_put(from);
1041 return of_node_get(np);
1042}
1043
1044/**
1045 * of_find_node_by_path - Find a node matching a full OF path
1046 * @path: The full path to match
1047 *
1048 * Returns a node pointer with refcount incremented, use
1049 * of_node_put() on it when done.
1050 */
1051struct device_node *of_find_node_by_path(const char *path)
1052{
1053 struct device_node *np = allnodes;
1054
1055 for (; np != 0; np = np->allnext)
1056 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1057 break;
1058 return of_node_get(np);
1059}
1060
1061/**
1062 * of_find_all_nodes - Get next node in global list
1063 * @prev: Previous node or NULL to start iteration
1064 * of_node_put() will be called on it
1065 *
1066 * Returns a node pointer with refcount incremented, use
1067 * of_node_put() on it when done.
1068 */
1069struct device_node *of_find_all_nodes(struct device_node *prev)
1070{
1071 return of_node_get(prev ? prev->allnext : allnodes);
1072}
1073
1074/**
1075 * of_get_parent - Get a node's parent if any
1076 * @node: Node to get parent
1077 *
1078 * Returns a node pointer with refcount incremented, use
1079 * of_node_put() on it when done.
1080 */
1081struct device_node *of_get_parent(const struct device_node *node)
1082{
1083 return node ? of_node_get(node->parent) : NULL;
1084}
1085
1086/**
1087 * of_get_next_child - Iterate a node childs
1088 * @node: parent node
1089 * @prev: previous child of the parent node, or NULL to get first
1090 *
1091 * Returns a node pointer with refcount incremented, use
1092 * of_node_put() on it when done.
1093 */
1094struct device_node *of_get_next_child(const struct device_node *node,
1095 struct device_node *prev)
1096{
1097 struct device_node *next = prev ? prev->sibling : node->child;
1098
1099 for (; next != 0; next = next->sibling)
1100 if (of_node_get(next))
1101 break;
1102 if (prev)
1103 of_node_put(prev);
1104 return next;
1105}
1106
1107/**
1108 * of_node_get - Increment refcount of a node
1109 * @node: Node to inc refcount, NULL is supported to
1110 * simplify writing of callers
1111 *
1112 * Returns the node itself or NULL if gone. Current implementation
1113 * does nothing as we don't yet do dynamic node allocation on ppc32
1114 */
1115struct device_node *of_node_get(struct device_node *node)
1116{
1117 return node;
1118}
1119
1120/**
1121 * of_node_put - Decrement refcount of a node
1122 * @node: Node to dec refcount, NULL is supported to
1123 * simplify writing of callers
1124 *
1125 * Current implementation does nothing as we don't yet do dynamic node
1126 * allocation on ppc32
1127 */
1128void of_node_put(struct device_node *node)
1129{
1130}
1131
1132/*
1133 * Find the device_node with a given phandle.
1134 */
1135static struct device_node * __init
1136find_phandle(phandle ph)
1137{
1138 struct device_node *np;
1139
1140 for (np = allnodes; np != 0; np = np->allnext)
1141 if (np->node == ph)
1142 return np;
1143 return NULL;
1144}
1145
1146/*
1147 * Find a property with a given name for a given node
1148 * and return the value.
1149 */
1150unsigned char *
1151get_property(struct device_node *np, const char *name, int *lenp)
1152{
1153 struct property *pp;
1154
1155 for (pp = np->properties; pp != 0; pp = pp->next)
1156 if (pp->name != NULL && strcmp(pp->name, name) == 0) {
1157 if (lenp != 0)
1158 *lenp = pp->length;
1159 return pp->value;
1160 }
1161 return NULL;
1162}
1163
1164/*
1165 * Add a property to a node
1166 */
183d0202 1167int
1da177e4
LT
1168prom_add_property(struct device_node* np, struct property* prop)
1169{
1170 struct property **next = &np->properties;
1171
1172 prop->next = NULL;
1173 while (*next)
1174 next = &(*next)->next;
1175 *next = prop;
183d0202
BH
1176
1177 return 0;
1da177e4
LT
1178}
1179
1180/* I quickly hacked that one, check against spec ! */
f495a8bf 1181static inline unsigned long
1da177e4
LT
1182bus_space_to_resource_flags(unsigned int bus_space)
1183{
1184 u8 space = (bus_space >> 24) & 0xf;
1185 if (space == 0)
1186 space = 0x02;
1187 if (space == 0x02)
1188 return IORESOURCE_MEM;
1189 else if (space == 0x01)
1190 return IORESOURCE_IO;
1191 else {
1192 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
1193 bus_space);
1194 return 0;
1195 }
1196}
1197
f495a8bf 1198static struct resource*
1da177e4
LT
1199find_parent_pci_resource(struct pci_dev* pdev, struct address_range *range)
1200{
1201 unsigned long mask;
1202 int i;
1203
1204 /* Check this one */
1205 mask = bus_space_to_resource_flags(range->space);
1206 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
1207 if ((pdev->resource[i].flags & mask) == mask &&
1208 pdev->resource[i].start <= range->address &&
1209 pdev->resource[i].end > range->address) {
1210 if ((range->address + range->size - 1) > pdev->resource[i].end) {
1211 /* Add better message */
1212 printk(KERN_WARNING "PCI/OF resource overlap !\n");
1213 return NULL;
1214 }
1215 break;
1216 }
1217 }
1218 if (i == DEVICE_COUNT_RESOURCE)
1219 return NULL;
1220 return &pdev->resource[i];
1221}
1222
1223/*
1224 * Request an OF device resource. Currently handles child of PCI devices,
1225 * or other nodes attached to the root node. Ultimately, put some
1226 * link to resources in the OF node.
1227 */
f495a8bf 1228struct resource*
1da177e4
LT
1229request_OF_resource(struct device_node* node, int index, const char* name_postfix)
1230{
1231 struct pci_dev* pcidev;
1232 u8 pci_bus, pci_devfn;
1233 unsigned long iomask;
1234 struct device_node* nd;
1235 struct resource* parent;
1236 struct resource *res = NULL;
1237 int nlen, plen;
1238
1239 if (index >= node->n_addrs)
1240 goto fail;
1241
1242 /* Sanity check on bus space */
1243 iomask = bus_space_to_resource_flags(node->addrs[index].space);
1244 if (iomask & IORESOURCE_MEM)
1245 parent = &iomem_resource;
1246 else if (iomask & IORESOURCE_IO)
1247 parent = &ioport_resource;
1248 else
1249 goto fail;
1250
1251 /* Find a PCI parent if any */
1252 nd = node;
1253 pcidev = NULL;
1254 while(nd) {
1255 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1256 pcidev = pci_find_slot(pci_bus, pci_devfn);
1257 if (pcidev) break;
1258 nd = nd->parent;
1259 }
1260 if (pcidev)
1261 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1262 if (!parent) {
1263 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
1264 node->name);
1265 goto fail;
1266 }
1267
1268 res = __request_region(parent, node->addrs[index].address, node->addrs[index].size, NULL);
1269 if (!res)
1270 goto fail;
1271 nlen = strlen(node->name);
1272 plen = name_postfix ? strlen(name_postfix) : 0;
1273 res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
1274 if (res->name) {
1275 strcpy((char *)res->name, node->name);
1276 if (plen)
1277 strcpy((char *)res->name+nlen, name_postfix);
1278 }
1279 return res;
1280fail:
1281 return NULL;
1282}
1283
f495a8bf 1284int
1da177e4
LT
1285release_OF_resource(struct device_node* node, int index)
1286{
1287 struct pci_dev* pcidev;
1288 u8 pci_bus, pci_devfn;
1289 unsigned long iomask, start, end;
1290 struct device_node* nd;
1291 struct resource* parent;
1292 struct resource *res = NULL;
1293
1294 if (index >= node->n_addrs)
1295 return -EINVAL;
1296
1297 /* Sanity check on bus space */
1298 iomask = bus_space_to_resource_flags(node->addrs[index].space);
1299 if (iomask & IORESOURCE_MEM)
1300 parent = &iomem_resource;
1301 else if (iomask & IORESOURCE_IO)
1302 parent = &ioport_resource;
1303 else
1304 return -EINVAL;
1305
1306 /* Find a PCI parent if any */
1307 nd = node;
1308 pcidev = NULL;
1309 while(nd) {
1310 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1311 pcidev = pci_find_slot(pci_bus, pci_devfn);
1312 if (pcidev) break;
1313 nd = nd->parent;
1314 }
1315 if (pcidev)
1316 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1317 if (!parent) {
1318 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
1319 node->name);
1320 return -ENODEV;
1321 }
1322
1323 /* Find us in the parent and its childs */
1324 res = parent->child;
1325 start = node->addrs[index].address;
1326 end = start + node->addrs[index].size - 1;
1327 while (res) {
1328 if (res->start == start && res->end == end &&
1329 (res->flags & IORESOURCE_BUSY))
1330 break;
1331 if (res->start <= start && res->end >= end)
1332 res = res->child;
1333 else
1334 res = res->sibling;
1335 }
1336 if (!res)
1337 return -ENODEV;
1338
b2325fe1
JJ
1339 kfree(res->name);
1340 res->name = NULL;
1da177e4
LT
1341 release_resource(res);
1342 kfree(res);
1343
1344 return 0;
1345}
1346
1347#if 0
f495a8bf 1348void
1da177e4
LT
1349print_properties(struct device_node *np)
1350{
1351 struct property *pp;
1352 char *cp;
1353 int i, n;
1354
1355 for (pp = np->properties; pp != 0; pp = pp->next) {
1356 printk(KERN_INFO "%s", pp->name);
1357 for (i = strlen(pp->name); i < 16; ++i)
1358 printk(" ");
1359 cp = (char *) pp->value;
1360 for (i = pp->length; i > 0; --i, ++cp)
1361 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1362 || (i == 1 && *cp != 0))
1363 break;
1364 if (i == 0 && pp->length > 1) {
1365 /* looks like a string */
1366 printk(" %s\n", (char *) pp->value);
1367 } else {
1368 /* dump it in hex */
1369 n = pp->length;
1370 if (n > 64)
1371 n = 64;
1372 if (pp->length % 4 == 0) {
1373 unsigned int *p = (unsigned int *) pp->value;
1374
1375 n /= 4;
1376 for (i = 0; i < n; ++i) {
1377 if (i != 0 && (i % 4) == 0)
1378 printk("\n ");
1379 printk(" %08x", *p++);
1380 }
1381 } else {
1382 unsigned char *bp = pp->value;
1383
1384 for (i = 0; i < n; ++i) {
1385 if (i != 0 && (i % 16) == 0)
1386 printk("\n ");
1387 printk(" %02x", *bp++);
1388 }
1389 }
1390 printk("\n");
1391 if (pp->length > 64)
1392 printk(" ... (length = %d)\n",
1393 pp->length);
1394 }
1395 }
1396}
1397#endif
1398
1399static DEFINE_SPINLOCK(rtas_lock);
1400
1401/* this can be called after setup -- Cort */
f495a8bf 1402int
1da177e4
LT
1403call_rtas(const char *service, int nargs, int nret,
1404 unsigned long *outputs, ...)
1405{
1406 va_list list;
1407 int i;
1408 unsigned long s;
1409 struct device_node *rtas;
1410 int *tokp;
1411 union {
1412 unsigned long words[16];
1413 double align;
1414 } u;
1415
1416 rtas = find_devices("rtas");
1417 if (rtas == NULL)
1418 return -1;
1419 tokp = (int *) get_property(rtas, service, NULL);
1420 if (tokp == NULL) {
1421 printk(KERN_ERR "No RTAS service called %s\n", service);
1422 return -1;
1423 }
1424 u.words[0] = *tokp;
1425 u.words[1] = nargs;
1426 u.words[2] = nret;
1427 va_start(list, outputs);
1428 for (i = 0; i < nargs; ++i)
1429 u.words[i+3] = va_arg(list, unsigned long);
1430 va_end(list);
1431
1432 /*
1433 * RTAS doesn't use floating point.
1434 * Or at least, according to the CHRP spec we enter RTAS
1435 * with FP disabled, and it doesn't change the FP registers.
1436 * -- paulus.
1437 */
1438 spin_lock_irqsave(&rtas_lock, s);
1439 enter_rtas((void *)__pa(&u));
1440 spin_unlock_irqrestore(&rtas_lock, s);
1441
1442 if (nret > 1 && outputs != NULL)
1443 for (i = 0; i < nret-1; ++i)
1444 outputs[i] = u.words[i+nargs+4];
1445 return u.words[nargs+3];
1446}