io.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/pgtable.h>
  20. #include <asm-generic/iomap.h>
  21. #ifdef __KERNEL__
  22. #define __IO_PREFIX generic
  23. #include <asm/io_generic.h>
  24. #include <asm/io_trapped.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. #ifdef CONFIG_SUPERH32
  103. void __raw_writesl(void __iomem *addr, const void *data, int longlen);
  104. void __raw_readsl(const void __iomem *addr, void *data, int longlen);
  105. #else
  106. __BUILD_MEMORY_STRING(__raw_, l, u32)
  107. #endif
  108. __BUILD_MEMORY_STRING(__raw_, q, u64)
  109. #ifdef CONFIG_HAS_IOPORT_MAP
  110. /*
  111. * Slowdown I/O port space accesses for antique hardware.
  112. */
  113. #undef CONF_SLOWDOWN_IO
  114. /*
  115. * On SuperH I/O ports are memory mapped, so we access them using normal
  116. * load/store instructions. sh_io_port_base is the virtual address to
  117. * which all ports are being mapped.
  118. */
  119. extern unsigned long sh_io_port_base;
  120. static inline void __set_io_port_base(unsigned long pbase)
  121. {
  122. *(unsigned long *)&sh_io_port_base = pbase;
  123. barrier();
  124. }
  125. #ifdef CONFIG_GENERIC_IOMAP
  126. #define __ioport_map ioport_map
  127. #else
  128. extern void __iomem *__ioport_map(unsigned long addr, unsigned int size);
  129. #endif
  130. #ifdef CONF_SLOWDOWN_IO
  131. #define SLOW_DOWN_IO __raw_readw(sh_io_port_base)
  132. #else
  133. #define SLOW_DOWN_IO
  134. #endif
  135. #define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p, slow) \
  136. \
  137. static inline void pfx##out##bwlq##p(type val, unsigned long port) \
  138. { \
  139. volatile type *__addr; \
  140. \
  141. __addr = __ioport_map(port, sizeof(type)); \
  142. *__addr = val; \
  143. slow; \
  144. } \
  145. \
  146. static inline type pfx##in##bwlq##p(unsigned long port) \
  147. { \
  148. volatile type *__addr; \
  149. type __val; \
  150. \
  151. __addr = __ioport_map(port, sizeof(type)); \
  152. __val = *__addr; \
  153. slow; \
  154. \
  155. return __val; \
  156. }
  157. #define __BUILD_IOPORT_PFX(bus, bwlq, type) \
  158. __BUILD_IOPORT_SINGLE(bus, bwlq, type, ,) \
  159. __BUILD_IOPORT_SINGLE(bus, bwlq, type, _p, SLOW_DOWN_IO)
  160. #define BUILDIO_IOPORT(bwlq, type) \
  161. __BUILD_IOPORT_PFX(, bwlq, type)
  162. BUILDIO_IOPORT(b, u8)
  163. BUILDIO_IOPORT(w, u16)
  164. BUILDIO_IOPORT(l, u32)
  165. BUILDIO_IOPORT(q, u64)
  166. #define __BUILD_IOPORT_STRING(bwlq, type) \
  167. \
  168. static inline void outs##bwlq(unsigned long port, const void *addr, \
  169. unsigned int count) \
  170. { \
  171. const volatile type *__addr = addr; \
  172. \
  173. while (count--) { \
  174. out##bwlq(*__addr, port); \
  175. __addr++; \
  176. } \
  177. } \
  178. \
  179. static inline void ins##bwlq(unsigned long port, void *addr, \
  180. unsigned int count) \
  181. { \
  182. volatile type *__addr = addr; \
  183. \
  184. while (count--) { \
  185. *__addr = in##bwlq(port); \
  186. __addr++; \
  187. } \
  188. }
  189. __BUILD_IOPORT_STRING(b, u8)
  190. __BUILD_IOPORT_STRING(w, u16)
  191. __BUILD_IOPORT_STRING(l, u32)
  192. __BUILD_IOPORT_STRING(q, u64)
  193. #else /* !CONFIG_HAS_IOPORT_MAP */
  194. #include <asm/io_noioport.h>
  195. #endif
  196. #define IO_SPACE_LIMIT 0xffffffff
  197. /* synco on SH-4A, otherwise a nop */
  198. #define mmiowb() wmb()
  199. /* We really want to try and get these to memcpy etc */
  200. void memcpy_fromio(void *, const volatile void __iomem *, unsigned long);
  201. void memcpy_toio(volatile void __iomem *, const void *, unsigned long);
  202. void memset_io(volatile void __iomem *, int, unsigned long);
  203. /* Quad-word real-mode I/O, don't ask.. */
  204. unsigned long long peek_real_address_q(unsigned long long addr);
  205. unsigned long long poke_real_address_q(unsigned long long addr,
  206. unsigned long long val);
  207. #if !defined(CONFIG_MMU)
  208. #define virt_to_phys(address) ((unsigned long)(address))
  209. #define phys_to_virt(address) ((void *)(address))
  210. #else
  211. #define virt_to_phys(address) (__pa(address))
  212. #define phys_to_virt(address) (__va(address))
  213. #endif
  214. /*
  215. * On 32-bit SH, we traditionally have the whole physical address space
  216. * mapped at all times (as MIPS does), so "ioremap()" and "iounmap()" do
  217. * not need to do anything but place the address in the proper segment.
  218. * This is true for P1 and P2 addresses, as well as some P3 ones.
  219. * However, most of the P3 addresses and newer cores using extended
  220. * addressing need to map through page tables, so the ioremap()
  221. * implementation becomes a bit more complicated.
  222. *
  223. * See arch/sh/mm/ioremap.c for additional notes on this.
  224. *
  225. * We cheat a bit and always return uncachable areas until we've fixed
  226. * the drivers to handle caching properly.
  227. *
  228. * On the SH-5 the concept of segmentation in the 1:1 PXSEG sense simply
  229. * doesn't exist, so everything must go through page tables.
  230. */
  231. #ifdef CONFIG_MMU
  232. void __iomem *__ioremap_caller(phys_addr_t offset, unsigned long size,
  233. pgprot_t prot, void *caller);
  234. void __iounmap(void __iomem *addr);
  235. static inline void __iomem *
  236. __ioremap(phys_addr_t offset, unsigned long size, pgprot_t prot)
  237. {
  238. return __ioremap_caller(offset, size, prot, __builtin_return_address(0));
  239. }
  240. static inline void __iomem *
  241. __ioremap_29bit(phys_addr_t offset, unsigned long size, pgprot_t prot)
  242. {
  243. #ifdef CONFIG_29BIT
  244. phys_addr_t last_addr = offset + size - 1;
  245. /*
  246. * For P1 and P2 space this is trivial, as everything is already
  247. * mapped. Uncached access for P1 addresses are done through P2.
  248. * In the P3 case or for addresses outside of the 29-bit space,
  249. * mapping must be done by the PMB or by using page tables.
  250. */
  251. if (likely(PXSEG(offset) < P3SEG && PXSEG(last_addr) < P3SEG)) {
  252. u64 flags = pgprot_val(prot);
  253. /*
  254. * Anything using the legacy PTEA space attributes needs
  255. * to be kicked down to page table mappings.
  256. */
  257. if (unlikely(flags & _PAGE_PCC_MASK))
  258. return NULL;
  259. if (unlikely(flags & _PAGE_CACHABLE))
  260. return (void __iomem *)P1SEGADDR(offset);
  261. return (void __iomem *)P2SEGADDR(offset);
  262. }
  263. /* P4 above the store queues are always mapped. */
  264. if (unlikely(offset >= P3_ADDR_MAX))
  265. return (void __iomem *)P4SEGADDR(offset);
  266. #endif
  267. return NULL;
  268. }
  269. static inline void __iomem *
  270. __ioremap_mode(phys_addr_t offset, unsigned long size, pgprot_t prot)
  271. {
  272. void __iomem *ret;
  273. ret = __ioremap_trapped(offset, size);
  274. if (ret)
  275. return ret;
  276. ret = __ioremap_29bit(offset, size, prot);
  277. if (ret)
  278. return ret;
  279. return __ioremap(offset, size, prot);
  280. }
  281. #else
  282. #define __ioremap(offset, size, prot) ((void __iomem *)(offset))
  283. #define __ioremap_mode(offset, size, prot) ((void __iomem *)(offset))
  284. #define __iounmap(addr) do { } while (0)
  285. #endif /* CONFIG_MMU */
  286. static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
  287. {
  288. return __ioremap_mode(offset, size, PAGE_KERNEL_NOCACHE);
  289. }
  290. static inline void __iomem *
  291. ioremap_cache(phys_addr_t offset, unsigned long size)
  292. {
  293. return __ioremap_mode(offset, size, PAGE_KERNEL);
  294. }
  295. #define ioremap_cache ioremap_cache
  296. #ifdef CONFIG_HAVE_IOREMAP_PROT
  297. static inline void __iomem *
  298. ioremap_prot(phys_addr_t offset, unsigned long size, unsigned long flags)
  299. {
  300. return __ioremap_mode(offset, size, __pgprot(flags));
  301. }
  302. #endif
  303. #ifdef CONFIG_IOREMAP_FIXED
  304. extern void __iomem *ioremap_fixed(phys_addr_t, unsigned long, pgprot_t);
  305. extern int iounmap_fixed(void __iomem *);
  306. extern void ioremap_fixed_init(void);
  307. #else
  308. static inline void __iomem *
  309. ioremap_fixed(phys_addr_t phys_addr, unsigned long size, pgprot_t prot)
  310. {
  311. BUG();
  312. return NULL;
  313. }
  314. static inline void ioremap_fixed_init(void) { }
  315. static inline int iounmap_fixed(void __iomem *addr) { return -EINVAL; }
  316. #endif
  317. #define ioremap_nocache ioremap
  318. #define ioremap_uc ioremap
  319. static inline void iounmap(void __iomem *addr)
  320. {
  321. __iounmap(addr);
  322. }
  323. /*
  324. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  325. * access
  326. */
  327. #define xlate_dev_mem_ptr(p) __va(p)
  328. /*
  329. * Convert a virtual cached pointer to an uncached pointer
  330. */
  331. #define xlate_dev_kmem_ptr(p) p
  332. #define ARCH_HAS_VALID_PHYS_ADDR_RANGE
  333. int valid_phys_addr_range(phys_addr_t addr, size_t size);
  334. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
  335. #endif /* __KERNEL__ */
  336. #endif /* __ASM_SH_IO_H */