io.h 6.4 KB

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