blob: f845f150f565b26a019dea86afa7a6b9d2970ce7 [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. Millera2bd4fd2006-06-23 01:44:10 -070058#ifdef CONFIG_PCI
Stephen Rothwell3f23de12007-05-03 02:38:57 +100059struct bus_type ebus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070060EXPORT_SYMBOL(ebus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070061#endif
62
63#ifdef CONFIG_SBUS
Stephen Rothwell3f23de12007-05-03 02:38:57 +100064struct bus_type sbus_bus_type;
David S. Miller69853912006-06-25 01:21:38 -070065EXPORT_SYMBOL(sbus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -070066#endif
67
Stephen Rothwell3f23de12007-05-03 02:38:57 +100068struct bus_type of_platform_bus_type;
Stephen Rothwell37b77542007-04-30 17:43:56 +100069EXPORT_SYMBOL(of_platform_bus_type);
David S. Millercf44bbc2006-06-29 14:34:50 -070070
David S. Millera83f9822006-07-12 23:19:31 -070071static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -070072{
73 u64 r = 0;
74 while (size--)
75 r = (r << 32) | *(cell++);
76 return r;
77}
78
79static void __init get_cells(struct device_node *dp,
80 int *addrc, int *sizec)
81{
82 if (addrc)
83 *addrc = of_n_addr_cells(dp);
84 if (sizec)
85 *sizec = of_n_size_cells(dp);
86}
87
88/* Max address size we deal with */
89#define OF_MAX_ADDR_CELLS 4
90
91struct of_bus {
92 const char *name;
93 const char *addr_prop_name;
94 int (*match)(struct device_node *parent);
95 void (*count_cells)(struct device_node *child,
96 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -070097 int (*map)(u32 *addr, const u32 *range,
98 int na, int ns, int pna);
David S. Millere3c71a32008-08-28 21:02:58 -070099 unsigned long (*get_flags)(const u32 *addr, unsigned long);
David S. Millercf44bbc2006-06-29 14:34:50 -0700100};
101
102/*
103 * Default translator (generic bus)
104 */
105
106static void of_bus_default_count_cells(struct device_node *dev,
107 int *addrc, int *sizec)
108{
109 get_cells(dev, addrc, sizec);
110}
111
David S. Millera83f9822006-07-12 23:19:31 -0700112/* Make sure the least significant 64-bits are in-range. Even
113 * for 3 or 4 cell values it is a good enough approximation.
114 */
115static int of_out_of_range(const u32 *addr, const u32 *base,
116 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700117{
118 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700119 u64 b = of_read_addr(base, na);
120
121 if (a < b)
122 return 1;
123
124 b += of_read_addr(size, ns);
125 if (a >= b)
126 return 1;
127
128 return 0;
129}
130
131static int of_bus_default_map(u32 *addr, const u32 *range,
132 int na, int ns, int pna)
133{
134 u32 result[OF_MAX_ADDR_CELLS];
135 int i;
136
137 if (ns > 2) {
138 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
139 return -EINVAL;
140 }
141
142 if (of_out_of_range(addr, range, range + na + pna, na, ns))
143 return -EINVAL;
144
145 /* Start with the parent range base. */
146 memcpy(result, range + na, pna * 4);
147
148 /* Add in the child address offset. */
149 for (i = 0; i < na; i++)
150 result[pna - 1 - i] +=
151 (addr[na - 1 - i] -
152 range[na - 1 - i]);
153
154 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700155
156 return 0;
157}
158
David S. Millere3c71a32008-08-28 21:02:58 -0700159static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -0700160{
David S. Millere3c71a32008-08-28 21:02:58 -0700161 if (flags)
162 return flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700163 return IORESOURCE_MEM;
164}
165
David S. Millercf44bbc2006-06-29 14:34:50 -0700166/*
167 * PCI bus specific translator
168 */
169
170static int of_bus_pci_match(struct device_node *np)
171{
David S. Millera83f9822006-07-12 23:19:31 -0700172 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
David S. Millera165b422007-03-29 01:50:16 -0700173 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800174
175 if (model && !strcmp(model, "SUNW,simba"))
176 return 0;
177
David S. Millera83f9822006-07-12 23:19:31 -0700178 /* Do not do PCI specific frobbing if the
179 * PCI bridge lacks a ranges property. We
180 * want to pass it through up to the next
181 * parent as-is, not with the PCI translate
182 * method which chops off the top address cell.
183 */
184 if (!of_find_property(np, "ranges", NULL))
185 return 0;
186
187 return 1;
188 }
189
190 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700191}
192
David S. Miller01f94c42007-03-04 12:53:19 -0800193static int of_bus_simba_match(struct device_node *np)
194{
David S. Millera165b422007-03-29 01:50:16 -0700195 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -0800196
197 if (model && !strcmp(model, "SUNW,simba"))
198 return 1;
David S. Miller8c2786c2007-06-07 21:59:44 -0700199
200 /* Treat PCI busses lacking ranges property just like
201 * simba.
202 */
203 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
204 if (!of_find_property(np, "ranges", NULL))
205 return 1;
206 }
207
David S. Miller01f94c42007-03-04 12:53:19 -0800208 return 0;
209}
210
211static int of_bus_simba_map(u32 *addr, const u32 *range,
212 int na, int ns, int pna)
213{
214 return 0;
215}
216
David S. Millercf44bbc2006-06-29 14:34:50 -0700217static void of_bus_pci_count_cells(struct device_node *np,
218 int *addrc, int *sizec)
219{
220 if (addrc)
221 *addrc = 3;
222 if (sizec)
223 *sizec = 2;
224}
225
David S. Millera83f9822006-07-12 23:19:31 -0700226static int of_bus_pci_map(u32 *addr, const u32 *range,
227 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700228{
David S. Millera83f9822006-07-12 23:19:31 -0700229 u32 result[OF_MAX_ADDR_CELLS];
230 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700231
232 /* Check address type match */
233 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700234 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700235
David S. Millera83f9822006-07-12 23:19:31 -0700236 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
237 na - 1, ns))
238 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700239
David S. Millera83f9822006-07-12 23:19:31 -0700240 /* Start with the parent range base. */
241 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700242
David S. Millera83f9822006-07-12 23:19:31 -0700243 /* Add in the child address offset, skipping high cell. */
244 for (i = 0; i < na - 1; i++)
245 result[pna - 1 - i] +=
246 (addr[na - 1 - i] -
247 range[na - 1 - i]);
248
249 memcpy(addr, result, pna * 4);
250
251 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700252}
253
David S. Millere3c71a32008-08-28 21:02:58 -0700254static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -0700255{
David S. Millercf44bbc2006-06-29 14:34:50 -0700256 u32 w = addr[0];
257
David S. Millere3c71a32008-08-28 21:02:58 -0700258 /* For PCI, we override whatever child busses may have used. */
259 flags = 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700260 switch((w >> 24) & 0x03) {
261 case 0x01:
262 flags |= IORESOURCE_IO;
David S. Millere3c71a32008-08-28 21:02:58 -0700263 break;
264
David S. Millercf44bbc2006-06-29 14:34:50 -0700265 case 0x02: /* 32 bits */
266 case 0x03: /* 64 bits */
267 flags |= IORESOURCE_MEM;
David S. Millere3c71a32008-08-28 21:02:58 -0700268 break;
David S. Millercf44bbc2006-06-29 14:34:50 -0700269 }
270 if (w & 0x40000000)
271 flags |= IORESOURCE_PREFETCH;
272 return flags;
273}
274
275/*
David S. Millercf44bbc2006-06-29 14:34:50 -0700276 * SBUS bus specific translator
277 */
278
279static int of_bus_sbus_match(struct device_node *np)
280{
281 return !strcmp(np->name, "sbus") ||
282 !strcmp(np->name, "sbi");
283}
284
285static void of_bus_sbus_count_cells(struct device_node *child,
286 int *addrc, int *sizec)
287{
288 if (addrc)
289 *addrc = 2;
290 if (sizec)
291 *sizec = 1;
292}
293
David S. Miller4130a4b2006-10-25 22:31:06 -0700294/*
295 * FHC/Central bus specific translator.
296 *
297 * This is just needed to hard-code the address and size cell
298 * counts. 'fhc' and 'central' nodes lack the #address-cells and
299 * #size-cells properties, and if you walk to the root on such
300 * Enterprise boxes all you'll get is a #size-cells of 2 which is
301 * not what we want to use.
302 */
303static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700304{
David S. Miller4130a4b2006-10-25 22:31:06 -0700305 return !strcmp(np->name, "fhc") ||
306 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700307}
308
David S. Miller4130a4b2006-10-25 22:31:06 -0700309#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700310
311/*
312 * Array of bus specific translators
313 */
314
315static struct of_bus of_busses[] = {
316 /* PCI */
317 {
318 .name = "pci",
319 .addr_prop_name = "assigned-addresses",
320 .match = of_bus_pci_match,
321 .count_cells = of_bus_pci_count_cells,
322 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700323 .get_flags = of_bus_pci_get_flags,
324 },
David S. Miller01f94c42007-03-04 12:53:19 -0800325 /* SIMBA */
326 {
327 .name = "simba",
328 .addr_prop_name = "assigned-addresses",
329 .match = of_bus_simba_match,
330 .count_cells = of_bus_pci_count_cells,
331 .map = of_bus_simba_map,
332 .get_flags = of_bus_pci_get_flags,
333 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700334 /* SBUS */
335 {
336 .name = "sbus",
337 .addr_prop_name = "reg",
338 .match = of_bus_sbus_match,
339 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700340 .map = of_bus_default_map,
341 .get_flags = of_bus_default_get_flags,
342 },
343 /* FHC */
344 {
345 .name = "fhc",
346 .addr_prop_name = "reg",
347 .match = of_bus_fhc_match,
348 .count_cells = of_bus_fhc_count_cells,
349 .map = of_bus_default_map,
350 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700351 },
352 /* Default */
353 {
354 .name = "default",
355 .addr_prop_name = "reg",
356 .match = NULL,
357 .count_cells = of_bus_default_count_cells,
358 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700359 .get_flags = of_bus_default_get_flags,
360 },
361};
362
363static struct of_bus *of_match_bus(struct device_node *np)
364{
365 int i;
366
367 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
368 if (!of_busses[i].match || of_busses[i].match(np))
369 return &of_busses[i];
370 BUG();
371 return NULL;
372}
373
374static int __init build_one_resource(struct device_node *parent,
375 struct of_bus *bus,
376 struct of_bus *pbus,
377 u32 *addr,
378 int na, int ns, int pna)
379{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700380 const u32 *ranges;
David S. Millercf44bbc2006-06-29 14:34:50 -0700381 unsigned int rlen;
382 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700383
384 ranges = of_get_property(parent, "ranges", &rlen);
385 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700386 u32 result[OF_MAX_ADDR_CELLS];
387 int i;
388
389 memset(result, 0, pna * 4);
390 for (i = 0; i < na; i++)
391 result[pna - 1 - i] =
392 addr[na - 1 - i];
393
394 memcpy(addr, result, pna * 4);
395 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700396 }
397
398 /* Now walk through the ranges */
399 rlen /= 4;
400 rone = na + pna + ns;
401 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700402 if (!bus->map(addr, ranges, na, ns, pna))
403 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700404 }
David S. Millera83f9822006-07-12 23:19:31 -0700405
David S. Miller49d23cf2007-05-13 22:01:18 -0700406 /* When we miss an I/O space match on PCI, just pass it up
407 * to the next PCI bridge and/or controller.
408 */
409 if (!strcmp(bus->name, "pci") &&
410 (addr[0] & 0x03000000) == 0x01000000)
411 return 0;
412
David S. Millera83f9822006-07-12 23:19:31 -0700413 return 1;
414}
415
416static int __init use_1to1_mapping(struct device_node *pp)
417{
David S. Millera83f9822006-07-12 23:19:31 -0700418 /* If we have a ranges property in the parent, use it. */
419 if (of_find_property(pp, "ranges", NULL) != NULL)
420 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700421
David S. Millera83f9822006-07-12 23:19:31 -0700422 /* If the parent is the dma node of an ISA bus, pass
423 * the translation up to the root.
424 */
425 if (!strcmp(pp->name, "dma"))
426 return 0;
427
David S. Miller8c2786c2007-06-07 21:59:44 -0700428 /* Similarly for all PCI bridges, if we get this far
429 * it lacks a ranges property, and this will include
430 * cases like Simba.
431 */
432 if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
David S. Millera83f9822006-07-12 23:19:31 -0700433 return 0;
434
435 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700436}
437
David S. Millera83f9822006-07-12 23:19:31 -0700438static int of_resource_verbose;
439
David S. Millercf44bbc2006-06-29 14:34:50 -0700440static void __init build_device_resources(struct of_device *op,
441 struct device *parent)
442{
443 struct of_device *p_op;
444 struct of_bus *bus;
445 int na, ns;
446 int index, num_reg;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700447 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700448
449 if (!parent)
450 return;
451
452 p_op = to_of_device(parent);
453 bus = of_match_bus(p_op->node);
454 bus->count_cells(op->node, &na, &ns);
455
456 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
457 if (!preg || num_reg == 0)
458 return;
459
460 /* Convert to num-cells. */
461 num_reg /= 4;
462
David S. Miller46ba6d72006-07-16 22:10:44 -0700463 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700464 num_reg /= na + ns;
465
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700466 /* Prevent overrunning the op->resources[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700467 if (num_reg > PROMREG_MAX) {
468 printk(KERN_WARNING "%s: Too many regs (%d), "
469 "limiting to %d.\n",
470 op->node->full_name, num_reg, PROMREG_MAX);
471 num_reg = PROMREG_MAX;
472 }
473
David S. Millercf44bbc2006-06-29 14:34:50 -0700474 for (index = 0; index < num_reg; index++) {
475 struct resource *r = &op->resource[index];
476 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700477 const u32 *reg = (preg + (index * ((na + ns) * 4)));
David S. Millercf44bbc2006-06-29 14:34:50 -0700478 struct device_node *dp = op->node;
479 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800480 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700481 u64 size, result = OF_BAD_ADDR;
482 unsigned long flags;
483 int dna, dns;
484 int pna, pns;
485
486 size = of_read_addr(reg + na, ns);
David S. Millercf44bbc2006-06-29 14:34:50 -0700487 memcpy(addr, reg, na * 4);
488
David S. Millere3c71a32008-08-28 21:02:58 -0700489 flags = bus->get_flags(addr, 0);
490
David S. Millera83f9822006-07-12 23:19:31 -0700491 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700492 result = of_read_addr(addr, na);
493 goto build_res;
494 }
495
496 dna = na;
497 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800498 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700499
500 while (1) {
501 dp = pp;
502 pp = dp->parent;
503 if (!pp) {
504 result = of_read_addr(addr, dna);
505 break;
506 }
507
508 pbus = of_match_bus(pp);
509 pbus->count_cells(dp, &pna, &pns);
510
David S. Millerb85cdd42007-02-28 23:20:12 -0800511 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700512 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700513 break;
514
David S. Millere3c71a32008-08-28 21:02:58 -0700515 flags = pbus->get_flags(addr, flags);
516
David S. Millercf44bbc2006-06-29 14:34:50 -0700517 dna = pna;
518 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800519 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700520 }
521
522 build_res:
523 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700524
525 if (of_resource_verbose)
526 printk("%s reg[%d] -> %lx\n",
527 op->node->full_name, index,
528 result);
529
David S. Millercf44bbc2006-06-29 14:34:50 -0700530 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700531 if (tlb_type == hypervisor)
532 result &= 0x0fffffffffffffffUL;
533
David S. Millercf44bbc2006-06-29 14:34:50 -0700534 r->start = result;
535 r->end = result + size - 1;
536 r->flags = flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700537 }
538 r->name = op->node->name;
539 }
540}
541
David S. Miller2b1e5972006-06-29 15:07:37 -0700542static struct device_node * __init
543apply_interrupt_map(struct device_node *dp, struct device_node *pp,
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700544 const u32 *imap, int imlen, const u32 *imask,
David S. Miller2b1e5972006-06-29 15:07:37 -0700545 unsigned int *irq_p)
546{
547 struct device_node *cp;
548 unsigned int irq = *irq_p;
549 struct of_bus *bus;
550 phandle handle;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700551 const u32 *reg;
David S. Miller2b1e5972006-06-29 15:07:37 -0700552 int na, num_reg, i;
553
554 bus = of_match_bus(pp);
555 bus->count_cells(dp, &na, NULL);
556
557 reg = of_get_property(dp, "reg", &num_reg);
558 if (!reg || !num_reg)
559 return NULL;
560
561 imlen /= ((na + 3) * 4);
562 handle = 0;
563 for (i = 0; i < imlen; i++) {
564 int j;
565
566 for (j = 0; j < na; j++) {
567 if ((reg[j] & imask[j]) != imap[j])
568 goto next;
569 }
570 if (imap[na] == irq) {
571 handle = imap[na + 1];
572 irq = imap[na + 2];
573 break;
574 }
575
576 next:
577 imap += (na + 3);
578 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700579 if (i == imlen) {
580 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
581 * properties that do not include the on-board device
582 * interrupts. Instead, the device's 'interrupts' property
583 * is already a fully specified INO value.
584 *
585 * Handle this by deciding that, if we didn't get a
586 * match in the parent's 'interrupt-map', and the
587 * parent is an IRQ translater, then use the parent as
588 * our IRQ controller.
589 */
590 if (pp->irq_trans)
591 return pp;
592
David S. Miller2b1e5972006-06-29 15:07:37 -0700593 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700594 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700595
596 *irq_p = irq;
597 cp = of_find_node_by_phandle(handle);
598
599 return cp;
600}
601
602static unsigned int __init pci_irq_swizzle(struct device_node *dp,
603 struct device_node *pp,
604 unsigned int irq)
605{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700606 const struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800607 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700608
609 if (irq < 1 || irq > 4)
610 return irq;
611
612 regs = of_get_property(dp, "reg", NULL);
613 if (!regs)
614 return irq;
615
David S. Millerbb4c18c2007-02-26 14:55:06 -0800616 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700617 devfn = (regs->phys_hi >> 8) & 0xff;
618 slot = (devfn >> 3) & 0x1f;
619
David S. Millerbb4c18c2007-02-26 14:55:06 -0800620 if (pp->irq_trans) {
621 /* Derived from Table 8-3, U2P User's Manual. This branch
622 * is handling a PCI controller that lacks a proper set of
623 * interrupt-map and interrupt-map-mask properties. The
624 * Ultra-E450 is one example.
625 *
626 * The bit layout is BSSLL, where:
627 * B: 0 on bus A, 1 on bus B
628 * D: 2-bit slot number, derived from PCI device number as
629 * (dev - 1) for bus A, or (dev - 2) for bus B
630 * L: 2-bit line number
David S. Millerbb4c18c2007-02-26 14:55:06 -0800631 */
632 if (bus & 0x80) {
633 /* PBM-A */
634 bus = 0x00;
635 slot = (slot - 1) << 2;
636 } else {
637 /* PBM-B */
638 bus = 0x10;
639 slot = (slot - 2) << 2;
640 }
641 irq -= 1;
642
643 ret = (bus | slot | irq);
644 } else {
645 /* Going through a PCI-PCI bridge that lacks a set of
646 * interrupt-map and interrupt-map-mask properties.
647 */
648 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
649 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700650
651 return ret;
652}
653
David S. Millera83f9822006-07-12 23:19:31 -0700654static int of_irq_verbose;
655
David S. Miller2b1e5972006-06-29 15:07:37 -0700656static unsigned int __init build_one_device_irq(struct of_device *op,
657 struct device *parent,
658 unsigned int irq)
659{
660 struct device_node *dp = op->node;
661 struct device_node *pp, *ip;
662 unsigned int orig_irq = irq;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700663 int nid;
David S. Miller2b1e5972006-06-29 15:07:37 -0700664
665 if (irq == 0xffffffff)
666 return irq;
667
668 if (dp->irq_trans) {
669 irq = dp->irq_trans->irq_build(dp, irq,
670 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700671
672 if (of_irq_verbose)
673 printk("%s: direct translate %x --> %x\n",
674 dp->full_name, orig_irq, irq);
675
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700676 goto out;
David S. Miller2b1e5972006-06-29 15:07:37 -0700677 }
678
679 /* Something more complicated. Walk up to the root, applying
680 * interrupt-map or bus specific translations, until we hit
681 * an IRQ translator.
682 *
683 * If we hit a bus type or situation we cannot handle, we
684 * stop and assume that the original IRQ number was in a
685 * format which has special meaning to it's immediate parent.
686 */
687 pp = dp->parent;
688 ip = NULL;
689 while (pp) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700690 const void *imap, *imsk;
David S. Miller2b1e5972006-06-29 15:07:37 -0700691 int imlen;
692
693 imap = of_get_property(pp, "interrupt-map", &imlen);
694 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
695 if (imap && imsk) {
696 struct device_node *iret;
697 int this_orig_irq = irq;
698
699 iret = apply_interrupt_map(dp, pp,
700 imap, imlen, imsk,
701 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700702
703 if (of_irq_verbose)
704 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
705 op->node->full_name,
706 pp->full_name, this_orig_irq,
707 (iret ? iret->full_name : "NULL"), irq);
708
David S. Miller2b1e5972006-06-29 15:07:37 -0700709 if (!iret)
710 break;
711
712 if (iret->irq_trans) {
713 ip = iret;
714 break;
715 }
716 } else {
717 if (!strcmp(pp->type, "pci") ||
718 !strcmp(pp->type, "pciex")) {
719 unsigned int this_orig_irq = irq;
720
721 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700722 if (of_irq_verbose)
723 printk("%s: PCI swizzle [%s] "
724 "%x --> %x\n",
725 op->node->full_name,
726 pp->full_name, this_orig_irq,
727 irq);
728
David S. Miller2b1e5972006-06-29 15:07:37 -0700729 }
730
731 if (pp->irq_trans) {
732 ip = pp;
733 break;
734 }
735 }
736 dp = pp;
737 pp = pp->parent;
738 }
739 if (!ip)
740 return orig_irq;
741
742 irq = ip->irq_trans->irq_build(op->node, irq,
743 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700744 if (of_irq_verbose)
745 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
746 op->node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700747
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700748out:
749 nid = of_node_to_nid(dp);
750 if (nid != -1) {
751 cpumask_t numa_mask = node_to_cpumask(nid);
752
753 irq_set_affinity(irq, numa_mask);
754 }
755
David S. Miller2b1e5972006-06-29 15:07:37 -0700756 return irq;
757}
758
David S. Millercf44bbc2006-06-29 14:34:50 -0700759static struct of_device * __init scan_one_device(struct device_node *dp,
760 struct device *parent)
761{
762 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700763 const unsigned int *irq;
David S. Miller3d6e4702007-07-18 22:03:25 -0700764 struct dev_archdata *sd;
David S. Miller2b1e5972006-06-29 15:07:37 -0700765 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700766
767 if (!op)
768 return NULL;
769
David S. Miller3d6e4702007-07-18 22:03:25 -0700770 sd = &op->dev.archdata;
771 sd->prom_node = dp;
772 sd->op = op;
773
David S. Millercf44bbc2006-06-29 14:34:50 -0700774 op->node = dp;
775
776 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
777 (25*1000*1000));
778 op->portid = of_getintprop_default(dp, "upa-portid", -1);
779 if (op->portid == -1)
780 op->portid = of_getintprop_default(dp, "portid", -1);
781
782 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700783 if (irq) {
784 memcpy(op->irqs, irq, len);
785 op->num_irqs = len / 4;
786 } else {
787 op->num_irqs = 0;
788 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700789
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700790 /* Prevent overrunning the op->irqs[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700791 if (op->num_irqs > PROMINTR_MAX) {
792 printk(KERN_WARNING "%s: Too many irqs (%d), "
793 "limiting to %d.\n",
794 dp->full_name, op->num_irqs, PROMINTR_MAX);
795 op->num_irqs = PROMINTR_MAX;
796 }
797
David S. Millercf44bbc2006-06-29 14:34:50 -0700798 build_device_resources(op, parent);
David S. Miller2b1e5972006-06-29 15:07:37 -0700799 for (i = 0; i < op->num_irqs; i++)
800 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700801
802 op->dev.parent = parent;
Stephen Rothwell37b77542007-04-30 17:43:56 +1000803 op->dev.bus = &of_platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700804 if (!parent)
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200805 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700806 else
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200807 dev_set_name(&op->dev, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700808
809 if (of_device_register(op)) {
810 printk("%s: Could not register of device.\n",
811 dp->full_name);
812 kfree(op);
813 op = NULL;
814 }
815
816 return op;
817}
818
819static void __init scan_tree(struct device_node *dp, struct device *parent)
820{
821 while (dp) {
822 struct of_device *op = scan_one_device(dp, parent);
823
824 if (op)
825 scan_tree(dp->child, &op->dev);
826
827 dp = dp->sibling;
828 }
829}
830
831static void __init scan_of_devices(void)
832{
833 struct device_node *root = of_find_node_by_path("/");
834 struct of_device *parent;
835
836 parent = scan_one_device(root, NULL);
837 if (!parent)
838 return;
839
840 scan_tree(root->child, &parent->dev);
841}
842
David S. Millera2bd4fd2006-06-23 01:44:10 -0700843static int __init of_bus_driver_init(void)
844{
David S. Millercf44bbc2006-06-29 14:34:50 -0700845 int err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700846
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000847 err = of_bus_type_init(&of_platform_bus_type, "of");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700848#ifdef CONFIG_PCI
849 if (!err)
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000850 err = of_bus_type_init(&ebus_bus_type, "ebus");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700851#endif
852#ifdef CONFIG_SBUS
853 if (!err)
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000854 err = of_bus_type_init(&sbus_bus_type, "sbus");
David S. Millera2bd4fd2006-06-23 01:44:10 -0700855#endif
David S. Millercf44bbc2006-06-29 14:34:50 -0700856
857 if (!err)
858 scan_of_devices();
859
860 return err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700861}
862
863postcore_initcall(of_bus_driver_init);
864
David S. Millera83f9822006-07-12 23:19:31 -0700865static int __init of_debug(char *str)
866{
867 int val = 0;
868
869 get_option(&str, &val);
870 if (val & 1)
871 of_resource_verbose = 1;
872 if (val & 2)
873 of_irq_verbose = 1;
874 return 1;
875}
876
877__setup("of_debug=", of_debug);