io.h 9.6 KB

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