io.h 6.3 KB

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