blob: 09138d403c7fd9ffbf1e31e69494f88c2870921f [file] [log] [blame]
David S. Millerfd531432006-06-23 15:55:17 -07001#include <linux/string.h>
2#include <linux/kernel.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +10003#include <linux/of.h>
David S. Millerfd531432006-06-23 15:55:17 -07004#include <linux/init.h>
5#include <linux/module.h>
6#include <linux/mod_devicetable.h>
7#include <linux/slab.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +10008#include <linux/errno.h>
Robert Reifc9f5b7e2009-06-04 02:00:02 -07009#include <linux/irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100010#include <linux/of_device.h>
11#include <linux/of_platform.h>
Konrad Eiselee63829d2009-08-17 00:13:32 +000012#include <asm/leon.h>
13#include <asm/leon_amba.h>
David S. Millerfd531432006-06-23 15:55:17 -070014
Robert Reifc9f5b7e2009-06-04 02:00:02 -070015#include "of_device_common.h"
David S. Millercf44bbc2006-06-29 14:34:50 -070016
David S. Millercf44bbc2006-06-29 14:34:50 -070017/*
18 * PCI bus specific translator
19 */
20
21static int of_bus_pci_match(struct device_node *np)
22{
David S. Millera83f9822006-07-12 23:19:31 -070023 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.
29 */
30 if (!of_find_property(np, "ranges", NULL))
31 return 0;
32
33 return 1;
34 }
35
36 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -070037}
38
39static void of_bus_pci_count_cells(struct device_node *np,
40 int *addrc, int *sizec)
41{
42 if (addrc)
43 *addrc = 3;
44 if (sizec)
45 *sizec = 2;
46}
47
David S. Millera83f9822006-07-12 23:19:31 -070048static int of_bus_pci_map(u32 *addr, const u32 *range,
49 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -070050{
David S. Millera83f9822006-07-12 23:19:31 -070051 u32 result[OF_MAX_ADDR_CELLS];
52 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -070053
54 /* Check address type match */
55 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -070056 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -070057
David S. Millera83f9822006-07-12 23:19:31 -070058 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
59 na - 1, ns))
60 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -070061
David S. Millera83f9822006-07-12 23:19:31 -070062 /* Start with the parent range base. */
63 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -070064
David S. Millera83f9822006-07-12 23:19:31 -070065 /* Add in the child address offset, skipping high cell. */
66 for (i = 0; i < na - 1; i++)
67 result[pna - 1 - i] +=
68 (addr[na - 1 - i] -
69 range[na - 1 - i]);
70
71 memcpy(addr, result, pna * 4);
72
73 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -070074}
75
David S. Millere3c71a32008-08-28 21:02:58 -070076static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -070077{
David S. Millercf44bbc2006-06-29 14:34:50 -070078 u32 w = addr[0];
79
David S. Millere3c71a32008-08-28 21:02:58 -070080 /* For PCI, we override whatever child busses may have used. */
81 flags = 0;
David S. Millercf44bbc2006-06-29 14:34:50 -070082 switch((w >> 24) & 0x03) {
83 case 0x01:
84 flags |= IORESOURCE_IO;
David S. Millere3c71a32008-08-28 21:02:58 -070085 break;
86
David S. Millercf44bbc2006-06-29 14:34:50 -070087 case 0x02: /* 32 bits */
88 case 0x03: /* 64 bits */
89 flags |= IORESOURCE_MEM;
David S. Millere3c71a32008-08-28 21:02:58 -070090 break;
David S. Millercf44bbc2006-06-29 14:34:50 -070091 }
92 if (w & 0x40000000)
93 flags |= IORESOURCE_PREFETCH;
94 return flags;
95}
96
David S. Millerbdba4d62008-09-10 23:38:51 -070097static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -070098{
99 return IORESOURCE_MEM;
100}
101
Konrad Eiselee63829d2009-08-17 00:13:32 +0000102 /*
103 * AMBAPP bus specific translator
104 */
105
106static int of_bus_ambapp_match(struct device_node *np)
107{
108 return !strcmp(np->name, "ambapp");
109}
110
111static void of_bus_ambapp_count_cells(struct device_node *child,
112 int *addrc, int *sizec)
113{
114 if (addrc)
115 *addrc = 1;
116 if (sizec)
117 *sizec = 1;
118}
119
120static int of_bus_ambapp_map(u32 *addr, const u32 *range,
121 int na, int ns, int pna)
122{
123 return of_bus_default_map(addr, range, na, ns, pna);
124}
125
126static unsigned long of_bus_ambapp_get_flags(const u32 *addr,
127 unsigned long flags)
128{
129 return IORESOURCE_MEM;
130}
David S. Millercf44bbc2006-06-29 14:34:50 -0700131
132/*
133 * Array of bus specific translators
134 */
135
136static struct of_bus of_busses[] = {
137 /* PCI */
138 {
139 .name = "pci",
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,
David S. Millercf44bbc2006-06-29 14:34:50 -0700144 .get_flags = of_bus_pci_get_flags,
145 },
146 /* SBUS */
147 {
148 .name = "sbus",
149 .addr_prop_name = "reg",
150 .match = of_bus_sbus_match,
151 .count_cells = of_bus_sbus_count_cells,
Robert Reifc9f5b7e2009-06-04 02:00:02 -0700152 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700153 .get_flags = of_bus_sbus_get_flags,
154 },
Konrad Eiselee63829d2009-08-17 00:13:32 +0000155 /* AMBA */
156 {
157 .name = "ambapp",
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,
163 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700164 /* Default */
165 {
166 .name = "default",
167 .addr_prop_name = "reg",
168 .match = NULL,
169 .count_cells = of_bus_default_count_cells,
170 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700171 .get_flags = of_bus_default_get_flags,
172 },
173};
174
175static struct of_bus *of_match_bus(struct device_node *np)
176{
177 int i;
178
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];
182 BUG();
183 return NULL;
184}
185
186static int __init build_one_resource(struct device_node *parent,
187 struct of_bus *bus,
188 struct of_bus *pbus,
189 u32 *addr,
190 int na, int ns, int pna)
191{
Stephen Rothwell8271f042007-03-29 00:47:23 -0700192 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700193 unsigned int rlen;
194 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700195
196 ranges = of_get_property(parent, "ranges", &rlen);
197 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700198 u32 result[OF_MAX_ADDR_CELLS];
199 int i;
200
201 memset(result, 0, pna * 4);
202 for (i = 0; i < na; i++)
203 result[pna - 1 - i] =
204 addr[na - 1 - i];
205
206 memcpy(addr, result, pna * 4);
207 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700208 }
209
210 /* Now walk through the ranges */
211 rlen /= 4;
212 rone = na + pna + ns;
213 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700214 if (!bus->map(addr, ranges, na, ns, pna))
215 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700216 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700217
David S. Millera83f9822006-07-12 23:19:31 -0700218 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700219}
220
David S. Miller52802672008-09-03 02:04:41 -0700221static int __init use_1to1_mapping(struct device_node *pp)
222{
223 /* If we have a ranges property in the parent, use it. */
224 if (of_find_property(pp, "ranges", NULL) != NULL)
225 return 0;
226
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.
232 */
233 if (!strcmp(pp->name, "dma") ||
234 !strcmp(pp->name, "espdma") ||
235 !strcmp(pp->name, "ledma") ||
236 !strcmp(pp->name, "lebuffer"))
237 return 0;
238
239 return 1;
240}
241
David S. Millera83f9822006-07-12 23:19:31 -0700242static int of_resource_verbose;
243
David S. Millercf44bbc2006-06-29 14:34:50 -0700244static void __init build_device_resources(struct of_device *op,
245 struct device *parent)
246{
247 struct of_device *p_op;
248 struct of_bus *bus;
249 int na, ns;
250 int index, num_reg;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700251 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700252
253 if (!parent)
254 return;
255
256 p_op = to_of_device(parent);
257 bus = of_match_bus(p_op->node);
258 bus->count_cells(op->node, &na, &ns);
259
260 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
261 if (!preg || num_reg == 0)
262 return;
263
264 /* Convert to num-cells. */
265 num_reg /= 4;
266
267 /* Conver to num-entries. */
268 num_reg /= na + ns;
269
270 for (index = 0; index < num_reg; index++) {
271 struct resource *r = &op->resource[index];
272 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell8271f042007-03-29 00:47:23 -0700273 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700274 struct device_node *dp = op->node;
275 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800276 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700277 u64 size, result = OF_BAD_ADDR;
278 unsigned long flags;
279 int dna, dns;
280 int pna, pns;
281
282 size = of_read_addr(reg + na, ns);
David S. Millercf44bbc2006-06-29 14:34:50 -0700283
284 memcpy(addr, reg, na * 4);
285
David S. Millere3c71a32008-08-28 21:02:58 -0700286 flags = bus->get_flags(reg, 0);
287
David S. Miller52802672008-09-03 02:04:41 -0700288 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700289 result = of_read_addr(addr, na);
290 goto build_res;
291 }
292
293 dna = na;
294 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800295 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700296
297 while (1) {
298 dp = pp;
299 pp = dp->parent;
300 if (!pp) {
301 result = of_read_addr(addr, dna);
302 break;
303 }
304
305 pbus = of_match_bus(pp);
306 pbus->count_cells(dp, &pna, &pns);
307
David S. Millerb85cdd42007-02-28 23:20:12 -0800308 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700309 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700310 break;
311
David S. Millere3c71a32008-08-28 21:02:58 -0700312 flags = pbus->get_flags(addr, flags);
313
David S. Millercf44bbc2006-06-29 14:34:50 -0700314 dna = pna;
315 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800316 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700317 }
318
319 build_res:
320 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700321
322 if (of_resource_verbose)
323 printk("%s reg[%d] -> %llx\n",
324 op->node->full_name, index,
325 result);
326
David S. Millercf44bbc2006-06-29 14:34:50 -0700327 if (result != OF_BAD_ADDR) {
David S. Miller95714e12006-06-29 14:35:14 -0700328 r->start = result & 0xffffffff;
David S. Millercf44bbc2006-06-29 14:34:50 -0700329 r->end = result + size - 1;
David S. Miller95714e12006-06-29 14:35:14 -0700330 r->flags = flags | ((result >> 32ULL) & 0xffUL);
David S. Millercf44bbc2006-06-29 14:34:50 -0700331 }
332 r->name = op->node->name;
333 }
334}
335
336static struct of_device * __init scan_one_device(struct device_node *dp,
337 struct device *parent)
338{
339 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell8271f042007-03-29 00:47:23 -0700340 const struct linux_prom_irqs *intr;
David S. Miller3d6e4702007-07-18 22:03:25 -0700341 struct dev_archdata *sd;
David S. Miller8f96cd12006-06-29 15:08:02 -0700342 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700343
344 if (!op)
345 return NULL;
346
David S. Miller3d6e4702007-07-18 22:03:25 -0700347 sd = &op->dev.archdata;
348 sd->prom_node = dp;
349 sd->op = op;
350
David S. Millercf44bbc2006-06-29 14:34:50 -0700351 op->node = dp;
352
353 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
354 (25*1000*1000));
355 op->portid = of_getintprop_default(dp, "upa-portid", -1);
356 if (op->portid == -1)
357 op->portid = of_getintprop_default(dp, "portid", -1);
358
David S. Miller8f96cd12006-06-29 15:08:02 -0700359 intr = of_get_property(dp, "intr", &len);
360 if (intr) {
361 op->num_irqs = len / sizeof(struct linux_prom_irqs);
362 for (i = 0; i < op->num_irqs; i++)
363 op->irqs[i] = intr[i].pri;
364 } else {
Stephen Rothwell8271f042007-03-29 00:47:23 -0700365 const unsigned int *irq =
366 of_get_property(dp, "interrupts", &len);
David S. Miller8f96cd12006-06-29 15:08:02 -0700367
368 if (irq) {
369 op->num_irqs = len / sizeof(unsigned int);
370 for (i = 0; i < op->num_irqs; i++)
371 op->irqs[i] = irq[i];
372 } else {
373 op->num_irqs = 0;
374 }
375 }
376 if (sparc_cpu_model == sun4d) {
377 static int pil_to_sbus[] = {
378 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
379 };
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700380 struct device_node *io_unit, *sbi = dp->parent;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700381 const struct linux_prom_registers *regs;
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700382 int board, slot;
383
384 while (sbi) {
385 if (!strcmp(sbi->name, "sbi"))
386 break;
387
388 sbi = sbi->parent;
389 }
390 if (!sbi)
391 goto build_resources;
David S. Miller8f96cd12006-06-29 15:08:02 -0700392
393 regs = of_get_property(dp, "reg", NULL);
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700394 if (!regs)
395 goto build_resources;
396
David S. Miller8f96cd12006-06-29 15:08:02 -0700397 slot = regs->which_io;
398
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700399 /* If SBI's parent is not io-unit or the io-unit lacks
400 * a "board#" property, something is very wrong.
401 */
402 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
403 printk("%s: Error, parent is not io-unit.\n",
404 sbi->full_name);
405 goto build_resources;
406 }
407 io_unit = sbi->parent;
408 board = of_getintprop_default(io_unit, "board#", -1);
409 if (board == -1) {
410 printk("%s: Error, lacks board# property.\n",
411 io_unit->full_name);
412 goto build_resources;
413 }
414
David S. Miller8f96cd12006-06-29 15:08:02 -0700415 for (i = 0; i < op->num_irqs; i++) {
416 int this_irq = op->irqs[i];
417 int sbusl = pil_to_sbus[this_irq];
418
419 if (sbusl)
420 this_irq = (((board + 1) << 5) +
421 (sbusl << 2) +
422 slot);
423
424 op->irqs[i] = this_irq;
425 }
426 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700427
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700428build_resources:
David S. Millercf44bbc2006-06-29 14:34:50 -0700429 build_device_resources(op, parent);
430
431 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000432 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700433 if (!parent)
Kay Sievers5acdc1f2008-11-12 23:48:40 -0800434 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700435 else
Grant Likely6016a362010-01-28 14:06:53 -0700436 dev_set_name(&op->dev, "%08x", dp->phandle);
David S. Millercf44bbc2006-06-29 14:34:50 -0700437
438 if (of_device_register(op)) {
439 printk("%s: Could not register of device.\n",
440 dp->full_name);
441 kfree(op);
442 op = NULL;
443 }
444
445 return op;
446}
447
448static void __init scan_tree(struct device_node *dp, struct device *parent)
449{
450 while (dp) {
451 struct of_device *op = scan_one_device(dp, parent);
452
453 if (op)
454 scan_tree(dp->child, &op->dev);
455
456 dp = dp->sibling;
457 }
458}
459
460static void __init scan_of_devices(void)
461{
462 struct device_node *root = of_find_node_by_path("/");
463 struct of_device *parent;
464
465 parent = scan_one_device(root, NULL);
466 if (!parent)
467 return;
468
469 scan_tree(root->child, &parent->dev);
470}
471
David S. Millerfd531432006-06-23 15:55:17 -0700472static int __init of_bus_driver_init(void)
473{
David S. Millercf44bbc2006-06-29 14:34:50 -0700474 int err;
David S. Millerfd531432006-06-23 15:55:17 -0700475
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000476 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millercf44bbc2006-06-29 14:34:50 -0700477 if (!err)
478 scan_of_devices();
479
480 return err;
David S. Millerfd531432006-06-23 15:55:17 -0700481}
482
483postcore_initcall(of_bus_driver_init);
484
David S. Millera83f9822006-07-12 23:19:31 -0700485static int __init of_debug(char *str)
486{
487 int val = 0;
488
489 get_option(&str, &val);
490 if (val & 1)
491 of_resource_verbose = 1;
492 return 1;
493}
494
495__setup("of_debug=", of_debug);