io.h 9.5 KB

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