Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 1 | |
| 2 | #include <linux/io.h> |
| 3 | #include <linux/ioport.h> |
| 4 | #include <linux/of_address.h> |
| 5 | |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame^] | 6 | int __of_address_to_resource(struct device_node *dev, const u32 *addrp, |
| 7 | u64 size, unsigned int flags, |
| 8 | struct resource *r) |
| 9 | { |
| 10 | u64 taddr; |
| 11 | |
| 12 | if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0) |
| 13 | return -EINVAL; |
| 14 | taddr = of_translate_address(dev, addrp); |
| 15 | if (taddr == OF_BAD_ADDR) |
| 16 | return -EINVAL; |
| 17 | memset(r, 0, sizeof(struct resource)); |
| 18 | if (flags & IORESOURCE_IO) { |
| 19 | unsigned long port; |
| 20 | port = pci_address_to_pio(taddr); |
| 21 | if (port == (unsigned long)-1) |
| 22 | return -EINVAL; |
| 23 | r->start = port; |
| 24 | r->end = port + size - 1; |
| 25 | } else { |
| 26 | r->start = taddr; |
| 27 | r->end = taddr + size - 1; |
| 28 | } |
| 29 | r->flags = flags; |
| 30 | r->name = dev->name; |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * of_address_to_resource - Translate device tree address and return as resource |
| 36 | * |
| 37 | * Note that if your address is a PIO address, the conversion will fail if |
| 38 | * the physical address can't be internally converted to an IO token with |
| 39 | * pci_address_to_pio(), that is because it's either called to early or it |
| 40 | * can't be matched to any host bridge IO space |
| 41 | */ |
| 42 | int of_address_to_resource(struct device_node *dev, int index, |
| 43 | struct resource *r) |
| 44 | { |
| 45 | const u32 *addrp; |
| 46 | u64 size; |
| 47 | unsigned int flags; |
| 48 | |
| 49 | addrp = of_get_address(dev, index, &size, &flags); |
| 50 | if (addrp == NULL) |
| 51 | return -EINVAL; |
| 52 | return __of_address_to_resource(dev, addrp, size, flags, r); |
| 53 | } |
| 54 | EXPORT_SYMBOL_GPL(of_address_to_resource); |
| 55 | |
| 56 | |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 57 | /** |
| 58 | * of_iomap - Maps the memory mapped IO for a given device_node |
| 59 | * @device: the device whose io range will be mapped |
| 60 | * @index: index of the io range |
| 61 | * |
| 62 | * Returns a pointer to the mapped memory |
| 63 | */ |
| 64 | void __iomem *of_iomap(struct device_node *np, int index) |
| 65 | { |
| 66 | struct resource res; |
| 67 | |
| 68 | if (of_address_to_resource(np, index, &res)) |
| 69 | return NULL; |
| 70 | |
| 71 | return ioremap(res.start, 1 + res.end - res.start); |
| 72 | } |
| 73 | EXPORT_SYMBOL(of_iomap); |