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/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>
23 #include <asm/sections.h>
26 #include <asm/processor.h>
30 #include <asm/bootx.h>
31 #include <asm/system.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>
46 struct pci_reg_property {
47 struct pci_address addr;
52 struct isa_reg_property {
58 typedef unsigned long interpret_func(struct device_node *, unsigned long,
60 static interpret_func interpret_pci_props;
61 static interpret_func interpret_dbdma_props;
62 static interpret_func interpret_isa_props;
63 static interpret_func interpret_macio_props;
64 static interpret_func interpret_root_props;
68 /* Set for a newworld or CHRP machine */
69 int use_of_interrupt_tree;
70 struct device_node *dflt_interrupt_controller;
71 int num_interrupt_controllers;
75 extern unsigned int rtas_entry; /* physical pointer */
77 extern struct device_node *allnodes;
79 static unsigned long finish_node(struct device_node *, unsigned long,
80 interpret_func *, int, int);
81 static unsigned long finish_node_interrupts(struct device_node *, unsigned long);
82 static struct device_node *find_phandle(phandle);
84 extern void enter_rtas(void *);
85 void phys_call_rtas(int, int, int, ...);
87 extern char cmd_line[512]; /* XXX */
88 extern boot_infos_t *boot_infos;
89 unsigned long dev_tree_size;
92 phys_call_rtas(int service, int nargs, int nret, ...)
96 unsigned long words[16];
99 void (*rtas)(void *, unsigned long);
102 u.words[0] = service;
105 va_start(list, nret);
106 for (i = 0; i < nargs; ++i)
107 u.words[i+3] = va_arg(list, unsigned long);
110 rtas = (void (*)(void *, unsigned long)) rtas_entry;
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.
121 finish_device_tree(void)
123 unsigned long mem = (unsigned long) klimit;
124 struct device_node *np;
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;
133 if (_machine == _MACH_Pmac && use_of_interrupt_tree)
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");
141 #endif /* CONFIG_BOOTX_TEXT */
143 if (use_of_interrupt_tree) {
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
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 */
161 && strcmp(name, "interrupt-controller") == 0)
162 || (ic != NULL && iclen == 0 && strcmp(name, "AppleKiwi"))) {
164 dflt_interrupt_controller = np;
168 num_interrupt_controllers = n;
171 mem = finish_node(allnodes, mem, NULL, 1, 1);
172 dev_tree_size = mem - (unsigned long) allnodes;
173 klimit = (char *) mem;
176 static unsigned long __init
177 finish_node(struct device_node *np, unsigned long mem_start,
178 interpret_func *ifunc, int naddrc, int nsizec)
180 struct device_node *child;
183 np->name = get_property(np, "name", NULL);
184 np->type = get_property(np, "device_type", NULL);
191 /* get the device addresses and interrupts */
193 mem_start = ifunc(np, mem_start, naddrc, nsizec);
195 if (use_of_interrupt_tree)
196 mem_start = finish_node_interrupts(np, mem_start);
198 /* Look for #address-cells and #size-cells properties. */
199 ip = (int *) get_property(np, "#address-cells", NULL);
202 ip = (int *) get_property(np, "#size-cells", NULL);
206 if (np->parent == NULL)
207 ifunc = interpret_root_props;
208 else if (np->type == 0)
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"))))
227 /* if we were booted from BootX, convert the full name */
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;
241 for (child = np->child; child != NULL; child = child->sibling)
242 mem_start = finish_node(child, mem_start, ifunc,
249 * Find the interrupt parent of a node.
251 static struct device_node * __init
252 intr_parent(struct device_node *p)
256 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
259 p = find_phandle(*parp);
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
268 if (num_interrupt_controllers == 1)
269 p = dflt_interrupt_controller;
274 * Find out the size of each entry of the interrupts property
278 prom_n_intr_cells(struct device_node *np)
280 struct device_node *p;
283 for (p = np; (p = intr_parent(p)) != NULL; ) {
284 icp = (unsigned int *)
285 get_property(p, "#interrupt-cells", NULL);
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",
295 printk("prom_n_intr_cells failed for %s\n", np->full_name);
300 * Map an interrupt from a device up to the platform interrupt
304 map_interrupt(unsigned int **irq, struct device_node **ictrler,
305 struct device_node *np, unsigned int *ints, int nintrc)
307 struct device_node *p, *ipar;
308 unsigned int *imap, *imask, *ip;
309 int i, imaplen, match;
310 int newintrc = 1, newaddrc = 1;
314 reg = (unsigned int *) get_property(np, "reg", NULL);
315 naddrc = prom_n_addr_cells(np);
318 if (get_property(p, "interrupt-controller", NULL) != NULL)
319 /* this node is an interrupt controller, stop here */
321 imap = (unsigned int *)
322 get_property(p, "interrupt-map", &imaplen);
327 imask = (unsigned int *)
328 get_property(p, "interrupt-map-mask", NULL);
330 printk("oops, %s has interrupt-map but no mask\n",
334 imaplen /= sizeof(unsigned int);
337 while (imaplen > 0 && !match) {
338 /* check the child-interrupt field */
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++);
349 if (ipar == NULL && num_interrupt_controllers == 1)
350 /* cope with BootX not giving us phandles */
351 ipar = dflt_interrupt_controller;
353 printk("oops, no int parent %x in map of %s\n",
354 imap[-1], p->full_name);
357 /* find the parent's # addr and intr cells */
358 ip = (unsigned int *)
359 get_property(ipar, "#interrupt-cells", NULL);
361 printk("oops, no #interrupt-cells on %s\n",
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;
373 printk("oops, error decoding int-map on %s, len=%d\n",
374 p->full_name, imaplen);
378 printk("oops, no match in %s int-map for %s\n",
379 p->full_name, np->full_name);
385 ints = imap - nintrc;
389 printk("hmmm, int tree for %s doesn't have ctrler\n",
397 * New version of finish_node_interrupts.
399 static unsigned long __init
400 finish_node_interrupts(struct device_node *np, unsigned long mem_start)
403 int intlen, intrcells;
406 struct device_node *ic;
408 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
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);
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);
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
437 if (_machine != _MACH_Pmac && num_interrupt_controllers > 1
439 && get_property(ic, "interrupt-parent", NULL) == NULL)
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"))
448 np->intrs[i].line = irq[0] + offset;
450 np->intrs[i].sense = irq[1];
452 printk("hmmm, got %d intr cells for %s:", n,
454 for (j = 0; j < n; ++j)
455 printk(" %d", irq[j]);
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.
474 struct device_node *np;
477 #define ADDBASE(x) (x = (typeof (x))((x)? ((unsigned long)(x) + base): 0))
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);
486 ADDBASE(np->sibling);
487 ADDBASE(np->allnext);
488 for (pp = np->properties; pp != 0; pp = pp->next) {
497 prom_n_addr_cells(struct device_node* np)
503 ip = (int *) get_property(np, "#address-cells", NULL);
506 } while (np->parent);
507 /* No #address-cells property for the root node, default to 1 */
512 prom_n_size_cells(struct device_node* np)
518 ip = (int *) get_property(np, "#size-cells", NULL);
521 } while (np->parent);
522 /* No #size-cells property for the root node, default to 1 */
526 static unsigned long __init
527 map_addr(struct device_node *np, unsigned long space, unsigned long addr)
530 unsigned int *ranges;
534 type = (space >> 24) & 3;
538 while ((np = np->parent) != NULL) {
539 if (strcmp(np->type, "pci") != 0)
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
547 && addr - ranges[2] < ranges[na+4]) {
548 /* ok, this matches, translate it */
549 addr += ranges[na+2] - ranges[2];
558 static unsigned long __init
559 interpret_pci_props(struct device_node *np, unsigned long mem_start,
560 int naddrc, int nsizec)
562 struct address_range *adr;
563 struct pci_reg_property *pci_addrs;
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)) {
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;
580 mem_start += i * sizeof(struct address_range);
583 if (use_of_interrupt_tree)
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);
590 ip = (int *) get_property(np, "interrupts", &l);
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;
604 static unsigned long __init
605 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
606 int naddrc, int nsizec)
608 struct reg_property *rp;
609 struct address_range *adr;
610 unsigned long base_address;
612 struct device_node *db;
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;
622 rp = (struct reg_property *) get_property(np, "reg", &l);
623 if (rp != 0 && l >= sizeof(struct reg_property)) {
625 adr = (struct address_range *) mem_start;
626 while ((l -= sizeof(struct reg_property)) >= 0) {
628 adr[i].address = rp[i].address + base_address;
629 adr[i].size = rp[i].size;
634 mem_start += i * sizeof(struct address_range);
637 if (use_of_interrupt_tree)
640 ip = (int *) get_property(np, "AAPL,interrupts", &l);
642 ip = (int *) get_property(np, "interrupts", &l);
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;
656 static unsigned long __init
657 interpret_macio_props(struct device_node *np, unsigned long mem_start,
658 int naddrc, int nsizec)
660 struct reg_property *rp;
661 struct address_range *adr;
662 unsigned long base_address;
664 struct device_node *db;
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;
674 rp = (struct reg_property *) get_property(np, "reg", &l);
675 if (rp != 0 && l >= sizeof(struct reg_property)) {
677 adr = (struct address_range *) mem_start;
678 while ((l -= sizeof(struct reg_property)) >= 0) {
680 adr[i].address = rp[i].address + base_address;
681 adr[i].size = rp[i].size;
686 mem_start += i * sizeof(struct address_range);
689 if (use_of_interrupt_tree)
692 ip = (int *) get_property(np, "interrupts", &l);
694 ip = (int *) get_property(np, "AAPL,interrupts", &l);
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;
702 mem_start += np->n_intrs * sizeof(struct interrupt_info);
708 static unsigned long __init
709 interpret_isa_props(struct device_node *np, unsigned long mem_start,
710 int naddrc, int nsizec)
712 struct isa_reg_property *rp;
713 struct address_range *adr;
716 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
717 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
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;
729 mem_start += i * sizeof(struct address_range);
732 if (use_of_interrupt_tree)
735 ip = (int *) get_property(np, "interrupts", &l);
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++;
749 static unsigned long __init
750 interpret_root_props(struct device_node *np, unsigned long mem_start,
751 int naddrc, int nsizec)
753 struct address_range *adr;
756 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
758 rp = (unsigned int *) get_property(np, "reg", &l);
759 if (rp != 0 && l >= rpsize) {
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];
767 rp += naddrc + nsizec;
771 mem_start += i * sizeof(struct address_range);
774 if (use_of_interrupt_tree)
777 ip = (int *) get_property(np, "AAPL,interrupts", &l);
779 ip = (int *) get_property(np, "interrupts", &l);
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;
794 * Work out the sense (active-low level / active-high edge)
795 * of each interrupt from the device tree.
798 prom_get_irq_senses(unsigned char *senses, int off, int max)
800 struct device_node *np;
803 /* default to level-triggered */
804 memset(senses, 1, max - off);
805 if (!use_of_interrupt_tree)
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);
816 senses[i-off] = (IRQ_SENSE_EDGE
817 | IRQ_POLARITY_POSITIVE);
824 * Construct and return a list of the device_nodes with a given name.
827 find_devices(const char *name)
829 struct device_node *head, **prevp, *np;
832 for (np = allnodes; np != 0; np = np->allnext) {
833 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
843 * Construct and return a list of the device_nodes with a given type.
846 find_type_devices(const char *type)
848 struct device_node *head, **prevp, *np;
851 for (np = allnodes; np != 0; np = np->allnext) {
852 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
862 * Returns all nodes linked together
867 struct device_node *head, **prevp, *np;
870 for (np = allnodes; np != 0; np = np->allnext) {
878 /* Checks if the given "compat" string matches one of the strings in
879 * the device's "compatible" property
882 device_is_compatible(struct device_node *device, const char *compat)
887 cp = (char *) get_property(device, "compatible", &cplen);
891 if (strncasecmp(cp, compat, strlen(compat)) == 0)
903 * Indicates whether the root node has a given value in its
904 * compatible property.
907 machine_is_compatible(const char *compat)
909 struct device_node *root;
911 root = find_path_device("/");
914 return device_is_compatible(root, compat);
918 * Construct and return a list of the device_nodes with a given type
919 * and compatible property.
922 find_compatible_devices(const char *type, const char *compat)
924 struct device_node *head, **prevp, *np;
927 for (np = allnodes; np != 0; np = np->allnext) {
929 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
931 if (device_is_compatible(np, compat)) {
941 * Find the device_node with a given full_name.
944 find_path_device(const char *path)
946 struct device_node *np;
948 for (np = allnodes; np != 0; np = np->allnext)
949 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
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.
960 * Note that property management will need some locking as well,
961 * this isn't dealt with yet
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
973 * Returns a node pointer with refcount incremented, use
974 * of_node_put() on it when done.
976 struct device_node *of_find_node_by_name(struct device_node *from,
979 struct device_node *np = from ? from->allnext : allnodes;
981 for (; np != 0; np = np->allnext)
982 if (np->name != 0 && strcasecmp(np->name, name) == 0)
986 return of_node_get(np);
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
997 * Returns a node pointer with refcount incremented, use
998 * of_node_put() on it when done.
1000 struct device_node *of_find_node_by_type(struct device_node *from,
1003 struct device_node *np = from ? from->allnext : allnodes;
1005 for (; np != 0; np = np->allnext)
1006 if (np->type != 0 && strcasecmp(np->type, type) == 0)
1010 return of_node_get(np);
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.
1024 * Returns a node pointer with refcount incremented, use
1025 * of_node_put() on it when done.
1027 struct device_node *of_find_compatible_node(struct device_node *from,
1028 const char *type, const char *compatible)
1030 struct device_node *np = from ? from->allnext : allnodes;
1032 for (; np != 0; np = np->allnext) {
1034 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1036 if (device_is_compatible(np, compatible))
1041 return of_node_get(np);
1045 * of_find_node_by_path - Find a node matching a full OF path
1046 * @path: The full path to match
1048 * Returns a node pointer with refcount incremented, use
1049 * of_node_put() on it when done.
1051 struct device_node *of_find_node_by_path(const char *path)
1053 struct device_node *np = allnodes;
1055 for (; np != 0; np = np->allnext)
1056 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1058 return of_node_get(np);
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
1066 * Returns a node pointer with refcount incremented, use
1067 * of_node_put() on it when done.
1069 struct device_node *of_find_all_nodes(struct device_node *prev)
1071 return of_node_get(prev ? prev->allnext : allnodes);
1075 * of_get_parent - Get a node's parent if any
1076 * @node: Node to get parent
1078 * Returns a node pointer with refcount incremented, use
1079 * of_node_put() on it when done.
1081 struct device_node *of_get_parent(const struct device_node *node)
1083 return node ? of_node_get(node->parent) : NULL;
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
1091 * Returns a node pointer with refcount incremented, use
1092 * of_node_put() on it when done.
1094 struct device_node *of_get_next_child(const struct device_node *node,
1095 struct device_node *prev)
1097 struct device_node *next = prev ? prev->sibling : node->child;
1099 for (; next != 0; next = next->sibling)
1100 if (of_node_get(next))
1108 * of_node_get - Increment refcount of a node
1109 * @node: Node to inc refcount, NULL is supported to
1110 * simplify writing of callers
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
1115 struct device_node *of_node_get(struct device_node *node)
1121 * of_node_put - Decrement refcount of a node
1122 * @node: Node to dec refcount, NULL is supported to
1123 * simplify writing of callers
1125 * Current implementation does nothing as we don't yet do dynamic node
1126 * allocation on ppc32
1128 void of_node_put(struct device_node *node)
1133 * Find the device_node with a given phandle.
1135 static struct device_node * __init
1136 find_phandle(phandle ph)
1138 struct device_node *np;
1140 for (np = allnodes; np != 0; np = np->allnext)
1147 * Find a property with a given name for a given node
1148 * and return the value.
1151 get_property(struct device_node *np, const char *name, int *lenp)
1153 struct property *pp;
1155 for (pp = np->properties; pp != 0; pp = pp->next)
1156 if (pp->name != NULL && strcmp(pp->name, name) == 0) {
1165 * Add a property to a node
1168 prom_add_property(struct device_node* np, struct property* prop)
1170 struct property **next = &np->properties;
1174 next = &(*next)->next;
1180 /* I quickly hacked that one, check against spec ! */
1181 static inline unsigned long
1182 bus_space_to_resource_flags(unsigned int bus_space)
1184 u8 space = (bus_space >> 24) & 0xf;
1188 return IORESOURCE_MEM;
1189 else if (space == 0x01)
1190 return IORESOURCE_IO;
1192 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
1198 static struct resource*
1199 find_parent_pci_resource(struct pci_dev* pdev, struct address_range *range)
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");
1218 if (i == DEVICE_COUNT_RESOURCE)
1220 return &pdev->resource[i];
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.
1229 request_OF_resource(struct device_node* node, int index, const char* name_postfix)
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;
1239 if (index >= node->n_addrs)
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;
1251 /* Find a PCI parent if any */
1255 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1256 pcidev = pci_find_slot(pci_bus, pci_devfn);
1261 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1263 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
1268 res = __request_region(parent, node->addrs[index].address, node->addrs[index].size, NULL);
1271 nlen = strlen(node->name);
1272 plen = name_postfix ? strlen(name_postfix) : 0;
1273 res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
1275 strcpy((char *)res->name, node->name);
1277 strcpy((char *)res->name+nlen, name_postfix);
1285 release_OF_resource(struct device_node* node, int index)
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;
1294 if (index >= node->n_addrs)
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;
1306 /* Find a PCI parent if any */
1310 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1311 pcidev = pci_find_slot(pci_bus, pci_devfn);
1316 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1318 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
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;
1328 if (res->start == start && res->end == end &&
1329 (res->flags & IORESOURCE_BUSY))
1331 if (res->start <= start && res->end >= end)
1341 release_resource(res);
1349 print_properties(struct device_node *np)
1351 struct property *pp;
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)
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))
1364 if (i == 0 && pp->length > 1) {
1365 /* looks like a string */
1366 printk(" %s\n", (char *) pp->value);
1368 /* dump it in hex */
1372 if (pp->length % 4 == 0) {
1373 unsigned int *p = (unsigned int *) pp->value;
1376 for (i = 0; i < n; ++i) {
1377 if (i != 0 && (i % 4) == 0)
1379 printk(" %08x", *p++);
1382 unsigned char *bp = pp->value;
1384 for (i = 0; i < n; ++i) {
1385 if (i != 0 && (i % 16) == 0)
1387 printk(" %02x", *bp++);
1391 if (pp->length > 64)
1392 printk(" ... (length = %d)\n",
1399 static DEFINE_SPINLOCK(rtas_lock);
1401 /* this can be called after setup -- Cort */
1403 call_rtas(const char *service, int nargs, int nret,
1404 unsigned long *outputs, ...)
1409 struct device_node *rtas;
1412 unsigned long words[16];
1416 rtas = find_devices("rtas");
1419 tokp = (int *) get_property(rtas, service, NULL);
1421 printk(KERN_ERR "No RTAS service called %s\n", service);
1427 va_start(list, outputs);
1428 for (i = 0; i < nargs; ++i)
1429 u.words[i+3] = va_arg(list, unsigned long);
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.
1438 spin_lock_irqsave(&rtas_lock, s);
1439 enter_rtas((void *)__pa(&u));
1440 spin_unlock_irqrestore(&rtas_lock, s);
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];