iomap.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. static void bad_io_access(unsigned long port, const char *access)
  35. {
  36. static int count = 10;
  37. if (count) {
  38. count--;
  39. printk(KERN_ERR "Bad IO access at port %lx (%s)\n", port, access);
  40. WARN_ON(1);
  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 fastcall ioread8(void __iomem *addr)
  65. {
  66. IO_COND(addr, return inb(port), return readb(addr));
  67. return 0xff;
  68. }
  69. unsigned int fastcall ioread16(void __iomem *addr)
  70. {
  71. IO_COND(addr, return inw(port), return readw(addr));
  72. return 0xffff;
  73. }
  74. unsigned int fastcall ioread16be(void __iomem *addr)
  75. {
  76. IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
  77. return 0xffff;
  78. }
  79. unsigned int fastcall ioread32(void __iomem *addr)
  80. {
  81. IO_COND(addr, return inl(port), return readl(addr));
  82. return 0xffffffff;
  83. }
  84. unsigned int fastcall ioread32be(void __iomem *addr)
  85. {
  86. IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
  87. return 0xffffffff;
  88. }
  89. EXPORT_SYMBOL(ioread8);
  90. EXPORT_SYMBOL(ioread16);
  91. EXPORT_SYMBOL(ioread16be);
  92. EXPORT_SYMBOL(ioread32);
  93. EXPORT_SYMBOL(ioread32be);
  94. #ifndef pio_write16be
  95. #define pio_write16be(val,port) outw(swab16(val),port)
  96. #define pio_write32be(val,port) outl(swab32(val),port)
  97. #endif
  98. #ifndef mmio_write16be
  99. #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
  100. #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
  101. #endif
  102. void fastcall iowrite8(u8 val, void __iomem *addr)
  103. {
  104. IO_COND(addr, outb(val,port), writeb(val, addr));
  105. }
  106. void fastcall iowrite16(u16 val, void __iomem *addr)
  107. {
  108. IO_COND(addr, outw(val,port), writew(val, addr));
  109. }
  110. void fastcall iowrite16be(u16 val, void __iomem *addr)
  111. {
  112. IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
  113. }
  114. void fastcall iowrite32(u32 val, void __iomem *addr)
  115. {
  116. IO_COND(addr, outl(val,port), writel(val, addr));
  117. }
  118. void fastcall iowrite32be(u32 val, void __iomem *addr)
  119. {
  120. IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
  121. }
  122. EXPORT_SYMBOL(iowrite8);
  123. EXPORT_SYMBOL(iowrite16);
  124. EXPORT_SYMBOL(iowrite16be);
  125. EXPORT_SYMBOL(iowrite32);
  126. EXPORT_SYMBOL(iowrite32be);
  127. /*
  128. * These are the "repeat MMIO read/write" functions.
  129. * Note the "__raw" accesses, since we don't want to
  130. * convert to CPU byte order. We write in "IO byte
  131. * order" (we also don't have IO barriers).
  132. */
  133. #ifndef mmio_insb
  134. static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
  135. {
  136. while (--count >= 0) {
  137. u8 data = __raw_readb(addr);
  138. *dst = data;
  139. dst++;
  140. }
  141. }
  142. static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
  143. {
  144. while (--count >= 0) {
  145. u16 data = __raw_readw(addr);
  146. *dst = data;
  147. dst++;
  148. }
  149. }
  150. static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
  151. {
  152. while (--count >= 0) {
  153. u32 data = __raw_readl(addr);
  154. *dst = data;
  155. dst++;
  156. }
  157. }
  158. #endif
  159. #ifndef mmio_outsb
  160. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  161. {
  162. while (--count >= 0) {
  163. __raw_writeb(*src, addr);
  164. src++;
  165. }
  166. }
  167. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  168. {
  169. while (--count >= 0) {
  170. __raw_writew(*src, addr);
  171. src++;
  172. }
  173. }
  174. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  175. {
  176. while (--count >= 0) {
  177. __raw_writel(*src, addr);
  178. src++;
  179. }
  180. }
  181. #endif
  182. void fastcall ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  183. {
  184. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  185. }
  186. void fastcall ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  187. {
  188. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  189. }
  190. void fastcall ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  191. {
  192. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  193. }
  194. EXPORT_SYMBOL(ioread8_rep);
  195. EXPORT_SYMBOL(ioread16_rep);
  196. EXPORT_SYMBOL(ioread32_rep);
  197. void fastcall iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  198. {
  199. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  200. }
  201. void fastcall iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  202. {
  203. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  204. }
  205. void fastcall iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  206. {
  207. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  208. }
  209. EXPORT_SYMBOL(iowrite8_rep);
  210. EXPORT_SYMBOL(iowrite16_rep);
  211. EXPORT_SYMBOL(iowrite32_rep);
  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. /**
  226. * pci_iomap - create a virtual mapping cookie for a PCI BAR
  227. * @dev: PCI device that owns the BAR
  228. * @bar: BAR number
  229. * @maxlen: length of the memory to map
  230. *
  231. * Using this function you will get a __iomem address to your device BAR.
  232. * You can access it using ioread*() and iowrite*(). These functions hide
  233. * the details if this is a MMIO or PIO address space and will just do what
  234. * you expect from them in the correct way.
  235. *
  236. * @maxlen specifies the maximum length to map. If you want to get access to
  237. * the complete BAR without checking for its length first, pass %0 here.
  238. * */
  239. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  240. {
  241. unsigned long start = pci_resource_start(dev, bar);
  242. unsigned long len = pci_resource_len(dev, bar);
  243. unsigned long flags = pci_resource_flags(dev, bar);
  244. if (!len || !start)
  245. return NULL;
  246. if (maxlen && len > maxlen)
  247. len = maxlen;
  248. if (flags & IORESOURCE_IO)
  249. return ioport_map(start, len);
  250. if (flags & IORESOURCE_MEM) {
  251. if (flags & IORESOURCE_CACHEABLE)
  252. return ioremap(start, len);
  253. return ioremap_nocache(start, len);
  254. }
  255. /* What? */
  256. return NULL;
  257. }
  258. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  259. {
  260. IO_COND(addr, /* nothing */, iounmap(addr));
  261. }
  262. EXPORT_SYMBOL(pci_iomap);
  263. EXPORT_SYMBOL(pci_iounmap);