blob: c15bcdf75c0d242359b296e82c93c3c103349ef9 [file] [log] [blame]
David S. Millera2bd4fd2006-06-23 01:44:10 -07001#include <linux/string.h>
2#include <linux/kernel.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +10003#include <linux/of.h>
David S. Millera2bd4fd2006-06-23 01:44:10 -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>
David S. Millerc1b1a5f2008-03-19 04:52:48 -07009#include <linux/irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100010#include <linux/of_device.h>
11#include <linux/of_platform.h>
David S. Millera2bd4fd2006-06-23 01:44:10 -070012
David S. Miller3ca9fab2006-06-29 14:35:33 -070013void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
14{
15 unsigned long ret = res->start + offset;
David S. Miller6bda5732006-10-18 23:00:35 -070016 struct resource *r;
David S. Miller3ca9fab2006-06-29 14:35:33 -070017
David S. Miller6bda5732006-10-18 23:00:35 -070018 if (res->flags & IORESOURCE_MEM)
19 r = request_mem_region(ret, size, name);
20 else
21 r = request_region(ret, size, name);
22 if (!r)
David S. Miller3ca9fab2006-06-29 14:35:33 -070023 ret = 0;
24
25 return (void __iomem *) ret;
26}
27EXPORT_SYMBOL(of_ioremap);
28
David S. Millere3a411a2006-12-28 21:01:32 -080029void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -070030{
David S. Millere3a411a2006-12-28 21:01:32 -080031 if (res->flags & IORESOURCE_MEM)
32 release_mem_region((unsigned long) base, size);
33 else
34 release_region((unsigned long) base, size);
David S. Miller3ca9fab2006-06-29 14:35:33 -070035}
36EXPORT_SYMBOL(of_iounmap);
37
David S. Miller2b1e5972006-06-29 15:07:37 -070038static int node_match(struct device *dev, void *data)
39{
40 struct of_device *op = to_of_device(dev);
41 struct device_node *dp = data;
42
43 return (op->node == dp);
44}
45
46struct of_device *of_find_device_by_node(struct device_node *dp)
47{
Stephen Rothwell37b77542007-04-30 17:43:56 +100048 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
David S. Miller2b1e5972006-06-29 15:07:37 -070049 dp, node_match);
50
51 if (dev)
52 return to_of_device(dev);
53
54 return NULL;
55}
56EXPORT_SYMBOL(of_find_device_by_node);
57
David S. Miller51e0f002008-08-25 16:44:58 -070058unsigned int irq_of_parse_and_map(struct device_node *node, int index)
David S. Miller44266212008-08-20 16:34:39 -070059{
60 struct of_device *op = of_find_device_by_node(node);
61
62 if (!op || index >= op->num_irqs)
David S. Miller51e0f002008-08-25 16:44:58 -070063 return 0;
David S. Miller44266212008-08-20 16:34:39 -070064
65 return op->irqs[index];
66}
67EXPORT_SYMBOL(irq_of_parse_and_map);
68
David S. Miller50596252008-08-27 04:22:37 -070069/* Take the archdata values for IOMMU, STC, and HOSTDATA found in
70 * BUS and propagate to all child of_device objects.
71 */
72void of_propagate_archdata(struct of_device *bus)
73{
74 struct dev_archdata *bus_sd = &bus->dev.archdata;
75 struct device_node *bus_dp = bus->node;
76 struct device_node *dp;
77
78 for (dp = bus_dp->child; dp; dp = dp->sibling) {
79 struct of_device *op = of_find_device_by_node(dp);
80
81 op->dev.archdata.iommu = bus_sd->iommu;
82 op->dev.archdata.stc = bus_sd->stc;
83 op->dev.archdata.host_controller = bus_sd->host_controller;
84 op->dev.archdata.numa_node = bus_sd->numa_node;
85
86 if (dp->child)
87 of_propagate_archdata(op);
88 }
89}
90
Stephen Rothwell3f23de12007-05-03 02:38:57 +100091struct bus_type of_platform_bus_type;
Stephen Rothwell37b77542007-04-30 17:43:56 +100092EXPORT_SYMBOL(of_platform_bus_type);
David S. Millercf44bbc2006-06-29 14:34:50 -070093
David S. Millera83f9822006-07-12 23:19:31 -070094static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -070095{
96 u64 r = 0;
97 while (size--)
98 r = (r << 32) | *(cell++);
99 return r;
100}
101
102static void __init get_cells(struct device_node *dp,
103 int *addrc, int *sizec)
104{
105 if (addrc)
106 *addrc = of_n_addr_cells(dp);
107 if (sizec)
108 *sizec = of_n_size_cells(dp);
109}
110
111/* Max address size we deal with */
112#define OF_MAX_ADDR_CELLS 4
113
114struct of_bus {
115 const char *name;
116 const char *addr_prop_name;
117 int (*match)(struct device_node *parent);
118 void (*count_cells)(struct device_node *child,
119 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -0700120 int (*map)(u32 *addr, const u32 *range,
121 int na, int ns, int pna);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700122 unsigned int (*get_flags)(const u32 *addr);
David S. Millercf44bbc2006-06-29 14:34:50 -0700123};
124
125/*
126 * Default translator (generic bus)
127 */
128
129static void of_bus_default_count_cells(struct device_node *dev,
130 int *addrc, int *sizec)
131{
132 get_cells(dev, addrc, sizec);
133}
134
David S. Millera83f9822006-07-12 23:19:31 -0700135/* Make sure the least significant 64-bits are in-range. Even
136 * for 3 or 4 cell values it is a good enough approximation.
137 */
138static int of_out_of_range(const u32 *addr, const u32 *base,
139 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700140{
141 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700142 u64 b = of_read_addr(base, na);
143
144 if (a < b)
145 return 1;
146
147 b += of_read_addr(size, ns);
148 if (a >= b)
149 return 1;
150
151 return 0;
152}
153
154static int of_bus_default_map(u32 *addr, const u32 *range,
155 int na, int ns, int pna)
156{
157 u32 result[OF_MAX_ADDR_CELLS];
158 int i;
159
160 if (ns > 2) {
161 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
162 return -EINVAL;
163 }
164
165 if (of_out_of_range(addr, range, range + na + pna, na, ns))
166 return -EINVAL;
167
168 /* Start with the parent range base. */
169 memcpy(result, range + na, pna * 4);
170
171 /* Add in the child address offset. */
172 for (i = 0; i < na; i++)
173 result[pna - 1 - i] +=
174 (addr[na - 1 - i] -
175 range[na - 1 - i]);
176
177 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700178
179 return 0;
180}
181
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700182static unsigned int of_bus_default_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700183{
184 return IORESOURCE_MEM;
185}
186
David S. Millercf44bbc2006-06-29 14:34:50 -0700187/*
188 * PCI bus specific translator
189 */
190
191static int of_bus_pci_match(struct device_node *np)
192{
David S. Millera83f9822006-07-12 23:19:31 -0700193 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
David S. Millera165b422007-03-29 01:50:16 -0700194 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800195
196 if (model && !strcmp(model, "SUNW,simba"))
197 return 0;
198
David S. Millera83f9822006-07-12 23:19:31 -0700199 /* Do not do PCI specific frobbing if the
200 * PCI bridge lacks a ranges property. We
201 * want to pass it through up to the next
202 * parent as-is, not with the PCI translate
203 * method which chops off the top address cell.
204 */
205 if (!of_find_property(np, "ranges", NULL))
206 return 0;
207
208 return 1;
209 }
210
211 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700212}
213
David S. Miller01f94c42007-03-04 12:53:19 -0800214static int of_bus_simba_match(struct device_node *np)
215{
David S. Millera165b422007-03-29 01:50:16 -0700216 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800217
218 if (model && !strcmp(model, "SUNW,simba"))
219 return 1;
David S. Miller8c2786c2007-06-07 21:59:44 -0700220
221 /* Treat PCI busses lacking ranges property just like
222 * simba.
223 */
224 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
225 if (!of_find_property(np, "ranges", NULL))
226 return 1;
227 }
228
David S. Miller01f94c42007-03-04 12:53:19 -0800229 return 0;
230}
231
232static int of_bus_simba_map(u32 *addr, const u32 *range,
233 int na, int ns, int pna)
234{
235 return 0;
236}
237
David S. Millercf44bbc2006-06-29 14:34:50 -0700238static void of_bus_pci_count_cells(struct device_node *np,
239 int *addrc, int *sizec)
240{
241 if (addrc)
242 *addrc = 3;
243 if (sizec)
244 *sizec = 2;
245}
246
David S. Millera83f9822006-07-12 23:19:31 -0700247static int of_bus_pci_map(u32 *addr, const u32 *range,
248 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700249{
David S. Millera83f9822006-07-12 23:19:31 -0700250 u32 result[OF_MAX_ADDR_CELLS];
251 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700252
253 /* Check address type match */
254 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700255 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700256
David S. Millera83f9822006-07-12 23:19:31 -0700257 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
258 na - 1, ns))
259 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700260
David S. Millera83f9822006-07-12 23:19:31 -0700261 /* Start with the parent range base. */
262 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700263
David S. Millera83f9822006-07-12 23:19:31 -0700264 /* Add in the child address offset, skipping high cell. */
265 for (i = 0; i < na - 1; i++)
266 result[pna - 1 - i] +=
267 (addr[na - 1 - i] -
268 range[na - 1 - i]);
269
270 memcpy(addr, result, pna * 4);
271
272 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700273}
274
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700275static unsigned int of_bus_pci_get_flags(const u32 *addr)
David S. Millercf44bbc2006-06-29 14:34:50 -0700276{
277 unsigned int flags = 0;
278 u32 w = addr[0];
279
280 switch((w >> 24) & 0x03) {
281 case 0x01:
282 flags |= IORESOURCE_IO;
283 case 0x02: /* 32 bits */
284 case 0x03: /* 64 bits */
285 flags |= IORESOURCE_MEM;
286 }
287 if (w & 0x40000000)
288 flags |= IORESOURCE_PREFETCH;
289 return flags;
290}
291
292/*
David S. Millercf44bbc2006-06-29 14:34:50 -0700293 * SBUS bus specific translator
294 */
295
296static int of_bus_sbus_match(struct device_node *np)
297{
298 return !strcmp(np->name, "sbus") ||
299 !strcmp(np->name, "sbi");
300}
301
302static void of_bus_sbus_count_cells(struct device_node *child,
303 int *addrc, int *sizec)
304{
305 if (addrc)
306 *addrc = 2;
307 if (sizec)
308 *sizec = 1;
309}
310
David S. Miller4130a4b2006-10-25 22:31:06 -0700311/*
312 * FHC/Central bus specific translator.
313 *
314 * This is just needed to hard-code the address and size cell
315 * counts. 'fhc' and 'central' nodes lack the #address-cells and
316 * #size-cells properties, and if you walk to the root on such
317 * Enterprise boxes all you'll get is a #size-cells of 2 which is
318 * not what we want to use.
319 */
320static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700321{
David S. Miller4130a4b2006-10-25 22:31:06 -0700322 return !strcmp(np->name, "fhc") ||
323 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700324}
325
David S. Miller4130a4b2006-10-25 22:31:06 -0700326#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700327
328/*
329 * Array of bus specific translators
330 */
331
332static struct of_bus of_busses[] = {
333 /* PCI */
334 {
335 .name = "pci",
336 .addr_prop_name = "assigned-addresses",
337 .match = of_bus_pci_match,
338 .count_cells = of_bus_pci_count_cells,
339 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700340 .get_flags = of_bus_pci_get_flags,
341 },
David S. Miller01f94c42007-03-04 12:53:19 -0800342 /* SIMBA */
343 {
344 .name = "simba",
345 .addr_prop_name = "assigned-addresses",
346 .match = of_bus_simba_match,
347 .count_cells = of_bus_pci_count_cells,
348 .map = of_bus_simba_map,
349 .get_flags = of_bus_pci_get_flags,
350 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700351 /* SBUS */
352 {
353 .name = "sbus",
354 .addr_prop_name = "reg",
355 .match = of_bus_sbus_match,
356 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700357 .map = of_bus_default_map,
358 .get_flags = of_bus_default_get_flags,
359 },
360 /* FHC */
361 {
362 .name = "fhc",
363 .addr_prop_name = "reg",
364 .match = of_bus_fhc_match,
365 .count_cells = of_bus_fhc_count_cells,
366 .map = of_bus_default_map,
367 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700368 },
369 /* Default */
370 {
371 .name = "default",
372 .addr_prop_name = "reg",
373 .match = NULL,
374 .count_cells = of_bus_default_count_cells,
375 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700376 .get_flags = of_bus_default_get_flags,
377 },
378};
379
380static struct of_bus *of_match_bus(struct device_node *np)
381{
382 int i;
383
384 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
385 if (!of_busses[i].match || of_busses[i].match(np))
386 return &of_busses[i];
387 BUG();
388 return NULL;
389}
390
391static int __init build_one_resource(struct device_node *parent,
392 struct of_bus *bus,
393 struct of_bus *pbus,
394 u32 *addr,
395 int na, int ns, int pna)
396{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700397 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700398 unsigned int rlen;
399 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700400
401 ranges = of_get_property(parent, "ranges", &rlen);
402 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700403 u32 result[OF_MAX_ADDR_CELLS];
404 int i;
405
406 memset(result, 0, pna * 4);
407 for (i = 0; i < na; i++)
408 result[pna - 1 - i] =
409 addr[na - 1 - i];
410
411 memcpy(addr, result, pna * 4);
412 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700413 }
414
415 /* Now walk through the ranges */
416 rlen /= 4;
417 rone = na + pna + ns;
418 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700419 if (!bus->map(addr, ranges, na, ns, pna))
420 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700421 }
David S. Millera83f9822006-07-12 23:19:31 -0700422
David S. Miller49d23cf2007-05-13 22:01:18 -0700423 /* When we miss an I/O space match on PCI, just pass it up
424 * to the next PCI bridge and/or controller.
425 */
426 if (!strcmp(bus->name, "pci") &&
427 (addr[0] & 0x03000000) == 0x01000000)
428 return 0;
429
David S. Millera83f9822006-07-12 23:19:31 -0700430 return 1;
431}
432
433static int __init use_1to1_mapping(struct device_node *pp)
434{
David S. Millera83f9822006-07-12 23:19:31 -0700435 /* If we have a ranges property in the parent, use it. */
436 if (of_find_property(pp, "ranges", NULL) != NULL)
437 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700438
David S. Millera83f9822006-07-12 23:19:31 -0700439 /* If the parent is the dma node of an ISA bus, pass
440 * the translation up to the root.
David S. Miller52802672008-09-03 02:04:41 -0700441 *
442 * Some SBUS devices use intermediate nodes to express
443 * hierarchy within the device itself. These aren't
444 * real bus nodes, and don't have a 'ranges' property.
445 * But, we should still pass the translation work up
446 * to the SBUS itself.
David S. Millera83f9822006-07-12 23:19:31 -0700447 */
David S. Miller52802672008-09-03 02:04:41 -0700448 if (!strcmp(pp->name, "dma") ||
449 !strcmp(pp->name, "espdma") ||
450 !strcmp(pp->name, "ledma") ||
451 !strcmp(pp->name, "lebuffer"))
David S. Millera83f9822006-07-12 23:19:31 -0700452 return 0;
453
David S. Miller8c2786c2007-06-07 21:59:44 -0700454 /* Similarly for all PCI bridges, if we get this far
455 * it lacks a ranges property, and this will include
456 * cases like Simba.
457 */
458 if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
David S. Millera83f9822006-07-12 23:19:31 -0700459 return 0;
460
461 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700462}
463
David S. Millera83f9822006-07-12 23:19:31 -0700464static int of_resource_verbose;
465
David S. Millercf44bbc2006-06-29 14:34:50 -0700466static void __init build_device_resources(struct of_device *op,
467 struct device *parent)
468{
469 struct of_device *p_op;
470 struct of_bus *bus;
471 int na, ns;
472 int index, num_reg;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700473 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700474
475 if (!parent)
476 return;
477
478 p_op = to_of_device(parent);
479 bus = of_match_bus(p_op->node);
480 bus->count_cells(op->node, &na, &ns);
481
482 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
483 if (!preg || num_reg == 0)
484 return;
485
486 /* Convert to num-cells. */
487 num_reg /= 4;
488
David S. Miller46ba6d72006-07-16 22:10:44 -0700489 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700490 num_reg /= na + ns;
491
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700492 /* Prevent overrunning the op->resources[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700493 if (num_reg > PROMREG_MAX) {
494 printk(KERN_WARNING "%s: Too many regs (%d), "
495 "limiting to %d.\n",
496 op->node->full_name, num_reg, PROMREG_MAX);
497 num_reg = PROMREG_MAX;
498 }
499
David S. Millercf44bbc2006-06-29 14:34:50 -0700500 for (index = 0; index < num_reg; index++) {
501 struct resource *r = &op->resource[index];
502 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700503 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700504 struct device_node *dp = op->node;
505 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800506 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700507 u64 size, result = OF_BAD_ADDR;
508 unsigned long flags;
509 int dna, dns;
510 int pna, pns;
511
512 size = of_read_addr(reg + na, ns);
513 flags = bus->get_flags(reg);
514
515 memcpy(addr, reg, na * 4);
516
David S. Millera83f9822006-07-12 23:19:31 -0700517 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700518 result = of_read_addr(addr, na);
519 goto build_res;
520 }
521
522 dna = na;
523 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800524 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700525
526 while (1) {
527 dp = pp;
528 pp = dp->parent;
529 if (!pp) {
530 result = of_read_addr(addr, dna);
531 break;
532 }
533
534 pbus = of_match_bus(pp);
535 pbus->count_cells(dp, &pna, &pns);
536
David S. Millerb85cdd42007-02-28 23:20:12 -0800537 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700538 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700539 break;
540
541 dna = pna;
542 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800543 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700544 }
545
546 build_res:
547 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700548
549 if (of_resource_verbose)
550 printk("%s reg[%d] -> %lx\n",
551 op->node->full_name, index,
552 result);
553
David S. Millercf44bbc2006-06-29 14:34:50 -0700554 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700555 if (tlb_type == hypervisor)
556 result &= 0x0fffffffffffffffUL;
557
David S. Millercf44bbc2006-06-29 14:34:50 -0700558 r->start = result;
559 r->end = result + size - 1;
560 r->flags = flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700561 }
562 r->name = op->node->name;
563 }
564}
565
David S. Miller2b1e5972006-06-29 15:07:37 -0700566static struct device_node * __init
567apply_interrupt_map(struct device_node *dp, struct device_node *pp,
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700568 const u32 *imap, int imlen, const u32 *imask,
David S. Miller2b1e5972006-06-29 15:07:37 -0700569 unsigned int *irq_p)
570{
571 struct device_node *cp;
572 unsigned int irq = *irq_p;
573 struct of_bus *bus;
574 phandle handle;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700575 const u32 *reg;
David S. Miller2b1e5972006-06-29 15:07:37 -0700576 int na, num_reg, i;
577
578 bus = of_match_bus(pp);
579 bus->count_cells(dp, &na, NULL);
580
581 reg = of_get_property(dp, "reg", &num_reg);
582 if (!reg || !num_reg)
583 return NULL;
584
585 imlen /= ((na + 3) * 4);
586 handle = 0;
587 for (i = 0; i < imlen; i++) {
588 int j;
589
590 for (j = 0; j < na; j++) {
591 if ((reg[j] & imask[j]) != imap[j])
592 goto next;
593 }
594 if (imap[na] == irq) {
595 handle = imap[na + 1];
596 irq = imap[na + 2];
597 break;
598 }
599
600 next:
601 imap += (na + 3);
602 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700603 if (i == imlen) {
604 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
605 * properties that do not include the on-board device
606 * interrupts. Instead, the device's 'interrupts' property
607 * is already a fully specified INO value.
608 *
609 * Handle this by deciding that, if we didn't get a
610 * match in the parent's 'interrupt-map', and the
611 * parent is an IRQ translater, then use the parent as
612 * our IRQ controller.
613 */
614 if (pp->irq_trans)
615 return pp;
616
David S. Miller2b1e5972006-06-29 15:07:37 -0700617 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700618 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700619
620 *irq_p = irq;
621 cp = of_find_node_by_phandle(handle);
622
623 return cp;
624}
625
626static unsigned int __init pci_irq_swizzle(struct device_node *dp,
627 struct device_node *pp,
628 unsigned int irq)
629{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700630 const struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800631 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700632
633 if (irq < 1 || irq > 4)
634 return irq;
635
636 regs = of_get_property(dp, "reg", NULL);
637 if (!regs)
638 return irq;
639
David S. Millerbb4c18c2007-02-26 14:55:06 -0800640 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700641 devfn = (regs->phys_hi >> 8) & 0xff;
642 slot = (devfn >> 3) & 0x1f;
643
David S. Millerbb4c18c2007-02-26 14:55:06 -0800644 if (pp->irq_trans) {
645 /* Derived from Table 8-3, U2P User's Manual. This branch
646 * is handling a PCI controller that lacks a proper set of
647 * interrupt-map and interrupt-map-mask properties. The
648 * Ultra-E450 is one example.
649 *
650 * The bit layout is BSSLL, where:
651 * B: 0 on bus A, 1 on bus B
652 * D: 2-bit slot number, derived from PCI device number as
653 * (dev - 1) for bus A, or (dev - 2) for bus B
654 * L: 2-bit line number
David S. Millerbb4c18c2007-02-26 14:55:06 -0800655 */
656 if (bus & 0x80) {
657 /* PBM-A */
658 bus = 0x00;
659 slot = (slot - 1) << 2;
660 } else {
661 /* PBM-B */
662 bus = 0x10;
663 slot = (slot - 2) << 2;
664 }
665 irq -= 1;
666
667 ret = (bus | slot | irq);
668 } else {
669 /* Going through a PCI-PCI bridge that lacks a set of
670 * interrupt-map and interrupt-map-mask properties.
671 */
672 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
673 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700674
675 return ret;
676}
677
David S. Millera83f9822006-07-12 23:19:31 -0700678static int of_irq_verbose;
679
David S. Miller2b1e5972006-06-29 15:07:37 -0700680static unsigned int __init build_one_device_irq(struct of_device *op,
681 struct device *parent,
682 unsigned int irq)
683{
684 struct device_node *dp = op->node;
685 struct device_node *pp, *ip;
686 unsigned int orig_irq = irq;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700687 int nid;
David S. Miller2b1e5972006-06-29 15:07:37 -0700688
689 if (irq == 0xffffffff)
690 return irq;
691
692 if (dp->irq_trans) {
693 irq = dp->irq_trans->irq_build(dp, irq,
694 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700695
696 if (of_irq_verbose)
697 printk("%s: direct translate %x --> %x\n",
698 dp->full_name, orig_irq, irq);
699
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700700 goto out;
David S. Miller2b1e5972006-06-29 15:07:37 -0700701 }
702
703 /* Something more complicated. Walk up to the root, applying
704 * interrupt-map or bus specific translations, until we hit
705 * an IRQ translator.
706 *
707 * If we hit a bus type or situation we cannot handle, we
708 * stop and assume that the original IRQ number was in a
709 * format which has special meaning to it's immediate parent.
710 */
711 pp = dp->parent;
712 ip = NULL;
713 while (pp) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700714 const void *imap, *imsk;
David S. Miller2b1e5972006-06-29 15:07:37 -0700715 int imlen;
716
717 imap = of_get_property(pp, "interrupt-map", &imlen);
718 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
719 if (imap && imsk) {
720 struct device_node *iret;
721 int this_orig_irq = irq;
722
723 iret = apply_interrupt_map(dp, pp,
724 imap, imlen, imsk,
725 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700726
727 if (of_irq_verbose)
728 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
729 op->node->full_name,
730 pp->full_name, this_orig_irq,
731 (iret ? iret->full_name : "NULL"), irq);
732
David S. Miller2b1e5972006-06-29 15:07:37 -0700733 if (!iret)
734 break;
735
736 if (iret->irq_trans) {
737 ip = iret;
738 break;
739 }
740 } else {
741 if (!strcmp(pp->type, "pci") ||
742 !strcmp(pp->type, "pciex")) {
743 unsigned int this_orig_irq = irq;
744
745 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700746 if (of_irq_verbose)
747 printk("%s: PCI swizzle [%s] "
748 "%x --> %x\n",
749 op->node->full_name,
750 pp->full_name, this_orig_irq,
751 irq);
752
David S. Miller2b1e5972006-06-29 15:07:37 -0700753 }
754
755 if (pp->irq_trans) {
756 ip = pp;
757 break;
758 }
759 }
760 dp = pp;
761 pp = pp->parent;
762 }
763 if (!ip)
764 return orig_irq;
765
766 irq = ip->irq_trans->irq_build(op->node, irq,
767 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700768 if (of_irq_verbose)
769 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
770 op->node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700771
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700772out:
773 nid = of_node_to_nid(dp);
774 if (nid != -1) {
775 cpumask_t numa_mask = node_to_cpumask(nid);
776
777 irq_set_affinity(irq, numa_mask);
778 }
779
David S. Miller2b1e5972006-06-29 15:07:37 -0700780 return irq;
781}
782
David S. Millercf44bbc2006-06-29 14:34:50 -0700783static struct of_device * __init scan_one_device(struct device_node *dp,
784 struct device *parent)
785{
786 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700787 const unsigned int *irq;
David S. Miller3d6e4702007-07-18 22:03:25 -0700788 struct dev_archdata *sd;
David S. Miller2b1e5972006-06-29 15:07:37 -0700789 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700790
791 if (!op)
792 return NULL;
793
David S. Miller3d6e4702007-07-18 22:03:25 -0700794 sd = &op->dev.archdata;
795 sd->prom_node = dp;
796 sd->op = op;
797
David S. Millercf44bbc2006-06-29 14:34:50 -0700798 op->node = dp;
799
800 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
801 (25*1000*1000));
802 op->portid = of_getintprop_default(dp, "upa-portid", -1);
803 if (op->portid == -1)
804 op->portid = of_getintprop_default(dp, "portid", -1);
805
806 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700807 if (irq) {
808 memcpy(op->irqs, irq, len);
809 op->num_irqs = len / 4;
810 } else {
811 op->num_irqs = 0;
812 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700813
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700814 /* Prevent overrunning the op->irqs[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700815 if (op->num_irqs > PROMINTR_MAX) {
816 printk(KERN_WARNING "%s: Too many irqs (%d), "
817 "limiting to %d.\n",
818 dp->full_name, op->num_irqs, PROMINTR_MAX);
819 op->num_irqs = PROMINTR_MAX;
820 }
821
David S. Millercf44bbc2006-06-29 14:34:50 -0700822 build_device_resources(op, parent);
David S. Miller2b1e5972006-06-29 15:07:37 -0700823 for (i = 0; i < op->num_irqs; i++)
824 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700825
826 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000827 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700828 if (!parent)
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200829 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700830 else
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200831 dev_set_name(&op->dev, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700832
833 if (of_device_register(op)) {
834 printk("%s: Could not register of device.\n",
835 dp->full_name);
836 kfree(op);
837 op = NULL;
838 }
839
840 return op;
841}
842
843static void __init scan_tree(struct device_node *dp, struct device *parent)
844{
845 while (dp) {
846 struct of_device *op = scan_one_device(dp, parent);
847
848 if (op)
849 scan_tree(dp->child, &op->dev);
850
851 dp = dp->sibling;
852 }
853}
854
855static void __init scan_of_devices(void)
856{
857 struct device_node *root = of_find_node_by_path("/");
858 struct of_device *parent;
859
860 parent = scan_one_device(root, NULL);
861 if (!parent)
862 return;
863
864 scan_tree(root->child, &parent->dev);
865}
866
David S. Millera2bd4fd2006-06-23 01:44:10 -0700867static int __init of_bus_driver_init(void)
868{
David S. Millercf44bbc2006-06-29 14:34:50 -0700869 int err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700870
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000871 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millercf44bbc2006-06-29 14:34:50 -0700872 if (!err)
873 scan_of_devices();
874
875 return err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700876}
877
878postcore_initcall(of_bus_driver_init);
879
David S. Millera83f9822006-07-12 23:19:31 -0700880static int __init of_debug(char *str)
881{
882 int val = 0;
883
884 get_option(&str, &val);
885 if (val & 1)
886 of_resource_verbose = 1;
887 if (val & 2)
888 of_irq_verbose = 1;
889 return 1;
890}
891
892__setup("of_debug=", of_debug);