iomap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Implement the default iomap interfaces
  3. *
  4. * (C) Copyright 2004 Linus Torvalds
  5. */
  6. #include <linux/pci.h>
  7. #include <linux/module.h>
  8. #include <asm/io.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. unsigned int fastcall ioread8(void __iomem *addr)
  49. {
  50. IO_COND(addr, return inb(port), return readb(addr));
  51. }
  52. unsigned int fastcall ioread16(void __iomem *addr)
  53. {
  54. IO_COND(addr, return inw(port), return readw(addr));
  55. }
  56. unsigned int fastcall ioread16be(void __iomem *addr)
  57. {
  58. IO_COND(addr, return inw(port), return be16_to_cpu(__raw_readw(addr)));
  59. }
  60. unsigned int fastcall ioread32(void __iomem *addr)
  61. {
  62. IO_COND(addr, return inl(port), return readl(addr));
  63. }
  64. unsigned int fastcall ioread32be(void __iomem *addr)
  65. {
  66. IO_COND(addr, return inl(port), return be32_to_cpu(__raw_readl(addr)));
  67. }
  68. EXPORT_SYMBOL(ioread8);
  69. EXPORT_SYMBOL(ioread16);
  70. EXPORT_SYMBOL(ioread16be);
  71. EXPORT_SYMBOL(ioread32);
  72. EXPORT_SYMBOL(ioread32be);
  73. void fastcall iowrite8(u8 val, void __iomem *addr)
  74. {
  75. IO_COND(addr, outb(val,port), writeb(val, addr));
  76. }
  77. void fastcall iowrite16(u16 val, void __iomem *addr)
  78. {
  79. IO_COND(addr, outw(val,port), writew(val, addr));
  80. }
  81. void fastcall iowrite16be(u16 val, void __iomem *addr)
  82. {
  83. IO_COND(addr, outw(val,port), __raw_writew(cpu_to_be16(val), addr));
  84. }
  85. void fastcall iowrite32(u32 val, void __iomem *addr)
  86. {
  87. IO_COND(addr, outl(val,port), writel(val, addr));
  88. }
  89. void fastcall iowrite32be(u32 val, void __iomem *addr)
  90. {
  91. IO_COND(addr, outl(val,port), __raw_writel(cpu_to_be32(val), addr));
  92. }
  93. EXPORT_SYMBOL(iowrite8);
  94. EXPORT_SYMBOL(iowrite16);
  95. EXPORT_SYMBOL(iowrite16be);
  96. EXPORT_SYMBOL(iowrite32);
  97. EXPORT_SYMBOL(iowrite32be);
  98. /*
  99. * These are the "repeat MMIO read/write" functions.
  100. * Note the "__raw" accesses, since we don't want to
  101. * convert to CPU byte order. We write in "IO byte
  102. * order" (we also don't have IO barriers).
  103. */
  104. static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
  105. {
  106. while (--count >= 0) {
  107. u8 data = __raw_readb(addr);
  108. *dst = data;
  109. dst++;
  110. }
  111. }
  112. static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
  113. {
  114. while (--count >= 0) {
  115. u16 data = __raw_readw(addr);
  116. *dst = data;
  117. dst++;
  118. }
  119. }
  120. static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
  121. {
  122. while (--count >= 0) {
  123. u32 data = __raw_readl(addr);
  124. *dst = data;
  125. dst++;
  126. }
  127. }
  128. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  129. {
  130. while (--count >= 0) {
  131. __raw_writeb(*src, addr);
  132. src++;
  133. }
  134. }
  135. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  136. {
  137. while (--count >= 0) {
  138. __raw_writew(*src, addr);
  139. src++;
  140. }
  141. }
  142. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  143. {
  144. while (--count >= 0) {
  145. __raw_writel(*src, addr);
  146. src++;
  147. }
  148. }
  149. void fastcall ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  150. {
  151. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  152. }
  153. void fastcall ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  154. {
  155. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  156. }
  157. void fastcall ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  158. {
  159. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  160. }
  161. EXPORT_SYMBOL(ioread8_rep);
  162. EXPORT_SYMBOL(ioread16_rep);
  163. EXPORT_SYMBOL(ioread32_rep);
  164. void fastcall iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  165. {
  166. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  167. }
  168. void fastcall iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  169. {
  170. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  171. }
  172. void fastcall iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  173. {
  174. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  175. }
  176. EXPORT_SYMBOL(iowrite8_rep);
  177. EXPORT_SYMBOL(iowrite16_rep);
  178. EXPORT_SYMBOL(iowrite32_rep);
  179. /* Create a virtual mapping cookie for an IO port range */
  180. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  181. {
  182. if (port > PIO_MASK)
  183. return NULL;
  184. return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  185. }
  186. void ioport_unmap(void __iomem *addr)
  187. {
  188. /* Nothing to do */
  189. }
  190. EXPORT_SYMBOL(ioport_map);
  191. EXPORT_SYMBOL(ioport_unmap);
  192. /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
  193. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  194. {
  195. unsigned long start = pci_resource_start(dev, bar);
  196. unsigned long len = pci_resource_len(dev, bar);
  197. unsigned long flags = pci_resource_flags(dev, bar);
  198. if (!len || !start)
  199. return NULL;
  200. if (maxlen && len > maxlen)
  201. len = maxlen;
  202. if (flags & IORESOURCE_IO)
  203. return ioport_map(start, len);
  204. if (flags & IORESOURCE_MEM) {
  205. if (flags & IORESOURCE_CACHEABLE)
  206. return ioremap(start, len);
  207. return ioremap_nocache(start, len);
  208. }
  209. /* What? */
  210. return NULL;
  211. }
  212. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  213. {
  214. IO_COND(addr, /* nothing */, iounmap(addr));
  215. }
  216. EXPORT_SYMBOL(pci_iomap);
  217. EXPORT_SYMBOL(pci_iounmap);