1 #include <linux/string.h>
2 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
10 #include <asm/of_device.h>
12 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
14 struct of_device
* of_dev
= to_of_device(dev
);
15 struct of_platform_driver
* of_drv
= to_of_platform_driver(drv
);
16 const struct of_device_id
* matches
= of_drv
->match_table
;
21 return of_match_device(matches
, of_dev
) != NULL
;
24 static int of_device_probe(struct device
*dev
)
27 struct of_platform_driver
*drv
;
28 struct of_device
*of_dev
;
29 const struct of_device_id
*match
;
31 drv
= to_of_platform_driver(dev
->driver
);
32 of_dev
= to_of_device(dev
);
39 match
= of_match_device(drv
->match_table
, of_dev
);
41 error
= drv
->probe(of_dev
, match
);
48 static int of_device_remove(struct device
*dev
)
50 struct of_device
* of_dev
= to_of_device(dev
);
51 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
53 if (dev
->driver
&& drv
->remove
)
58 static int of_device_suspend(struct device
*dev
, pm_message_t state
)
60 struct of_device
* of_dev
= to_of_device(dev
);
61 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
64 if (dev
->driver
&& drv
->suspend
)
65 error
= drv
->suspend(of_dev
, state
);
69 static int of_device_resume(struct device
* dev
)
71 struct of_device
* of_dev
= to_of_device(dev
);
72 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
75 if (dev
->driver
&& drv
->resume
)
76 error
= drv
->resume(of_dev
);
80 void __iomem
*of_ioremap(struct resource
*res
, unsigned long offset
, unsigned long size
, char *name
)
82 unsigned long ret
= res
->start
+ offset
;
85 if (res
->flags
& IORESOURCE_MEM
)
86 r
= request_mem_region(ret
, size
, name
);
88 r
= request_region(ret
, size
, name
);
92 return (void __iomem
*) ret
;
94 EXPORT_SYMBOL(of_ioremap
);
96 void of_iounmap(struct resource
*res
, void __iomem
*base
, unsigned long size
)
98 if (res
->flags
& IORESOURCE_MEM
)
99 release_mem_region((unsigned long) base
, size
);
101 release_region((unsigned long) base
, size
);
103 EXPORT_SYMBOL(of_iounmap
);
105 static int node_match(struct device
*dev
, void *data
)
107 struct of_device
*op
= to_of_device(dev
);
108 struct device_node
*dp
= data
;
110 return (op
->node
== dp
);
113 struct of_device
*of_find_device_by_node(struct device_node
*dp
)
115 struct device
*dev
= bus_find_device(&of_bus_type
, NULL
,
119 return to_of_device(dev
);
123 EXPORT_SYMBOL(of_find_device_by_node
);
126 struct bus_type isa_bus_type
= {
128 .match
= of_platform_bus_match
,
129 .probe
= of_device_probe
,
130 .remove
= of_device_remove
,
131 .suspend
= of_device_suspend
,
132 .resume
= of_device_resume
,
134 EXPORT_SYMBOL(isa_bus_type
);
136 struct bus_type ebus_bus_type
= {
138 .match
= of_platform_bus_match
,
139 .probe
= of_device_probe
,
140 .remove
= of_device_remove
,
141 .suspend
= of_device_suspend
,
142 .resume
= of_device_resume
,
144 EXPORT_SYMBOL(ebus_bus_type
);
148 struct bus_type sbus_bus_type
= {
150 .match
= of_platform_bus_match
,
151 .probe
= of_device_probe
,
152 .remove
= of_device_remove
,
153 .suspend
= of_device_suspend
,
154 .resume
= of_device_resume
,
156 EXPORT_SYMBOL(sbus_bus_type
);
159 struct bus_type of_bus_type
= {
161 .match
= of_platform_bus_match
,
162 .probe
= of_device_probe
,
163 .remove
= of_device_remove
,
164 .suspend
= of_device_suspend
,
165 .resume
= of_device_resume
,
167 EXPORT_SYMBOL(of_bus_type
);
169 static inline u64
of_read_addr(const u32
*cell
, int size
)
173 r
= (r
<< 32) | *(cell
++);
177 static void __init
get_cells(struct device_node
*dp
,
178 int *addrc
, int *sizec
)
181 *addrc
= of_n_addr_cells(dp
);
183 *sizec
= of_n_size_cells(dp
);
186 /* Max address size we deal with */
187 #define OF_MAX_ADDR_CELLS 4
191 const char *addr_prop_name
;
192 int (*match
)(struct device_node
*parent
);
193 void (*count_cells
)(struct device_node
*child
,
194 int *addrc
, int *sizec
);
195 int (*map
)(u32
*addr
, const u32
*range
,
196 int na
, int ns
, int pna
);
197 unsigned int (*get_flags
)(const u32
*addr
);
201 * Default translator (generic bus)
204 static void of_bus_default_count_cells(struct device_node
*dev
,
205 int *addrc
, int *sizec
)
207 get_cells(dev
, addrc
, sizec
);
210 /* Make sure the least significant 64-bits are in-range. Even
211 * for 3 or 4 cell values it is a good enough approximation.
213 static int of_out_of_range(const u32
*addr
, const u32
*base
,
214 const u32
*size
, int na
, int ns
)
216 u64 a
= of_read_addr(addr
, na
);
217 u64 b
= of_read_addr(base
, na
);
222 b
+= of_read_addr(size
, ns
);
229 static int of_bus_default_map(u32
*addr
, const u32
*range
,
230 int na
, int ns
, int pna
)
232 u32 result
[OF_MAX_ADDR_CELLS
];
236 printk("of_device: Cannot handle size cells (%d) > 2.", ns
);
240 if (of_out_of_range(addr
, range
, range
+ na
+ pna
, na
, ns
))
243 /* Start with the parent range base. */
244 memcpy(result
, range
+ na
, pna
* 4);
246 /* Add in the child address offset. */
247 for (i
= 0; i
< na
; i
++)
248 result
[pna
- 1 - i
] +=
252 memcpy(addr
, result
, pna
* 4);
257 static unsigned int of_bus_default_get_flags(const u32
*addr
)
259 return IORESOURCE_MEM
;
263 * PCI bus specific translator
266 static int of_bus_pci_match(struct device_node
*np
)
268 if (!strcmp(np
->type
, "pci") || !strcmp(np
->type
, "pciex")) {
269 const char *model
= of_get_property(np
, "model", NULL
);
271 if (model
&& !strcmp(model
, "SUNW,simba"))
274 /* Do not do PCI specific frobbing if the
275 * PCI bridge lacks a ranges property. We
276 * want to pass it through up to the next
277 * parent as-is, not with the PCI translate
278 * method which chops off the top address cell.
280 if (!of_find_property(np
, "ranges", NULL
))
289 static int of_bus_simba_match(struct device_node
*np
)
291 const char *model
= of_get_property(np
, "model", NULL
);
293 if (model
&& !strcmp(model
, "SUNW,simba"))
296 /* Treat PCI busses lacking ranges property just like
299 if (!strcmp(np
->type
, "pci") || !strcmp(np
->type
, "pciex")) {
300 if (!of_find_property(np
, "ranges", NULL
))
307 static int of_bus_simba_map(u32
*addr
, const u32
*range
,
308 int na
, int ns
, int pna
)
313 static void of_bus_pci_count_cells(struct device_node
*np
,
314 int *addrc
, int *sizec
)
322 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
323 int na
, int ns
, int pna
)
325 u32 result
[OF_MAX_ADDR_CELLS
];
328 /* Check address type match */
329 if ((addr
[0] ^ range
[0]) & 0x03000000)
332 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
336 /* Start with the parent range base. */
337 memcpy(result
, range
+ na
, pna
* 4);
339 /* Add in the child address offset, skipping high cell. */
340 for (i
= 0; i
< na
- 1; i
++)
341 result
[pna
- 1 - i
] +=
345 memcpy(addr
, result
, pna
* 4);
350 static unsigned int of_bus_pci_get_flags(const u32
*addr
)
352 unsigned int flags
= 0;
355 switch((w
>> 24) & 0x03) {
357 flags
|= IORESOURCE_IO
;
358 case 0x02: /* 32 bits */
359 case 0x03: /* 64 bits */
360 flags
|= IORESOURCE_MEM
;
363 flags
|= IORESOURCE_PREFETCH
;
368 * SBUS bus specific translator
371 static int of_bus_sbus_match(struct device_node
*np
)
373 return !strcmp(np
->name
, "sbus") ||
374 !strcmp(np
->name
, "sbi");
377 static void of_bus_sbus_count_cells(struct device_node
*child
,
378 int *addrc
, int *sizec
)
387 * FHC/Central bus specific translator.
389 * This is just needed to hard-code the address and size cell
390 * counts. 'fhc' and 'central' nodes lack the #address-cells and
391 * #size-cells properties, and if you walk to the root on such
392 * Enterprise boxes all you'll get is a #size-cells of 2 which is
393 * not what we want to use.
395 static int of_bus_fhc_match(struct device_node
*np
)
397 return !strcmp(np
->name
, "fhc") ||
398 !strcmp(np
->name
, "central");
401 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
404 * Array of bus specific translators
407 static struct of_bus of_busses
[] = {
411 .addr_prop_name
= "assigned-addresses",
412 .match
= of_bus_pci_match
,
413 .count_cells
= of_bus_pci_count_cells
,
414 .map
= of_bus_pci_map
,
415 .get_flags
= of_bus_pci_get_flags
,
420 .addr_prop_name
= "assigned-addresses",
421 .match
= of_bus_simba_match
,
422 .count_cells
= of_bus_pci_count_cells
,
423 .map
= of_bus_simba_map
,
424 .get_flags
= of_bus_pci_get_flags
,
429 .addr_prop_name
= "reg",
430 .match
= of_bus_sbus_match
,
431 .count_cells
= of_bus_sbus_count_cells
,
432 .map
= of_bus_default_map
,
433 .get_flags
= of_bus_default_get_flags
,
438 .addr_prop_name
= "reg",
439 .match
= of_bus_fhc_match
,
440 .count_cells
= of_bus_fhc_count_cells
,
441 .map
= of_bus_default_map
,
442 .get_flags
= of_bus_default_get_flags
,
447 .addr_prop_name
= "reg",
449 .count_cells
= of_bus_default_count_cells
,
450 .map
= of_bus_default_map
,
451 .get_flags
= of_bus_default_get_flags
,
455 static struct of_bus
*of_match_bus(struct device_node
*np
)
459 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
460 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
461 return &of_busses
[i
];
466 static int __init
build_one_resource(struct device_node
*parent
,
470 int na
, int ns
, int pna
)
476 ranges
= of_get_property(parent
, "ranges", &rlen
);
477 if (ranges
== NULL
|| rlen
== 0) {
478 u32 result
[OF_MAX_ADDR_CELLS
];
481 memset(result
, 0, pna
* 4);
482 for (i
= 0; i
< na
; i
++)
483 result
[pna
- 1 - i
] =
486 memcpy(addr
, result
, pna
* 4);
490 /* Now walk through the ranges */
492 rone
= na
+ pna
+ ns
;
493 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
494 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
498 /* When we miss an I/O space match on PCI, just pass it up
499 * to the next PCI bridge and/or controller.
501 if (!strcmp(bus
->name
, "pci") &&
502 (addr
[0] & 0x03000000) == 0x01000000)
508 static int __init
use_1to1_mapping(struct device_node
*pp
)
510 /* If this is on the PMU bus, don't try to translate it even
511 * if a ranges property exists.
513 if (!strcmp(pp
->name
, "pmu"))
516 /* If we have a ranges property in the parent, use it. */
517 if (of_find_property(pp
, "ranges", NULL
) != NULL
)
520 /* If the parent is the dma node of an ISA bus, pass
521 * the translation up to the root.
523 if (!strcmp(pp
->name
, "dma"))
526 /* Similarly for all PCI bridges, if we get this far
527 * it lacks a ranges property, and this will include
530 if (!strcmp(pp
->type
, "pci") || !strcmp(pp
->type
, "pciex"))
536 static int of_resource_verbose
;
538 static void __init
build_device_resources(struct of_device
*op
,
539 struct device
*parent
)
541 struct of_device
*p_op
;
550 p_op
= to_of_device(parent
);
551 bus
= of_match_bus(p_op
->node
);
552 bus
->count_cells(op
->node
, &na
, &ns
);
554 preg
= of_get_property(op
->node
, bus
->addr_prop_name
, &num_reg
);
555 if (!preg
|| num_reg
== 0)
558 /* Convert to num-cells. */
561 /* Convert to num-entries. */
564 /* Prevent overrunning the op->resources[] array. */
565 if (num_reg
> PROMREG_MAX
) {
566 printk(KERN_WARNING
"%s: Too many regs (%d), "
568 op
->node
->full_name
, num_reg
, PROMREG_MAX
);
569 num_reg
= PROMREG_MAX
;
572 for (index
= 0; index
< num_reg
; index
++) {
573 struct resource
*r
= &op
->resource
[index
];
574 u32 addr
[OF_MAX_ADDR_CELLS
];
575 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
576 struct device_node
*dp
= op
->node
;
577 struct device_node
*pp
= p_op
->node
;
578 struct of_bus
*pbus
, *dbus
;
579 u64 size
, result
= OF_BAD_ADDR
;
584 size
= of_read_addr(reg
+ na
, ns
);
585 flags
= bus
->get_flags(reg
);
587 memcpy(addr
, reg
, na
* 4);
589 if (use_1to1_mapping(pp
)) {
590 result
= of_read_addr(addr
, na
);
602 result
= of_read_addr(addr
, dna
);
606 pbus
= of_match_bus(pp
);
607 pbus
->count_cells(dp
, &pna
, &pns
);
609 if (build_one_resource(dp
, dbus
, pbus
, addr
,
619 memset(r
, 0, sizeof(*r
));
621 if (of_resource_verbose
)
622 printk("%s reg[%d] -> %lx\n",
623 op
->node
->full_name
, index
,
626 if (result
!= OF_BAD_ADDR
) {
627 if (tlb_type
== hypervisor
)
628 result
&= 0x0fffffffffffffffUL
;
631 r
->end
= result
+ size
- 1;
634 r
->name
= op
->node
->name
;
638 static struct device_node
* __init
639 apply_interrupt_map(struct device_node
*dp
, struct device_node
*pp
,
640 const u32
*imap
, int imlen
, const u32
*imask
,
643 struct device_node
*cp
;
644 unsigned int irq
= *irq_p
;
650 bus
= of_match_bus(pp
);
651 bus
->count_cells(dp
, &na
, NULL
);
653 reg
= of_get_property(dp
, "reg", &num_reg
);
654 if (!reg
|| !num_reg
)
657 imlen
/= ((na
+ 3) * 4);
659 for (i
= 0; i
< imlen
; i
++) {
662 for (j
= 0; j
< na
; j
++) {
663 if ((reg
[j
] & imask
[j
]) != imap
[j
])
666 if (imap
[na
] == irq
) {
667 handle
= imap
[na
+ 1];
676 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
677 * properties that do not include the on-board device
678 * interrupts. Instead, the device's 'interrupts' property
679 * is already a fully specified INO value.
681 * Handle this by deciding that, if we didn't get a
682 * match in the parent's 'interrupt-map', and the
683 * parent is an IRQ translater, then use the parent as
684 * our IRQ controller.
693 cp
= of_find_node_by_phandle(handle
);
698 static unsigned int __init
pci_irq_swizzle(struct device_node
*dp
,
699 struct device_node
*pp
,
702 const struct linux_prom_pci_registers
*regs
;
703 unsigned int bus
, devfn
, slot
, ret
;
705 if (irq
< 1 || irq
> 4)
708 regs
= of_get_property(dp
, "reg", NULL
);
712 bus
= (regs
->phys_hi
>> 16) & 0xff;
713 devfn
= (regs
->phys_hi
>> 8) & 0xff;
714 slot
= (devfn
>> 3) & 0x1f;
717 /* Derived from Table 8-3, U2P User's Manual. This branch
718 * is handling a PCI controller that lacks a proper set of
719 * interrupt-map and interrupt-map-mask properties. The
720 * Ultra-E450 is one example.
722 * The bit layout is BSSLL, where:
723 * B: 0 on bus A, 1 on bus B
724 * D: 2-bit slot number, derived from PCI device number as
725 * (dev - 1) for bus A, or (dev - 2) for bus B
726 * L: 2-bit line number
731 slot
= (slot
- 1) << 2;
735 slot
= (slot
- 2) << 2;
739 ret
= (bus
| slot
| irq
);
741 /* Going through a PCI-PCI bridge that lacks a set of
742 * interrupt-map and interrupt-map-mask properties.
744 ret
= ((irq
- 1 + (slot
& 3)) & 3) + 1;
750 static int of_irq_verbose
;
752 static unsigned int __init
build_one_device_irq(struct of_device
*op
,
753 struct device
*parent
,
756 struct device_node
*dp
= op
->node
;
757 struct device_node
*pp
, *ip
;
758 unsigned int orig_irq
= irq
;
760 if (irq
== 0xffffffff)
764 irq
= dp
->irq_trans
->irq_build(dp
, irq
,
765 dp
->irq_trans
->data
);
768 printk("%s: direct translate %x --> %x\n",
769 dp
->full_name
, orig_irq
, irq
);
774 /* Something more complicated. Walk up to the root, applying
775 * interrupt-map or bus specific translations, until we hit
778 * If we hit a bus type or situation we cannot handle, we
779 * stop and assume that the original IRQ number was in a
780 * format which has special meaning to it's immediate parent.
785 const void *imap
, *imsk
;
788 imap
= of_get_property(pp
, "interrupt-map", &imlen
);
789 imsk
= of_get_property(pp
, "interrupt-map-mask", NULL
);
791 struct device_node
*iret
;
792 int this_orig_irq
= irq
;
794 iret
= apply_interrupt_map(dp
, pp
,
799 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
801 pp
->full_name
, this_orig_irq
,
802 (iret
? iret
->full_name
: "NULL"), irq
);
807 if (iret
->irq_trans
) {
812 if (!strcmp(pp
->type
, "pci") ||
813 !strcmp(pp
->type
, "pciex")) {
814 unsigned int this_orig_irq
= irq
;
816 irq
= pci_irq_swizzle(dp
, pp
, irq
);
818 printk("%s: PCI swizzle [%s] "
821 pp
->full_name
, this_orig_irq
,
837 irq
= ip
->irq_trans
->irq_build(op
->node
, irq
,
838 ip
->irq_trans
->data
);
840 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
841 op
->node
->full_name
, ip
->full_name
, orig_irq
, irq
);
846 static struct of_device
* __init
scan_one_device(struct device_node
*dp
,
847 struct device
*parent
)
849 struct of_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
850 const unsigned int *irq
;
858 op
->clock_freq
= of_getintprop_default(dp
, "clock-frequency",
860 op
->portid
= of_getintprop_default(dp
, "upa-portid", -1);
861 if (op
->portid
== -1)
862 op
->portid
= of_getintprop_default(dp
, "portid", -1);
864 irq
= of_get_property(dp
, "interrupts", &len
);
866 memcpy(op
->irqs
, irq
, len
);
867 op
->num_irqs
= len
/ 4;
872 /* Prevent overrunning the op->irqs[] array. */
873 if (op
->num_irqs
> PROMINTR_MAX
) {
874 printk(KERN_WARNING
"%s: Too many irqs (%d), "
876 dp
->full_name
, op
->num_irqs
, PROMINTR_MAX
);
877 op
->num_irqs
= PROMINTR_MAX
;
880 build_device_resources(op
, parent
);
881 for (i
= 0; i
< op
->num_irqs
; i
++)
882 op
->irqs
[i
] = build_one_device_irq(op
, parent
, op
->irqs
[i
]);
884 op
->dev
.parent
= parent
;
885 op
->dev
.bus
= &of_bus_type
;
887 strcpy(op
->dev
.bus_id
, "root");
889 sprintf(op
->dev
.bus_id
, "%08x", dp
->node
);
891 if (of_device_register(op
)) {
892 printk("%s: Could not register of device.\n",
901 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
904 struct of_device
*op
= scan_one_device(dp
, parent
);
907 scan_tree(dp
->child
, &op
->dev
);
913 static void __init
scan_of_devices(void)
915 struct device_node
*root
= of_find_node_by_path("/");
916 struct of_device
*parent
;
918 parent
= scan_one_device(root
, NULL
);
922 scan_tree(root
->child
, &parent
->dev
);
925 static int __init
of_bus_driver_init(void)
929 err
= bus_register(&of_bus_type
);
932 err
= bus_register(&isa_bus_type
);
934 err
= bus_register(&ebus_bus_type
);
938 err
= bus_register(&sbus_bus_type
);
947 postcore_initcall(of_bus_driver_init
);
949 static int __init
of_debug(char *str
)
953 get_option(&str
, &val
);
955 of_resource_verbose
= 1;
961 __setup("of_debug=", of_debug
);
963 int of_register_driver(struct of_platform_driver
*drv
, struct bus_type
*bus
)
965 /* initialize common driver fields */
966 drv
->driver
.name
= drv
->name
;
967 drv
->driver
.bus
= bus
;
969 /* register with core */
970 return driver_register(&drv
->driver
);
972 EXPORT_SYMBOL(of_register_driver
);
974 void of_unregister_driver(struct of_platform_driver
*drv
)
976 driver_unregister(&drv
->driver
);
978 EXPORT_SYMBOL(of_unregister_driver
);
980 struct of_device
* of_platform_device_create(struct device_node
*np
,
982 struct device
*parent
,
983 struct bus_type
*bus
)
985 struct of_device
*dev
;
987 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
991 dev
->dev
.parent
= parent
;
993 dev
->dev
.release
= of_release_dev
;
995 strlcpy(dev
->dev
.bus_id
, bus_id
, BUS_ID_SIZE
);
997 if (of_device_register(dev
) != 0) {
1004 EXPORT_SYMBOL(of_platform_device_create
);