comdef.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /**
  3. ******************************************************************************
  4. * @file comdef.h
  5. * @author StarFive Technology
  6. * @version V1.0
  7. * @date 06/25/2020
  8. * @brief
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * COPYRIGHT 2020 Shanghai StarFive Technology Co., Ltd.
  20. */
  21. #ifndef _COMDEF_H_
  22. #define _COMDEF_H_
  23. typedef unsigned char UINT8;
  24. typedef unsigned char UCHAR;
  25. typedef unsigned short UINT16;
  26. typedef unsigned int UINT32;
  27. typedef unsigned short int WCHAR;
  28. typedef unsigned short int UNICODE;
  29. typedef unsigned long UINT64;
  30. typedef int INT64;
  31. typedef int INT32;
  32. typedef short int INT16;
  33. typedef char INT8;
  34. typedef char CHAR;
  35. typedef unsigned short int bool;
  36. typedef unsigned short int STATUS_T;
  37. typedef signed char s8;
  38. typedef unsigned char u8;
  39. typedef signed short s16;
  40. typedef unsigned short u16;
  41. typedef signed int s32;
  42. typedef unsigned int u32;
  43. typedef unsigned long u64;
  44. //typedef int ssize_t;
  45. typedef unsigned char uint8_t;
  46. typedef unsigned short uint16_t;
  47. typedef unsigned int uint32_t;
  48. typedef unsigned long uintptr_t;
  49. typedef unsigned long uint64_t;
  50. typedef signed long s64;
  51. typedef unsigned long size_t;
  52. #define NULL 0
  53. #define FALSE 0
  54. #define TRUE 1
  55. #define typeof __typeof__
  56. #define ARCH_DMA_MINALIGN 32
  57. #define BIT_0 0x00000001
  58. #define BIT_1 0x00000002
  59. #define BIT_2 0x00000004
  60. #define BIT_3 0x00000008
  61. #define BIT_4 0x00000010
  62. #define BIT_5 0x00000020
  63. #define BIT_6 0x00000040
  64. #define BIT_7 0x00000080
  65. #define BIT_8 0x00000100
  66. #define BIT_9 0x00000200
  67. #define BIT_10 0x00000400
  68. #define BIT_11 0x00000800
  69. #define BIT_12 0x00001000
  70. #define BIT_13 0x00002000
  71. #define BIT_14 0x00004000
  72. #define BIT_15 0x00008000
  73. #define BIT_16 0x00010000
  74. #define BIT_17 0x00020000
  75. #define BIT_18 0x00040000
  76. #define BIT_19 0x00080000
  77. #define BIT_20 0x00100000
  78. #define BIT_21 0x00200000
  79. #define BIT_22 0x00400000
  80. #define BIT_23 0x00800000
  81. #define BIT_24 0x01000000
  82. #define BIT_25 0x02000000
  83. #define BIT_26 0x04000000
  84. #define BIT_27 0x08000000
  85. #define BIT_28 0x10000000
  86. #define BIT_29 0x20000000
  87. #define BIT_30 0x40000000
  88. #define BIT_31 0x80000000
  89. #define RSP_SUCCESS 0
  90. #define RSP_GENERAL_FAIL 1
  91. #ifndef BIT
  92. #define BIT(x) (1UL << (x))
  93. #endif
  94. #ifndef writeq
  95. static inline void writeq(u64 val, volatile void *addr)
  96. {
  97. asm volatile("sd %0, 0(%1)" : : "r" (val), "r" (addr));
  98. }
  99. #endif
  100. #ifndef readq
  101. static inline u64 readq(volatile void *addr)
  102. {
  103. u64 val;
  104. asm volatile("ld %0, 0(%1)" : "=r" (val) : "r" (addr));
  105. return val;
  106. }
  107. #endif
  108. #ifndef writel
  109. static inline void writel(u32 val, volatile void *addr)
  110. {
  111. asm volatile("sw %0, 0(%1)" : : "r" (val), "r" (addr));
  112. }
  113. #endif
  114. #ifndef readl
  115. static inline u32 readl(volatile void *addr)
  116. {
  117. u32 val;
  118. asm volatile("lw %0, 0(%1)" : "=r" (val) : "r" (addr));
  119. return val;
  120. }
  121. #endif
  122. #ifndef writew
  123. static inline void writew(u16 val, volatile void *addr)
  124. {
  125. asm volatile("sh %0, 0(%1)" : : "r" (val), "r" (addr));
  126. }
  127. #endif
  128. #ifndef readw
  129. static inline u16 readw(const volatile void *addr)
  130. {
  131. u16 val;
  132. asm volatile("lh %0, 0(%1)" : "=r" (val) : "r" (addr));
  133. return val;
  134. }
  135. #endif
  136. #define inpb(port) (*((volatile UINT8 *) (port)))
  137. #define inph(port) (*((volatile UINT16*) (port)))
  138. #define inpw(port) (*((volatile UINT32*) (port)))
  139. #define outpb(port, val) (*((volatile UINT8 *) (port)) = ((UINT8 ) (val)))
  140. #define outph(port, val) (*((volatile UINT16*) (port)) = ((UINT16) (val)))
  141. #define outpw(port, val) (*((volatile UINT32*) (port)) = ((UINT32) (val)))
  142. #define MA_INB( io ) (UINT8 ) inpb( io )
  143. #define MA_INH( io ) (UINT16) inph( io )
  144. #define MA_INW( io ) (UINT32) inpw( io )
  145. #define MA_INBM( io, mask ) ( inpb( io ) & (mask) )
  146. #define MA_INHM( io, mask ) ( inph( io ) & (mask) )
  147. #define MA_INWM( io, mask ) ( inpw( io ) & (mask) )
  148. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  149. /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
  150. #define roundup(x, y) ( \
  151. { \
  152. const typeof(y) __y = y; \
  153. (((x) + (__y - 1)) / __y) * __y; \
  154. } \
  155. )
  156. #define LOG2(x) (((x & 0xaaaaaaaa) ? 1 : 0) + ((x & 0xcccccccc) ? 2 : 0) + \
  157. ((x & 0xf0f0f0f0) ? 4 : 0) + ((x & 0xff00ff00) ? 8 : 0) + \
  158. ((x & 0xffff0000) ? 16 : 0))
  159. #define ___swab32(x) \
  160. ((u32)( \
  161. (((u32)(x) & (u32)0x000000ffUL) << 24) | \
  162. (((u32)(x) & (u32)0x0000ff00UL) << 8) | \
  163. (((u32)(x) & (u32)0x00ff0000UL) >> 8) | \
  164. (((u32)(x) & (u32)0xff000000UL) >> 24) ))
  165. #define __be32_to_cpu(x) ___swab32((u32)(x))
  166. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  167. #define MA_OUTB( io, val )\
  168. {\
  169. outpb( io, val );\
  170. }
  171. #define MA_OUTH( io, val )\
  172. {\
  173. outph( io, val );\
  174. }
  175. #define MA_OUTW( io, val )\
  176. {\
  177. outpw( io, val );\
  178. }
  179. #define MA_OUTBM( io, mask, val ) \
  180. {\
  181. UINT8 temp;\
  182. (temp) =(((MA_INW(io) & (UINT8)(~(mask))) | ((UINT8)((val) & (mask)))));\
  183. outpb( io, temp );\
  184. }
  185. #define MA_OUTHM( io, mask, val ) \
  186. {\
  187. UINT16 temp;\
  188. (temp) =(((MA_INH(io) & (UINT16)(~(mask))) |((UINT16)((val) & (mask)))));\
  189. outph( io, temp );\
  190. }
  191. #define MA_OUTWM( io, mask, val ) \
  192. {\
  193. UINT32 temp;\
  194. (temp) =(((MA_INW(io) & (UINT32)(~(mask))) |((UINT32)((val) & (mask)))));\
  195. outpw( io, temp );\
  196. }
  197. #define min(x,y) ((x)<(y)?(x):(y))
  198. #define min3(x, y, z) min(min(x, y), z)
  199. #define max(x, y) ((x)>(y)?(x):(y))
  200. #define ROUND(a,b) (((a) + (b) - 1) & ~((b) - 1))
  201. #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1)
  202. #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  203. #define PAD_COUNT(s, pad) (((s) - 1) / (pad) + 1)
  204. #define PAD_SIZE(s, pad) (PAD_COUNT(s, pad) * pad)
  205. #define ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, pad) \
  206. char __##name[ROUND(PAD_SIZE((size) * sizeof(type), pad), align) \
  207. + (align - 1)]; \
  208. \
  209. type *name = (type *)ALIGN((uintptr_t)__##name, align)
  210. #define ALLOC_ALIGN_BUFFER(type, name, size, align) \
  211. ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, 1)
  212. #define ALLOC_CACHE_ALIGN_BUFFER_PAD(type, name, size, pad) \
  213. ALLOC_ALIGN_BUFFER_PAD(type, name, size, ARCH_DMA_MINALIGN, pad)
  214. #define ALLOC_CACHE_ALIGN_BUFFER(type, name, size) \
  215. ALLOC_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN)
  216. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  217. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
  218. {
  219. *remainder = dividend % divisor;
  220. return dividend / divisor;
  221. }
  222. #ifndef false
  223. #define false (0)
  224. #endif
  225. #ifndef true
  226. #define true (1)
  227. #endif
  228. #endif