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>
10 #include <asm/pci-bridge.h>
13 #define DBG(fmt...) do { printk(fmt); } while(0)
15 #define DBG(fmt...) do { } while(0)
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 && \
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,
37 static void of_dump_addr(const char *s, const u32 *addr, int na)
41 printk(" %08x", *(addr++));
45 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
49 /* Callbacks for bus specific translators */
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);
64 * Default translator (generic bus)
67 static void of_bus_default_count_cells(struct device_node *dev,
68 int *addrc, int *sizec)
71 *addrc = of_n_addr_cells(dev);
73 *sizec = of_n_size_cells(dev);
76 static u64 of_bus_default_map(u32 *addr, const u32 *range,
77 int na, int ns, int pna)
81 cp = of_read_number(range, na);
82 s = of_read_number(range + na + pna, ns);
83 da = of_read_number(addr, na);
85 DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
88 if (da < cp || da >= (cp + s))
93 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
95 u64 a = of_read_number(addr, na);
96 memset(addr, 0, na * 4);
99 addr[na - 2] = a >> 32;
100 addr[na - 1] = a & 0xffffffffu;
105 static unsigned int of_bus_default_get_flags(const u32 *addr)
107 return IORESOURCE_MEM;
113 * PCI bus specific translator
116 static int of_bus_pci_match(struct device_node *np)
118 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
119 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
122 static void of_bus_pci_count_cells(struct device_node *np,
123 int *addrc, int *sizec)
131 static unsigned int of_bus_pci_get_flags(const u32 *addr)
133 unsigned int flags = 0;
136 switch((w >> 24) & 0x03) {
138 flags |= IORESOURCE_IO;
140 case 0x02: /* 32 bits */
141 case 0x03: /* 64 bits */
142 flags |= IORESOURCE_MEM;
146 flags |= IORESOURCE_PREFETCH;
150 static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
155 af = of_bus_pci_get_flags(addr);
156 rf = of_bus_pci_get_flags(range);
158 /* Check address type match */
159 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
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);
167 DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
169 if (da < cp || da >= (cp + s))
174 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
176 return of_bus_default_translate(addr + 1, offset, na - 1);
179 const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
184 struct device_node *parent;
186 int onesize, i, na, ns;
188 /* Get parent & match bus type */
189 parent = of_get_parent(dev);
192 bus = of_match_bus(parent);
193 if (strcmp(bus->name, "pci")) {
197 bus->count_cells(dev, &na, &ns);
199 if (!OF_CHECK_COUNTS(na, ns))
202 /* Get "reg" or "assigned-addresses" property */
203 prop = of_get_property(dev, bus->addresses, &psize);
209 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
210 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
212 *size = of_read_number(prop + na, ns);
214 *flags = bus->get_flags(prop);
219 EXPORT_SYMBOL(of_get_pci_address);
221 int of_pci_address_to_resource(struct device_node *dev, int bar,
228 addrp = of_get_pci_address(dev, bar, &size, &flags);
231 return __of_address_to_resource(dev, addrp, size, flags, r);
233 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
235 int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
237 struct device_node *dn, *ppnode;
238 struct pci_dev *ppdev;
244 /* Check if we have a device node, if yes, fallback to standard OF
247 dn = pci_device_to_OF_node(pdev);
249 rc = of_irq_map_one(dn, 0, out_irq);
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.
258 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
265 /* Now we walk up the PCI tree */
268 /* Get the pci_dev of our parent */
269 ppdev = pdev->bus->self;
271 /* Ouch, it's a host bridge... */
274 ppnode = pci_bus_to_OF_node(pdev->bus);
276 struct pci_controller *host;
277 host = pci_bus_to_host(pdev->bus);
278 ppnode = host ? host->dn : NULL;
280 /* No node for host bridge ? give up */
284 /* We found a P2P bridge, check if it has a node */
285 ppnode = pci_device_to_OF_node(ppdev);
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).
301 /* We can only get here if we hit a P2P bridge with no node,
302 * let's do standard swizzling and try again
304 lspec = pci_swizzle_interrupt_pin(pdev, lspec);
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);
313 EXPORT_SYMBOL_GPL(of_irq_map_pci);
314 #endif /* CONFIG_PCI */
317 * ISA bus specific translator
320 static int of_bus_isa_match(struct device_node *np)
322 return !strcmp(np->name, "isa");
325 static void of_bus_isa_count_cells(struct device_node *child,
326 int *addrc, int *sizec)
334 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
338 /* Check address type match */
339 if ((addr[0] ^ range[0]) & 0x00000001)
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);
347 DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
349 if (da < cp || da >= (cp + s))
354 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
356 return of_bus_default_translate(addr + 1, offset, na - 1);
359 static unsigned int of_bus_isa_get_flags(const u32 *addr)
361 unsigned int flags = 0;
365 flags |= IORESOURCE_IO;
367 flags |= IORESOURCE_MEM;
373 * Array of bus specific translators
376 static struct of_bus of_busses[] = {
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,
388 #endif /* CONFIG_PCI */
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,
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,
411 static struct of_bus *of_match_bus(struct device_node *np)
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];
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)
429 u64 offset = OF_BAD_ADDR;
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.
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");
451 DBG("OF: walking ranges...\n");
453 /* Now walk through the ranges */
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)
461 if (offset == OF_BAD_ADDR) {
462 DBG("OF: not found !\n");
465 memcpy(addr, ranges + na, 4 * pna);
468 of_dump_addr("OF: parent translation for:", addr, pna);
469 DBG("OF: with offset: "PRu64"\n", offset);
471 /* Translate it into parent bus space */
472 return pbus->translate(addr, offset, pna);
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
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
486 u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
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;
495 DBG("OF: ** translation for device %s **\n", dev->full_name);
497 /* Increase refcount at current level */
500 /* Get parent & match bus type */
501 parent = of_get_parent(dev);
504 bus = of_match_bus(parent);
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",
513 memcpy(addr, in_addr, na * 4);
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);
521 /* Switch to parent bus */
524 parent = of_get_parent(dev);
526 /* If root, we have finished */
527 if (parent == NULL) {
528 DBG("OF: reached root node\n");
529 result = of_read_number(addr, na);
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",
542 DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
543 pbus->name, pna, pns, parent->full_name);
545 /* Apply bus translation */
546 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
549 /* Complete the move up one level */
554 of_dump_addr("OF: one level translation:", addr, na);
563 u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
565 return __of_translate_address(dev, in_addr, "ranges");
567 EXPORT_SYMBOL(of_translate_address);
569 u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
571 return __of_translate_address(dev, in_addr, "dma-ranges");
573 EXPORT_SYMBOL(of_translate_dma_address);
575 const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
580 struct device_node *parent;
582 int onesize, i, na, ns;
584 /* Get parent & match bus type */
585 parent = of_get_parent(dev);
588 bus = of_match_bus(parent);
589 bus->count_cells(dev, &na, &ns);
591 if (!OF_CHECK_COUNTS(na, ns))
594 /* Get "reg" or "assigned-addresses" property */
595 prop = of_get_property(dev, bus->addresses, &psize);
601 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
604 *size = of_read_number(prop + na, ns);
606 *flags = bus->get_flags(prop);
611 EXPORT_SYMBOL(of_get_address);
613 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
614 u64 size, unsigned int flags,
619 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
621 taddr = of_translate_address(dev, addrp);
622 if (taddr == OF_BAD_ADDR)
624 memset(r, 0, sizeof(struct resource));
625 if (flags & IORESOURCE_IO) {
627 port = pci_address_to_pio(taddr);
628 if (port == (unsigned long)-1)
631 r->end = port + size - 1;
634 r->end = taddr + size - 1;
641 int of_address_to_resource(struct device_node *dev, int index,
648 addrp = of_get_address(dev, index, &size, &flags);
651 return __of_address_to_resource(dev, addrp, size, flags, r);
653 EXPORT_SYMBOL_GPL(of_address_to_resource);
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)
658 const u32 *dma_window;
660 const unsigned char *prop;
662 dma_window = dma_window_prop;
664 /* busno is always one cell */
665 *busno = *(dma_window++);
667 prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
669 prop = of_get_property(dn, "#address-cells", NULL);
671 cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
672 *phys = of_read_number(dma_window, cells);
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);
682 * Search the device tree for the best MAC address to use. 'mac-address' is
683 * checked first, because that is supposed to contain to "most recent" MAC
684 * address. If that isn't set, then 'local-mac-address' is checked next,
685 * because that is the default address. If that isn't set, then the obsolete
686 * 'address' is checked, just in case we're using an old device tree.
688 * Note that the 'address' property is supposed to contain a virtual address of
689 * the register set, but some DTS files have redefined that property to be the
692 * All-zero MAC addresses are rejected, because those could be properties that
693 * exist in the device tree, but were not set by U-Boot. For example, the
694 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
695 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
696 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
699 const void *of_get_mac_address(struct device_node *np)
703 pp = of_find_property(np, "mac-address", NULL);
704 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
707 pp = of_find_property(np, "local-mac-address", NULL);
708 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
711 pp = of_find_property(np, "address", NULL);
712 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
717 EXPORT_SYMBOL(of_get_mac_address);
719 void __iomem *of_iomap(struct device_node *np, int index)
723 if (of_address_to_resource(np, index, &res))
726 return ioremap(res.start, 1 + res.end - res.start);
728 EXPORT_SYMBOL(of_iomap);