io.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_SH_IO_H
  3. #define __ASM_SH_IO_H
  4. /*
  5. * Convention:
  6. * read{b,w,l,q}/write{b,w,l,q} are for PCI,
  7. * while in{b,w,l}/out{b,w,l} are for ISA
  8. *
  9. * In addition we have 'pausing' versions: in{b,w,l}_p/out{b,w,l}_p
  10. * and 'string' versions: ins{b,w,l}/outs{b,w,l}
  11. *
  12. * While read{b,w,l,q} and write{b,w,l,q} contain memory barriers
  13. * automatically, there are also __raw versions, which do not.
  14. */
  15. #include <linux/errno.h>
  16. #include <asm/cache.h>
  17. #include <asm/addrspace.h>
  18. #include <asm/machvec.h>
  19. #include <asm/page.h>
  20. #include <linux/pgtable.h>
  21. #include <asm-generic/iomap.h>
  22. #define __IO_PREFIX generic
  23. #include <asm/io_generic.h>
  24. #include <asm-generic/pci_iomap.h>
  25. #include <mach/mangle-port.h>
  26. #define __raw_writeb(v,a) (__chk_io_ptr(a), *(volatile u8 __force *)(a) = (v))
  27. #define __raw_writew(v,a) (__chk_io_ptr(a), *(volatile u16 __force *)(a) = (v))
  28. #define __raw_writel(v,a) (__chk_io_ptr(a), *(volatile u32 __force *)(a) = (v))
  29. #define __raw_writeq(v,a) (__chk_io_ptr(a), *(volatile u64 __force *)(a) = (v))
  30. #define __raw_readb(a) (__chk_io_ptr(a), *(volatile u8 __force *)(a))
  31. #define __raw_readw(a) (__chk_io_ptr(a), *(volatile u16 __force *)(a))
  32. #define __raw_readl(a) (__chk_io_ptr(a), *(volatile u32 __force *)(a))
  33. #define __raw_readq(a) (__chk_io_ptr(a), *(volatile u64 __force *)(a))
  34. #define readb_relaxed(c) ({ u8 __v = ioswabb(__raw_readb(c)); __v; })
  35. #define readw_relaxed(c) ({ u16 __v = ioswabw(__raw_readw(c)); __v; })
  36. #define readl_relaxed(c) ({ u32 __v = ioswabl(__raw_readl(c)); __v; })
  37. #define readq_relaxed(c) ({ u64 __v = ioswabq(__raw_readq(c)); __v; })
  38. #define writeb_relaxed(v,c) ((void)__raw_writeb((__force u8)ioswabb(v),c))
  39. #define writew_relaxed(v,c) ((void)__raw_writew((__force u16)ioswabw(v),c))
  40. #define writel_relaxed(v,c) ((void)__raw_writel((__force u32)ioswabl(v),c))
  41. #define writeq_relaxed(v,c) ((void)__raw_writeq((__force u64)ioswabq(v),c))
  42. #define readb(a) ({ u8 r_ = readb_relaxed(a); rmb(); r_; })
  43. #define readw(a) ({ u16 r_ = readw_relaxed(a); rmb(); r_; })
  44. #define readl(a) ({ u32 r_ = readl_relaxed(a); rmb(); r_; })
  45. #define readq(a) ({ u64 r_ = readq_relaxed(a); rmb(); r_; })
  46. #define writeb(v,a) ({ wmb(); writeb_relaxed((v),(a)); })
  47. #define writew(v,a) ({ wmb(); writew_relaxed((v),(a)); })
  48. #define writel(v,a) ({ wmb(); writel_relaxed((v),(a)); })
  49. #define writeq(v,a) ({ wmb(); writeq_relaxed((v),(a)); })
  50. #define readsb(p,d,l) __raw_readsb(p,d,l)
  51. #define readsw(p,d,l) __raw_readsw(p,d,l)
  52. #define readsl(p,d,l) __raw_readsl(p,d,l)
  53. #define writesb(p,d,l) __raw_writesb(p,d,l)
  54. #define writesw(p,d,l) __raw_writesw(p,d,l)
  55. #define writesl(p,d,l) __raw_writesl(p,d,l)
  56. #define __BUILD_UNCACHED_IO(bwlq, type) \
  57. static inline type read##bwlq##_uncached(unsigned long addr) \
  58. { \
  59. type ret; \
  60. jump_to_uncached(); \
  61. ret = __raw_read##bwlq(addr); \
  62. back_to_cached(); \
  63. return ret; \
  64. } \
  65. \
  66. static inline void write##bwlq##_uncached(type v, unsigned long addr) \
  67. { \
  68. jump_to_uncached(); \
  69. __raw_write##bwlq(v, addr); \
  70. back_to_cached(); \
  71. }
  72. __BUILD_UNCACHED_IO(b, u8)
  73. __BUILD_UNCACHED_IO(w, u16)
  74. __BUILD_UNCACHED_IO(l, u32)
  75. __BUILD_UNCACHED_IO(q, u64)
  76. #define __BUILD_MEMORY_STRING(pfx, bwlq, type) \
  77. \
  78. static inline void \
  79. pfx##writes##bwlq(volatile void __iomem *mem, const void *addr, \
  80. unsigned int count) \
  81. { \
  82. const volatile type *__addr = addr; \
  83. \
  84. while (count--) { \
  85. __raw_write##bwlq(*__addr, mem); \
  86. __addr++; \
  87. } \
  88. } \
  89. \
  90. static inline void pfx##reads##bwlq(volatile void __iomem *mem, \
  91. void *addr, unsigned int count) \
  92. { \
  93. volatile type *__addr = addr; \
  94. \
  95. while (count--) { \
  96. *__addr = __raw_read##bwlq(mem); \
  97. __addr++; \
  98. } \
  99. }
  100. __BUILD_MEMORY_STRING(__raw_, b, u8)
  101. __BUILD_MEMORY_STRING(__raw_, w, u16)
  102. void __raw_writesl(void __iomem *addr, const void *data, int longlen);
  103. void __raw_readsl(const void __iomem *addr, void *data, int longlen);
  104. __BUILD_MEMORY_STRING(__raw_, q, u64)
  105. #ifdef CONFIG_HAS_IOPORT_MAP
  106. /*
  107. * Slowdown I/O port space accesses for antique hardware.
  108. */
  109. #undef CONF_SLOWDOWN_IO
  110. /*
  111. * On SuperH I/O ports are memory mapped, so we access them using normal
  112. * load/store instructions. sh_io_port_base is the virtual address to
  113. * which all ports are being mapped.
  114. */
  115. extern unsigned long sh_io_port_base;
  116. static inline void __set_io_port_base(unsigned long pbase)
  117. {
  118. *(unsigned long *)&sh_io_port_base = pbase;
  119. barrier();
  120. }
  121. #ifdef CONFIG_GENERIC_IOMAP
  122. #define __ioport_map ioport_map
  123. #else
  124. extern void __iomem *__ioport_map(unsigned long addr, unsigned int size);
  125. #endif
  126. #ifdef CONF_SLOWDOWN_IO
  127. #define SLOW_DOWN_IO __raw_readw(sh_io_port_base)
  128. #else
  129. #define SLOW_DOWN_IO
  130. #endif
  131. #define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p, slow) \
  132. \
  133. static inline void pfx##out##bwlq##p(type val, unsigned long port) \
  134. { \
  135. volatile type *__addr; \
  136. \
  137. __addr = __ioport_map(port, sizeof(type)); \
  138. *__addr = val; \
  139. slow; \
  140. } \
  141. \
  142. static inline type pfx##in##bwlq##p(unsigned long port) \
  143. { \
  144. volatile type *__addr; \
  145. type __val; \
  146. \
  147. __addr = __ioport_map(port, sizeof(type)); \
  148. __val = *__addr; \
  149. slow; \
  150. \
  151. return __val; \
  152. }
  153. #define __BUILD_IOPORT_PFX(bus, bwlq, type) \
  154. __BUILD_IOPORT_SINGLE(bus, bwlq, type, ,) \
  155. __BUILD_IOPORT_SINGLE(bus, bwlq, type, _p, SLOW_DOWN_IO)
  156. #define BUILDIO_IOPORT(bwlq, type) \
  157. __BUILD_IOPORT_PFX(, bwlq, type)
  158. BUILDIO_IOPORT(b, u8)
  159. BUILDIO_IOPORT(w, u16)
  160. BUILDIO_IOPORT(l, u32)
  161. BUILDIO_IOPORT(q, u64)
  162. #define __BUILD_IOPORT_STRING(bwlq, type) \
  163. \
  164. static inline void outs##bwlq(unsigned long port, const void *addr, \
  165. unsigned int count) \
  166. { \
  167. const volatile type *__addr = addr; \
  168. \
  169. while (count--) { \
  170. out##bwlq(*__addr, port); \
  171. __addr++; \
  172. } \
  173. } \
  174. \
  175. static inline void ins##bwlq(unsigned long port, void *addr, \
  176. unsigned int count) \
  177. { \
  178. volatile type *__addr = addr; \
  179. \
  180. while (count--) { \
  181. *__addr = in##bwlq(port); \
  182. __addr++; \
  183. } \
  184. }
  185. __BUILD_IOPORT_STRING(b, u8)
  186. __BUILD_IOPORT_STRING(w, u16)
  187. __BUILD_IOPORT_STRING(l, u32)
  188. __BUILD_IOPORT_STRING(q, u64)
  189. #else /* !CONFIG_HAS_IOPORT_MAP */
  190. #include <asm/io_noioport.h>
  191. #endif
  192. #define IO_SPACE_LIMIT 0xffffffff
  193. /* We really want to try and get these to memcpy etc */
  194. void memcpy_fromio(void *, const volatile void __iomem *, unsigned long);
  195. void memcpy_toio(volatile void __iomem *, const void *, unsigned long);
  196. void memset_io(volatile void __iomem *, int, unsigned long);
  197. /* Quad-word real-mode I/O, don't ask.. */
  198. unsigned long long peek_real_address_q(unsigned long long addr);
  199. unsigned long long poke_real_address_q(unsigned long long addr,
  200. unsigned long long val);
  201. #if !defined(CONFIG_MMU)
  202. #define virt_to_phys(address) ((unsigned long)(address))
  203. #define phys_to_virt(address) ((void *)(address))
  204. #else
  205. #define virt_to_phys(address) (__pa(address))
  206. #define phys_to_virt(address) (__va(address))
  207. #endif
  208. #ifdef CONFIG_MMU
  209. void iounmap(void __iomem *addr);
  210. void __iomem *__ioremap_caller(phys_addr_t offset, unsigned long size,
  211. pgprot_t prot, void *caller);
  212. static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
  213. {
  214. return __ioremap_caller(offset, size, PAGE_KERNEL_NOCACHE,
  215. __builtin_return_address(0));
  216. }
  217. static inline void __iomem *
  218. ioremap_cache(phys_addr_t offset, unsigned long size)
  219. {
  220. return __ioremap_caller(offset, size, PAGE_KERNEL,
  221. __builtin_return_address(0));
  222. }
  223. #define ioremap_cache ioremap_cache
  224. #ifdef CONFIG_HAVE_IOREMAP_PROT
  225. static inline void __iomem *ioremap_prot(phys_addr_t offset, unsigned long size,
  226. unsigned long flags)
  227. {
  228. return __ioremap_caller(offset, size, __pgprot(flags),
  229. __builtin_return_address(0));
  230. }
  231. #endif /* CONFIG_HAVE_IOREMAP_PROT */
  232. #else /* CONFIG_MMU */
  233. static inline void __iomem *ioremap(phys_addr_t offset, size_t size)
  234. {
  235. return (void __iomem *)(unsigned long)offset;
  236. }
  237. static inline void iounmap(volatile void __iomem *addr) { }
  238. #endif /* CONFIG_MMU */
  239. #define ioremap_uc ioremap
  240. /*
  241. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  242. * access
  243. */
  244. #define xlate_dev_mem_ptr(p) __va(p)
  245. #define ARCH_HAS_VALID_PHYS_ADDR_RANGE
  246. int valid_phys_addr_range(phys_addr_t addr, size_t size);
  247. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
  248. #endif /* __ASM_SH_IO_H */