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