iomap.c 7.2 KB

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