io.h 8.3 KB

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