pci_iomap.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Implement the default iomap interfaces
  3. *
  4. * (C) Copyright 2004 Linus Torvalds
  5. */
  6. #include <linux/pci.h>
  7. #include <linux/io.h>
  8. #include <linux/export.h>
  9. #ifdef CONFIG_PCI
  10. /**
  11. * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
  12. * @dev: PCI device that owns the BAR
  13. * @bar: BAR number
  14. * @offset: map memory at the given offset in BAR
  15. * @maxlen: max length of the memory to map
  16. *
  17. * Using this function you will get a __iomem address to your device BAR.
  18. * You can access it using ioread*() and iowrite*(). These functions hide
  19. * the details if this is a MMIO or PIO address space and will just do what
  20. * you expect from them in the correct way.
  21. *
  22. * @maxlen specifies the maximum length to map. If you want to get access to
  23. * the complete BAR from offset to the end, pass %0 here.
  24. * */
  25. void __iomem *pci_iomap_range(struct pci_dev *dev,
  26. int bar,
  27. unsigned long offset,
  28. unsigned long maxlen)
  29. {
  30. resource_size_t start = pci_resource_start(dev, bar);
  31. resource_size_t len = pci_resource_len(dev, bar);
  32. unsigned long flags = pci_resource_flags(dev, bar);
  33. if (len <= offset || !start)
  34. return NULL;
  35. len -= offset;
  36. start += offset;
  37. if (maxlen && len > maxlen)
  38. len = maxlen;
  39. if (flags & IORESOURCE_IO)
  40. return __pci_ioport_map(dev, start, len);
  41. if (flags & IORESOURCE_MEM) {
  42. if (flags & IORESOURCE_CACHEABLE)
  43. return ioremap(start, len);
  44. return ioremap_nocache(start, len);
  45. }
  46. /* What? */
  47. return NULL;
  48. }
  49. EXPORT_SYMBOL(pci_iomap_range);
  50. /**
  51. * pci_iomap - create a virtual mapping cookie for a PCI BAR
  52. * @dev: PCI device that owns the BAR
  53. * @bar: BAR number
  54. * @maxlen: length of the memory to map
  55. *
  56. * Using this function you will get a __iomem address to your device BAR.
  57. * You can access it using ioread*() and iowrite*(). These functions hide
  58. * the details if this is a MMIO or PIO address space and will just do what
  59. * you expect from them in the correct way.
  60. *
  61. * @maxlen specifies the maximum length to map. If you want to get access to
  62. * the complete BAR without checking for its length first, pass %0 here.
  63. * */
  64. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  65. {
  66. return pci_iomap_range(dev, bar, 0, maxlen);
  67. }
  68. EXPORT_SYMBOL(pci_iomap);
  69. #endif /* CONFIG_PCI */