1 #include <linux/string.h>
2 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/slab.h>
7 #include <linux/errno.h>
9 #include <linux/of_device.h>
10 #include <linux/of_platform.h>
12 #include <asm/leon_amba.h>
14 #include "of_device_common.h"
18 * PCI bus specific translator
21 static int of_bus_pci_match(struct device_node *np)
23 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
24 /* Do not do PCI specific frobbing if the
25 * PCI bridge lacks a ranges property. We
26 * want to pass it through up to the next
27 * parent as-is, not with the PCI translate
28 * method which chops off the top address cell.
30 if (!of_find_property(np, "ranges", NULL))
39 static void of_bus_pci_count_cells(struct device_node *np,
40 int *addrc, int *sizec)
48 static int of_bus_pci_map(u32 *addr, const u32 *range,
49 int na, int ns, int pna)
51 u32 result[OF_MAX_ADDR_CELLS];
54 /* Check address type match */
55 if ((addr[0] ^ range[0]) & 0x03000000)
58 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
62 /* Start with the parent range base. */
63 memcpy(result, range + na, pna * 4);
65 /* Add in the child address offset, skipping high cell. */
66 for (i = 0; i < na - 1; i++)
67 result[pna - 1 - i] +=
71 memcpy(addr, result, pna * 4);
76 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
80 /* For PCI, we override whatever child busses may have used. */
82 switch((w >> 24) & 0x03) {
84 flags |= IORESOURCE_IO;
87 case 0x02: /* 32 bits */
88 case 0x03: /* 64 bits */
89 flags |= IORESOURCE_MEM;
93 flags |= IORESOURCE_PREFETCH;
97 static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
99 return IORESOURCE_MEM;
103 * AMBAPP bus specific translator
106 static int of_bus_ambapp_match(struct device_node *np)
108 return !strcmp(np->type, "ambapp");
111 static void of_bus_ambapp_count_cells(struct device_node *child,
112 int *addrc, int *sizec)
120 static int of_bus_ambapp_map(u32 *addr, const u32 *range,
121 int na, int ns, int pna)
123 return of_bus_default_map(addr, range, na, ns, pna);
126 static unsigned long of_bus_ambapp_get_flags(const u32 *addr,
129 return IORESOURCE_MEM;
133 * Array of bus specific translators
136 static struct of_bus of_busses[] = {
140 .addr_prop_name = "assigned-addresses",
141 .match = of_bus_pci_match,
142 .count_cells = of_bus_pci_count_cells,
143 .map = of_bus_pci_map,
144 .get_flags = of_bus_pci_get_flags,
149 .addr_prop_name = "reg",
150 .match = of_bus_sbus_match,
151 .count_cells = of_bus_sbus_count_cells,
152 .map = of_bus_default_map,
153 .get_flags = of_bus_sbus_get_flags,
158 .addr_prop_name = "reg",
159 .match = of_bus_ambapp_match,
160 .count_cells = of_bus_ambapp_count_cells,
161 .map = of_bus_ambapp_map,
162 .get_flags = of_bus_ambapp_get_flags,
167 .addr_prop_name = "reg",
169 .count_cells = of_bus_default_count_cells,
170 .map = of_bus_default_map,
171 .get_flags = of_bus_default_get_flags,
175 static struct of_bus *of_match_bus(struct device_node *np)
179 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
180 if (!of_busses[i].match || of_busses[i].match(np))
181 return &of_busses[i];
186 static int __init build_one_resource(struct device_node *parent,
190 int na, int ns, int pna)
196 ranges = of_get_property(parent, "ranges", &rlen);
197 if (ranges == NULL || rlen == 0) {
198 u32 result[OF_MAX_ADDR_CELLS];
201 memset(result, 0, pna * 4);
202 for (i = 0; i < na; i++)
203 result[pna - 1 - i] =
206 memcpy(addr, result, pna * 4);
210 /* Now walk through the ranges */
212 rone = na + pna + ns;
213 for (; rlen >= rone; rlen -= rone, ranges += rone) {
214 if (!bus->map(addr, ranges, na, ns, pna))
221 static int __init use_1to1_mapping(struct device_node *pp)
223 /* If we have a ranges property in the parent, use it. */
224 if (of_find_property(pp, "ranges", NULL) != NULL)
227 /* Some SBUS devices use intermediate nodes to express
228 * hierarchy within the device itself. These aren't
229 * real bus nodes, and don't have a 'ranges' property.
230 * But, we should still pass the translation work up
231 * to the SBUS itself.
233 if (!strcmp(pp->name, "dma") ||
234 !strcmp(pp->name, "espdma") ||
235 !strcmp(pp->name, "ledma") ||
236 !strcmp(pp->name, "lebuffer"))
242 static int of_resource_verbose;
244 static void __init build_device_resources(struct platform_device *op,
245 struct device *parent)
247 struct platform_device *p_op;
256 p_op = to_platform_device(parent);
257 bus = of_match_bus(p_op->dev.of_node);
258 bus->count_cells(op->dev.of_node, &na, &ns);
260 preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
261 if (!preg || num_reg == 0)
264 /* Convert to num-cells. */
267 /* Conver to num-entries. */
270 op->resource = op->archdata.resource;
271 op->num_resources = num_reg;
272 for (index = 0; index < num_reg; index++) {
273 struct resource *r = &op->resource[index];
274 u32 addr[OF_MAX_ADDR_CELLS];
275 const u32 *reg = (preg + (index * ((na + ns) * 4)));
276 struct device_node *dp = op->dev.of_node;
277 struct device_node *pp = p_op->dev.of_node;
278 struct of_bus *pbus, *dbus;
279 u64 size, result = OF_BAD_ADDR;
284 size = of_read_addr(reg + na, ns);
286 memcpy(addr, reg, na * 4);
288 flags = bus->get_flags(reg, 0);
290 if (use_1to1_mapping(pp)) {
291 result = of_read_addr(addr, na);
303 result = of_read_addr(addr, dna);
307 pbus = of_match_bus(pp);
308 pbus->count_cells(dp, &pna, &pns);
310 if (build_one_resource(dp, dbus, pbus, addr,
314 flags = pbus->get_flags(addr, flags);
322 memset(r, 0, sizeof(*r));
324 if (of_resource_verbose)
325 printk("%s reg[%d] -> %llx\n",
326 op->dev.of_node->full_name, index,
329 if (result != OF_BAD_ADDR) {
330 r->start = result & 0xffffffff;
331 r->end = result + size - 1;
332 r->flags = flags | ((result >> 32ULL) & 0xffUL);
334 r->name = op->dev.of_node->name;
338 static struct platform_device * __init scan_one_device(struct device_node *dp,
339 struct device *parent)
341 struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
342 const struct linux_prom_irqs *intr;
343 struct dev_archdata *sd;
349 sd = &op->dev.archdata;
352 op->dev.of_node = dp;
354 intr = of_get_property(dp, "intr", &len);
356 op->archdata.num_irqs = len / sizeof(struct linux_prom_irqs);
357 for (i = 0; i < op->archdata.num_irqs; i++)
358 op->archdata.irqs[i] =
359 sparc_config.build_device_irq(op, intr[i].pri);
361 const unsigned int *irq =
362 of_get_property(dp, "interrupts", &len);
365 op->archdata.num_irqs = len / sizeof(unsigned int);
366 for (i = 0; i < op->archdata.num_irqs; i++)
367 op->archdata.irqs[i] =
368 sparc_config.build_device_irq(op, irq[i]);
370 op->archdata.num_irqs = 0;
374 build_device_resources(op, parent);
376 op->dev.parent = parent;
377 op->dev.bus = &platform_bus_type;
379 dev_set_name(&op->dev, "root");
381 dev_set_name(&op->dev, "%08x", dp->phandle);
383 if (of_device_register(op)) {
384 printk("%s: Could not register of device.\n",
393 static void __init scan_tree(struct device_node *dp, struct device *parent)
396 struct platform_device *op = scan_one_device(dp, parent);
399 scan_tree(dp->child, &op->dev);
405 static int __init scan_of_devices(void)
407 struct device_node *root = of_find_node_by_path("/");
408 struct platform_device *parent;
410 parent = scan_one_device(root, NULL);
414 scan_tree(root->child, &parent->dev);
417 postcore_initcall(scan_of_devices);
419 static int __init of_debug(char *str)
423 get_option(&str, &val);
425 of_resource_verbose = 1;
429 __setup("of_debug=", of_debug);