1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/dma-mapping.h>
5 #include <linux/init.h>
6 #include <linux/export.h>
7 #include <linux/mod_devicetable.h>
8 #include <linux/slab.h>
9 #include <linux/errno.h>
10 #include <linux/irq.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <asm/spitfire.h>
16 #include "of_device_common.h"
18 void __iomem
*of_ioremap(struct resource
*res
, unsigned long offset
, unsigned long size
, char *name
)
20 unsigned long ret
= res
->start
+ offset
;
23 if (res
->flags
& IORESOURCE_MEM
)
24 r
= request_mem_region(ret
, size
, name
);
26 r
= request_region(ret
, size
, name
);
30 return (void __iomem
*) ret
;
32 EXPORT_SYMBOL(of_ioremap
);
34 void of_iounmap(struct resource
*res
, void __iomem
*base
, unsigned long size
)
36 if (res
->flags
& IORESOURCE_MEM
)
37 release_mem_region((unsigned long) base
, size
);
39 release_region((unsigned long) base
, size
);
41 EXPORT_SYMBOL(of_iounmap
);
44 * PCI bus specific translator
47 static int of_bus_pci_match(struct device_node
*np
)
49 if (of_node_name_eq(np
, "pci")) {
50 const char *model
= of_get_property(np
, "model", NULL
);
52 if (model
&& !strcmp(model
, "SUNW,simba"))
55 /* Do not do PCI specific frobbing if the
56 * PCI bridge lacks a ranges property. We
57 * want to pass it through up to the next
58 * parent as-is, not with the PCI translate
59 * method which chops off the top address cell.
61 if (!of_property_present(np
, "ranges"))
70 static int of_bus_simba_match(struct device_node
*np
)
72 const char *model
= of_get_property(np
, "model", NULL
);
74 if (model
&& !strcmp(model
, "SUNW,simba"))
77 /* Treat PCI busses lacking ranges property just like
80 if (of_node_name_eq(np
, "pci")) {
81 if (!of_property_present(np
, "ranges"))
88 static int of_bus_simba_map(u32
*addr
, const u32
*range
,
89 int na
, int ns
, int pna
)
94 static void of_bus_pci_count_cells(struct device_node
*np
,
95 int *addrc
, int *sizec
)
103 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
104 int na
, int ns
, int pna
)
106 u32 result
[OF_MAX_ADDR_CELLS
];
109 /* Check address type match */
110 if (!((addr
[0] ^ range
[0]) & 0x03000000))
113 /* Special exception, we can map a 64-bit address into
116 if ((addr
[0] & 0x03000000) == 0x03000000 &&
117 (range
[0] & 0x03000000) == 0x02000000)
123 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
127 /* Start with the parent range base. */
128 memcpy(result
, range
+ na
, pna
* 4);
130 /* Add in the child address offset, skipping high cell. */
131 for (i
= 0; i
< na
- 1; i
++)
132 result
[pna
- 1 - i
] +=
136 memcpy(addr
, result
, pna
* 4);
141 static unsigned long of_bus_pci_get_flags(const u32
*addr
, unsigned long flags
)
145 /* For PCI, we override whatever child busses may have used. */
147 switch((w
>> 24) & 0x03) {
149 flags
|= IORESOURCE_IO
;
152 case 0x02: /* 32 bits */
153 case 0x03: /* 64 bits */
154 flags
|= IORESOURCE_MEM
;
158 flags
|= IORESOURCE_PREFETCH
;
163 * FHC/Central bus specific translator.
165 * This is just needed to hard-code the address and size cell
166 * counts. 'fhc' and 'central' nodes lack the #address-cells and
167 * #size-cells properties, and if you walk to the root on such
168 * Enterprise boxes all you'll get is a #size-cells of 2 which is
169 * not what we want to use.
171 static int of_bus_fhc_match(struct device_node
*np
)
173 return of_node_name_eq(np
, "fhc") ||
174 of_node_name_eq(np
, "central");
177 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
180 * Array of bus specific translators
183 static struct of_bus of_busses
[] = {
187 .addr_prop_name
= "assigned-addresses",
188 .match
= of_bus_pci_match
,
189 .count_cells
= of_bus_pci_count_cells
,
190 .map
= of_bus_pci_map
,
191 .get_flags
= of_bus_pci_get_flags
,
196 .addr_prop_name
= "assigned-addresses",
197 .match
= of_bus_simba_match
,
198 .count_cells
= of_bus_pci_count_cells
,
199 .map
= of_bus_simba_map
,
200 .get_flags
= of_bus_pci_get_flags
,
205 .addr_prop_name
= "reg",
206 .match
= of_bus_sbus_match
,
207 .count_cells
= of_bus_sbus_count_cells
,
208 .map
= of_bus_default_map
,
209 .get_flags
= of_bus_default_get_flags
,
214 .addr_prop_name
= "reg",
215 .match
= of_bus_fhc_match
,
216 .count_cells
= of_bus_fhc_count_cells
,
217 .map
= of_bus_default_map
,
218 .get_flags
= of_bus_default_get_flags
,
223 .addr_prop_name
= "reg",
225 .count_cells
= of_bus_default_count_cells
,
226 .map
= of_bus_default_map
,
227 .get_flags
= of_bus_default_get_flags
,
231 static struct of_bus
*of_match_bus(struct device_node
*np
)
235 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
236 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
237 return &of_busses
[i
];
242 static int __init
build_one_resource(struct device_node
*parent
,
246 int na
, int ns
, int pna
)
251 ranges
= of_get_property(parent
, "ranges", &rlen
);
252 if (ranges
== NULL
|| rlen
== 0) {
253 u32 result
[OF_MAX_ADDR_CELLS
];
256 memset(result
, 0, pna
* 4);
257 for (i
= 0; i
< na
; i
++)
258 result
[pna
- 1 - i
] =
261 memcpy(addr
, result
, pna
* 4);
265 /* Now walk through the ranges */
267 rone
= na
+ pna
+ ns
;
268 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
269 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
273 /* When we miss an I/O space match on PCI, just pass it up
274 * to the next PCI bridge and/or controller.
276 if (!strcmp(bus
->name
, "pci") &&
277 (addr
[0] & 0x03000000) == 0x01000000)
283 static int __init
use_1to1_mapping(struct device_node
*pp
)
285 /* If we have a ranges property in the parent, use it. */
286 if (of_property_present(pp
, "ranges"))
289 /* If the parent is the dma node of an ISA bus, pass
290 * the translation up to the root.
292 * Some SBUS devices use intermediate nodes to express
293 * hierarchy within the device itself. These aren't
294 * real bus nodes, and don't have a 'ranges' property.
295 * But, we should still pass the translation work up
296 * to the SBUS itself.
298 if (of_node_name_eq(pp
, "dma") ||
299 of_node_name_eq(pp
, "espdma") ||
300 of_node_name_eq(pp
, "ledma") ||
301 of_node_name_eq(pp
, "lebuffer"))
304 /* Similarly for all PCI bridges, if we get this far
305 * it lacks a ranges property, and this will include
308 if (of_node_name_eq(pp
, "pci"))
314 static int of_resource_verbose
;
316 static void __init
build_device_resources(struct platform_device
*op
,
317 struct device
*parent
)
319 struct platform_device
*p_op
;
328 p_op
= to_platform_device(parent
);
329 bus
= of_match_bus(p_op
->dev
.of_node
);
330 bus
->count_cells(op
->dev
.of_node
, &na
, &ns
);
332 preg
= of_get_property(op
->dev
.of_node
, bus
->addr_prop_name
, &num_reg
);
333 if (!preg
|| num_reg
== 0)
336 /* Convert to num-cells. */
339 /* Convert to num-entries. */
342 /* Prevent overrunning the op->resources[] array. */
343 if (num_reg
> PROMREG_MAX
) {
344 printk(KERN_WARNING
"%pOF: Too many regs (%d), "
346 op
->dev
.of_node
, num_reg
, PROMREG_MAX
);
347 num_reg
= PROMREG_MAX
;
350 op
->resource
= op
->archdata
.resource
;
351 op
->num_resources
= num_reg
;
352 for (index
= 0; index
< num_reg
; index
++) {
353 struct resource
*r
= &op
->resource
[index
];
354 u32 addr
[OF_MAX_ADDR_CELLS
];
355 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
356 struct device_node
*dp
= op
->dev
.of_node
;
357 struct device_node
*pp
= p_op
->dev
.of_node
;
358 struct of_bus
*pbus
, *dbus
;
359 u64 size
, result
= OF_BAD_ADDR
;
364 size
= of_read_addr(reg
+ na
, ns
);
365 memcpy(addr
, reg
, na
* 4);
367 flags
= bus
->get_flags(addr
, 0);
369 if (use_1to1_mapping(pp
)) {
370 result
= of_read_addr(addr
, na
);
382 result
= of_read_addr(addr
, dna
);
386 pbus
= of_match_bus(pp
);
387 pbus
->count_cells(dp
, &pna
, &pns
);
389 if (build_one_resource(dp
, dbus
, pbus
, addr
,
393 flags
= pbus
->get_flags(addr
, flags
);
401 memset(r
, 0, sizeof(*r
));
403 if (of_resource_verbose
)
404 printk("%pOF reg[%d] -> %llx\n",
405 op
->dev
.of_node
, index
,
408 if (result
!= OF_BAD_ADDR
) {
409 if (tlb_type
== hypervisor
)
410 result
&= 0x0fffffffffffffffUL
;
413 r
->end
= result
+ size
- 1;
416 r
->name
= op
->dev
.of_node
->full_name
;
420 static struct device_node
* __init
421 apply_interrupt_map(struct device_node
*dp
, struct device_node
*pp
,
422 const u32
*imap
, int imlen
, const u32
*imask
,
425 struct device_node
*cp
;
426 unsigned int irq
= *irq_p
;
432 bus
= of_match_bus(pp
);
433 bus
->count_cells(dp
, &na
, NULL
);
435 reg
= of_get_property(dp
, "reg", &num_reg
);
436 if (!reg
|| !num_reg
)
439 imlen
/= ((na
+ 3) * 4);
441 for (i
= 0; i
< imlen
; i
++) {
444 for (j
= 0; j
< na
; j
++) {
445 if ((reg
[j
] & imask
[j
]) != imap
[j
])
448 if (imap
[na
] == irq
) {
449 handle
= imap
[na
+ 1];
458 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
459 * properties that do not include the on-board device
460 * interrupts. Instead, the device's 'interrupts' property
461 * is already a fully specified INO value.
463 * Handle this by deciding that, if we didn't get a
464 * match in the parent's 'interrupt-map', and the
465 * parent is an IRQ translator, then use the parent as
466 * our IRQ controller.
475 cp
= of_find_node_by_phandle(handle
);
480 static unsigned int __init
pci_irq_swizzle(struct device_node
*dp
,
481 struct device_node
*pp
,
484 const struct linux_prom_pci_registers
*regs
;
485 unsigned int bus
, devfn
, slot
, ret
;
487 if (irq
< 1 || irq
> 4)
490 regs
= of_get_property(dp
, "reg", NULL
);
494 bus
= (regs
->phys_hi
>> 16) & 0xff;
495 devfn
= (regs
->phys_hi
>> 8) & 0xff;
496 slot
= (devfn
>> 3) & 0x1f;
499 /* Derived from Table 8-3, U2P User's Manual. This branch
500 * is handling a PCI controller that lacks a proper set of
501 * interrupt-map and interrupt-map-mask properties. The
502 * Ultra-E450 is one example.
504 * The bit layout is BSSLL, where:
505 * B: 0 on bus A, 1 on bus B
506 * D: 2-bit slot number, derived from PCI device number as
507 * (dev - 1) for bus A, or (dev - 2) for bus B
508 * L: 2-bit line number
513 slot
= (slot
- 1) << 2;
517 slot
= (slot
- 2) << 2;
521 ret
= (bus
| slot
| irq
);
523 /* Going through a PCI-PCI bridge that lacks a set of
524 * interrupt-map and interrupt-map-mask properties.
526 ret
= ((irq
- 1 + (slot
& 3)) & 3) + 1;
532 static int of_irq_verbose
;
534 static unsigned int __init
build_one_device_irq(struct platform_device
*op
,
535 struct device
*parent
,
538 struct device_node
*dp
= op
->dev
.of_node
;
539 struct device_node
*pp
, *ip
;
540 unsigned int orig_irq
= irq
;
543 if (irq
== 0xffffffff)
547 irq
= dp
->irq_trans
->irq_build(dp
, irq
,
548 dp
->irq_trans
->data
);
551 printk("%pOF: direct translate %x --> %x\n",
557 /* Something more complicated. Walk up to the root, applying
558 * interrupt-map or bus specific translations, until we hit
561 * If we hit a bus type or situation we cannot handle, we
562 * stop and assume that the original IRQ number was in a
563 * format which has special meaning to its immediate parent.
568 const void *imap
, *imsk
;
571 imap
= of_get_property(pp
, "interrupt-map", &imlen
);
572 imsk
= of_get_property(pp
, "interrupt-map-mask", NULL
);
574 struct device_node
*iret
;
575 int this_orig_irq
= irq
;
577 iret
= apply_interrupt_map(dp
, pp
,
582 printk("%pOF: Apply [%pOF:%x] imap --> [%pOF:%x]\n",
584 pp
, this_orig_irq
, iret
, irq
);
589 if (iret
->irq_trans
) {
594 if (of_node_name_eq(pp
, "pci")) {
595 unsigned int this_orig_irq
= irq
;
597 irq
= pci_irq_swizzle(dp
, pp
, irq
);
599 printk("%pOF: PCI swizzle [%pOF] "
618 irq
= ip
->irq_trans
->irq_build(op
->dev
.of_node
, irq
,
619 ip
->irq_trans
->data
);
621 printk("%pOF: Apply IRQ trans [%pOF] %x --> %x\n",
622 op
->dev
.of_node
, ip
, orig_irq
, irq
);
625 nid
= of_node_to_nid(dp
);
627 irq_set_affinity(irq
, cpumask_of_node(nid
));
633 static struct platform_device
* __init
scan_one_device(struct device_node
*dp
,
634 struct device
*parent
)
636 struct platform_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
637 const unsigned int *irq
;
638 struct dev_archdata
*sd
;
644 sd
= &op
->dev
.archdata
;
647 op
->dev
.of_node
= dp
;
649 irq
= of_get_property(dp
, "interrupts", &len
);
651 op
->archdata
.num_irqs
= len
/ 4;
653 /* Prevent overrunning the op->irqs[] array. */
654 if (op
->archdata
.num_irqs
> PROMINTR_MAX
) {
655 printk(KERN_WARNING
"%pOF: Too many irqs (%d), "
657 dp
, op
->archdata
.num_irqs
, PROMINTR_MAX
);
658 op
->archdata
.num_irqs
= PROMINTR_MAX
;
660 memcpy(op
->archdata
.irqs
, irq
, op
->archdata
.num_irqs
* 4);
662 op
->archdata
.num_irqs
= 0;
665 build_device_resources(op
, parent
);
666 for (i
= 0; i
< op
->archdata
.num_irqs
; i
++)
667 op
->archdata
.irqs
[i
] = build_one_device_irq(op
, parent
, op
->archdata
.irqs
[i
]);
669 op
->dev
.parent
= parent
;
670 op
->dev
.bus
= &platform_bus_type
;
672 dev_set_name(&op
->dev
, "root");
674 dev_set_name(&op
->dev
, "%08x", dp
->phandle
);
675 op
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
676 op
->dev
.dma_mask
= &op
->dev
.coherent_dma_mask
;
678 if (of_device_register(op
)) {
679 printk("%pOF: Could not register of device.\n", dp
);
687 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
690 struct platform_device
*op
= scan_one_device(dp
, parent
);
693 scan_tree(dp
->child
, &op
->dev
);
699 static int __init
scan_of_devices(void)
701 struct device_node
*root
= of_find_node_by_path("/");
702 struct platform_device
*parent
;
704 parent
= scan_one_device(root
, NULL
);
708 scan_tree(root
->child
, &parent
->dev
);
711 postcore_initcall(scan_of_devices
);
713 static int __init
of_debug(char *str
)
717 get_option(&str
, &val
);
719 of_resource_verbose
= 1;
725 __setup("of_debug=", of_debug
);