1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file asmCPU.c
98 /// @brief MIPS Inline Assembly Wrapper
99 /// @author MStar Semiconductor Inc.
100 /// @note Compile only with mips32 or mips32r2, but not mips16 or mips16e
101 ///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 //-------------------------------------------------------------------------------------------------
104 // Include Files
105 //-------------------------------------------------------------------------------------------------
106 #include "MsCommon.h"
107 #include "asmCPU.h"
108 #include "halCHIP.h"
109
110 //-------------------------------------------------------------------------------------------------
111 // Local Defines
112 //------------------------------------------------------------------------------------------------
113 #ifdef CONFIG_MBOOT
114 #define RIUBASE 0x1F000000
115 //#define PERIBASE 0x16000000
116 #else
117 #define RIUBASE 0xFD000000
118 //#define PERIBASE 0xFC000000
119 #endif
120
121 #if defined (__aarch64__)
122 #else
123 #define MSOS_HAL_RTC_DENOMINATOR 100
124 const MS_U32 msos_hal_rtc_period = 12000000 / MSOS_HAL_RTC_DENOMINATOR; //CNTCLK 12MHz -> ARM timer
125 #endif
126 //-------------------------------------------------------------------------------------------------
127 // Macros
128 //-------------------------------------------------------------------------------------------------
129
130 //-------------------------------------------------------------------------------------------------
131 // Global Variables
132 //-------------------------------------------------------------------------------------------------
133
134 //-------------------------------------------------------------------------------------------------
135 // Local Variables
136 //-------------------------------------------------------------------------------------------------
137
138 //-------------------------------------------------------------------------------------------------
139 // Local Function Prototypes
140 //-------------------------------------------------------------------------------------------------
141
142 //-------------------------------------------------------------------------------------------------
143 /// Flush EC's write FIFO
144 /// @return None
145 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Sync(void)146 void MAsm_CPU_Sync(void)
147 {
148 #if defined (__aarch64__)
149 asm __volatile__ (
150 "dsb sy;"
151 );
152 #else
153 asm __volatile__ (
154 "DSB"
155 );
156 #endif
157 }
158
159 //-------------------------------------------------------------------------------------------------
160 /// Nop
161 /// @return None
162 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Nop(void)163 inline void MAsm_CPU_Nop(void)
164 {
165 #if defined (__aarch64__)
166 asm __volatile__ (
167 "nop;"
168 );
169 #else
170 asm __volatile__ (
171 "nop;"
172 );
173 #endif
174 }
175
176 //-------------------------------------------------------------------------------------------------
177 /// SW Debug Breakpoint
178 /// @return None
179 //-------------------------------------------------------------------------------------------------
MAsm_CPU_SwDbgBp(void)180 void MAsm_CPU_SwDbgBp(void)
181 {
182 // @FIXME: Richard: which trap should I issue?
183 #if defined (__aarch64__)
184 asm __volatile__ (
185 "nop;"
186 );
187 #else
188 asm __volatile__ (
189 "BKPT 0x1234;"
190 );
191 #endif
192 }
193
194 //-------------------------------------------------------------------------------------------------
195 /// Enter CPU power saving mode
196 /// @return None
197 //-------------------------------------------------------------------------------------------------
MAsm_CPU_PowerDown(void)198 void MAsm_CPU_PowerDown(void)
199 {
200 //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
201 }
202
203 //-------------------------------------------------------------------------------------------------
204 /// Set Status BEV
205 /// @param bBEV \b IN: TRUE/FALSE: 1 bootstrap / 0 normal
206 /// @return FALSE : fail
207 //-------------------------------------------------------------------------------------------------
MAsm_CPU_StatusBEV(MS_BOOL bBEV)208 void MAsm_CPU_StatusBEV(MS_BOOL bBEV)
209 {
210 //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
211 }
212
213 //-------------------------------------------------------------------------------------------------
214 /// Jump to the specified PC
215 /// @param u32PC \b IN: PC
216 /// @return None
217 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Jump(MS_U32 u32PC)218 void MAsm_CPU_Jump(MS_U32 u32PC)
219 {
220 //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
221 }
222
223 //-------------------------------------------------------------------------------------------------
224 /// Get trail one
225 /// @param u32Flags \b IN: 32-bit flag
226 /// @return trail one position
227 //-------------------------------------------------------------------------------------------------
MAsm_CPU_GetTrailOne(MS_U32 u32Flags)228 MS_U8 MAsm_CPU_GetTrailOne(MS_U32 u32Flags)
229 {
230 MS_U32 i;
231 for (i = 0; i< 32; i++)
232 {
233 if (u32Flags & (1<< i))
234 {
235 return i;
236 }
237 }
238 return 32;
239 }
240
241 //-------------------------------------------------------------------------------------------------
242 /// Disable system timer interrupt
243 /// @param bEnable \b IN: TRUE: Enable timer interrupt, FALSE: Disable timer interrupt.
244 /// @return None
245 //-------------------------------------------------------------------------------------------------
MAsm_CPU_EnableTimerInterrupt(MS_BOOL bEnable)246 void MAsm_CPU_EnableTimerInterrupt(MS_BOOL bEnable)
247 {
248 //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
249 }
250
251 /*To do: you should use MsOS_DelayTask instead of this API*/
MAsm_CPU_DelayMs(MS_U32 msec)252 void MAsm_CPU_DelayMs(MS_U32 msec)
253 {
254 #define CPU_LOOP_MSEC(_msec) ((_msec) * ((ARM_CLOCK_FREQ / 2 / 1000) / 3)) // 3 cycles / loop
255
256 register MS_U32 loop = CPU_LOOP_MSEC(msec);
257 while(loop--);
258 }
259
260 #if defined (__aarch64__)
261 #else
_physical_count_read(MS_U64 * pvalue)262 static void _physical_count_read(MS_U64 *pvalue)
263 {
264 MS_U32 temp;
265 MS_U32 temp1;
266
267 asm volatile(
268 "mrrc p15, 0, r0, r1, c14\n"
269 "str r0, %0\n"
270 "str r1, %1\n"
271 :"=m"(temp), "=m"(temp1)::"r0","r1"
272 );
273
274 *pvalue = (MS_U64)temp1 << 32 | temp;
275 }
276
_hal_delay_us(MS_U32 usecs)277 static void _hal_delay_us(MS_U32 usecs)
278 {
279 MS_U64 val1, val2, delaycount;
280 MS_U64 usticks;
281
282 // Calculate the number of counter register ticks per microsecond.
283 usticks = msos_hal_rtc_period * MSOS_HAL_RTC_DENOMINATOR / 1000000;
284
285 // Make sure that the value is not zero. This will only happen if the
286 // CPU is running at < 2MHz.
287 if( usticks == 0 ) usticks = 1;
288
289 delaycount = usticks * usecs;
290
291 _physical_count_read(&val1);
292
293 while( usecs > 0 )
294 {
295 _physical_count_read(&val2);
296 if ((val2 - val1) > delaycount)
297 break;
298 }
299 }
300 #endif
301 //-------------------------------------------------------------------------------------------------
302 /// Pause for specifc duration by CPU dummy instruction
303 /// @param usec \b IN: microseconds ( usec < (12800000/CPU_CLK_MHZ) )
304 /// @return None
305 //-------------------------------------------------------------------------------------------------
306
MAsm_CPU_DelayUs(MS_U32 usec)307 void MAsm_CPU_DelayUs(MS_U32 usec)
308 {
309 #if defined (__aarch64__)
310 asm __volatile__ (
311 "nop;"
312 );
313 #else
314 #if (defined(CHIP_KANO) || defined(CHIP_K6) || defined(CHIP_K7U))
315 _hal_delay_us(usec);
316 #else
317 asm __volatile__ (
318 "mov r0, %0\n"
319 "wait: subs r0, r0, #1\n"
320 "bne wait\n"
321 :
322 :"r" (usec)
323 :"memory"
324 );
325 #endif
326 #endif
327 }
328
329 //-------------------------------------------------------------------------------------------------
330 /// Set EBASE
331 /// @param u32addr \b IN: MIPS Code Start Address
332 /// @return None
333 //-------------------------------------------------------------------------------------------------
MAsm_CPU_SetEBASE(MS_U32 u32addr)334 void MAsm_CPU_SetEBASE(MS_U32 u32addr)
335 {
336 //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
337 }
338
339 //
340 // System time
341 //
342
343
344 //-------------------------------------------------------------------------------------------------
345 /// Get current system time in timer ticks
346 /// @return system time in timer ticks
347 //-------------------------------------------------------------------------------------------------
348 MS_U32 gsystem_time_ms=0;
MAsm_GetSystemTime(void)349 MS_U32 MAsm_GetSystemTime (void)
350 {
351 return gsystem_time_ms;
352 }
353
MAsm_CPU_TimerInit(void)354 void MAsm_CPU_TimerInit(void)
355 {
356 #if defined (__aarch64__)
357 #else
358 asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (0));
359 asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (ARM_CLOCK_FREQ / 2 / 1000));
360 #endif
361 }
362
MAsm_CPU_TimerStart(void)363 void MAsm_CPU_TimerStart(void)
364 {
365 #if defined (__aarch64__)
366 #else
367 asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (1));
368 #endif
369 }
370
MAsm_CPU_TimerClean(void)371 void MAsm_CPU_TimerClean(void)
372 {
373 #if defined (__aarch64__)
374 #else
375 asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (0));
376 #endif
377 }
378