io.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. */
  5. #ifndef _ASM_ARC_IO_H
  6. #define _ASM_ARC_IO_H
  7. #include <linux/types.h>
  8. #include <asm/byteorder.h>
  9. #include <asm/page.h>
  10. #include <asm/unaligned.h>
  11. #ifdef CONFIG_ISA_ARCV2
  12. #include <asm/barrier.h>
  13. #define __iormb() rmb()
  14. #define __iowmb() wmb()
  15. #else
  16. #define __iormb() do { } while (0)
  17. #define __iowmb() do { } while (0)
  18. #endif
  19. extern void __iomem *ioremap(phys_addr_t paddr, unsigned long size);
  20. extern void __iomem *ioremap_prot(phys_addr_t paddr, unsigned long size,
  21. unsigned long flags);
  22. static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
  23. {
  24. return (void __iomem *)port;
  25. }
  26. static inline void ioport_unmap(void __iomem *addr)
  27. {
  28. }
  29. extern void iounmap(const volatile void __iomem *addr);
  30. /*
  31. * io{read,write}{16,32}be() macros
  32. */
  33. #define ioread16be(p) ({ u16 __v = be16_to_cpu((__force __be16)__raw_readw(p)); __iormb(); __v; })
  34. #define ioread32be(p) ({ u32 __v = be32_to_cpu((__force __be32)__raw_readl(p)); __iormb(); __v; })
  35. #define iowrite16be(v,p) ({ __iowmb(); __raw_writew((__force u16)cpu_to_be16(v), p); })
  36. #define iowrite32be(v,p) ({ __iowmb(); __raw_writel((__force u32)cpu_to_be32(v), p); })
  37. /* Change struct page to physical address */
  38. #define page_to_phys(page) (page_to_pfn(page) << PAGE_SHIFT)
  39. #define __raw_readb __raw_readb
  40. static inline u8 __raw_readb(const volatile void __iomem *addr)
  41. {
  42. u8 b;
  43. __asm__ __volatile__(
  44. " ldb%U1 %0, %1 \n"
  45. : "=r" (b)
  46. : "m" (*(volatile u8 __force *)addr)
  47. : "memory");
  48. return b;
  49. }
  50. #define __raw_readw __raw_readw
  51. static inline u16 __raw_readw(const volatile void __iomem *addr)
  52. {
  53. u16 s;
  54. __asm__ __volatile__(
  55. " ldw%U1 %0, %1 \n"
  56. : "=r" (s)
  57. : "m" (*(volatile u16 __force *)addr)
  58. : "memory");
  59. return s;
  60. }
  61. #define __raw_readl __raw_readl
  62. static inline u32 __raw_readl(const volatile void __iomem *addr)
  63. {
  64. u32 w;
  65. __asm__ __volatile__(
  66. " ld%U1 %0, %1 \n"
  67. : "=r" (w)
  68. : "m" (*(volatile u32 __force *)addr)
  69. : "memory");
  70. return w;
  71. }
  72. /*
  73. * {read,write}s{b,w,l}() repeatedly access the same IO address in
  74. * native endianness in 8-, 16-, 32-bit chunks {into,from} memory,
  75. * @count times
  76. */
  77. #define __raw_readsx(t,f) \
  78. static inline void __raw_reads##f(const volatile void __iomem *addr, \
  79. void *ptr, unsigned int count) \
  80. { \
  81. bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \
  82. u##t *buf = ptr; \
  83. \
  84. if (!count) \
  85. return; \
  86. \
  87. /* Some ARC CPU's don't support unaligned accesses */ \
  88. if (is_aligned) { \
  89. do { \
  90. u##t x = __raw_read##f(addr); \
  91. *buf++ = x; \
  92. } while (--count); \
  93. } else { \
  94. do { \
  95. u##t x = __raw_read##f(addr); \
  96. put_unaligned(x, buf++); \
  97. } while (--count); \
  98. } \
  99. }
  100. #define __raw_readsb __raw_readsb
  101. __raw_readsx(8, b)
  102. #define __raw_readsw __raw_readsw
  103. __raw_readsx(16, w)
  104. #define __raw_readsl __raw_readsl
  105. __raw_readsx(32, l)
  106. #define __raw_writeb __raw_writeb
  107. static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
  108. {
  109. __asm__ __volatile__(
  110. " stb%U1 %0, %1 \n"
  111. :
  112. : "r" (b), "m" (*(volatile u8 __force *)addr)
  113. : "memory");
  114. }
  115. #define __raw_writew __raw_writew
  116. static inline void __raw_writew(u16 s, volatile void __iomem *addr)
  117. {
  118. __asm__ __volatile__(
  119. " stw%U1 %0, %1 \n"
  120. :
  121. : "r" (s), "m" (*(volatile u16 __force *)addr)
  122. : "memory");
  123. }
  124. #define __raw_writel __raw_writel
  125. static inline void __raw_writel(u32 w, volatile void __iomem *addr)
  126. {
  127. __asm__ __volatile__(
  128. " st%U1 %0, %1 \n"
  129. :
  130. : "r" (w), "m" (*(volatile u32 __force *)addr)
  131. : "memory");
  132. }
  133. #define __raw_writesx(t,f) \
  134. static inline void __raw_writes##f(volatile void __iomem *addr, \
  135. const void *ptr, unsigned int count) \
  136. { \
  137. bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \
  138. const u##t *buf = ptr; \
  139. \
  140. if (!count) \
  141. return; \
  142. \
  143. /* Some ARC CPU's don't support unaligned accesses */ \
  144. if (is_aligned) { \
  145. do { \
  146. __raw_write##f(*buf++, addr); \
  147. } while (--count); \
  148. } else { \
  149. do { \
  150. __raw_write##f(get_unaligned(buf++), addr); \
  151. } while (--count); \
  152. } \
  153. }
  154. #define __raw_writesb __raw_writesb
  155. __raw_writesx(8, b)
  156. #define __raw_writesw __raw_writesw
  157. __raw_writesx(16, w)
  158. #define __raw_writesl __raw_writesl
  159. __raw_writesx(32, l)
  160. /*
  161. * MMIO can also get buffered/optimized in micro-arch, so barriers needed
  162. * Based on ARM model for the typical use case
  163. *
  164. * <ST [DMA buffer]>
  165. * <writel MMIO "go" reg>
  166. * or:
  167. * <readl MMIO "status" reg>
  168. * <LD [DMA buffer]>
  169. *
  170. * http://lkml.kernel.org/r/[email protected]
  171. */
  172. #define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; })
  173. #define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; })
  174. #define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; })
  175. #define readsb(p,d,l) ({ __raw_readsb(p,d,l); __iormb(); })
  176. #define readsw(p,d,l) ({ __raw_readsw(p,d,l); __iormb(); })
  177. #define readsl(p,d,l) ({ __raw_readsl(p,d,l); __iormb(); })
  178. #define writeb(v,c) ({ __iowmb(); writeb_relaxed(v,c); })
  179. #define writew(v,c) ({ __iowmb(); writew_relaxed(v,c); })
  180. #define writel(v,c) ({ __iowmb(); writel_relaxed(v,c); })
  181. #define writesb(p,d,l) ({ __iowmb(); __raw_writesb(p,d,l); })
  182. #define writesw(p,d,l) ({ __iowmb(); __raw_writesw(p,d,l); })
  183. #define writesl(p,d,l) ({ __iowmb(); __raw_writesl(p,d,l); })
  184. /*
  185. * Relaxed API for drivers which can handle barrier ordering themselves
  186. *
  187. * Also these are defined to perform little endian accesses.
  188. * To provide the typical device register semantics of fixed endian,
  189. * swap the byte order for Big Endian
  190. *
  191. * http://lkml.kernel.org/r/[email protected]
  192. */
  193. #define readb_relaxed(c) __raw_readb(c)
  194. #define readw_relaxed(c) ({ u16 __r = le16_to_cpu((__force __le16) \
  195. __raw_readw(c)); __r; })
  196. #define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32) \
  197. __raw_readl(c)); __r; })
  198. #define writeb_relaxed(v,c) __raw_writeb(v,c)
  199. #define writew_relaxed(v,c) __raw_writew((__force u16) cpu_to_le16(v),c)
  200. #define writel_relaxed(v,c) __raw_writel((__force u32) cpu_to_le32(v),c)
  201. #include <asm-generic/io.h>
  202. #endif /* _ASM_ARC_IO_H */