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>
13 #include <asm/leon_amba.h>
15 #include "of_device_common.h"
19 * PCI bus specific translator
22 static int of_bus_pci_match(struct device_node *np)
24 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
25 /* Do not do PCI specific frobbing if the
26 * PCI bridge lacks a ranges property. We
27 * want to pass it through up to the next
28 * parent as-is, not with the PCI translate
29 * method which chops off the top address cell.
31 if (!of_find_property(np, "ranges", NULL))
40 static void of_bus_pci_count_cells(struct device_node *np,
41 int *addrc, int *sizec)
49 static int of_bus_pci_map(u32 *addr, const u32 *range,
50 int na, int ns, int pna)
52 u32 result[OF_MAX_ADDR_CELLS];
55 /* Check address type match */
56 if ((addr[0] ^ range[0]) & 0x03000000)
59 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
63 /* Start with the parent range base. */
64 memcpy(result, range + na, pna * 4);
66 /* Add in the child address offset, skipping high cell. */
67 for (i = 0; i < na - 1; i++)
68 result[pna - 1 - i] +=
72 memcpy(addr, result, pna * 4);
77 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
81 /* For PCI, we override whatever child busses may have used. */
83 switch((w >> 24) & 0x03) {
85 flags |= IORESOURCE_IO;
88 case 0x02: /* 32 bits */
89 case 0x03: /* 64 bits */
90 flags |= IORESOURCE_MEM;
94 flags |= IORESOURCE_PREFETCH;
98 static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
100 return IORESOURCE_MEM;
104 * AMBAPP bus specific translator
107 static int of_bus_ambapp_match(struct device_node *np)
109 return !strcmp(np->type, "ambapp");
112 static void of_bus_ambapp_count_cells(struct device_node *child,
113 int *addrc, int *sizec)
121 static int of_bus_ambapp_map(u32 *addr, const u32 *range,
122 int na, int ns, int pna)
124 return of_bus_default_map(addr, range, na, ns, pna);
127 static unsigned long of_bus_ambapp_get_flags(const u32 *addr,
130 return IORESOURCE_MEM;
134 * Array of bus specific translators
137 static struct of_bus of_busses[] = {
141 .addr_prop_name = "assigned-addresses",
142 .match = of_bus_pci_match,
143 .count_cells = of_bus_pci_count_cells,
144 .map = of_bus_pci_map,
145 .get_flags = of_bus_pci_get_flags,
150 .addr_prop_name = "reg",
151 .match = of_bus_sbus_match,
152 .count_cells = of_bus_sbus_count_cells,
153 .map = of_bus_default_map,
154 .get_flags = of_bus_sbus_get_flags,
159 .addr_prop_name = "reg",
160 .match = of_bus_ambapp_match,
161 .count_cells = of_bus_ambapp_count_cells,
162 .map = of_bus_ambapp_map,
163 .get_flags = of_bus_ambapp_get_flags,
168 .addr_prop_name = "reg",
170 .count_cells = of_bus_default_count_cells,
171 .map = of_bus_default_map,
172 .get_flags = of_bus_default_get_flags,
176 static struct of_bus *of_match_bus(struct device_node *np)
180 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
181 if (!of_busses[i].match || of_busses[i].match(np))
182 return &of_busses[i];
187 static int __init build_one_resource(struct device_node *parent,
191 int na, int ns, int pna)
197 ranges = of_get_property(parent, "ranges", &rlen);
198 if (ranges == NULL || rlen == 0) {
199 u32 result[OF_MAX_ADDR_CELLS];
202 memset(result, 0, pna * 4);
203 for (i = 0; i < na; i++)
204 result[pna - 1 - i] =
207 memcpy(addr, result, pna * 4);
211 /* Now walk through the ranges */
213 rone = na + pna + ns;
214 for (; rlen >= rone; rlen -= rone, ranges += rone) {
215 if (!bus->map(addr, ranges, na, ns, pna))
222 static int __init use_1to1_mapping(struct device_node *pp)
224 /* If we have a ranges property in the parent, use it. */
225 if (of_find_property(pp, "ranges", NULL) != NULL)
228 /* Some SBUS devices use intermediate nodes to express
229 * hierarchy within the device itself. These aren't
230 * real bus nodes, and don't have a 'ranges' property.
231 * But, we should still pass the translation work up
232 * to the SBUS itself.
234 if (!strcmp(pp->name, "dma") ||
235 !strcmp(pp->name, "espdma") ||
236 !strcmp(pp->name, "ledma") ||
237 !strcmp(pp->name, "lebuffer"))
243 static int of_resource_verbose;
245 static void __init build_device_resources(struct platform_device *op,
246 struct device *parent)
248 struct platform_device *p_op;
257 p_op = to_platform_device(parent);
258 bus = of_match_bus(p_op->dev.of_node);
259 bus->count_cells(op->dev.of_node, &na, &ns);
261 preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
262 if (!preg || num_reg == 0)
265 /* Convert to num-cells. */
268 /* Conver to num-entries. */
271 op->resource = op->archdata.resource;
272 op->num_resources = num_reg;
273 for (index = 0; index < num_reg; index++) {
274 struct resource *r = &op->resource[index];
275 u32 addr[OF_MAX_ADDR_CELLS];
276 const u32 *reg = (preg + (index * ((na + ns) * 4)));
277 struct device_node *dp = op->dev.of_node;
278 struct device_node *pp = p_op->dev.of_node;
279 struct of_bus *pbus, *dbus;
280 u64 size, result = OF_BAD_ADDR;
285 size = of_read_addr(reg + na, ns);
287 memcpy(addr, reg, na * 4);
289 flags = bus->get_flags(reg, 0);
291 if (use_1to1_mapping(pp)) {
292 result = of_read_addr(addr, na);
304 result = of_read_addr(addr, dna);
308 pbus = of_match_bus(pp);
309 pbus->count_cells(dp, &pna, &pns);
311 if (build_one_resource(dp, dbus, pbus, addr,
315 flags = pbus->get_flags(addr, flags);
323 memset(r, 0, sizeof(*r));
325 if (of_resource_verbose)
326 printk("%s reg[%d] -> %llx\n",
327 op->dev.of_node->full_name, index,
330 if (result != OF_BAD_ADDR) {
331 r->start = result & 0xffffffff;
332 r->end = result + size - 1;
333 r->flags = flags | ((result >> 32ULL) & 0xffUL);
335 r->name = op->dev.of_node->name;
339 static struct platform_device * __init scan_one_device(struct device_node *dp,
340 struct device *parent)
342 struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
343 const struct linux_prom_irqs *intr;
344 struct dev_archdata *sd;
350 sd = &op->dev.archdata;
353 op->dev.of_node = dp;
355 intr = of_get_property(dp, "intr", &len);
357 op->archdata.num_irqs = len / sizeof(struct linux_prom_irqs);
358 for (i = 0; i < op->archdata.num_irqs; i++)
359 op->archdata.irqs[i] =
360 sparc_config.build_device_irq(op, intr[i].pri);
362 const unsigned int *irq =
363 of_get_property(dp, "interrupts", &len);
366 op->archdata.num_irqs = len / sizeof(unsigned int);
367 for (i = 0; i < op->archdata.num_irqs; i++)
368 op->archdata.irqs[i] =
369 sparc_config.build_device_irq(op, irq[i]);
371 op->archdata.num_irqs = 0;
375 build_device_resources(op, parent);
377 op->dev.parent = parent;
378 op->dev.bus = &platform_bus_type;
380 dev_set_name(&op->dev, "root");
382 dev_set_name(&op->dev, "%08x", dp->phandle);
384 if (of_device_register(op)) {
385 printk("%s: Could not register of device.\n",
394 static void __init scan_tree(struct device_node *dp, struct device *parent)
397 struct platform_device *op = scan_one_device(dp, parent);
400 scan_tree(dp->child, &op->dev);
406 static int __init scan_of_devices(void)
408 struct device_node *root = of_find_node_by_path("/");
409 struct platform_device *parent;
411 parent = scan_one_device(root, NULL);
415 scan_tree(root->child, &parent->dev);
418 postcore_initcall(scan_of_devices);
420 static int __init of_debug(char *str)
424 get_option(&str, &val);
426 of_resource_verbose = 1;
430 __setup("of_debug=", of_debug);