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