1 #include <linux/string.h>
2 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/export.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>
12 #include <asm/spitfire.h>
14 #include "of_device_common.h"
16 void __iomem
*of_ioremap(struct resource
*res
, unsigned long offset
, unsigned long size
, char *name
)
18 unsigned long ret
= res
->start
+ offset
;
21 if (res
->flags
& IORESOURCE_MEM
)
22 r
= request_mem_region(ret
, size
, name
);
24 r
= request_region(ret
, size
, name
);
28 return (void __iomem
*) ret
;
30 EXPORT_SYMBOL(of_ioremap
);
32 void of_iounmap(struct resource
*res
, void __iomem
*base
, unsigned long size
)
34 if (res
->flags
& IORESOURCE_MEM
)
35 release_mem_region((unsigned long) base
, size
);
37 release_region((unsigned long) base
, size
);
39 EXPORT_SYMBOL(of_iounmap
);
42 * PCI bus specific translator
45 static int of_bus_pci_match(struct device_node
*np
)
47 if (!strcmp(np
->name
, "pci")) {
48 const char *model
= of_get_property(np
, "model", NULL
);
50 if (model
&& !strcmp(model
, "SUNW,simba"))
53 /* Do not do PCI specific frobbing if the
54 * PCI bridge lacks a ranges property. We
55 * want to pass it through up to the next
56 * parent as-is, not with the PCI translate
57 * method which chops off the top address cell.
59 if (!of_find_property(np
, "ranges", NULL
))
68 static int of_bus_simba_match(struct device_node
*np
)
70 const char *model
= of_get_property(np
, "model", NULL
);
72 if (model
&& !strcmp(model
, "SUNW,simba"))
75 /* Treat PCI busses lacking ranges property just like
78 if (!strcmp(np
->name
, "pci")) {
79 if (!of_find_property(np
, "ranges", NULL
))
86 static int of_bus_simba_map(u32
*addr
, const u32
*range
,
87 int na
, int ns
, int pna
)
92 static void of_bus_pci_count_cells(struct device_node
*np
,
93 int *addrc
, int *sizec
)
101 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
102 int na
, int ns
, int pna
)
104 u32 result
[OF_MAX_ADDR_CELLS
];
107 /* Check address type match */
108 if (!((addr
[0] ^ range
[0]) & 0x03000000))
111 /* Special exception, we can map a 64-bit address into
114 if ((addr
[0] & 0x03000000) == 0x03000000 &&
115 (range
[0] & 0x03000000) == 0x02000000)
121 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
125 /* Start with the parent range base. */
126 memcpy(result
, range
+ na
, pna
* 4);
128 /* Add in the child address offset, skipping high cell. */
129 for (i
= 0; i
< na
- 1; i
++)
130 result
[pna
- 1 - i
] +=
134 memcpy(addr
, result
, pna
* 4);
139 static unsigned long of_bus_pci_get_flags(const u32
*addr
, unsigned long flags
)
143 /* For PCI, we override whatever child busses may have used. */
145 switch((w
>> 24) & 0x03) {
147 flags
|= IORESOURCE_IO
;
150 case 0x02: /* 32 bits */
151 case 0x03: /* 64 bits */
152 flags
|= IORESOURCE_MEM
;
156 flags
|= IORESOURCE_PREFETCH
;
161 * FHC/Central bus specific translator.
163 * This is just needed to hard-code the address and size cell
164 * counts. 'fhc' and 'central' nodes lack the #address-cells and
165 * #size-cells properties, and if you walk to the root on such
166 * Enterprise boxes all you'll get is a #size-cells of 2 which is
167 * not what we want to use.
169 static int of_bus_fhc_match(struct device_node
*np
)
171 return !strcmp(np
->name
, "fhc") ||
172 !strcmp(np
->name
, "central");
175 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
178 * Array of bus specific translators
181 static struct of_bus of_busses
[] = {
185 .addr_prop_name
= "assigned-addresses",
186 .match
= of_bus_pci_match
,
187 .count_cells
= of_bus_pci_count_cells
,
188 .map
= of_bus_pci_map
,
189 .get_flags
= of_bus_pci_get_flags
,
194 .addr_prop_name
= "assigned-addresses",
195 .match
= of_bus_simba_match
,
196 .count_cells
= of_bus_pci_count_cells
,
197 .map
= of_bus_simba_map
,
198 .get_flags
= of_bus_pci_get_flags
,
203 .addr_prop_name
= "reg",
204 .match
= of_bus_sbus_match
,
205 .count_cells
= of_bus_sbus_count_cells
,
206 .map
= of_bus_default_map
,
207 .get_flags
= of_bus_default_get_flags
,
212 .addr_prop_name
= "reg",
213 .match
= of_bus_fhc_match
,
214 .count_cells
= of_bus_fhc_count_cells
,
215 .map
= of_bus_default_map
,
216 .get_flags
= of_bus_default_get_flags
,
221 .addr_prop_name
= "reg",
223 .count_cells
= of_bus_default_count_cells
,
224 .map
= of_bus_default_map
,
225 .get_flags
= of_bus_default_get_flags
,
229 static struct of_bus
*of_match_bus(struct device_node
*np
)
233 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
234 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
235 return &of_busses
[i
];
240 static int __init
build_one_resource(struct device_node
*parent
,
244 int na
, int ns
, int pna
)
249 ranges
= of_get_property(parent
, "ranges", &rlen
);
250 if (ranges
== NULL
|| rlen
== 0) {
251 u32 result
[OF_MAX_ADDR_CELLS
];
254 memset(result
, 0, pna
* 4);
255 for (i
= 0; i
< na
; i
++)
256 result
[pna
- 1 - i
] =
259 memcpy(addr
, result
, pna
* 4);
263 /* Now walk through the ranges */
265 rone
= na
+ pna
+ ns
;
266 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
267 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
271 /* When we miss an I/O space match on PCI, just pass it up
272 * to the next PCI bridge and/or controller.
274 if (!strcmp(bus
->name
, "pci") &&
275 (addr
[0] & 0x03000000) == 0x01000000)
281 static int __init
use_1to1_mapping(struct device_node
*pp
)
283 /* If we have a ranges property in the parent, use it. */
284 if (of_find_property(pp
, "ranges", NULL
) != NULL
)
287 /* If the parent is the dma node of an ISA bus, pass
288 * the translation up to the root.
290 * Some SBUS devices use intermediate nodes to express
291 * hierarchy within the device itself. These aren't
292 * real bus nodes, and don't have a 'ranges' property.
293 * But, we should still pass the translation work up
294 * to the SBUS itself.
296 if (!strcmp(pp
->name
, "dma") ||
297 !strcmp(pp
->name
, "espdma") ||
298 !strcmp(pp
->name
, "ledma") ||
299 !strcmp(pp
->name
, "lebuffer"))
302 /* Similarly for all PCI bridges, if we get this far
303 * it lacks a ranges property, and this will include
306 if (!strcmp(pp
->name
, "pci"))
312 static int of_resource_verbose
;
314 static void __init
build_device_resources(struct platform_device
*op
,
315 struct device
*parent
)
317 struct platform_device
*p_op
;
326 p_op
= to_platform_device(parent
);
327 bus
= of_match_bus(p_op
->dev
.of_node
);
328 bus
->count_cells(op
->dev
.of_node
, &na
, &ns
);
330 preg
= of_get_property(op
->dev
.of_node
, bus
->addr_prop_name
, &num_reg
);
331 if (!preg
|| num_reg
== 0)
334 /* Convert to num-cells. */
337 /* Convert to num-entries. */
340 /* Prevent overrunning the op->resources[] array. */
341 if (num_reg
> PROMREG_MAX
) {
342 printk(KERN_WARNING
"%s: Too many regs (%d), "
344 op
->dev
.of_node
->full_name
, num_reg
, PROMREG_MAX
);
345 num_reg
= PROMREG_MAX
;
348 op
->resource
= op
->archdata
.resource
;
349 op
->num_resources
= num_reg
;
350 for (index
= 0; index
< num_reg
; index
++) {
351 struct resource
*r
= &op
->resource
[index
];
352 u32 addr
[OF_MAX_ADDR_CELLS
];
353 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
354 struct device_node
*dp
= op
->dev
.of_node
;
355 struct device_node
*pp
= p_op
->dev
.of_node
;
356 struct of_bus
*pbus
, *dbus
;
357 u64 size
, result
= OF_BAD_ADDR
;
362 size
= of_read_addr(reg
+ na
, ns
);
363 memcpy(addr
, reg
, na
* 4);
365 flags
= bus
->get_flags(addr
, 0);
367 if (use_1to1_mapping(pp
)) {
368 result
= of_read_addr(addr
, na
);
380 result
= of_read_addr(addr
, dna
);
384 pbus
= of_match_bus(pp
);
385 pbus
->count_cells(dp
, &pna
, &pns
);
387 if (build_one_resource(dp
, dbus
, pbus
, addr
,
391 flags
= pbus
->get_flags(addr
, flags
);
399 memset(r
, 0, sizeof(*r
));
401 if (of_resource_verbose
)
402 printk("%s reg[%d] -> %llx\n",
403 op
->dev
.of_node
->full_name
, index
,
406 if (result
!= OF_BAD_ADDR
) {
407 if (tlb_type
== hypervisor
)
408 result
&= 0x0fffffffffffffffUL
;
411 r
->end
= result
+ size
- 1;
414 r
->name
= op
->dev
.of_node
->name
;
418 static struct device_node
* __init
419 apply_interrupt_map(struct device_node
*dp
, struct device_node
*pp
,
420 const u32
*imap
, int imlen
, const u32
*imask
,
423 struct device_node
*cp
;
424 unsigned int irq
= *irq_p
;
430 bus
= of_match_bus(pp
);
431 bus
->count_cells(dp
, &na
, NULL
);
433 reg
= of_get_property(dp
, "reg", &num_reg
);
434 if (!reg
|| !num_reg
)
437 imlen
/= ((na
+ 3) * 4);
439 for (i
= 0; i
< imlen
; i
++) {
442 for (j
= 0; j
< na
; j
++) {
443 if ((reg
[j
] & imask
[j
]) != imap
[j
])
446 if (imap
[na
] == irq
) {
447 handle
= imap
[na
+ 1];
456 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
457 * properties that do not include the on-board device
458 * interrupts. Instead, the device's 'interrupts' property
459 * is already a fully specified INO value.
461 * Handle this by deciding that, if we didn't get a
462 * match in the parent's 'interrupt-map', and the
463 * parent is an IRQ translator, then use the parent as
464 * our IRQ controller.
473 cp
= of_find_node_by_phandle(handle
);
478 static unsigned int __init
pci_irq_swizzle(struct device_node
*dp
,
479 struct device_node
*pp
,
482 const struct linux_prom_pci_registers
*regs
;
483 unsigned int bus
, devfn
, slot
, ret
;
485 if (irq
< 1 || irq
> 4)
488 regs
= of_get_property(dp
, "reg", NULL
);
492 bus
= (regs
->phys_hi
>> 16) & 0xff;
493 devfn
= (regs
->phys_hi
>> 8) & 0xff;
494 slot
= (devfn
>> 3) & 0x1f;
497 /* Derived from Table 8-3, U2P User's Manual. This branch
498 * is handling a PCI controller that lacks a proper set of
499 * interrupt-map and interrupt-map-mask properties. The
500 * Ultra-E450 is one example.
502 * The bit layout is BSSLL, where:
503 * B: 0 on bus A, 1 on bus B
504 * D: 2-bit slot number, derived from PCI device number as
505 * (dev - 1) for bus A, or (dev - 2) for bus B
506 * L: 2-bit line number
511 slot
= (slot
- 1) << 2;
515 slot
= (slot
- 2) << 2;
519 ret
= (bus
| slot
| irq
);
521 /* Going through a PCI-PCI bridge that lacks a set of
522 * interrupt-map and interrupt-map-mask properties.
524 ret
= ((irq
- 1 + (slot
& 3)) & 3) + 1;
530 static int of_irq_verbose
;
532 static unsigned int __init
build_one_device_irq(struct platform_device
*op
,
533 struct device
*parent
,
536 struct device_node
*dp
= op
->dev
.of_node
;
537 struct device_node
*pp
, *ip
;
538 unsigned int orig_irq
= irq
;
541 if (irq
== 0xffffffff)
545 irq
= dp
->irq_trans
->irq_build(dp
, irq
,
546 dp
->irq_trans
->data
);
549 printk("%s: direct translate %x --> %x\n",
550 dp
->full_name
, orig_irq
, irq
);
555 /* Something more complicated. Walk up to the root, applying
556 * interrupt-map or bus specific translations, until we hit
559 * If we hit a bus type or situation we cannot handle, we
560 * stop and assume that the original IRQ number was in a
561 * format which has special meaning to it's immediate parent.
566 const void *imap
, *imsk
;
569 imap
= of_get_property(pp
, "interrupt-map", &imlen
);
570 imsk
= of_get_property(pp
, "interrupt-map-mask", NULL
);
572 struct device_node
*iret
;
573 int this_orig_irq
= irq
;
575 iret
= apply_interrupt_map(dp
, pp
,
580 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
581 op
->dev
.of_node
->full_name
,
582 pp
->full_name
, this_orig_irq
,
583 (iret
? iret
->full_name
: "NULL"), irq
);
588 if (iret
->irq_trans
) {
593 if (!strcmp(pp
->name
, "pci")) {
594 unsigned int this_orig_irq
= irq
;
596 irq
= pci_irq_swizzle(dp
, pp
, irq
);
598 printk("%s: PCI swizzle [%s] "
600 op
->dev
.of_node
->full_name
,
601 pp
->full_name
, this_orig_irq
,
617 irq
= ip
->irq_trans
->irq_build(op
->dev
.of_node
, irq
,
618 ip
->irq_trans
->data
);
620 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
621 op
->dev
.of_node
->full_name
, ip
->full_name
, orig_irq
, irq
);
624 nid
= of_node_to_nid(dp
);
628 cpumask_copy(&numa_mask
, cpumask_of_node(nid
));
629 irq_set_affinity(irq
, &numa_mask
);
635 static struct platform_device
* __init
scan_one_device(struct device_node
*dp
,
636 struct device
*parent
)
638 struct platform_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
639 const unsigned int *irq
;
640 struct dev_archdata
*sd
;
646 sd
= &op
->dev
.archdata
;
649 op
->dev
.of_node
= dp
;
651 irq
= of_get_property(dp
, "interrupts", &len
);
653 op
->archdata
.num_irqs
= len
/ 4;
655 /* Prevent overrunning the op->irqs[] array. */
656 if (op
->archdata
.num_irqs
> PROMINTR_MAX
) {
657 printk(KERN_WARNING
"%s: Too many irqs (%d), "
659 dp
->full_name
, op
->archdata
.num_irqs
, PROMINTR_MAX
);
660 op
->archdata
.num_irqs
= PROMINTR_MAX
;
662 memcpy(op
->archdata
.irqs
, irq
, op
->archdata
.num_irqs
* 4);
664 op
->archdata
.num_irqs
= 0;
667 build_device_resources(op
, parent
);
668 for (i
= 0; i
< op
->archdata
.num_irqs
; i
++)
669 op
->archdata
.irqs
[i
] = build_one_device_irq(op
, parent
, op
->archdata
.irqs
[i
]);
671 op
->dev
.parent
= parent
;
672 op
->dev
.bus
= &platform_bus_type
;
674 dev_set_name(&op
->dev
, "root");
676 dev_set_name(&op
->dev
, "%08x", dp
->phandle
);
678 if (of_device_register(op
)) {
679 printk("%s: Could not register of device.\n",
688 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
691 struct platform_device
*op
= scan_one_device(dp
, parent
);
694 scan_tree(dp
->child
, &op
->dev
);
700 static int __init
scan_of_devices(void)
702 struct device_node
*root
= of_find_node_by_path("/");
703 struct platform_device
*parent
;
705 parent
= scan_one_device(root
, NULL
);
709 scan_tree(root
->child
, &parent
->dev
);
712 postcore_initcall(scan_of_devices
);
714 static int __init
of_debug(char *str
)
718 get_option(&str
, &val
);
720 of_resource_verbose
= 1;
726 __setup("of_debug=", of_debug
);