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
114 //-------------------------------------------------------------------------------------------------
115 // Macros
116 //-------------------------------------------------------------------------------------------------
117
118 //-------------------------------------------------------------------------------------------------
119 // Global Variables
120 //-------------------------------------------------------------------------------------------------
121
122 //-------------------------------------------------------------------------------------------------
123 // Local Variables
124 //-------------------------------------------------------------------------------------------------
125
126 //-------------------------------------------------------------------------------------------------
127 // Local Function Prototypes
128 //-------------------------------------------------------------------------------------------------
129
130 //-------------------------------------------------------------------------------------------------
131 /// Flush EC's write FIFO
132 /// @return None
133 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Sync(void)134 void ATTRIBUTE MAsm_CPU_Sync(void)
135 {
136 asm volatile (
137 "sync;"
138 );
139 }
140
141 //-------------------------------------------------------------------------------------------------
142 /// Nop
143 /// @return None
144 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Nop(void)145 inline void ATTRIBUTE MAsm_CPU_Nop(void)
146 {
147 asm volatile (
148 "nop;"
149 );
150 }
151
152 //-------------------------------------------------------------------------------------------------
153 /// SW Debug Breakpoint
154 /// @return None
155 //-------------------------------------------------------------------------------------------------
MAsm_CPU_SwDbgBp(void)156 void ATTRIBUTE MAsm_CPU_SwDbgBp(void)
157 {
158 asm volatile (
159 "SDBBP;"
160 );
161 }
162
163 //-------------------------------------------------------------------------------------------------
164 /// Enter CPU power saving mode
165 /// @return None
166 //-------------------------------------------------------------------------------------------------
MAsm_CPU_PowerDown(void)167 void ATTRIBUTE MAsm_CPU_PowerDown(void)
168 {
169 asm volatile (
170 "wait;"
171 "nop;"
172 "nop;"
173 );
174 }
175
176 //-------------------------------------------------------------------------------------------------
177 /// Set Status BEV
178 /// @param bBEV \b IN: TRUE/FALSE: 1 bootstrap / 0 normal
179 /// @return FALSE : fail
180 //-------------------------------------------------------------------------------------------------
MAsm_CPU_StatusBEV(MS_BOOL bBEV)181 void ATTRIBUTE MAsm_CPU_StatusBEV(MS_BOOL bBEV)
182 {
183 MS_U32 u32BEV = ((MS_U32)bBEV) << 22;
184
185 asm volatile (
186 "mfc0 $8, $12;"
187 "nop;"
188 "move $9, %0;"
189 "or $8, $8, $9;"
190 "mtc0 $8, $12;"
191 "nop; nop; nop;"
192 :
193 : "r"(u32BEV)
194 : "$8", "$9"
195 );
196 }
197
198 //-------------------------------------------------------------------------------------------------
199 /// Jump to the specified PC
200 /// @param u32PC \b IN: PC
201 /// @return None
202 //-------------------------------------------------------------------------------------------------
MAsm_CPU_Jump(MS_U32 u32PC)203 void ATTRIBUTE MAsm_CPU_Jump(MS_U32 u32PC)
204 {
205 typedef void ( *DirectJump ) (void);
206 DirectJump pFunc = (DirectJump)u32PC;
207 (*pFunc)();
208 }
209
210 //-------------------------------------------------------------------------------------------------
211 /// Get trail one
212 /// @param u32Flags \b IN: 32-bit flag
213 /// @return trail one position
214 //-------------------------------------------------------------------------------------------------
MAsm_CPU_GetTrailOne(MS_U32 u32Flags)215 MS_U8 ATTRIBUTE MAsm_CPU_GetTrailOne(MS_U32 u32Flags)
216 {
217 u32Flags = (~u32Flags) & (u32Flags-1);
218 asm volatile
219 (
220 "clz %0, %0\n"
221 : "=r" (u32Flags)
222 : "0" (u32Flags)
223 );
224 return 32 - u32Flags;
225 }
226
227 //-------------------------------------------------------------------------------------------------
228 /// Disable system timer interrupt
229 /// @param bEnable \b IN: TRUE: Enable timer interrupt, FALSE: Disable timer interrupt.
230 /// @return None
231 //-------------------------------------------------------------------------------------------------
MAsm_CPU_EnableTimerInterrupt(MS_BOOL bEnable)232 void ATTRIBUTE MAsm_CPU_EnableTimerInterrupt(MS_BOOL bEnable)
233 {
234 if(bEnable)
235 {
236 asm volatile (
237 "mfc0 $8, $12;"
238 "nop;"
239 "or $8,$8,0x00008000;" // IM7 = 1, timer interrupt enabled.
240 "mtc0 $8,$12;"
241 "nop; nop; nop;"
242 );
243 }
244 else
245 {
246 asm volatile (
247 "mfc0 $8, $12;"
248 "nop;"
249 "and $8,$8,0XFFFF7FFF;" // IM7 = 0, timer interrupt disabled.
250 "mtc0 $8,$12;"
251 "nop; nop; nop;"
252 );
253 }
254 }
255
256 //-------------------------------------------------------------------------------------------------
257 /// Pause for specifc duration by CPU dummy instruction
258 /// @param msec \b IN: miniseconds ( msec < (12800000/CPU_CLK_MHZ) )
259 /// @return None
260 //-------------------------------------------------------------------------------------------------
MAsm_CPU_DelayMs(MS_U32 msec)261 void ATTRIBUTE MAsm_CPU_DelayMs(MS_U32 msec)
262 {
263 #define CPU_LOOP_MSEC(_msec) ((_msec)*(MIPS_CLOCK_FREQ/1000/3)) // 3 cycles / loop
264
265 register MS_U32 loop = CPU_LOOP_MSEC(msec);
266 while(loop--);
267 }
268
269 //-------------------------------------------------------------------------------------------------
270 /// Pause for specifc duration by CPU dummy instruction
271 /// @param usec \b IN: microseconds ( usec < (12800000/CPU_CLK_MHZ) )
272 /// @return None
273 //-------------------------------------------------------------------------------------------------
MAsm_CPU_DelayUs(MS_U32 usec)274 void ATTRIBUTE MAsm_CPU_DelayUs(MS_U32 usec)
275 {
276 #if 0
277 #define CPU_LOOP_USEC(_usec) ((_usec)*(MIPS_CLOCK_FREQ/1000000/3)) // 3 cycles / loop
278
279 register MS_U32 loop = CPU_LOOP_USEC(usec);
280 while(loop--);
281 #endif
282
283 asm __volatile__ (
284 "li $8, %0\n"
285 "mul $9, $4, $8\n"
286 "1:\n"
287 "addiu $9, $9, -1\n"
288 "bnez $9, 1b\n"
289 :
290 :"I" (MIPS_CLOCK_FREQ/1000000/3)
291 :"$8", "$9"
292 );
293 }
294
295 //-------------------------------------------------------------------------------------------------
296 /// Set EBASE
297 /// @param u32addr \b IN: MIPS Code Start Address
298 /// @return None
299 //-------------------------------------------------------------------------------------------------
MAsm_CPU_SetEBASE(MS_U32 u32addr)300 void ATTRIBUTE MAsm_CPU_SetEBASE(MS_U32 u32addr)
301 {
302 asm __volatile__ (
303 // Set interrupt mode to compaitible mode.
304 // 1. Need not to fill IntCtlVs, the default value is already 0.
305 // 2. Set CauseIv = 0. Use general exception vector. page 66, 117.
306 "mfc0 $26, $13;" //mfc0 k0, cause
307 "nop; nop;" //mfc0_delay
308 "li $27, ~(1<<23);" //li k1, ~(1<<23)
309 "and $26, $26, $27;" //and k0, k0, k1
310 "mtc0 $26, $13;" //mtc0 k0, cause
311 "nop; nop;" //mfc0_delay
312
313 // 3. Set StatusBEV = 1 and adjust EBASE coprocessor
314 "mfc0 $26, $12;" //mfc0 k0, status
315 "nop; nop;" //mfc0_delay
316 "li $27, (1<<22);" //li k1, (1<<22)
317 "or $26, $26, $27;" //or k0, k0, k1
318 "mtc0 $26, $12;" //mtc0 k0, status
319 "nop; nop;" //mfc0_delay
320
321 "mfc0 $26, $15, 1;" //mfc0 k0, $15, 1
322 "nop; nop;" //mfc0_delay
323 "move $27, %0;" //move k1, u32addr
324 "or $26, $26, $27;" //or k0, k0, k1
325 "mtc0 $26, $15, 1;" //mtc0 k0, $15, 1
326 "nop; nop;" //mfc0_delay
327
328 // 4. Set StatusBEV & StatusEXL to be zero. page 66,
329 "mfc0 $26, $12;" //mfc0 k0, status
330 "nop; nop;" //mfc0_delay
331 "li $27, ~((1<<22)|(1<<1));" //li k1, ~((1<<22)|(1<<1))
332 "and $26, $26, $27;" //and k0, k0, k1
333 "mtc0 $26, $12;" //mtc0 k0, status
334 "nop; nop;" //mfc0_delay
335 // Now the Refill exception vector is u32addr and
336 // general exception vector is u32addr.
337 :
338 : "r"(u32addr)
339 : "$12", "$13", "$15", "$26", "$27"
340 );
341 }
342
343 //-------------------------------------------------------------------------------------------------
344 /// Get current system time in timer ticks
345 /// @return system time in timer ticks
346 //-------------------------------------------------------------------------------------------------
347 volatile MS_U32 gsystem_time_ms=0;
MAsm_GetSystemTime(void)348 MS_U32 ATTRIBUTE MAsm_GetSystemTime (void)
349 {
350 return gsystem_time_ms;
351 }
352
353
MAsm_CPU_TimerInit(void)354 void ATTRIBUTE MAsm_CPU_TimerInit(void)
355 {
356
357 asm __volatile__ (
358 "lui $8, ((%0*1000)>>16);"
359 "ori $8, $8, ((%0*1000)&0xFFFF);"
360 "mtc0 $0,$9;"
361 "nop; nop; nop;"
362 "mtc0 $8,$11;"
363 "nop; nop; nop;"
364 :
365 :"I" (MIPS_CLOCK_FREQ/1000000/2)
366 :"$8", "$9"
367 );
368
369 }
370
371
372 #define PAGE_SHIFT 12
373 #define CKSEG0 0x80000000
374 #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
375
376 #include "mipsreg.h"
377
local_flush_tlb_all(void)378 void ATTRIBUTE local_flush_tlb_all(void)
379 {
380 unsigned long flags;
381 unsigned long old_ctx;
382 int entry;
383
384 // ENTER_CRITICAL(flags);
385 flags = MsOS_CPU_DisableInterrupt();
386 /* Save old context and create impossible VPN2 value */
387 old_ctx = read_c0_entryhigh();
388 write_c0_entrylow0(0);
389 write_c0_entrylow1(0);
390
391 entry = read_c0_with_wired();
392
393 /* Blast 'em all away. */
394 while (entry < 16){ // current_cpu_data.tlbsize) {
395 /* Make sure all entries differ. */
396 write_c0_entryhigh(UNIQUE_ENTRYHI(entry));
397 write_c0_by_index(entry);
398 // mtc0_tlbw_hazard();
399 asm volatile ("sll $0, $0, 3;");
400 tlb_index_write();
401 entry++;
402 }
403 // tlbw_use_hazard();
404 asm volatile ("sll $0, $0, 3;");
405 write_c0_entryhigh(old_ctx);
406 // FLUSH_ITLB;
407 // EXIT_CRITICAL(flags);
408 MsOS_CPU_RestoreInterrupt (flags);
409 }
410
add_wired_entry(unsigned long entrylow0,unsigned long entrylow1,unsigned long entryhigh,unsigned long pagemask)411 void ATTRIBUTE add_wired_entry(unsigned long entrylow0, unsigned long entrylow1,
412 unsigned long entryhigh, unsigned long pagemask)
413 {
414 unsigned long wired;
415 unsigned long old_pagemask;
416 unsigned long old_ctx;
417 MS_U32 u32OldInt;
418
419 u32OldInt = MsOS_CPU_DisableInterrupt();
420
421 // ENTER_CRITICAL(flags);
422 /* Save old context and create impossible VPN2 value */
423 old_ctx = read_c0_entryhigh();
424 old_pagemask = read_c0_by_pagemask();
425 wired = read_c0_with_wired();
426 write_c0_with_wired(wired + 1);
427 write_c0_by_index(wired);
428
429 // tlbw_use_hazard(); /* What is the hazard here? */
430 asm volatile ("sll $0, $0, 3;");
431
432 write_c0_by_pagemask(pagemask);
433 write_c0_entryhigh(entryhigh);
434 write_c0_entrylow0(entrylow0);
435 write_c0_entrylow1(entrylow1);
436 // mtc0_tlbw_hazard();
437 asm volatile ("sll $0, $0, 3;");
438 tlb_index_write();
439 // tlbw_use_hazard();
440 asm volatile ("sll $0, $0, 3;");
441
442 write_c0_entryhigh(old_ctx);
443 // richard tlbw_use_hazard(); /* What is the hazard here? */
444 asm volatile ("sll $0, $0, 3;");
445 write_c0_by_pagemask(old_pagemask);
446 local_flush_tlb_all();
447 MsOS_CPU_RestoreInterrupt (u32OldInt);
448 // EXIT_CRITICAL(flags);
449 }
450
451