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;
73 extern unsigned int rtas_entry; /* physical pointer */
75 extern struct device_node *allnodes;
77 static unsigned long finish_node(struct device_node *, unsigned long,
78 interpret_func *, int, int);
79 static unsigned long finish_node_interrupts(struct device_node *, unsigned long);
80 static struct device_node *find_phandle(phandle);
82 extern void enter_rtas(void *);
83 void phys_call_rtas(int, int, int, ...);
85 extern char cmd_line[512]; /* XXX */
86 extern boot_infos_t *boot_infos;
87 unsigned long dev_tree_size;
90 phys_call_rtas(int service, int nargs, int nret, ...)
94 unsigned long words[16];
97 void (*rtas)(void *, unsigned long);
100 u.words[0] = service;
103 va_start(list, nret);
104 for (i = 0; i < nargs; ++i)
105 u.words[i+3] = va_arg(list, unsigned long);
108 rtas = (void (*)(void *, unsigned long)) rtas_entry;
113 * finish_device_tree is called once things are running normally
114 * (i.e. with text and data mapped to the address they were linked at).
115 * It traverses the device tree and fills in the name, type,
116 * {n_}addrs and {n_}intrs fields of each node.
119 finish_device_tree(void)
121 unsigned long mem = (unsigned long) klimit;
122 struct device_node *np;
124 /* All CHRPs now use the interrupt tree */
125 for (np = allnodes; np != NULL; np = np->allnext) {
126 if (get_property(np, "interrupt-parent", NULL)) {
127 use_of_interrupt_tree = 1;
132 if (use_of_interrupt_tree) {
134 * We want to find out here how many interrupt-controller
135 * nodes there are, and if we are booted from BootX,
136 * we need a pointer to the first (and hopefully only)
137 * such node. But we can't use find_devices here since
138 * np->name has not been set yet. -- paulus
144 for (np = allnodes; np != NULL; np = np->allnext) {
145 ic = get_property(np, "interrupt-controller", &iclen);
146 name = get_property(np, "name", NULL);
147 /* checking iclen makes sure we don't get a false
148 match on /chosen.interrupt_controller */
150 && strcmp(name, "interrupt-controller") == 0)
151 || (ic != NULL && iclen == 0 && strcmp(name, "AppleKiwi"))) {
153 dflt_interrupt_controller = np;
157 num_interrupt_controllers = n;
160 mem = finish_node(allnodes, mem, NULL, 1, 1);
161 dev_tree_size = mem - (unsigned long) allnodes;
162 klimit = (char *) mem;
165 static unsigned long __init
166 finish_node(struct device_node *np, unsigned long mem_start,
167 interpret_func *ifunc, int naddrc, int nsizec)
169 struct device_node *child;
172 np->name = get_property(np, "name", NULL);
173 np->type = get_property(np, "device_type", NULL);
180 /* get the device addresses and interrupts */
182 mem_start = ifunc(np, mem_start, naddrc, nsizec);
184 if (use_of_interrupt_tree)
185 mem_start = finish_node_interrupts(np, mem_start);
187 /* Look for #address-cells and #size-cells properties. */
188 ip = (int *) get_property(np, "#address-cells", NULL);
191 ip = (int *) get_property(np, "#size-cells", NULL);
195 if (np->parent == NULL)
196 ifunc = interpret_root_props;
197 else if (np->type == 0)
199 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
200 ifunc = interpret_pci_props;
201 else if (!strcmp(np->type, "dbdma"))
202 ifunc = interpret_dbdma_props;
203 else if (!strcmp(np->type, "mac-io")
204 || ifunc == interpret_macio_props)
205 ifunc = interpret_macio_props;
206 else if (!strcmp(np->type, "isa"))
207 ifunc = interpret_isa_props;
208 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
209 ifunc = interpret_root_props;
210 else if (!((ifunc == interpret_dbdma_props
211 || ifunc == interpret_macio_props)
212 && (!strcmp(np->type, "escc")
213 || !strcmp(np->type, "media-bay"))))
216 /* if we were booted from BootX, convert the full name */
218 && strncmp(np->full_name, "Devices:device-tree", 19) == 0) {
219 if (np->full_name[19] == 0) {
220 strcpy(np->full_name, "/");
221 } else if (np->full_name[19] == ':') {
222 char *p = np->full_name + 19;
230 for (child = np->child; child != NULL; child = child->sibling)
231 mem_start = finish_node(child, mem_start, ifunc,
238 * Find the interrupt parent of a node.
240 static struct device_node * __init
241 intr_parent(struct device_node *p)
245 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
248 p = find_phandle(*parp);
252 * On a powermac booted with BootX, we don't get to know the
253 * phandles for any nodes, so find_phandle will return NULL.
254 * Fortunately these machines only have one interrupt controller
255 * so there isn't in fact any ambiguity. -- paulus
257 if (num_interrupt_controllers == 1)
258 p = dflt_interrupt_controller;
263 * Find out the size of each entry of the interrupts property
267 prom_n_intr_cells(struct device_node *np)
269 struct device_node *p;
272 for (p = np; (p = intr_parent(p)) != NULL; ) {
273 icp = (unsigned int *)
274 get_property(p, "#interrupt-cells", NULL);
277 if (get_property(p, "interrupt-controller", NULL) != NULL
278 || get_property(p, "interrupt-map", NULL) != NULL) {
279 printk("oops, node %s doesn't have #interrupt-cells\n",
284 printk("prom_n_intr_cells failed for %s\n", np->full_name);
289 * Map an interrupt from a device up to the platform interrupt
293 map_interrupt(unsigned int **irq, struct device_node **ictrler,
294 struct device_node *np, unsigned int *ints, int nintrc)
296 struct device_node *p, *ipar;
297 unsigned int *imap, *imask, *ip;
298 int i, imaplen, match;
299 int newintrc = 1, newaddrc = 1;
303 reg = (unsigned int *) get_property(np, "reg", NULL);
304 naddrc = prom_n_addr_cells(np);
307 if (get_property(p, "interrupt-controller", NULL) != NULL)
308 /* this node is an interrupt controller, stop here */
310 imap = (unsigned int *)
311 get_property(p, "interrupt-map", &imaplen);
316 imask = (unsigned int *)
317 get_property(p, "interrupt-map-mask", NULL);
319 printk("oops, %s has interrupt-map but no mask\n",
323 imaplen /= sizeof(unsigned int);
326 while (imaplen > 0 && !match) {
327 /* check the child-interrupt field */
329 for (i = 0; i < naddrc && match; ++i)
330 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
331 for (; i < naddrc + nintrc && match; ++i)
332 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
333 imap += naddrc + nintrc;
334 imaplen -= naddrc + nintrc;
335 /* grab the interrupt parent */
336 ipar = find_phandle((phandle) *imap++);
338 if (ipar == NULL && num_interrupt_controllers == 1)
339 /* cope with BootX not giving us phandles */
340 ipar = dflt_interrupt_controller;
342 printk("oops, no int parent %x in map of %s\n",
343 imap[-1], p->full_name);
346 /* find the parent's # addr and intr cells */
347 ip = (unsigned int *)
348 get_property(ipar, "#interrupt-cells", NULL);
350 printk("oops, no #interrupt-cells on %s\n",
355 ip = (unsigned int *)
356 get_property(ipar, "#address-cells", NULL);
357 newaddrc = (ip == NULL)? 0: *ip;
358 imap += newaddrc + newintrc;
359 imaplen -= newaddrc + newintrc;
362 printk("oops, error decoding int-map on %s, len=%d\n",
363 p->full_name, imaplen);
367 printk("oops, no match in %s int-map for %s\n",
368 p->full_name, np->full_name);
374 ints = imap - nintrc;
378 printk("hmmm, int tree for %s doesn't have ctrler\n",
386 * New version of finish_node_interrupts.
388 static unsigned long __init
389 finish_node_interrupts(struct device_node *np, unsigned long mem_start)
392 int intlen, intrcells;
395 struct device_node *ic;
397 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
400 intrcells = prom_n_intr_cells(np);
401 intlen /= intrcells * sizeof(unsigned int);
402 np->n_intrs = intlen;
403 np->intrs = (struct interrupt_info *) mem_start;
404 mem_start += intlen * sizeof(struct interrupt_info);
406 for (i = 0; i < intlen; ++i) {
407 np->intrs[i].line = 0;
408 np->intrs[i].sense = 1;
409 n = map_interrupt(&irq, &ic, np, ints, intrcells);
414 * On a CHRP we have an 8259 which is subordinate to
415 * the openpic in the interrupt tree, but we want the
416 * openpic's interrupt numbers offsetted, not the 8259's.
417 * So we apply the offset if the controller is at the
418 * root of the interrupt tree, i.e. has no interrupt-parent.
419 * This doesn't cope with the general case of multiple
420 * cascaded interrupt controllers, but then neither will
421 * irq.c at the moment either. -- paulus
422 * The G5 triggers that code, I add a machine test. On
423 * those machines, we want to offset interrupts from the
424 * second openpic by 128 -- BenH
426 if (num_interrupt_controllers > 1
428 && get_property(ic, "interrupt-parent", NULL) == NULL)
431 np->intrs[i].line = irq[0] + offset;
433 np->intrs[i].sense = irq[1];
435 printk("hmmm, got %d intr cells for %s:", n,
437 for (j = 0; j < n; ++j)
438 printk(" %d", irq[j]);
448 * When BootX makes a copy of the device tree from the MacOS
449 * Name Registry, it is in the format we use but all of the pointers
450 * are offsets from the start of the tree.
451 * This procedure updates the pointers.
457 struct device_node *np;
460 #define ADDBASE(x) (x = (typeof (x))((x)? ((unsigned long)(x) + base): 0))
462 base = (unsigned long) boot_infos + boot_infos->deviceTreeOffset;
463 allnodes = (struct device_node *)(base + 4);
464 for (np = allnodes; np != 0; np = np->allnext) {
465 ADDBASE(np->full_name);
466 ADDBASE(np->properties);
469 ADDBASE(np->sibling);
470 ADDBASE(np->allnext);
471 for (pp = np->properties; pp != 0; pp = pp->next) {
480 prom_n_addr_cells(struct device_node* np)
486 ip = (int *) get_property(np, "#address-cells", NULL);
489 } while (np->parent);
490 /* No #address-cells property for the root node, default to 1 */
495 prom_n_size_cells(struct device_node* np)
501 ip = (int *) get_property(np, "#size-cells", NULL);
504 } while (np->parent);
505 /* No #size-cells property for the root node, default to 1 */
509 static unsigned long __init
510 map_addr(struct device_node *np, unsigned long space, unsigned long addr)
513 unsigned int *ranges;
517 type = (space >> 24) & 3;
521 while ((np = np->parent) != NULL) {
522 if (strcmp(np->type, "pci") != 0)
524 /* PCI bridge: map the address through the ranges property */
525 na = prom_n_addr_cells(np);
526 ranges = (unsigned int *) get_property(np, "ranges", &rlen);
527 while ((rlen -= (na + 5) * sizeof(unsigned int)) >= 0) {
528 if (((ranges[0] >> 24) & 3) == type
530 && addr - ranges[2] < ranges[na+4]) {
531 /* ok, this matches, translate it */
532 addr += ranges[na+2] - ranges[2];
541 static unsigned long __init
542 interpret_pci_props(struct device_node *np, unsigned long mem_start,
543 int naddrc, int nsizec)
545 struct address_range *adr;
546 struct pci_reg_property *pci_addrs;
549 pci_addrs = (struct pci_reg_property *)
550 get_property(np, "assigned-addresses", &l);
551 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
553 adr = (struct address_range *) mem_start;
554 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
555 adr[i].space = pci_addrs[i].addr.a_hi;
556 adr[i].address = map_addr(np, pci_addrs[i].addr.a_hi,
557 pci_addrs[i].addr.a_lo);
558 adr[i].size = pci_addrs[i].size_lo;
563 mem_start += i * sizeof(struct address_range);
566 if (use_of_interrupt_tree)
569 ip = (int *) get_property(np, "AAPL,interrupts", &l);
570 if (ip == 0 && np->parent)
571 ip = (int *) get_property(np->parent, "AAPL,interrupts", &l);
573 ip = (int *) get_property(np, "interrupts", &l);
575 np->intrs = (struct interrupt_info *) mem_start;
576 np->n_intrs = l / sizeof(int);
577 mem_start += np->n_intrs * sizeof(struct interrupt_info);
578 for (i = 0; i < np->n_intrs; ++i) {
579 np->intrs[i].line = *ip++;
580 np->intrs[i].sense = 1;
587 static unsigned long __init
588 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
589 int naddrc, int nsizec)
591 struct reg_property *rp;
592 struct address_range *adr;
593 unsigned long base_address;
595 struct device_node *db;
598 for (db = np->parent; db != NULL; db = db->parent) {
599 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
600 base_address = db->addrs[0].address;
605 rp = (struct reg_property *) get_property(np, "reg", &l);
606 if (rp != 0 && l >= sizeof(struct reg_property)) {
608 adr = (struct address_range *) mem_start;
609 while ((l -= sizeof(struct reg_property)) >= 0) {
611 adr[i].address = rp[i].address + base_address;
612 adr[i].size = rp[i].size;
617 mem_start += i * sizeof(struct address_range);
620 if (use_of_interrupt_tree)
623 ip = (int *) get_property(np, "AAPL,interrupts", &l);
625 ip = (int *) get_property(np, "interrupts", &l);
627 np->intrs = (struct interrupt_info *) mem_start;
628 np->n_intrs = l / sizeof(int);
629 mem_start += np->n_intrs * sizeof(struct interrupt_info);
630 for (i = 0; i < np->n_intrs; ++i) {
631 np->intrs[i].line = *ip++;
632 np->intrs[i].sense = 1;
639 static unsigned long __init
640 interpret_macio_props(struct device_node *np, unsigned long mem_start,
641 int naddrc, int nsizec)
643 struct reg_property *rp;
644 struct address_range *adr;
645 unsigned long base_address;
647 struct device_node *db;
650 for (db = np->parent; db != NULL; db = db->parent) {
651 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
652 base_address = db->addrs[0].address;
657 rp = (struct reg_property *) get_property(np, "reg", &l);
658 if (rp != 0 && l >= sizeof(struct reg_property)) {
660 adr = (struct address_range *) mem_start;
661 while ((l -= sizeof(struct reg_property)) >= 0) {
663 adr[i].address = rp[i].address + base_address;
664 adr[i].size = rp[i].size;
669 mem_start += i * sizeof(struct address_range);
672 if (use_of_interrupt_tree)
675 ip = (int *) get_property(np, "interrupts", &l);
677 ip = (int *) get_property(np, "AAPL,interrupts", &l);
679 np->intrs = (struct interrupt_info *) mem_start;
680 np->n_intrs = l / sizeof(int);
681 for (i = 0; i < np->n_intrs; ++i) {
682 np->intrs[i].line = *ip++;
683 np->intrs[i].sense = 1;
685 mem_start += np->n_intrs * sizeof(struct interrupt_info);
691 static unsigned long __init
692 interpret_isa_props(struct device_node *np, unsigned long mem_start,
693 int naddrc, int nsizec)
695 struct isa_reg_property *rp;
696 struct address_range *adr;
699 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
700 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
702 adr = (struct address_range *) mem_start;
703 while ((l -= sizeof(struct reg_property)) >= 0) {
704 adr[i].space = rp[i].space;
705 adr[i].address = rp[i].address
706 + (adr[i].space? 0: _ISA_MEM_BASE);
707 adr[i].size = rp[i].size;
712 mem_start += i * sizeof(struct address_range);
715 if (use_of_interrupt_tree)
718 ip = (int *) get_property(np, "interrupts", &l);
720 np->intrs = (struct interrupt_info *) mem_start;
721 np->n_intrs = l / (2 * sizeof(int));
722 mem_start += np->n_intrs * sizeof(struct interrupt_info);
723 for (i = 0; i < np->n_intrs; ++i) {
724 np->intrs[i].line = *ip++;
725 np->intrs[i].sense = *ip++;
732 static unsigned long __init
733 interpret_root_props(struct device_node *np, unsigned long mem_start,
734 int naddrc, int nsizec)
736 struct address_range *adr;
739 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
741 rp = (unsigned int *) get_property(np, "reg", &l);
742 if (rp != 0 && l >= rpsize) {
744 adr = (struct address_range *) mem_start;
745 while ((l -= rpsize) >= 0) {
746 adr[i].space = (naddrc >= 2? rp[naddrc-2]: 2);
747 adr[i].address = rp[naddrc - 1];
748 adr[i].size = rp[naddrc + nsizec - 1];
750 rp += naddrc + nsizec;
754 mem_start += i * sizeof(struct address_range);
757 if (use_of_interrupt_tree)
760 ip = (int *) get_property(np, "AAPL,interrupts", &l);
762 ip = (int *) get_property(np, "interrupts", &l);
764 np->intrs = (struct interrupt_info *) mem_start;
765 np->n_intrs = l / sizeof(int);
766 mem_start += np->n_intrs * sizeof(struct interrupt_info);
767 for (i = 0; i < np->n_intrs; ++i) {
768 np->intrs[i].line = *ip++;
769 np->intrs[i].sense = 1;
777 * Work out the sense (active-low level / active-high edge)
778 * of each interrupt from the device tree.
781 prom_get_irq_senses(unsigned char *senses, int off, int max)
783 struct device_node *np;
786 /* default to level-triggered */
787 memset(senses, 1, max - off);
788 if (!use_of_interrupt_tree)
791 for (np = allnodes; np != 0; np = np->allnext) {
792 for (j = 0; j < np->n_intrs; j++) {
793 i = np->intrs[j].line;
794 if (i >= off && i < max) {
795 if (np->intrs[j].sense == 1)
796 senses[i-off] = (IRQ_SENSE_LEVEL
797 | IRQ_POLARITY_NEGATIVE);
799 senses[i-off] = (IRQ_SENSE_EDGE
800 | IRQ_POLARITY_POSITIVE);
807 * Construct and return a list of the device_nodes with a given name.
810 find_devices(const char *name)
812 struct device_node *head, **prevp, *np;
815 for (np = allnodes; np != 0; np = np->allnext) {
816 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
826 * Construct and return a list of the device_nodes with a given type.
829 find_type_devices(const char *type)
831 struct device_node *head, **prevp, *np;
834 for (np = allnodes; np != 0; np = np->allnext) {
835 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
845 * Returns all nodes linked together
850 struct device_node *head, **prevp, *np;
853 for (np = allnodes; np != 0; np = np->allnext) {
861 /* Checks if the given "compat" string matches one of the strings in
862 * the device's "compatible" property
865 device_is_compatible(struct device_node *device, const char *compat)
870 cp = (char *) get_property(device, "compatible", &cplen);
874 if (strncasecmp(cp, compat, strlen(compat)) == 0)
886 * Indicates whether the root node has a given value in its
887 * compatible property.
890 machine_is_compatible(const char *compat)
892 struct device_node *root;
894 root = find_path_device("/");
897 return device_is_compatible(root, compat);
901 * Construct and return a list of the device_nodes with a given type
902 * and compatible property.
905 find_compatible_devices(const char *type, const char *compat)
907 struct device_node *head, **prevp, *np;
910 for (np = allnodes; np != 0; np = np->allnext) {
912 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
914 if (device_is_compatible(np, compat)) {
924 * Find the device_node with a given full_name.
927 find_path_device(const char *path)
929 struct device_node *np;
931 for (np = allnodes; np != 0; np = np->allnext)
932 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
939 * New implementation of the OF "find" APIs, return a refcounted
940 * object, call of_node_put() when done. Currently, still lacks
941 * locking as old implementation, this is beeing done for ppc64.
943 * Note that property management will need some locking as well,
944 * this isn't dealt with yet
949 * of_find_node_by_name - Find a node by it's "name" property
950 * @from: The node to start searching from or NULL, the node
951 * you pass will not be searched, only the next one
952 * will; typically, you pass what the previous call
953 * returned. of_node_put() will be called on it
954 * @name: The name string to match against
956 * Returns a node pointer with refcount incremented, use
957 * of_node_put() on it when done.
959 struct device_node *of_find_node_by_name(struct device_node *from,
962 struct device_node *np = from ? from->allnext : allnodes;
964 for (; np != 0; np = np->allnext)
965 if (np->name != 0 && strcasecmp(np->name, name) == 0)
969 return of_node_get(np);
973 * of_find_node_by_type - Find a node by it's "device_type" property
974 * @from: The node to start searching from or NULL, the node
975 * you pass will not be searched, only the next one
976 * will; typically, you pass what the previous call
977 * returned. of_node_put() will be called on it
978 * @name: The type string to match against
980 * Returns a node pointer with refcount incremented, use
981 * of_node_put() on it when done.
983 struct device_node *of_find_node_by_type(struct device_node *from,
986 struct device_node *np = from ? from->allnext : allnodes;
988 for (; np != 0; np = np->allnext)
989 if (np->type != 0 && strcasecmp(np->type, type) == 0)
993 return of_node_get(np);
997 * of_find_compatible_node - Find a node based on type and one of the
998 * tokens in it's "compatible" property
999 * @from: The node to start searching from or NULL, the node
1000 * you pass will not be searched, only the next one
1001 * will; typically, you pass what the previous call
1002 * returned. of_node_put() will be called on it
1003 * @type: The type string to match "device_type" or NULL to ignore
1004 * @compatible: The string to match to one of the tokens in the device
1005 * "compatible" list.
1007 * Returns a node pointer with refcount incremented, use
1008 * of_node_put() on it when done.
1010 struct device_node *of_find_compatible_node(struct device_node *from,
1011 const char *type, const char *compatible)
1013 struct device_node *np = from ? from->allnext : allnodes;
1015 for (; np != 0; np = np->allnext) {
1017 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1019 if (device_is_compatible(np, compatible))
1024 return of_node_get(np);
1028 * of_find_node_by_path - Find a node matching a full OF path
1029 * @path: The full path to match
1031 * Returns a node pointer with refcount incremented, use
1032 * of_node_put() on it when done.
1034 struct device_node *of_find_node_by_path(const char *path)
1036 struct device_node *np = allnodes;
1038 for (; np != 0; np = np->allnext)
1039 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1041 return of_node_get(np);
1045 * of_find_all_nodes - Get next node in global list
1046 * @prev: Previous node or NULL to start iteration
1047 * of_node_put() will be called on it
1049 * Returns a node pointer with refcount incremented, use
1050 * of_node_put() on it when done.
1052 struct device_node *of_find_all_nodes(struct device_node *prev)
1054 return of_node_get(prev ? prev->allnext : allnodes);
1058 * of_get_parent - Get a node's parent if any
1059 * @node: Node to get parent
1061 * Returns a node pointer with refcount incremented, use
1062 * of_node_put() on it when done.
1064 struct device_node *of_get_parent(const struct device_node *node)
1066 return node ? of_node_get(node->parent) : NULL;
1070 * of_get_next_child - Iterate a node childs
1071 * @node: parent node
1072 * @prev: previous child of the parent node, or NULL to get first
1074 * Returns a node pointer with refcount incremented, use
1075 * of_node_put() on it when done.
1077 struct device_node *of_get_next_child(const struct device_node *node,
1078 struct device_node *prev)
1080 struct device_node *next = prev ? prev->sibling : node->child;
1082 for (; next != 0; next = next->sibling)
1083 if (of_node_get(next))
1091 * of_node_get - Increment refcount of a node
1092 * @node: Node to inc refcount, NULL is supported to
1093 * simplify writing of callers
1095 * Returns the node itself or NULL if gone. Current implementation
1096 * does nothing as we don't yet do dynamic node allocation on ppc32
1098 struct device_node *of_node_get(struct device_node *node)
1104 * of_node_put - Decrement refcount of a node
1105 * @node: Node to dec refcount, NULL is supported to
1106 * simplify writing of callers
1108 * Current implementation does nothing as we don't yet do dynamic node
1109 * allocation on ppc32
1111 void of_node_put(struct device_node *node)
1116 * Find the device_node with a given phandle.
1118 static struct device_node * __init
1119 find_phandle(phandle ph)
1121 struct device_node *np;
1123 for (np = allnodes; np != 0; np = np->allnext)
1130 * Find a property with a given name for a given node
1131 * and return the value.
1134 get_property(struct device_node *np, const char *name, int *lenp)
1136 struct property *pp;
1138 for (pp = np->properties; pp != 0; pp = pp->next)
1139 if (pp->name != NULL && strcmp(pp->name, name) == 0) {
1148 * Add a property to a node
1151 prom_add_property(struct device_node* np, struct property* prop)
1153 struct property **next = &np->properties;
1157 next = &(*next)->next;
1163 /* I quickly hacked that one, check against spec ! */
1164 static inline unsigned long
1165 bus_space_to_resource_flags(unsigned int bus_space)
1167 u8 space = (bus_space >> 24) & 0xf;
1171 return IORESOURCE_MEM;
1172 else if (space == 0x01)
1173 return IORESOURCE_IO;
1175 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
1181 static struct resource*
1182 find_parent_pci_resource(struct pci_dev* pdev, struct address_range *range)
1187 /* Check this one */
1188 mask = bus_space_to_resource_flags(range->space);
1189 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
1190 if ((pdev->resource[i].flags & mask) == mask &&
1191 pdev->resource[i].start <= range->address &&
1192 pdev->resource[i].end > range->address) {
1193 if ((range->address + range->size - 1) > pdev->resource[i].end) {
1194 /* Add better message */
1195 printk(KERN_WARNING "PCI/OF resource overlap !\n");
1201 if (i == DEVICE_COUNT_RESOURCE)
1203 return &pdev->resource[i];
1207 * Request an OF device resource. Currently handles child of PCI devices,
1208 * or other nodes attached to the root node. Ultimately, put some
1209 * link to resources in the OF node.
1212 request_OF_resource(struct device_node* node, int index, const char* name_postfix)
1214 struct pci_dev* pcidev;
1215 u8 pci_bus, pci_devfn;
1216 unsigned long iomask;
1217 struct device_node* nd;
1218 struct resource* parent;
1219 struct resource *res = NULL;
1222 if (index >= node->n_addrs)
1225 /* Sanity check on bus space */
1226 iomask = bus_space_to_resource_flags(node->addrs[index].space);
1227 if (iomask & IORESOURCE_MEM)
1228 parent = &iomem_resource;
1229 else if (iomask & IORESOURCE_IO)
1230 parent = &ioport_resource;
1234 /* Find a PCI parent if any */
1238 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1239 pcidev = pci_find_slot(pci_bus, pci_devfn);
1244 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1246 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
1251 res = __request_region(parent, node->addrs[index].address, node->addrs[index].size, NULL);
1254 nlen = strlen(node->name);
1255 plen = name_postfix ? strlen(name_postfix) : 0;
1256 res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
1258 strcpy((char *)res->name, node->name);
1260 strcpy((char *)res->name+nlen, name_postfix);
1268 release_OF_resource(struct device_node* node, int index)
1270 struct pci_dev* pcidev;
1271 u8 pci_bus, pci_devfn;
1272 unsigned long iomask, start, end;
1273 struct device_node* nd;
1274 struct resource* parent;
1275 struct resource *res = NULL;
1277 if (index >= node->n_addrs)
1280 /* Sanity check on bus space */
1281 iomask = bus_space_to_resource_flags(node->addrs[index].space);
1282 if (iomask & IORESOURCE_MEM)
1283 parent = &iomem_resource;
1284 else if (iomask & IORESOURCE_IO)
1285 parent = &ioport_resource;
1289 /* Find a PCI parent if any */
1293 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
1294 pcidev = pci_find_slot(pci_bus, pci_devfn);
1299 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
1301 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
1306 /* Find us in the parent and its childs */
1307 res = parent->child;
1308 start = node->addrs[index].address;
1309 end = start + node->addrs[index].size - 1;
1311 if (res->start == start && res->end == end &&
1312 (res->flags & IORESOURCE_BUSY))
1314 if (res->start <= start && res->end >= end)
1324 release_resource(res);
1332 print_properties(struct device_node *np)
1334 struct property *pp;
1338 for (pp = np->properties; pp != 0; pp = pp->next) {
1339 printk(KERN_INFO "%s", pp->name);
1340 for (i = strlen(pp->name); i < 16; ++i)
1342 cp = (char *) pp->value;
1343 for (i = pp->length; i > 0; --i, ++cp)
1344 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1345 || (i == 1 && *cp != 0))
1347 if (i == 0 && pp->length > 1) {
1348 /* looks like a string */
1349 printk(" %s\n", (char *) pp->value);
1351 /* dump it in hex */
1355 if (pp->length % 4 == 0) {
1356 unsigned int *p = (unsigned int *) pp->value;
1359 for (i = 0; i < n; ++i) {
1360 if (i != 0 && (i % 4) == 0)
1362 printk(" %08x", *p++);
1365 unsigned char *bp = pp->value;
1367 for (i = 0; i < n; ++i) {
1368 if (i != 0 && (i % 16) == 0)
1370 printk(" %02x", *bp++);
1374 if (pp->length > 64)
1375 printk(" ... (length = %d)\n",
1382 static DEFINE_SPINLOCK(rtas_lock);
1384 /* this can be called after setup -- Cort */
1386 call_rtas(const char *service, int nargs, int nret,
1387 unsigned long *outputs, ...)
1392 struct device_node *rtas;
1395 unsigned long words[16];
1399 rtas = find_devices("rtas");
1402 tokp = (int *) get_property(rtas, service, NULL);
1404 printk(KERN_ERR "No RTAS service called %s\n", service);
1410 va_start(list, outputs);
1411 for (i = 0; i < nargs; ++i)
1412 u.words[i+3] = va_arg(list, unsigned long);
1416 * RTAS doesn't use floating point.
1417 * Or at least, according to the CHRP spec we enter RTAS
1418 * with FP disabled, and it doesn't change the FP registers.
1421 spin_lock_irqsave(&rtas_lock, s);
1422 enter_rtas((void *)__pa(&u));
1423 spin_unlock_irqrestore(&rtas_lock, s);
1425 if (nret > 1 && outputs != NULL)
1426 for (i = 0; i < nret-1; ++i)
1427 outputs[i] = u.words[i+nargs+4];
1428 return u.words[nargs+3];