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", *(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] = a >> 32;
83 addr[na - 1] = 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 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
196 *size = of_read_number(prop + na, ns);
198 *flags = bus->get_flags(prop);
203 EXPORT_SYMBOL(of_get_pci_address);
205 int of_pci_address_to_resource(struct device_node *dev, int bar,
212 addrp = of_get_pci_address(dev, bar, &size, &flags);
215 return __of_address_to_resource(dev, addrp, size, flags, r);
217 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
218 #endif /* CONFIG_PCI */
221 * ISA bus specific translator
224 static int of_bus_isa_match(struct device_node *np)
226 return !strcmp(np->name, "isa");
229 static void of_bus_isa_count_cells(struct device_node *child,
230 int *addrc, int *sizec)
238 static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
242 /* Check address type match */
243 if ((addr[0] ^ range[0]) & 0x00000001)
246 /* Read address values, skipping high cell */
247 cp = of_read_number(range + 1, na - 1);
248 s = of_read_number(range + na + pna, ns);
249 da = of_read_number(addr + 1, na - 1);
251 pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
252 (unsigned long long)cp, (unsigned long long)s,
253 (unsigned long long)da);
255 if (da < cp || da >= (cp + s))
260 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
262 return of_bus_default_translate(addr + 1, offset, na - 1);
265 static unsigned int of_bus_isa_get_flags(const u32 *addr)
267 unsigned int flags = 0;
271 flags |= IORESOURCE_IO;
273 flags |= IORESOURCE_MEM;
278 * Array of bus specific translators
281 static struct of_bus of_busses[] = {
286 .addresses = "assigned-addresses",
287 .match = of_bus_pci_match,
288 .count_cells = of_bus_pci_count_cells,
289 .map = of_bus_pci_map,
290 .translate = of_bus_pci_translate,
291 .get_flags = of_bus_pci_get_flags,
293 #endif /* CONFIG_PCI */
298 .match = of_bus_isa_match,
299 .count_cells = of_bus_isa_count_cells,
300 .map = of_bus_isa_map,
301 .translate = of_bus_isa_translate,
302 .get_flags = of_bus_isa_get_flags,
309 .count_cells = of_bus_default_count_cells,
310 .map = of_bus_default_map,
311 .translate = of_bus_default_translate,
312 .get_flags = of_bus_default_get_flags,
316 static struct of_bus *of_match_bus(struct device_node *np)
320 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
321 if (!of_busses[i].match || of_busses[i].match(np))
322 return &of_busses[i];
327 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
328 struct of_bus *pbus, u32 *addr,
329 int na, int ns, int pna, const char *rprop)
334 u64 offset = OF_BAD_ADDR;
336 /* Normally, an absence of a "ranges" property means we are
337 * crossing a non-translatable boundary, and thus the addresses
338 * below the current not cannot be converted to CPU physical ones.
339 * Unfortunately, while this is very clear in the spec, it's not
340 * what Apple understood, and they do have things like /uni-n or
341 * /ht nodes with no "ranges" property and a lot of perfectly
342 * useable mapped devices below them. Thus we treat the absence of
343 * "ranges" as equivalent to an empty "ranges" property which means
344 * a 1:1 translation at that level. It's up to the caller not to try
345 * to translate addresses that aren't supposed to be translated in
346 * the first place. --BenH.
348 ranges = of_get_property(parent, rprop, &rlen);
349 if (ranges == NULL || rlen == 0) {
350 offset = of_read_number(addr, na);
351 memset(addr, 0, pna * 4);
352 pr_debug("OF: no ranges, 1:1 translation\n");
356 pr_debug("OF: walking ranges...\n");
358 /* Now walk through the ranges */
360 rone = na + pna + ns;
361 for (; rlen >= rone; rlen -= rone, ranges += rone) {
362 offset = bus->map(addr, ranges, na, ns, pna);
363 if (offset != OF_BAD_ADDR)
366 if (offset == OF_BAD_ADDR) {
367 pr_debug("OF: not found !\n");
370 memcpy(addr, ranges + na, 4 * pna);
373 of_dump_addr("OF: parent translation for:", addr, pna);
374 pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
376 /* Translate it into parent bus space */
377 return pbus->translate(addr, offset, pna);
381 * Translate an address from the device-tree into a CPU physical address,
382 * this walks up the tree and applies the various bus mappings on the
385 * Note: We consider that crossing any level with #size-cells == 0 to mean
386 * that translation is impossible (that is we are not dealing with a value
387 * that can be mapped to a cpu physical address). This is not really specified
388 * that way, but this is traditionally the way IBM at least do things
390 u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
393 struct device_node *parent = NULL;
394 struct of_bus *bus, *pbus;
395 u32 addr[OF_MAX_ADDR_CELLS];
396 int na, ns, pna, pns;
397 u64 result = OF_BAD_ADDR;
399 pr_debug("OF: ** translation for device %s **\n", dev->full_name);
401 /* Increase refcount at current level */
404 /* Get parent & match bus type */
405 parent = of_get_parent(dev);
408 bus = of_match_bus(parent);
410 /* Cound address cells & copy address locally */
411 bus->count_cells(dev, &na, &ns);
412 if (!OF_CHECK_COUNTS(na, ns)) {
413 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
417 memcpy(addr, in_addr, na * 4);
419 pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
420 bus->name, na, ns, parent->full_name);
421 of_dump_addr("OF: translating address:", addr, na);
425 /* Switch to parent bus */
428 parent = of_get_parent(dev);
430 /* If root, we have finished */
431 if (parent == NULL) {
432 pr_debug("OF: reached root node\n");
433 result = of_read_number(addr, na);
437 /* Get new parent bus and counts */
438 pbus = of_match_bus(parent);
439 pbus->count_cells(dev, &pna, &pns);
440 if (!OF_CHECK_COUNTS(pna, pns)) {
441 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
446 pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
447 pbus->name, pna, pns, parent->full_name);
449 /* Apply bus translation */
450 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
453 /* Complete the move up one level */
458 of_dump_addr("OF: one level translation:", addr, na);
467 u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
469 return __of_translate_address(dev, in_addr, "ranges");
471 EXPORT_SYMBOL(of_translate_address);
473 u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
475 return __of_translate_address(dev, in_addr, "dma-ranges");
477 EXPORT_SYMBOL(of_translate_dma_address);
479 const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
484 struct device_node *parent;
486 int onesize, i, na, ns;
488 /* Get parent & match bus type */
489 parent = of_get_parent(dev);
492 bus = of_match_bus(parent);
493 bus->count_cells(dev, &na, &ns);
495 if (!OF_CHECK_COUNTS(na, ns))
498 /* Get "reg" or "assigned-addresses" property */
499 prop = of_get_property(dev, bus->addresses, &psize);
505 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
508 *size = of_read_number(prop + na, ns);
510 *flags = bus->get_flags(prop);
515 EXPORT_SYMBOL(of_get_address);
517 static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
518 u64 size, unsigned int flags,
523 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
525 taddr = of_translate_address(dev, addrp);
526 if (taddr == OF_BAD_ADDR)
528 memset(r, 0, sizeof(struct resource));
529 if (flags & IORESOURCE_IO) {
531 port = pci_address_to_pio(taddr);
532 if (port == (unsigned long)-1)
535 r->end = port + size - 1;
538 r->end = taddr + size - 1;
546 * of_address_to_resource - Translate device tree address and return as resource
548 * Note that if your address is a PIO address, the conversion will fail if
549 * the physical address can't be internally converted to an IO token with
550 * pci_address_to_pio(), that is because it's either called to early or it
551 * can't be matched to any host bridge IO space
553 int of_address_to_resource(struct device_node *dev, int index,
560 addrp = of_get_address(dev, index, &size, &flags);
563 return __of_address_to_resource(dev, addrp, size, flags, r);
565 EXPORT_SYMBOL_GPL(of_address_to_resource);
569 * of_iomap - Maps the memory mapped IO for a given device_node
570 * @device: the device whose io range will be mapped
571 * @index: index of the io range
573 * Returns a pointer to the mapped memory
575 void __iomem *of_iomap(struct device_node *np, int index)
579 if (of_address_to_resource(np, index, &res))
582 return ioremap(res.start, 1 + res.end - res.start);
584 EXPORT_SYMBOL(of_iomap);