io.h 9.4 KB

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