xref: /utopia/UTPA2-700.0.x/modules/msos/msos/arm9/asmCPU.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
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 //-------------------------------------------------------------------------------------------------
122 // Macros
123 //-------------------------------------------------------------------------------------------------
124 
125 //-------------------------------------------------------------------------------------------------
126 // Global Variables
127 //-------------------------------------------------------------------------------------------------
128 
129 //-------------------------------------------------------------------------------------------------
130 // Local Variables
131 //-------------------------------------------------------------------------------------------------
132 
133 //-------------------------------------------------------------------------------------------------
134 // Local Function Prototypes
135 //-------------------------------------------------------------------------------------------------
136 
137 //-------------------------------------------------------------------------------------------------
138 /// Flush EC's write FIFO
139 /// @return None
140 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Sync(void)141 void MAsm_CPU_Sync(void)
142 {
143     asm __volatile__ (
144         "DSB"
145         );
146 }
147 
148 //-------------------------------------------------------------------------------------------------
149 /// Nop
150 /// @return None
151 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Nop(void)152 inline void MAsm_CPU_Nop(void)
153 {
154     asm __volatile__ (
155         "nop;"
156         );
157 }
158 
159 //-------------------------------------------------------------------------------------------------
160 /// SW Debug Breakpoint
161 /// @return None
162 //-------------------------------------------------------------------------------------------------
MAsm_CPU_SwDbgBp(void)163 void MAsm_CPU_SwDbgBp(void)
164 {
165     // @FIXME: Richard: which trap should I issue?
166     asm __volatile__ (
167         "BKPT 0x1234;"
168         );
169 }
170 
171 //-------------------------------------------------------------------------------------------------
172 /// Enter CPU power saving mode
173 /// @return None
174 //-------------------------------------------------------------------------------------------------
MAsm_CPU_PowerDown(void)175 void MAsm_CPU_PowerDown(void)
176 {
177     //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
178 }
179 
180 //-------------------------------------------------------------------------------------------------
181 /// Set Status BEV
182 /// @param  bBEV \b IN: TRUE/FALSE: 1 bootstrap / 0 normal
183 /// @return FALSE : fail
184 //-------------------------------------------------------------------------------------------------
MAsm_CPU_StatusBEV(MS_BOOL bBEV)185 void MAsm_CPU_StatusBEV(MS_BOOL bBEV)
186 {
187     //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
188 }
189 
190 //-------------------------------------------------------------------------------------------------
191 /// Jump to the specified PC
192 /// @param  u32PC \b IN: PC
193 /// @return None
194 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Jump(MS_U32 u32PC)195 void MAsm_CPU_Jump(MS_U32 u32PC)
196 {
197     //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
198 }
199 
200 //-------------------------------------------------------------------------------------------------
201 /// Get trail one
202 /// @param  u32Flags \b IN: 32-bit flag
203 /// @return trail one position
204 //-------------------------------------------------------------------------------------------------
MAsm_CPU_GetTrailOne(MS_U32 u32Flags)205 MS_U8 MAsm_CPU_GetTrailOne(MS_U32 u32Flags)
206 {
207     MS_U32 i;
208     for (i = 0; i< 32; i++)
209     {
210         if (u32Flags & (1<< i))
211         {
212             return i;
213         }
214     }
215     return 32;
216 }
217 
218 //-------------------------------------------------------------------------------------------------
219 /// Disable system timer interrupt
220 /// @param  bEnable \b IN: TRUE: Enable timer interrupt, FALSE: Disable timer interrupt.
221 /// @return None
222 //-------------------------------------------------------------------------------------------------
MAsm_CPU_EnableTimerInterrupt(MS_BOOL bEnable)223 void MAsm_CPU_EnableTimerInterrupt(MS_BOOL bEnable)
224 {
225     //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
226 }
227 
228 /*To do: you should use MsOS_DelayTask instead of this API*/
MAsm_CPU_DelayMs(MS_U32 msec)229 void MAsm_CPU_DelayMs(MS_U32 msec)
230 {
231     #define CPU_LOOP_MSEC(_msec)        ((_msec)*(900000000/1000/3)) // 3 cycles / loop
232 
233     register MS_U32 loop = CPU_LOOP_MSEC(msec);
234     while(loop--);
235 }
236 
237 //-------------------------------------------------------------------------------------------------
238 /// Pause for specifc duration by CPU dummy instruction
239 /// @param  usec \b IN: microseconds ( usec < (12800000/CPU_CLK_MHZ) )
240 /// @return None
241 //-------------------------------------------------------------------------------------------------
MAsm_CPU_DelayUs(MS_U32 usec)242 void MAsm_CPU_DelayUs(MS_U32 usec)
243 {
244 #if 0
245     #define CPU_LOOP_USEC(_usec)        ((_usec)*(MIPS_CLOCK_FREQ/1000000/3)) // 3 cycles / loop
246 
247     register MS_U32 loop = CPU_LOOP_USEC(usec);
248     while(loop--);
249 #endif
250 
251     asm __volatile__ (
252         "mov  r0, %0\n"
253         "wait: subs    r0, r0, #1\n"
254         "bne     wait\n"
255         :
256         :"r" (usec)
257         :"memory"
258     );
259 }
260 
261 //-------------------------------------------------------------------------------------------------
262 /// Set EBASE
263 /// @param  u32addr \b IN: MIPS Code Start Address
264 /// @return None
265 //-------------------------------------------------------------------------------------------------
MAsm_CPU_SetEBASE(MS_U32 u32addr)266 void MAsm_CPU_SetEBASE(MS_U32 u32addr)
267 {
268     //printf("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
269 }
270 
271 //
272 // System time
273 //
274 
275 
276 //-------------------------------------------------------------------------------------------------
277 /// Get current system time in timer ticks
278 /// @return system time in timer ticks
279 //-------------------------------------------------------------------------------------------------
280 MS_U32 gsystem_time_ms=0;
MAsm_GetSystemTime(void)281 MS_U32 MAsm_GetSystemTime (void)
282 {
283     return gsystem_time_ms;
284 }
285 
286 //-------------------------------------------------------------------------------------------------
287 /// For Cortex-A9, the period of a timer tick is
288 /// timer tick period = (PRESCALER + 1) / PERIPHCLK
289 ///
290 /// On PBX-A9 all the PERIPHCLK is half the frequency of CPUCLK (70 MHz by default)
291 ///
292 /// With PRESCALER = 174, PERIPHCLK = 35 MHz
293 /// timer tick period = 5 microseconds
294 /// timer tick frequency = 200 kHz
295 /// @param  u32periphclk \b IN: ARM peripheral clock with unit of MHZ
296 /// @return None
297 //-------------------------------------------------------------------------------------------------
298 
MAsm_CPU_TimerInit(void)299 void MAsm_CPU_TimerInit(void)
300 {
301     MS_U32 prescale;
302     MS_U32 periphclk;
303     MS_U32 regctl;
304 
305     periphclk = ARM_CLOCK_FREQ / (1000000*2);
306     prescale = 99;
307     regctl = (prescale << 8) | 0x06; //set auto increment mode and enable interrupt
308 
309    *(volatile MS_U32*)(PERIBASE + (0x0608)) = regctl; //Set Timer control register
310    *(volatile MS_U32*)(PERIBASE + (0x0600)) = periphclk*10 - 1; //Set Timer load register
311 }
312 
MAsm_CPU_TimerStart(void)313 void MAsm_CPU_TimerStart(void)
314 {
315     *(volatile MS_U32*)(PERIBASE + (0x0608)) |= 0x0001;
316 }
317 
MAsm_CPU_TimerClean(void)318 void MAsm_CPU_TimerClean(void)
319 {
320     *(volatile MS_U32*)(PERIBASE + (0x0608)) = 0x0000;
321 }
322 
323