blob: c59014886afe4b9c6a63f8c6977ccc88fb742d2f [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>
9#include <linux/of_device.h>
10#include <linux/of_platform.h>
David S. Millerfd531432006-06-23 15:55:17 -070011
David S. Miller8f96cd12006-06-29 15:08:02 -070012static int node_match(struct device *dev, void *data)
13{
14 struct of_device *op = to_of_device(dev);
15 struct device_node *dp = data;
16
17 return (op->node == dp);
18}
19
20struct of_device *of_find_device_by_node(struct device_node *dp)
21{
Stephen Rothwell37b77542007-04-30 17:43:56 +100022 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
David S. Miller8f96cd12006-06-29 15:08:02 -070023 dp, node_match);
24
25 if (dev)
26 return to_of_device(dev);
27
28 return NULL;
29}
30EXPORT_SYMBOL(of_find_device_by_node);
31
David S. Miller51e0f0042008-08-25 16:44:58 -070032unsigned int irq_of_parse_and_map(struct device_node *node, int index)
David S. Miller44266212008-08-20 16:34:39 -070033{
34 struct of_device *op = of_find_device_by_node(node);
35
36 if (!op || index >= op->num_irqs)
David S. Miller51e0f0042008-08-25 16:44:58 -070037 return 0;
David S. Miller44266212008-08-20 16:34:39 -070038
39 return op->irqs[index];
40}
41EXPORT_SYMBOL(irq_of_parse_and_map);
42
David S. Miller50596252008-08-27 04:22:37 -070043/* Take the archdata values for IOMMU, STC, and HOSTDATA found in
44 * BUS and propagate to all child of_device objects.
45 */
46void of_propagate_archdata(struct of_device *bus)
47{
48 struct dev_archdata *bus_sd = &bus->dev.archdata;
49 struct device_node *bus_dp = bus->node;
50 struct device_node *dp;
51
52 for (dp = bus_dp->child; dp; dp = dp->sibling) {
53 struct of_device *op = of_find_device_by_node(dp);
54
55 op->dev.archdata.iommu = bus_sd->iommu;
56 op->dev.archdata.stc = bus_sd->stc;
57 op->dev.archdata.host_controller = bus_sd->host_controller;
58 op->dev.archdata.numa_node = bus_sd->numa_node;
59
60 if (dp->child)
61 of_propagate_archdata(op);
62 }
63}
64
Stephen Rothwell3f23de12007-05-03 02:38:57 +100065struct bus_type of_platform_bus_type;
Stephen Rothwell37b77542007-04-30 17:43:56 +100066EXPORT_SYMBOL(of_platform_bus_type);
David S. Millercf44bbc2006-06-29 14:34:50 -070067
David S. Millera83f9822006-07-12 23:19:31 -070068static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -070069{
70 u64 r = 0;
71 while (size--)
72 r = (r << 32) | *(cell++);
73 return r;
74}
75
76static void __init get_cells(struct device_node *dp,
77 int *addrc, int *sizec)
78{
79 if (addrc)
80 *addrc = of_n_addr_cells(dp);
81 if (sizec)
82 *sizec = of_n_size_cells(dp);
83}
84
85/* Max address size we deal with */
86#define OF_MAX_ADDR_CELLS 4
87
88struct of_bus {
89 const char *name;
90 const char *addr_prop_name;
91 int (*match)(struct device_node *parent);
92 void (*count_cells)(struct device_node *child,
93 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -070094 int (*map)(u32 *addr, const u32 *range,
95 int na, int ns, int pna);
Stephen Rothwell8271f042007-03-29 00:47:23 -070096 unsigned int (*get_flags)(const u32 *addr);
David S. Millercf44bbc2006-06-29 14:34:50 -070097};
98
99/*
100 * Default translator (generic bus)
101 */
102
103static void of_bus_default_count_cells(struct device_node *dev,
104 int *addrc, int *sizec)
105{
106 get_cells(dev, addrc, sizec);
107}
108
David S. Millera83f9822006-07-12 23:19:31 -0700109/* Make sure the least significant 64-bits are in-range. Even
110 * for 3 or 4 cell values it is a good enough approximation.
111 */
112static int of_out_of_range(const u32 *addr, const u32 *base,
113 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700114{
115 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700116 u64 b = of_read_addr(base, na);
117
118 if (a < b)
119 return 1;
120
121 b += of_read_addr(size, ns);
122 if (a >= b)
123 return 1;
124
125 return 0;
126}
127
128static int of_bus_default_map(u32 *addr, const u32 *range,
129 int na, int ns, int pna)
130{
131 u32 result[OF_MAX_ADDR_CELLS];
132 int i;
133
134 if (ns > 2) {
135 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
136 return -EINVAL;
137 }
138
139 if (of_out_of_range(addr, range, range + na + pna, na, ns))
140 return -EINVAL;
141
142 /* Start with the parent range base. */
143 memcpy(result, range + na, pna * 4);
144
145 /* Add in the child address offset. */
146 for (i = 0; i < na; i++)
147 result[pna - 1 - i] +=
148 (addr[na - 1 - i] -
149 range[na - 1 - i]);
150
151 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700152
153 return 0;
154}
155
Stephen Rothwell8271f042007-03-29 00:47:23 -0700156static unsigned int of_bus_default_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700157{
158 return IORESOURCE_MEM;
159}
160
David S. Millercf44bbc2006-06-29 14:34:50 -0700161/*
162 * PCI bus specific translator
163 */
164
165static int of_bus_pci_match(struct device_node *np)
166{
David S. Millera83f9822006-07-12 23:19:31 -0700167 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
168 /* Do not do PCI specific frobbing if the
169 * PCI bridge lacks a ranges property. We
170 * want to pass it through up to the next
171 * parent as-is, not with the PCI translate
172 * method which chops off the top address cell.
173 */
174 if (!of_find_property(np, "ranges", NULL))
175 return 0;
176
177 return 1;
178 }
179
180 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700181}
182
183static void of_bus_pci_count_cells(struct device_node *np,
184 int *addrc, int *sizec)
185{
186 if (addrc)
187 *addrc = 3;
188 if (sizec)
189 *sizec = 2;
190}
191
David S. Millera83f9822006-07-12 23:19:31 -0700192static int of_bus_pci_map(u32 *addr, const u32 *range,
193 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700194{
David S. Millera83f9822006-07-12 23:19:31 -0700195 u32 result[OF_MAX_ADDR_CELLS];
196 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700197
198 /* Check address type match */
199 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700200 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700201
David S. Millera83f9822006-07-12 23:19:31 -0700202 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
203 na - 1, ns))
204 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700205
David S. Millera83f9822006-07-12 23:19:31 -0700206 /* Start with the parent range base. */
207 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700208
David S. Millera83f9822006-07-12 23:19:31 -0700209 /* Add in the child address offset, skipping high cell. */
210 for (i = 0; i < na - 1; i++)
211 result[pna - 1 - i] +=
212 (addr[na - 1 - i] -
213 range[na - 1 - i]);
214
215 memcpy(addr, result, pna * 4);
216
217 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700218}
219
Stephen Rothwell8271f042007-03-29 00:47:23 -0700220static unsigned int of_bus_pci_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700221{
222 unsigned int flags = 0;
223 u32 w = addr[0];
224
225 switch((w >> 24) & 0x03) {
226 case 0x01:
227 flags |= IORESOURCE_IO;
228 case 0x02: /* 32 bits */
229 case 0x03: /* 64 bits */
230 flags |= IORESOURCE_MEM;
231 }
232 if (w & 0x40000000)
233 flags |= IORESOURCE_PREFETCH;
234 return flags;
235}
236
237/*
238 * SBUS bus specific translator
239 */
240
241static int of_bus_sbus_match(struct device_node *np)
242{
243 return !strcmp(np->name, "sbus") ||
244 !strcmp(np->name, "sbi");
245}
246
247static void of_bus_sbus_count_cells(struct device_node *child,
248 int *addrc, int *sizec)
249{
250 if (addrc)
251 *addrc = 2;
252 if (sizec)
253 *sizec = 1;
254}
255
David S. Millera83f9822006-07-12 23:19:31 -0700256static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700257{
258 return of_bus_default_map(addr, range, na, ns, pna);
259}
260
Stephen Rothwell8271f042007-03-29 00:47:23 -0700261static unsigned int of_bus_sbus_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700262{
263 return IORESOURCE_MEM;
264}
265
266
267/*
268 * Array of bus specific translators
269 */
270
271static struct of_bus of_busses[] = {
272 /* PCI */
273 {
274 .name = "pci",
275 .addr_prop_name = "assigned-addresses",
276 .match = of_bus_pci_match,
277 .count_cells = of_bus_pci_count_cells,
278 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700279 .get_flags = of_bus_pci_get_flags,
280 },
281 /* SBUS */
282 {
283 .name = "sbus",
284 .addr_prop_name = "reg",
285 .match = of_bus_sbus_match,
286 .count_cells = of_bus_sbus_count_cells,
287 .map = of_bus_sbus_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700288 .get_flags = of_bus_sbus_get_flags,
289 },
290 /* Default */
291 {
292 .name = "default",
293 .addr_prop_name = "reg",
294 .match = NULL,
295 .count_cells = of_bus_default_count_cells,
296 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700297 .get_flags = of_bus_default_get_flags,
298 },
299};
300
301static struct of_bus *of_match_bus(struct device_node *np)
302{
303 int i;
304
305 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
306 if (!of_busses[i].match || of_busses[i].match(np))
307 return &of_busses[i];
308 BUG();
309 return NULL;
310}
311
312static int __init build_one_resource(struct device_node *parent,
313 struct of_bus *bus,
314 struct of_bus *pbus,
315 u32 *addr,
316 int na, int ns, int pna)
317{
Stephen Rothwell8271f042007-03-29 00:47:23 -0700318 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700319 unsigned int rlen;
320 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700321
322 ranges = of_get_property(parent, "ranges", &rlen);
323 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700324 u32 result[OF_MAX_ADDR_CELLS];
325 int i;
326
327 memset(result, 0, pna * 4);
328 for (i = 0; i < na; i++)
329 result[pna - 1 - i] =
330 addr[na - 1 - i];
331
332 memcpy(addr, result, pna * 4);
333 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700334 }
335
336 /* Now walk through the ranges */
337 rlen /= 4;
338 rone = na + pna + ns;
339 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700340 if (!bus->map(addr, ranges, na, ns, pna))
341 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700342 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700343
David S. Millera83f9822006-07-12 23:19:31 -0700344 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700345}
346
David S. Millera83f9822006-07-12 23:19:31 -0700347static int of_resource_verbose;
348
David S. Millercf44bbc2006-06-29 14:34:50 -0700349static void __init build_device_resources(struct of_device *op,
350 struct device *parent)
351{
352 struct of_device *p_op;
353 struct of_bus *bus;
354 int na, ns;
355 int index, num_reg;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700356 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700357
358 if (!parent)
359 return;
360
361 p_op = to_of_device(parent);
362 bus = of_match_bus(p_op->node);
363 bus->count_cells(op->node, &na, &ns);
364
365 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
366 if (!preg || num_reg == 0)
367 return;
368
369 /* Convert to num-cells. */
370 num_reg /= 4;
371
372 /* Conver to num-entries. */
373 num_reg /= na + ns;
374
375 for (index = 0; index < num_reg; index++) {
376 struct resource *r = &op->resource[index];
377 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell8271f042007-03-29 00:47:23 -0700378 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700379 struct device_node *dp = op->node;
380 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800381 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700382 u64 size, result = OF_BAD_ADDR;
383 unsigned long flags;
384 int dna, dns;
385 int pna, pns;
386
387 size = of_read_addr(reg + na, ns);
388 flags = bus->get_flags(reg);
389
390 memcpy(addr, reg, na * 4);
391
392 /* If the immediate parent has no ranges property to apply,
393 * just use a 1<->1 mapping.
394 */
395 if (of_find_property(pp, "ranges", NULL) == NULL) {
396 result = of_read_addr(addr, na);
397 goto build_res;
398 }
399
400 dna = na;
401 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800402 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700403
404 while (1) {
405 dp = pp;
406 pp = dp->parent;
407 if (!pp) {
408 result = of_read_addr(addr, dna);
409 break;
410 }
411
412 pbus = of_match_bus(pp);
413 pbus->count_cells(dp, &pna, &pns);
414
David S. Millerb85cdd42007-02-28 23:20:12 -0800415 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700416 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700417 break;
418
419 dna = pna;
420 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800421 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700422 }
423
424 build_res:
425 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700426
427 if (of_resource_verbose)
428 printk("%s reg[%d] -> %llx\n",
429 op->node->full_name, index,
430 result);
431
David S. Millercf44bbc2006-06-29 14:34:50 -0700432 if (result != OF_BAD_ADDR) {
David S. Miller95714e12006-06-29 14:35:14 -0700433 r->start = result & 0xffffffff;
David S. Millercf44bbc2006-06-29 14:34:50 -0700434 r->end = result + size - 1;
David S. Miller95714e12006-06-29 14:35:14 -0700435 r->flags = flags | ((result >> 32ULL) & 0xffUL);
David S. Millercf44bbc2006-06-29 14:34:50 -0700436 }
437 r->name = op->node->name;
438 }
439}
440
441static struct of_device * __init scan_one_device(struct device_node *dp,
442 struct device *parent)
443{
444 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell8271f042007-03-29 00:47:23 -0700445 const struct linux_prom_irqs *intr;
David S. Miller3d6e4702007-07-18 22:03:25 -0700446 struct dev_archdata *sd;
David S. Miller8f96cd12006-06-29 15:08:02 -0700447 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700448
449 if (!op)
450 return NULL;
451
David S. Miller3d6e4702007-07-18 22:03:25 -0700452 sd = &op->dev.archdata;
453 sd->prom_node = dp;
454 sd->op = op;
455
David S. Millercf44bbc2006-06-29 14:34:50 -0700456 op->node = dp;
457
458 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
459 (25*1000*1000));
460 op->portid = of_getintprop_default(dp, "upa-portid", -1);
461 if (op->portid == -1)
462 op->portid = of_getintprop_default(dp, "portid", -1);
463
David S. Miller8f96cd12006-06-29 15:08:02 -0700464 intr = of_get_property(dp, "intr", &len);
465 if (intr) {
466 op->num_irqs = len / sizeof(struct linux_prom_irqs);
467 for (i = 0; i < op->num_irqs; i++)
468 op->irqs[i] = intr[i].pri;
469 } else {
Stephen Rothwell8271f042007-03-29 00:47:23 -0700470 const unsigned int *irq =
471 of_get_property(dp, "interrupts", &len);
David S. Miller8f96cd12006-06-29 15:08:02 -0700472
473 if (irq) {
474 op->num_irqs = len / sizeof(unsigned int);
475 for (i = 0; i < op->num_irqs; i++)
476 op->irqs[i] = irq[i];
477 } else {
478 op->num_irqs = 0;
479 }
480 }
481 if (sparc_cpu_model == sun4d) {
482 static int pil_to_sbus[] = {
483 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
484 };
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700485 struct device_node *io_unit, *sbi = dp->parent;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700486 const struct linux_prom_registers *regs;
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700487 int board, slot;
488
489 while (sbi) {
490 if (!strcmp(sbi->name, "sbi"))
491 break;
492
493 sbi = sbi->parent;
494 }
495 if (!sbi)
496 goto build_resources;
David S. Miller8f96cd12006-06-29 15:08:02 -0700497
498 regs = of_get_property(dp, "reg", NULL);
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700499 if (!regs)
500 goto build_resources;
501
David S. Miller8f96cd12006-06-29 15:08:02 -0700502 slot = regs->which_io;
503
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700504 /* If SBI's parent is not io-unit or the io-unit lacks
505 * a "board#" property, something is very wrong.
506 */
507 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
508 printk("%s: Error, parent is not io-unit.\n",
509 sbi->full_name);
510 goto build_resources;
511 }
512 io_unit = sbi->parent;
513 board = of_getintprop_default(io_unit, "board#", -1);
514 if (board == -1) {
515 printk("%s: Error, lacks board# property.\n",
516 io_unit->full_name);
517 goto build_resources;
518 }
519
David S. Miller8f96cd12006-06-29 15:08:02 -0700520 for (i = 0; i < op->num_irqs; i++) {
521 int this_irq = op->irqs[i];
522 int sbusl = pil_to_sbus[this_irq];
523
524 if (sbusl)
525 this_irq = (((board + 1) << 5) +
526 (sbusl << 2) +
527 slot);
528
529 op->irqs[i] = this_irq;
530 }
531 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700532
David S. Miller9d7ab1f2006-07-17 21:39:09 -0700533build_resources:
David S. Millercf44bbc2006-06-29 14:34:50 -0700534 build_device_resources(op, parent);
535
536 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000537 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700538 if (!parent)
539 strcpy(op->dev.bus_id, "root");
540 else
David S. Millerf5ef9d12006-10-27 01:03:31 -0700541 sprintf(op->dev.bus_id, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700542
543 if (of_device_register(op)) {
544 printk("%s: Could not register of device.\n",
545 dp->full_name);
546 kfree(op);
547 op = NULL;
548 }
549
550 return op;
551}
552
553static void __init scan_tree(struct device_node *dp, struct device *parent)
554{
555 while (dp) {
556 struct of_device *op = scan_one_device(dp, parent);
557
558 if (op)
559 scan_tree(dp->child, &op->dev);
560
561 dp = dp->sibling;
562 }
563}
564
565static void __init scan_of_devices(void)
566{
567 struct device_node *root = of_find_node_by_path("/");
568 struct of_device *parent;
569
570 parent = scan_one_device(root, NULL);
571 if (!parent)
572 return;
573
574 scan_tree(root->child, &parent->dev);
575}
576
David S. Millerfd531432006-06-23 15:55:17 -0700577static int __init of_bus_driver_init(void)
578{
David S. Millercf44bbc2006-06-29 14:34:50 -0700579 int err;
David S. Millerfd531432006-06-23 15:55:17 -0700580
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000581 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millercf44bbc2006-06-29 14:34:50 -0700582 if (!err)
583 scan_of_devices();
584
585 return err;
David S. Millerfd531432006-06-23 15:55:17 -0700586}
587
588postcore_initcall(of_bus_driver_init);
589
David S. Millera83f9822006-07-12 23:19:31 -0700590static int __init of_debug(char *str)
591{
592 int val = 0;
593
594 get_option(&str, &val);
595 if (val & 1)
596 of_resource_verbose = 1;
597 return 1;
598}
599
600__setup("of_debug=", of_debug);