iomap.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /*
  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. static void bad_io_access(unsigned long port, const char *access)
  35. {
  36. static int count = 10;
  37. if (count) {
  38. count--;
  39. WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access);
  40. }
  41. }
  42. /*
  43. * Ugly macros are a way of life.
  44. */
  45. #define IO_COND(addr, is_pio, is_mmio) do { \
  46. unsigned long port = (unsigned long __force)addr; \
  47. if (port >= PIO_RESERVED) { \
  48. is_mmio; \
  49. } else if (port > PIO_OFFSET) { \
  50. port &= PIO_MASK; \
  51. is_pio; \
  52. } else \
  53. bad_io_access(port, #is_pio ); \
  54. } while (0)
  55. #ifndef pio_read16be
  56. #define pio_read16be(port) swab16(inw(port))
  57. #define pio_read32be(port) swab32(inl(port))
  58. #endif
  59. #ifndef mmio_read16be
  60. #define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr))
  61. #define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr))
  62. #endif
  63. unsigned int ioread8(void __iomem *addr)
  64. {
  65. IO_COND(addr, return inb(port), return readb(addr));
  66. return 0xff;
  67. }
  68. unsigned int ioread16(void __iomem *addr)
  69. {
  70. IO_COND(addr, return inw(port), return readw(addr));
  71. return 0xffff;
  72. }
  73. unsigned int ioread16be(void __iomem *addr)
  74. {
  75. IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
  76. return 0xffff;
  77. }
  78. unsigned int ioread32(void __iomem *addr)
  79. {
  80. IO_COND(addr, return inl(port), return readl(addr));
  81. return 0xffffffff;
  82. }
  83. unsigned int ioread32be(void __iomem *addr)
  84. {
  85. IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
  86. return 0xffffffff;
  87. }
  88. EXPORT_SYMBOL(ioread8);
  89. EXPORT_SYMBOL(ioread16);
  90. EXPORT_SYMBOL(ioread16be);
  91. EXPORT_SYMBOL(ioread32);
  92. EXPORT_SYMBOL(ioread32be);
  93. #ifndef pio_write16be
  94. #define pio_write16be(val,port) outw(swab16(val),port)
  95. #define pio_write32be(val,port) outl(swab32(val),port)
  96. #endif
  97. #ifndef mmio_write16be
  98. #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
  99. #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
  100. #endif
  101. void iowrite8(u8 val, void __iomem *addr)
  102. {
  103. IO_COND(addr, outb(val,port), writeb(val, addr));
  104. }
  105. void iowrite16(u16 val, void __iomem *addr)
  106. {
  107. IO_COND(addr, outw(val,port), writew(val, addr));
  108. }
  109. void iowrite16be(u16 val, void __iomem *addr)
  110. {
  111. IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
  112. }
  113. void iowrite32(u32 val, void __iomem *addr)
  114. {
  115. IO_COND(addr, outl(val,port), writel(val, addr));
  116. }
  117. void iowrite32be(u32 val, void __iomem *addr)
  118. {
  119. IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
  120. }
  121. EXPORT_SYMBOL(iowrite8);
  122. EXPORT_SYMBOL(iowrite16);
  123. EXPORT_SYMBOL(iowrite16be);
  124. EXPORT_SYMBOL(iowrite32);
  125. EXPORT_SYMBOL(iowrite32be);
  126. /*
  127. * These are the "repeat MMIO read/write" functions.
  128. * Note the "__raw" accesses, since we don't want to
  129. * convert to CPU byte order. We write in "IO byte
  130. * order" (we also don't have IO barriers).
  131. */
  132. #ifndef mmio_insb
  133. static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
  134. {
  135. while (--count >= 0) {
  136. u8 data = __raw_readb(addr);
  137. *dst = data;
  138. dst++;
  139. }
  140. }
  141. static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
  142. {
  143. while (--count >= 0) {
  144. u16 data = __raw_readw(addr);
  145. *dst = data;
  146. dst++;
  147. }
  148. }
  149. static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
  150. {
  151. while (--count >= 0) {
  152. u32 data = __raw_readl(addr);
  153. *dst = data;
  154. dst++;
  155. }
  156. }
  157. #endif
  158. #ifndef mmio_outsb
  159. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  160. {
  161. while (--count >= 0) {
  162. __raw_writeb(*src, addr);
  163. src++;
  164. }
  165. }
  166. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  167. {
  168. while (--count >= 0) {
  169. __raw_writew(*src, addr);
  170. src++;
  171. }
  172. }
  173. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  174. {
  175. while (--count >= 0) {
  176. __raw_writel(*src, addr);
  177. src++;
  178. }
  179. }
  180. #endif
  181. void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  182. {
  183. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  184. }
  185. void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  186. {
  187. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  188. }
  189. void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  190. {
  191. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  192. }
  193. EXPORT_SYMBOL(ioread8_rep);
  194. EXPORT_SYMBOL(ioread16_rep);
  195. EXPORT_SYMBOL(ioread32_rep);
  196. void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  197. {
  198. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  199. }
  200. void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  201. {
  202. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  203. }
  204. void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  205. {
  206. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  207. }
  208. EXPORT_SYMBOL(iowrite8_rep);
  209. EXPORT_SYMBOL(iowrite16_rep);
  210. EXPORT_SYMBOL(iowrite32_rep);
  211. #ifdef CONFIG_HAS_IOPORT_MAP
  212. /* Create a virtual mapping cookie for an IO port range */
  213. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  214. {
  215. if (port > PIO_MASK)
  216. return NULL;
  217. return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  218. }
  219. void ioport_unmap(void __iomem *addr)
  220. {
  221. /* Nothing to do */
  222. }
  223. EXPORT_SYMBOL(ioport_map);
  224. EXPORT_SYMBOL(ioport_unmap);
  225. #endif /* CONFIG_HAS_IOPORT_MAP */
  226. #ifdef CONFIG_PCI
  227. /* Hide the details if this is a MMIO or PIO address space and just do what
  228. * you expect in the correct way. */
  229. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  230. {
  231. IO_COND(addr, /* nothing */, iounmap(addr));
  232. }
  233. EXPORT_SYMBOL(pci_iounmap);
  234. #endif /* CONFIG_PCI */