blob: 9743d1d9fa03fc06d89c33f86bf5d06c40ed404d [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. Millerc1b1a5f12008-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
Robert Reifc9f5b7e2009-06-04 02:00:02 -070013#include "of_device_common.h"
14
David S. Miller3ca9fab2006-06-29 14:35:33 -070015void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
16{
17 unsigned long ret = res->start + offset;
David S. Miller6bda5732006-10-18 23:00:35 -070018 struct resource *r;
David S. Miller3ca9fab2006-06-29 14:35:33 -070019
David S. Miller6bda5732006-10-18 23:00:35 -070020 if (res->flags & IORESOURCE_MEM)
21 r = request_mem_region(ret, size, name);
22 else
23 r = request_region(ret, size, name);
24 if (!r)
David S. Miller3ca9fab2006-06-29 14:35:33 -070025 ret = 0;
26
27 return (void __iomem *) ret;
28}
29EXPORT_SYMBOL(of_ioremap);
30
David S. Millere3a411a32006-12-28 21:01:32 -080031void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -070032{
David S. Millere3a411a32006-12-28 21:01:32 -080033 if (res->flags & IORESOURCE_MEM)
34 release_mem_region((unsigned long) base, size);
35 else
36 release_region((unsigned long) base, size);
David S. Miller3ca9fab2006-06-29 14:35:33 -070037}
38EXPORT_SYMBOL(of_iounmap);
39
David S. Millercf44bbc2006-06-29 14:34:50 -070040/*
41 * PCI bus specific translator
42 */
43
44static int of_bus_pci_match(struct device_node *np)
45{
David S. Miller7ee766d2008-09-20 22:00:40 -070046 if (!strcmp(np->name, "pci")) {
David S. Millera165b422007-03-29 01:50:16 -070047 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -080048
49 if (model && !strcmp(model, "SUNW,simba"))
50 return 0;
51
David S. Millera83f9822006-07-12 23:19:31 -070052 /* Do not do PCI specific frobbing if the
53 * PCI bridge lacks a ranges property. We
54 * want to pass it through up to the next
55 * parent as-is, not with the PCI translate
56 * method which chops off the top address cell.
57 */
58 if (!of_find_property(np, "ranges", NULL))
59 return 0;
60
61 return 1;
62 }
63
64 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -070065}
66
David S. Miller01f94c42007-03-04 12:53:19 -080067static int of_bus_simba_match(struct device_node *np)
68{
David S. Millera165b422007-03-29 01:50:16 -070069 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -080070
71 if (model && !strcmp(model, "SUNW,simba"))
72 return 1;
David S. Miller8c2786c2007-06-07 21:59:44 -070073
74 /* Treat PCI busses lacking ranges property just like
75 * simba.
76 */
David S. Miller7ee766d2008-09-20 22:00:40 -070077 if (!strcmp(np->name, "pci")) {
David S. Miller8c2786c2007-06-07 21:59:44 -070078 if (!of_find_property(np, "ranges", NULL))
79 return 1;
80 }
81
David S. Miller01f94c42007-03-04 12:53:19 -080082 return 0;
83}
84
85static int of_bus_simba_map(u32 *addr, const u32 *range,
86 int na, int ns, int pna)
87{
88 return 0;
89}
90
David S. Millercf44bbc2006-06-29 14:34:50 -070091static void of_bus_pci_count_cells(struct device_node *np,
92 int *addrc, int *sizec)
93{
94 if (addrc)
95 *addrc = 3;
96 if (sizec)
97 *sizec = 2;
98}
99
David S. Millera83f9822006-07-12 23:19:31 -0700100static int of_bus_pci_map(u32 *addr, const u32 *range,
101 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700102{
David S. Millera83f9822006-07-12 23:19:31 -0700103 u32 result[OF_MAX_ADDR_CELLS];
104 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700105
106 /* Check address type match */
David S. Miller4230fa32009-12-09 01:39:09 -0800107 if (!((addr[0] ^ range[0]) & 0x03000000))
108 goto type_match;
David S. Millercf44bbc2006-06-29 14:34:50 -0700109
David S. Miller4230fa32009-12-09 01:39:09 -0800110 /* Special exception, we can map a 64-bit address into
111 * a 32-bit range.
112 */
113 if ((addr[0] & 0x03000000) == 0x03000000 &&
114 (range[0] & 0x03000000) == 0x02000000)
115 goto type_match;
116
117 return -EINVAL;
118
119type_match:
David S. Millera83f9822006-07-12 23:19:31 -0700120 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
121 na - 1, ns))
122 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700123
David S. Millera83f9822006-07-12 23:19:31 -0700124 /* Start with the parent range base. */
125 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700126
David S. Millera83f9822006-07-12 23:19:31 -0700127 /* Add in the child address offset, skipping high cell. */
128 for (i = 0; i < na - 1; i++)
129 result[pna - 1 - i] +=
130 (addr[na - 1 - i] -
131 range[na - 1 - i]);
132
133 memcpy(addr, result, pna * 4);
134
135 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700136}
137
David S. Millere3c71a32008-08-28 21:02:58 -0700138static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -0700139{
David S. Millercf44bbc2006-06-29 14:34:50 -0700140 u32 w = addr[0];
141
David S. Millere3c71a32008-08-28 21:02:58 -0700142 /* For PCI, we override whatever child busses may have used. */
143 flags = 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700144 switch((w >> 24) & 0x03) {
145 case 0x01:
146 flags |= IORESOURCE_IO;
David S. Millere3c71a32008-08-28 21:02:58 -0700147 break;
148
David S. Millercf44bbc2006-06-29 14:34:50 -0700149 case 0x02: /* 32 bits */
150 case 0x03: /* 64 bits */
151 flags |= IORESOURCE_MEM;
David S. Millere3c71a32008-08-28 21:02:58 -0700152 break;
David S. Millercf44bbc2006-06-29 14:34:50 -0700153 }
154 if (w & 0x40000000)
155 flags |= IORESOURCE_PREFETCH;
156 return flags;
157}
158
159/*
David S. Miller4130a4b2006-10-25 22:31:06 -0700160 * FHC/Central bus specific translator.
161 *
162 * This is just needed to hard-code the address and size cell
163 * counts. 'fhc' and 'central' nodes lack the #address-cells and
164 * #size-cells properties, and if you walk to the root on such
165 * Enterprise boxes all you'll get is a #size-cells of 2 which is
166 * not what we want to use.
167 */
168static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700169{
David S. Miller4130a4b2006-10-25 22:31:06 -0700170 return !strcmp(np->name, "fhc") ||
171 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700172}
173
David S. Miller4130a4b2006-10-25 22:31:06 -0700174#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700175
176/*
177 * Array of bus specific translators
178 */
179
180static struct of_bus of_busses[] = {
181 /* PCI */
182 {
183 .name = "pci",
184 .addr_prop_name = "assigned-addresses",
185 .match = of_bus_pci_match,
186 .count_cells = of_bus_pci_count_cells,
187 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700188 .get_flags = of_bus_pci_get_flags,
189 },
David S. Miller01f94c42007-03-04 12:53:19 -0800190 /* SIMBA */
191 {
192 .name = "simba",
193 .addr_prop_name = "assigned-addresses",
194 .match = of_bus_simba_match,
195 .count_cells = of_bus_pci_count_cells,
196 .map = of_bus_simba_map,
197 .get_flags = of_bus_pci_get_flags,
198 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700199 /* SBUS */
200 {
201 .name = "sbus",
202 .addr_prop_name = "reg",
203 .match = of_bus_sbus_match,
204 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700205 .map = of_bus_default_map,
206 .get_flags = of_bus_default_get_flags,
207 },
208 /* FHC */
209 {
210 .name = "fhc",
211 .addr_prop_name = "reg",
212 .match = of_bus_fhc_match,
213 .count_cells = of_bus_fhc_count_cells,
214 .map = of_bus_default_map,
215 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700216 },
217 /* Default */
218 {
219 .name = "default",
220 .addr_prop_name = "reg",
221 .match = NULL,
222 .count_cells = of_bus_default_count_cells,
223 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700224 .get_flags = of_bus_default_get_flags,
225 },
226};
227
228static struct of_bus *of_match_bus(struct device_node *np)
229{
230 int i;
231
232 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
233 if (!of_busses[i].match || of_busses[i].match(np))
234 return &of_busses[i];
235 BUG();
236 return NULL;
237}
238
239static int __init build_one_resource(struct device_node *parent,
240 struct of_bus *bus,
241 struct of_bus *pbus,
242 u32 *addr,
243 int na, int ns, int pna)
244{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700245 const u32 *ranges;
David S. Miller21cd8832008-09-11 23:53:41 -0700246 int rone, rlen;
David S. Millercf44bbc2006-06-29 14:34:50 -0700247
248 ranges = of_get_property(parent, "ranges", &rlen);
249 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700250 u32 result[OF_MAX_ADDR_CELLS];
251 int i;
252
253 memset(result, 0, pna * 4);
254 for (i = 0; i < na; i++)
255 result[pna - 1 - i] =
256 addr[na - 1 - i];
257
258 memcpy(addr, result, pna * 4);
259 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700260 }
261
262 /* Now walk through the ranges */
263 rlen /= 4;
264 rone = na + pna + ns;
265 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700266 if (!bus->map(addr, ranges, na, ns, pna))
267 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700268 }
David S. Millera83f9822006-07-12 23:19:31 -0700269
David S. Miller49d23cf2007-05-13 22:01:18 -0700270 /* When we miss an I/O space match on PCI, just pass it up
271 * to the next PCI bridge and/or controller.
272 */
273 if (!strcmp(bus->name, "pci") &&
274 (addr[0] & 0x03000000) == 0x01000000)
275 return 0;
276
David S. Millera83f9822006-07-12 23:19:31 -0700277 return 1;
278}
279
280static int __init use_1to1_mapping(struct device_node *pp)
281{
David S. Millera83f9822006-07-12 23:19:31 -0700282 /* If we have a ranges property in the parent, use it. */
283 if (of_find_property(pp, "ranges", NULL) != NULL)
284 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700285
David S. Millera83f9822006-07-12 23:19:31 -0700286 /* If the parent is the dma node of an ISA bus, pass
287 * the translation up to the root.
David S. Miller52802672008-09-03 02:04:41 -0700288 *
289 * Some SBUS devices use intermediate nodes to express
290 * hierarchy within the device itself. These aren't
291 * real bus nodes, and don't have a 'ranges' property.
292 * But, we should still pass the translation work up
293 * to the SBUS itself.
David S. Millera83f9822006-07-12 23:19:31 -0700294 */
David S. Miller52802672008-09-03 02:04:41 -0700295 if (!strcmp(pp->name, "dma") ||
296 !strcmp(pp->name, "espdma") ||
297 !strcmp(pp->name, "ledma") ||
298 !strcmp(pp->name, "lebuffer"))
David S. Millera83f9822006-07-12 23:19:31 -0700299 return 0;
300
David S. Miller8c2786c2007-06-07 21:59:44 -0700301 /* Similarly for all PCI bridges, if we get this far
302 * it lacks a ranges property, and this will include
303 * cases like Simba.
304 */
David S. Miller7ee766d2008-09-20 22:00:40 -0700305 if (!strcmp(pp->name, "pci"))
David S. Millera83f9822006-07-12 23:19:31 -0700306 return 0;
307
308 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700309}
310
David S. Millera83f9822006-07-12 23:19:31 -0700311static int of_resource_verbose;
312
David S. Millercf44bbc2006-06-29 14:34:50 -0700313static void __init build_device_resources(struct of_device *op,
314 struct device *parent)
315{
316 struct of_device *p_op;
317 struct of_bus *bus;
318 int na, ns;
319 int index, num_reg;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700320 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700321
322 if (!parent)
323 return;
324
325 p_op = to_of_device(parent);
Grant Likely61c7a082010-04-13 16:12:29 -0700326 bus = of_match_bus(p_op->dev.of_node);
327 bus->count_cells(op->dev.of_node, &na, &ns);
David S. Millercf44bbc2006-06-29 14:34:50 -0700328
Grant Likely61c7a082010-04-13 16:12:29 -0700329 preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
David S. Millercf44bbc2006-06-29 14:34:50 -0700330 if (!preg || num_reg == 0)
331 return;
332
333 /* Convert to num-cells. */
334 num_reg /= 4;
335
David S. Miller46ba6d72006-07-16 22:10:44 -0700336 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700337 num_reg /= na + ns;
338
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700339 /* Prevent overrunning the op->resources[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700340 if (num_reg > PROMREG_MAX) {
341 printk(KERN_WARNING "%s: Too many regs (%d), "
342 "limiting to %d.\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700343 op->dev.of_node->full_name, num_reg, PROMREG_MAX);
David S. Miller46ba6d72006-07-16 22:10:44 -0700344 num_reg = PROMREG_MAX;
345 }
346
Grant Likely1636f8a2010-06-18 11:09:58 -0600347 op->resource = op->archdata.resource;
348 op->num_resources = num_reg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700349 for (index = 0; index < num_reg; index++) {
350 struct resource *r = &op->resource[index];
351 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700352 const u32 *reg = (preg + (index * ((na + ns) * 4)));
Grant Likely61c7a082010-04-13 16:12:29 -0700353 struct device_node *dp = op->dev.of_node;
354 struct device_node *pp = p_op->dev.of_node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800355 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700356 u64 size, result = OF_BAD_ADDR;
357 unsigned long flags;
358 int dna, dns;
359 int pna, pns;
360
361 size = of_read_addr(reg + na, ns);
David S. Millercf44bbc2006-06-29 14:34:50 -0700362 memcpy(addr, reg, na * 4);
363
David S. Millere3c71a32008-08-28 21:02:58 -0700364 flags = bus->get_flags(addr, 0);
365
David S. Millera83f9822006-07-12 23:19:31 -0700366 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700367 result = of_read_addr(addr, na);
368 goto build_res;
369 }
370
371 dna = na;
372 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800373 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700374
375 while (1) {
376 dp = pp;
377 pp = dp->parent;
378 if (!pp) {
379 result = of_read_addr(addr, dna);
380 break;
381 }
382
383 pbus = of_match_bus(pp);
384 pbus->count_cells(dp, &pna, &pns);
385
David S. Millerb85cdd42007-02-28 23:20:12 -0800386 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700387 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700388 break;
389
David S. Millere3c71a32008-08-28 21:02:58 -0700390 flags = pbus->get_flags(addr, flags);
391
David S. Millercf44bbc2006-06-29 14:34:50 -0700392 dna = pna;
393 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800394 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700395 }
396
397 build_res:
398 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700399
400 if (of_resource_verbose)
Sam Ravnborg90181132009-01-06 13:19:28 -0800401 printk("%s reg[%d] -> %llx\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700402 op->dev.of_node->full_name, index,
David S. Millera83f9822006-07-12 23:19:31 -0700403 result);
404
David S. Millercf44bbc2006-06-29 14:34:50 -0700405 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700406 if (tlb_type == hypervisor)
407 result &= 0x0fffffffffffffffUL;
408
David S. Millercf44bbc2006-06-29 14:34:50 -0700409 r->start = result;
410 r->end = result + size - 1;
411 r->flags = flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700412 }
Grant Likely61c7a082010-04-13 16:12:29 -0700413 r->name = op->dev.of_node->name;
David S. Millercf44bbc2006-06-29 14:34:50 -0700414 }
415}
416
David S. Miller2b1e5972006-06-29 15:07:37 -0700417static struct device_node * __init
418apply_interrupt_map(struct device_node *dp, struct device_node *pp,
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700419 const u32 *imap, int imlen, const u32 *imask,
David S. Miller2b1e5972006-06-29 15:07:37 -0700420 unsigned int *irq_p)
421{
422 struct device_node *cp;
423 unsigned int irq = *irq_p;
424 struct of_bus *bus;
425 phandle handle;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700426 const u32 *reg;
David S. Miller2b1e5972006-06-29 15:07:37 -0700427 int na, num_reg, i;
428
429 bus = of_match_bus(pp);
430 bus->count_cells(dp, &na, NULL);
431
432 reg = of_get_property(dp, "reg", &num_reg);
433 if (!reg || !num_reg)
434 return NULL;
435
436 imlen /= ((na + 3) * 4);
437 handle = 0;
438 for (i = 0; i < imlen; i++) {
439 int j;
440
441 for (j = 0; j < na; j++) {
442 if ((reg[j] & imask[j]) != imap[j])
443 goto next;
444 }
445 if (imap[na] == irq) {
446 handle = imap[na + 1];
447 irq = imap[na + 2];
448 break;
449 }
450
451 next:
452 imap += (na + 3);
453 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700454 if (i == imlen) {
455 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
456 * properties that do not include the on-board device
457 * interrupts. Instead, the device's 'interrupts' property
458 * is already a fully specified INO value.
459 *
460 * Handle this by deciding that, if we didn't get a
461 * match in the parent's 'interrupt-map', and the
462 * parent is an IRQ translater, then use the parent as
463 * our IRQ controller.
464 */
465 if (pp->irq_trans)
466 return pp;
467
David S. Miller2b1e5972006-06-29 15:07:37 -0700468 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700469 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700470
471 *irq_p = irq;
472 cp = of_find_node_by_phandle(handle);
473
474 return cp;
475}
476
477static unsigned int __init pci_irq_swizzle(struct device_node *dp,
478 struct device_node *pp,
479 unsigned int irq)
480{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700481 const struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800482 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700483
484 if (irq < 1 || irq > 4)
485 return irq;
486
487 regs = of_get_property(dp, "reg", NULL);
488 if (!regs)
489 return irq;
490
David S. Millerbb4c18c2007-02-26 14:55:06 -0800491 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700492 devfn = (regs->phys_hi >> 8) & 0xff;
493 slot = (devfn >> 3) & 0x1f;
494
David S. Millerbb4c18c2007-02-26 14:55:06 -0800495 if (pp->irq_trans) {
496 /* Derived from Table 8-3, U2P User's Manual. This branch
497 * is handling a PCI controller that lacks a proper set of
498 * interrupt-map and interrupt-map-mask properties. The
499 * Ultra-E450 is one example.
500 *
501 * The bit layout is BSSLL, where:
502 * B: 0 on bus A, 1 on bus B
503 * D: 2-bit slot number, derived from PCI device number as
504 * (dev - 1) for bus A, or (dev - 2) for bus B
505 * L: 2-bit line number
David S. Millerbb4c18c2007-02-26 14:55:06 -0800506 */
507 if (bus & 0x80) {
508 /* PBM-A */
509 bus = 0x00;
510 slot = (slot - 1) << 2;
511 } else {
512 /* PBM-B */
513 bus = 0x10;
514 slot = (slot - 2) << 2;
515 }
516 irq -= 1;
517
518 ret = (bus | slot | irq);
519 } else {
520 /* Going through a PCI-PCI bridge that lacks a set of
521 * interrupt-map and interrupt-map-mask properties.
522 */
523 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
524 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700525
526 return ret;
527}
528
David S. Millera83f9822006-07-12 23:19:31 -0700529static int of_irq_verbose;
530
David S. Miller2b1e5972006-06-29 15:07:37 -0700531static unsigned int __init build_one_device_irq(struct of_device *op,
532 struct device *parent,
533 unsigned int irq)
534{
Grant Likely61c7a082010-04-13 16:12:29 -0700535 struct device_node *dp = op->dev.of_node;
David S. Miller2b1e5972006-06-29 15:07:37 -0700536 struct device_node *pp, *ip;
537 unsigned int orig_irq = irq;
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700538 int nid;
David S. Miller2b1e5972006-06-29 15:07:37 -0700539
540 if (irq == 0xffffffff)
541 return irq;
542
543 if (dp->irq_trans) {
544 irq = dp->irq_trans->irq_build(dp, irq,
545 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700546
547 if (of_irq_verbose)
548 printk("%s: direct translate %x --> %x\n",
549 dp->full_name, orig_irq, irq);
550
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700551 goto out;
David S. Miller2b1e5972006-06-29 15:07:37 -0700552 }
553
554 /* Something more complicated. Walk up to the root, applying
555 * interrupt-map or bus specific translations, until we hit
556 * an IRQ translator.
557 *
558 * If we hit a bus type or situation we cannot handle, we
559 * stop and assume that the original IRQ number was in a
560 * format which has special meaning to it's immediate parent.
561 */
562 pp = dp->parent;
563 ip = NULL;
564 while (pp) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700565 const void *imap, *imsk;
David S. Miller2b1e5972006-06-29 15:07:37 -0700566 int imlen;
567
568 imap = of_get_property(pp, "interrupt-map", &imlen);
569 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
570 if (imap && imsk) {
571 struct device_node *iret;
572 int this_orig_irq = irq;
573
574 iret = apply_interrupt_map(dp, pp,
575 imap, imlen, imsk,
576 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700577
578 if (of_irq_verbose)
579 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700580 op->dev.of_node->full_name,
David S. Millera83f9822006-07-12 23:19:31 -0700581 pp->full_name, this_orig_irq,
582 (iret ? iret->full_name : "NULL"), irq);
583
David S. Miller2b1e5972006-06-29 15:07:37 -0700584 if (!iret)
585 break;
586
587 if (iret->irq_trans) {
588 ip = iret;
589 break;
590 }
591 } else {
David S. Miller7ee766d2008-09-20 22:00:40 -0700592 if (!strcmp(pp->name, "pci")) {
David S. Miller2b1e5972006-06-29 15:07:37 -0700593 unsigned int this_orig_irq = irq;
594
595 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700596 if (of_irq_verbose)
597 printk("%s: PCI swizzle [%s] "
598 "%x --> %x\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700599 op->dev.of_node->full_name,
David S. Millera83f9822006-07-12 23:19:31 -0700600 pp->full_name, this_orig_irq,
601 irq);
602
David S. Miller2b1e5972006-06-29 15:07:37 -0700603 }
604
605 if (pp->irq_trans) {
606 ip = pp;
607 break;
608 }
609 }
610 dp = pp;
611 pp = pp->parent;
612 }
613 if (!ip)
614 return orig_irq;
615
Grant Likely61c7a082010-04-13 16:12:29 -0700616 irq = ip->irq_trans->irq_build(op->dev.of_node, irq,
David S. Miller2b1e5972006-06-29 15:07:37 -0700617 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700618 if (of_irq_verbose)
619 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700620 op->dev.of_node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700621
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700622out:
623 nid = of_node_to_nid(dp);
624 if (nid != -1) {
Rusty Russell96d76a72008-12-26 22:23:38 +1030625 cpumask_t numa_mask = *cpumask_of_node(nid);
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700626
Rusty Russell0de26522008-12-13 21:20:26 +1030627 irq_set_affinity(irq, &numa_mask);
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700628 }
629
David S. Miller2b1e5972006-06-29 15:07:37 -0700630 return irq;
631}
632
David S. Millercf44bbc2006-06-29 14:34:50 -0700633static struct of_device * __init scan_one_device(struct device_node *dp,
634 struct device *parent)
635{
636 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700637 const unsigned int *irq;
David S. Miller3d6e4702007-07-18 22:03:25 -0700638 struct dev_archdata *sd;
David S. Miller2b1e5972006-06-29 15:07:37 -0700639 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700640
641 if (!op)
642 return NULL;
643
David S. Miller3d6e4702007-07-18 22:03:25 -0700644 sd = &op->dev.archdata;
David S. Miller3d6e4702007-07-18 22:03:25 -0700645 sd->op = op;
646
Grant Likelyd706c1b2010-04-13 16:12:28 -0700647 op->dev.of_node = dp;
David S. Millercf44bbc2006-06-29 14:34:50 -0700648
David S. Millercf44bbc2006-06-29 14:34:50 -0700649 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700650 if (irq) {
Grant Likely1636f8a2010-06-18 11:09:58 -0600651 op->archdata.num_irqs = len / 4;
Robert Reif92d90912008-12-26 15:39:11 -0800652
653 /* Prevent overrunning the op->irqs[] array. */
Grant Likely1636f8a2010-06-18 11:09:58 -0600654 if (op->archdata.num_irqs > PROMINTR_MAX) {
Robert Reif92d90912008-12-26 15:39:11 -0800655 printk(KERN_WARNING "%s: Too many irqs (%d), "
656 "limiting to %d.\n",
Grant Likely1636f8a2010-06-18 11:09:58 -0600657 dp->full_name, op->archdata.num_irqs, PROMINTR_MAX);
658 op->archdata.num_irqs = PROMINTR_MAX;
Robert Reif92d90912008-12-26 15:39:11 -0800659 }
Grant Likely1636f8a2010-06-18 11:09:58 -0600660 memcpy(op->archdata.irqs, irq, op->archdata.num_irqs * 4);
David S. Miller2b1e5972006-06-29 15:07:37 -0700661 } else {
Grant Likely1636f8a2010-06-18 11:09:58 -0600662 op->archdata.num_irqs = 0;
David S. Miller2b1e5972006-06-29 15:07:37 -0700663 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700664
665 build_device_resources(op, parent);
Grant Likely1636f8a2010-06-18 11:09:58 -0600666 for (i = 0; i < op->archdata.num_irqs; i++)
667 op->archdata.irqs[i] = build_one_device_irq(op, parent, op->archdata.irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700668
669 op->dev.parent = parent;
Grant Likelyeca39302010-06-08 07:48:21 -0600670 op->dev.bus = &platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700671 if (!parent)
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200672 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700673 else
Grant Likely6016a362010-01-28 14:06:53 -0700674 dev_set_name(&op->dev, "%08x", dp->phandle);
David S. Millercf44bbc2006-06-29 14:34:50 -0700675
676 if (of_device_register(op)) {
677 printk("%s: Could not register of device.\n",
678 dp->full_name);
679 kfree(op);
680 op = NULL;
681 }
682
683 return op;
684}
685
686static void __init scan_tree(struct device_node *dp, struct device *parent)
687{
688 while (dp) {
689 struct of_device *op = scan_one_device(dp, parent);
690
691 if (op)
692 scan_tree(dp->child, &op->dev);
693
694 dp = dp->sibling;
695 }
696}
697
Grant Likelyeca39302010-06-08 07:48:21 -0600698static int __init scan_of_devices(void)
David S. Millercf44bbc2006-06-29 14:34:50 -0700699{
700 struct device_node *root = of_find_node_by_path("/");
701 struct of_device *parent;
702
703 parent = scan_one_device(root, NULL);
704 if (!parent)
Grant Likelyeca39302010-06-08 07:48:21 -0600705 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700706
707 scan_tree(root->child, &parent->dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600708 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700709}
Grant Likelyeca39302010-06-08 07:48:21 -0600710postcore_initcall(scan_of_devices);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700711
David S. Millera83f9822006-07-12 23:19:31 -0700712static int __init of_debug(char *str)
713{
714 int val = 0;
715
716 get_option(&str, &val);
717 if (val & 1)
718 of_resource_verbose = 1;
719 if (val & 2)
720 of_irq_verbose = 1;
721 return 1;
722}
723
724__setup("of_debug=", of_debug);