iomap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/module.h>
  9. /*
  10. * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
  11. * access or a MMIO access, these functions don't care. The info is
  12. * encoded in the hardware mapping set up by the mapping functions
  13. * (or the cookie itself, depending on implementation and hw).
  14. *
  15. * The generic routines don't assume any hardware mappings, and just
  16. * encode the PIO/MMIO as part of the cookie. They coldly assume that
  17. * the MMIO IO mappings are not in the low address range.
  18. *
  19. * Architectures for which this is not true can't use this generic
  20. * implementation and should do their own copy.
  21. */
  22. #ifndef HAVE_ARCH_PIO_SIZE
  23. /*
  24. * We encode the physical PIO addresses (0-0xffff) into the
  25. * pointer by offsetting them with a constant (0x10000) and
  26. * assuming that all the low addresses are always PIO. That means
  27. * we can do some sanity checks on the low bits, and don't
  28. * need to just take things for granted.
  29. */
  30. #define PIO_OFFSET 0x10000UL
  31. #define PIO_MASK 0x0ffffUL
  32. #define PIO_RESERVED 0x40000UL
  33. #endif
  34. /*
  35. * Ugly macros are a way of life.
  36. */
  37. #define VERIFY_PIO(port) BUG_ON((port & ~PIO_MASK) != PIO_OFFSET)
  38. #define IO_COND(addr, is_pio, is_mmio) do { \
  39. unsigned long port = (unsigned long __force)addr; \
  40. if (port < PIO_RESERVED) { \
  41. VERIFY_PIO(port); \
  42. port &= PIO_MASK; \
  43. is_pio; \
  44. } else { \
  45. is_mmio; \
  46. } \
  47. } while (0)
  48. #ifndef pio_read16be
  49. #define pio_read16be(port) swab16(inw(port))
  50. #define pio_read32be(port) swab32(inl(port))
  51. #endif
  52. #ifndef mmio_read16be
  53. #define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr))
  54. #define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr))
  55. #endif
  56. unsigned int fastcall ioread8(void __iomem *addr)
  57. {
  58. IO_COND(addr, return inb(port), return readb(addr));
  59. }
  60. unsigned int fastcall ioread16(void __iomem *addr)
  61. {
  62. IO_COND(addr, return inw(port), return readw(addr));
  63. }
  64. unsigned int fastcall ioread16be(void __iomem *addr)
  65. {
  66. IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
  67. }
  68. unsigned int fastcall ioread32(void __iomem *addr)
  69. {
  70. IO_COND(addr, return inl(port), return readl(addr));
  71. }
  72. unsigned int fastcall ioread32be(void __iomem *addr)
  73. {
  74. IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
  75. }
  76. EXPORT_SYMBOL(ioread8);
  77. EXPORT_SYMBOL(ioread16);
  78. EXPORT_SYMBOL(ioread16be);
  79. EXPORT_SYMBOL(ioread32);
  80. EXPORT_SYMBOL(ioread32be);
  81. #ifndef pio_write16be
  82. #define pio_write16be(val,port) outw(swab16(val),port)
  83. #define pio_write32be(val,port) outl(swab32(val),port)
  84. #endif
  85. #ifndef mmio_write16be
  86. #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
  87. #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
  88. #endif
  89. void fastcall iowrite8(u8 val, void __iomem *addr)
  90. {
  91. IO_COND(addr, outb(val,port), writeb(val, addr));
  92. }
  93. void fastcall iowrite16(u16 val, void __iomem *addr)
  94. {
  95. IO_COND(addr, outw(val,port), writew(val, addr));
  96. }
  97. void fastcall iowrite16be(u16 val, void __iomem *addr)
  98. {
  99. IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
  100. }
  101. void fastcall iowrite32(u32 val, void __iomem *addr)
  102. {
  103. IO_COND(addr, outl(val,port), writel(val, addr));
  104. }
  105. void fastcall iowrite32be(u32 val, void __iomem *addr)
  106. {
  107. IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
  108. }
  109. EXPORT_SYMBOL(iowrite8);
  110. EXPORT_SYMBOL(iowrite16);
  111. EXPORT_SYMBOL(iowrite16be);
  112. EXPORT_SYMBOL(iowrite32);
  113. EXPORT_SYMBOL(iowrite32be);
  114. /*
  115. * These are the "repeat MMIO read/write" functions.
  116. * Note the "__raw" accesses, since we don't want to
  117. * convert to CPU byte order. We write in "IO byte
  118. * order" (we also don't have IO barriers).
  119. */
  120. #ifndef mmio_insb
  121. static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
  122. {
  123. while (--count >= 0) {
  124. u8 data = __raw_readb(addr);
  125. *dst = data;
  126. dst++;
  127. }
  128. }
  129. static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
  130. {
  131. while (--count >= 0) {
  132. u16 data = __raw_readw(addr);
  133. *dst = data;
  134. dst++;
  135. }
  136. }
  137. static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
  138. {
  139. while (--count >= 0) {
  140. u32 data = __raw_readl(addr);
  141. *dst = data;
  142. dst++;
  143. }
  144. }
  145. #endif
  146. #ifndef mmio_outsb
  147. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  148. {
  149. while (--count >= 0) {
  150. __raw_writeb(*src, addr);
  151. src++;
  152. }
  153. }
  154. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  155. {
  156. while (--count >= 0) {
  157. __raw_writew(*src, addr);
  158. src++;
  159. }
  160. }
  161. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  162. {
  163. while (--count >= 0) {
  164. __raw_writel(*src, addr);
  165. src++;
  166. }
  167. }
  168. #endif
  169. void fastcall ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  170. {
  171. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  172. }
  173. void fastcall ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  174. {
  175. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  176. }
  177. void fastcall ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  178. {
  179. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  180. }
  181. EXPORT_SYMBOL(ioread8_rep);
  182. EXPORT_SYMBOL(ioread16_rep);
  183. EXPORT_SYMBOL(ioread32_rep);
  184. void fastcall iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  185. {
  186. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  187. }
  188. void fastcall iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  189. {
  190. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  191. }
  192. void fastcall iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  193. {
  194. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  195. }
  196. EXPORT_SYMBOL(iowrite8_rep);
  197. EXPORT_SYMBOL(iowrite16_rep);
  198. EXPORT_SYMBOL(iowrite32_rep);
  199. /* Create a virtual mapping cookie for an IO port range */
  200. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  201. {
  202. if (port > PIO_MASK)
  203. return NULL;
  204. return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  205. }
  206. void ioport_unmap(void __iomem *addr)
  207. {
  208. /* Nothing to do */
  209. }
  210. EXPORT_SYMBOL(ioport_map);
  211. EXPORT_SYMBOL(ioport_unmap);
  212. /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
  213. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  214. {
  215. unsigned long start = pci_resource_start(dev, bar);
  216. unsigned long len = pci_resource_len(dev, bar);
  217. unsigned long flags = pci_resource_flags(dev, bar);
  218. if (!len || !start)
  219. return NULL;
  220. if (maxlen && len > maxlen)
  221. len = maxlen;
  222. if (flags & IORESOURCE_IO)
  223. return ioport_map(start, len);
  224. if (flags & IORESOURCE_MEM) {
  225. if (flags & IORESOURCE_CACHEABLE)
  226. return ioremap(start, len);
  227. return ioremap_nocache(start, len);
  228. }
  229. /* What? */
  230. return NULL;
  231. }
  232. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  233. {
  234. IO_COND(addr, /* nothing */, iounmap(addr));
  235. }
  236. EXPORT_SYMBOL(pci_iomap);
  237. EXPORT_SYMBOL(pci_iounmap);