io.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #include <asm-generic/pci_iomap.h>
  20. #ifndef mmiowb
  21. #define mmiowb() do {} while (0)
  22. #endif
  23. /*****************************************************************************/
  24. /*
  25. * readX/writeX() are used to access memory mapped devices. On some
  26. * architectures the memory mapped IO stuff needs to be accessed
  27. * differently. On the simple architectures, we just read/write the
  28. * memory location directly.
  29. */
  30. #ifndef __raw_readb
  31. static inline u8 __raw_readb(const volatile void __iomem *addr)
  32. {
  33. return *(const volatile u8 __force *) addr;
  34. }
  35. #endif
  36. #ifndef __raw_readw
  37. static inline u16 __raw_readw(const volatile void __iomem *addr)
  38. {
  39. return *(const volatile u16 __force *) addr;
  40. }
  41. #endif
  42. #ifndef __raw_readl
  43. static inline u32 __raw_readl(const volatile void __iomem *addr)
  44. {
  45. return *(const volatile u32 __force *) addr;
  46. }
  47. #endif
  48. #define readb __raw_readb
  49. #define readw(addr) __le16_to_cpu(__raw_readw(addr))
  50. #define readl(addr) __le32_to_cpu(__raw_readl(addr))
  51. #ifndef __raw_writeb
  52. static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
  53. {
  54. *(volatile u8 __force *) addr = b;
  55. }
  56. #endif
  57. #ifndef __raw_writew
  58. static inline void __raw_writew(u16 b, volatile void __iomem *addr)
  59. {
  60. *(volatile u16 __force *) addr = b;
  61. }
  62. #endif
  63. #ifndef __raw_writel
  64. static inline void __raw_writel(u32 b, volatile void __iomem *addr)
  65. {
  66. *(volatile u32 __force *) addr = b;
  67. }
  68. #endif
  69. #define writeb __raw_writeb
  70. #define writew(b,addr) __raw_writew(__cpu_to_le16(b),addr)
  71. #define writel(b,addr) __raw_writel(__cpu_to_le32(b),addr)
  72. #ifdef CONFIG_64BIT
  73. static inline u64 __raw_readq(const volatile void __iomem *addr)
  74. {
  75. return *(const volatile u64 __force *) addr;
  76. }
  77. #define readq(addr) __le64_to_cpu(__raw_readq(addr))
  78. static inline void __raw_writeq(u64 b, volatile void __iomem *addr)
  79. {
  80. *(volatile u64 __force *) addr = b;
  81. }
  82. #define writeq(b,addr) __raw_writeq(__cpu_to_le64(b),addr)
  83. #endif
  84. #ifndef PCI_IOBASE
  85. #define PCI_IOBASE ((void __iomem *) 0)
  86. #endif
  87. /*****************************************************************************/
  88. /*
  89. * traditional input/output functions
  90. */
  91. static inline u8 inb(unsigned long addr)
  92. {
  93. return readb(addr + PCI_IOBASE);
  94. }
  95. static inline u16 inw(unsigned long addr)
  96. {
  97. return readw(addr + PCI_IOBASE);
  98. }
  99. static inline u32 inl(unsigned long addr)
  100. {
  101. return readl(addr + PCI_IOBASE);
  102. }
  103. static inline void outb(u8 b, unsigned long addr)
  104. {
  105. writeb(b, addr + PCI_IOBASE);
  106. }
  107. static inline void outw(u16 b, unsigned long addr)
  108. {
  109. writew(b, addr + PCI_IOBASE);
  110. }
  111. static inline void outl(u32 b, unsigned long addr)
  112. {
  113. writel(b, addr + PCI_IOBASE);
  114. }
  115. #define inb_p(addr) inb(addr)
  116. #define inw_p(addr) inw(addr)
  117. #define inl_p(addr) inl(addr)
  118. #define outb_p(x, addr) outb((x), (addr))
  119. #define outw_p(x, addr) outw((x), (addr))
  120. #define outl_p(x, addr) outl((x), (addr))
  121. #ifndef insb
  122. static inline void insb(unsigned long addr, void *buffer, int count)
  123. {
  124. if (count) {
  125. u8 *buf = buffer;
  126. do {
  127. u8 x = inb(addr);
  128. *buf++ = x;
  129. } while (--count);
  130. }
  131. }
  132. #endif
  133. #ifndef insw
  134. static inline void insw(unsigned long addr, void *buffer, int count)
  135. {
  136. if (count) {
  137. u16 *buf = buffer;
  138. do {
  139. u16 x = inw(addr);
  140. *buf++ = x;
  141. } while (--count);
  142. }
  143. }
  144. #endif
  145. #ifndef insl
  146. static inline void insl(unsigned long addr, void *buffer, int count)
  147. {
  148. if (count) {
  149. u32 *buf = buffer;
  150. do {
  151. u32 x = inl(addr);
  152. *buf++ = x;
  153. } while (--count);
  154. }
  155. }
  156. #endif
  157. #ifndef outsb
  158. static inline void outsb(unsigned long addr, const void *buffer, int count)
  159. {
  160. if (count) {
  161. const u8 *buf = buffer;
  162. do {
  163. outb(*buf++, addr);
  164. } while (--count);
  165. }
  166. }
  167. #endif
  168. #ifndef outsw
  169. static inline void outsw(unsigned long addr, const void *buffer, int count)
  170. {
  171. if (count) {
  172. const u16 *buf = buffer;
  173. do {
  174. outw(*buf++, addr);
  175. } while (--count);
  176. }
  177. }
  178. #endif
  179. #ifndef outsl
  180. static inline void outsl(unsigned long addr, const void *buffer, int count)
  181. {
  182. if (count) {
  183. const u32 *buf = buffer;
  184. do {
  185. outl(*buf++, addr);
  186. } while (--count);
  187. }
  188. }
  189. #endif
  190. static inline void readsl(const void __iomem *addr, void *buf, int len)
  191. {
  192. insl(addr - PCI_IOBASE, buf, len);
  193. }
  194. static inline void readsw(const void __iomem *addr, void *buf, int len)
  195. {
  196. insw(addr - PCI_IOBASE, buf, len);
  197. }
  198. static inline void readsb(const void __iomem *addr, void *buf, int len)
  199. {
  200. insb(addr - PCI_IOBASE, buf, len);
  201. }
  202. static inline void writesl(const void __iomem *addr, const void *buf, int len)
  203. {
  204. outsl(addr - PCI_IOBASE, buf, len);
  205. }
  206. static inline void writesw(const void __iomem *addr, const void *buf, int len)
  207. {
  208. outsw(addr - PCI_IOBASE, buf, len);
  209. }
  210. static inline void writesb(const void __iomem *addr, const void *buf, int len)
  211. {
  212. outsb(addr - PCI_IOBASE, buf, len);
  213. }
  214. #ifndef CONFIG_GENERIC_IOMAP
  215. #define ioread8(addr) readb(addr)
  216. #define ioread16(addr) readw(addr)
  217. #define ioread16be(addr) be16_to_cpu(ioread16(addr))
  218. #define ioread32(addr) readl(addr)
  219. #define ioread32be(addr) be32_to_cpu(ioread32(addr))
  220. #define iowrite8(v, addr) writeb((v), (addr))
  221. #define iowrite16(v, addr) writew((v), (addr))
  222. #define iowrite16be(v, addr) iowrite16(be16_to_cpu(v), (addr))
  223. #define iowrite32(v, addr) writel((v), (addr))
  224. #define iowrite32be(v, addr) iowrite32(be32_to_cpu(v), (addr))
  225. #define ioread8_rep(p, dst, count) \
  226. insb((unsigned long) (p), (dst), (count))
  227. #define ioread16_rep(p, dst, count) \
  228. insw((unsigned long) (p), (dst), (count))
  229. #define ioread32_rep(p, dst, count) \
  230. insl((unsigned long) (p), (dst), (count))
  231. #define iowrite8_rep(p, src, count) \
  232. outsb((unsigned long) (p), (src), (count))
  233. #define iowrite16_rep(p, src, count) \
  234. outsw((unsigned long) (p), (src), (count))
  235. #define iowrite32_rep(p, src, count) \
  236. outsl((unsigned long) (p), (src), (count))
  237. #endif /* CONFIG_GENERIC_IOMAP */
  238. #ifndef IO_SPACE_LIMIT
  239. #define IO_SPACE_LIMIT 0xffff
  240. #endif
  241. #ifdef __KERNEL__
  242. #include <linux/vmalloc.h>
  243. #define __io_virt(x) ((void __force *) (x))
  244. #ifndef CONFIG_GENERIC_IOMAP
  245. struct pci_dev;
  246. static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
  247. {
  248. }
  249. #endif /* CONFIG_GENERIC_IOMAP */
  250. /*
  251. * Change virtual addresses to physical addresses and vv.
  252. * These are pretty trivial
  253. */
  254. static inline unsigned long virt_to_phys(volatile void *address)
  255. {
  256. return __pa((unsigned long)address);
  257. }
  258. static inline void *phys_to_virt(unsigned long address)
  259. {
  260. return __va(address);
  261. }
  262. /*
  263. * Change "struct page" to physical address.
  264. *
  265. * This implementation is for the no-MMU case only... if you have an MMU
  266. * you'll need to provide your own definitions.
  267. */
  268. #ifndef CONFIG_MMU
  269. static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
  270. {
  271. return (void __iomem*) (unsigned long)offset;
  272. }
  273. #define __ioremap(offset, size, flags) ioremap(offset, size)
  274. #ifndef ioremap_nocache
  275. #define ioremap_nocache ioremap
  276. #endif
  277. #ifndef ioremap_wc
  278. #define ioremap_wc ioremap_nocache
  279. #endif
  280. static inline void iounmap(void __iomem *addr)
  281. {
  282. }
  283. #endif /* CONFIG_MMU */
  284. #ifdef CONFIG_HAS_IOPORT
  285. #ifndef CONFIG_GENERIC_IOMAP
  286. static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
  287. {
  288. return (void __iomem *) port;
  289. }
  290. static inline void ioport_unmap(void __iomem *p)
  291. {
  292. }
  293. #else /* CONFIG_GENERIC_IOMAP */
  294. extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
  295. extern void ioport_unmap(void __iomem *p);
  296. #endif /* CONFIG_GENERIC_IOMAP */
  297. #endif /* CONFIG_HAS_IOPORT */
  298. #define xlate_dev_kmem_ptr(p) p
  299. #define xlate_dev_mem_ptr(p) __va(p)
  300. #ifndef virt_to_bus
  301. static inline unsigned long virt_to_bus(volatile void *address)
  302. {
  303. return ((unsigned long) address);
  304. }
  305. static inline void *bus_to_virt(unsigned long address)
  306. {
  307. return (void *) address;
  308. }
  309. #endif
  310. #define memset_io(a, b, c) memset(__io_virt(a), (b), (c))
  311. #define memcpy_fromio(a, b, c) memcpy((a), __io_virt(b), (c))
  312. #define memcpy_toio(a, b, c) memcpy(__io_virt(a), (b), (c))
  313. #endif /* __KERNEL__ */
  314. #endif /* __ASM_GENERIC_IO_H */