io.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* Generic I/O port emulation, based on MN10300 code
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #ifndef __ASM_GENERIC_IO_H
  12. #define __ASM_GENERIC_IO_H
  13. #include <asm/page.h> /* I/O is all done through memory accesses */
  14. #include <asm/cacheflush.h>
  15. #include <linux/types.h>
  16. #ifdef CONFIG_GENERIC_IOMAP
  17. #include <asm-generic/iomap.h>
  18. #endif
  19. #ifndef mmiowb
  20. #define mmiowb() do {} while (0)
  21. #endif
  22. /*****************************************************************************/
  23. /*
  24. * readX/writeX() are used to access memory mapped devices. On some
  25. * architectures the memory mapped IO stuff needs to be accessed
  26. * differently. On the simple architectures, we just read/write the
  27. * memory location directly.
  28. */
  29. #ifndef __raw_readb
  30. static inline u8 __raw_readb(const volatile void __iomem *addr)
  31. {
  32. return *(const volatile u8 __force *) addr;
  33. }
  34. #endif
  35. #ifndef __raw_readw
  36. static inline u16 __raw_readw(const volatile void __iomem *addr)
  37. {
  38. return *(const volatile u16 __force *) addr;
  39. }
  40. #endif
  41. #ifndef __raw_readl
  42. static inline u32 __raw_readl(const volatile void __iomem *addr)
  43. {
  44. return *(const volatile u32 __force *) addr;
  45. }
  46. #endif
  47. #define readb __raw_readb
  48. #define readw(addr) __le16_to_cpu(__raw_readw(addr))
  49. #define readl(addr) __le32_to_cpu(__raw_readl(addr))
  50. #ifndef __raw_writeb
  51. static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
  52. {
  53. *(volatile u8 __force *) addr = b;
  54. }
  55. #endif
  56. #ifndef __raw_writew
  57. static inline void __raw_writew(u16 b, volatile void __iomem *addr)
  58. {
  59. *(volatile u16 __force *) addr = b;
  60. }
  61. #endif
  62. #ifndef __raw_writel
  63. static inline void __raw_writel(u32 b, volatile void __iomem *addr)
  64. {
  65. *(volatile u32 __force *) addr = b;
  66. }
  67. #endif
  68. #define writeb __raw_writeb
  69. #define writew(b,addr) __raw_writew(__cpu_to_le16(b),addr)
  70. #define writel(b,addr) __raw_writel(__cpu_to_le32(b),addr)
  71. #ifdef CONFIG_64BIT
  72. static inline u64 __raw_readq(const volatile void __iomem *addr)
  73. {
  74. return *(const volatile u64 __force *) addr;
  75. }
  76. #define readq(addr) __le64_to_cpu(__raw_readq(addr))
  77. static inline void __raw_writeq(u64 b, volatile void __iomem *addr)
  78. {
  79. *(volatile u64 __force *) addr = b;
  80. }
  81. #define writeq(b,addr) __raw_writeq(__cpu_to_le64(b),addr)
  82. #endif
  83. /*****************************************************************************/
  84. /*
  85. * traditional input/output functions
  86. */
  87. static inline u8 inb(unsigned long addr)
  88. {
  89. return readb((volatile void __iomem *) addr);
  90. }
  91. static inline u16 inw(unsigned long addr)
  92. {
  93. return readw((volatile void __iomem *) addr);
  94. }
  95. static inline u32 inl(unsigned long addr)
  96. {
  97. return readl((volatile void __iomem *) addr);
  98. }
  99. static inline void outb(u8 b, unsigned long addr)
  100. {
  101. writeb(b, (volatile void __iomem *) addr);
  102. }
  103. static inline void outw(u16 b, unsigned long addr)
  104. {
  105. writew(b, (volatile void __iomem *) addr);
  106. }
  107. static inline void outl(u32 b, unsigned long addr)
  108. {
  109. writel(b, (volatile void __iomem *) addr);
  110. }
  111. #define inb_p(addr) inb(addr)
  112. #define inw_p(addr) inw(addr)
  113. #define inl_p(addr) inl(addr)
  114. #define outb_p(x, addr) outb((x), (addr))
  115. #define outw_p(x, addr) outw((x), (addr))
  116. #define outl_p(x, addr) outl((x), (addr))
  117. #ifndef insb
  118. static inline void insb(unsigned long addr, void *buffer, int count)
  119. {
  120. if (count) {
  121. u8 *buf = buffer;
  122. do {
  123. u8 x = inb(addr);
  124. *buf++ = x;
  125. } while (--count);
  126. }
  127. }
  128. #endif
  129. #ifndef insw
  130. static inline void insw(unsigned long addr, void *buffer, int count)
  131. {
  132. if (count) {
  133. u16 *buf = buffer;
  134. do {
  135. u16 x = inw(addr);
  136. *buf++ = x;
  137. } while (--count);
  138. }
  139. }
  140. #endif
  141. #ifndef insl
  142. static inline void insl(unsigned long addr, void *buffer, int count)
  143. {
  144. if (count) {
  145. u32 *buf = buffer;
  146. do {
  147. u32 x = inl(addr);
  148. *buf++ = x;
  149. } while (--count);
  150. }
  151. }
  152. #endif
  153. #ifndef outsb
  154. static inline void outsb(unsigned long addr, const void *buffer, int count)
  155. {
  156. if (count) {
  157. const u8 *buf = buffer;
  158. do {
  159. outb(*buf++, addr);
  160. } while (--count);
  161. }
  162. }
  163. #endif
  164. #ifndef outsw
  165. static inline void outsw(unsigned long addr, const void *buffer, int count)
  166. {
  167. if (count) {
  168. const u16 *buf = buffer;
  169. do {
  170. outw(*buf++, addr);
  171. } while (--count);
  172. }
  173. }
  174. #endif
  175. #ifndef outsl
  176. static inline void outsl(unsigned long addr, const void *buffer, int count)
  177. {
  178. if (count) {
  179. const u32 *buf = buffer;
  180. do {
  181. outl(*buf++, addr);
  182. } while (--count);
  183. }
  184. }
  185. #endif
  186. #ifndef CONFIG_GENERIC_IOMAP
  187. #define ioread8(addr) readb(addr)
  188. #define ioread16(addr) readw(addr)
  189. #define ioread16be(addr) be16_to_cpu(ioread16(addr))
  190. #define ioread32(addr) readl(addr)
  191. #define ioread32be(addr) be32_to_cpu(ioread32(addr))
  192. #define iowrite8(v, addr) writeb((v), (addr))
  193. #define iowrite16(v, addr) writew((v), (addr))
  194. #define iowrite16be(v, addr) iowrite16(be16_to_cpu(v), (addr))
  195. #define iowrite32(v, addr) writel((v), (addr))
  196. #define iowrite32be(v, addr) iowrite32(be32_to_cpu(v), (addr))
  197. #define ioread8_rep(p, dst, count) \
  198. insb((unsigned long) (p), (dst), (count))
  199. #define ioread16_rep(p, dst, count) \
  200. insw((unsigned long) (p), (dst), (count))
  201. #define ioread32_rep(p, dst, count) \
  202. insl((unsigned long) (p), (dst), (count))
  203. #define iowrite8_rep(p, src, count) \
  204. outsb((unsigned long) (p), (src), (count))
  205. #define iowrite16_rep(p, src, count) \
  206. outsw((unsigned long) (p), (src), (count))
  207. #define iowrite32_rep(p, src, count) \
  208. outsl((unsigned long) (p), (src), (count))
  209. #endif /* CONFIG_GENERIC_IOMAP */
  210. #define IO_SPACE_LIMIT 0xffffffff
  211. #ifdef __KERNEL__
  212. #include <linux/vmalloc.h>
  213. #define __io_virt(x) ((void __force *) (x))
  214. #ifndef CONFIG_GENERIC_IOMAP
  215. /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
  216. struct pci_dev;
  217. extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);
  218. static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
  219. {
  220. }
  221. #endif /* CONFIG_GENERIC_IOMAP */
  222. /*
  223. * Change virtual addresses to physical addresses and vv.
  224. * These are pretty trivial
  225. */
  226. static inline unsigned long virt_to_phys(volatile void *address)
  227. {
  228. return __pa((unsigned long)address);
  229. }
  230. static inline void *phys_to_virt(unsigned long address)
  231. {
  232. return __va(address);
  233. }
  234. /*
  235. * Change "struct page" to physical address.
  236. */
  237. static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
  238. {
  239. return (void __iomem*) (unsigned long)offset;
  240. }
  241. #define __ioremap(offset, size, flags) ioremap(offset, size)
  242. #ifndef ioremap_nocache
  243. #define ioremap_nocache ioremap
  244. #endif
  245. #ifndef ioremap_wc
  246. #define ioremap_wc ioremap_nocache
  247. #endif
  248. static inline void iounmap(void *addr)
  249. {
  250. }
  251. #ifndef CONFIG_GENERIC_IOMAP
  252. static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
  253. {
  254. return (void __iomem *) port;
  255. }
  256. static inline void ioport_unmap(void __iomem *p)
  257. {
  258. }
  259. #else /* CONFIG_GENERIC_IOMAP */
  260. extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
  261. extern void ioport_unmap(void __iomem *p);
  262. #endif /* CONFIG_GENERIC_IOMAP */
  263. #define xlate_dev_kmem_ptr(p) p
  264. #define xlate_dev_mem_ptr(p) ((void *) (p))
  265. #ifndef virt_to_bus
  266. static inline unsigned long virt_to_bus(volatile void *address)
  267. {
  268. return ((unsigned long) address);
  269. }
  270. static inline void *bus_to_virt(unsigned long address)
  271. {
  272. return (void *) address;
  273. }
  274. #endif
  275. #define memset_io(a, b, c) memset(__io_virt(a), (b), (c))
  276. #define memcpy_fromio(a, b, c) memcpy((a), __io_virt(b), (c))
  277. #define memcpy_toio(a, b, c) memcpy(__io_virt(a), (b), (c))
  278. #endif /* __KERNEL__ */
  279. #endif /* __ASM_GENERIC_IO_H */