2 * Procedures for interfacing to the Open Firmware PROM on
3 * Power Macintosh computers.
5 * In particular, we are interested in the device tree
6 * and in using some of its services (exit, write to stdout).
8 * Paul Mackerras August 1996.
9 * Copyright (C) 1996 Paul Mackerras.
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/version.h>
17 #include <linux/threads.h>
18 #include <linux/spinlock.h>
19 #include <linux/ioport.h>
20 #include <linux/pci.h>
21 #include <linux/slab.h>
22 #include <linux/bitops.h>
24 #include <asm/sections.h>
27 #include <asm/processor.h>
31 #include <asm/bootx.h>
32 #include <asm/system.h>
34 #include <asm/pgtable.h>
35 #include <asm/bootinfo.h>
36 #include <asm/btext.h>
37 #include <asm/pci-bridge.h>
38 #include <asm/open_pic.h>
47 struct pci_reg_property {
48 struct pci_address addr;
53 struct isa_reg_property {
59 typedef unsigned long interpret_func(struct device_node *, unsigned long,
61 static interpret_func interpret_pci_props;
62 static interpret_func interpret_dbdma_props;
63 static interpret_func interpret_isa_props;
64 static interpret_func interpret_macio_props;
65 static interpret_func interpret_root_props;
69 /* Set for a newworld or CHRP machine */
70 int use_of_interrupt_tree;
71 struct device_node *dflt_interrupt_controller;
72 int num_interrupt_controllers;
76 extern unsigned int rtas_entry; /* physical pointer */
78 extern struct device_node *allnodes;
80 static unsigned long finish_node(struct device_node *, unsigned long,
81 interpret_func *, int, int);
82 static unsigned long finish_node_interrupts(struct device_node *, unsigned long);
83 static struct device_node *find_phandle(phandle);
85 extern void enter_rtas(void *);
86 void phys_call_rtas(int, int, int, ...);
88 extern char cmd_line[512]; /* XXX */
89 extern boot_infos_t *boot_infos;
90 unsigned long dev_tree_size;
93 phys_call_rtas(int service, int nargs, int nret, ...)
97 unsigned long words[16];
100 void (*rtas)(void *, unsigned long);
103 u.words[0] = service;
106 va_start(list, nret);
107 for (i = 0; i < nargs; ++i)
108 u.words[i+3] = va_arg(list, unsigned long);
111 rtas = (void (*)(void *, unsigned long)) rtas_entry;
116 * finish_device_tree is called once things are running normally
117 * (i.e. with text and data mapped to the address they were linked at).
118 * It traverses the device tree and fills in the name, type,
119 * {n_}addrs and {n_}intrs fields of each node.
122 finish_device_tree(void)
124 unsigned long mem = (unsigned long) klimit;
125 struct device_node *np;
127 /* All newworld pmac machines and CHRPs now use the interrupt tree */
128 for (np = allnodes; np != NULL; np = np->allnext) {
129 if (get_property(np, "interrupt-parent", NULL)) {
130 use_of_interrupt_tree = 1;
134 if (_machine == _MACH_Pmac && use_of_interrupt_tree)
137 #ifdef CONFIG_BOOTX_TEXT
138 if (boot_infos && pmac_newworld) {
139 prom_print("WARNING ! BootX/miBoot booting is not supported on this machine\n");
140 prom_print(" You should use an Open Firmware bootloader\n");
142 #endif /* CONFIG_BOOTX_TEXT */
144 if (use_of_interrupt_tree) {
146 * We want to find out here how many interrupt-controller
147 * nodes there are, and if we are booted from BootX,
148 * we need a pointer to the first (and hopefully only)
149 * such node. But we can't use find_devices here since
150 * np->name has not been set yet. -- paulus
156 for (np = allnodes; np != NULL; np = np->allnext) {
157 ic = get_property(np, "interrupt-controller", &iclen);
158 name = get_property(np, "name", NULL);
159 /* checking iclen makes sure we don't get a false
160 match on /chosen.interrupt_controller */
162 && strcmp(name, "interrupt-controller") == 0)
163 || (ic != NULL && iclen == 0 && strcmp(name, "AppleKiwi"))) {
165 dflt_interrupt_controller = np;
169 num_interrupt_controllers = n;
172 mem = finish_node(allnodes, mem, NULL, 1, 1);
173 dev_tree_size = mem - (unsigned long) allnodes;
174 klimit = (char *) mem;
177 static unsigned long __init
178 finish_node(struct device_node *np, unsigned long mem_start,
179 interpret_func *ifunc, int naddrc, int nsizec)
181 struct device_node *child;
184 np->name = get_property(np, "name", NULL);
185 np->type = get_property(np, "device_type", NULL);
192 /* get the device addresses and interrupts */
194 mem_start = ifunc(np, mem_start, naddrc, nsizec);
196 if (use_of_interrupt_tree)
197 mem_start = finish_node_interrupts(np, mem_start);
199 /* Look for #address-cells and #size-cells properties. */
200 ip = (int *) get_property(np, "#address-cells", NULL);
203 ip = (int *) get_property(np, "#size-cells", NULL);
207 if (np->parent == NULL)
208 ifunc = interpret_root_props;
209 else if (np->type == 0)
211 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
212 ifunc = interpret_pci_props;
213 else if (!strcmp(np->type, "dbdma"))
214 ifunc = interpret_dbdma_props;
215 else if (!strcmp(np->type, "mac-io")
216 || ifunc == interpret_macio_props)
217 ifunc = interpret_macio_props;
218 else if (!strcmp(np->type, "isa"))
219 ifunc = interpret_isa_props;
220 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
221 ifunc = interpret_root_props;
222 else if (!((ifunc == interpret_dbdma_props
223 || ifunc == interpret_macio_props)
224 && (!strcmp(np->type, "escc")
225 || !strcmp(np->type, "media-bay"))))
228 /* if we were booted from BootX, convert the full name */
230 && strncmp(np->full_name, "Devices:device-tree", 19) == 0) {
231 if (np->full_name[19] == 0) {
232 strcpy(np->full_name, "/");
233 } else if (np->full_name[19] == ':') {
234 char *p = np->full_name + 19;
242 for (child = np->child; child != NULL; child = child->sibling)
243 mem_start = finish_node(child, mem_start, ifunc,
250 * Find the interrupt parent of a node.
252 static struct device_node * __init
253 intr_parent(struct device_node *p)
257 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
260 p = find_phandle(*parp);
264 * On a powermac booted with BootX, we don't get to know the
265 * phandles for any nodes, so find_phandle will return NULL.
266 * Fortunately these machines only have one interrupt controller
267 * so there isn't in fact any ambiguity. -- paulus
269 if (num_interrupt_controllers == 1)
270 p = dflt_interrupt_controller;
275 * Find out the size of each entry of the interrupts property
279 prom_n_intr_cells(struct device_node *np)
281 struct device_node *p;
284 for (p = np; (p = intr_parent(p)) != NULL; ) {
285 icp = (unsigned int *)
286 get_property(p, "#interrupt-cells", NULL);
289 if (get_property(p, "interrupt-controller", NULL) != NULL
290 || get_property(p, "interrupt-map", NULL) != NULL) {
291 printk("oops, node %s doesn't have #interrupt-cells\n",
296 printk("prom_n_intr_cells failed for %s\n", np->full_name);
301 * Map an interrupt from a device up to the platform interrupt
305 map_interrupt(unsigned int **irq, struct device_node **ictrler,
306 struct device_node *np, unsigned int *ints, int nintrc)
308 struct device_node *p, *ipar;
309 unsigned int *imap, *imask, *ip;
310 int i, imaplen, match;
311 int newintrc = 1, newaddrc = 1;
315 reg = (unsigned int *) get_property(np, "reg", NULL);
316 naddrc = prom_n_addr_cells(np);
319 if (get_property(p, "interrupt-controller", NULL) != NULL)
320 /* this node is an interrupt controller, stop here */
322 imap = (unsigned int *)
323 get_property(p, "interrupt-map", &imaplen);
328 imask = (unsigned int *)
329 get_property(p, "interrupt-map-mask", NULL);
331 printk("oops, %s has interrupt-map but no mask\n",
335 imaplen /= sizeof(unsigned int);
338 while (imaplen > 0 && !match) {
339 /* check the child-interrupt field */
341 for (i = 0; i < naddrc && match; ++i)
342 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
343 for (; i < naddrc + nintrc && match; ++i)
344 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
345 imap += naddrc + nintrc;
346 imaplen -= naddrc + nintrc;
347 /* grab the interrupt parent */
348 ipar = find_phandle((phandle) *imap++);
350 if (ipar == NULL && num_interrupt_controllers == 1)
351 /* cope with BootX not giving us phandles */
352 ipar = dflt_interrupt_controller;
354 printk("oops, no int parent %x in map of %s\n",
355 imap[-1], p->full_name);
358 /* find the parent's # addr and intr cells */
359 ip = (unsigned int *)
360 get_property(ipar, "#interrupt-cells", NULL);
362 printk("oops, no #interrupt-cells on %s\n",
367 ip = (unsigned int *)
368 get_property(ipar, "#address-cells", NULL);
369 newaddrc = (ip == NULL)? 0: *ip;
370 imap += newaddrc + newintrc;
371 imaplen -= newaddrc + newintrc;
374 printk("oops, error decoding int-map on %s, len=%d\n",
375 p->full_name, imaplen);
379 printk("oops, no match in %s int-map for %s\n",
380 p->full_name, np->full_name);
386 ints = imap - nintrc;
390 printk("hmmm, int tree for %s doesn't have ctrler\n",
398 * New version of finish_node_interrupts.
400 static unsigned long __init
401 finish_node_interrupts(struct device_node *np, unsigned long mem_start)
404 int intlen, intrcells;
407 struct device_node *ic;
409 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
412 intrcells = prom_n_intr_cells(np);
413 intlen /= intrcells * sizeof(unsigned int);
414 np->n_intrs = intlen;
415 np->intrs = (struct interrupt_info *) mem_start;
416 mem_start += intlen * sizeof(struct interrupt_info);
418 for (i = 0; i < intlen; ++i) {
419 np->intrs[i].line = 0;
420 np->intrs[i].sense = 1;
421 n = map_interrupt(&irq, &ic, np, ints, intrcells);
426 * On a CHRP we have an 8259 which is subordinate to
427 * the openpic in the interrupt tree, but we want the
428 * openpic's interrupt numbers offsetted, not the 8259's.
429 * So we apply the offset if the controller is at the
430 * root of the interrupt tree, i.e. has no interrupt-parent.
431 * This doesn't cope with the general case of multiple
432 * cascaded interrupt controllers, but then neither will
433 * irq.c at the moment either. -- paulus
434 * The G5 triggers that code, I add a machine test. On
435 * those machines, we want to offset interrupts from the
436 * second openpic by 128 -- BenH
438 if (_machine != _MACH_Pmac && num_interrupt_controllers > 1
440 && get_property(ic, "interrupt-parent", NULL) == NULL)
442 else if (_machine == _MACH_Pmac && num_interrupt_controllers > 1
443 && ic != NULL && ic->parent != NULL) {
444 char *name = get_property(ic->parent, "name", NULL);
445 if (name && !strcmp(name, "u3"))
449 np->intrs[i].line = irq[0] + offset;
451 np->intrs[i].sense = irq[1];
453 printk("hmmm, got %d intr cells for %s:", n,
455 for (j = 0; j < n; ++j)
456 printk(" %d", irq[j]);
466 * When BootX makes a copy of the device tree from the MacOS
467 * Name Registry, it is in the format we use but all of the pointers
468 * are offsets from the start of the tree.
469 * This procedure updates the pointers.
475 struct device_node *np;
478 #define ADDBASE(x) (x = (typeof (x))((x)? ((unsigned long)(x) + base): 0))
480 base = (unsigned long) boot_infos + boot_infos->deviceTreeOffset;
481 allnodes = (struct device_node *)(base + 4);
482 for (np = allnodes; np != 0; np = np->allnext) {
483 ADDBASE(np->full_name);
484 ADDBASE(np->properties);
487 ADDBASE(np->sibling);
488 ADDBASE(np->allnext);
489 for (pp = np->properties; pp != 0; pp = pp->next) {
498 prom_n_addr_cells(struct device_node* np)
504 ip = (int *) get_property(np, "#address-cells", NULL);
507 } while (np->parent);
508 /* No #address-cells property for the root node, default to 1 */
513 prom_n_size_cells(struct device_node* np)
519 ip = (int *) get_property(np, "#size-cells", NULL);
522 } while (np->parent);
523 /* No #size-cells property for the root node, default to 1 */
527 static unsigned long __init
528 map_addr(struct device_node *np, unsigned long space, unsigned long addr)
531 unsigned int *ranges;
535 type = (space >> 24) & 3;
539 while ((np = np->parent) != NULL) {
540 if (strcmp(np->type, "pci") != 0)
542 /* PCI bridge: map the address through the ranges property */
543 na = prom_n_addr_cells(np);
544 ranges = (unsigned int *) get_property(np, "ranges", &rlen);
545 while ((rlen -= (na + 5) * sizeof(unsigned int)) >= 0) {
546 if (((ranges[0] >> 24) & 3) == type
548 && addr - ranges[2] < ranges[na+4]) {
549 /* ok, this matches, translate it */
550 addr += ranges[na+2] - ranges[2];
559 static unsigned long __init
560 interpret_pci_props(struct device_node *np, unsigned long mem_start,
561 int naddrc, int nsizec)
563 struct address_range *adr;
564 struct pci_reg_property *pci_addrs;
567 pci_addrs = (struct pci_reg_property *)
568 get_property(np, "assigned-addresses", &l);
569 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
571 adr = (struct address_range *) mem_start;
572 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
573 adr[i].space = pci_addrs[i].addr.a_hi;
574 adr[i].address = map_addr(np, pci_addrs[i].addr.a_hi,
575 pci_addrs[i].addr.a_lo);
576 adr[i].size = pci_addrs[i].size_lo;
581 mem_start += i * sizeof(struct address_range);
584 if (use_of_interrupt_tree)
587 ip = (int *) get_property(np, "AAPL,interrupts", &l);
588 if (ip == 0 && np->parent)
589 ip = (int *) get_property(np->parent, "AAPL,interrupts", &l);
591 ip = (int *) get_property(np, "interrupts", &l);
593 np->intrs = (struct interrupt_info *) mem_start;
594 np->n_intrs = l / sizeof(int);
595 mem_start += np->n_intrs * sizeof(struct interrupt_info);
596 for (i = 0; i < np->n_intrs; ++i) {
597 np->intrs[i].line = *ip++;
598 np->intrs[i].sense = 1;
605 static unsigned long __init
606 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
607 int naddrc, int nsizec)
609 struct reg_property *rp;
610 struct address_range *adr;
611 unsigned long base_address;
613 struct device_node *db;
616 for (db = np->parent; db != NULL; db = db->parent) {
617 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
618 base_address = db->addrs[0].address;
623 rp = (struct reg_property *) get_property(np, "reg", &l);
624 if (rp != 0 && l >= sizeof(struct reg_property)) {
626 adr = (struct address_range *) mem_start;
627 while ((l -= sizeof(struct reg_property)) >= 0) {
629 adr[i].address = rp[i].address + base_address;
630 adr[i].size = rp[i].size;
635 mem_start += i * sizeof(struct address_range);
638 if (use_of_interrupt_tree)
641 ip = (int *) get_property(np, "AAPL,interrupts", &l);
643 ip = (int *) get_property(np, "interrupts", &l);
645 np->intrs = (struct interrupt_info *) mem_start;
646 np->n_intrs = l / sizeof(int);
647 mem_start += np->n_intrs * sizeof(struct interrupt_info);
648 for (i = 0; i < np->n_intrs; ++i) {
649 np->intrs[i].line = *ip++;
650 np->intrs[i].sense = 1;
657 static unsigned long __init
658 interpret_macio_props(struct device_node *np, unsigned long mem_start,
659 int naddrc, int nsizec)
661 struct reg_property *rp;
662 struct address_range *adr;
663 unsigned long base_address;
665 struct device_node *db;
668 for (db = np->parent; db != NULL; db = db->parent) {
669 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
670 base_address = db->addrs[0].address;
675 rp = (struct reg_property *) get_property(np, "reg", &l);
676 if (rp != 0 && l >= sizeof(struct reg_property)) {
678 adr = (struct address_range *) mem_start;
679 while ((l -= sizeof(struct reg_property)) >= 0) {
681 adr[i].address = rp[i].address + base_address;
682 adr[i].size = rp[i].size;
687 mem_start += i * sizeof(struct address_range);
690 if (use_of_interrupt_tree)
693 ip = (int *) get_property(np, "interrupts", &l);
695 ip = (int *) get_property(np, "AAPL,interrupts", &l);
697 np->intrs = (struct interrupt_info *) mem_start;
698 np->n_intrs = l / sizeof(int);
699 for (i = 0; i < np->n_intrs; ++i) {
700 np->intrs[i].line = *ip++;
701 np->intrs[i].sense = 1;
703 mem_start += np->n_intrs * sizeof(struct interrupt_info);
709 static unsigned long __init
710 interpret_isa_props(struct device_node *np, unsigned long mem_start,
711 int naddrc, int nsizec)
713 struct isa_reg_property *rp;
714 struct address_range *adr;
717 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
718 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
720 adr = (struct address_range *) mem_start;
721 while ((l -= sizeof(struct reg_property)) >= 0) {
722 adr[i].space = rp[i].space;
723 adr[i].address = rp[i].address
724 + (adr[i].space? 0: _ISA_MEM_BASE);
725 adr[i].size = rp[i].size;
730 mem_start += i * sizeof(struct address_range);
733 if (use_of_interrupt_tree)
736 ip = (int *) get_property(np, "interrupts", &l);
738 np->intrs = (struct interrupt_info *) mem_start;
739 np->n_intrs = l / (2 * sizeof(int));
740 mem_start += np->n_intrs * sizeof(struct interrupt_info);
741 for (i = 0; i < np->n_intrs; ++i) {
742 np->intrs[i].line = *ip++;
743 np->intrs[i].sense = *ip++;
750 static unsigned long __init
751 interpret_root_props(struct device_node *np, unsigned long mem_start,
752 int naddrc, int nsizec)
754 struct address_range *adr;
757 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
759 rp = (unsigned int *) get_property(np, "reg", &l);
760 if (rp != 0 && l >= rpsize) {
762 adr = (struct address_range *) mem_start;
763 while ((l -= rpsize) >= 0) {
764 adr[i].space = (naddrc >= 2? rp[naddrc-2]: 2);
765 adr[i].address = rp[naddrc - 1];
766 adr[i].size = rp[naddrc + nsizec - 1];
768 rp += naddrc + nsizec;
772 mem_start += i * sizeof(struct address_range);
775 if (use_of_interrupt_tree)
778 ip = (int *) get_property(np, "AAPL,interrupts", &l);
780 ip = (int *) get_property(np, "interrupts", &l);
782 np->intrs = (struct interrupt_info *) mem_start;
783 np->n_intrs = l / sizeof(int);
784 mem_start += np->n_intrs * sizeof(struct interrupt_info);
785 for (i = 0; i < np->n_intrs; ++i) {
786 np->intrs[i].line = *ip++;
787 np->intrs[i].sense = 1;
795 * Work out the sense (active-low level / active-high edge)
796 * of each interrupt from the device tree.
799 prom_get_irq_senses(unsigned char *senses, int off, int max)
801 struct device_node *np;
804 /* default to level-triggered */
805 memset(senses, 1, max - off);
806 if (!use_of_interrupt_tree)
809 for (np = allnodes; np != 0; np = np->allnext) {
810 for (j = 0; j < np->n_intrs; j++) {
811 i = np->intrs[j].line;
812 if (i >= off && i < max) {
813 if (np->intrs[j].sense == 1)
814 senses[i-off] = (IRQ_SENSE_LEVEL
815 | IRQ_POLARITY_NEGATIVE);
817 senses[i-off] = (IRQ_SENSE_EDGE
818 | IRQ_POLARITY_POSITIVE);
825 * Construct and return a list of the device_nodes with a given name.
828 find_devices(const char *name)
830 struct device_node *head, **prevp, *np;
833 for (np = allnodes; np != 0; np = np->allnext) {
834 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
844 * Construct and return a list of the device_nodes with a given type.
847 find_type_devices(const char *type)
849 struct device_node *head, **prevp, *np;
852 for (np = allnodes; np != 0; np = np->allnext) {
853 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
863 * Returns all nodes linked together
868 struct device_node *head, **prevp, *np;
871 for (np = allnodes; np != 0; np = np->allnext) {
879 /* Checks if the given "compat" string matches one of the strings in
880 * the device's "compatible" property
883 device_is_compatible(struct device_node *device, const char *compat)
888 cp = (char *) get_property(device, "compatible", &cplen);
892 if (strncasecmp(cp, compat, strlen(compat)) == 0)
904 * Indicates whether the root node has a given value in its
905 * compatible property.
908 machine_is_compatible(const char *compat)
910 struct device_node *root;
912 root = find_path_device("/");
915 return device_is_compatible(root, compat);
919 * Construct and return a list of the device_nodes with a given type
920 * and compatible property.
923 find_compatible_devices(const char *type, const char *compat)
925 struct device_node *head, **prevp, *np;
928 for (np = allnodes; np != 0; np = np->allnext) {
930 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
932 if (device_is_compatible(np, compat)) {
942 * Find the device_node with a given full_name.
945 find_path_device(const char *path)
947 struct device_node *np;
949 for (np = allnodes; np != 0; np = np->allnext)
950 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
957 * New implementation of the OF "find" APIs, return a refcounted
958 * object, call of_node_put() when done. Currently, still lacks
959 * locking as old implementation, this is beeing done for ppc64.
961 * Note that property management will need some locking as well,
962 * this isn't dealt with yet
967 * of_find_node_by_name - Find a node by it's "name" property
968 * @from: The node to start searching from or NULL, the node
969 * you pass will not be searched, only the next one
970 * will; typically, you pass what the previous call
971 * returned. of_node_put() will be called on it
972 * @name: The name string to match against
974 * Returns a node pointer with refcount incremented, use
975 * of_node_put() on it when done.
977 struct device_node *of_find_node_by_name(struct device_node *from,
980 struct device_node *np = from ? from->allnext : allnodes;
982 for (; np != 0; np = np->allnext)
983 if (np->name != 0 && strcasecmp(np->name, name) == 0)
987 return of_node_get(np);
991 * of_find_node_by_type - Find a node by it's "device_type" property
992 * @from: The node to start searching from or NULL, the node
993 * you pass will not be searched, only the next one
994 * will; typically, you pass what the previous call
995 * returned. of_node_put() will be called on it
996 * @name: The type string to match against
998 * Returns a node pointer with refcount incremented, use
999 * of_node_put() on it when done.
1001 struct device_node *of_find_node_by_type(struct device_node *from,
1004 struct device_node *np = from ? from->allnext : allnodes;
1006 for (; np != 0; np = np->allnext)
1007 if (np->type != 0 && strcasecmp(np->type, type) == 0)
1011 return of_node_get(np);
1015 * of_find_compatible_node - Find a node based on type and one of the
1016 * tokens in it's "compatible" property
1017 * @from: The node to start searching from or NULL, the node
1018 * you pass will not be searched, only the next one
1019 * will; typically, you pass what the previous call
1020 * returned. of_node_put() will be called on it
1021 * @type: The type string to match "device_type" or NULL to ignore
1022 * @compatible: The string to match to one of the tokens in the device
1023 * "compatible" list.
1025 * Returns a node pointer with refcount incremented, use
1026 * of_node_put() on it when done.
1028 struct device_node *of_find_compatible_node(struct device_node *from,
1029 const char *type, const char *compatible)
1031 struct device_node *np = from ? from->allnext : allnodes;
1033 for (; np != 0; np = np->allnext) {
1035 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1037 if (device_is_compatible(np, compatible))
1042 return of_node_get(np);
1046 * of_find_node_by_path - Find a node matching a full OF path
1047 * @path: The full path to match
1049 * Returns a node pointer with refcount incremented, use
1050 * of_node_put() on it when done.
1052 struct device_node *of_find_node_by_path(const char *path)
1054 struct device_node *np = allnodes;
1056 for (; np != 0; np = np->allnext)
1057 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1059 return of_node_get(np);
1063 * of_find_all_nodes - Get next node in global list
1064 * @prev: Previous node or NULL to start iteration
1065 * of_node_put() will be called on it
1067 * Returns a node pointer with refcount incremented, use
1068 * of_node_put() on it when done.
1070 struct device_node *of_find_all_nodes(struct device_node *prev)
1072 return of_node_get(prev ? prev->allnext : allnodes);
1076 * of_get_parent - Get a node's parent if any
1077 * @node: Node to get parent
1079 * Returns a node pointer with refcount incremented, use
1080 * of_node_put() on it when done.
1082 struct device_node *of_get_parent(const struct device_node *node)
1084 return node ? of_node_get(node->parent) : NULL;
1088 * of_get_next_child - Iterate a node childs
1089 * @node: parent node
1090 * @prev: previous child of the parent node, or NULL to get first
1092 * Returns a node pointer with refcount incremented, use
1093 * of_node_put() on it when done.
1095 struct device_node *of_get_next_child(const struct device_node *node,
1096 struct device_node *prev)
1098 struct device_node *next = prev ? prev->sibling : node->child;
1100 for (; next != 0; next = next->sibling)
1101 if (of_node_get(next))
1109 * of_node_get - Increment refcount of a node
1110 * @node: Node to inc refcount, NULL is supported to
1111 * simplify writing of callers
1113 * Returns the node itself or NULL if gone. Current implementation
1114 * does nothing as we don't yet do dynamic node allocation on ppc32
1116 struct device_node *of_node_get(struct device_node *node)
1122 * of_node_put - Decrement refcount of a node
1123 * @node: Node to dec refcount, NULL is supported to
1124 * simplify writing of callers
1126 * Current implementation does nothing as we don't yet do dynamic node
1127 * allocation on ppc32
1129 void of_node_put(struct device_node *node)
1134 * Find the device_node with a given phandle.
1136 static struct device_node * __init
1137 find_phandle(phandle ph)
1139 struct device_node *np;
1141 for (np = allnodes; np != 0; np = np->allnext)
1148 * Find a property with a given name for a given node
1149 * and return the value.
1152 get_property(struct device_node *np, const char *name, int *lenp)
1154 struct property *pp;
1156 for (pp = np->properties; pp != 0; pp = pp->next)
1157 if (pp->name != NULL && strcmp(pp->name, name) == 0) {
1166 * Add a property to a node
1169 prom_add_property(struct device_node* np, struct property* prop)
1171 struct property **next = &np->properties;
1175 next = &(*next)->next;
1179 /* I quickly hacked that one, check against spec ! */
1180 static inline unsigned long
1181 bus_space_to_resource_flags(unsigned int bus_space)
1183 u8 space = (bus_space >> 24) & 0xf;
1187 return IORESOURCE_MEM;
1188 else if (space == 0x01)
1189 return IORESOURCE_IO;
1191 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
1197 static struct resource*
1198 find_parent_pci_resource(struct pci_dev* pdev, struct address_range *range)
1203 /* Check this one */
1204 mask = bus_space_to_resource_flags(range->space);
1205 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
1206 if ((pdev->resource[i].flags & mask) == mask &&
1207 pdev->resource[i].start <= range->address &&
1208 pdev->resource[i].end > range->address) {
1209 if ((range->address + range->size - 1) > pdev->resource[i].end) {
1210 /* Add better message */
1211 printk(KERN_WARNING "PCI/OF resource overlap !\n");
1217 if (i == DEVICE_COUNT_RESOURCE)
1219 return &pdev->resource[i];
1223 * Request an OF device resource. Currently handles child of PCI devices,
1224 * or other nodes attached to the root node. Ultimately, put some
1225 * link to resources in the OF node.
1228 request_OF_resource(struct device_node* node, int index, const char* name_postfix)
1230 struct pci_dev* pcidev;
1231 u8 pci_bus, pci_devfn;
1232 unsigned long iomask;
1233 struct device_node* nd;
1234 struct resource* parent;
1235 struct resource *res = NULL;
1238 if (index >= node->n_addrs)
1241 /* Sanity check on bus space */
1242 iomask = bus_space_to_resource_flags(node->addrs[index].space);
1243 if (iomask & IORESOURCE_MEM)
1244 parent = &iomem_resource;
1245 else if (iomask & IORESOURCE_IO)
1246 parent = &ioport_resource;
1250 /* Find a PCI parent if any */
1254 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1255 pcidev = pci_find_slot(pci_bus, pci_devfn);
1260 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1262 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
1267 res = __request_region(parent, node->addrs[index].address, node->addrs[index].size, NULL);
1270 nlen = strlen(node->name);
1271 plen = name_postfix ? strlen(name_postfix) : 0;
1272 res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
1274 strcpy((char *)res->name, node->name);
1276 strcpy((char *)res->name+nlen, name_postfix);
1284 release_OF_resource(struct device_node* node, int index)
1286 struct pci_dev* pcidev;
1287 u8 pci_bus, pci_devfn;
1288 unsigned long iomask, start, end;
1289 struct device_node* nd;
1290 struct resource* parent;
1291 struct resource *res = NULL;
1293 if (index >= node->n_addrs)
1296 /* Sanity check on bus space */
1297 iomask = bus_space_to_resource_flags(node->addrs[index].space);
1298 if (iomask & IORESOURCE_MEM)
1299 parent = &iomem_resource;
1300 else if (iomask & IORESOURCE_IO)
1301 parent = &ioport_resource;
1305 /* Find a PCI parent if any */
1309 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1310 pcidev = pci_find_slot(pci_bus, pci_devfn);
1315 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1317 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
1322 /* Find us in the parent and its childs */
1323 res = parent->child;
1324 start = node->addrs[index].address;
1325 end = start + node->addrs[index].size - 1;
1327 if (res->start == start && res->end == end &&
1328 (res->flags & IORESOURCE_BUSY))
1330 if (res->start <= start && res->end >= end)
1342 release_resource(res);
1350 print_properties(struct device_node *np)
1352 struct property *pp;
1356 for (pp = np->properties; pp != 0; pp = pp->next) {
1357 printk(KERN_INFO "%s", pp->name);
1358 for (i = strlen(pp->name); i < 16; ++i)
1360 cp = (char *) pp->value;
1361 for (i = pp->length; i > 0; --i, ++cp)
1362 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1363 || (i == 1 && *cp != 0))
1365 if (i == 0 && pp->length > 1) {
1366 /* looks like a string */
1367 printk(" %s\n", (char *) pp->value);
1369 /* dump it in hex */
1373 if (pp->length % 4 == 0) {
1374 unsigned int *p = (unsigned int *) pp->value;
1377 for (i = 0; i < n; ++i) {
1378 if (i != 0 && (i % 4) == 0)
1380 printk(" %08x", *p++);
1383 unsigned char *bp = pp->value;
1385 for (i = 0; i < n; ++i) {
1386 if (i != 0 && (i % 16) == 0)
1388 printk(" %02x", *bp++);
1392 if (pp->length > 64)
1393 printk(" ... (length = %d)\n",
1400 static DEFINE_SPINLOCK(rtas_lock);
1402 /* this can be called after setup -- Cort */
1404 call_rtas(const char *service, int nargs, int nret,
1405 unsigned long *outputs, ...)
1410 struct device_node *rtas;
1413 unsigned long words[16];
1417 rtas = find_devices("rtas");
1420 tokp = (int *) get_property(rtas, service, NULL);
1422 printk(KERN_ERR "No RTAS service called %s\n", service);
1428 va_start(list, outputs);
1429 for (i = 0; i < nargs; ++i)
1430 u.words[i+3] = va_arg(list, unsigned long);
1434 * RTAS doesn't use floating point.
1435 * Or at least, according to the CHRP spec we enter RTAS
1436 * with FP disabled, and it doesn't change the FP registers.
1439 spin_lock_irqsave(&rtas_lock, s);
1440 enter_rtas((void *)__pa(&u));
1441 spin_unlock_irqrestore(&rtas_lock, s);
1443 if (nret > 1 && outputs != NULL)
1444 for (i = 0; i < nret-1; ++i)
1445 outputs[i] = u.words[i+nargs+4];
1446 return u.words[nargs+3];