1 #include <linux/config.h>
2 #include <linux/string.h>
3 #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>
13 * of_match_device - Tell if an of_device structure has a matching
15 * @ids: array of of device match structures to search in
16 * @dev: the of device structure to match against
18 * Used by a driver to check whether an of_device present in the
19 * system is in its list of supported devices.
21 const struct of_device_id
*of_match_device(const struct of_device_id
*matches
,
22 const struct of_device
*dev
)
26 while (matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]) {
29 match
&= dev
->node
->name
30 && !strcmp(matches
->name
, dev
->node
->name
);
32 match
&= dev
->node
->type
33 && !strcmp(matches
->type
, dev
->node
->type
);
34 if (matches
->compatible
[0])
35 match
&= of_device_is_compatible(dev
->node
,
44 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
46 struct of_device
* of_dev
= to_of_device(dev
);
47 struct of_platform_driver
* of_drv
= to_of_platform_driver(drv
);
48 const struct of_device_id
* matches
= of_drv
->match_table
;
53 return of_match_device(matches
, of_dev
) != NULL
;
56 struct of_device
*of_dev_get(struct of_device
*dev
)
62 tmp
= get_device(&dev
->dev
);
64 return to_of_device(tmp
);
69 void of_dev_put(struct of_device
*dev
)
72 put_device(&dev
->dev
);
76 static int of_device_probe(struct device
*dev
)
79 struct of_platform_driver
*drv
;
80 struct of_device
*of_dev
;
81 const struct of_device_id
*match
;
83 drv
= to_of_platform_driver(dev
->driver
);
84 of_dev
= to_of_device(dev
);
91 match
= of_match_device(drv
->match_table
, of_dev
);
93 error
= drv
->probe(of_dev
, match
);
100 static int of_device_remove(struct device
*dev
)
102 struct of_device
* of_dev
= to_of_device(dev
);
103 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
105 if (dev
->driver
&& drv
->remove
)
110 static int of_device_suspend(struct device
*dev
, pm_message_t state
)
112 struct of_device
* of_dev
= to_of_device(dev
);
113 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
116 if (dev
->driver
&& drv
->suspend
)
117 error
= drv
->suspend(of_dev
, state
);
121 static int of_device_resume(struct device
* dev
)
123 struct of_device
* of_dev
= to_of_device(dev
);
124 struct of_platform_driver
* drv
= to_of_platform_driver(dev
->driver
);
127 if (dev
->driver
&& drv
->resume
)
128 error
= drv
->resume(of_dev
);
132 static int node_match(struct device
*dev
, void *data
)
134 struct of_device
*op
= to_of_device(dev
);
135 struct device_node
*dp
= data
;
137 return (op
->node
== dp
);
140 struct of_device
*of_find_device_by_node(struct device_node
*dp
)
142 struct device
*dev
= bus_find_device(&of_bus_type
, NULL
,
146 return to_of_device(dev
);
150 EXPORT_SYMBOL(of_find_device_by_node
);
153 struct bus_type ebus_bus_type
= {
155 .match
= of_platform_bus_match
,
156 .probe
= of_device_probe
,
157 .remove
= of_device_remove
,
158 .suspend
= of_device_suspend
,
159 .resume
= of_device_resume
,
161 EXPORT_SYMBOL(ebus_bus_type
);
165 struct bus_type sbus_bus_type
= {
167 .match
= of_platform_bus_match
,
168 .probe
= of_device_probe
,
169 .remove
= of_device_remove
,
170 .suspend
= of_device_suspend
,
171 .resume
= of_device_resume
,
173 EXPORT_SYMBOL(sbus_bus_type
);
176 struct bus_type of_bus_type
= {
178 .match
= of_platform_bus_match
,
179 .probe
= of_device_probe
,
180 .remove
= of_device_remove
,
181 .suspend
= of_device_suspend
,
182 .resume
= of_device_resume
,
184 EXPORT_SYMBOL(of_bus_type
);
186 static inline u64
of_read_addr(const u32
*cell
, int size
)
190 r
= (r
<< 32) | *(cell
++);
194 static void __init
get_cells(struct device_node
*dp
,
195 int *addrc
, int *sizec
)
198 *addrc
= of_n_addr_cells(dp
);
200 *sizec
= of_n_size_cells(dp
);
203 /* Max address size we deal with */
204 #define OF_MAX_ADDR_CELLS 4
208 const char *addr_prop_name
;
209 int (*match
)(struct device_node
*parent
);
210 void (*count_cells
)(struct device_node
*child
,
211 int *addrc
, int *sizec
);
212 int (*map
)(u32
*addr
, const u32
*range
,
213 int na
, int ns
, int pna
);
214 unsigned int (*get_flags
)(u32
*addr
);
218 * Default translator (generic bus)
221 static void of_bus_default_count_cells(struct device_node
*dev
,
222 int *addrc
, int *sizec
)
224 get_cells(dev
, addrc
, sizec
);
227 /* Make sure the least significant 64-bits are in-range. Even
228 * for 3 or 4 cell values it is a good enough approximation.
230 static int of_out_of_range(const u32
*addr
, const u32
*base
,
231 const u32
*size
, int na
, int ns
)
233 u64 a
= of_read_addr(addr
, na
);
234 u64 b
= of_read_addr(base
, na
);
239 b
+= of_read_addr(size
, ns
);
246 static int of_bus_default_map(u32
*addr
, const u32
*range
,
247 int na
, int ns
, int pna
)
249 u32 result
[OF_MAX_ADDR_CELLS
];
253 printk("of_device: Cannot handle size cells (%d) > 2.", ns
);
257 if (of_out_of_range(addr
, range
, range
+ na
+ pna
, na
, ns
))
260 /* Start with the parent range base. */
261 memcpy(result
, range
+ na
, pna
* 4);
263 /* Add in the child address offset. */
264 for (i
= 0; i
< na
; i
++)
265 result
[pna
- 1 - i
] +=
269 memcpy(addr
, result
, pna
* 4);
274 static unsigned int of_bus_default_get_flags(u32
*addr
)
276 return IORESOURCE_MEM
;
280 * PCI bus specific translator
283 static int of_bus_pci_match(struct device_node
*np
)
285 if (!strcmp(np
->type
, "pci") || !strcmp(np
->type
, "pciex")) {
286 /* Do not do PCI specific frobbing if the
287 * PCI bridge lacks a ranges property. We
288 * want to pass it through up to the next
289 * parent as-is, not with the PCI translate
290 * method which chops off the top address cell.
292 if (!of_find_property(np
, "ranges", NULL
))
301 static void of_bus_pci_count_cells(struct device_node
*np
,
302 int *addrc
, int *sizec
)
310 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
311 int na
, int ns
, int pna
)
313 u32 result
[OF_MAX_ADDR_CELLS
];
316 /* Check address type match */
317 if ((addr
[0] ^ range
[0]) & 0x03000000)
320 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
324 /* Start with the parent range base. */
325 memcpy(result
, range
+ na
, pna
* 4);
327 /* Add in the child address offset, skipping high cell. */
328 for (i
= 0; i
< na
- 1; i
++)
329 result
[pna
- 1 - i
] +=
333 memcpy(addr
, result
, pna
* 4);
338 static unsigned int of_bus_pci_get_flags(u32
*addr
)
340 unsigned int flags
= 0;
343 switch((w
>> 24) & 0x03) {
345 flags
|= IORESOURCE_IO
;
346 case 0x02: /* 32 bits */
347 case 0x03: /* 64 bits */
348 flags
|= IORESOURCE_MEM
;
351 flags
|= IORESOURCE_PREFETCH
;
356 * SBUS bus specific translator
359 static int of_bus_sbus_match(struct device_node
*np
)
361 return !strcmp(np
->name
, "sbus") ||
362 !strcmp(np
->name
, "sbi");
365 static void of_bus_sbus_count_cells(struct device_node
*child
,
366 int *addrc
, int *sizec
)
374 static int of_bus_sbus_map(u32
*addr
, const u32
*range
, int na
, int ns
, int pna
)
376 return of_bus_default_map(addr
, range
, na
, ns
, pna
);
379 static unsigned int of_bus_sbus_get_flags(u32
*addr
)
381 return IORESOURCE_MEM
;
386 * Array of bus specific translators
389 static struct of_bus of_busses
[] = {
393 .addr_prop_name
= "assigned-addresses",
394 .match
= of_bus_pci_match
,
395 .count_cells
= of_bus_pci_count_cells
,
396 .map
= of_bus_pci_map
,
397 .get_flags
= of_bus_pci_get_flags
,
402 .addr_prop_name
= "reg",
403 .match
= of_bus_sbus_match
,
404 .count_cells
= of_bus_sbus_count_cells
,
405 .map
= of_bus_sbus_map
,
406 .get_flags
= of_bus_sbus_get_flags
,
411 .addr_prop_name
= "reg",
413 .count_cells
= of_bus_default_count_cells
,
414 .map
= of_bus_default_map
,
415 .get_flags
= of_bus_default_get_flags
,
419 static struct of_bus
*of_match_bus(struct device_node
*np
)
423 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
424 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
425 return &of_busses
[i
];
430 static int __init
build_one_resource(struct device_node
*parent
,
434 int na
, int ns
, int pna
)
440 ranges
= of_get_property(parent
, "ranges", &rlen
);
441 if (ranges
== NULL
|| rlen
== 0) {
442 u32 result
[OF_MAX_ADDR_CELLS
];
445 memset(result
, 0, pna
* 4);
446 for (i
= 0; i
< na
; i
++)
447 result
[pna
- 1 - i
] =
450 memcpy(addr
, result
, pna
* 4);
454 /* Now walk through the ranges */
456 rone
= na
+ pna
+ ns
;
457 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
458 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
465 static int of_resource_verbose
;
467 static void __init
build_device_resources(struct of_device
*op
,
468 struct device
*parent
)
470 struct of_device
*p_op
;
479 p_op
= to_of_device(parent
);
480 bus
= of_match_bus(p_op
->node
);
481 bus
->count_cells(op
->node
, &na
, &ns
);
483 preg
= of_get_property(op
->node
, bus
->addr_prop_name
, &num_reg
);
484 if (!preg
|| num_reg
== 0)
487 /* Convert to num-cells. */
490 /* Conver to num-entries. */
493 for (index
= 0; index
< num_reg
; index
++) {
494 struct resource
*r
= &op
->resource
[index
];
495 u32 addr
[OF_MAX_ADDR_CELLS
];
496 u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
497 struct device_node
*dp
= op
->node
;
498 struct device_node
*pp
= p_op
->node
;
500 u64 size
, result
= OF_BAD_ADDR
;
505 size
= of_read_addr(reg
+ na
, ns
);
506 flags
= bus
->get_flags(reg
);
508 memcpy(addr
, reg
, na
* 4);
510 /* If the immediate parent has no ranges property to apply,
511 * just use a 1<->1 mapping.
513 if (of_find_property(pp
, "ranges", NULL
) == NULL
) {
514 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
, bus
, 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
);
557 r
->name
= op
->node
->name
;
561 static struct of_device
* __init
scan_one_device(struct device_node
*dp
,
562 struct device
*parent
)
564 struct of_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
565 struct linux_prom_irqs
*intr
;
573 op
->clock_freq
= of_getintprop_default(dp
, "clock-frequency",
575 op
->portid
= of_getintprop_default(dp
, "upa-portid", -1);
576 if (op
->portid
== -1)
577 op
->portid
= of_getintprop_default(dp
, "portid", -1);
579 intr
= of_get_property(dp
, "intr", &len
);
581 op
->num_irqs
= len
/ sizeof(struct linux_prom_irqs
);
582 for (i
= 0; i
< op
->num_irqs
; i
++)
583 op
->irqs
[i
] = intr
[i
].pri
;
585 unsigned int *irq
= of_get_property(dp
, "interrupts", &len
);
588 op
->num_irqs
= len
/ sizeof(unsigned int);
589 for (i
= 0; i
< op
->num_irqs
; i
++)
590 op
->irqs
[i
] = irq
[i
];
595 if (sparc_cpu_model
== sun4d
) {
596 static int pil_to_sbus
[] = {
597 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
599 struct device_node
*io_unit
, *sbi
= dp
->parent
;
600 struct linux_prom_registers
*regs
;
604 if (!strcmp(sbi
->name
, "sbi"))
610 goto build_resources
;
612 regs
= of_get_property(dp
, "reg", NULL
);
614 goto build_resources
;
616 slot
= regs
->which_io
;
618 /* If SBI's parent is not io-unit or the io-unit lacks
619 * a "board#" property, something is very wrong.
621 if (!sbi
->parent
|| strcmp(sbi
->parent
->name
, "io-unit")) {
622 printk("%s: Error, parent is not io-unit.\n",
624 goto build_resources
;
626 io_unit
= sbi
->parent
;
627 board
= of_getintprop_default(io_unit
, "board#", -1);
629 printk("%s: Error, lacks board# property.\n",
631 goto build_resources
;
634 for (i
= 0; i
< op
->num_irqs
; i
++) {
635 int this_irq
= op
->irqs
[i
];
636 int sbusl
= pil_to_sbus
[this_irq
];
639 this_irq
= (((board
+ 1) << 5) +
643 op
->irqs
[i
] = this_irq
;
648 build_device_resources(op
, parent
);
650 op
->dev
.parent
= parent
;
651 op
->dev
.bus
= &of_bus_type
;
653 strcpy(op
->dev
.bus_id
, "root");
655 strcpy(op
->dev
.bus_id
, dp
->path_component_name
);
657 if (of_device_register(op
)) {
658 printk("%s: Could not register of device.\n",
667 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
670 struct of_device
*op
= scan_one_device(dp
, parent
);
673 scan_tree(dp
->child
, &op
->dev
);
679 static void __init
scan_of_devices(void)
681 struct device_node
*root
= of_find_node_by_path("/");
682 struct of_device
*parent
;
684 parent
= scan_one_device(root
, NULL
);
688 scan_tree(root
->child
, &parent
->dev
);
691 static int __init
of_bus_driver_init(void)
695 err
= bus_register(&of_bus_type
);
698 err
= bus_register(&ebus_bus_type
);
702 err
= bus_register(&sbus_bus_type
);
711 postcore_initcall(of_bus_driver_init
);
713 static int __init
of_debug(char *str
)
717 get_option(&str
, &val
);
719 of_resource_verbose
= 1;
723 __setup("of_debug=", of_debug
);
725 int of_register_driver(struct of_platform_driver
*drv
, struct bus_type
*bus
)
727 /* initialize common driver fields */
728 drv
->driver
.name
= drv
->name
;
729 drv
->driver
.bus
= bus
;
731 /* register with core */
732 return driver_register(&drv
->driver
);
735 void of_unregister_driver(struct of_platform_driver
*drv
)
737 driver_unregister(&drv
->driver
);
741 static ssize_t
dev_show_devspec(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
743 struct of_device
*ofdev
;
745 ofdev
= to_of_device(dev
);
746 return sprintf(buf
, "%s", ofdev
->node
->full_name
);
749 static DEVICE_ATTR(devspec
, S_IRUGO
, dev_show_devspec
, NULL
);
752 * of_release_dev - free an of device structure when all users of it are finished.
753 * @dev: device that's been disconnected
755 * Will be called only by the device core when all users of this of device are
758 void of_release_dev(struct device
*dev
)
760 struct of_device
*ofdev
;
762 ofdev
= to_of_device(dev
);
767 int of_device_register(struct of_device
*ofdev
)
771 BUG_ON(ofdev
->node
== NULL
);
773 rc
= device_register(&ofdev
->dev
);
777 rc
= device_create_file(&ofdev
->dev
, &dev_attr_devspec
);
779 device_unregister(&ofdev
->dev
);
784 void of_device_unregister(struct of_device
*ofdev
)
786 device_remove_file(&ofdev
->dev
, &dev_attr_devspec
);
787 device_unregister(&ofdev
->dev
);
790 struct of_device
* of_platform_device_create(struct device_node
*np
,
792 struct device
*parent
,
793 struct bus_type
*bus
)
795 struct of_device
*dev
;
797 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
800 memset(dev
, 0, sizeof(*dev
));
802 dev
->dev
.parent
= parent
;
804 dev
->dev
.release
= of_release_dev
;
806 strlcpy(dev
->dev
.bus_id
, bus_id
, BUS_ID_SIZE
);
808 if (of_device_register(dev
) != 0) {
816 EXPORT_SYMBOL(of_match_device
);
817 EXPORT_SYMBOL(of_register_driver
);
818 EXPORT_SYMBOL(of_unregister_driver
);
819 EXPORT_SYMBOL(of_device_register
);
820 EXPORT_SYMBOL(of_device_unregister
);
821 EXPORT_SYMBOL(of_dev_get
);
822 EXPORT_SYMBOL(of_dev_put
);
823 EXPORT_SYMBOL(of_platform_device_create
);
824 EXPORT_SYMBOL(of_release_dev
);