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>
10 #include <linux/of_device.h>
11 #include <linux/of_platform.h>
13 #include "of_device_common.h"
15 void __iomem
*of_ioremap(struct resource
*res
, unsigned long offset
, unsigned long size
, char *name
)
17 unsigned long ret
= res
->start
+ offset
;
20 if (res
->flags
& IORESOURCE_MEM
)
21 r
= request_mem_region(ret
, size
, name
);
23 r
= request_region(ret
, size
, name
);
27 return (void __iomem
*) ret
;
29 EXPORT_SYMBOL(of_ioremap
);
31 void of_iounmap(struct resource
*res
, void __iomem
*base
, unsigned long size
)
33 if (res
->flags
& IORESOURCE_MEM
)
34 release_mem_region((unsigned long) base
, size
);
36 release_region((unsigned long) base
, size
);
38 EXPORT_SYMBOL(of_iounmap
);
41 * PCI bus specific translator
44 static int of_bus_pci_match(struct device_node
*np
)
46 if (!strcmp(np
->name
, "pci")) {
47 const char *model
= of_get_property(np
, "model", NULL
);
49 if (model
&& !strcmp(model
, "SUNW,simba"))
52 /* Do not do PCI specific frobbing if the
53 * PCI bridge lacks a ranges property. We
54 * want to pass it through up to the next
55 * parent as-is, not with the PCI translate
56 * method which chops off the top address cell.
58 if (!of_find_property(np
, "ranges", NULL
))
67 static int of_bus_simba_match(struct device_node
*np
)
69 const char *model
= of_get_property(np
, "model", NULL
);
71 if (model
&& !strcmp(model
, "SUNW,simba"))
74 /* Treat PCI busses lacking ranges property just like
77 if (!strcmp(np
->name
, "pci")) {
78 if (!of_find_property(np
, "ranges", NULL
))
85 static int of_bus_simba_map(u32
*addr
, const u32
*range
,
86 int na
, int ns
, int pna
)
91 static void of_bus_pci_count_cells(struct device_node
*np
,
92 int *addrc
, int *sizec
)
100 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
101 int na
, int ns
, int pna
)
103 u32 result
[OF_MAX_ADDR_CELLS
];
106 /* Check address type match */
107 if (!((addr
[0] ^ range
[0]) & 0x03000000))
110 /* Special exception, we can map a 64-bit address into
113 if ((addr
[0] & 0x03000000) == 0x03000000 &&
114 (range
[0] & 0x03000000) == 0x02000000)
120 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
124 /* Start with the parent range base. */
125 memcpy(result
, range
+ na
, pna
* 4);
127 /* Add in the child address offset, skipping high cell. */
128 for (i
= 0; i
< na
- 1; i
++)
129 result
[pna
- 1 - i
] +=
133 memcpy(addr
, result
, pna
* 4);
138 static unsigned long of_bus_pci_get_flags(const u32
*addr
, unsigned long flags
)
142 /* For PCI, we override whatever child busses may have used. */
144 switch((w
>> 24) & 0x03) {
146 flags
|= IORESOURCE_IO
;
149 case 0x02: /* 32 bits */
150 case 0x03: /* 64 bits */
151 flags
|= IORESOURCE_MEM
;
155 flags
|= IORESOURCE_PREFETCH
;
160 * FHC/Central bus specific translator.
162 * This is just needed to hard-code the address and size cell
163 * counts. 'fhc' and 'central' nodes lack the #address-cells and
164 * #size-cells properties, and if you walk to the root on such
165 * Enterprise boxes all you'll get is a #size-cells of 2 which is
166 * not what we want to use.
168 static int of_bus_fhc_match(struct device_node
*np
)
170 return !strcmp(np
->name
, "fhc") ||
171 !strcmp(np
->name
, "central");
174 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
177 * Array of bus specific translators
180 static struct of_bus of_busses
[] = {
184 .addr_prop_name
= "assigned-addresses",
185 .match
= of_bus_pci_match
,
186 .count_cells
= of_bus_pci_count_cells
,
187 .map
= of_bus_pci_map
,
188 .get_flags
= of_bus_pci_get_flags
,
193 .addr_prop_name
= "assigned-addresses",
194 .match
= of_bus_simba_match
,
195 .count_cells
= of_bus_pci_count_cells
,
196 .map
= of_bus_simba_map
,
197 .get_flags
= of_bus_pci_get_flags
,
202 .addr_prop_name
= "reg",
203 .match
= of_bus_sbus_match
,
204 .count_cells
= of_bus_sbus_count_cells
,
205 .map
= of_bus_default_map
,
206 .get_flags
= of_bus_default_get_flags
,
211 .addr_prop_name
= "reg",
212 .match
= of_bus_fhc_match
,
213 .count_cells
= of_bus_fhc_count_cells
,
214 .map
= of_bus_default_map
,
215 .get_flags
= of_bus_default_get_flags
,
220 .addr_prop_name
= "reg",
222 .count_cells
= of_bus_default_count_cells
,
223 .map
= of_bus_default_map
,
224 .get_flags
= of_bus_default_get_flags
,
228 static struct of_bus
*of_match_bus(struct device_node
*np
)
232 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
233 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
234 return &of_busses
[i
];
239 static int __init
build_one_resource(struct device_node
*parent
,
243 int na
, int ns
, int pna
)
248 ranges
= of_get_property(parent
, "ranges", &rlen
);
249 if (ranges
== NULL
|| rlen
== 0) {
250 u32 result
[OF_MAX_ADDR_CELLS
];
253 memset(result
, 0, pna
* 4);
254 for (i
= 0; i
< na
; i
++)
255 result
[pna
- 1 - i
] =
258 memcpy(addr
, result
, pna
* 4);
262 /* Now walk through the ranges */
264 rone
= na
+ pna
+ ns
;
265 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
266 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
270 /* When we miss an I/O space match on PCI, just pass it up
271 * to the next PCI bridge and/or controller.
273 if (!strcmp(bus
->name
, "pci") &&
274 (addr
[0] & 0x03000000) == 0x01000000)
280 static int __init
use_1to1_mapping(struct device_node
*pp
)
282 /* If we have a ranges property in the parent, use it. */
283 if (of_find_property(pp
, "ranges", NULL
) != NULL
)
286 /* If the parent is the dma node of an ISA bus, pass
287 * the translation up to the root.
289 * Some SBUS devices use intermediate nodes to express
290 * hierarchy within the device itself. These aren't
291 * real bus nodes, and don't have a 'ranges' property.
292 * But, we should still pass the translation work up
293 * to the SBUS itself.
295 if (!strcmp(pp
->name
, "dma") ||
296 !strcmp(pp
->name
, "espdma") ||
297 !strcmp(pp
->name
, "ledma") ||
298 !strcmp(pp
->name
, "lebuffer"))
301 /* Similarly for all PCI bridges, if we get this far
302 * it lacks a ranges property, and this will include
305 if (!strcmp(pp
->name
, "pci"))
311 static int of_resource_verbose
;
313 static void __init
build_device_resources(struct of_device
*op
,
314 struct device
*parent
)
316 struct of_device
*p_op
;
325 p_op
= to_of_device(parent
);
326 bus
= of_match_bus(p_op
->node
);
327 bus
->count_cells(op
->node
, &na
, &ns
);
329 preg
= of_get_property(op
->node
, bus
->addr_prop_name
, &num_reg
);
330 if (!preg
|| num_reg
== 0)
333 /* Convert to num-cells. */
336 /* Convert to num-entries. */
339 /* Prevent overrunning the op->resources[] array. */
340 if (num_reg
> PROMREG_MAX
) {
341 printk(KERN_WARNING
"%s: Too many regs (%d), "
343 op
->node
->full_name
, num_reg
, PROMREG_MAX
);
344 num_reg
= PROMREG_MAX
;
347 for (index
= 0; index
< num_reg
; index
++) {
348 struct resource
*r
= &op
->resource
[index
];
349 u32 addr
[OF_MAX_ADDR_CELLS
];
350 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
351 struct device_node
*dp
= op
->node
;
352 struct device_node
*pp
= p_op
->node
;
353 struct of_bus
*pbus
, *dbus
;
354 u64 size
, result
= OF_BAD_ADDR
;
359 size
= of_read_addr(reg
+ na
, ns
);
360 memcpy(addr
, reg
, na
* 4);
362 flags
= bus
->get_flags(addr
, 0);
364 if (use_1to1_mapping(pp
)) {
365 result
= of_read_addr(addr
, na
);
377 result
= of_read_addr(addr
, dna
);
381 pbus
= of_match_bus(pp
);
382 pbus
->count_cells(dp
, &pna
, &pns
);
384 if (build_one_resource(dp
, dbus
, pbus
, addr
,
388 flags
= pbus
->get_flags(addr
, flags
);
396 memset(r
, 0, sizeof(*r
));
398 if (of_resource_verbose
)
399 printk("%s reg[%d] -> %llx\n",
400 op
->node
->full_name
, index
,
403 if (result
!= OF_BAD_ADDR
) {
404 if (tlb_type
== hypervisor
)
405 result
&= 0x0fffffffffffffffUL
;
408 r
->end
= result
+ size
- 1;
411 r
->name
= op
->node
->name
;
415 static struct device_node
* __init
416 apply_interrupt_map(struct device_node
*dp
, struct device_node
*pp
,
417 const u32
*imap
, int imlen
, const u32
*imask
,
420 struct device_node
*cp
;
421 unsigned int irq
= *irq_p
;
427 bus
= of_match_bus(pp
);
428 bus
->count_cells(dp
, &na
, NULL
);
430 reg
= of_get_property(dp
, "reg", &num_reg
);
431 if (!reg
|| !num_reg
)
434 imlen
/= ((na
+ 3) * 4);
436 for (i
= 0; i
< imlen
; i
++) {
439 for (j
= 0; j
< na
; j
++) {
440 if ((reg
[j
] & imask
[j
]) != imap
[j
])
443 if (imap
[na
] == irq
) {
444 handle
= imap
[na
+ 1];
453 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
454 * properties that do not include the on-board device
455 * interrupts. Instead, the device's 'interrupts' property
456 * is already a fully specified INO value.
458 * Handle this by deciding that, if we didn't get a
459 * match in the parent's 'interrupt-map', and the
460 * parent is an IRQ translater, then use the parent as
461 * our IRQ controller.
470 cp
= of_find_node_by_phandle(handle
);
475 static unsigned int __init
pci_irq_swizzle(struct device_node
*dp
,
476 struct device_node
*pp
,
479 const struct linux_prom_pci_registers
*regs
;
480 unsigned int bus
, devfn
, slot
, ret
;
482 if (irq
< 1 || irq
> 4)
485 regs
= of_get_property(dp
, "reg", NULL
);
489 bus
= (regs
->phys_hi
>> 16) & 0xff;
490 devfn
= (regs
->phys_hi
>> 8) & 0xff;
491 slot
= (devfn
>> 3) & 0x1f;
494 /* Derived from Table 8-3, U2P User's Manual. This branch
495 * is handling a PCI controller that lacks a proper set of
496 * interrupt-map and interrupt-map-mask properties. The
497 * Ultra-E450 is one example.
499 * The bit layout is BSSLL, where:
500 * B: 0 on bus A, 1 on bus B
501 * D: 2-bit slot number, derived from PCI device number as
502 * (dev - 1) for bus A, or (dev - 2) for bus B
503 * L: 2-bit line number
508 slot
= (slot
- 1) << 2;
512 slot
= (slot
- 2) << 2;
516 ret
= (bus
| slot
| irq
);
518 /* Going through a PCI-PCI bridge that lacks a set of
519 * interrupt-map and interrupt-map-mask properties.
521 ret
= ((irq
- 1 + (slot
& 3)) & 3) + 1;
527 static int of_irq_verbose
;
529 static unsigned int __init
build_one_device_irq(struct of_device
*op
,
530 struct device
*parent
,
533 struct device_node
*dp
= op
->node
;
534 struct device_node
*pp
, *ip
;
535 unsigned int orig_irq
= irq
;
538 if (irq
== 0xffffffff)
542 irq
= dp
->irq_trans
->irq_build(dp
, irq
,
543 dp
->irq_trans
->data
);
546 printk("%s: direct translate %x --> %x\n",
547 dp
->full_name
, orig_irq
, irq
);
552 /* Something more complicated. Walk up to the root, applying
553 * interrupt-map or bus specific translations, until we hit
556 * If we hit a bus type or situation we cannot handle, we
557 * stop and assume that the original IRQ number was in a
558 * format which has special meaning to it's immediate parent.
563 const void *imap
, *imsk
;
566 imap
= of_get_property(pp
, "interrupt-map", &imlen
);
567 imsk
= of_get_property(pp
, "interrupt-map-mask", NULL
);
569 struct device_node
*iret
;
570 int this_orig_irq
= irq
;
572 iret
= apply_interrupt_map(dp
, pp
,
577 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
579 pp
->full_name
, this_orig_irq
,
580 (iret
? iret
->full_name
: "NULL"), irq
);
585 if (iret
->irq_trans
) {
590 if (!strcmp(pp
->name
, "pci")) {
591 unsigned int this_orig_irq
= irq
;
593 irq
= pci_irq_swizzle(dp
, pp
, irq
);
595 printk("%s: PCI swizzle [%s] "
598 pp
->full_name
, this_orig_irq
,
614 irq
= ip
->irq_trans
->irq_build(op
->node
, irq
,
615 ip
->irq_trans
->data
);
617 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
618 op
->node
->full_name
, ip
->full_name
, orig_irq
, irq
);
621 nid
= of_node_to_nid(dp
);
623 cpumask_t numa_mask
= *cpumask_of_node(nid
);
625 irq_set_affinity(irq
, &numa_mask
);
631 static struct of_device
* __init
scan_one_device(struct device_node
*dp
,
632 struct device
*parent
)
634 struct of_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
635 const unsigned int *irq
;
636 struct dev_archdata
*sd
;
642 sd
= &op
->dev
.archdata
;
648 op
->clock_freq
= of_getintprop_default(dp
, "clock-frequency",
650 op
->portid
= of_getintprop_default(dp
, "upa-portid", -1);
651 if (op
->portid
== -1)
652 op
->portid
= of_getintprop_default(dp
, "portid", -1);
654 irq
= of_get_property(dp
, "interrupts", &len
);
656 op
->num_irqs
= len
/ 4;
658 /* Prevent overrunning the op->irqs[] array. */
659 if (op
->num_irqs
> PROMINTR_MAX
) {
660 printk(KERN_WARNING
"%s: Too many irqs (%d), "
662 dp
->full_name
, op
->num_irqs
, PROMINTR_MAX
);
663 op
->num_irqs
= PROMINTR_MAX
;
665 memcpy(op
->irqs
, irq
, op
->num_irqs
* 4);
670 build_device_resources(op
, parent
);
671 for (i
= 0; i
< op
->num_irqs
; i
++)
672 op
->irqs
[i
] = build_one_device_irq(op
, parent
, op
->irqs
[i
]);
674 op
->dev
.parent
= parent
;
675 op
->dev
.bus
= &of_platform_bus_type
;
677 dev_set_name(&op
->dev
, "root");
679 dev_set_name(&op
->dev
, "%08x", dp
->phandle
);
681 if (of_device_register(op
)) {
682 printk("%s: Could not register of device.\n",
691 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
694 struct of_device
*op
= scan_one_device(dp
, parent
);
697 scan_tree(dp
->child
, &op
->dev
);
703 static void __init
scan_of_devices(void)
705 struct device_node
*root
= of_find_node_by_path("/");
706 struct of_device
*parent
;
708 parent
= scan_one_device(root
, NULL
);
712 scan_tree(root
->child
, &parent
->dev
);
715 static int __init
of_bus_driver_init(void)
719 err
= of_bus_type_init(&of_platform_bus_type
, "of");
726 postcore_initcall(of_bus_driver_init
);
728 static int __init
of_debug(char *str
)
732 get_option(&str
, &val
);
734 of_resource_verbose
= 1;
740 __setup("of_debug=", of_debug
);