io.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef _ASM_GENERIC_IO_H_
  2. #define _ASM_GENERIC_IO_H_
  3. /*
  4. * asm-generic/io.h
  5. * adapted from the Linux kernel's include/asm-generic/io.h
  6. * and arch/arm/include/asm/io.h
  7. *
  8. * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2.
  11. */
  12. #include "libcflat.h"
  13. #include "asm/page.h"
  14. #include "asm/barrier.h"
  15. #ifndef __raw_readb
  16. static inline u8 __raw_readb(const volatile void *addr)
  17. {
  18. return *(const volatile u8 *)addr;
  19. }
  20. #endif
  21. #ifndef __raw_readw
  22. static inline u16 __raw_readw(const volatile void *addr)
  23. {
  24. return *(const volatile u16 *)addr;
  25. }
  26. #endif
  27. #ifndef __raw_readl
  28. static inline u32 __raw_readl(const volatile void *addr)
  29. {
  30. return *(const volatile u32 *)addr;
  31. }
  32. #endif
  33. #ifndef __raw_readq
  34. static inline u64 __raw_readq(const volatile void *addr)
  35. {
  36. assert(sizeof(unsigned long) == sizeof(u64));
  37. return *(const volatile u64 *)addr;
  38. }
  39. #endif
  40. #ifndef __raw_writeb
  41. static inline void __raw_writeb(u8 b, volatile void *addr)
  42. {
  43. *(volatile u8 *)addr = b;
  44. }
  45. #endif
  46. #ifndef __raw_writew
  47. static inline void __raw_writew(u16 b, volatile void *addr)
  48. {
  49. *(volatile u16 *)addr = b;
  50. }
  51. #endif
  52. #ifndef __raw_writel
  53. static inline void __raw_writel(u32 b, volatile void *addr)
  54. {
  55. *(volatile u32 *)addr = b;
  56. }
  57. #endif
  58. #ifndef __raw_writeq
  59. static inline void __raw_writeq(u64 b, volatile void *addr)
  60. {
  61. assert(sizeof(unsigned long) == sizeof(u64));
  62. *(volatile u64 *)addr = b;
  63. }
  64. #endif
  65. #ifndef __bswap16
  66. static inline u16 __bswap16(u16 x)
  67. {
  68. return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
  69. }
  70. #endif
  71. #ifndef __bswap32
  72. static inline u32 __bswap32(u32 x)
  73. {
  74. return ((x & 0xff000000) >> 24) | ((x & 0x00ff0000) >> 8) |
  75. ((x & 0x0000ff00) << 8) | ((x & 0x000000ff) << 24);
  76. }
  77. #endif
  78. #ifndef __bswap64
  79. static inline u64 __bswap64(u64 x)
  80. {
  81. return ((x & 0x00000000000000ffULL) << 56) |
  82. ((x & 0x000000000000ff00ULL) << 40) |
  83. ((x & 0x0000000000ff0000ULL) << 24) |
  84. ((x & 0x00000000ff000000ULL) << 8) |
  85. ((x & 0x000000ff00000000ULL) >> 8) |
  86. ((x & 0x0000ff0000000000ULL) >> 24) |
  87. ((x & 0x00ff000000000000ULL) >> 40) |
  88. ((x & 0xff00000000000000ULL) >> 56);
  89. }
  90. #endif
  91. #ifndef __cpu_is_be
  92. #define __cpu_is_be() (0)
  93. #endif
  94. #define le16_to_cpu(x) \
  95. ({ u16 __r = __cpu_is_be() ? __bswap16(x) : ((u16)x); __r; })
  96. #define cpu_to_le16 le16_to_cpu
  97. #define le32_to_cpu(x) \
  98. ({ u32 __r = __cpu_is_be() ? __bswap32(x) : ((u32)x); __r; })
  99. #define cpu_to_le32 le32_to_cpu
  100. #define le64_to_cpu(x) \
  101. ({ u64 __r = __cpu_is_be() ? __bswap64(x) : ((u64)x); __r; })
  102. #define cpu_to_le64 le64_to_cpu
  103. #define be16_to_cpu(x) \
  104. ({ u16 __r = !__cpu_is_be() ? __bswap16(x) : ((u16)x); __r; })
  105. #define cpu_to_be16 be16_to_cpu
  106. #define be32_to_cpu(x) \
  107. ({ u32 __r = !__cpu_is_be() ? __bswap32(x) : ((u32)x); __r; })
  108. #define cpu_to_be32 be32_to_cpu
  109. #define be64_to_cpu(x) \
  110. ({ u64 __r = !__cpu_is_be() ? __bswap64(x) : ((u64)x); __r; })
  111. #define cpu_to_be64 be64_to_cpu
  112. #define readb(addr) \
  113. ({ u8 __r = __raw_readb(addr); rmb(); __r; })
  114. #define readw(addr) \
  115. ({ u16 __r = le16_to_cpu(__raw_readw(addr)); rmb(); __r; })
  116. #define readl(addr) \
  117. ({ u32 __r = le32_to_cpu(__raw_readl(addr)); rmb(); __r; })
  118. #define readq(addr) \
  119. ({ u64 __r = le64_to_cpu(__raw_readq(addr)); rmb(); __r; })
  120. #define writeb(b, addr) \
  121. ({ wmb(); __raw_writeb(b, addr); })
  122. #define writew(b, addr) \
  123. ({ wmb(); __raw_writew(cpu_to_le16(b), addr); })
  124. #define writel(b, addr) \
  125. ({ wmb(); __raw_writel(cpu_to_le32(b), addr); })
  126. #define writeq(b, addr) \
  127. ({ wmb(); __raw_writeq(cpu_to_le64(b), addr); })
  128. #ifndef inb
  129. static inline uint8_t inb(unsigned long port)
  130. {
  131. return readb((const volatile void __iomem *)port);
  132. }
  133. #endif
  134. #ifndef inw
  135. static inline uint16_t inw(unsigned long port)
  136. {
  137. return readw((const volatile void __iomem *)port);
  138. }
  139. #endif
  140. #ifndef inl
  141. static inline uint32_t inl(unsigned long port)
  142. {
  143. return readl((const volatile void __iomem *)port);
  144. }
  145. #endif
  146. #ifndef outb
  147. static inline void outb(uint8_t value, unsigned long port)
  148. {
  149. writeb(value, (volatile void __iomem *)port);
  150. }
  151. #endif
  152. #ifndef outw
  153. static inline void outw(uint16_t value, unsigned long port)
  154. {
  155. writew(value, (volatile void __iomem *)port);
  156. }
  157. #endif
  158. #ifndef outl
  159. static inline void outl(uint32_t value, unsigned long port)
  160. {
  161. writel(value, (volatile void __iomem *)port);
  162. }
  163. #endif
  164. #ifndef ioremap
  165. static inline void __iomem *ioremap(phys_addr_t phys_addr, size_t size __unused)
  166. {
  167. assert(sizeof(long) == 8 || !(phys_addr >> 32));
  168. return (void __iomem *)(unsigned long)phys_addr;
  169. }
  170. #endif
  171. #ifndef virt_to_phys
  172. static inline unsigned long virt_to_phys(volatile void *address)
  173. {
  174. return __pa((unsigned long)address);
  175. }
  176. #endif
  177. #ifndef phys_to_virt
  178. static inline void *phys_to_virt(unsigned long address)
  179. {
  180. return __va(address);
  181. }
  182. #endif
  183. #endif /* _ASM_GENERIC_IO_H_ */