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) 2006-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 #define _HAL_PM_C
94
95 ////////////////////////////////////////////////////////////////////////////////
96 /// @file halPM.c
97 /// @author MStar Semiconductor Inc.
98 /// @brief PM control driver
99 ////////////////////////////////////////////////////////////////////////////////
100
101 ////////////////////////////////////////////////////////////////////////////////
102 // Header Files
103 ////////////////////////////////////////////////////////////////////////////////
104 #include "MsCommon.h"
105 #include "MsTypes.h"
106 #include "halPM.h"
107 #include "regPM.h"
108 //#include "datatype.h"
109 #include "MsOS.h"
110
111 ////////////////////////////////////////////////////////////////////////////////
112 // Define & data type
113 ///////////////////////////////////////////////////////////////////////////////
114 #define PM_HAL_ERR(x, args...) //{printf(x, ##args);}
115
116
117
118 #if 0//def WORDS_BIGENDIAN
119
120 #define VARBYTE(var, n) (((MS_U8 *)&(var))[n])
121 #else
122
123 #define VARBYTE(var, n) (((MS_U8 *)&(var))[sizeof(var) - n - 1])
124 #endif
125
126 ////////////////////////////////////////////////////////////////////////////////
127 // Global variable
128 ////////////////////////////////////////////////////////////////////////////////
129 static MS_VIRT _gMIO_MapBase = 0;
130
131 ////////////////////////////////////////////////////////////////////////////////
132 // Extern Function
133 ////////////////////////////////////////////////////////////////////////////////
134
135 ////////////////////////////////////////////////////////////////////////////////
136 // Function Declaration
137 ////////////////////////////////////////////////////////////////////////////////
138
139 ////////////////////////////////////////////////////////////////////////////////
140 // Local Function
141 ////////////////////////////////////////////////////////////////////////////////
142 #ifndef UNUSED
143 #define UNUSED(x) ((x)=(x))
144 #endif
145
146 ////////////////////////////////////////////////////////////////////////////////
147 // Global Function
148 ////////////////////////////////////////////////////////////////////////////////
149 ////////////////////////////////////////////////////////////////////////////////
150 /// @brief \b Function \b Name: HAL_PM_ReadByte
151 /// @brief \b Function \b Description: read 1 Byte data
152 /// @param <IN> \b u32RegAddr: register address
153 /// @param <OUT> \b None :
154 /// @param <RET> \b MS_U8
155 /// @param <GLOBAL> \b None :
156 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_ReadByte(MS_U32 u32RegAddr)157 MS_U8 HAL_PM_ReadByte(MS_U32 u32RegAddr)
158 {
159 return ((volatile MS_U8*)(_gMIO_MapBase))[(u32RegAddr << 1) - (u32RegAddr & 1)];
160 }
161
162 ////////////////////////////////////////////////////////////////////////////////
163 /// @brief \b Function \b Name: HAL_PM_Read2Byte
164 /// @brief \b Function \b Description: read 2 Byte data
165 /// @param <IN> \b u32RegAddr: register address
166 /// @param <OUT> \b None :
167 /// @param <RET> \b MS_U16
168 /// @param <GLOBAL> \b None :
169 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_Read2Byte(MS_U32 u32RegAddr)170 MS_U16 HAL_PM_Read2Byte(MS_U32 u32RegAddr)
171 {
172 return ((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr];
173 }
174
175 ////////////////////////////////////////////////////////////////////////////////
176 /// @brief \b Function \b Name: HAL_PM_Read4Byte
177 /// @brief \b Function \b Description: read 4 Byte data
178 /// @param <IN> \b u32RegAddr: register address
179 /// @param <OUT> \b None :
180 /// @param <RET> \b MS_U32
181 /// @param <GLOBAL> \b None :
182 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_Read4Byte(MS_U32 u32RegAddr)183 MS_U32 HAL_PM_Read4Byte(MS_U32 u32RegAddr)
184 {
185 return (HAL_PM_Read2Byte(u32RegAddr) | HAL_PM_Read2Byte(u32RegAddr+2) << 16);
186 }
187
188 ////////////////////////////////////////////////////////////////////////////////
189 /// @brief \b Function \b Name: HAL_PM_WriteByte
190 /// @brief \b Function \b Description: write 1 Byte data
191 /// @param <IN> \b u32RegAddr: register address
192 /// @param <IN> \b u8Val : 1 byte data
193 /// @param <OUT> \b None :
194 /// @param <RET> \b TRUE: Ok FALSE: Fail
195 /// @param <GLOBAL> \b None :
196 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_WriteByte(MS_U32 u32RegAddr,MS_U8 u8Val)197 MS_BOOL HAL_PM_WriteByte(MS_U32 u32RegAddr, MS_U8 u8Val)
198 {
199 if (!u32RegAddr)
200 {
201 PM_HAL_ERR("%s reg error!\n", __FUNCTION__);
202 return FALSE;
203 }
204
205 ((volatile MS_U8*)(_gMIO_MapBase))[(u32RegAddr << 1) - (u32RegAddr & 1)] = u8Val;
206 return TRUE;
207 }
208
209 ////////////////////////////////////////////////////////////////////////////////
210 /// @brief \b Function \b Name: HAL_PM_Write2Byte
211 /// @brief \b Function \b Description: write 2 Byte data
212 /// @param <IN> \b u32RegAddr: register address
213 /// @param <IN> \b u16Val : 2 byte data
214 /// @param <OUT> \b None :
215 /// @param <RET> \b TRUE: Ok FALSE: Fail
216 /// @param <GLOBAL> \b None :
217 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_Write2Byte(MS_U32 u32RegAddr,MS_U16 u16Val)218 MS_BOOL HAL_PM_Write2Byte(MS_U32 u32RegAddr, MS_U16 u16Val)
219 {
220 if (!u32RegAddr)
221 {
222 PM_HAL_ERR("%s reg error!\n", __FUNCTION__);
223 return FALSE;
224 }
225
226 ((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr] = u16Val;
227 return TRUE;
228 }
229
230 ////////////////////////////////////////////////////////////////////////////////
231 /// @brief \b Function \b Name: HAL_PM_Write4Byte
232 /// @brief \b Function \b Description: write 4 Byte data
233 /// @param <IN> \b u32RegAddr: register address
234 /// @param <IN> \b u32Val : 4 byte data
235 /// @param <OUT> \b None :
236 /// @param <RET> \b TRUE: Ok FALSE: Fail
237 /// @param <GLOBAL> \b None :
238 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_Write4Byte(MS_U32 u32RegAddr,MS_U32 u32Val)239 MS_BOOL HAL_PM_Write4Byte(MS_U32 u32RegAddr, MS_U32 u32Val)
240 {
241 if (!u32RegAddr)
242 {
243 PM_HAL_ERR("%s reg error!\n", __FUNCTION__);
244 return FALSE;
245 }
246
247 HAL_PM_Write2Byte(u32RegAddr, u32Val & 0x0000FFFF);
248 HAL_PM_Write2Byte(u32RegAddr+2, u32Val >> 16);
249 return TRUE;
250 }
251
252 ////////////////////////////////////////////////////////////////////////////////
253 /// @brief \b Function \b Name: HAL_PM_ReadRegBit
254 /// @brief \b Function \b Description: write 1 Byte data
255 /// @param <IN> \b u32RegAddr: register address
256 /// @param <IN> \b u8Val : 1 byte data
257 /// @param <OUT> \b None :
258 /// @param <RET> \b TRUE: Ok FALSE: Fail
259 /// @param <GLOBAL> \b None :
260 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_ReadRegBit(MS_U32 u32RegAddr,MS_U8 u8Mask)261 MS_BOOL HAL_PM_ReadRegBit(MS_U32 u32RegAddr, MS_U8 u8Mask)
262 {
263 MS_U8 u8Val;
264 if (!u32RegAddr)
265 {
266 PM_HAL_ERR("%s reg error!\n", __FUNCTION__);
267 return FALSE;
268 }
269
270 u8Val = HAL_PM_ReadByte(u32RegAddr);
271 return (u8Val & u8Mask);
272 }
273
274 ////////////////////////////////////////////////////////////////////////////////
275 /// @brief \b Function \b Name: HAL_PM_WriteRegBit
276 /// @brief \b Function \b Description: write 1 Byte data
277 /// @param <IN> \b u32RegAddr: register address
278 /// @param <IN> \b u8Val : 1 byte data
279 /// @param <OUT> \b None :
280 /// @param <RET> \b TRUE: Ok FALSE: Fail
281 /// @param <GLOBAL> \b None :
282 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_WriteRegBit(MS_U32 u32RegAddr,MS_BOOL bEnable,MS_U8 u8Mask)283 MS_BOOL HAL_PM_WriteRegBit(MS_U32 u32RegAddr, MS_BOOL bEnable, MS_U8 u8Mask)
284 {
285 MS_U8 u8Val;
286 if (!u32RegAddr)
287 {
288 PM_HAL_ERR("%s reg error!\n", __FUNCTION__);
289 return FALSE;
290 }
291
292 u8Val = HAL_PM_ReadByte(u32RegAddr);
293 u8Val = (bEnable) ? (u8Val | u8Mask) : (u8Val & ~u8Mask);
294 HAL_PM_WriteByte(u32RegAddr, u8Val);
295 return TRUE;
296 }
297
298 ////////////////////////////////////////////////////////////////////////////////
299 /// @brief \b Function \b Name: HAL_PM_WriteByteMask
300 /// @brief \b Function \b Description: write 1 Byte data
301 /// @param <IN> \b u32RegAddr: register address
302 /// @param <IN> \b u8Val : 1 byte data
303 /// @param <OUT> \b None :
304 /// @param <RET> \b TRUE: Ok FALSE: Fail
305 /// @param <GLOBAL> \b None :
306 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_WriteByteMask(MS_U32 u32RegAddr,MS_U8 u8ValIn,MS_U8 u8Msk)307 MS_BOOL HAL_PM_WriteByteMask(MS_U32 u32RegAddr, MS_U8 u8ValIn, MS_U8 u8Msk)
308 {
309 MS_U8 u8Val;
310 if (!u32RegAddr)
311 {
312 PM_HAL_ERR("%s reg error!\n", __FUNCTION__);
313 return FALSE;
314 }
315
316 u8Val = HAL_PM_ReadByte(u32RegAddr);
317 u8Val = (u8Val & ~(u8Msk)) | ((u8ValIn) & (u8Msk));
318 HAL_PM_WriteByte(u32RegAddr, u8Val);
319 return TRUE;
320 }
321
322
323 ////////////////////////////////////////////////////////////////////////////////
324 /// @brief \b Function \b Name: HAL_PM_SetIOMapBase
325 /// @brief \b Function \b Description: Set IO Map base
326 /// @param <IN> \b None :
327 /// @param <OUT> \b None :
328 /// @param <RET> \b None :
329 /// @param <GLOBAL> \b None :
330 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_SetIOMapBase(MS_VIRT virtBase)331 void HAL_PM_SetIOMapBase(MS_VIRT virtBase)
332 {
333 _gMIO_MapBase = virtBase;
334 PM_HAL_ERR("PM IOMap base:%8lx Reg offset:%4x\n", virtBase, PMRTC_REG_BASE);
335 }
336
337
338 ////////////////////////////////////////////////////////////////////////////////
339 /// @brief \b Function \b Name: HAL_PM_GetIOMapBase
340 /// @brief \b Function \b Description: Get IO Map base
341 /// @param <IN> \b None :
342 /// @param <OUT> \b None :
343 /// @param <RET> \b None :
344 /// @param <GLOBAL> \b None :
345 ////////////////////////////////////////////////////////////////////////////////
HAL_PM_GetIOMapBase(void)346 MS_VIRT HAL_PM_GetIOMapBase(void)
347 {
348 return _gMIO_MapBase;
349 }
350
HAL_PM_RtcInit(MS_U8 u8RtcIndex,MS_U32 u32RtcCtrlWord)351 void HAL_PM_RtcInit(MS_U8 u8RtcIndex, MS_U32 u32RtcCtrlWord)
352 {
353 HAL_PM_Write2Byte(REG_PM_CKG_RTC,
354 (HAL_PM_Read2Byte(REG_PM_CKG_RTC)&(~(__BIT4|__BIT3|__BIT2|__BIT1|__BIT0)))|(1<<2)); //RTC clock switch to 12MHz
355
356 switch(u8RtcIndex)
357 {
358 PM_HAL_ERR("RTC init\n");
359
360 case PM_RTCIDX_0:
361 PM_HAL_ERR("PM_RTCIDX_0\n");
362 if((HAL_PM_ReadByte(REG_PMRTC_CTRL) & (0x00 | PMRTC_CTRL_CNT_EN))) //check if RTC was initialized before
363 {
364 HAL_PM_WriteRegBit(REG_PMRTC_CTRL, DISABLE, PMRTC_CTRL_CNT_EN); // Disable init
365 HAL_PM_WriteRegBit(REG_PMRTC_CTRL, DISABLE, PMRTC_CTRL_NOT_RSTZ); // Disable RSTZ
366 }
367 //HAL_PM_WriteByte(REG_PMRTC_CTRL, 0x00);
368 HAL_PM_WriteByte(REG_PMRTC_CTRL, (PMRTC_CTRL_NOT_RSTZ | PMRTC_CTRL_INT_MASK));
369 HAL_PM_WriteByte(REG_PMRTC_FREQ_CW + 0, VARBYTE(u32RtcCtrlWord, 3));
370 HAL_PM_WriteByte(REG_PMRTC_FREQ_CW + 1, VARBYTE(u32RtcCtrlWord, 2));
371 HAL_PM_WriteByte(REG_PMRTC_FREQ_CW + 2, VARBYTE(u32RtcCtrlWord, 1));
372 HAL_PM_WriteByte(REG_PMRTC_FREQ_CW + 3, VARBYTE(u32RtcCtrlWord, 0));
373 HAL_PM_WriteByte(REG_PMRTC_CTRL, (PMRTC_CTRL_NOT_RSTZ | PMRTC_CTRL_CNT_EN | PMRTC_CTRL_INT_MASK | PMRTC_CTRL_INT_CLEAR));
374 break;
375 case PM_RTCIDX_1:
376 PM_HAL_ERR("PM_RTCIDX_1\n");
377 if((HAL_PM_ReadByte(REG_PMRTC1_CTRL) & (0x00 | PMRTC1_CTRL_CNT_EN))) //check if RTC was initialized before
378 {
379 HAL_PM_WriteRegBit(REG_PMRTC1_CTRL, DISABLE, PMRTC1_CTRL_CNT_EN); // Disable init
380 HAL_PM_WriteRegBit(REG_PMRTC1_CTRL, DISABLE, PMRTC1_CTRL_NOT_RSTZ); // Disable RSTZ
381 }
382 //HAL_PM_WriteByte(REG_PMRTC1_CTRL, 0x00);
383 HAL_PM_WriteByte(REG_PMRTC1_CTRL, (PMRTC1_CTRL_NOT_RSTZ | PMRTC1_CTRL_INT_MASK));
384 HAL_PM_WriteByte(REG_PMRTC1_FREQ_CW + 0, VARBYTE(u32RtcCtrlWord, 3));
385 HAL_PM_WriteByte(REG_PMRTC1_FREQ_CW + 1, VARBYTE(u32RtcCtrlWord, 2));
386 HAL_PM_WriteByte(REG_PMRTC1_FREQ_CW + 2, VARBYTE(u32RtcCtrlWord, 1));
387 HAL_PM_WriteByte(REG_PMRTC1_FREQ_CW + 3, VARBYTE(u32RtcCtrlWord, 0));
388 HAL_PM_WriteByte(REG_PMRTC1_CTRL, (PMRTC1_CTRL_NOT_RSTZ | PMRTC1_CTRL_CNT_EN | PMRTC1_CTRL_INT_MASK | PMRTC1_CTRL_INT_CLEAR));
389 break;
390 default:
391 PM_HAL_ERR("ERROR RTC Index Number\n");
392 break;
393 }
394 }
395
HAL_PM_RTC_DisableInit(MS_U8 u8RtcIndex)396 void HAL_PM_RTC_DisableInit(MS_U8 u8RtcIndex)
397 {
398 switch(u8RtcIndex)
399 {
400 case PM_RTCIDX_0:
401 HAL_PM_WriteRegBit(REG_PMRTC_CTRL, DISABLE, PMRTC_CTRL_CNT_EN); // Disable init
402 HAL_PM_WriteRegBit(REG_PMRTC_CTRL, DISABLE, PMRTC_CTRL_NOT_RSTZ); // Disable RSTZ
403 break;
404 case PM_RTCIDX_1:
405 HAL_PM_WriteRegBit(REG_PMRTC1_CTRL, DISABLE, PMRTC1_CTRL_CNT_EN); // Disable init
406 HAL_PM_WriteRegBit(REG_PMRTC1_CTRL, DISABLE, PMRTC1_CTRL_NOT_RSTZ); // Disable RSTZ
407 break;
408 default:
409 PM_HAL_ERR("ERROR RTC Index Number\n");
410 break;
411 }
412 }
413
HAL_PM_RtcSetCounter(MS_U8 u8RtcIndex,MS_U32 u32RtcSetCounter)414 void HAL_PM_RtcSetCounter(MS_U8 u8RtcIndex, MS_U32 u32RtcSetCounter)
415 {
416 switch(u8RtcIndex)
417 {
418 PM_HAL_ERR("RTC SetCounter\n");
419 case PM_RTCIDX_0:
420 PM_HAL_ERR("PM_RTCIDX_0\n");
421 HAL_PM_WriteByte(REG_PMRTC_LOAD_VAL + 0, VARBYTE(u32RtcSetCounter, 3));
422 HAL_PM_WriteByte(REG_PMRTC_LOAD_VAL + 1, VARBYTE(u32RtcSetCounter, 2));
423 HAL_PM_WriteByte(REG_PMRTC_LOAD_VAL + 2, VARBYTE(u32RtcSetCounter, 1));
424 HAL_PM_WriteByte(REG_PMRTC_LOAD_VAL + 3, VARBYTE(u32RtcSetCounter, 0));
425 HAL_PM_WriteByte(REG_PMRTC_CTRL, (HAL_PM_ReadByte(REG_PMRTC_CTRL)|PMRTC_CTRL_LOAD_EN));
426 break;
427 case PM_RTCIDX_1:
428 PM_HAL_ERR("PM_RTCIDX_1\n");
429 HAL_PM_WriteByte(REG_PMRTC1_LOAD_VAL + 0, VARBYTE(u32RtcSetCounter, 3));
430 HAL_PM_WriteByte(REG_PMRTC1_LOAD_VAL + 1, VARBYTE(u32RtcSetCounter, 2));
431 HAL_PM_WriteByte(REG_PMRTC1_LOAD_VAL + 2, VARBYTE(u32RtcSetCounter, 1));
432 HAL_PM_WriteByte(REG_PMRTC1_LOAD_VAL + 3, VARBYTE(u32RtcSetCounter, 0));
433 HAL_PM_WriteByte(REG_PMRTC1_CTRL, (HAL_PM_ReadByte(REG_PMRTC1_CTRL)|PMRTC1_CTRL_LOAD_EN));
434 break;
435 default:
436 PM_HAL_ERR("ERROR RTC Index Number\n");
437 break;
438 }
439 }
440
441
HAL_PM_RtcGetCounter(MS_U8 u8RtcIndex)442 MS_U32 HAL_PM_RtcGetCounter(MS_U8 u8RtcIndex)
443 {
444 switch(u8RtcIndex)
445 {
446 case PM_RTCIDX_0:
447 HAL_PM_WriteByte(REG_PMRTC_CTRL, (HAL_PM_ReadByte(REG_PMRTC_CTRL)|PMRTC_CTRL_READ_EN));
448 MsOS_DelayTaskUs(100);
449 return HAL_PM_Read4Byte(REG_PMRTC_CNT);
450 break;
451 case PM_RTCIDX_1:
452 HAL_PM_WriteByte(REG_PMRTC1_CTRL, (HAL_PM_ReadByte(REG_PMRTC1_CTRL)|PMRTC1_CTRL_READ_EN));
453 MsOS_DelayTaskUs(100);
454 return HAL_PM_Read4Byte(REG_PMRTC1_CNT);
455 break;
456 default:
457 PM_HAL_ERR("ERROR RTC Index Number\n");
458 break;
459 }
460 PM_HAL_ERR("ERROR\n");
461 return 0;
462 }
463
HAL_PM_RtcSetMatchCounter(MS_U8 u8RtcIndex,MS_U32 u32RtcSetMatchCounter)464 void HAL_PM_RtcSetMatchCounter(MS_U8 u8RtcIndex, MS_U32 u32RtcSetMatchCounter)
465 {
466 switch(u8RtcIndex)
467 {
468 case PM_RTCIDX_0:
469 HAL_PM_WriteByte(REG_PMRTC_CTRL, (HAL_PM_ReadByte(REG_PMRTC_CTRL)|(PMRTC_CTRL_INT_MASK|PMRTC_CTRL_INT_CLEAR)));
470 if (u32RtcSetMatchCounter)
471 {
472 HAL_PM_Write4Byte(REG_PMRTC_MATCH_VAL + 4, 0x00000000);
473 HAL_PM_WriteByte(REG_PMRTC_MATCH_VAL + 0, VARBYTE(u32RtcSetMatchCounter, 3));
474 HAL_PM_WriteByte(REG_PMRTC_MATCH_VAL + 1, VARBYTE(u32RtcSetMatchCounter, 2));
475 HAL_PM_WriteByte(REG_PMRTC_MATCH_VAL + 2, VARBYTE(u32RtcSetMatchCounter, 1));
476 HAL_PM_WriteByte(REG_PMRTC_MATCH_VAL + 3, VARBYTE(u32RtcSetMatchCounter, 0));
477 HAL_PM_WriteByte(REG_PMRTC_CTRL, (HAL_PM_ReadByte(REG_PMRTC_CTRL)&(~(PMRTC_CTRL_INT_MASK|PMRTC_CTRL_INT_CLEAR))));
478 }
479 break;
480 case PM_RTCIDX_1:
481 HAL_PM_WriteByte(REG_PMRTC1_CTRL, (HAL_PM_ReadByte(REG_PMRTC1_CTRL)|(PMRTC1_CTRL_INT_MASK|PMRTC1_CTRL_INT_CLEAR)));
482 if (u32RtcSetMatchCounter)
483 {
484 HAL_PM_Write4Byte(REG_PMRTC1_MATCH_VAL + 4, 0x00000000);
485 HAL_PM_WriteByte(REG_PMRTC1_MATCH_VAL + 0, VARBYTE(u32RtcSetMatchCounter, 3));
486 HAL_PM_WriteByte(REG_PMRTC1_MATCH_VAL + 1, VARBYTE(u32RtcSetMatchCounter, 2));
487 HAL_PM_WriteByte(REG_PMRTC1_MATCH_VAL + 2, VARBYTE(u32RtcSetMatchCounter, 1));
488 HAL_PM_WriteByte(REG_PMRTC1_MATCH_VAL + 3, VARBYTE(u32RtcSetMatchCounter, 0));
489 HAL_PM_WriteByte(REG_PMRTC1_CTRL, (HAL_PM_ReadByte(REG_PMRTC1_CTRL)&(~(PMRTC1_CTRL_INT_MASK|PMRTC1_CTRL_INT_CLEAR))));
490 }
491 break;
492 default:
493 PM_HAL_ERR("ERROR RTC Index Number\n");
494 break;
495 }
496 }
497
HAL_PM_RtcGetMatchCounter(MS_U8 u8RtcIndex)498 MS_U32 HAL_PM_RtcGetMatchCounter(MS_U8 u8RtcIndex)
499 {
500 MS_U32 RTCCount=0;
501 switch(u8RtcIndex)
502 {
503 case PM_RTCIDX_0:
504 RTCCount= HAL_PM_Read4Byte(REG_PMRTC_MATCH_VAL);
505 break;
506 case PM_RTCIDX_1:
507 RTCCount= HAL_PM_Read4Byte(REG_PMRTC1_MATCH_VAL);
508 break;
509 default:
510 PM_HAL_ERR("ERROR RTC Index Number\n");
511 break;
512 }
513 return RTCCount;
514 }
515
516 ////////////////////////////////////////////////////////////////////////////////
517 /// @brief \b Function \b Name: HAL_BDMA_SetSPIOffsetForMCU
518 /// @brief \b Function \b Description:
519 /// @param <IN> \b None :
520 /// @param <OUT> \b None :
521 /// @param <RET> \b None :
522 /// @param <GLOBAL> \b None :
523 ////////////////////////////////////////////////////////////////////////////////
524 #define MCU_REG_BASE 0x1000UL //0x001000
525 #define PMSLEEP_REG_BASE 0x0e00UL //0x000e00
526 #define REG_PM_CPUX_SW_RSTZ (PMSLEEP_REG_BASE + 0x29UL*2)
527
HAL_PM_SetSPIOffsetForMCU(MS_U32 BANK)528 void HAL_PM_SetSPIOffsetForMCU(MS_U32 BANK)
529 {
530
531 MS_U32 start_addr = 0x00000UL;
532 MS_U32 end_addr = 0xff0000UL;
533 MS_U32 u32temCnt;
534
535 HAL_PM_WriteRegBit(0x000e41UL, DISABLE, __BIT6); //spi_clk=xtal
536
537 HAL_PM_WriteRegBit(0x000e40UL, DISABLE, __BIT7); //mcu51 clk=xtal
538
539 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT3); // i_cache rstz
540
541 HAL_PM_WriteRegBit(0x002ba0UL, ENABLE, __BIT0); //disable i cache
542
543 HAL_PM_Write2Byte(0x0e24UL,0x0000UL);
544 HAL_PM_Write2Byte(0x0e26UL,0x0000UL);
545
546 HAL_PM_Write2Byte(0x2e52UL,0x4002UL); //PM 51 rst
547
548 HAL_PM_Write2Byte(0x2e3eUL,0x44UL); // Chip_overwrite to mips boot
549
550
551 HAL_PM_Write2Byte(MCU_REG_BASE+0x14UL,(MS_U16)start_addr & 0x0000FFFFUL);
552 HAL_PM_Write2Byte(MCU_REG_BASE+0x10UL,(MS_U16)(start_addr>>16) & 0x000000FFUL);
553 HAL_PM_Write2Byte(MCU_REG_BASE+0x16UL,(MS_U16)end_addr & 0x0000FFFFUL);
554 HAL_PM_Write2Byte(MCU_REG_BASE+0x12UL,(MS_U16)(end_addr>>16) & 0x000000FFUL);
555
556
557
558
559 //set spi offset set reg_spi_offset_addr[7:0] 0x0010f9 = 0x01 => 0x10000
560 //set reg_spi_offset_en 0x0010f8 = 0x01
561 //HAL_PM_Write2Byte(MCU_REG_BASE+0xf8,0x0101);
562 HAL_PM_WriteByte(MCU_REG_BASE+0xf9UL,(MS_U8) (0xFFUL&BANK) );
563 HAL_PM_WriteByte(MCU_REG_BASE+0xf8UL,0x01UL);
564 //set reset password 0x0e54=0x829f
565 // HAL_PM_Write2Byte(PMSLEEP_REG_BASE+0x54,0x829f);
566 HAL_PM_Write2Byte(0x2e54,0x829fUL);
567
568
569 //reg_fire[3:0] 0x000e53 = 0x01
570
571 //reg_fire[3:0] 0x000e53 = 0x00
572
573 HAL_PM_WriteByte(0x2e53UL,0x41UL);
574 for(u32temCnt=0; u32temCnt<0xffffUL; u32temCnt++);
575 HAL_PM_WriteByte(0x2e53UL,0x40UL);
576
577 for(u32temCnt=0; u32temCnt<0xffffUL; u32temCnt++);
578 HAL_PM_WriteByte(0x2e53UL,0x50UL);
579
580 printf("PM Wait for PM51 standby...........\n");
581 while(HAL_PM_Read2Byte(0x0e24UL)!=0x02UL);
582
583 printf("PM51 run ok...........\n");
584
585
586 }
587
HAL_PM_SetSRAMOffsetForMCU(void)588 void HAL_PM_SetSRAMOffsetForMCU(void)
589 {
590 MS_U32 start_addr = 0x0000000UL;
591 MS_U32 end_addr = 0x00005FFFUL;
592 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)&(~__BIT0));
593 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)&(~__BIT4)); //8051 reset
594 HAL_PM_WriteByte(0x0e24UL, 0x00UL);//Clear bit
595
596
597 //HAL_PM_WriteRegBit(0x000e41, DISABLE, __BIT6); //spi_clk=xtal
598
599 HAL_PM_WriteRegBit(0x000e40UL, DISABLE, __BIT7); //mcu51 clk=xtal
600
601 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT3); // i_cache rstz
602
603 HAL_PM_WriteRegBit(0x002ba0UL, ENABLE , __BIT0); //disable i cache (enable icache bypass)
604
605 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT1); //SPI disable
606
607 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT2); //DRAM disable
608
609 HAL_PM_WriteRegBit(0x001018UL, ENABLE , __BIT0); //SRAM enable
610
611 HAL_PM_Write2Byte(MCU_REG_BASE+0x04UL,(MS_U16)start_addr & 0x0000FFFFUL);
612 HAL_PM_Write2Byte(MCU_REG_BASE+0x00UL,(MS_U16)(start_addr>>16) & 0x000000FFUL);
613 HAL_PM_Write2Byte(MCU_REG_BASE+0x06UL,(MS_U16)end_addr & 0x0000FFFFUL);
614 HAL_PM_Write2Byte(MCU_REG_BASE+0x02UL,(MS_U16)(end_addr>>16) & 0x000000FFUL);
615
616 //set reset password 0x0e54=0x829f
617 HAL_PM_Write2Byte(PMSLEEP_REG_BASE+0x54UL,0x829fUL);
618
619
620 //reg_fire[3:0] 0x000e53 = 0x01
621
622 //reg_fire[3:0] 0x000e53 = 0x00
623 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)|__BIT0);
624 MsOS_DelayTaskUs(120);
625 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)|__BIT4); //release 8051 reset
626
627 printf("Wait for PM51 standby...........\n");
628 while(HAL_PM_Read2Byte(0x0e24UL)!=0x02UL);
629
630 printf("PM51 run ok...........\n");
631
632 }
633
HAL_PM_SetDRAMOffsetForMCU(MS_U32 u32Offset)634 MS_BOOL HAL_PM_SetDRAMOffsetForMCU(MS_U32 u32Offset)
635 {
636 MS_U32 start_addr = 0x0000000UL;
637 MS_U32 end_addr = 0xFFFFFFFFUL;
638 MS_U8 times = 0;
639
640 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)&(~__BIT0));
641 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)&(~__BIT4)); //8051 reset
642 HAL_PM_WriteByte(0x0e24UL, 0x00UL);//Clear bit
643
644
645 //HAL_PM_WriteRegBit(0x000e41, DISABLE, __BIT6); //spi_clk=xtal
646
647 HAL_PM_WriteRegBit(0x000e40UL, DISABLE, __BIT7); //mcu51 clk=xtal
648
649 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT3); // i_cache rstz
650
651 HAL_PM_WriteRegBit(0x002ba0UL, ENABLE , __BIT0); //disable i cache (enable icache bypass)
652
653 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT1); //SPI disable
654
655 HAL_PM_WriteRegBit(0x001018UL, ENABLE, __BIT2); //DRAM enable
656
657 HAL_PM_WriteRegBit(0x001018UL, DISABLE , __BIT0); //SRAM disable
658
659 HAL_PM_Write2Byte(MCU_REG_BASE+0x0cUL,(MS_U16)start_addr & 0x0000FFFFUL); //reg_dram_start_addr_0
660 HAL_PM_Write2Byte(MCU_REG_BASE+0x08UL,(MS_U16)(start_addr>>16) & 0x0000FFFFUL);
661 HAL_PM_Write2Byte(MCU_REG_BASE+0x0eUL,(MS_U16)end_addr & 0x0000FFFFUL); //reg_dram_end_addr_0
662 HAL_PM_Write2Byte(MCU_REG_BASE+0x0AUL,(MS_U16)(end_addr>>16) & 0x00000FFFFUL);
663
664 //set reset password 0x0e54=0x829f
665 HAL_PM_Write2Byte(0x2e54UL,0x829fUL);
666
667 HAL_PM_Write2Byte(0x2B80,(u32Offset>>16));
668
669 //reg_fire[3:0] 0x000e53 = 0x01
670
671 //reg_fire[3:0] 0x000e53 = 0x00
672 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)|__BIT0);
673 MsOS_DelayTaskUs(120);
674 HAL_PM_WriteByte(0x2e53UL,HAL_PM_ReadByte(0x2e53UL)|__BIT4); //release 8051 reset
675
676 printf("Wait for PM51 standby DRAM ...........\n");
677
678 while((HAL_PM_Read2Byte(0x0e24UL)!=0x02UL))
679 {
680 if(times > 20)
681 {
682 printf("PM51 run failed...........\n"); // it will try to run RT_PM in 400ms, when time's up, the function will return directly
683 return FALSE;
684 }
685 MsOS_DelayTask(20); // delay 20ms
686 times++;
687 }
688
689 printf("PM51 run ok...........\n");
690 return TRUE;
691 }
692
HAL_PM_PowerOnMode(void)693 MS_U8 HAL_PM_PowerOnMode(void)
694 {
695 MS_U8 u8PowerDownMode = HAL_PM_ReadByte(REG_PM_DUMMY_POWERON_MODE);
696
697 return u8PowerDownMode;
698
699 }
700
HAL_PM_GetWakeupSource(void)701 MS_U8 HAL_PM_GetWakeupSource(void)
702 {
703 MS_U8 u8WakeupSource = HAL_PM_ReadByte(REG_PM_DUMMY_WAKEUP_SOURCE);
704
705 return u8WakeupSource;
706
707 }
708
HAL_PM_GetWakeupKey(void)709 MS_U8 HAL_PM_GetWakeupKey(void)
710 {
711 PM_HAL_ERR("[%s] Not implemented yet\n", __FUNCTION__);
712 return 0;
713 }
714
HAL_PM_isRunning(void)715 MS_BOOL HAL_PM_isRunning(void)
716 {
717 MS_U16 u16Data_old;
718 MS_U16 u16Data_new;
719
720 HAL_PM_Write2Byte(0x0ea2UL,0x1234UL); //dummy register
721 u16Data_old = HAL_PM_Read2Byte(0x10fcUL); //pc counter
722 MsOS_DelayTaskUs(10);
723 u16Data_new = HAL_PM_Read2Byte(0x10fcUL);
724
725 if((u16Data_old != u16Data_new) && (HAL_PM_Read2Byte(0x0ea2UL)==0x4321UL))
726 return TRUE;
727 else
728 return FALSE;
729 }
730
HAL_PM_Disable51(void)731 void HAL_PM_Disable51(void)
732 {
733 HAL_PM_WriteByte(0x0e38UL, HAL_PM_ReadByte(0x0e38UL)&(~__BIT2));
734 HAL_PM_WriteByte(0x0e38UL, HAL_PM_ReadByte(0x0e38UL)&(~__BIT3));
735 HAL_PM_WriteRegBit(0x002e53UL, DISABLE, __BIT4); //8051 reset disable
736 HAL_PM_WriteRegBit(0x002e53UL, DISABLE, __BIT0); //8051 reset disable
737 HAL_PM_WriteRegBit(0x001018UL, ENABLE, __BIT1); //SPI disable
738 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT2); //DRAM disable
739 HAL_PM_WriteRegBit(0x001018UL, DISABLE , __BIT0); //SRAM enable
740 HAL_PM_WriteRegBit(0x001018UL, DISABLE, __BIT3); // i_cache rstz
741 HAL_PM_WriteByte(0x0e24UL, 0x00UL);//Clear bit
742 HAL_PM_Write2Byte(0x2B80UL,0x0000UL);
743
744 #if 0
745 HAL_PM_WriteRegBit(REG_PM_CHIP_CFG_OVERWRITE, 1, CHIP_CFG_MIPS_EN);
746 HAL_PM_WriteRegBit(REG_PM_CHIP_CFG_OVERWRITE, 1, CHIP_CFG_MIPS_VAL);
747 MsOS_DelayTask(10);
748 HAL_PM_WriteRegBit(REG_PM_CPU_SW_RST, 0, PM_51_SW_RST); // active low
749 #endif
750
751 }
752
753 #define ENABLE_PATTERN 0xAA
754 #define DISABLE_PATTERN 0x55
755 #define TRIGGER_MASK (__BIT7|__BIT6|__BIT5|__BIT4)
756 #define ACTIVE_MASK (__BIT3|__BIT2|__BIT1|__BIT0)
757
HAL_PM_GetBackGroundActive(void)758 MS_BOOL HAL_PM_GetBackGroundActive(void)
759 {
760 MS_U8 u8Mask,u8Data;
761 u8Mask = (MS_U8)ACTIVE_MASK;
762 u8Data = HAL_PM_ReadByte(REG_PM_DUMMY_ACTIVE_STANDBY);
763 return ((ENABLE_PATTERN & u8Mask) == (u8Data & u8Mask));
764 }
765
HAL_PM_SetBackGroundTrigger(MS_BOOL bTrigger)766 void HAL_PM_SetBackGroundTrigger(MS_BOOL bTrigger)
767 {
768 MS_U8 u8Mask,u8Data,u8CtrlData;
769 u8Mask = (MS_U8)TRIGGER_MASK;
770 u8Data = HAL_PM_ReadByte(REG_PM_DUMMY_ACTIVE_STANDBY);
771 if(TRUE == bTrigger)
772 u8CtrlData = (MS_U8)(ENABLE_PATTERN);
773 else
774 u8CtrlData = (MS_U8)(DISABLE_PATTERN);
775 //HAL_PM_WriteByte(REG_PM_DUMMY_ACTIVE_STANDBY, ((u8Data & (~u8Mask)) | (u8CtrlData & u8Mask)));
776 HAL_PM_WriteByte(REG_PM_DUMMY_ACTIVE_STANDBY, (u8CtrlData));
777 }
778
HAL_PM_GPIO4_SetPower(MS_BOOL bOn)779 void HAL_PM_GPIO4_SetPower(MS_BOOL bOn)
780 {
781 if(bOn)
782 {
783 HAL_PM_Write2Byte(REG_PM_GPIO4_OUT, (HAL_PM_Read2Byte(REG_PM_GPIO4_OUT))|(PMGPIO_OUT));
784 HAL_PM_Write2Byte(REG_PM_GPIO4_OEN, (HAL_PM_Read2Byte(REG_PM_GPIO4_OEN))&(~PMGPIO_OEN));
785 }
786 else
787 {
788 HAL_PM_Write2Byte(REG_PM_GPIO4_OUT, (HAL_PM_Read2Byte(REG_PM_GPIO4_OUT))&(~PMGPIO_OUT));
789 HAL_PM_Write2Byte(REG_PM_GPIO4_OEN, (HAL_PM_Read2Byte(REG_PM_GPIO4_OEN))&(~PMGPIO_OEN));
790 HAL_PM_Write2Byte(REG_PM_GPIO_PM_LOCK, 0xBABEUL);
791 }
792 }
793
HAL_PM_SetScratchPad(MS_U8 u8PadNum,MS_U16 u16Val)794 void HAL_PM_SetScratchPad(MS_U8 u8PadNum,MS_U16 u16Val)
795 {
796 switch(u8PadNum)
797 {
798 case 1:
799 HAL_PM_Write2Byte(PM_REG1_ScratchPad,u16Val);
800 break;
801 case 2:
802 HAL_PM_Write2Byte(PM_REG2_ScratchPad,u16Val);
803 break;
804 case 3:
805 HAL_PM_Write2Byte(PM_REG3_ScratchPad,u16Val);
806 break;
807 case 4:
808 HAL_PM_Write2Byte(PM_REG4_ScratchPad,u16Val);
809 break;
810 default:
811 break;
812 }
813 }
814
HAL_PM_GetScratchPad(MS_U8 u8PadNum,MS_U16 * u16Val)815 MS_BOOL HAL_PM_GetScratchPad(MS_U8 u8PadNum, MS_U16 *u16Val)
816 {
817 if(u16Val == NULL)
818 {
819 return FALSE;
820 }
821
822 switch(u8PadNum)
823 {
824 case 1:
825 *u16Val = HAL_PM_Read2Byte(PM_REG1_ScratchPad);
826 return TRUE;
827 case 2:
828 *u16Val = HAL_PM_Read2Byte(PM_REG2_ScratchPad);
829 return TRUE;
830 case 3:
831 *u16Val = HAL_PM_Read2Byte(PM_REG3_ScratchPad);
832 return TRUE;
833 case 4:
834 *u16Val = HAL_PM_Read2Byte(PM_REG4_ScratchPad);
835 return TRUE;
836 default:
837 return FALSE;
838 }
839 }
840
HAL_PM_GetVersion(MS_U8 * u8Val)841 MS_BOOL HAL_PM_GetVersion(MS_U8 *u8Val)
842 {
843 if(u8Val == NULL)
844 {
845 return FALSE;
846 }
847
848 *u8Val = HAL_PM_ReadByte(MCU_REG_BASE+0xA6UL);
849 return TRUE;
850 }
851
HAL_PM_GetSRAMSize(void)852 MS_U16 HAL_PM_GetSRAMSize(void)
853 {
854 MS_U16 u16SRAMSize = 0;
855 u16SRAMSize = 0x6000UL;
856 return u16SRAMSize;
857
858 }
859
HAL_PM_GetIRPowerOnKey(void)860 MS_U8 HAL_PM_GetIRPowerOnKey(void)
861 {
862 MS_U8 u8IRKey = 0;
863 u8IRKey = HAL_PM_ReadByte(MCU_REG_BASE+0xA7UL);
864 return u8IRKey;
865 }
866
HAL_PM_SetWakupDevice(MS_U16 u16WakeSrc)867 void HAL_PM_SetWakupDevice(MS_U16 u16WakeSrc)
868 {
869 //HAL_PM_Write2Byte(REG_PM_SLEEP_NOTIFY_WAKEUP_DEVICE_FLAG,u16WakeSrc);
870 }
871