1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/slab.h>
9 #include <asm/of_device.h>
12 * of_match_device - Tell if an of_device structure has a matching
14 * @ids: array of of device match structures to search in
15 * @dev: the of device structure to match against
17 * Used by a driver to check whether an of_device present in the
18 * system is in its list of supported devices.
20 const struct of_device_id
*of_match_device(const struct of_device_id
*matches
,
21 const struct of_device
*dev
)
25 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
28 match
&= dev
->node
->name
29 && !strcmp(matches
->name
, dev
->node
->name
);
31 match
&= dev
->node
->type
32 && !strcmp(matches
->type
, dev
->node
->type
);
33 if (matches
->compatible
[0])
34 match
&= of_device_is_compatible(dev
->node
,
43 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
45 struct of_device
* of_dev
= to_of_device(dev
);
46 struct of_platform_driver
* of_drv
= to_of_platform_driver(drv
);
47 const struct of_device_id
* matches
= of_drv
->match_table
;
52 return of_match_device(matches
, of_dev
) != NULL
;
55 struct of_device
*of_dev_get(struct of_device
*dev
)
61 tmp
= get_device(&dev
->dev
);
63 return to_of_device(tmp
);
68 void of_dev_put(struct of_device
*dev
)
71 put_device(&dev
->dev
);
75 static int of_device_probe(struct device
*dev
)
78 struct of_platform_driver
*drv
;
79 struct of_device
*of_dev
;
80 const struct of_device_id
*match
;
82 drv
= to_of_platform_driver(dev
->driver
);
83 of_dev
= to_of_device(dev
);
90 match
= of_match_device(drv
->match_table
, of_dev
);
92 error
= drv
->probe(of_dev
, match
);
99 static int of_device_remove(struct device
*dev
)
101 struct of_device
* of_dev
= to_of_device(dev
);
102 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
104 if (dev
->driver
&& drv
->remove
)
109 static int of_device_suspend(struct device
*dev
, pm_message_t state
)
111 struct of_device
* of_dev
= to_of_device(dev
);
112 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
115 if (dev
->driver
&& drv
->suspend
)
116 error
= drv
->suspend(of_dev
, state
);
120 static int of_device_resume(struct device
* dev
)
122 struct of_device
* of_dev
= to_of_device(dev
);
123 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
126 if (dev
->driver
&& drv
->resume
)
127 error
= drv
->resume(of_dev
);
131 static int node_match(struct device
*dev
, void *data
)
133 struct of_device
*op
= to_of_device(dev
);
134 struct device_node
*dp
= data
;
136 return (op
->node
== dp
);
139 struct of_device
*of_find_device_by_node(struct device_node
*dp
)
141 struct device
*dev
= bus_find_device(&of_bus_type
, NULL
,
145 return to_of_device(dev
);
149 EXPORT_SYMBOL(of_find_device_by_node
);
152 struct bus_type ebus_bus_type
= {
154 .match
= of_platform_bus_match
,
155 .probe
= of_device_probe
,
156 .remove
= of_device_remove
,
157 .suspend
= of_device_suspend
,
158 .resume
= of_device_resume
,
160 EXPORT_SYMBOL(ebus_bus_type
);
164 struct bus_type sbus_bus_type
= {
166 .match
= of_platform_bus_match
,
167 .probe
= of_device_probe
,
168 .remove
= of_device_remove
,
169 .suspend
= of_device_suspend
,
170 .resume
= of_device_resume
,
172 EXPORT_SYMBOL(sbus_bus_type
);
175 struct bus_type of_bus_type
= {
177 .match
= of_platform_bus_match
,
178 .probe
= of_device_probe
,
179 .remove
= of_device_remove
,
180 .suspend
= of_device_suspend
,
181 .resume
= of_device_resume
,
183 EXPORT_SYMBOL(of_bus_type
);
185 static inline u64
of_read_addr(const u32
*cell
, int size
)
189 r
= (r
<< 32) | *(cell
++);
193 static void __init
get_cells(struct device_node
*dp
,
194 int *addrc
, int *sizec
)
197 *addrc
= of_n_addr_cells(dp
);
199 *sizec
= of_n_size_cells(dp
);
202 /* Max address size we deal with */
203 #define OF_MAX_ADDR_CELLS 4
207 const char *addr_prop_name
;
208 int (*match
)(struct device_node
*parent
);
209 void (*count_cells
)(struct device_node
*child
,
210 int *addrc
, int *sizec
);
211 int (*map
)(u32
*addr
, const u32
*range
,
212 int na
, int ns
, int pna
);
213 unsigned int (*get_flags
)(const u32
*addr
);
217 * Default translator (generic bus)
220 static void of_bus_default_count_cells(struct device_node
*dev
,
221 int *addrc
, int *sizec
)
223 get_cells(dev
, addrc
, sizec
);
226 /* Make sure the least significant 64-bits are in-range. Even
227 * for 3 or 4 cell values it is a good enough approximation.
229 static int of_out_of_range(const u32
*addr
, const u32
*base
,
230 const u32
*size
, int na
, int ns
)
232 u64 a
= of_read_addr(addr
, na
);
233 u64 b
= of_read_addr(base
, na
);
238 b
+= of_read_addr(size
, ns
);
245 static int of_bus_default_map(u32
*addr
, const u32
*range
,
246 int na
, int ns
, int pna
)
248 u32 result
[OF_MAX_ADDR_CELLS
];
252 printk("of_device: Cannot handle size cells (%d) > 2.", ns
);
256 if (of_out_of_range(addr
, range
, range
+ na
+ pna
, na
, ns
))
259 /* Start with the parent range base. */
260 memcpy(result
, range
+ na
, pna
* 4);
262 /* Add in the child address offset. */
263 for (i
= 0; i
< na
; i
++)
264 result
[pna
- 1 - i
] +=
268 memcpy(addr
, result
, pna
* 4);
273 static unsigned int of_bus_default_get_flags(const u32
*addr
)
275 return IORESOURCE_MEM
;
279 * PCI bus specific translator
282 static int of_bus_pci_match(struct device_node
*np
)
284 if (!strcmp(np
->type
, "pci") || !strcmp(np
->type
, "pciex")) {
285 /* Do not do PCI specific frobbing if the
286 * PCI bridge lacks a ranges property. We
287 * want to pass it through up to the next
288 * parent as-is, not with the PCI translate
289 * method which chops off the top address cell.
291 if (!of_find_property(np
, "ranges", NULL
))
300 static void of_bus_pci_count_cells(struct device_node
*np
,
301 int *addrc
, int *sizec
)
309 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
310 int na
, int ns
, int pna
)
312 u32 result
[OF_MAX_ADDR_CELLS
];
315 /* Check address type match */
316 if ((addr
[0] ^ range
[0]) & 0x03000000)
319 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
323 /* Start with the parent range base. */
324 memcpy(result
, range
+ na
, pna
* 4);
326 /* Add in the child address offset, skipping high cell. */
327 for (i
= 0; i
< na
- 1; i
++)
328 result
[pna
- 1 - i
] +=
332 memcpy(addr
, result
, pna
* 4);
337 static unsigned int of_bus_pci_get_flags(const u32
*addr
)
339 unsigned int flags
= 0;
342 switch((w
>> 24) & 0x03) {
344 flags
|= IORESOURCE_IO
;
345 case 0x02: /* 32 bits */
346 case 0x03: /* 64 bits */
347 flags
|= IORESOURCE_MEM
;
350 flags
|= IORESOURCE_PREFETCH
;
355 * SBUS bus specific translator
358 static int of_bus_sbus_match(struct device_node
*np
)
360 return !strcmp(np
->name
, "sbus") ||
361 !strcmp(np
->name
, "sbi");
364 static void of_bus_sbus_count_cells(struct device_node
*child
,
365 int *addrc
, int *sizec
)
373 static int of_bus_sbus_map(u32
*addr
, const u32
*range
, int na
, int ns
, int pna
)
375 return of_bus_default_map(addr
, range
, na
, ns
, pna
);
378 static unsigned int of_bus_sbus_get_flags(const u32
*addr
)
380 return IORESOURCE_MEM
;
385 * Array of bus specific translators
388 static struct of_bus of_busses
[] = {
392 .addr_prop_name
= "assigned-addresses",
393 .match
= of_bus_pci_match
,
394 .count_cells
= of_bus_pci_count_cells
,
395 .map
= of_bus_pci_map
,
396 .get_flags
= of_bus_pci_get_flags
,
401 .addr_prop_name
= "reg",
402 .match
= of_bus_sbus_match
,
403 .count_cells
= of_bus_sbus_count_cells
,
404 .map
= of_bus_sbus_map
,
405 .get_flags
= of_bus_sbus_get_flags
,
410 .addr_prop_name
= "reg",
412 .count_cells
= of_bus_default_count_cells
,
413 .map
= of_bus_default_map
,
414 .get_flags
= of_bus_default_get_flags
,
418 static struct of_bus
*of_match_bus(struct device_node
*np
)
422 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
423 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
424 return &of_busses
[i
];
429 static int __init
build_one_resource(struct device_node
*parent
,
433 int na
, int ns
, int pna
)
439 ranges
= of_get_property(parent
, "ranges", &rlen
);
440 if (ranges
== NULL
|| rlen
== 0) {
441 u32 result
[OF_MAX_ADDR_CELLS
];
444 memset(result
, 0, pna
* 4);
445 for (i
= 0; i
< na
; i
++)
446 result
[pna
- 1 - i
] =
449 memcpy(addr
, result
, pna
* 4);
453 /* Now walk through the ranges */
455 rone
= na
+ pna
+ ns
;
456 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
457 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
464 static int of_resource_verbose
;
466 static void __init
build_device_resources(struct of_device
*op
,
467 struct device
*parent
)
469 struct of_device
*p_op
;
478 p_op
= to_of_device(parent
);
479 bus
= of_match_bus(p_op
->node
);
480 bus
->count_cells(op
->node
, &na
, &ns
);
482 preg
= of_get_property(op
->node
, bus
->addr_prop_name
, &num_reg
);
483 if (!preg
|| num_reg
== 0)
486 /* Convert to num-cells. */
489 /* Conver to num-entries. */
492 for (index
= 0; index
< num_reg
; index
++) {
493 struct resource
*r
= &op
->resource
[index
];
494 u32 addr
[OF_MAX_ADDR_CELLS
];
495 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
496 struct device_node
*dp
= op
->node
;
497 struct device_node
*pp
= p_op
->node
;
498 struct of_bus
*pbus
, *dbus
;
499 u64 size
, result
= OF_BAD_ADDR
;
504 size
= of_read_addr(reg
+ na
, ns
);
505 flags
= bus
->get_flags(reg
);
507 memcpy(addr
, reg
, na
* 4);
509 /* If the immediate parent has no ranges property to apply,
510 * just use a 1<->1 mapping.
512 if (of_find_property(pp
, "ranges", NULL
) == NULL
) {
513 result
= of_read_addr(addr
, na
);
525 result
= of_read_addr(addr
, dna
);
529 pbus
= of_match_bus(pp
);
530 pbus
->count_cells(dp
, &pna
, &pns
);
532 if (build_one_resource(dp
, dbus
, pbus
, addr
,
542 memset(r
, 0, sizeof(*r
));
544 if (of_resource_verbose
)
545 printk("%s reg[%d] -> %llx\n",
546 op
->node
->full_name
, index
,
549 if (result
!= OF_BAD_ADDR
) {
550 r
->start
= result
& 0xffffffff;
551 r
->end
= result
+ size
- 1;
552 r
->flags
= flags
| ((result
>> 32ULL) & 0xffUL
);
554 r
->name
= op
->node
->name
;
558 static struct of_device
* __init
scan_one_device(struct device_node
*dp
,
559 struct device
*parent
)
561 struct of_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
562 const struct linux_prom_irqs
*intr
;
570 op
->clock_freq
= of_getintprop_default(dp
, "clock-frequency",
572 op
->portid
= of_getintprop_default(dp
, "upa-portid", -1);
573 if (op
->portid
== -1)
574 op
->portid
= of_getintprop_default(dp
, "portid", -1);
576 intr
= of_get_property(dp
, "intr", &len
);
578 op
->num_irqs
= len
/ sizeof(struct linux_prom_irqs
);
579 for (i
= 0; i
< op
->num_irqs
; i
++)
580 op
->irqs
[i
] = intr
[i
].pri
;
582 const unsigned int *irq
=
583 of_get_property(dp
, "interrupts", &len
);
586 op
->num_irqs
= len
/ sizeof(unsigned int);
587 for (i
= 0; i
< op
->num_irqs
; i
++)
588 op
->irqs
[i
] = irq
[i
];
593 if (sparc_cpu_model
== sun4d
) {
594 static int pil_to_sbus
[] = {
595 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
597 struct device_node
*io_unit
, *sbi
= dp
->parent
;
598 const struct linux_prom_registers
*regs
;
602 if (!strcmp(sbi
->name
, "sbi"))
608 goto build_resources
;
610 regs
= of_get_property(dp
, "reg", NULL
);
612 goto build_resources
;
614 slot
= regs
->which_io
;
616 /* If SBI's parent is not io-unit or the io-unit lacks
617 * a "board#" property, something is very wrong.
619 if (!sbi
->parent
|| strcmp(sbi
->parent
->name
, "io-unit")) {
620 printk("%s: Error, parent is not io-unit.\n",
622 goto build_resources
;
624 io_unit
= sbi
->parent
;
625 board
= of_getintprop_default(io_unit
, "board#", -1);
627 printk("%s: Error, lacks board# property.\n",
629 goto build_resources
;
632 for (i
= 0; i
< op
->num_irqs
; i
++) {
633 int this_irq
= op
->irqs
[i
];
634 int sbusl
= pil_to_sbus
[this_irq
];
637 this_irq
= (((board
+ 1) << 5) +
641 op
->irqs
[i
] = this_irq
;
646 build_device_resources(op
, parent
);
648 op
->dev
.parent
= parent
;
649 op
->dev
.bus
= &of_bus_type
;
651 strcpy(op
->dev
.bus_id
, "root");
653 sprintf(op
->dev
.bus_id
, "%08x", dp
->node
);
655 if (of_device_register(op
)) {
656 printk("%s: Could not register of device.\n",
665 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
668 struct of_device
*op
= scan_one_device(dp
, parent
);
671 scan_tree(dp
->child
, &op
->dev
);
677 static void __init
scan_of_devices(void)
679 struct device_node
*root
= of_find_node_by_path("/");
680 struct of_device
*parent
;
682 parent
= scan_one_device(root
, NULL
);
686 scan_tree(root
->child
, &parent
->dev
);
689 static int __init
of_bus_driver_init(void)
693 err
= bus_register(&of_bus_type
);
696 err
= bus_register(&ebus_bus_type
);
700 err
= bus_register(&sbus_bus_type
);
709 postcore_initcall(of_bus_driver_init
);
711 static int __init
of_debug(char *str
)
715 get_option(&str
, &val
);
717 of_resource_verbose
= 1;
721 __setup("of_debug=", of_debug
);
723 int of_register_driver(struct of_platform_driver
*drv
, struct bus_type
*bus
)
725 /* initialize common driver fields */
726 drv
->driver
.name
= drv
->name
;
727 drv
->driver
.bus
= bus
;
729 /* register with core */
730 return driver_register(&drv
->driver
);
733 void of_unregister_driver(struct of_platform_driver
*drv
)
735 driver_unregister(&drv
->driver
);
739 static ssize_t
dev_show_devspec(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
741 struct of_device
*ofdev
;
743 ofdev
= to_of_device(dev
);
744 return sprintf(buf
, "%s", ofdev
->node
->full_name
);
747 static DEVICE_ATTR(devspec
, S_IRUGO
, dev_show_devspec
, NULL
);
750 * of_release_dev - free an of device structure when all users of it are finished.
751 * @dev: device that's been disconnected
753 * Will be called only by the device core when all users of this of device are
756 void of_release_dev(struct device
*dev
)
758 struct of_device
*ofdev
;
760 ofdev
= to_of_device(dev
);
765 int of_device_register(struct of_device
*ofdev
)
769 BUG_ON(ofdev
->node
== NULL
);
771 rc
= device_register(&ofdev
->dev
);
775 rc
= device_create_file(&ofdev
->dev
, &dev_attr_devspec
);
777 device_unregister(&ofdev
->dev
);
782 void of_device_unregister(struct of_device
*ofdev
)
784 device_remove_file(&ofdev
->dev
, &dev_attr_devspec
);
785 device_unregister(&ofdev
->dev
);
788 struct of_device
* of_platform_device_create(struct device_node
*np
,
790 struct device
*parent
,
791 struct bus_type
*bus
)
793 struct of_device
*dev
;
795 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
799 dev
->dev
.parent
= parent
;
801 dev
->dev
.release
= of_release_dev
;
803 strlcpy(dev
->dev
.bus_id
, bus_id
, BUS_ID_SIZE
);
805 if (of_device_register(dev
) != 0) {
813 EXPORT_SYMBOL(of_match_device
);
814 EXPORT_SYMBOL(of_register_driver
);
815 EXPORT_SYMBOL(of_unregister_driver
);
816 EXPORT_SYMBOL(of_device_register
);
817 EXPORT_SYMBOL(of_device_unregister
);
818 EXPORT_SYMBOL(of_dev_get
);
819 EXPORT_SYMBOL(of_dev_put
);
820 EXPORT_SYMBOL(of_platform_device_create
);
821 EXPORT_SYMBOL(of_release_dev
);