address.c 1.8 KB

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