1 /*
2  * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _DRIVER_CHIP_HAL_OS_H_
31 #define _DRIVER_CHIP_HAL_OS_H_
32 
33 #include "os.h"
34 #include <string.h>
35 #include <stdlib.h>
36 #include <hal_atomic.h>
37 #ifndef CONFIG_KERNEL_FREERTOS
38 #include <ktimer.h>
39 #include <hal_cfg.h>
40 #else
41 #include <hal_cache.h>
42 #endif
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /* IRQ disable/enable */
49 #define HAL_DisableIRQ()    arch_irq_disable()
50 #define HAL_EnableIRQ()     arch_irq_enable()
51 
52 /* Check if IRQ is disabled */
53 #define HAL_IsIRQDisabled() __get_PRIMASK()
54 
55 /* Check if in ISR context or not */
56 #define HAL_IsISRContext()  __get_IPSR()
57 
58 extern hal_spinlock_t sdmmc_lock;
59 /* Critical Sections */
60 //#define HAL_EnterCriticalSection()      arch_irq_save()
61 //#define HAL_ExitCriticalSection(flags)  arch_irq_restore(flags)
62 #define HAL_EnterCriticalSection()  ({hal_spin_lock_irqsave(&sdmmc_lock);})
63 #define HAL_ExitCriticalSection(f)  ({hal_spin_unlock_irqrestore(&sdmmc_lock, f);})
64 #define HAL_ATMOTIC_SET(a,v)            ({int flags = HAL_EnterCriticalSection();\
65                                         a = v;HAL_ExitCriticalSection(flags);})
66 #define HAL_ATMOTIC_READ(a)         ({int flags=0;int v=0; flags = HAL_EnterCriticalSection();v=a;HAL_ExitCriticalSection(flags);v;})
67 
68 #define HAL_FlushDcacheRegion(s,len)            (hal_dcache_clean_invalidate(s,len))
69 #define HAL_InvalidDcacheRegion(s,len)      (hal_dcache_invalidate(s, len))
70 
71 #if 0
72 #define HAL_GetTimeMs()             (aos_now_ms())
73 #define HAL_GetTimeUs()             (aos_now()/1000)
74 //#define HAL_GetTimeUs()           (OS_GetTime()*1000)
75 #define HAL_GetTimeNs()             (aos_now())
76 #else
77 #if 0
78 #define HAL_GetTimeMs()             ((rt_tick_get()*1000)/CONFIG_HZ)
79 #define HAL_GetTimeUs()             ((rt_tick_get()*1000*1000)/CONFIG_HZ)
80 #define HAL_GetTimeNs()             ((rt_tick_get()*1000*1000)/CONFIG_HZ)
81 #endif
82 #define HAL_GetTimeMs()             ({\
83                             struct timeval tv;\
84                             gettimeofday(&tv, NULL);\
85                             (tv.tv_usec + tv.tv_sec * 1000000ll)/1000;\
86                         })
87 #define HAL_GetTimeUs()             ({\
88                             struct timeval tv;\
89                             gettimeofday(&tv, NULL);\
90                             (tv.tv_usec + tv.tv_sec * 1000000ll);\
91                         })
92 #define HAL_GetTimeNs()             ({\
93                             struct timespec64  sdmmc_timeval;\
94                             do_gettimeofday(&sdmmc_timeval);\
95                             (sdmmc_timeval.tv_sec*1000ll*1000*1000ll + sdmmc_timeval.tv_nsec);\
96                         })
97 #endif
98 
99 
100 
101 /* Semaphore */
102 typedef OS_Semaphore_t HAL_Semaphore;
103 
104 #define HAL_SemaphoreInit(sem, initCount, maxCount) \
105     (OS_SemaphoreCreate(sem, initCount, maxCount) == OS_OK ? HAL_OK : HAL_ERROR)
106 
107 #define HAL_SemaphoreInitBinary(sem) \
108     (OS_SemaphoreCreateBinary(sem) == OS_OK ? HAL_OK : HAL_ERROR)
109 
110 #define HAL_SemaphoreDeinit(sem) \
111     (OS_SemaphoreDelete(sem) == OS_OK ? HAL_OK : HAL_ERROR)
112 
113 #define HAL_SemaphoreWait(sem, msec) \
114     (OS_SemaphoreWait(sem, msec) == OS_OK ? HAL_OK : HAL_ERROR)
115 
116 #define HAL_SemaphoreRelease(sem) \
117     (OS_SemaphoreRelease(sem) == OS_OK ? HAL_OK : HAL_ERROR)
118 
119 #define HAL_SemaphoreIsValid(sem) \
120     OS_SemaphoreIsValid(sem)
121 
122 #define HAL_SemaphoreSetInvalid(sem) \
123     OS_SemaphoreSetInvalid(sem)
124 
125 /* Mutex */
126 typedef OS_Mutex_t HAL_Mutex;
127 
128 #define HAL_MutexInit(mtx) \
129     (OS_RecursiveMutexCreate(mtx) == OS_OK ? HAL_OK : HAL_ERROR)
130 
131 #define HAL_MutexDeinit(mtx) \
132     (OS_RecursiveMutexDelete(mtx) == OS_OK ? HAL_OK : HAL_ERROR)
133 
134 #define HAL_MutexLock(mtx, msec) \
135     (OS_RecursiveMutexLock(mtx, msec) == OS_OK ? HAL_OK : HAL_ERROR)
136 
137 #define HAL_MutexUnlock(mtx) \
138     (OS_RecursiveMutexUnlock(mtx) == OS_OK ? HAL_OK : HAL_ERROR)
139 
140 /* Thread */
141 #define HAL_ThreadSuspendScheduler()    OS_ThreadSuspendScheduler()
142 #define HAL_ThreadResumeScheduler()     OS_ThreadResumeScheduler()
143 #define HAL_ThreadIsSchedulerRunning()  OS_ThreadIsSchedulerRunning()
144 #define HAL_ThreadEnd(s)                (HAL_ATMOTIC_SET(s,0))
145 #define HAL_ThreadStop(s)               (HAL_ATMOTIC_SET(s,1))
146 #define HAL_Thread_Should_Stop(s)       (HAL_ATMOTIC_READ(s))
147 #define HAL_ThreadDelete(w)         (OS_ThreadDelete(NULL))
148 
149 
150 /* Keep system alive, eg. feed watchdog */
151 #define HAL_Alive()             HAL_WDG_Feed()
152 
153 /* Time */
154 #define HAL_Ticks()             OS_GetTicks()
155 #define HAL_MSleep(msec)        OS_MSleep(msec)
156 #define HAL_UDelay(us)          OS_Udelay(us)
157 
158 #define HAL_SecsToTicks(sec)    OS_SecsToTicks(sec)
159 #define HAL_MSecsToTicks(msec)  OS_MSecsToTicks(msec)
160 #define HAL_TicksToMSecs(t)     OS_TicksToMSecs(t)
161 #define HAL_TicksToSecs(t)      OS_TicksToSecs(t)
162 
163 #define HAL_TimeAfter(a, b)         OS_TimeAfter(a, b)
164 #define HAL_TimeBefore(a, b)        OS_TimeBefore(a, b)
165 #define HAL_TimeAfterEqual(a, b)    OS_TimeAfterEqual(a, b)
166 #define HAL_TimeBeforeEqual(a, b)   OS_TimeBeforeEqual(a, b)
167 
168 #define HAL_ALIGN(x, a) __ALIGN_KERNEL((x), (a))
169 #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a)-1), (a))
170 #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a)-1)
171 #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
172 
173 #define OS_CACHE_ALIGN_BYTES  (64)
174 
malloc_align_buf(size_t size)175 static inline void *malloc_align_buf(size_t size)
176 {
177     void *fake_ptr = NULL;
178     void *malloc_ptr = NULL;
179 
180     /*malloc_ptr = krhino_mm_alloc(size + OS_CACHE_ALIGN_BYTES);*/
181     malloc_ptr = hal_malloc(size + OS_CACHE_ALIGN_BYTES);
182     if (HAL_PT_TO_U(malloc_ptr) & 0x3) {
183         printf("error: krhino_mm_alloc not align to 4 byte\r\n");
184     }
185     fake_ptr = (void *)(HAL_PT_TO_U(malloc_ptr + OS_CACHE_ALIGN_BYTES) & (~(OS_CACHE_ALIGN_BYTES -1)));
186     *(uint32_t *)((uint32_t *)fake_ptr - 1) = HAL_PT_TO_U(malloc_ptr);
187 
188     return fake_ptr;
189 }
190 
free_align_buf(void * addr)191 static inline void free_align_buf(void *addr)
192 {
193     void *malloc_ptr = NULL;
194     if (!addr)
195         return;
196     malloc_ptr = (void *)HAL_PT_TO_U(*(uint32_t *)((uint32_t *)addr - 1));
197     /*krhino_mm_free(malloc_ptr);*/
198     hal_free(malloc_ptr);
199 }
200 
201 
202 /* Memory */
203 #define HAL_Malloc(l)           malloc(l)
204 #define HAL_Free(p)             free(p)
205 #define HAL_Memcpy(d, s, l)     memcpy(d, s, l)
206 #define HAL_Memset(d, c, l)     memset(d, c, l)
207 #define HAL_Memcmp(a, b, l)     memcmp(a, b, l)
208 #define HAL_Memmove(d, s, n)    memmove(d, s, n)
209 #define HAL_MallocAlign(l)      (malloc_align_buf(l))
210 #define HAL_FreeAlign(p)        (free_align_buf(p))
211 #ifndef CONFIG_KERNEL_FREERTOS
212 #define HAL_SetPin
213 #endif
214 
215 
216 #ifdef __cplusplus
217 }
218 #endif
219 
220 #endif /* _DRIVER_CHIP_HAL_OS_H_ */
221