1 /*
2  * ===========================================================================================
3  *
4  *       Filename:  sunxi_hal_common.h
5  *
6  *    Description:  Add sunxi hal common header file, common defitions used by all hal.
7  *
8  *        Version:  Melis3.0
9  *         Create:  2019-11-14 11:38:34
10  *       Revision:  none
11  *       Compiler:  GCC:version 7.2.1 20170904 (release),ARM/embedded-7-branch revision 255204
12  *
13  *         Author:  caozilong@allwinnertech.com
14  *   Organization:  BU1-PSW
15  *  Last Modified:  2020-10-20 17:48:41
16  *
17  * ===========================================================================================
18  */
19 #ifndef SUNXI_HAL_COMMON_H
20 #define SUNXI_HAL_COMMON_H
21 
22 #include <barrier.h>
23 
24 #ifdef __cplusplus
25 extern "C"
26 {
27 #endif
28 
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <kconfig.h>
34 
35 #undef min
36 #undef max
37 #define min(a, b)  ((a) < (b) ? (a) : (b))
38 #define max(a,b)   ((a) < (b) ? (b) : (a))
39 
40 /* return value defines */
41 #define OK  (0)
42 #define FAIL    (-1)
43 
44 #define CACHELINE_LEN (64)
45 
46 typedef uint64_t  u64;
47 typedef uint32_t  u32;
48 typedef uint16_t  u16;
49 typedef uint8_t   u8;
50 typedef int64_t  s64;
51 typedef int32_t  s32;
52 typedef int16_t  s16;
53 typedef int8_t   s8;
54 #define HAL_ARG_UNUSED(NAME)   (void)(NAME)
55 
56 /* general function pointer defines */
57 typedef s32(*__pCBK_t) (void *p_arg);           /* call-back */
58 typedef s32(*__pISR_hdle_t) (int dummy, void *p_arg);   /* ISR */
59 typedef s32(*__pNotifier_t) (u32 message, u32 aux); /* notifer call-back */
60 typedef s32(*__pCPUExceptionHandler) (void);        /* cpu exception handler pointer */
61 typedef s32(*long_jump_fn) (void *arg);
62 typedef s32(*mem_long_jump_fn) (u32 arg);
63 
64 // Exception Dealt With.
65 #define BUG() do {                                                             \
66         printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
67         while(1);                                                              \
68     } while (0)
69 
70 #ifndef BUG_ON
71 #define BUG_ON(condition)   do { if (unlikely(condition)) BUG(); } while (0)
72 #define WARN_ON(condition)  ({                                          \
73         int __ret_warn_on = !!(condition);                              \
74         unlikely(__ret_warn_on);                                        \
75     })
76 #endif
77 
78 #ifndef WARN
79 #define WARN(condition, format...) ({                                   \
80         int __ret_warn_on = !!(condition);                              \
81         if(__ret_warn_on)                                               \
82             printf(format);                                             \
83         unlikely(__ret_warn_on);                                        \
84     })
85 #endif
86 
87 #ifdef CONFIG_KERNEL_FREERTOS
88 #define in_interrupt(...)       uGetInterruptNest()
89 #else
90 #define in_interrupt(...)       rt_interrupt_get_nest()
91 #endif
92 
93 #define in_nmi(...)             (0)
94 
95 #define hal_assert(ex)                                                  \
96     if (!(ex))                                                          \
97     {                                                                   \
98         printf("%s line %d, fatal error.\n", __func__, __LINE__);       \
99         while(1);                                                       \
100     }
101 
102 
103 // common register access operation.
104 #define hal_readb(reg)          (*(volatile uint8_t  *)(long)(reg))
105 #define hal_readw(reg)          (*(volatile uint16_t *)(reg))
106 #define hal_readl(reg)          (*(volatile uint32_t *)(reg))
107 #define hal_writeb(value,reg)   (*(volatile uint8_t  *)(long)(reg) = (value))
108 #define hal_writew(value,reg)   (*(volatile uint16_t *)(reg) = (value))
109 #define hal_writel(value,reg)   (*(volatile uint32_t *)(reg) = (value))
110 
111 // version combine.
112 #define SUNXI_HAL_VERSION_MAJOR_MINOR(major, minor)     (((major) << 8) | (minor))
113 
114 typedef struct sunxi_hal_version
115 {
116     // API version NO.
117     uint16_t api;
118 
119     // Driver version NO.
120     uint16_t drv;
121 } sunxi_hal_version_t;
122 
123 // General return code of hal driver.
124 #define SUNXI_HAL_OK                     0UL
125 // Unspecified error.
126 #define SUNXI_HAL_ERROR                 -1UL
127 // Hal is busy.
128 #define SUNXI_HAL_ERROR_BUSY            -2UL
129 // Timout occured.
130 #define SUNXI_HAL_ERROR_TIMEOUT         -3UL
131 // Operaion not supported.
132 #define SUNXI_HAL_ERROR_UNSUPOT         -4UL
133 // Parameter error.
134 #define SUNXI_HAL_ERROR_PARAERR         -5UL
135 // Start of driver specific errors.
136 #define SUNXI_HAL_ERROR_DRVSPECIFIC     -6UL
137 
138 // brief General power states
139 typedef enum sunxi_hal_power_state
140 {
141     ///< Power off: no operation possible
142     SUSNXI_HAL_POWER_OFF,
143     ///< Low Power mode: retain state, detect and signal wake-up events
144     SUSNXI_HAL_POWER_LOW,
145     ///< Power on: full operation at maximum performance
146     SUSNXI_HAL_POWER_FULL
147 } sunxi_hal_power_state_e;
148 
149 typedef int32_t (*poll_wakeup_func)(int32_t dev_id, short key);
150 
151 typedef struct _sunxi_hal_poll_ops
152 {
153     int32_t (* check_poll_state) (int32_t dev, short key);
154     int32_t (* hal_poll_wakeup) (int32_t dev, short key);
155     int32_t (* register_poll_wakeup) (poll_wakeup_func poll_wakeup);
156 } sunxi_hal_poll_ops;
157 
158 /* bitops */
159 extern int fls(int x);
160 
161 void dma_free_coherent(void *addr);
162 void *dma_alloc_coherent(size_t size);
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif  /*SUNXI_HAL_COMMON_H*/
169 
170