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/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 <linux/dma-mapping.h>
14 #include <asm/leon_amba.h>
16 #include "of_device_common.h"
20 * PCI bus specific translator
23 static int of_bus_pci_match(struct device_node
*np
)
25 if (of_node_is_type(np
, "pci") || of_node_is_type(np
, "pciex")) {
26 /* Do not do PCI specific frobbing if the
27 * PCI bridge lacks a ranges property. We
28 * want to pass it through up to the next
29 * parent as-is, not with the PCI translate
30 * method which chops off the top address cell.
32 if (!of_find_property(np
, "ranges", NULL
))
41 static void of_bus_pci_count_cells(struct device_node
*np
,
42 int *addrc
, int *sizec
)
50 static int of_bus_pci_map(u32
*addr
, const u32
*range
,
51 int na
, int ns
, int pna
)
53 u32 result
[OF_MAX_ADDR_CELLS
];
56 /* Check address type match */
57 if ((addr
[0] ^ range
[0]) & 0x03000000)
60 if (of_out_of_range(addr
+ 1, range
+ 1, range
+ na
+ pna
,
64 /* Start with the parent range base. */
65 memcpy(result
, range
+ na
, pna
* 4);
67 /* Add in the child address offset, skipping high cell. */
68 for (i
= 0; i
< na
- 1; i
++)
69 result
[pna
- 1 - i
] +=
73 memcpy(addr
, result
, pna
* 4);
78 static unsigned long of_bus_pci_get_flags(const u32
*addr
, unsigned long flags
)
82 /* For PCI, we override whatever child busses may have used. */
84 switch((w
>> 24) & 0x03) {
86 flags
|= IORESOURCE_IO
;
89 case 0x02: /* 32 bits */
90 case 0x03: /* 64 bits */
91 flags
|= IORESOURCE_MEM
;
95 flags
|= IORESOURCE_PREFETCH
;
99 static unsigned long of_bus_sbus_get_flags(const u32
*addr
, unsigned long flags
)
101 return IORESOURCE_MEM
;
105 * AMBAPP bus specific translator
108 static int of_bus_ambapp_match(struct device_node
*np
)
110 return of_node_is_type(np
, "ambapp");
113 static void of_bus_ambapp_count_cells(struct device_node
*child
,
114 int *addrc
, int *sizec
)
122 static int of_bus_ambapp_map(u32
*addr
, const u32
*range
,
123 int na
, int ns
, int pna
)
125 return of_bus_default_map(addr
, range
, na
, ns
, pna
);
128 static unsigned long of_bus_ambapp_get_flags(const u32
*addr
,
131 return IORESOURCE_MEM
;
135 * Array of bus specific translators
138 static struct of_bus of_busses
[] = {
142 .addr_prop_name
= "assigned-addresses",
143 .match
= of_bus_pci_match
,
144 .count_cells
= of_bus_pci_count_cells
,
145 .map
= of_bus_pci_map
,
146 .get_flags
= of_bus_pci_get_flags
,
151 .addr_prop_name
= "reg",
152 .match
= of_bus_sbus_match
,
153 .count_cells
= of_bus_sbus_count_cells
,
154 .map
= of_bus_default_map
,
155 .get_flags
= of_bus_sbus_get_flags
,
160 .addr_prop_name
= "reg",
161 .match
= of_bus_ambapp_match
,
162 .count_cells
= of_bus_ambapp_count_cells
,
163 .map
= of_bus_ambapp_map
,
164 .get_flags
= of_bus_ambapp_get_flags
,
169 .addr_prop_name
= "reg",
171 .count_cells
= of_bus_default_count_cells
,
172 .map
= of_bus_default_map
,
173 .get_flags
= of_bus_default_get_flags
,
177 static struct of_bus
*of_match_bus(struct device_node
*np
)
181 for (i
= 0; i
< ARRAY_SIZE(of_busses
); i
++)
182 if (!of_busses
[i
].match
|| of_busses
[i
].match(np
))
183 return &of_busses
[i
];
188 static int __init
build_one_resource(struct device_node
*parent
,
192 int na
, int ns
, int pna
)
198 ranges
= of_get_property(parent
, "ranges", &rlen
);
199 if (ranges
== NULL
|| rlen
== 0) {
200 u32 result
[OF_MAX_ADDR_CELLS
];
203 memset(result
, 0, pna
* 4);
204 for (i
= 0; i
< na
; i
++)
205 result
[pna
- 1 - i
] =
208 memcpy(addr
, result
, pna
* 4);
212 /* Now walk through the ranges */
214 rone
= na
+ pna
+ ns
;
215 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
216 if (!bus
->map(addr
, ranges
, na
, ns
, pna
))
223 static int __init
use_1to1_mapping(struct device_node
*pp
)
225 /* If we have a ranges property in the parent, use it. */
226 if (of_find_property(pp
, "ranges", NULL
) != NULL
)
229 /* Some SBUS devices use intermediate nodes to express
230 * hierarchy within the device itself. These aren't
231 * real bus nodes, and don't have a 'ranges' property.
232 * But, we should still pass the translation work up
233 * to the SBUS itself.
235 if (of_node_name_eq(pp
, "dma") ||
236 of_node_name_eq(pp
, "espdma") ||
237 of_node_name_eq(pp
, "ledma") ||
238 of_node_name_eq(pp
, "lebuffer"))
244 static int of_resource_verbose
;
246 static void __init
build_device_resources(struct platform_device
*op
,
247 struct device
*parent
)
249 struct platform_device
*p_op
;
258 p_op
= to_platform_device(parent
);
259 bus
= of_match_bus(p_op
->dev
.of_node
);
260 bus
->count_cells(op
->dev
.of_node
, &na
, &ns
);
262 preg
= of_get_property(op
->dev
.of_node
, bus
->addr_prop_name
, &num_reg
);
263 if (!preg
|| num_reg
== 0)
266 /* Convert to num-cells. */
269 /* Conver to num-entries. */
272 op
->resource
= op
->archdata
.resource
;
273 op
->num_resources
= num_reg
;
274 for (index
= 0; index
< num_reg
; index
++) {
275 struct resource
*r
= &op
->resource
[index
];
276 u32 addr
[OF_MAX_ADDR_CELLS
];
277 const u32
*reg
= (preg
+ (index
* ((na
+ ns
) * 4)));
278 struct device_node
*dp
= op
->dev
.of_node
;
279 struct device_node
*pp
= p_op
->dev
.of_node
;
280 struct of_bus
*pbus
, *dbus
;
281 u64 size
, result
= OF_BAD_ADDR
;
286 size
= of_read_addr(reg
+ na
, ns
);
288 memcpy(addr
, reg
, na
* 4);
290 flags
= bus
->get_flags(reg
, 0);
292 if (use_1to1_mapping(pp
)) {
293 result
= of_read_addr(addr
, na
);
305 result
= of_read_addr(addr
, dna
);
309 pbus
= of_match_bus(pp
);
310 pbus
->count_cells(dp
, &pna
, &pns
);
312 if (build_one_resource(dp
, dbus
, pbus
, addr
,
316 flags
= pbus
->get_flags(addr
, flags
);
324 memset(r
, 0, sizeof(*r
));
326 if (of_resource_verbose
)
327 printk("%pOF reg[%d] -> %llx\n",
328 op
->dev
.of_node
, index
,
331 if (result
!= OF_BAD_ADDR
) {
332 r
->start
= result
& 0xffffffff;
333 r
->end
= result
+ size
- 1;
334 r
->flags
= flags
| ((result
>> 32ULL) & 0xffUL
);
336 r
->name
= op
->dev
.of_node
->full_name
;
340 static struct platform_device
* __init
scan_one_device(struct device_node
*dp
,
341 struct device
*parent
)
343 struct platform_device
*op
= kzalloc(sizeof(*op
), GFP_KERNEL
);
344 const struct linux_prom_irqs
*intr
;
345 struct dev_archdata
*sd
;
351 sd
= &op
->dev
.archdata
;
354 op
->dev
.of_node
= dp
;
356 intr
= of_get_property(dp
, "intr", &len
);
358 op
->archdata
.num_irqs
= len
/ sizeof(struct linux_prom_irqs
);
359 for (i
= 0; i
< op
->archdata
.num_irqs
; i
++)
360 op
->archdata
.irqs
[i
] =
361 sparc_config
.build_device_irq(op
, intr
[i
].pri
);
363 const unsigned int *irq
=
364 of_get_property(dp
, "interrupts", &len
);
367 op
->archdata
.num_irqs
= len
/ sizeof(unsigned int);
368 for (i
= 0; i
< op
->archdata
.num_irqs
; i
++)
369 op
->archdata
.irqs
[i
] =
370 sparc_config
.build_device_irq(op
, irq
[i
]);
372 op
->archdata
.num_irqs
= 0;
376 build_device_resources(op
, parent
);
378 op
->dev
.parent
= parent
;
379 op
->dev
.bus
= &platform_bus_type
;
381 dev_set_name(&op
->dev
, "root");
383 dev_set_name(&op
->dev
, "%08x", dp
->phandle
);
385 op
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
386 op
->dev
.dma_mask
= &op
->dev
.coherent_dma_mask
;
388 if (of_device_register(op
)) {
389 printk("%pOF: Could not register of device.\n", dp
);
397 static void __init
scan_tree(struct device_node
*dp
, struct device
*parent
)
400 struct platform_device
*op
= scan_one_device(dp
, parent
);
403 scan_tree(dp
->child
, &op
->dev
);
409 static int __init
scan_of_devices(void)
411 struct device_node
*root
= of_find_node_by_path("/");
412 struct platform_device
*parent
;
414 parent
= scan_one_device(root
, NULL
);
418 scan_tree(root
->child
, &parent
->dev
);
421 postcore_initcall(scan_of_devices
);
423 static int __init
of_debug(char *str
)
427 get_option(&str
, &val
);
429 of_resource_verbose
= 1;
433 __setup("of_debug=", of_debug
);