1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/string.h>
3 #include <linux/kernel.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>
11 #include <linux/of_device.h>
12 #include <linux/of_platform.h>
13 #include <asm/spitfire.h>
15 #include "of_device_common.h"
17 void __iomem
*of_ioremap(struct resource
*res
, unsigned long offset
, unsigned long size
, char *name
)
19 unsigned long ret
= res
->start
+ offset
;
22 if (res
->flags
& IORESOURCE_MEM
)
23 r
= request_mem_region(ret
, size
, name
);
25 r
= request_region(ret
, size
, name
);
29 return (void __iomem
*) ret
;
31 EXPORT_SYMBOL(of_ioremap
);
33 void of_iounmap(struct resource
*res
, void __iomem
*base
, unsigned long size
)
35 if (res
->flags
& IORESOURCE_MEM
)
36 release_mem_region((unsigned long) base
, size
);
38 release_region((unsigned long) base
, size
);
40 EXPORT_SYMBOL(of_iounmap
);
43 * PCI bus specific translator
46 static int of_bus_pci_match(struct device_node
*np
)
48 if (!strcmp(np
->name
, "pci")) {
49 const char *model
= of_get_property(np
, "model", NULL
);
51 if (model
&& !strcmp(model
, "SUNW,simba"))
54 /* Do not do PCI specific frobbing if the
55 * PCI bridge lacks a ranges property. We
56 * want to pass it through up to the next
57 * parent as-is, not with the PCI translate
58 * method which chops off the top address cell.
60 if (!of_find_property(np
, "ranges", NULL
))
69 static int of_bus_simba_match(struct device_node
*np
)
71 const char *model
= of_get_property(np
, "model", NULL
);
73 if (model
&& !strcmp(model
, "SUNW,simba"))
76 /* Treat PCI busses lacking ranges property just like
79 if (!strcmp(np
->name
, "pci")) {
80 if (!of_find_property(np
, "ranges", NULL
))
87 static int of_bus_simba_map(u32
*addr
, const u32
*range
,
88 int na
, int ns
, int pna
)
93 static void of_bus_pci_count_cells(struct device_node
*np
,
94 int *addrc
, int *sizec
)
102 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
103 int na
, int ns
, int pna
)
105 u32 result
[OF_MAX_ADDR_CELLS
];
108 /* Check address type match */
109 if (!((addr
[0] ^ range
[0]) & 0x03000000))
112 /* Special exception, we can map a 64-bit address into
115 if ((addr
[0] & 0x03000000) == 0x03000000 &&
116 (range
[0] & 0x03000000) == 0x02000000)
122 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
126 /* Start with the parent range base. */
127 memcpy(result
, range
+ na
, pna
* 4);
129 /* Add in the child address offset, skipping high cell. */
130 for (i
= 0; i
< na
- 1; i
++)
131 result
[pna
- 1 - i
] +=
135 memcpy(addr
, result
, pna
* 4);
140 static unsigned long of_bus_pci_get_flags(const u32
*addr
, unsigned long flags
)
144 /* For PCI, we override whatever child busses may have used. */
146 switch((w
>> 24) & 0x03) {
148 flags
|= IORESOURCE_IO
;
151 case 0x02: /* 32 bits */
152 case 0x03: /* 64 bits */
153 flags
|= IORESOURCE_MEM
;
157 flags
|= IORESOURCE_PREFETCH
;
162 * FHC/Central bus specific translator.
164 * This is just needed to hard-code the address and size cell
165 * counts. 'fhc' and 'central' nodes lack the #address-cells and
166 * #size-cells properties, and if you walk to the root on such
167 * Enterprise boxes all you'll get is a #size-cells of 2 which is
168 * not what we want to use.
170 static int of_bus_fhc_match(struct device_node
*np
)
172 return !strcmp(np
->name
, "fhc") ||
173 !strcmp(np
->name
, "central");
176 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
179 * Array of bus specific translators
182 static struct of_bus of_busses
[] = {
186 .addr_prop_name
= "assigned-addresses",
187 .match
= of_bus_pci_match
,
188 .count_cells
= of_bus_pci_count_cells
,
189 .map
= of_bus_pci_map
,
190 .get_flags
= of_bus_pci_get_flags
,
195 .addr_prop_name
= "assigned-addresses",
196 .match
= of_bus_simba_match
,
197 .count_cells
= of_bus_pci_count_cells
,
198 .map
= of_bus_simba_map
,
199 .get_flags
= of_bus_pci_get_flags
,
204 .addr_prop_name
= "reg",
205 .match
= of_bus_sbus_match
,
206 .count_cells
= of_bus_sbus_count_cells
,
207 .map
= of_bus_default_map
,
208 .get_flags
= of_bus_default_get_flags
,
213 .addr_prop_name
= "reg",
214 .match
= of_bus_fhc_match
,
215 .count_cells
= of_bus_fhc_count_cells
,
216 .map
= of_bus_default_map
,
217 .get_flags
= of_bus_default_get_flags
,
222 .addr_prop_name
= "reg",
224 .count_cells
= of_bus_default_count_cells
,
225 .map
= of_bus_default_map
,
226 .get_flags
= of_bus_default_get_flags
,
230 static struct of_bus
*of_match_bus(struct device_node
*np
)
234 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
235 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
236 return &of_busses
[i
];
241 static int __init
build_one_resource(struct device_node
*parent
,
245 int na
, int ns
, int pna
)
250 ranges
= of_get_property(parent
, "ranges", &rlen
);
251 if (ranges
== NULL
|| rlen
== 0) {
252 u32 result
[OF_MAX_ADDR_CELLS
];
255 memset(result
, 0, pna
* 4);
256 for (i
= 0; i
< na
; i
++)
257 result
[pna
- 1 - i
] =
260 memcpy(addr
, result
, pna
* 4);
264 /* Now walk through the ranges */
266 rone
= na
+ pna
+ ns
;
267 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
268 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
272 /* When we miss an I/O space match on PCI, just pass it up
273 * to the next PCI bridge and/or controller.
275 if (!strcmp(bus
->name
, "pci") &&
276 (addr
[0] & 0x03000000) == 0x01000000)
282 static int __init
use_1to1_mapping(struct device_node
*pp
)
284 /* If we have a ranges property in the parent, use it. */
285 if (of_find_property(pp
, "ranges", NULL
) != NULL
)
288 /* If the parent is the dma node of an ISA bus, pass
289 * the translation up to the root.
291 * Some SBUS devices use intermediate nodes to express
292 * hierarchy within the device itself. These aren't
293 * real bus nodes, and don't have a 'ranges' property.
294 * But, we should still pass the translation work up
295 * to the SBUS itself.
297 if (!strcmp(pp
->name
, "dma") ||
298 !strcmp(pp
->name
, "espdma") ||
299 !strcmp(pp
->name
, "ledma") ||
300 !strcmp(pp
->name
, "lebuffer"))
303 /* Similarly for all PCI bridges, if we get this far
304 * it lacks a ranges property, and this will include
307 if (!strcmp(pp
->name
, "pci"))
313 static int of_resource_verbose
;
315 static void __init
build_device_resources(struct platform_device
*op
,
316 struct device
*parent
)
318 struct platform_device
*p_op
;
327 p_op
= to_platform_device(parent
);
328 bus
= of_match_bus(p_op
->dev
.of_node
);
329 bus
->count_cells(op
->dev
.of_node
, &na
, &ns
);
331 preg
= of_get_property(op
->dev
.of_node
, bus
->addr_prop_name
, &num_reg
);
332 if (!preg
|| num_reg
== 0)
335 /* Convert to num-cells. */
338 /* Convert to num-entries. */
341 /* Prevent overrunning the op->resources[] array. */
342 if (num_reg
> PROMREG_MAX
) {
343 printk(KERN_WARNING
"%s: Too many regs (%d), "
345 op
->dev
.of_node
->full_name
, num_reg
, PROMREG_MAX
);
346 num_reg
= PROMREG_MAX
;
349 op
->resource
= op
->archdata
.resource
;
350 op
->num_resources
= num_reg
;
351 for (index
= 0; index
< num_reg
; index
++) {
352 struct resource
*r
= &op
->resource
[index
];
353 u32 addr
[OF_MAX_ADDR_CELLS
];
354 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
355 struct device_node
*dp
= op
->dev
.of_node
;
356 struct device_node
*pp
= p_op
->dev
.of_node
;
357 struct of_bus
*pbus
, *dbus
;
358 u64 size
, result
= OF_BAD_ADDR
;
363 size
= of_read_addr(reg
+ na
, ns
);
364 memcpy(addr
, reg
, na
* 4);
366 flags
= bus
->get_flags(addr
, 0);
368 if (use_1to1_mapping(pp
)) {
369 result
= of_read_addr(addr
, na
);
381 result
= of_read_addr(addr
, dna
);
385 pbus
= of_match_bus(pp
);
386 pbus
->count_cells(dp
, &pna
, &pns
);
388 if (build_one_resource(dp
, dbus
, pbus
, addr
,
392 flags
= pbus
->get_flags(addr
, flags
);
400 memset(r
, 0, sizeof(*r
));
402 if (of_resource_verbose
)
403 printk("%s reg[%d] -> %llx\n",
404 op
->dev
.of_node
->full_name
, index
,
407 if (result
!= OF_BAD_ADDR
) {
408 if (tlb_type
== hypervisor
)
409 result
&= 0x0fffffffffffffffUL
;
412 r
->end
= result
+ size
- 1;
415 r
->name
= op
->dev
.of_node
->name
;
419 static struct device_node
* __init
420 apply_interrupt_map(struct device_node
*dp
, struct device_node
*pp
,
421 const u32
*imap
, int imlen
, const u32
*imask
,
424 struct device_node
*cp
;
425 unsigned int irq
= *irq_p
;
431 bus
= of_match_bus(pp
);
432 bus
->count_cells(dp
, &na
, NULL
);
434 reg
= of_get_property(dp
, "reg", &num_reg
);
435 if (!reg
|| !num_reg
)
438 imlen
/= ((na
+ 3) * 4);
440 for (i
= 0; i
< imlen
; i
++) {
443 for (j
= 0; j
< na
; j
++) {
444 if ((reg
[j
] & imask
[j
]) != imap
[j
])
447 if (imap
[na
] == irq
) {
448 handle
= imap
[na
+ 1];
457 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
458 * properties that do not include the on-board device
459 * interrupts. Instead, the device's 'interrupts' property
460 * is already a fully specified INO value.
462 * Handle this by deciding that, if we didn't get a
463 * match in the parent's 'interrupt-map', and the
464 * parent is an IRQ translator, then use the parent as
465 * our IRQ controller.
474 cp
= of_find_node_by_phandle(handle
);
479 static unsigned int __init
pci_irq_swizzle(struct device_node
*dp
,
480 struct device_node
*pp
,
483 const struct linux_prom_pci_registers
*regs
;
484 unsigned int bus
, devfn
, slot
, ret
;
486 if (irq
< 1 || irq
> 4)
489 regs
= of_get_property(dp
, "reg", NULL
);
493 bus
= (regs
->phys_hi
>> 16) & 0xff;
494 devfn
= (regs
->phys_hi
>> 8) & 0xff;
495 slot
= (devfn
>> 3) & 0x1f;
498 /* Derived from Table 8-3, U2P User's Manual. This branch
499 * is handling a PCI controller that lacks a proper set of
500 * interrupt-map and interrupt-map-mask properties. The
501 * Ultra-E450 is one example.
503 * The bit layout is BSSLL, where:
504 * B: 0 on bus A, 1 on bus B
505 * D: 2-bit slot number, derived from PCI device number as
506 * (dev - 1) for bus A, or (dev - 2) for bus B
507 * L: 2-bit line number
512 slot
= (slot
- 1) << 2;
516 slot
= (slot
- 2) << 2;
520 ret
= (bus
| slot
| irq
);
522 /* Going through a PCI-PCI bridge that lacks a set of
523 * interrupt-map and interrupt-map-mask properties.
525 ret
= ((irq
- 1 + (slot
& 3)) & 3) + 1;
531 static int of_irq_verbose
;
533 static unsigned int __init
build_one_device_irq(struct platform_device
*op
,
534 struct device
*parent
,
537 struct device_node
*dp
= op
->dev
.of_node
;
538 struct device_node
*pp
, *ip
;
539 unsigned int orig_irq
= irq
;
542 if (irq
== 0xffffffff)
546 irq
= dp
->irq_trans
->irq_build(dp
, irq
,
547 dp
->irq_trans
->data
);
550 printk("%s: direct translate %x --> %x\n",
551 dp
->full_name
, orig_irq
, irq
);
556 /* Something more complicated. Walk up to the root, applying
557 * interrupt-map or bus specific translations, until we hit
560 * If we hit a bus type or situation we cannot handle, we
561 * stop and assume that the original IRQ number was in a
562 * format which has special meaning to it's immediate parent.
567 const void *imap
, *imsk
;
570 imap
= of_get_property(pp
, "interrupt-map", &imlen
);
571 imsk
= of_get_property(pp
, "interrupt-map-mask", NULL
);
573 struct device_node
*iret
;
574 int this_orig_irq
= irq
;
576 iret
= apply_interrupt_map(dp
, pp
,
581 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
582 op
->dev
.of_node
->full_name
,
583 pp
->full_name
, this_orig_irq
,
584 of_node_full_name(iret
), irq
);
589 if (iret
->irq_trans
) {
594 if (!strcmp(pp
->name
, "pci")) {
595 unsigned int this_orig_irq
= irq
;
597 irq
= pci_irq_swizzle(dp
, pp
, irq
);
599 printk("%s: PCI swizzle [%s] "
601 op
->dev
.of_node
->full_name
,
602 pp
->full_name
, this_orig_irq
,
618 irq
= ip
->irq_trans
->irq_build(op
->dev
.of_node
, irq
,
619 ip
->irq_trans
->data
);
621 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
622 op
->dev
.of_node
->full_name
, ip
->full_name
, orig_irq
, irq
);
625 nid
= of_node_to_nid(dp
);
629 cpumask_copy(&numa_mask
, cpumask_of_node(nid
));
630 irq_set_affinity(irq
, &numa_mask
);
636 static struct platform_device
* __init
scan_one_device(struct device_node
*dp
,
637 struct device
*parent
)
639 struct platform_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
640 const unsigned int *irq
;
641 struct dev_archdata
*sd
;
647 sd
= &op
->dev
.archdata
;
650 op
->dev
.of_node
= dp
;
652 irq
= of_get_property(dp
, "interrupts", &len
);
654 op
->archdata
.num_irqs
= len
/ 4;
656 /* Prevent overrunning the op->irqs[] array. */
657 if (op
->archdata
.num_irqs
> PROMINTR_MAX
) {
658 printk(KERN_WARNING
"%s: Too many irqs (%d), "
660 dp
->full_name
, op
->archdata
.num_irqs
, PROMINTR_MAX
);
661 op
->archdata
.num_irqs
= PROMINTR_MAX
;
663 memcpy(op
->archdata
.irqs
, irq
, op
->archdata
.num_irqs
* 4);
665 op
->archdata
.num_irqs
= 0;
668 build_device_resources(op
, parent
);
669 for (i
= 0; i
< op
->archdata
.num_irqs
; i
++)
670 op
->archdata
.irqs
[i
] = build_one_device_irq(op
, parent
, op
->archdata
.irqs
[i
]);
672 op
->dev
.parent
= parent
;
673 op
->dev
.bus
= &platform_bus_type
;
675 dev_set_name(&op
->dev
, "root");
677 dev_set_name(&op
->dev
, "%08x", dp
->phandle
);
679 if (of_device_register(op
)) {
680 printk("%s: Could not register of device.\n",
689 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
692 struct platform_device
*op
= scan_one_device(dp
, parent
);
695 scan_tree(dp
->child
, &op
->dev
);
701 static int __init
scan_of_devices(void)
703 struct device_node
*root
= of_find_node_by_path("/");
704 struct platform_device
*parent
;
706 parent
= scan_one_device(root
, NULL
);
710 scan_tree(root
->child
, &parent
->dev
);
713 postcore_initcall(scan_of_devices
);
715 static int __init
of_debug(char *str
)
719 get_option(&str
, &val
);
721 of_resource_verbose
= 1;
727 __setup("of_debug=", of_debug
);