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>
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 && \
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,
26 static void of_dump_addr(const char *s, const u32 *addr, int na)
28 printk(KERN_INFO "%s", s);
30 printk(KERN_INFO " %08x", *(addr++));
31 printk(KERN_INFO "\n");
34 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
37 /* Callbacks for bus specific translators */
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);
51 * Default translator (generic bus)
54 static void of_bus_default_count_cells(struct device_node *dev,
55 int *addrc, int *sizec)
58 *addrc = of_n_addr_cells(dev);
60 *sizec = of_n_size_cells(dev);
63 static u64 of_bus_default_map(u32 *addr, const u32 *range,
64 int na, int ns, int pna)
68 cp = of_read_number(range, na);
69 s = of_read_number(range + na + pna, ns);
70 da = of_read_number(addr, na);
72 pr_debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
75 if (da < cp || da >= (cp + s))
80 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
82 u64 a = of_read_number(addr, na);
83 memset(addr, 0, na * 4);
86 addr[na - 2] = a >> 32;
87 addr[na - 1] = a & 0xffffffffu;
92 static unsigned int of_bus_default_get_flags(const u32 *addr)
94 return IORESOURCE_MEM;
99 * PCI bus specific translator
102 static int of_bus_pci_match(struct device_node *np)
104 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
105 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
108 static void of_bus_pci_count_cells(struct device_node *np,
109 int *addrc, int *sizec)
117 static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
121 /* Check address type match */
122 if ((addr[0] ^ range[0]) & 0x03000000)
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);
130 pr_debug("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
132 if (da < cp || da >= (cp + s))
137 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
139 return of_bus_default_translate(addr + 1, offset, na - 1);
142 static unsigned int of_bus_pci_get_flags(const u32 *addr)
144 unsigned int flags = 0;
147 switch ((w >> 24) & 0x03) {
149 flags |= IORESOURCE_IO;
151 case 0x02: /* 32 bits */
152 case 0x03: /* 64 bits */
153 flags |= IORESOURCE_MEM;
157 flags |= IORESOURCE_PREFETCH;
161 const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
166 struct device_node *parent;
168 int onesize, i, na, ns;
170 /* Get parent & match bus type */
171 parent = of_get_parent(dev);
174 bus = of_match_bus(parent);
175 if (strcmp(bus->name, "pci")) {
179 bus->count_cells(dev, &na, &ns);
181 if (!OF_CHECK_COUNTS(na, ns))
184 /* Get "reg" or "assigned-addresses" property */
185 prop = of_get_property(dev, bus->addresses, &psize);
191 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
192 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
194 *size = of_read_number(prop + na, ns);
196 *flags = bus->get_flags(prop);
201 EXPORT_SYMBOL(of_get_pci_address);
203 int of_pci_address_to_resource(struct device_node *dev, int bar,
210 addrp = of_get_pci_address(dev, bar, &size, &flags);
213 return __of_address_to_resource(dev, addrp, size, flags, r);
215 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
217 static u8 of_irq_pci_swizzle(u8 slot, u8 pin)
219 return (((pin - 1) + slot) % 4) + 1;
222 int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
224 struct device_node *dn, *ppnode;
225 struct pci_dev *ppdev;
231 /* Check if we have a device node, if yes, fallback to standard OF
234 dn = pci_device_to_OF_node(pdev);
236 return of_irq_map_one(dn, 0, out_irq);
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.
242 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
249 /* Now we walk up the PCI tree */
252 /* Get the pci_dev of our parent */
253 ppdev = pdev->bus->self;
255 /* Ouch, it's a host bridge... */
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 */
264 /* We found a P2P bridge, check if it has a node */
265 ppnode = pci_device_to_OF_node(ppdev);
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).
281 /* We can only get here if we hit a P2P bridge with no node,
282 * let's do standard swizzling and try again
284 lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec);
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);
293 EXPORT_SYMBOL_GPL(of_irq_map_pci);
294 #endif /* CONFIG_PCI */
297 * ISA bus specific translator
300 static int of_bus_isa_match(struct device_node *np)
302 return !strcmp(np->name, "isa");
305 static void of_bus_isa_count_cells(struct device_node *child,
306 int *addrc, int *sizec)
314 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
318 /* Check address type match */
319 if ((addr[0] ^ range[0]) & 0x00000001)
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);
327 pr_debug("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
329 if (da < cp || da >= (cp + s))
334 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
336 return of_bus_default_translate(addr + 1, offset, na - 1);
339 static unsigned int of_bus_isa_get_flags(const u32 *addr)
341 unsigned int flags = 0;
345 flags |= IORESOURCE_IO;
347 flags |= IORESOURCE_MEM;
352 * Array of bus specific translators
355 static struct of_bus of_busses[] = {
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,
367 #endif /* CONFIG_PCI */
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,
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,
390 static struct of_bus *of_match_bus(struct device_node *np)
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];
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)
408 u64 offset = OF_BAD_ADDR;
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.
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");
430 pr_debug("OF: walking ranges...\n");
432 /* Now walk through the ranges */
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)
440 if (offset == OF_BAD_ADDR) {
441 pr_debug("OF: not found !\n");
444 memcpy(addr, ranges + na, 4 * pna);
447 of_dump_addr("OF: parent translation for:", addr, pna);
448 pr_debug("OF: with offset: "PRu64"\n", offset);
450 /* Translate it into parent bus space */
451 return pbus->translate(addr, offset, pna);
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
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
464 u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
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;
472 pr_debug("OF: ** translation for device %s **\n", dev->full_name);
474 /* Increase refcount at current level */
477 /* Get parent & match bus type */
478 parent = of_get_parent(dev);
481 bus = of_match_bus(parent);
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",
490 memcpy(addr, in_addr, na * 4);
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);
498 /* Switch to parent bus */
501 parent = of_get_parent(dev);
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);
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",
519 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
520 pbus->name, pna, pns, parent->full_name);
522 /* Apply bus translation */
523 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
526 /* Complete the move up one level */
531 of_dump_addr("OF: one level translation:", addr, na);
539 EXPORT_SYMBOL(of_translate_address);
541 const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
546 struct device_node *parent;
548 int onesize, i, na, ns;
550 /* Get parent & match bus type */
551 parent = of_get_parent(dev);
554 bus = of_match_bus(parent);
555 bus->count_cells(dev, &na, &ns);
557 if (!OF_CHECK_COUNTS(na, ns))
560 /* Get "reg" or "assigned-addresses" property */
561 prop = of_get_property(dev, bus->addresses, (int *) &psize);
567 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
570 *size = of_read_number(prop + na, ns);
572 *flags = bus->get_flags(prop);
577 EXPORT_SYMBOL(of_get_address);
579 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
580 u64 size, unsigned int flags,
585 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
587 taddr = of_translate_address(dev, addrp);
588 if (taddr == OF_BAD_ADDR)
590 memset(r, 0, sizeof(struct resource));
591 if (flags & IORESOURCE_IO) {
593 port = -1; /* pci_address_to_pio(taddr); */
594 if (port == (unsigned long)-1)
597 r->end = port + size - 1;
600 r->end = taddr + size - 1;
607 int of_address_to_resource(struct device_node *dev, int index,
614 addrp = of_get_address(dev, index, &size, &flags);
617 return __of_address_to_resource(dev, addrp, size, flags, r);
619 EXPORT_SYMBOL_GPL(of_address_to_resource);
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)
624 const u32 *dma_window;
626 const unsigned char *prop;
628 dma_window = dma_window_prop;
630 /* busno is always one cell */
631 *busno = *(dma_window++);
633 prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
635 prop = of_get_property(dn, "#address-cells", NULL);
637 cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
638 *phys = of_read_number(dma_window, cells);
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);
648 * Search the device tree for the best MAC address to use. 'mac-address' is
649 * checked first, because that is supposed to contain to "most recent" MAC
650 * address. If that isn't set, then 'local-mac-address' is checked next,
651 * because that is the default address. If that isn't set, then the obsolete
652 * 'address' is checked, just in case we're using an old device tree.
654 * Note that the 'address' property is supposed to contain a virtual address of
655 * the register set, but some DTS files have redefined that property to be the
658 * All-zero MAC addresses are rejected, because those could be properties that
659 * exist in the device tree, but were not set by U-Boot. For example, the
660 * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
661 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
662 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
665 const void *of_get_mac_address(struct device_node *np)
669 pp = of_find_property(np, "mac-address", NULL);
670 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
673 pp = of_find_property(np, "local-mac-address", NULL);
674 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
677 pp = of_find_property(np, "address", NULL);
678 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
683 EXPORT_SYMBOL(of_get_mac_address);