1 #undef DEBUG
2
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/pci_regs.h>
6 #include <linux/module.h>
7 #include <linux/ioport.h>
8 #include <linux/etherdevice.h>
9 #include <asm/prom.h>
10 #include <asm/pci-bridge.h>
11
12 #ifdef DEBUG
13 #define DBG(fmt...) do { printk(fmt); } while(0)
14 #else
15 #define DBG(fmt...) do { } while(0)
16 #endif
17
18 #ifdef CONFIG_PPC64
19 #define PRu64 "%lx"
20 #else
21 #define PRu64 "%llx"
22 #endif
23
24 /* Max address size we deal with */
25 #define OF_MAX_ADDR_CELLS 4
26 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
27 (ns) > 0)
28
29 static struct of_bus *of_match_bus(struct device_node *np);
30 static int __of_address_to_resource(struct device_node *dev,
31 const u32 *addrp, u64 size, unsigned int flags,
32 struct resource *r);
33
34
35 /* Debug utility */
36 #ifdef DEBUG
of_dump_addr(const char * s,const u32 * addr,int na)37 static void of_dump_addr(const char *s, const u32 *addr, int na)
38 {
39 printk("%s", s);
40 while(na--)
41 printk(" %08x", *(addr++));
42 printk("\n");
43 }
44 #else
of_dump_addr(const char * s,const u32 * addr,int na)45 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
46 #endif
47
48
49 /* Callbacks for bus specific translators */
50 struct of_bus {
51 const char *name;
52 const char *addresses;
53 int (*match)(struct device_node *parent);
54 void (*count_cells)(struct device_node *child,
55 int *addrc, int *sizec);
56 u64 (*map)(u32 *addr, const u32 *range,
57 int na, int ns, int pna);
58 int (*translate)(u32 *addr, u64 offset, int na);
59 unsigned int (*get_flags)(const u32 *addr);
60 };
61
62
63 /*
64 * Default translator (generic bus)
65 */
66
of_bus_default_count_cells(struct device_node * dev,int * addrc,int * sizec)67 static void of_bus_default_count_cells(struct device_node *dev,
68 int *addrc, int *sizec)
69 {
70 if (addrc)
71 *addrc = of_n_addr_cells(dev);
72 if (sizec)
73 *sizec = of_n_size_cells(dev);
74 }
75
of_bus_default_map(u32 * addr,const u32 * range,int na,int ns,int pna)76 static u64 of_bus_default_map(u32 *addr, const u32 *range,
77 int na, int ns, int pna)
78 {
79 u64 cp, s, da;
80
81 cp = of_read_number(range, na);
82 s = of_read_number(range + na + pna, ns);
83 da = of_read_number(addr, na);
84
85 DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
86 cp, s, da);
87
88 if (da < cp || da >= (cp + s))
89 return OF_BAD_ADDR;
90 return da - cp;
91 }
92
of_bus_default_translate(u32 * addr,u64 offset,int na)93 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
94 {
95 u64 a = of_read_number(addr, na);
96 memset(addr, 0, na * 4);
97 a += offset;
98 if (na > 1)
99 addr[na - 2] = a >> 32;
100 addr[na - 1] = a & 0xffffffffu;
101
102 return 0;
103 }
104
of_bus_default_get_flags(const u32 * addr)105 static unsigned int of_bus_default_get_flags(const u32 *addr)
106 {
107 return IORESOURCE_MEM;
108 }
109
110
111 #ifdef CONFIG_PCI
112 /*
113 * PCI bus specific translator
114 */
115
of_bus_pci_match(struct device_node * np)116 static int of_bus_pci_match(struct device_node *np)
117 {
118 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
119 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
120 }
121
of_bus_pci_count_cells(struct device_node * np,int * addrc,int * sizec)122 static void of_bus_pci_count_cells(struct device_node *np,
123 int *addrc, int *sizec)
124 {
125 if (addrc)
126 *addrc = 3;
127 if (sizec)
128 *sizec = 2;
129 }
130
of_bus_pci_get_flags(const u32 * addr)131 static unsigned int of_bus_pci_get_flags(const u32 *addr)
132 {
133 unsigned int flags = 0;
134 u32 w = addr[0];
135
136 switch((w >> 24) & 0x03) {
137 case 0x01:
138 flags |= IORESOURCE_IO;
139 break;
140 case 0x02: /* 32 bits */
141 case 0x03: /* 64 bits */
142 flags |= IORESOURCE_MEM;
143 break;
144 }
145 if (w & 0x40000000)
146 flags |= IORESOURCE_PREFETCH;
147 return flags;
148 }
149
of_bus_pci_map(u32 * addr,const u32 * range,int na,int ns,int pna)150 static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
151 {
152 u64 cp, s, da;
153 unsigned int af, rf;
154
155 af = of_bus_pci_get_flags(addr);
156 rf = of_bus_pci_get_flags(range);
157
158 /* Check address type match */
159 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
160 return OF_BAD_ADDR;
161
162 /* Read address values, skipping high cell */
163 cp = of_read_number(range + 1, na - 1);
164 s = of_read_number(range + na + pna, ns);
165 da = of_read_number(addr + 1, na - 1);
166
167 DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
168
169 if (da < cp || da >= (cp + s))
170 return OF_BAD_ADDR;
171 return da - cp;
172 }
173
of_bus_pci_translate(u32 * addr,u64 offset,int na)174 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
175 {
176 return of_bus_default_translate(addr + 1, offset, na - 1);
177 }
178
of_get_pci_address(struct device_node * dev,int bar_no,u64 * size,unsigned int * flags)179 const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
180 unsigned int *flags)
181 {
182 const u32 *prop;
183 unsigned int psize;
184 struct device_node *parent;
185 struct of_bus *bus;
186 int onesize, i, na, ns;
187
188 /* Get parent & match bus type */
189 parent = of_get_parent(dev);
190 if (parent == NULL)
191 return NULL;
192 bus = of_match_bus(parent);
193 if (strcmp(bus->name, "pci")) {
194 of_node_put(parent);
195 return NULL;
196 }
197 bus->count_cells(dev, &na, &ns);
198 of_node_put(parent);
199 if (!OF_CHECK_COUNTS(na, ns))
200 return NULL;
201
202 /* Get "reg" or "assigned-addresses" property */
203 prop = of_get_property(dev, bus->addresses, &psize);
204 if (prop == NULL)
205 return NULL;
206 psize /= 4;
207
208 onesize = na + ns;
209 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
210 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
211 if (size)
212 *size = of_read_number(prop + na, ns);
213 if (flags)
214 *flags = bus->get_flags(prop);
215 return prop;
216 }
217 return NULL;
218 }
219 EXPORT_SYMBOL(of_get_pci_address);
220
of_pci_address_to_resource(struct device_node * dev,int bar,struct resource * r)221 int of_pci_address_to_resource(struct device_node *dev, int bar,
222 struct resource *r)
223 {
224 const u32 *addrp;
225 u64 size;
226 unsigned int flags;
227
228 addrp = of_get_pci_address(dev, bar, &size, &flags);
229 if (addrp == NULL)
230 return -EINVAL;
231 return __of_address_to_resource(dev, addrp, size, flags, r);
232 }
233 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
234
of_irq_map_pci(struct pci_dev * pdev,struct of_irq * out_irq)235 int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
236 {
237 struct device_node *dn, *ppnode;
238 struct pci_dev *ppdev;
239 u32 lspec;
240 u32 laddr[3];
241 u8 pin;
242 int rc;
243
244 /* Check if we have a device node, if yes, fallback to standard OF
245 * parsing
246 */
247 dn = pci_device_to_OF_node(pdev);
248 if (dn) {
249 rc = of_irq_map_one(dn, 0, out_irq);
250 if (!rc)
251 return rc;
252 }
253
254 /* Ok, we don't, time to have fun. Let's start by building up an
255 * interrupt spec. we assume #interrupt-cells is 1, which is standard
256 * for PCI. If you do different, then don't use that routine.
257 */
258 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
259 if (rc != 0)
260 return rc;
261 /* No pin, exit */
262 if (pin == 0)
263 return -ENODEV;
264
265 /* Now we walk up the PCI tree */
266 lspec = pin;
267 for (;;) {
268 /* Get the pci_dev of our parent */
269 ppdev = pdev->bus->self;
270
271 /* Ouch, it's a host bridge... */
272 if (ppdev == NULL) {
273 #ifdef CONFIG_PPC64
274 ppnode = pci_bus_to_OF_node(pdev->bus);
275 #else
276 struct pci_controller *host;
277 host = pci_bus_to_host(pdev->bus);
278 ppnode = host ? host->dn : NULL;
279 #endif
280 /* No node for host bridge ? give up */
281 if (ppnode == NULL)
282 return -EINVAL;
283 } else
284 /* We found a P2P bridge, check if it has a node */
285 ppnode = pci_device_to_OF_node(ppdev);
286
287 /* Ok, we have found a parent with a device-node, hand over to
288 * the OF parsing code.
289 * We build a unit address from the linux device to be used for
290 * resolution. Note that we use the linux bus number which may
291 * not match your firmware bus numbering.
292 * Fortunately, in most cases, interrupt-map-mask doesn't include
293 * the bus number as part of the matching.
294 * You should still be careful about that though if you intend
295 * to rely on this function (you ship a firmware that doesn't
296 * create device nodes for all PCI devices).
297 */
298 if (ppnode)
299 break;
300
301 /* We can only get here if we hit a P2P bridge with no node,
302 * let's do standard swizzling and try again
303 */
304 lspec = pci_swizzle_interrupt_pin(pdev, lspec);
305 pdev = ppdev;
306 }
307
308 laddr[0] = (pdev->bus->number << 16)
309 | (pdev->devfn << 8);
310 laddr[1] = laddr[2] = 0;
311 return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq);
312 }
313 EXPORT_SYMBOL_GPL(of_irq_map_pci);
314 #endif /* CONFIG_PCI */
315
316 /*
317 * ISA bus specific translator
318 */
319
of_bus_isa_match(struct device_node * np)320 static int of_bus_isa_match(struct device_node *np)
321 {
322 return !strcmp(np->name, "isa");
323 }
324
of_bus_isa_count_cells(struct device_node * child,int * addrc,int * sizec)325 static void of_bus_isa_count_cells(struct device_node *child,
326 int *addrc, int *sizec)
327 {
328 if (addrc)
329 *addrc = 2;
330 if (sizec)
331 *sizec = 1;
332 }
333
of_bus_isa_map(u32 * addr,const u32 * range,int na,int ns,int pna)334 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
335 {
336 u64 cp, s, da;
337
338 /* Check address type match */
339 if ((addr[0] ^ range[0]) & 0x00000001)
340 return OF_BAD_ADDR;
341
342 /* Read address values, skipping high cell */
343 cp = of_read_number(range + 1, na - 1);
344 s = of_read_number(range + na + pna, ns);
345 da = of_read_number(addr + 1, na - 1);
346
347 DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
348
349 if (da < cp || da >= (cp + s))
350 return OF_BAD_ADDR;
351 return da - cp;
352 }
353
of_bus_isa_translate(u32 * addr,u64 offset,int na)354 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
355 {
356 return of_bus_default_translate(addr + 1, offset, na - 1);
357 }
358
of_bus_isa_get_flags(const u32 * addr)359 static unsigned int of_bus_isa_get_flags(const u32 *addr)
360 {
361 unsigned int flags = 0;
362 u32 w = addr[0];
363
364 if (w & 1)
365 flags |= IORESOURCE_IO;
366 else
367 flags |= IORESOURCE_MEM;
368 return flags;
369 }
370
371
372 /*
373 * Array of bus specific translators
374 */
375
376 static struct of_bus of_busses[] = {
377 #ifdef CONFIG_PCI
378 /* PCI */
379 {
380 .name = "pci",
381 .addresses = "assigned-addresses",
382 .match = of_bus_pci_match,
383 .count_cells = of_bus_pci_count_cells,
384 .map = of_bus_pci_map,
385 .translate = of_bus_pci_translate,
386 .get_flags = of_bus_pci_get_flags,
387 },
388 #endif /* CONFIG_PCI */
389 /* ISA */
390 {
391 .name = "isa",
392 .addresses = "reg",
393 .match = of_bus_isa_match,
394 .count_cells = of_bus_isa_count_cells,
395 .map = of_bus_isa_map,
396 .translate = of_bus_isa_translate,
397 .get_flags = of_bus_isa_get_flags,
398 },
399 /* Default */
400 {
401 .name = "default",
402 .addresses = "reg",
403 .match = NULL,
404 .count_cells = of_bus_default_count_cells,
405 .map = of_bus_default_map,
406 .translate = of_bus_default_translate,
407 .get_flags = of_bus_default_get_flags,
408 },
409 };
410
of_match_bus(struct device_node * np)411 static struct of_bus *of_match_bus(struct device_node *np)
412 {
413 int i;
414
415 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
416 if (!of_busses[i].match || of_busses[i].match(np))
417 return &of_busses[i];
418 BUG();
419 return NULL;
420 }
421
of_translate_one(struct device_node * parent,struct of_bus * bus,struct of_bus * pbus,u32 * addr,int na,int ns,int pna,const char * rprop)422 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
423 struct of_bus *pbus, u32 *addr,
424 int na, int ns, int pna, const char *rprop)
425 {
426 const u32 *ranges;
427 unsigned int rlen;
428 int rone;
429 u64 offset = OF_BAD_ADDR;
430
431 /* Normally, an absence of a "ranges" property means we are
432 * crossing a non-translatable boundary, and thus the addresses
433 * below the current not cannot be converted to CPU physical ones.
434 * Unfortunately, while this is very clear in the spec, it's not
435 * what Apple understood, and they do have things like /uni-n or
436 * /ht nodes with no "ranges" property and a lot of perfectly
437 * useable mapped devices below them. Thus we treat the absence of
438 * "ranges" as equivalent to an empty "ranges" property which means
439 * a 1:1 translation at that level. It's up to the caller not to try
440 * to translate addresses that aren't supposed to be translated in
441 * the first place. --BenH.
442 */
443 ranges = of_get_property(parent, rprop, &rlen);
444 if (ranges == NULL || rlen == 0) {
445 offset = of_read_number(addr, na);
446 memset(addr, 0, pna * 4);
447 DBG("OF: no ranges, 1:1 translation\n");
448 goto finish;
449 }
450
451 DBG("OF: walking ranges...\n");
452
453 /* Now walk through the ranges */
454 rlen /= 4;
455 rone = na + pna + ns;
456 for (; rlen >= rone; rlen -= rone, ranges += rone) {
457 offset = bus->map(addr, ranges, na, ns, pna);
458 if (offset != OF_BAD_ADDR)
459 break;
460 }
461 if (offset == OF_BAD_ADDR) {
462 DBG("OF: not found !\n");
463 return 1;
464 }
465 memcpy(addr, ranges + na, 4 * pna);
466
467 finish:
468 of_dump_addr("OF: parent translation for:", addr, pna);
469 DBG("OF: with offset: "PRu64"\n", offset);
470
471 /* Translate it into parent bus space */
472 return pbus->translate(addr, offset, pna);
473 }
474
475
476 /*
477 * Translate an address from the device-tree into a CPU physical address,
478 * this walks up the tree and applies the various bus mappings on the
479 * way.
480 *
481 * Note: We consider that crossing any level with #size-cells == 0 to mean
482 * that translation is impossible (that is we are not dealing with a value
483 * that can be mapped to a cpu physical address). This is not really specified
484 * that way, but this is traditionally the way IBM at least do things
485 */
__of_translate_address(struct device_node * dev,const u32 * in_addr,const char * rprop)486 u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
487 const char *rprop)
488 {
489 struct device_node *parent = NULL;
490 struct of_bus *bus, *pbus;
491 u32 addr[OF_MAX_ADDR_CELLS];
492 int na, ns, pna, pns;
493 u64 result = OF_BAD_ADDR;
494
495 DBG("OF: ** translation for device %s **\n", dev->full_name);
496
497 /* Increase refcount at current level */
498 of_node_get(dev);
499
500 /* Get parent & match bus type */
501 parent = of_get_parent(dev);
502 if (parent == NULL)
503 goto bail;
504 bus = of_match_bus(parent);
505
506 /* Cound address cells & copy address locally */
507 bus->count_cells(dev, &na, &ns);
508 if (!OF_CHECK_COUNTS(na, ns)) {
509 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
510 dev->full_name);
511 goto bail;
512 }
513 memcpy(addr, in_addr, na * 4);
514
515 DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
516 bus->name, na, ns, parent->full_name);
517 of_dump_addr("OF: translating address:", addr, na);
518
519 /* Translate */
520 for (;;) {
521 /* Switch to parent bus */
522 of_node_put(dev);
523 dev = parent;
524 parent = of_get_parent(dev);
525
526 /* If root, we have finished */
527 if (parent == NULL) {
528 DBG("OF: reached root node\n");
529 result = of_read_number(addr, na);
530 break;
531 }
532
533 /* Get new parent bus and counts */
534 pbus = of_match_bus(parent);
535 pbus->count_cells(dev, &pna, &pns);
536 if (!OF_CHECK_COUNTS(pna, pns)) {
537 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
538 dev->full_name);
539 break;
540 }
541
542 DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
543 pbus->name, pna, pns, parent->full_name);
544
545 /* Apply bus translation */
546 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
547 break;
548
549 /* Complete the move up one level */
550 na = pna;
551 ns = pns;
552 bus = pbus;
553
554 of_dump_addr("OF: one level translation:", addr, na);
555 }
556 bail:
557 of_node_put(parent);
558 of_node_put(dev);
559
560 return result;
561 }
562
of_translate_address(struct device_node * dev,const u32 * in_addr)563 u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
564 {
565 return __of_translate_address(dev, in_addr, "ranges");
566 }
567 EXPORT_SYMBOL(of_translate_address);
568
of_translate_dma_address(struct device_node * dev,const u32 * in_addr)569 u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
570 {
571 return __of_translate_address(dev, in_addr, "dma-ranges");
572 }
573 EXPORT_SYMBOL(of_translate_dma_address);
574
of_get_address(struct device_node * dev,int index,u64 * size,unsigned int * flags)575 const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
576 unsigned int *flags)
577 {
578 const u32 *prop;
579 unsigned int psize;
580 struct device_node *parent;
581 struct of_bus *bus;
582 int onesize, i, na, ns;
583
584 /* Get parent & match bus type */
585 parent = of_get_parent(dev);
586 if (parent == NULL)
587 return NULL;
588 bus = of_match_bus(parent);
589 bus->count_cells(dev, &na, &ns);
590 of_node_put(parent);
591 if (!OF_CHECK_COUNTS(na, ns))
592 return NULL;
593
594 /* Get "reg" or "assigned-addresses" property */
595 prop = of_get_property(dev, bus->addresses, &psize);
596 if (prop == NULL)
597 return NULL;
598 psize /= 4;
599
600 onesize = na + ns;
601 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
602 if (i == index) {
603 if (size)
604 *size = of_read_number(prop + na, ns);
605 if (flags)
606 *flags = bus->get_flags(prop);
607 return prop;
608 }
609 return NULL;
610 }
611 EXPORT_SYMBOL(of_get_address);
612
__of_address_to_resource(struct device_node * dev,const u32 * addrp,u64 size,unsigned int flags,struct resource * r)613 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
614 u64 size, unsigned int flags,
615 struct resource *r)
616 {
617 u64 taddr;
618
619 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
620 return -EINVAL;
621 taddr = of_translate_address(dev, addrp);
622 if (taddr == OF_BAD_ADDR)
623 return -EINVAL;
624 memset(r, 0, sizeof(struct resource));
625 if (flags & IORESOURCE_IO) {
626 unsigned long port;
627 port = pci_address_to_pio(taddr);
628 if (port == (unsigned long)-1)
629 return -EINVAL;
630 r->start = port;
631 r->end = port + size - 1;
632 } else {
633 r->start = taddr;
634 r->end = taddr + size - 1;
635 }
636 r->flags = flags;
637 r->name = dev->name;
638 return 0;
639 }
640
of_address_to_resource(struct device_node * dev,int index,struct resource * r)641 int of_address_to_resource(struct device_node *dev, int index,
642 struct resource *r)
643 {
644 const u32 *addrp;
645 u64 size;
646 unsigned int flags;
647
648 addrp = of_get_address(dev, index, &size, &flags);
649 if (addrp == NULL)
650 return -EINVAL;
651 return __of_address_to_resource(dev, addrp, size, flags, r);
652 }
653 EXPORT_SYMBOL_GPL(of_address_to_resource);
654
of_parse_dma_window(struct device_node * dn,const void * dma_window_prop,unsigned long * busno,unsigned long * phys,unsigned long * size)655 void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
656 unsigned long *busno, unsigned long *phys, unsigned long *size)
657 {
658 const u32 *dma_window;
659 u32 cells;
660 const unsigned char *prop;
661
662 dma_window = dma_window_prop;
663
664 /* busno is always one cell */
665 *busno = *(dma_window++);
666
667 prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
668 if (!prop)
669 prop = of_get_property(dn, "#address-cells", NULL);
670
671 cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
672 *phys = of_read_number(dma_window, cells);
673
674 dma_window += cells;
675
676 prop = of_get_property(dn, "ibm,#dma-size-cells", NULL);
677 cells = prop ? *(u32 *)prop : of_n_size_cells(dn);
678 *size = of_read_number(dma_window, cells);
679 }
680
681 /*
682 * Interrupt remapper
683 */
684
685 static unsigned int of_irq_workarounds;
686 static struct device_node *of_irq_dflt_pic;
687
of_irq_find_parent(struct device_node * child)688 static struct device_node *of_irq_find_parent(struct device_node *child)
689 {
690 struct device_node *p;
691 const phandle *parp;
692
693 if (!of_node_get(child))
694 return NULL;
695
696 do {
697 parp = of_get_property(child, "interrupt-parent", NULL);
698 if (parp == NULL)
699 p = of_get_parent(child);
700 else {
701 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
702 p = of_node_get(of_irq_dflt_pic);
703 else
704 p = of_find_node_by_phandle(*parp);
705 }
706 of_node_put(child);
707 child = p;
708 } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
709
710 return p;
711 }
712
713 /* This doesn't need to be called if you don't have any special workaround
714 * flags to pass
715 */
of_irq_map_init(unsigned int flags)716 void of_irq_map_init(unsigned int flags)
717 {
718 of_irq_workarounds = flags;
719
720 /* OldWorld, don't bother looking at other things */
721 if (flags & OF_IMAP_OLDWORLD_MAC)
722 return;
723
724 /* If we don't have phandles, let's try to locate a default interrupt
725 * controller (happens when booting with BootX). We do a first match
726 * here, hopefully, that only ever happens on machines with one
727 * controller.
728 */
729 if (flags & OF_IMAP_NO_PHANDLE) {
730 struct device_node *np;
731
732 for_each_node_with_property(np, "interrupt-controller") {
733 /* Skip /chosen/interrupt-controller */
734 if (strcmp(np->name, "chosen") == 0)
735 continue;
736 /* It seems like at least one person on this planet wants
737 * to use BootX on a machine with an AppleKiwi controller
738 * which happens to pretend to be an interrupt
739 * controller too.
740 */
741 if (strcmp(np->name, "AppleKiwi") == 0)
742 continue;
743 /* I think we found one ! */
744 of_irq_dflt_pic = np;
745 break;
746 }
747 }
748
749 }
750
of_irq_map_raw(struct device_node * parent,const u32 * intspec,u32 ointsize,const u32 * addr,struct of_irq * out_irq)751 int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize,
752 const u32 *addr, struct of_irq *out_irq)
753 {
754 struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
755 const u32 *tmp, *imap, *imask;
756 u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
757 int imaplen, match, i;
758
759 DBG("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
760 parent->full_name, intspec[0], intspec[1], ointsize);
761
762 ipar = of_node_get(parent);
763
764 /* First get the #interrupt-cells property of the current cursor
765 * that tells us how to interpret the passed-in intspec. If there
766 * is none, we are nice and just walk up the tree
767 */
768 do {
769 tmp = of_get_property(ipar, "#interrupt-cells", NULL);
770 if (tmp != NULL) {
771 intsize = *tmp;
772 break;
773 }
774 tnode = ipar;
775 ipar = of_irq_find_parent(ipar);
776 of_node_put(tnode);
777 } while (ipar);
778 if (ipar == NULL) {
779 DBG(" -> no parent found !\n");
780 goto fail;
781 }
782
783 DBG("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
784
785 if (ointsize != intsize)
786 return -EINVAL;
787
788 /* Look for this #address-cells. We have to implement the old linux
789 * trick of looking for the parent here as some device-trees rely on it
790 */
791 old = of_node_get(ipar);
792 do {
793 tmp = of_get_property(old, "#address-cells", NULL);
794 tnode = of_get_parent(old);
795 of_node_put(old);
796 old = tnode;
797 } while(old && tmp == NULL);
798 of_node_put(old);
799 old = NULL;
800 addrsize = (tmp == NULL) ? 2 : *tmp;
801
802 DBG(" -> addrsize=%d\n", addrsize);
803
804 /* Now start the actual "proper" walk of the interrupt tree */
805 while (ipar != NULL) {
806 /* Now check if cursor is an interrupt-controller and if it is
807 * then we are done
808 */
809 if (of_get_property(ipar, "interrupt-controller", NULL) !=
810 NULL) {
811 DBG(" -> got it !\n");
812 memcpy(out_irq->specifier, intspec,
813 intsize * sizeof(u32));
814 out_irq->size = intsize;
815 out_irq->controller = ipar;
816 of_node_put(old);
817 return 0;
818 }
819
820 /* Now look for an interrupt-map */
821 imap = of_get_property(ipar, "interrupt-map", &imaplen);
822 /* No interrupt map, check for an interrupt parent */
823 if (imap == NULL) {
824 DBG(" -> no map, getting parent\n");
825 newpar = of_irq_find_parent(ipar);
826 goto skiplevel;
827 }
828 imaplen /= sizeof(u32);
829
830 /* Look for a mask */
831 imask = of_get_property(ipar, "interrupt-map-mask", NULL);
832
833 /* If we were passed no "reg" property and we attempt to parse
834 * an interrupt-map, then #address-cells must be 0.
835 * Fail if it's not.
836 */
837 if (addr == NULL && addrsize != 0) {
838 DBG(" -> no reg passed in when needed !\n");
839 goto fail;
840 }
841
842 /* Parse interrupt-map */
843 match = 0;
844 while (imaplen > (addrsize + intsize + 1) && !match) {
845 /* Compare specifiers */
846 match = 1;
847 for (i = 0; i < addrsize && match; ++i) {
848 u32 mask = imask ? imask[i] : 0xffffffffu;
849 match = ((addr[i] ^ imap[i]) & mask) == 0;
850 }
851 for (; i < (addrsize + intsize) && match; ++i) {
852 u32 mask = imask ? imask[i] : 0xffffffffu;
853 match =
854 ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
855 }
856 imap += addrsize + intsize;
857 imaplen -= addrsize + intsize;
858
859 DBG(" -> match=%d (imaplen=%d)\n", match, imaplen);
860
861 /* Get the interrupt parent */
862 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
863 newpar = of_node_get(of_irq_dflt_pic);
864 else
865 newpar = of_find_node_by_phandle((phandle)*imap);
866 imap++;
867 --imaplen;
868
869 /* Check if not found */
870 if (newpar == NULL) {
871 DBG(" -> imap parent not found !\n");
872 goto fail;
873 }
874
875 /* Get #interrupt-cells and #address-cells of new
876 * parent
877 */
878 tmp = of_get_property(newpar, "#interrupt-cells", NULL);
879 if (tmp == NULL) {
880 DBG(" -> parent lacks #interrupt-cells !\n");
881 goto fail;
882 }
883 newintsize = *tmp;
884 tmp = of_get_property(newpar, "#address-cells", NULL);
885 newaddrsize = (tmp == NULL) ? 0 : *tmp;
886
887 DBG(" -> newintsize=%d, newaddrsize=%d\n",
888 newintsize, newaddrsize);
889
890 /* Check for malformed properties */
891 if (imaplen < (newaddrsize + newintsize))
892 goto fail;
893
894 imap += newaddrsize + newintsize;
895 imaplen -= newaddrsize + newintsize;
896
897 DBG(" -> imaplen=%d\n", imaplen);
898 }
899 if (!match)
900 goto fail;
901
902 of_node_put(old);
903 old = of_node_get(newpar);
904 addrsize = newaddrsize;
905 intsize = newintsize;
906 intspec = imap - intsize;
907 addr = intspec - addrsize;
908
909 skiplevel:
910 /* Iterate again with new parent */
911 DBG(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
912 of_node_put(ipar);
913 ipar = newpar;
914 newpar = NULL;
915 }
916 fail:
917 of_node_put(ipar);
918 of_node_put(old);
919 of_node_put(newpar);
920
921 return -EINVAL;
922 }
923 EXPORT_SYMBOL_GPL(of_irq_map_raw);
924
925 #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
of_irq_map_oldworld(struct device_node * device,int index,struct of_irq * out_irq)926 static int of_irq_map_oldworld(struct device_node *device, int index,
927 struct of_irq *out_irq)
928 {
929 const u32 *ints = NULL;
930 int intlen;
931
932 /*
933 * Old machines just have a list of interrupt numbers
934 * and no interrupt-controller nodes. We also have dodgy
935 * cases where the APPL,interrupts property is completely
936 * missing behind pci-pci bridges and we have to get it
937 * from the parent (the bridge itself, as apple just wired
938 * everything together on these)
939 */
940 while (device) {
941 ints = of_get_property(device, "AAPL,interrupts", &intlen);
942 if (ints != NULL)
943 break;
944 device = device->parent;
945 if (device && strcmp(device->type, "pci") != 0)
946 break;
947 }
948 if (ints == NULL)
949 return -EINVAL;
950 intlen /= sizeof(u32);
951
952 if (index >= intlen)
953 return -EINVAL;
954
955 out_irq->controller = NULL;
956 out_irq->specifier[0] = ints[index];
957 out_irq->size = 1;
958
959 return 0;
960 }
961 #else /* defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) */
of_irq_map_oldworld(struct device_node * device,int index,struct of_irq * out_irq)962 static int of_irq_map_oldworld(struct device_node *device, int index,
963 struct of_irq *out_irq)
964 {
965 return -EINVAL;
966 }
967 #endif /* !(defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)) */
968
of_irq_map_one(struct device_node * device,int index,struct of_irq * out_irq)969 int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
970 {
971 struct device_node *p;
972 const u32 *intspec, *tmp, *addr;
973 u32 intsize, intlen;
974 int res;
975
976 DBG("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
977
978 /* OldWorld mac stuff is "special", handle out of line */
979 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
980 return of_irq_map_oldworld(device, index, out_irq);
981
982 /* Get the interrupts property */
983 intspec = of_get_property(device, "interrupts", &intlen);
984 if (intspec == NULL)
985 return -EINVAL;
986 intlen /= sizeof(u32);
987
988 /* Get the reg property (if any) */
989 addr = of_get_property(device, "reg", NULL);
990
991 /* Look for the interrupt parent. */
992 p = of_irq_find_parent(device);
993 if (p == NULL)
994 return -EINVAL;
995
996 /* Get size of interrupt specifier */
997 tmp = of_get_property(p, "#interrupt-cells", NULL);
998 if (tmp == NULL) {
999 of_node_put(p);
1000 return -EINVAL;
1001 }
1002 intsize = *tmp;
1003
1004 DBG(" intsize=%d intlen=%d\n", intsize, intlen);
1005
1006 /* Check index */
1007 if ((index + 1) * intsize > intlen)
1008 return -EINVAL;
1009
1010 /* Get new specifier and map it */
1011 res = of_irq_map_raw(p, intspec + index * intsize, intsize,
1012 addr, out_irq);
1013 of_node_put(p);
1014 return res;
1015 }
1016 EXPORT_SYMBOL_GPL(of_irq_map_one);
1017
1018 /**
1019 * Search the device tree for the best MAC address to use. 'mac-address' is
1020 * checked first, because that is supposed to contain to "most recent" MAC
1021 * address. If that isn't set, then 'local-mac-address' is checked next,
1022 * because that is the default address. If that isn't set, then the obsolete
1023 * 'address' is checked, just in case we're using an old device tree.
1024 *
1025 * Note that the 'address' property is supposed to contain a virtual address of
1026 * the register set, but some DTS files have redefined that property to be the
1027 * MAC address.
1028 *
1029 * All-zero MAC addresses are rejected, because those could be properties that
1030 * exist in the device tree, but were not set by U-Boot. For example, the
1031 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
1032 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
1033 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
1034 * but is all zeros.
1035 */
of_get_mac_address(struct device_node * np)1036 const void *of_get_mac_address(struct device_node *np)
1037 {
1038 struct property *pp;
1039
1040 pp = of_find_property(np, "mac-address", NULL);
1041 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
1042 return pp->value;
1043
1044 pp = of_find_property(np, "local-mac-address", NULL);
1045 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
1046 return pp->value;
1047
1048 pp = of_find_property(np, "address", NULL);
1049 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
1050 return pp->value;
1051
1052 return NULL;
1053 }
1054 EXPORT_SYMBOL(of_get_mac_address);
1055
of_irq_to_resource(struct device_node * dev,int index,struct resource * r)1056 int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
1057 {
1058 int irq = irq_of_parse_and_map(dev, index);
1059
1060 /* Only dereference the resource if both the
1061 * resource and the irq are valid. */
1062 if (r && irq != NO_IRQ) {
1063 r->start = r->end = irq;
1064 r->flags = IORESOURCE_IRQ;
1065 }
1066
1067 return irq;
1068 }
1069 EXPORT_SYMBOL_GPL(of_irq_to_resource);
1070
of_iomap(struct device_node * np,int index)1071 void __iomem *of_iomap(struct device_node *np, int index)
1072 {
1073 struct resource res;
1074
1075 if (of_address_to_resource(np, index, &res))
1076 return NULL;
1077
1078 return ioremap(res.start, 1 + res.end - res.start);
1079 }
1080 EXPORT_SYMBOL(of_iomap);
1081