io.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* io.h: FRV I/O operations
  2. *
  3. * Copyright (C) 2004 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 License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * This gets interesting when talking to the PCI bus - the CPU is in big endian
  12. * mode, the PCI bus is little endian and the hardware in the middle can do
  13. * byte swapping
  14. */
  15. #ifndef _ASM_IO_H
  16. #define _ASM_IO_H
  17. #ifdef __KERNEL__
  18. #define ARCH_HAS_IOREMAP_WT
  19. #include <linux/types.h>
  20. #include <asm/virtconvert.h>
  21. #include <asm/string.h>
  22. #include <asm/mb-regs.h>
  23. #include <asm-generic/pci_iomap.h>
  24. #include <linux/delay.h>
  25. /*
  26. * swap functions are sometimes needed to interface little-endian hardware
  27. */
  28. static inline unsigned short _swapw(unsigned short v)
  29. {
  30. return ((v << 8) | (v >> 8));
  31. }
  32. static inline unsigned long _swapl(unsigned long v)
  33. {
  34. return ((v << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | (v >> 24));
  35. }
  36. //#define __iormb() asm volatile("membar")
  37. //#define __iowmb() asm volatile("membar")
  38. static inline u8 __raw_readb(const volatile void __iomem *addr)
  39. {
  40. return __builtin_read8((volatile void __iomem *)addr);
  41. }
  42. static inline u16 __raw_readw(const volatile void __iomem *addr)
  43. {
  44. return __builtin_read16((volatile void __iomem *)addr);
  45. }
  46. static inline u32 __raw_readl(const volatile void __iomem *addr)
  47. {
  48. return __builtin_read32((volatile void __iomem *)addr);
  49. }
  50. #define __raw_writeb(datum, addr) __builtin_write8(addr, datum)
  51. #define __raw_writew(datum, addr) __builtin_write16(addr, datum)
  52. #define __raw_writel(datum, addr) __builtin_write32(addr, datum)
  53. static inline void io_outsb(unsigned int addr, const void *buf, int len)
  54. {
  55. unsigned long __ioaddr = (unsigned long) addr;
  56. const uint8_t *bp = buf;
  57. while (len--)
  58. __builtin_write8((volatile void __iomem *) __ioaddr, *bp++);
  59. }
  60. static inline void io_outsw(unsigned int addr, const void *buf, int len)
  61. {
  62. unsigned long __ioaddr = (unsigned long) addr;
  63. const uint16_t *bp = buf;
  64. while (len--)
  65. __builtin_write16((volatile void __iomem *) __ioaddr, (*bp++));
  66. }
  67. extern void __outsl_ns(unsigned int addr, const void *buf, int len);
  68. extern void __outsl_sw(unsigned int addr, const void *buf, int len);
  69. static inline void __outsl(unsigned int addr, const void *buf, int len, int swap)
  70. {
  71. unsigned long __ioaddr = (unsigned long) addr;
  72. if (!swap)
  73. __outsl_ns(__ioaddr, buf, len);
  74. else
  75. __outsl_sw(__ioaddr, buf, len);
  76. }
  77. static inline void io_insb(unsigned long addr, void *buf, int len)
  78. {
  79. uint8_t *bp = buf;
  80. while (len--)
  81. *bp++ = __builtin_read8((volatile void __iomem *) addr);
  82. }
  83. static inline void io_insw(unsigned long addr, void *buf, int len)
  84. {
  85. uint16_t *bp = buf;
  86. while (len--)
  87. *bp++ = __builtin_read16((volatile void __iomem *) addr);
  88. }
  89. extern void __insl_ns(unsigned long addr, void *buf, int len);
  90. extern void __insl_sw(unsigned long addr, void *buf, int len);
  91. static inline void __insl(unsigned long addr, void *buf, int len, int swap)
  92. {
  93. if (!swap)
  94. __insl_ns(addr, buf, len);
  95. else
  96. __insl_sw(addr, buf, len);
  97. }
  98. #define mmiowb() mb()
  99. /*
  100. * make the short names macros so specific devices
  101. * can override them as required
  102. */
  103. static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
  104. {
  105. memset((void __force *) addr, val, count);
  106. }
  107. static inline void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
  108. {
  109. memcpy(dst, (void __force *) src, count);
  110. }
  111. static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
  112. {
  113. memcpy((void __force *) dst, src, count);
  114. }
  115. static inline uint8_t inb(unsigned long addr)
  116. {
  117. return __builtin_read8((void __iomem *)addr);
  118. }
  119. static inline uint16_t inw(unsigned long addr)
  120. {
  121. uint16_t ret = __builtin_read16((void __iomem *)addr);
  122. if (__is_PCI_IO(addr))
  123. ret = _swapw(ret);
  124. return ret;
  125. }
  126. static inline uint32_t inl(unsigned long addr)
  127. {
  128. uint32_t ret = __builtin_read32((void __iomem *)addr);
  129. if (__is_PCI_IO(addr))
  130. ret = _swapl(ret);
  131. return ret;
  132. }
  133. static inline void outb(uint8_t datum, unsigned long addr)
  134. {
  135. __builtin_write8((void __iomem *)addr, datum);
  136. }
  137. static inline void outw(uint16_t datum, unsigned long addr)
  138. {
  139. if (__is_PCI_IO(addr))
  140. datum = _swapw(datum);
  141. __builtin_write16((void __iomem *)addr, datum);
  142. }
  143. static inline void outl(uint32_t datum, unsigned long addr)
  144. {
  145. if (__is_PCI_IO(addr))
  146. datum = _swapl(datum);
  147. __builtin_write32((void __iomem *)addr, datum);
  148. }
  149. #define inb_p(addr) inb(addr)
  150. #define inw_p(addr) inw(addr)
  151. #define inl_p(addr) inl(addr)
  152. #define outb_p(x,addr) outb(x,addr)
  153. #define outw_p(x,addr) outw(x,addr)
  154. #define outl_p(x,addr) outl(x,addr)
  155. #define outsb(a,b,l) io_outsb(a,b,l)
  156. #define outsw(a,b,l) io_outsw(a,b,l)
  157. #define outsl(a,b,l) __outsl(a,b,l,0)
  158. #define insb(a,b,l) io_insb(a,b,l)
  159. #define insw(a,b,l) io_insw(a,b,l)
  160. #define insl(a,b,l) __insl(a,b,l,0)
  161. #define IO_SPACE_LIMIT 0xffffffff
  162. static inline uint8_t readb(const volatile void __iomem *addr)
  163. {
  164. return __builtin_read8((__force void volatile __iomem *) addr);
  165. }
  166. static inline uint16_t readw(const volatile void __iomem *addr)
  167. {
  168. uint16_t ret = __builtin_read16((__force void volatile __iomem *)addr);
  169. if (__is_PCI_MEM(addr))
  170. ret = _swapw(ret);
  171. return ret;
  172. }
  173. static inline uint32_t readl(const volatile void __iomem *addr)
  174. {
  175. uint32_t ret = __builtin_read32((__force void volatile __iomem *)addr);
  176. if (__is_PCI_MEM(addr))
  177. ret = _swapl(ret);
  178. return ret;
  179. }
  180. #define readb_relaxed readb
  181. #define readw_relaxed readw
  182. #define readl_relaxed readl
  183. static inline void writeb(uint8_t datum, volatile void __iomem *addr)
  184. {
  185. __builtin_write8(addr, datum);
  186. if (__is_PCI_MEM(addr))
  187. __flush_PCI_writes();
  188. }
  189. static inline void writew(uint16_t datum, volatile void __iomem *addr)
  190. {
  191. if (__is_PCI_MEM(addr))
  192. datum = _swapw(datum);
  193. __builtin_write16(addr, datum);
  194. if (__is_PCI_MEM(addr))
  195. __flush_PCI_writes();
  196. }
  197. static inline void writel(uint32_t datum, volatile void __iomem *addr)
  198. {
  199. if (__is_PCI_MEM(addr))
  200. datum = _swapl(datum);
  201. __builtin_write32(addr, datum);
  202. if (__is_PCI_MEM(addr))
  203. __flush_PCI_writes();
  204. }
  205. #define writeb_relaxed writeb
  206. #define writew_relaxed writew
  207. #define writel_relaxed writel
  208. /* Values for nocacheflag and cmode */
  209. #define IOMAP_FULL_CACHING 0
  210. #define IOMAP_NOCACHE_SER 1
  211. #define IOMAP_NOCACHE_NONSER 2
  212. #define IOMAP_WRITETHROUGH 3
  213. extern void __iomem *__ioremap(unsigned long physaddr, unsigned long size, int cacheflag);
  214. static inline void __iomem *ioremap(unsigned long physaddr, unsigned long size)
  215. {
  216. return __ioremap(physaddr, size, IOMAP_NOCACHE_SER);
  217. }
  218. static inline void __iomem *ioremap_nocache(unsigned long physaddr, unsigned long size)
  219. {
  220. return __ioremap(physaddr, size, IOMAP_NOCACHE_SER);
  221. }
  222. static inline void __iomem *ioremap_wt(unsigned long physaddr, unsigned long size)
  223. {
  224. return __ioremap(physaddr, size, IOMAP_WRITETHROUGH);
  225. }
  226. static inline void __iomem *ioremap_fullcache(unsigned long physaddr, unsigned long size)
  227. {
  228. return __ioremap(physaddr, size, IOMAP_FULL_CACHING);
  229. }
  230. #define ioremap_wc ioremap_nocache
  231. #define ioremap_uc ioremap_nocache
  232. extern void iounmap(void volatile __iomem *addr);
  233. static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
  234. {
  235. return (void __iomem *) port;
  236. }
  237. static inline void ioport_unmap(void __iomem *p)
  238. {
  239. }
  240. static inline void flush_write_buffers(void)
  241. {
  242. __asm__ __volatile__ ("membar" : : :"memory");
  243. }
  244. /*
  245. * do appropriate I/O accesses for token type
  246. */
  247. static inline unsigned int ioread8(void __iomem *p)
  248. {
  249. return __builtin_read8(p);
  250. }
  251. static inline unsigned int ioread16(void __iomem *p)
  252. {
  253. uint16_t ret = __builtin_read16(p);
  254. if (__is_PCI_addr(p))
  255. ret = _swapw(ret);
  256. return ret;
  257. }
  258. static inline unsigned int ioread32(void __iomem *p)
  259. {
  260. uint32_t ret = __builtin_read32(p);
  261. if (__is_PCI_addr(p))
  262. ret = _swapl(ret);
  263. return ret;
  264. }
  265. static inline void iowrite8(u8 val, void __iomem *p)
  266. {
  267. __builtin_write8(p, val);
  268. if (__is_PCI_MEM(p))
  269. __flush_PCI_writes();
  270. }
  271. static inline void iowrite16(u16 val, void __iomem *p)
  272. {
  273. if (__is_PCI_addr(p))
  274. val = _swapw(val);
  275. __builtin_write16(p, val);
  276. if (__is_PCI_MEM(p))
  277. __flush_PCI_writes();
  278. }
  279. static inline void iowrite32(u32 val, void __iomem *p)
  280. {
  281. if (__is_PCI_addr(p))
  282. val = _swapl(val);
  283. __builtin_write32(p, val);
  284. if (__is_PCI_MEM(p))
  285. __flush_PCI_writes();
  286. }
  287. #define ioread16be(addr) be16_to_cpu(ioread16(addr))
  288. #define ioread32be(addr) be32_to_cpu(ioread32(addr))
  289. #define iowrite16be(v, addr) iowrite16(cpu_to_be16(v), (addr))
  290. #define iowrite32be(v, addr) iowrite32(cpu_to_be32(v), (addr))
  291. static inline void ioread8_rep(void __iomem *p, void *dst, unsigned long count)
  292. {
  293. io_insb((unsigned long) p, dst, count);
  294. }
  295. static inline void ioread16_rep(void __iomem *p, void *dst, unsigned long count)
  296. {
  297. io_insw((unsigned long) p, dst, count);
  298. }
  299. static inline void ioread32_rep(void __iomem *p, void *dst, unsigned long count)
  300. {
  301. __insl_ns((unsigned long) p, dst, count);
  302. }
  303. static inline void iowrite8_rep(void __iomem *p, const void *src, unsigned long count)
  304. {
  305. io_outsb((unsigned long) p, src, count);
  306. }
  307. static inline void iowrite16_rep(void __iomem *p, const void *src, unsigned long count)
  308. {
  309. io_outsw((unsigned long) p, src, count);
  310. }
  311. static inline void iowrite32_rep(void __iomem *p, const void *src, unsigned long count)
  312. {
  313. __outsl_ns((unsigned long) p, src, count);
  314. }
  315. /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
  316. struct pci_dev;
  317. static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
  318. {
  319. }
  320. /*
  321. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  322. * access
  323. */
  324. #define xlate_dev_mem_ptr(p) __va(p)
  325. /*
  326. * Convert a virtual cached pointer to an uncached pointer
  327. */
  328. #define xlate_dev_kmem_ptr(p) p
  329. #endif /* __KERNEL__ */
  330. #endif /* _ASM_IO_H */