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>
8 #include <linux/errno.h>
9 #include <linux/of_device.h>
10 #include <linux/of_platform.h>
12 static int node_match(struct device
*dev
, void *data
)
14 struct of_device
*op
= to_of_device(dev
);
15 struct device_node
*dp
= data
;
17 return (op
->node
== dp
);
20 struct of_device
*of_find_device_by_node(struct device_node
*dp
)
22 struct device
*dev
= bus_find_device(&of_platform_bus_type
, NULL
,
26 return to_of_device(dev
);
30 EXPORT_SYMBOL(of_find_device_by_node
);
32 unsigned int irq_of_parse_and_map(struct device_node
*node
, int index
)
34 struct of_device
*op
= of_find_device_by_node(node
);
36 if (!op
|| index
>= op
->num_irqs
)
39 return op
->irqs
[index
];
41 EXPORT_SYMBOL(irq_of_parse_and_map
);
43 /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
44 * BUS and propagate to all child of_device objects.
46 void of_propagate_archdata(struct of_device
*bus
)
48 struct dev_archdata
*bus_sd
= &bus
->dev
.archdata
;
49 struct device_node
*bus_dp
= bus
->node
;
50 struct device_node
*dp
;
52 for (dp
= bus_dp
->child
; dp
; dp
= dp
->sibling
) {
53 struct of_device
*op
= of_find_device_by_node(dp
);
55 op
->dev
.archdata
.iommu
= bus_sd
->iommu
;
56 op
->dev
.archdata
.stc
= bus_sd
->stc
;
57 op
->dev
.archdata
.host_controller
= bus_sd
->host_controller
;
58 op
->dev
.archdata
.numa_node
= bus_sd
->numa_node
;
61 of_propagate_archdata(op
);
65 struct bus_type of_platform_bus_type
;
66 EXPORT_SYMBOL(of_platform_bus_type
);
68 static inline u64
of_read_addr(const u32
*cell
, int size
)
72 r
= (r
<< 32) | *(cell
++);
76 static void __init
get_cells(struct device_node
*dp
,
77 int *addrc
, int *sizec
)
80 *addrc
= of_n_addr_cells(dp
);
82 *sizec
= of_n_size_cells(dp
);
85 /* Max address size we deal with */
86 #define OF_MAX_ADDR_CELLS 4
90 const char *addr_prop_name
;
91 int (*match
)(struct device_node
*parent
);
92 void (*count_cells
)(struct device_node
*child
,
93 int *addrc
, int *sizec
);
94 int (*map
)(u32
*addr
, const u32
*range
,
95 int na
, int ns
, int pna
);
96 unsigned long (*get_flags
)(const u32
*addr
, unsigned long);
100 * Default translator (generic bus)
103 static void of_bus_default_count_cells(struct device_node
*dev
,
104 int *addrc
, int *sizec
)
106 get_cells(dev
, addrc
, sizec
);
109 /* Make sure the least significant 64-bits are in-range. Even
110 * for 3 or 4 cell values it is a good enough approximation.
112 static int of_out_of_range(const u32
*addr
, const u32
*base
,
113 const u32
*size
, int na
, int ns
)
115 u64 a
= of_read_addr(addr
, na
);
116 u64 b
= of_read_addr(base
, na
);
121 b
+= of_read_addr(size
, ns
);
128 static int of_bus_default_map(u32
*addr
, const u32
*range
,
129 int na
, int ns
, int pna
)
131 u32 result
[OF_MAX_ADDR_CELLS
];
135 printk("of_device: Cannot handle size cells (%d) > 2.", ns
);
139 if (of_out_of_range(addr
, range
, range
+ na
+ pna
, na
, ns
))
142 /* Start with the parent range base. */
143 memcpy(result
, range
+ na
, pna
* 4);
145 /* Add in the child address offset. */
146 for (i
= 0; i
< na
; i
++)
147 result
[pna
- 1 - i
] +=
151 memcpy(addr
, result
, pna
* 4);
156 static unsigned long of_bus_default_get_flags(const u32
*addr
, unsigned long flags
)
160 return IORESOURCE_MEM
;
164 * PCI bus specific translator
167 static int of_bus_pci_match(struct device_node
*np
)
169 if (!strcmp(np
->type
, "pci") || !strcmp(np
->type
, "pciex")) {
170 /* Do not do PCI specific frobbing if the
171 * PCI bridge lacks a ranges property. We
172 * want to pass it through up to the next
173 * parent as-is, not with the PCI translate
174 * method which chops off the top address cell.
176 if (!of_find_property(np
, "ranges", NULL
))
185 static void of_bus_pci_count_cells(struct device_node
*np
,
186 int *addrc
, int *sizec
)
194 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
195 int na
, int ns
, int pna
)
197 u32 result
[OF_MAX_ADDR_CELLS
];
200 /* Check address type match */
201 if ((addr
[0] ^ range
[0]) & 0x03000000)
204 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
208 /* Start with the parent range base. */
209 memcpy(result
, range
+ na
, pna
* 4);
211 /* Add in the child address offset, skipping high cell. */
212 for (i
= 0; i
< na
- 1; i
++)
213 result
[pna
- 1 - i
] +=
217 memcpy(addr
, result
, pna
* 4);
222 static unsigned long of_bus_pci_get_flags(const u32
*addr
, unsigned long flags
)
226 /* For PCI, we override whatever child busses may have used. */
228 switch((w
>> 24) & 0x03) {
230 flags
|= IORESOURCE_IO
;
233 case 0x02: /* 32 bits */
234 case 0x03: /* 64 bits */
235 flags
|= IORESOURCE_MEM
;
239 flags
|= IORESOURCE_PREFETCH
;
244 * SBUS bus specific translator
247 static int of_bus_sbus_match(struct device_node
*np
)
249 return !strcmp(np
->name
, "sbus") ||
250 !strcmp(np
->name
, "sbi");
253 static void of_bus_sbus_count_cells(struct device_node
*child
,
254 int *addrc
, int *sizec
)
262 static int of_bus_sbus_map(u32
*addr
, const u32
*range
, int na
, int ns
, int pna
)
264 return of_bus_default_map(addr
, range
, na
, ns
, pna
);
267 static unsigned long of_bus_sbus_get_flags(const u32
*addr
, unsigned long flags
)
269 return IORESOURCE_MEM
;
274 * Array of bus specific translators
277 static struct of_bus of_busses
[] = {
281 .addr_prop_name
= "assigned-addresses",
282 .match
= of_bus_pci_match
,
283 .count_cells
= of_bus_pci_count_cells
,
284 .map
= of_bus_pci_map
,
285 .get_flags
= of_bus_pci_get_flags
,
290 .addr_prop_name
= "reg",
291 .match
= of_bus_sbus_match
,
292 .count_cells
= of_bus_sbus_count_cells
,
293 .map
= of_bus_sbus_map
,
294 .get_flags
= of_bus_sbus_get_flags
,
299 .addr_prop_name
= "reg",
301 .count_cells
= of_bus_default_count_cells
,
302 .map
= of_bus_default_map
,
303 .get_flags
= of_bus_default_get_flags
,
307 static struct of_bus
*of_match_bus(struct device_node
*np
)
311 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
312 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
313 return &of_busses
[i
];
318 static int __init
build_one_resource(struct device_node
*parent
,
322 int na
, int ns
, int pna
)
328 ranges
= of_get_property(parent
, "ranges", &rlen
);
329 if (ranges
== NULL
|| rlen
== 0) {
330 u32 result
[OF_MAX_ADDR_CELLS
];
333 memset(result
, 0, pna
* 4);
334 for (i
= 0; i
< na
; i
++)
335 result
[pna
- 1 - i
] =
338 memcpy(addr
, result
, pna
* 4);
342 /* Now walk through the ranges */
344 rone
= na
+ pna
+ ns
;
345 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
346 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
353 static int __init
use_1to1_mapping(struct device_node
*pp
)
355 /* If we have a ranges property in the parent, use it. */
356 if (of_find_property(pp
, "ranges", NULL
) != NULL
)
359 /* Some SBUS devices use intermediate nodes to express
360 * hierarchy within the device itself. These aren't
361 * real bus nodes, and don't have a 'ranges' property.
362 * But, we should still pass the translation work up
363 * to the SBUS itself.
365 if (!strcmp(pp
->name
, "dma") ||
366 !strcmp(pp
->name
, "espdma") ||
367 !strcmp(pp
->name
, "ledma") ||
368 !strcmp(pp
->name
, "lebuffer"))
374 static int of_resource_verbose
;
376 static void __init
build_device_resources(struct of_device
*op
,
377 struct device
*parent
)
379 struct of_device
*p_op
;
388 p_op
= to_of_device(parent
);
389 bus
= of_match_bus(p_op
->node
);
390 bus
->count_cells(op
->node
, &na
, &ns
);
392 preg
= of_get_property(op
->node
, bus
->addr_prop_name
, &num_reg
);
393 if (!preg
|| num_reg
== 0)
396 /* Convert to num-cells. */
399 /* Conver to num-entries. */
402 for (index
= 0; index
< num_reg
; index
++) {
403 struct resource
*r
= &op
->resource
[index
];
404 u32 addr
[OF_MAX_ADDR_CELLS
];
405 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
406 struct device_node
*dp
= op
->node
;
407 struct device_node
*pp
= p_op
->node
;
408 struct of_bus
*pbus
, *dbus
;
409 u64 size
, result
= OF_BAD_ADDR
;
414 size
= of_read_addr(reg
+ na
, ns
);
416 memcpy(addr
, reg
, na
* 4);
418 flags
= bus
->get_flags(reg
, 0);
420 if (use_1to1_mapping(pp
)) {
421 result
= of_read_addr(addr
, na
);
433 result
= of_read_addr(addr
, dna
);
437 pbus
= of_match_bus(pp
);
438 pbus
->count_cells(dp
, &pna
, &pns
);
440 if (build_one_resource(dp
, dbus
, pbus
, addr
,
444 flags
= pbus
->get_flags(addr
, flags
);
452 memset(r
, 0, sizeof(*r
));
454 if (of_resource_verbose
)
455 printk("%s reg[%d] -> %llx\n",
456 op
->node
->full_name
, index
,
459 if (result
!= OF_BAD_ADDR
) {
460 r
->start
= result
& 0xffffffff;
461 r
->end
= result
+ size
- 1;
462 r
->flags
= flags
| ((result
>> 32ULL) & 0xffUL
);
464 r
->name
= op
->node
->name
;
468 static struct of_device
* __init
scan_one_device(struct device_node
*dp
,
469 struct device
*parent
)
471 struct of_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
472 const struct linux_prom_irqs
*intr
;
473 struct dev_archdata
*sd
;
479 sd
= &op
->dev
.archdata
;
485 op
->clock_freq
= of_getintprop_default(dp
, "clock-frequency",
487 op
->portid
= of_getintprop_default(dp
, "upa-portid", -1);
488 if (op
->portid
== -1)
489 op
->portid
= of_getintprop_default(dp
, "portid", -1);
491 intr
= of_get_property(dp
, "intr", &len
);
493 op
->num_irqs
= len
/ sizeof(struct linux_prom_irqs
);
494 for (i
= 0; i
< op
->num_irqs
; i
++)
495 op
->irqs
[i
] = intr
[i
].pri
;
497 const unsigned int *irq
=
498 of_get_property(dp
, "interrupts", &len
);
501 op
->num_irqs
= len
/ sizeof(unsigned int);
502 for (i
= 0; i
< op
->num_irqs
; i
++)
503 op
->irqs
[i
] = irq
[i
];
508 if (sparc_cpu_model
== sun4d
) {
509 static int pil_to_sbus
[] = {
510 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
512 struct device_node
*io_unit
, *sbi
= dp
->parent
;
513 const struct linux_prom_registers
*regs
;
517 if (!strcmp(sbi
->name
, "sbi"))
523 goto build_resources
;
525 regs
= of_get_property(dp
, "reg", NULL
);
527 goto build_resources
;
529 slot
= regs
->which_io
;
531 /* If SBI's parent is not io-unit or the io-unit lacks
532 * a "board#" property, something is very wrong.
534 if (!sbi
->parent
|| strcmp(sbi
->parent
->name
, "io-unit")) {
535 printk("%s: Error, parent is not io-unit.\n",
537 goto build_resources
;
539 io_unit
= sbi
->parent
;
540 board
= of_getintprop_default(io_unit
, "board#", -1);
542 printk("%s: Error, lacks board# property.\n",
544 goto build_resources
;
547 for (i
= 0; i
< op
->num_irqs
; i
++) {
548 int this_irq
= op
->irqs
[i
];
549 int sbusl
= pil_to_sbus
[this_irq
];
552 this_irq
= (((board
+ 1) << 5) +
556 op
->irqs
[i
] = this_irq
;
561 build_device_resources(op
, parent
);
563 op
->dev
.parent
= parent
;
564 op
->dev
.bus
= &of_platform_bus_type
;
566 dev_set_name(&op
->dev
, "root");
568 dev_set_name(&op
->dev
, "%08x", dp
->node
);
570 if (of_device_register(op
)) {
571 printk("%s: Could not register of device.\n",
580 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
583 struct of_device
*op
= scan_one_device(dp
, parent
);
586 scan_tree(dp
->child
, &op
->dev
);
592 static void __init
scan_of_devices(void)
594 struct device_node
*root
= of_find_node_by_path("/");
595 struct of_device
*parent
;
597 parent
= scan_one_device(root
, NULL
);
601 scan_tree(root
->child
, &parent
->dev
);
604 static int __init
of_bus_driver_init(void)
608 err
= of_bus_type_init(&of_platform_bus_type
, "of");
615 postcore_initcall(of_bus_driver_init
);
617 static int __init
of_debug(char *str
)
621 get_option(&str
, &val
);
623 of_resource_verbose
= 1;
627 __setup("of_debug=", of_debug
);