3 #include <linux/ioport.h>
4 #include <linux/module.h>
5 #include <linux/of_address.h>
6 #include <linux/pci_regs.h>
7 #include <linux/string.h>
9 /* Max address size we deal with */
10 #define OF_MAX_ADDR_CELLS 4
11 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
14 static struct of_bus *of_match_bus(struct device_node *np);
15 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
16 u64 size, unsigned int flags,
21 static void of_dump_addr(const char *s, const u32 *addr, int na)
23 printk(KERN_DEBUG "%s", s);
25 printk(" %08x", be32_to_cpu(*(addr++)));
29 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
32 /* Callbacks for bus specific translators */
35 const char *addresses;
36 int (*match)(struct device_node *parent);
37 void (*count_cells)(struct device_node *child,
38 int *addrc, int *sizec);
39 u64 (*map)(u32 *addr, const u32 *range,
40 int na, int ns, int pna);
41 int (*translate)(u32 *addr, u64 offset, int na);
42 unsigned int (*get_flags)(const u32 *addr);
46 * Default translator (generic bus)
49 static void of_bus_default_count_cells(struct device_node *dev,
50 int *addrc, int *sizec)
53 *addrc = of_n_addr_cells(dev);
55 *sizec = of_n_size_cells(dev);
58 static u64 of_bus_default_map(u32 *addr, const u32 *range,
59 int na, int ns, int pna)
63 cp = of_read_number(range, na);
64 s = of_read_number(range + na + pna, ns);
65 da = of_read_number(addr, na);
67 pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
68 (unsigned long long)cp, (unsigned long long)s,
69 (unsigned long long)da);
71 if (da < cp || da >= (cp + s))
76 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
78 u64 a = of_read_number(addr, na);
79 memset(addr, 0, na * 4);
82 addr[na - 2] = cpu_to_be32(a >> 32);
83 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
88 static unsigned int of_bus_default_get_flags(const u32 *addr)
90 return IORESOURCE_MEM;
95 * PCI bus specific translator
98 static int of_bus_pci_match(struct device_node *np)
100 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
101 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
104 static void of_bus_pci_count_cells(struct device_node *np,
105 int *addrc, int *sizec)
113 static unsigned int of_bus_pci_get_flags(const u32 *addr)
115 unsigned int flags = 0;
118 switch((w >> 24) & 0x03) {
120 flags |= IORESOURCE_IO;
122 case 0x02: /* 32 bits */
123 case 0x03: /* 64 bits */
124 flags |= IORESOURCE_MEM;
128 flags |= IORESOURCE_PREFETCH;
132 static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
137 af = of_bus_pci_get_flags(addr);
138 rf = of_bus_pci_get_flags(range);
140 /* Check address type match */
141 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
144 /* Read address values, skipping high cell */
145 cp = of_read_number(range + 1, na - 1);
146 s = of_read_number(range + na + pna, ns);
147 da = of_read_number(addr + 1, na - 1);
149 pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
150 (unsigned long long)cp, (unsigned long long)s,
151 (unsigned long long)da);
153 if (da < cp || da >= (cp + s))
158 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
160 return of_bus_default_translate(addr + 1, offset, na - 1);
163 const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
168 struct device_node *parent;
170 int onesize, i, na, ns;
172 /* Get parent & match bus type */
173 parent = of_get_parent(dev);
176 bus = of_match_bus(parent);
177 if (strcmp(bus->name, "pci")) {
181 bus->count_cells(dev, &na, &ns);
183 if (!OF_CHECK_COUNTS(na, ns))
186 /* Get "reg" or "assigned-addresses" property */
187 prop = of_get_property(dev, bus->addresses, &psize);
193 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
194 u32 val = be32_to_cpu(prop[0]);
195 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
197 *size = of_read_number(prop + na, ns);
199 *flags = bus->get_flags(prop);
205 EXPORT_SYMBOL(of_get_pci_address);
207 int of_pci_address_to_resource(struct device_node *dev, int bar,
214 addrp = of_get_pci_address(dev, bar, &size, &flags);
217 return __of_address_to_resource(dev, addrp, size, flags, r);
219 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
220 #endif /* CONFIG_PCI */
223 * ISA bus specific translator
226 static int of_bus_isa_match(struct device_node *np)
228 return !strcmp(np->name, "isa");
231 static void of_bus_isa_count_cells(struct device_node *child,
232 int *addrc, int *sizec)
240 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
244 /* Check address type match */
245 if ((addr[0] ^ range[0]) & 0x00000001)
248 /* Read address values, skipping high cell */
249 cp = of_read_number(range + 1, na - 1);
250 s = of_read_number(range + na + pna, ns);
251 da = of_read_number(addr + 1, na - 1);
253 pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
254 (unsigned long long)cp, (unsigned long long)s,
255 (unsigned long long)da);
257 if (da < cp || da >= (cp + s))
262 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
264 return of_bus_default_translate(addr + 1, offset, na - 1);
267 static unsigned int of_bus_isa_get_flags(const u32 *addr)
269 unsigned int flags = 0;
273 flags |= IORESOURCE_IO;
275 flags |= IORESOURCE_MEM;
280 * Array of bus specific translators
283 static struct of_bus of_busses[] = {
288 .addresses = "assigned-addresses",
289 .match = of_bus_pci_match,
290 .count_cells = of_bus_pci_count_cells,
291 .map = of_bus_pci_map,
292 .translate = of_bus_pci_translate,
293 .get_flags = of_bus_pci_get_flags,
295 #endif /* CONFIG_PCI */
300 .match = of_bus_isa_match,
301 .count_cells = of_bus_isa_count_cells,
302 .map = of_bus_isa_map,
303 .translate = of_bus_isa_translate,
304 .get_flags = of_bus_isa_get_flags,
311 .count_cells = of_bus_default_count_cells,
312 .map = of_bus_default_map,
313 .translate = of_bus_default_translate,
314 .get_flags = of_bus_default_get_flags,
318 static struct of_bus *of_match_bus(struct device_node *np)
322 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
323 if (!of_busses[i].match || of_busses[i].match(np))
324 return &of_busses[i];
329 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
330 struct of_bus *pbus, u32 *addr,
331 int na, int ns, int pna, const char *rprop)
336 u64 offset = OF_BAD_ADDR;
338 /* Normally, an absence of a "ranges" property means we are
339 * crossing a non-translatable boundary, and thus the addresses
340 * below the current not cannot be converted to CPU physical ones.
341 * Unfortunately, while this is very clear in the spec, it's not
342 * what Apple understood, and they do have things like /uni-n or
343 * /ht nodes with no "ranges" property and a lot of perfectly
344 * useable mapped devices below them. Thus we treat the absence of
345 * "ranges" as equivalent to an empty "ranges" property which means
346 * a 1:1 translation at that level. It's up to the caller not to try
347 * to translate addresses that aren't supposed to be translated in
348 * the first place. --BenH.
350 * As far as we know, this damage only exists on Apple machines, so
351 * This code is only enabled on powerpc. --gcl
353 ranges = of_get_property(parent, rprop, &rlen);
354 #if !defined(CONFIG_PPC)
355 if (ranges == NULL) {
356 pr_err("OF: no ranges; cannot translate\n");
359 #endif /* !defined(CONFIG_PPC) */
360 if (ranges == NULL || rlen == 0) {
361 offset = of_read_number(addr, na);
362 memset(addr, 0, pna * 4);
363 pr_debug("OF: empty ranges; 1:1 translation\n");
367 pr_debug("OF: walking ranges...\n");
369 /* Now walk through the ranges */
371 rone = na + pna + ns;
372 for (; rlen >= rone; rlen -= rone, ranges += rone) {
373 offset = bus->map(addr, ranges, na, ns, pna);
374 if (offset != OF_BAD_ADDR)
377 if (offset == OF_BAD_ADDR) {
378 pr_debug("OF: not found !\n");
381 memcpy(addr, ranges + na, 4 * pna);
384 of_dump_addr("OF: parent translation for:", addr, pna);
385 pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
387 /* Translate it into parent bus space */
388 return pbus->translate(addr, offset, pna);
392 * Translate an address from the device-tree into a CPU physical address,
393 * this walks up the tree and applies the various bus mappings on the
396 * Note: We consider that crossing any level with #size-cells == 0 to mean
397 * that translation is impossible (that is we are not dealing with a value
398 * that can be mapped to a cpu physical address). This is not really specified
399 * that way, but this is traditionally the way IBM at least do things
401 u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
404 struct device_node *parent = NULL;
405 struct of_bus *bus, *pbus;
406 u32 addr[OF_MAX_ADDR_CELLS];
407 int na, ns, pna, pns;
408 u64 result = OF_BAD_ADDR;
410 pr_debug("OF: ** translation for device %s **\n", dev->full_name);
412 /* Increase refcount at current level */
415 /* Get parent & match bus type */
416 parent = of_get_parent(dev);
419 bus = of_match_bus(parent);
421 /* Cound address cells & copy address locally */
422 bus->count_cells(dev, &na, &ns);
423 if (!OF_CHECK_COUNTS(na, ns)) {
424 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
428 memcpy(addr, in_addr, na * 4);
430 pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
431 bus->name, na, ns, parent->full_name);
432 of_dump_addr("OF: translating address:", addr, na);
436 /* Switch to parent bus */
439 parent = of_get_parent(dev);
441 /* If root, we have finished */
442 if (parent == NULL) {
443 pr_debug("OF: reached root node\n");
444 result = of_read_number(addr, na);
448 /* Get new parent bus and counts */
449 pbus = of_match_bus(parent);
450 pbus->count_cells(dev, &pna, &pns);
451 if (!OF_CHECK_COUNTS(pna, pns)) {
452 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
457 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
458 pbus->name, pna, pns, parent->full_name);
460 /* Apply bus translation */
461 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
464 /* Complete the move up one level */
469 of_dump_addr("OF: one level translation:", addr, na);
478 u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
480 return __of_translate_address(dev, in_addr, "ranges");
482 EXPORT_SYMBOL(of_translate_address);
484 u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
486 return __of_translate_address(dev, in_addr, "dma-ranges");
488 EXPORT_SYMBOL(of_translate_dma_address);
490 const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
495 struct device_node *parent;
497 int onesize, i, na, ns;
499 /* Get parent & match bus type */
500 parent = of_get_parent(dev);
503 bus = of_match_bus(parent);
504 bus->count_cells(dev, &na, &ns);
506 if (!OF_CHECK_COUNTS(na, ns))
509 /* Get "reg" or "assigned-addresses" property */
510 prop = of_get_property(dev, bus->addresses, &psize);
516 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
519 *size = of_read_number(prop + na, ns);
521 *flags = bus->get_flags(prop);
526 EXPORT_SYMBOL(of_get_address);
528 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
529 u64 size, unsigned int flags,
534 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
536 taddr = of_translate_address(dev, addrp);
537 if (taddr == OF_BAD_ADDR)
539 memset(r, 0, sizeof(struct resource));
540 if (flags & IORESOURCE_IO) {
542 port = pci_address_to_pio(taddr);
543 if (port == (unsigned long)-1)
546 r->end = port + size - 1;
549 r->end = taddr + size - 1;
552 r->name = dev->full_name;
557 * of_address_to_resource - Translate device tree address and return as resource
559 * Note that if your address is a PIO address, the conversion will fail if
560 * the physical address can't be internally converted to an IO token with
561 * pci_address_to_pio(), that is because it's either called to early or it
562 * can't be matched to any host bridge IO space
564 int of_address_to_resource(struct device_node *dev, int index,
571 addrp = of_get_address(dev, index, &size, &flags);
574 return __of_address_to_resource(dev, addrp, size, flags, r);
576 EXPORT_SYMBOL_GPL(of_address_to_resource);
580 * of_iomap - Maps the memory mapped IO for a given device_node
581 * @device: the device whose io range will be mapped
582 * @index: index of the io range
584 * Returns a pointer to the mapped memory
586 void __iomem *of_iomap(struct device_node *np, int index)
590 if (of_address_to_resource(np, index, &res))
593 return ioremap(res.start, 1 + res.end - res.start);
595 EXPORT_SYMBOL(of_iomap);