xref: /utopia/UTPA2-700.0.x/modules/wdt/drv/wdt/drvWDT.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    drvWDT.c
98 /// @brief  Piu Watch Dog Timer Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 // Include Files
105 ////////////////////////////////////////////////////////////////////////////////
106 #if !defined(MSOS_TYPE_LINUX_KERNEL)
107 #include <stdio.h>
108 #include <string.h>
109 #else
110 #include <linux/string.h>
111 #include <linux/slab.h>
112 #endif
113 
114 #include "MsDevice.h"
115 #include "MsVersion.h"
116 #include "MsCommon.h"
117 #include "MsIRQ.h"
118 #include "drvMMIO.h"
119 #include "drvWDT.h"
120 #include "halWDT.h"
121 #include "regWDT.h"
122 #include "MsOS.h"
123 
124 #include "ULog.h"
125 
126 #define WDT_DBG_FUNC()            if (_geDbfLevel >= E_WDT_DBGLV_ALL) \
127                                            {MS_DEBUG_MSG(ULOGI("WDT", "\t====   %s   ====\n", __FUNCTION__));}
128 #define WDT_DBG_INFO(x, args...)  if (_geDbfLevel >= E_WDT_DBGLV_INFO ) \
129                                            {MS_DEBUG_MSG(ULOGI("WDT", x, ##args));}
130 #define WDT_DBG_ERR(x, args...)   if (_geDbfLevel >= E_WDT_DBGLV_ERR_ONLY) \
131 										   {MS_DEBUG_MSG(ULOGE("WDT", x, ##args));}
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 // Local & Global Variables
135 ////////////////////////////////////////////////////////////////////////////////
136 #ifdef CONFIG_MSTAR_UTPA2K_WDT_ENABLE
137 #define WDT_UTOPIA20 (1)
138 #else
139 #define WDT_UTOPIA20 (0)
140 #endif
141 
142 #ifdef CONFIG_MSTAR_WDT_KERNEL_MODE
143 #define WDT_KERNEL_MODE (1)
144 #else
145 #define WDT_KERNEL_MODE (0)
146 #endif
147 
148 static tmr_interrupt tTmrTbl[MAX_TIMER_NUM];	/* Table of timers managed by this module		*/
149 static MS_BOOL  _gbInitWDT = FALSE;
150 static WDT_DbgLv _geDbfLevel = E_WDT_DBGLV_ERR_ONLY;
151 static MSIF_Version _drv_wdt_version = {
152     .DDI = { WDT_DRV_VERSION },
153 };
154 
155 #if (WDT_UTOPIA20)
156 MS_BOOL bU20WdtOpened = FALSE;
157 void* pInstantWdt;
158 void* pAttributeWdt;
159 #include "drvWDT_private.h"
160 #include "utopia.h"
_MDrv_WDT_CheckUtopia20Open(void ** pInstance,MS_U32 u32ModuleVersion,void * pAttribute)161 MS_BOOL _MDrv_WDT_CheckUtopia20Open(void** pInstance, MS_U32 u32ModuleVersion, void* pAttribute)
162 {
163 	if (FALSE == bU20WdtOpened)
164 	{
165 #if (WDT_KERNEL_MODE)
166 		if(UtopiaOpen(MODULE_WDT|KERNEL_MODE, pInstance, u32ModuleVersion, pAttribute) != UTOPIA_STATUS_SUCCESS)
167 #else
168 		if(UtopiaOpen(MODULE_WDT, pInstance, u32ModuleVersion, pAttribute) != UTOPIA_STATUS_SUCCESS)
169 #endif
170 		{
171 			WDT_DBG_ERR("Open WDT fail\n");
172 			return FALSE;
173 		}
174 		bU20WdtOpened= TRUE;
175 	}
176 	return TRUE;
177 }
178 #endif
179 
180 inline static void _MDrv_TIMER_Trigger(E_PIU_Timer eTimer, MS_U32 u32count);
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// @brief \b Function \b Name: MDrv_WDT_GetLibVer
184 /// @brief \b Function \b Description: Show the WDT driver version
185 /// @param <IN>        \b None:
186 /// @param <OUT>       \b **pVerString : output WDT driver version
187 /// @param <RET>       \b eRETCODE :
188 /// @param <GLOBAL>    \b VER: Library version string
189 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_GetLibVer(const MSIF_Version ** ppVersion)190 WDT_Result MDrv_WDT_GetLibVer(const MSIF_Version **ppVersion)
191 {
192     WDT_DBG_FUNC();
193 
194     if (!ppVersion)
195         return E_WDT_FAIL;
196 
197     *ppVersion = &_drv_wdt_version;
198     return E_WDT_OK;
199 }
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 /// @brief \b Function  \b Name: MDrv_WDT_IsEnable
203 /// @brief \b Function  \b Description: check if WDT is Enable
204 /// @param <IN>         \b None:
205 /// @param <OUT>        \b None:
206 /// @param <RET>        \b TRUE: Initial FALSE: Not initial
207 /// @param <GLOBAL>     \b None :
208 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_IsEnable_U2K(void)209 WDT_Result MDrv_WDT_IsEnable_U2K(void)
210 {
211     WDT_DBG_FUNC();
212 
213     _gbInitWDT = HAL_WDT_IsEnable();
214 
215     if (!_gbInitWDT)
216         WDT_DBG_ERR("Call MDrv_WDT_Start first!\n");
217     return (WDT_Result)_gbInitWDT;
218 }
219 
MDrv_WDT_IsEnable(void)220 WDT_Result MDrv_WDT_IsEnable(void)
221 {
222 #if (WDT_UTOPIA20)
223 	WDT_Result enstatus;
224 	PWDT_IS_EN_PARAM pWdtIsEnParam = NULL;
225 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
226 		return 0;
227 	pWdtIsEnParam = (PWDT_IS_EN_PARAM)malloc(sizeof(WDT_IS_EN_PARAM));
228 	pWdtIsEnParam->status=0;
229 	UtopiaIoctl(pInstantWdt, MDrv_CMD_WDT_IsEnable,(MS_U32*)pWdtIsEnParam);
230 	enstatus = pWdtIsEnParam->status;
231 	free(pWdtIsEnParam);
232 	return (WDT_Result)enstatus;
233 #else
234 	return MDrv_WDT_IsEnable_U2K();
235 #endif
236 }
237 
238 
239 ////////////////////////////////////////////////////////////////////////////////
240 /// @brief \b Function  \b Name: MDrv_WDT_Init
241 /// @brief \b Function  \b Description: Init and Start WDT
242 /// @param <IN>         \b eLevel: E_WDT_DBGLV_NONE/E_WDT_DBGLV_ERR_ONLY/E_WDT_DBGLV_INFO/E_WDT_DBGLV_ALL
243 /// @param <OUT>        \b None:
244 /// @param <RET>        \b TRUE: Initial FALSE: Not initial
245 /// @param <GLOBAL>     \b None :
246 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_Init_U2K(WDT_DbgLv eLevel)247 WDT_Result MDrv_WDT_Init_U2K(WDT_DbgLv eLevel)
248 {
249     MS_VIRT virtBaseAddr;
250     MS_PHY u32BaseSize = 0; /* No use */
251 
252     _geDbfLevel = eLevel;
253     WDT_DBG_INFO("%s dbg level: %u\n", __FUNCTION__, eLevel);
254     /*  This is not really necessary, WDT is enabled after MCU reset
255         *   but it is important to get the IOMAP Base to access RIU.
256         */
257     if (!_gbInitWDT)
258     {
259         if(!MDrv_MMIO_GetBASE(&virtBaseAddr, &u32BaseSize, MS_MODULE_PM))
260         {
261             WDT_DBG_ERR("Get IOMAP Base faill!\n");
262             return E_WDT_FAIL;
263         }
264 
265         HAL_WDT_SetIOMapBase(virtBaseAddr);
266         HAL_WDT_Start();
267 
268         _gbInitWDT = TRUE;
269         return E_WDT_OK;
270     }
271     else
272     {
273         WDT_DBG_ERR("WDT had initial!\n");
274         return E_WDT_OK;
275     }
276 }
277 
MDrv_WDT_Init(WDT_DbgLv eLevel)278 WDT_Result MDrv_WDT_Init(WDT_DbgLv eLevel)
279 {
280 #if (WDT_UTOPIA20)
281 	MS_U32 u32Ret;
282 	PWDT_INIT_PARAM pWdtInitParam = NULL;
283 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
284 		return 0;
285 	pWdtInitParam = (PWDT_INIT_PARAM)malloc(sizeof(WDT_INIT_PARAM));
286 	pWdtInitParam->eLevel=eLevel;
287 	u32Ret = UtopiaIoctl(pInstantWdt,MDrv_CMD_WDT_Init,(MS_U32*)pWdtInitParam);
288 	free(pWdtInitParam);
289 	return (MS_BOOL)u32Ret;
290 #else
291 	return MDrv_WDT_Init_U2K(eLevel);
292 #endif
293 }
294 
295 
296 ////////////////////////////////////////////////////////////////////////////////
297 /// @brief \b Function  \b Name: MDrv_WDT_Stop
298 /// @brief \b Function  \b Description: Init WDT Stop
299 /// @param <IN>         \b eLevel: E_WDT_DBGLV_NONE/E_WDT_DBGLV_ERR_ONLY/E_WDT_DBGLV_INFO/E_WDT_DBGLV_ALL
300 /// @param <OUT>        \b None :
301 /// @param <RET>        \b WDT_Result
302 /// @param <GLOBAL>     \b None :
303 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_Stop_U2K(WDT_DbgLv eLevel)304 WDT_Result MDrv_WDT_Stop_U2K(WDT_DbgLv eLevel)
305 {
306     if (!_gbInitWDT)
307     {
308         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
309         return E_WDT_FAIL;
310     }
311 
312     WDT_Result eWDTRe = E_WDT_FAIL;
313     _geDbfLevel = eLevel;
314 
315     WDT_DBG_INFO("%s dbg level: %u\n", __FUNCTION__, eLevel);
316 
317     eWDTRe = (WDT_Result)HAL_WDT_Stop();
318 
319     return eWDTRe;
320 }
321 
MDrv_WDT_Stop(WDT_DbgLv eLevel)322 WDT_Result MDrv_WDT_Stop(WDT_DbgLv eLevel)
323 {
324 #if (WDT_UTOPIA20)
325 	MS_U32 u32Ret;
326 	PWDT_STOP_PARAM pWdtStopParam = NULL;
327 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
328 		return 0;
329 	pWdtStopParam = (PWDT_STOP_PARAM)malloc(sizeof(WDT_STOP_PARAM));
330 	pWdtStopParam->eLevel=eLevel;
331 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_WDT_Stop,(MS_U32*)pWdtStopParam);
332 	free(pWdtStopParam);
333 	return (MS_BOOL)u32Ret;
334 #else
335 	return MDrv_WDT_Stop_U2K(eLevel);
336 #endif
337 }
338 
339 
340 ////////////////////////////////////////////////////////////////////////////////
341 /// @brief \b Function  \b Name: MDrv_WDT_Clear
342 /// @brief \b Function  \b Description: clear WDT.
343 /// @param <IN>         \b None:
344 /// @param <OUT>        \b None:
345 /// @param <RET>        \b TRUE: Initial FALSE: Not initial
346 /// @param <GLOBAL>     \b None :
347 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_Clear_U2K(void)348 WDT_Result MDrv_WDT_Clear_U2K(void)
349 {
350     if (!_gbInitWDT)
351     {
352         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
353         return E_WDT_FAIL;
354     }
355 
356     WDT_Result eWDTRe = E_WDT_FAIL;
357 
358     eWDTRe = (WDT_Result)HAL_WDT_Clear();
359 
360     return eWDTRe;
361 }
362 
MDrv_WDT_Clear(void)363 WDT_Result MDrv_WDT_Clear(void)
364 {
365 #if (WDT_UTOPIA20)
366 	MS_U32 u32Ret;
367 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
368 		return 0;
369 	u32Ret = UtopiaIoctl(pInstantWdt,MDrv_CMD_WDT_Clear,(MS_U32*)NULL);
370 	return (WDT_Result)u32Ret;
371 #else
372 	return MDrv_WDT_Clear_U2K();
373 #endif
374 }
375 
376 ////////////////////////////////////////////////////////////////////////////////
377 /// @brief \b Function  \b Name: MDrv_WDT_ClearRstFlag
378 /// @brief \b Function  \b Description: clear WDT reset flag.
379 /// @param <IN>         \b None:
380 /// @param <OUT>        \b None:
381 /// @param <RET>        \b TRUE: Initial FALSE: Not initial
382 /// @param <GLOBAL>     \b None :
383 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_ClearRstFlag_U2K(void)384 WDT_Result MDrv_WDT_ClearRstFlag_U2K(void)
385 {
386     if (!_gbInitWDT)
387     {
388         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
389         return E_WDT_FAIL;
390     }
391 
392     WDT_Result eWDTRe = E_WDT_FAIL;
393 
394     eWDTRe = (WDT_Result)HAL_WDT_ClearRstFlag();
395 
396     return eWDTRe;
397 }
398 
MDrv_WDT_ClearRstFlag(void)399 WDT_Result MDrv_WDT_ClearRstFlag(void)
400 {
401 #if (WDT_UTOPIA20)
402 	MS_U32 u32Ret;
403 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
404 		return 0;
405 	u32Ret = UtopiaIoctl(pInstantWdt,MDrv_CMD_WDT_ClearRstFlag,(MS_U32*)NULL);
406 	return (WDT_Result)u32Ret;
407 #else
408 	return MDrv_WDT_ClearRstFlag_U2K();
409 #endif
410 }
411 
412 ////////////////////////////////////////////////////////////////////////////////
413 /// @brief \b Function  \b Name: MDrv_WDT_IsReset
414 /// @brief \b Function  \b Description: check if WDT is reset
415 /// @param <IN>         \b None:
416 /// @param <OUT>        \b None:
417 /// @param <RET>        \b TRUE: Initial FALSE: Not initial
418 /// @param <GLOBAL>     \b None :
419 ////////////////////////////////////////////////////////////////////////////////
420 
MDrv_WDT_IsReset_U2K(void)421 WDT_Result MDrv_WDT_IsReset_U2K(void)
422 {
423     if (!_gbInitWDT)
424     {
425         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
426         return E_WDT_FAIL;
427     }
428 
429     WDT_Result eWDTRe = E_WDT_FAIL;
430 
431     eWDTRe = (WDT_Result)HAL_WDT_IsReset();
432 
433     return eWDTRe;
434 }
435 
MDrv_WDT_IsReset(void)436 WDT_Result MDrv_WDT_IsReset(void)
437 {
438 #if (WDT_UTOPIA20)
439 	WDT_Result StatusVal;
440 	PWDT_IS_RST_PARAM pWdtIsRstParam = NULL;
441 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
442 		return 0;
443 	pWdtIsRstParam = (PWDT_IS_RST_PARAM)malloc(sizeof(WDT_IS_RST_PARAM));
444 	pWdtIsRstParam->status=0;
445 	UtopiaIoctl(pInstantWdt, MDrv_CMD_WDT_IsReset,(MS_U32*)pWdtIsRstParam);
446 	StatusVal = pWdtIsRstParam->status;
447 	free(pWdtIsRstParam);
448 	return (WDT_Result)StatusVal;
449 #else
450 	return MDrv_WDT_IsReset_U2K();
451 #endif
452 }
453 
454 ////////////////////////////////////////////////////////////////////////////////
455 /// @brief \b Function  \b Name: MDrv_WDT_SetTimer
456 /// @brief \b Function  \b Description: Set Timer
457 /// @param <IN>         \b eLevel: E_WDT_DBGLV_NONE/E_WDT_DBGLV_ERR_ONLY/E_WDT_DBGLV_INFO/E_WDT_DBGLV_ALL
458 /// @param <IN>         \b MS_U16: sec
459 /// @param <OUT>        \b None :
460 /// @param <RET>        \b WDT_Result
461 /// @param <GLOBAL>     \b None :
462 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_SetTimer_U2K(WDT_DbgLv eLevel,MS_U16 sec)463 WDT_Result MDrv_WDT_SetTimer_U2K(WDT_DbgLv eLevel, MS_U16 sec)
464 {
465     if (!_gbInitWDT)
466     {
467         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
468         return E_WDT_FAIL;
469     }
470 
471     WDT_Result eWDTRe = E_WDT_FAIL;
472     _geDbfLevel = eLevel;
473 
474     WDT_DBG_INFO("%s dbg level: %u\n", __FUNCTION__, eLevel);
475 
476     eWDTRe = (WDT_Result)HAL_WDT_SetTimer(sec);
477 
478     return eWDTRe;
479 }
480 
MDrv_WDT_SetTimer(WDT_DbgLv eLevel,MS_U16 sec)481 WDT_Result MDrv_WDT_SetTimer(WDT_DbgLv eLevel, MS_U16 sec)
482 {
483 #if (WDT_UTOPIA20)
484 	WDT_Result u32Ret;
485 	PWDT_SET_S_PARAM pWdtSetSecParam = NULL;
486 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
487 		return 0;
488 	pWdtSetSecParam = (PWDT_SET_S_PARAM)malloc(sizeof(WDT_SET_S_PARAM));
489 	pWdtSetSecParam->eLevel=eLevel;
490 	pWdtSetSecParam->sec=sec;
491 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_WDT_SetTimer,(MS_U32*)pWdtSetSecParam);
492 	free(pWdtSetSecParam);
493 	return (WDT_Result)u32Ret;
494 #else
495 	return MDrv_WDT_SetTimer_U2K(eLevel,sec);
496 #endif
497 }
498 
499 ////////////////////////////////////////////////////////////////////////////////
500 /// @brief \b Function  \b Name: MDrv_WDT_SetTimer
501 /// @brief \b Function  \b Description: Set Timer
502 /// @param <IN>         \b eLevel: E_WDT_DBGLV_NONE/E_WDT_DBGLV_ERR_ONLY/E_WDT_DBGLV_INFO/E_WDT_DBGLV_ALL
503 /// @param <IN>         \b MS_U16: msec
504 /// @param <OUT>        \b None :
505 /// @param <RET>        \b WDT_Result
506 /// @param <GLOBAL>     \b None :
507 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_SetTimer_ms_U2K(WDT_DbgLv eLevel,MS_U16 msec)508 WDT_Result MDrv_WDT_SetTimer_ms_U2K(WDT_DbgLv eLevel, MS_U16 msec)
509 {
510     if (!_gbInitWDT)
511     {
512         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
513         return E_WDT_FAIL;
514     }
515 
516     WDT_Result eWDTRe = E_WDT_FAIL;
517     _geDbfLevel = eLevel;
518 
519     WDT_DBG_INFO("%s dbg level: %u\n", __FUNCTION__, eLevel);
520 
521     eWDTRe = (WDT_Result)HAL_WDT_SetTimer_ms(msec);
522 
523     return eWDTRe;
524 }
525 
MDrv_WDT_SetTimer_ms(WDT_DbgLv eLevel,MS_U16 msec)526 WDT_Result MDrv_WDT_SetTimer_ms(WDT_DbgLv eLevel, MS_U16 msec)
527 {
528 #if (WDT_UTOPIA20)
529 	WDT_Result u32Ret;
530 	PWDT_SET_MS_PARAM pWdtSetMsParam = NULL;
531 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
532 		return 0;
533 	pWdtSetMsParam = (PWDT_SET_MS_PARAM)malloc(sizeof(WDT_SET_MS_PARAM));
534 	pWdtSetMsParam->eLevel=eLevel;
535 	pWdtSetMsParam->msec=msec;
536 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_WDT_SetTimer_ms,(MS_U32*)pWdtSetMsParam);
537 	free(pWdtSetMsParam);
538 	return (WDT_Result)u32Ret;
539 #else
540 	return MDrv_WDT_SetTimer_ms_U2K(eLevel,msec);
541 #endif
542 }
543 
544 ////////////////////////////////////////////////////////////////////////////////
545 /// @brief \b Function  \b Name: MDrv_WDT_SetTimer
546 /// @brief \b Function  \b Description: Set Timer
547 /// @param <IN>         \b eLevel: E_WDT_DBGLV_NONE/E_WDT_DBGLV_ERR_ONLY/E_WDT_DBGLV_INFO/E_WDT_DBGLV_ALL
548 /// @param <IN>         \b MS_U16: usec
549 /// @param <OUT>        \b None :
550 /// @param <RET>        \b WDT_Result
551 /// @param <GLOBAL>     \b None :
552 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_SetTimer_us_U2K(WDT_DbgLv eLevel,MS_U16 usec)553 WDT_Result MDrv_WDT_SetTimer_us_U2K(WDT_DbgLv eLevel, MS_U16 usec)
554 {
555     if (!_gbInitWDT)
556     {
557         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
558         return E_WDT_FAIL;
559     }
560 
561     WDT_Result eWDTRe = E_WDT_FAIL;
562     _geDbfLevel = eLevel;
563 
564     WDT_DBG_INFO("%s dbg level: %u\n", __FUNCTION__, eLevel);
565 
566     eWDTRe = (WDT_Result)HAL_WDT_SetTimer_us(usec);
567 
568     return eWDTRe;
569 }
570 
MDrv_WDT_SetTimer_us(WDT_DbgLv eLevel,MS_U16 usec)571 WDT_Result MDrv_WDT_SetTimer_us(WDT_DbgLv eLevel, MS_U16 usec)
572 {
573 #if (WDT_UTOPIA20)
574 	WDT_Result u32Ret;
575 	PWDT_SET_US_PARAM pWdtSetUsParam = NULL;
576 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
577 		return 0;
578 	pWdtSetUsParam = (PWDT_SET_US_PARAM)malloc(sizeof(WDT_SET_US_PARAM));
579 	pWdtSetUsParam->eLevel=eLevel;
580 	pWdtSetUsParam->usec=usec;
581 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_WDT_SetTimer_us,(MS_U32*)pWdtSetUsParam);
582 	free(pWdtSetUsParam);
583 	return (WDT_Result)u32Ret;
584 #else
585 	return MDrv_WDT_SetTimer_us_U2K(eLevel,usec);
586 #endif
587 }
588 
589 ////////////////////////////////////////////////////////////////////////////////
590 /// @brief \b Function  \b Name: MDrv_WDT_SetIntTimer
591 /// @brief \b Function  \b Description: Set INT Timer
592 /// @param <IN>         \b eLevel: E_WDT_DBGLV_NONE/E_WDT_DBGLV_ERR_ONLY/E_WDT_DBGLV_INFO/E_WDT_DBGLV_ALL
593 /// @param <IN>         \b MS_U16: sec
594 /// @param <OUT>        \b None :
595 /// @param <RET>        \b WDT_Result
596 /// @param <GLOBAL>     \b None :
597 ////////////////////////////////////////////////////////////////////////////////
MDrv_WDT_SetIntTimer_U2K(WDT_DbgLv eLevel,MS_U16 sec)598 WDT_Result MDrv_WDT_SetIntTimer_U2K(WDT_DbgLv eLevel, MS_U16 sec)
599 {
600     if (!_gbInitWDT)
601     {
602         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
603         return E_WDT_FAIL;
604     }
605 
606     WDT_Result eWDTRe = E_WDT_FAIL;
607     _geDbfLevel = eLevel;
608 
609     WDT_DBG_INFO("%s dbg level: %u\n", __FUNCTION__, eLevel);
610 
611     eWDTRe = (WDT_Result)HAL_WDT_SetIntTimer(sec);
612 
613     return eWDTRe;
614 }
615 
MDrv_WDT_SetIntTimer(WDT_DbgLv eLevel,MS_U16 sec)616 WDT_Result MDrv_WDT_SetIntTimer(WDT_DbgLv eLevel, MS_U16 sec)
617 {
618 #if (WDT_UTOPIA20)
619 	WDT_Result u32Ret;
620 	PWDT_SETTIMER_PARAM pWdtSetTimerParam = NULL;
621 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
622 		return 0;
623 	pWdtSetTimerParam = (PWDT_SETTIMER_PARAM)malloc(sizeof(WDT_SETTIMER_PARAM));
624 	pWdtSetTimerParam->eLevel=eLevel;
625 	pWdtSetTimerParam->sec=sec;
626 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_WDT_SetIntTimer,(MS_U32*)pWdtSetTimerParam);
627 	free(pWdtSetTimerParam);
628 	return (WDT_Result)u32Ret;
629 #else
630 	return MDrv_WDT_SetIntTimer_U2K(eLevel,sec);
631 #endif
632 }
633 
634 //------- ------------------------------------------------------------------------------------------
635 /// Description : Store and resume WDT initial status for fast booting.
636 /// @param  EN_POWER_MODE    \b IN: The mode of power on/off
637 /// @return TRUE : succeed
638 /// @return FALSE : failed
639 /// @return 1: Resume OK; 2: Suspend OK; 3: Suspend failed
640 //-------------------------------------------------------------------------------------------------
MDrv_WDT_SetPowerState(EN_POWER_MODE u16PowerState)641 MS_U16 MDrv_WDT_SetPowerState(EN_POWER_MODE u16PowerState)
642 {
643 	static EN_POWER_MODE _prev_u16PowerState = E_POWER_MECHANICAL;
644 	MS_U16 u16Return = FALSE;
645 
646 	if (u16PowerState == E_POWER_SUSPEND)
647 	{
648 		_prev_u16PowerState = u16PowerState;
649 		_gbInitWDT = FALSE;
650 		MDrv_TIMER_Exit();
651 		u16Return = 2;				// suspend OK
652 	}
653 	else if (u16PowerState == E_POWER_RESUME)
654 	{
655 		if (_prev_u16PowerState == E_POWER_SUSPEND)
656 		{
657 			MDrv_WDT_Init(E_WDT_DBGLV_ERR_ONLY);
658 			MDrv_TIMER_Init();
659 			_prev_u16PowerState = u16PowerState;
660             u16Return = 1;		// resume OK
661 		}
662 		else
663 		{
664             printf("[%s,%5d]It is not suspended yet. We shouldn't resume\n",__FUNCTION__,__LINE__);
665             u16Return = 3;		// resume failed
666 		}
667 	}
668 	else
669 	{
670         printf("[%s,%5d]Do Nothing: %d\n",__FUNCTION__,__LINE__,u16PowerState);
671         u16Return = FALSE;
672 	}
673 
674 	return u16Return;
675 }
676 // PIU TIMER
_MDrv_TIMER_Trigger(E_PIU_Timer eTimer,MS_U32 u32count)677 inline static void _MDrv_TIMER_Trigger(E_PIU_Timer eTimer, MS_U32 u32count)
678 {
679 	if ( eTimer == E_TIMER_0 )
680 	{
681 		HAL_WDT_Write4Byte(TIMER_0_MAX_REG,u32count);
682 		HAL_WDT_WriteByte(TIMER_0_CTRL_REG,TIMER_DISABLE);
683 		HAL_WDT_WriteByte(TIMER_0_CTRL_REG,HAL_WDT_ReadByte(TIMER_0_CTRL_REG)|TIMER_TRIGGER);
684 	}
685 	else
686 	{
687 		HAL_WDT_Write4Byte(TIMER_1_MAX_REG,u32count);
688 		HAL_WDT_WriteByte(TIMER_1_CTRL_REG,TIMER_DISABLE);
689 		HAL_WDT_WriteByte(TIMER_1_CTRL_REG,HAL_WDT_ReadByte(TIMER_1_CTRL_REG)|TIMER_TRIGGER);
690 	}
691 	while(!MDrv_TIMER_HitMaxMatch(eTimer));
692 }
693 
694 #if !defined(CONFIG_FRC)
MDrv_TIMER_Stop(E_PIU_Timer eTimer)695 static void MDrv_TIMER_Stop(E_PIU_Timer eTimer)
696 {
697     if ( eTimer == E_TIMER_0 )
698     {
699         HAL_WDT_WriteByte(TIMER_0_CTRL_REG,HAL_WDT_ReadByte(TIMER_0_CTRL_REG)&(~TIMER_ENABLE));
700         HAL_WDT_WriteByte(TIMER_0_MATCH_REG,HAL_WDT_ReadByte(TIMER_0_MATCH_REG)|(TIMER_MATCH));
701     }
702     else
703     {
704         HAL_WDT_WriteByte(TIMER_1_CTRL_REG,HAL_WDT_ReadByte(TIMER_1_CTRL_REG)&(~TIMER_ENABLE));
705         HAL_WDT_WriteByte(TIMER_1_MATCH_REG,HAL_WDT_ReadByte(TIMER_1_MATCH_REG)|(TIMER_MATCH));
706     }
707 }
708 #endif
709 
MDrv_TIMER_Count_U2K(E_PIU_Timer eTimer,MS_BOOL bEnable)710 void MDrv_TIMER_Count_U2K(E_PIU_Timer eTimer, MS_BOOL bEnable)
711 {
712     if (!_gbInitWDT)
713     {
714         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
715         return;
716     }
717 
718     tmr_interrupt *ptmr;
719 
720     ptmr = &tTmrTbl[eTimer];
721 	ptmr->bTmrEn = bEnable;
722 
723 	if ( eTimer == E_TIMER_0 )
724 	{
725 		if(bEnable)
726 			HAL_WDT_WriteByte(TIMER_0_CTRL_REG,HAL_WDT_ReadByte(TIMER_0_CTRL_REG)|TIMER_ENABLE);
727 		else
728 			HAL_WDT_WriteByte(TIMER_0_CTRL_REG,HAL_WDT_ReadByte(TIMER_0_CTRL_REG)&(~TIMER_ENABLE));
729 	}
730 	else
731 	{
732 		if(bEnable)
733 			HAL_WDT_WriteByte(TIMER_1_CTRL_REG,HAL_WDT_ReadByte(TIMER_1_CTRL_REG)|TIMER_ENABLE);
734 		else
735 			HAL_WDT_WriteByte(TIMER_1_CTRL_REG,HAL_WDT_ReadByte(TIMER_1_CTRL_REG)&(~TIMER_ENABLE));
736 	}
737 }
738 
MDrv_TIMER_Count(E_PIU_Timer eTimer,MS_BOOL bEnable)739 void MDrv_TIMER_Count(E_PIU_Timer eTimer, MS_BOOL bEnable)
740 {
741 #if (WDT_UTOPIA20)
742 	WDT_Result u32Ret;
743 	PTIMER_COUNT_PARAM pTimerCntrParam = NULL;
744 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
745 		printf("TIMER_Count Open Fail \n");
746 	pTimerCntrParam = (PTIMER_COUNT_PARAM)malloc(sizeof(TIMER_COUNT_PARAM));
747 	pTimerCntrParam->eTimer=eTimer;
748 	pTimerCntrParam->bEnable=bEnable;
749 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_Count,(MS_U32*)pTimerCntrParam);
750 	free(pTimerCntrParam);
751 #else
752 	MDrv_TIMER_Count_U2K(eTimer,bEnable);
753 #endif
754 }
755 
MDrv_TIMER_INT_U2K(E_PIU_Timer eTimer,MS_BOOL bEnable)756 void MDrv_TIMER_INT_U2K(E_PIU_Timer eTimer, MS_BOOL bEnable)
757 {
758     if (!_gbInitWDT)
759     {
760         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
761         return;
762     }
763 
764 	if ( eTimer == E_TIMER_0 )
765 	{
766 		if(bEnable)
767 			HAL_WDT_WriteByte(TIMER_0_CTRL_REG+1,HAL_WDT_ReadByte(TIMER_0_CTRL_REG+1)|TIMER_INTEN);
768 		else
769 			HAL_WDT_WriteByte(TIMER_0_CTRL_REG+1,HAL_WDT_ReadByte(TIMER_0_CTRL_REG+1)&(~TIMER_INTEN));
770 	}
771 	else
772 	{
773 		if(bEnable)
774 			HAL_WDT_WriteByte(TIMER_1_CTRL_REG+1,HAL_WDT_ReadByte(TIMER_1_CTRL_REG+1)|TIMER_INTEN);
775 		else
776 			HAL_WDT_WriteByte(TIMER_1_CTRL_REG+1,HAL_WDT_ReadByte(TIMER_1_CTRL_REG+1)&(~TIMER_INTEN));
777 	}
778 }
779 
MDrv_TIMER_INT(E_PIU_Timer eTimer,MS_BOOL bEnable)780 void MDrv_TIMER_INT(E_PIU_Timer eTimer, MS_BOOL bEnable)
781 {
782 #if (WDT_UTOPIA20)
783 	PTIMER_INT_PARAM pTimerInitParam = NULL;
784 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
785 		printf("Timer INT Open Fail \n");
786 	pTimerInitParam = (PTIMER_INT_PARAM)malloc(sizeof(TIMER_INT_PARAM));
787 	pTimerInitParam->eTimer=eTimer;
788 	pTimerInitParam->bEnable=bEnable;
789 	UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_Init,(MS_U32*)pTimerInitParam);
790 	free(pTimerInitParam);
791 #else
792 	MDrv_TIMER_INT_U2K(eTimer,bEnable);
793 #endif
794 }
795 
MDrv_TIMER_HitMaxMatch_U2K(E_PIU_Timer eTimer)796 MS_BOOL MDrv_TIMER_HitMaxMatch_U2K(E_PIU_Timer eTimer)
797 {
798     if (!_gbInitWDT)
799     {
800         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
801         return FALSE;
802     }
803 
804 	if ( eTimer == E_TIMER_0 )
805 	{
806 		return (HAL_WDT_ReadByte(TIMER_0_MATCH_REG)&TIMER_MATCH);
807 	}
808 	else
809 	{
810 		return (HAL_WDT_ReadByte(TIMER_1_MATCH_REG)&TIMER_MATCH);
811 	}
812 }
813 
MDrv_TIMER_HitMaxMatch(E_PIU_Timer eTimer)814 MS_BOOL MDrv_TIMER_HitMaxMatch(E_PIU_Timer eTimer)
815 {
816 #if (WDT_UTOPIA20)
817 	MS_BOOL u32Ret;
818 	PTIMER_HIT_MAX_MATCH_PARAM pTimerHitMaxMatchParam = NULL;
819 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
820 		return FALSE;
821 	pTimerHitMaxMatchParam = (PTIMER_HIT_MAX_MATCH_PARAM)malloc(sizeof(TIMER_HIT_MAX_MATCH_PARAM));
822 	pTimerHitMaxMatchParam->eTimer=eTimer;
823 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_HitMaxMatch,(MS_U32*)pTimerHitMaxMatchParam);
824 	free(pTimerHitMaxMatchParam);
825 	return (MS_BOOL)u32Ret;
826 #else
827 	return MDrv_TIMER_HitMaxMatch_U2K(eTimer);
828 #endif
829 }
830 
MDrv_TIMER_Rst_U2K(E_PIU_Timer eTimer)831 void MDrv_TIMER_Rst_U2K(E_PIU_Timer eTimer)
832 {
833     if (!_gbInitWDT)
834     {
835         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
836         return;
837     }
838 
839 	if ( eTimer == E_TIMER_0 )
840 		HAL_WDT_WriteByte(TIMER_0_CTRL_REG,HAL_WDT_ReadByte(TIMER_0_CTRL_REG)|TIMER_ENABLE);
841 	else
842 		HAL_WDT_WriteByte(TIMER_1_CTRL_REG,HAL_WDT_ReadByte(TIMER_1_CTRL_REG)|TIMER_ENABLE);
843 }
844 
MDrv_TIMER_Rst(E_PIU_Timer eTimer)845 void MDrv_TIMER_Rst(E_PIU_Timer eTimer)
846 {
847 #if (WDT_UTOPIA20)
848 	PTIMER_RST_PARAM pTimerRstParam = NULL;
849 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
850 		printf("TIMER_Rst Open Fail \n");
851 	pTimerRstParam = (PTIMER_RST_PARAM)malloc(sizeof(TIMER_RST_PARAM));
852 	pTimerRstParam->eTimer=eTimer;
853 	UtopiaIoctl(pInstantWdt,  MDrv_CMD_TIMER_Rst,(MS_U32*)pTimerRstParam);
854 	free(pTimerRstParam);
855 #else
856 	MDrv_TIMER_Rst_U2K(eTimer);
857 #endif
858 }
859 
MDrv_TIMER_SetMaxMatch_U2K(E_PIU_Timer eTimer,MS_U32 u32MaxTimer)860 void MDrv_TIMER_SetMaxMatch_U2K(E_PIU_Timer eTimer, MS_U32 u32MaxTimer)
861 {
862     if (!_gbInitWDT)
863     {
864         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
865         return;
866     }
867 
868     tmr_interrupt *ptmr;
869 
870     ptmr = &tTmrTbl[eTimer];
871 	ptmr->u32TmrMax = u32MaxTimer;
872 
873 	if ( eTimer == E_TIMER_0 )
874 		HAL_WDT_Write4Byte(TIMER_0_MAX_REG, u32MaxTimer);
875 	else
876 		HAL_WDT_Write4Byte(TIMER_1_MAX_REG, u32MaxTimer);
877 }
878 
MDrv_TIMER_SetMaxMatch(E_PIU_Timer eTimer,MS_U32 u32MaxTimer)879 void MDrv_TIMER_SetMaxMatch(E_PIU_Timer eTimer, MS_U32 u32MaxTimer)
880 {
881 #if (WDT_UTOPIA20)
882 	PTIMER_SET_MAX_MATCH_PARAM pTimerSetMaxMatchParam = NULL;
883 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
884 		printf("TIMER_SetMaxMatch Open Fail \n");
885 	pTimerSetMaxMatchParam = (PTIMER_SET_MAX_MATCH_PARAM)malloc(sizeof(TIMER_SET_MAX_MATCH_PARAM));
886 	pTimerSetMaxMatchParam->eTimer=eTimer;
887 	pTimerSetMaxMatchParam->u32MaxTimer=u32MaxTimer;
888 	UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_SetMaxMatch,(MS_U32*)pTimerSetMaxMatchParam);
889 	free(pTimerSetMaxMatchParam);
890 #else
891 	MDrv_TIMER_SetMaxMatch_U2K(eTimer,u32MaxTimer);
892 #endif
893 }
894 
MDrv_TIMER_GetMaxMatch_U2K(E_PIU_Timer eTimer)895 MS_U32 MDrv_TIMER_GetMaxMatch_U2K(E_PIU_Timer eTimer)
896 {
897     if (!_gbInitWDT)
898     {
899         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
900         return 0;
901     }
902 
903 	if ( eTimer == E_TIMER_0 )
904 		return (HAL_WDT_Read4Byte(TIMER_0_MAX_REG));
905 	else
906 		return (HAL_WDT_Read4Byte(TIMER_1_MAX_REG));
907 }
908 
MDrv_TIMER_GetMaxMatch(E_PIU_Timer eTimer)909 MS_U32 MDrv_TIMER_GetMaxMatch(E_PIU_Timer eTimer)
910 {
911 #if (WDT_UTOPIA20)
912 	MS_BOOL u32Ret;
913 	MS_U32 u32MaxMatchVal;
914 	PTIMER_GET_MAX_MATCH_PARAM pTimerGetMaxMatchParam = NULL;
915 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
916 		return 0xFFFFFFFF;
917 	pTimerGetMaxMatchParam = (PTIMER_GET_MAX_MATCH_PARAM)malloc(sizeof(TIMER_GET_MAX_MATCH_PARAM));
918 	pTimerGetMaxMatchParam->eTimer=eTimer;
919 	pTimerGetMaxMatchParam->u32MaxMatchVal=0;
920 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_GetMaxMatch,(MS_U32*)pTimerGetMaxMatchParam);
921 	if(u32Ret!= 0)
922 	{
923 		free(pTimerGetMaxMatchParam);
924 		return 0xFFFFFFFF;
925 	}
926 	u32MaxMatchVal = pTimerGetMaxMatchParam->u32MaxMatchVal;
927 	free(pTimerGetMaxMatchParam);
928 	return (MS_U32)u32MaxMatchVal;
929 #else
930 	return MDrv_TIMER_GetMaxMatch_U2K(eTimer);
931 #endif
932 }
933 
MDrv_TIMER_GetCounter_U2K(E_PIU_Timer eTimer)934 MS_U32 MDrv_TIMER_GetCounter_U2K(E_PIU_Timer eTimer)
935 {
936     if (!_gbInitWDT)
937     {
938         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
939         return 0;
940     }
941 
942 	if ( eTimer == E_TIMER_0 )
943 		return (HAL_WDT_Read4Byte(TIMER_0_COUNT_REG));
944 	else
945 		return (HAL_WDT_Read4Byte(TIMER_1_COUNT_REG));
946 }
947 
MDrv_TIMER_GetCounter(E_PIU_Timer eTimer)948 MS_U32 MDrv_TIMER_GetCounter(E_PIU_Timer eTimer)
949 {
950 #if (WDT_UTOPIA20)
951 	MS_U32 u32Ret;
952 	MS_U32 u32cntval;
953 	PTIMER_GET_COUNTER_PARAM pTimerGetCntParam = NULL;
954 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
955 		return 0xFFFFFFFF;
956 	pTimerGetCntParam = (PTIMER_GET_COUNTER_PARAM)malloc(sizeof(TIMER_GET_COUNTER_PARAM));
957 	pTimerGetCntParam->eTimer=eTimer;
958 	pTimerGetCntParam->u32Cnt=0;
959 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_GetCounter,(MS_U32*)pTimerGetCntParam);
960 	if(u32Ret!= 0)
961 	{
962 		free(pTimerGetCntParam);
963 		return 0xFFFFFFFF;
964 	}
965 	u32cntval = pTimerGetCntParam->u32Cnt;
966 	free(pTimerGetCntParam);
967 	return (MS_U32)u32cntval;
968 #else
969 	return MDrv_TIMER_GetCounter_U2K(eTimer);
970 #endif
971 }
972 
MDrv_TIMER_GetSecond_U2K(E_PIU_Timer eTimer)973 MS_U32 MDrv_TIMER_GetSecond_U2K(E_PIU_Timer eTimer)
974 {
975     if (!_gbInitWDT)
976     {
977         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
978         return 0;
979     }
980 
981 	if ( eTimer == E_TIMER_0 )
982 		return (HAL_WDT_Read4Byte(TIMER_0_COUNT_REG)/MST_XTAL_CLOCK_HZ);
983 	else
984 		return (HAL_WDT_Read4Byte(TIMER_1_COUNT_REG)/MST_XTAL_CLOCK_HZ);
985 }
986 
MDrv_TIMER_GetSecond(E_PIU_Timer eTimer)987 MS_U32 MDrv_TIMER_GetSecond(E_PIU_Timer eTimer)
988 {
989 #if (WDT_UTOPIA20)
990 	MS_U32 u32Ret;
991 	MS_U32 u32SecVal;
992 	PTIMER_GET_SEC_PARAM pTimerSecParam = NULL;
993 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
994 		return 0xFFFFFFFF;
995 	pTimerSecParam = (PTIMER_GET_SEC_PARAM)malloc(sizeof(TIMER_GET_SEC_PARAM));
996 	pTimerSecParam->eTimer=eTimer;
997 	pTimerSecParam->u32Sec=0;
998 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_GetSecond,(MS_U32*)pTimerSecParam);
999 	if(u32Ret!= 0)
1000 	{
1001 		free(pTimerSecParam);
1002 		return 0xFFFFFFFF;
1003 	}
1004 	u32SecVal = pTimerSecParam->u32Sec;
1005 	free(pTimerSecParam);
1006 	return (MS_U32)u32SecVal;
1007 #else
1008 	return MDrv_TIMER_GetSecond_U2K(eTimer);
1009 #endif
1010 }
1011 
MDrv_TIMER_GetMs_U2K(E_PIU_Timer eTimer)1012 MS_U32 MDrv_TIMER_GetMs_U2K(E_PIU_Timer eTimer)
1013 {
1014     if (!_gbInitWDT)
1015     {
1016         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
1017         return 0;
1018     }
1019 
1020 	if ( eTimer == E_TIMER_0 )
1021 		return (HAL_WDT_Read4Byte(TIMER_0_COUNT_REG)/MST_XTAL_CLOCK_KHZ);
1022 	else
1023 		return (HAL_WDT_Read4Byte(TIMER_1_COUNT_REG)/MST_XTAL_CLOCK_KHZ);
1024 }
1025 
MDrv_TIMER_GetMs(E_PIU_Timer eTimer)1026 MS_U32 MDrv_TIMER_GetMs(E_PIU_Timer eTimer)
1027 {
1028 #if (WDT_UTOPIA20)
1029 	MS_U32 u32Ret;
1030 	MS_U32 u32MsecVal;
1031 	PTIMER_GET_MS_PARAM pTimerMSecParam = NULL;
1032 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1033 		return 0xFFFFFFFF;
1034 	pTimerMSecParam = (PTIMER_GET_MS_PARAM)malloc(sizeof(TIMER_GET_MS_PARAM));
1035 	pTimerMSecParam->eTimer=eTimer;
1036 	pTimerMSecParam->u32Msec=0;
1037 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_GetMs,(MS_U32*)pTimerMSecParam);
1038 	if(u32Ret!= 0)
1039 	{
1040 		free(pTimerMSecParam);
1041 		return 0xFFFFFFFF;
1042 	}
1043 	u32MsecVal = pTimerMSecParam->u32Msec;
1044 	free(pTimerMSecParam);
1045 	return (MS_U32)u32MsecVal;
1046 #else
1047 	return MDrv_TIMER_GetMs_U2K(eTimer);
1048 #endif
1049 }
1050 
MDrv_TIMER_GetUs_U2K(E_PIU_Timer eTimer)1051 MS_U32 MDrv_TIMER_GetUs_U2K(E_PIU_Timer eTimer)
1052 {
1053     if (!_gbInitWDT)
1054     {
1055         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
1056         return 0;
1057     }
1058 
1059 	if ( eTimer == E_TIMER_0 )
1060 		return (HAL_WDT_Read4Byte(TIMER_0_COUNT_REG)/MST_XTAL_CLOCK_MHZ);
1061 	else
1062 		return (HAL_WDT_Read4Byte(TIMER_1_COUNT_REG)/MST_XTAL_CLOCK_MHZ);
1063 }
1064 
MDrv_TIMER_GetUs(E_PIU_Timer eTimer)1065 MS_U32 MDrv_TIMER_GetUs(E_PIU_Timer eTimer)
1066 {
1067 #if (WDT_UTOPIA20)
1068 	MS_U32 u32Ret;
1069 	MS_U32 u32USecVal;
1070 	PTIMER_GET_US_PARAM pTimerUSecParam = NULL;
1071 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1072 		return 0xFFFFFFFF;
1073 	pTimerUSecParam = (PTIMER_GET_US_PARAM)malloc(sizeof(TIMER_GET_US_PARAM));
1074 	pTimerUSecParam->eTimer=eTimer;
1075 	pTimerUSecParam->u32Usec=0;
1076 	u32Ret = UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_GetUs,(MS_U32*)pTimerUSecParam);
1077 	if(u32Ret!= 0)
1078 	{
1079 		free(pTimerUSecParam);
1080 		return 0xFFFFFFFF;
1081 	}
1082 	u32USecVal = pTimerUSecParam->u32Usec;
1083 	free(pTimerUSecParam);
1084 	return (MS_U32)u32USecVal;
1085 #else
1086 	return MDrv_TIMER_GetUs_U2K(eTimer);
1087 #endif
1088 }
1089 
MDrv_TIMER_Delay_U2K(E_PIU_Timer eTimer,MS_U32 u32Second)1090 void MDrv_TIMER_Delay_U2K(E_PIU_Timer eTimer, MS_U32 u32Second)//@@@need restore status
1091 {
1092     if (!_gbInitWDT)
1093     {
1094         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
1095         return;
1096     }
1097 
1098     MS_U32 u32Salt = HAL_WDTCycles(u32Second);
1099 	MS_U32 u32Bkup = MDrv_TIMER_GetMaxMatch(eTimer);
1100 
1101 	_MDrv_TIMER_Trigger(eTimer,u32Salt);
1102 	MDrv_TIMER_SetMaxMatch(eTimer,u32Bkup);
1103 	MDrv_TIMER_Count(eTimer, ENABLE);
1104 }
1105 
MDrv_TIMER_Delay(E_PIU_Timer eTimer,MS_U32 u32Second)1106 void MDrv_TIMER_Delay(E_PIU_Timer eTimer, MS_U32 u32Second)
1107 {
1108 #if (WDT_UTOPIA20)
1109 	PTIMER_DELAY_PARAM pTimerDelayParam = NULL;
1110 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1111 		printf("TIMER_Delay Open Fail \n");
1112 	pTimerDelayParam = (PTIMER_DELAY_PARAM)malloc(sizeof(TIMER_DELAY_PARAM));
1113 	pTimerDelayParam->eTimer=eTimer;
1114 	pTimerDelayParam->u32Second=u32Second;
1115 	UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_Delay,(MS_U32*)pTimerDelayParam);
1116 	free(pTimerDelayParam);
1117 #else
1118 	MDrv_TIMER_Delay_U2K(eTimer,u32Second);
1119 #endif
1120 }
1121 
MDrv_TIMER_DelayMs_U2K(E_PIU_Timer eTimer,MS_U32 u32MS)1122 void MDrv_TIMER_DelayMs_U2K(E_PIU_Timer eTimer, MS_U32 u32MS)//@@@need restore status
1123 {
1124     if (!_gbInitWDT)
1125     {
1126         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
1127         return;
1128     }
1129 
1130 	MS_U32 u32Salt = HAL_WDTCyclesM(u32MS);
1131 	MS_U32 u32Bkup = MDrv_TIMER_GetMaxMatch(eTimer);
1132 
1133 	_MDrv_TIMER_Trigger(eTimer,u32Salt);
1134 	MDrv_TIMER_SetMaxMatch(eTimer,u32Bkup);
1135 	MDrv_TIMER_Count(eTimer, ENABLE);
1136 }
1137 
MDrv_TIMER_DelayMs(E_PIU_Timer eTimer,MS_U32 u32MS)1138 void MDrv_TIMER_DelayMs(E_PIU_Timer eTimer, MS_U32 u32MS)
1139 {
1140 #if (WDT_UTOPIA20)
1141 	PTIMER_DELAY_MS_PARAM pTimerDelayMsParam = NULL;
1142 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1143 		printf("TIMER_DelayMs Open Fail \n");
1144 	pTimerDelayMsParam = (PTIMER_DELAY_MS_PARAM)malloc(sizeof(TIMER_DELAY_MS_PARAM));
1145 	pTimerDelayMsParam->eTimer=eTimer;
1146 	pTimerDelayMsParam->u32ms=u32MS;
1147 	UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_DelayMs,(MS_U32*)pTimerDelayMsParam);
1148 	free(pTimerDelayMsParam);
1149 #else
1150 	MDrv_TIMER_DelayMs_U2K(eTimer,u32MS);
1151 #endif
1152 }
1153 
MDrv_TIMER_DelayUs_U2K(E_PIU_Timer eTimer,MS_U32 u32US)1154 void MDrv_TIMER_DelayUs_U2K(E_PIU_Timer eTimer, MS_U32 u32US)//@@@need restore status
1155 {
1156     if (!_gbInitWDT)
1157     {
1158         WDT_DBG_ERR("%s is called before init\n", __FUNCTION__);
1159         return;
1160     }
1161 
1162     printf("TIMER_DelayUs unSupported!! \n");
1163 
1164     if (0)
1165     {
1166       MS_U32 u32Salt = HAL_WDTCyclesU(u32US);
1167       MS_U32 u32Bkup = MDrv_TIMER_GetMaxMatch(eTimer);
1168 
1169       _MDrv_TIMER_Trigger(eTimer,u32Salt);
1170       MDrv_TIMER_SetMaxMatch(eTimer,u32Bkup);
1171       MDrv_TIMER_Count(eTimer, ENABLE);
1172     }
1173 
1174 }
1175 
MDrv_TIMER_DelayUs(E_PIU_Timer eTimer,MS_U32 u32US)1176 void MDrv_TIMER_DelayUs(E_PIU_Timer eTimer, MS_U32 u32US)
1177 {
1178 #if (WDT_UTOPIA20)
1179 	PTIMER_DELAY_US_PARAM pTimerDelayUsParam = NULL;
1180 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1181 		printf("TIMER_DelayUs Open Fail \n");
1182 	pTimerDelayUsParam = (PTIMER_DELAY_US_PARAM)malloc(sizeof(TIMER_DELAY_US_PARAM));
1183 	pTimerDelayUsParam->eTimer=eTimer;
1184 	pTimerDelayUsParam->u32us=u32US;
1185 	UtopiaIoctl(pInstantWdt, MDrv_CMD_TIMER_DelayUs,(MS_U32*)pTimerDelayUsParam);
1186 	free(pTimerDelayUsParam);
1187 #else
1188 	MDrv_TIMER_DelayUs_U2K(eTimer,u32US);
1189 #endif
1190 }
1191 
MDrv_TIMER_Init_U2K(void)1192 void MDrv_TIMER_Init_U2K(void)
1193 {
1194     MS_VIRT virtBaseAddr;
1195     MS_PHY u32BaseSize = 0, idx;
1196     tmr_interrupt *ptmr;
1197 
1198     // Clear & disable all timers
1199     ptmr = &tTmrTbl[0];
1200     for( idx = 0; idx < MAX_TIMER_NUM; idx++ )
1201     {
1202 		ptmr->bTmrEn = FALSE;
1203 		ptmr->u32TmrInit = 0x00000000;
1204 		ptmr->u32TmrMax = 0xFFFFFFFF;
1205 		ptmr->TmrFnct = NULL;
1206 		ptmr++;
1207     }
1208 
1209     // Get Memory Base
1210     if (!_gbInitWDT)
1211     {
1212         if(!MDrv_MMIO_GetBASE(&virtBaseAddr, &u32BaseSize, MS_MODULE_PM))
1213         {
1214             WDT_DBG_ERR("Get IOMAP Base faill!\n");
1215             return;
1216         }
1217 
1218         HAL_WDT_SetIOMapBase(virtBaseAddr);
1219 
1220         _gbInitWDT = TRUE;
1221         return;
1222     }
1223     else
1224     {
1225         WDT_DBG_ERR("PIU timer had initial!\n");
1226         return;
1227     }
1228 }
1229 
MDrv_TIMER_Init(void)1230 void MDrv_TIMER_Init(void)
1231 {
1232 #if (WDT_UTOPIA20)
1233 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1234 		printf("Timer Init Fail \n");
1235 	UtopiaIoctl(pInstantWdt,MDrv_CMD_TIMER_Initial,(MS_U32*)NULL);
1236 #else
1237 	return MDrv_TIMER_Init_U2K();
1238 #endif
1239 }
1240 
1241 #if !defined(CONFIG_FRC)
DRV_TIMER0_PIU_Isr(void)1242 static void DRV_TIMER0_PIU_Isr(void)
1243 {
1244 	tmr_interrupt *ptmr;
1245 
1246 	ptmr = &tTmrTbl[E_TIMER_0];
1247 
1248 	if(MDrv_TIMER_HitMaxMatch(E_TIMER_0))
1249 	{
1250             WDT_DBG_INFO("%s(%d) =======> comes\n", __FUNCTION__ ,__LINE__);
1251             MDrv_TIMER_Stop(E_TIMER_0);
1252             ptmr->TmrFnct(ptmr->TmrFnctArg0,ptmr->TmrFnctArg1,ptmr->TmrFnctArg2);
1253             // restore tmr default
1254             //MDrv_TIMER_Rst(E_TIMER_0);
1255 	}
1256 }
DRV_TIMER1_PIU_Isr(void)1257 static void DRV_TIMER1_PIU_Isr(void)
1258 {
1259  	tmr_interrupt *ptmr;
1260 
1261 	ptmr = &tTmrTbl[E_TIMER_1];
1262 
1263 	if(MDrv_TIMER_HitMaxMatch(E_TIMER_1))
1264 	{
1265             WDT_DBG_INFO("%s(%d) =======> comes\n", __FUNCTION__ ,__LINE__);
1266             MDrv_TIMER_Stop(E_TIMER_1);
1267             ptmr->TmrFnct(ptmr->TmrFnctArg0,ptmr->TmrFnctArg1,ptmr->TmrFnctArg2);
1268             // restore tmr default
1269             //MDrv_TIMER_Rst(E_TIMER_1);
1270 	}
1271 }
1272 #endif
1273 
MDrv_TIMER_CfgFnct(E_PIU_Timer eTimer,void (* fnct)(void *,void *,void *),void * arg0,void * arg1,void * arg2)1274 void MDrv_TIMER_CfgFnct(E_PIU_Timer eTimer, void (*fnct)(void *, void *, void *), void *arg0, void *arg1, void *arg2 )
1275 {
1276 	tmr_interrupt *ptmr;
1277 
1278 	if( fnct )
1279 	{
1280 		if( eTimer < MAX_TIMER_NUM )
1281 		{
1282 			ptmr = &tTmrTbl[eTimer];
1283 			//OS_ENTER_CRITICAL();
1284 			ptmr->TmrFnct		= fnct;
1285 			ptmr->TmrFnctArg0 	= arg0;
1286 			ptmr->TmrFnctArg1 	= arg1;
1287 			ptmr->TmrFnctArg2	= arg2;
1288 			//OS_EXIT_CRITICAL();
1289 			MDrv_TIMER_INT(eTimer, 1);
1290 #if !defined(CONFIG_FRC)
1291 			if( eTimer )
1292 			{
1293 				 MsOS_AttachInterrupt(E_INT_FIQ_EXTIMER1, (InterruptCb)DRV_TIMER1_PIU_Isr);
1294         			 MsOS_EnableInterrupt(E_INT_FIQ_EXTIMER1);
1295 			}
1296 			else
1297 			{
1298 				 MsOS_AttachInterrupt(E_INT_FIQ_EXTIMER0, (InterruptCb)DRV_TIMER0_PIU_Isr);
1299         			 MsOS_EnableInterrupt(E_INT_FIQ_EXTIMER0);
1300 			}
1301 #endif
1302 		}
1303 	}
1304 }
1305 
MDrv_TIMER_Exit_U2K(void)1306 void MDrv_TIMER_Exit_U2K(void)
1307 {
1308     MS_U32 idx;
1309     tmr_interrupt *ptmr;
1310 
1311     // Clear & disable all timers
1312     for( idx = 0; idx < MAX_TIMER_NUM; idx++ )
1313     {
1314         ptmr = &tTmrTbl[idx];
1315 		ptmr->bTmrEn = FALSE;
1316 		ptmr->u32TmrInit = 0x00000000;
1317 		ptmr->u32TmrMax = 0xFFFFFFFF;
1318 		ptmr->TmrFnct = NULL;
1319 		MDrv_TIMER_Count((E_PIU_Timer) idx, 0);
1320 		MDrv_TIMER_SetMaxMatch((E_PIU_Timer) idx, ptmr->u32TmrMax);
1321 		MDrv_TIMER_INT((E_PIU_Timer) idx, 0);
1322     }
1323 }
1324 
MDrv_TIMER_Exit(void)1325 void MDrv_TIMER_Exit(void)
1326 {
1327 #if (WDT_UTOPIA20)
1328 	if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1329 		printf("Timer Exit Open Fail \n");
1330 	UtopiaIoctl(pInstantWdt,MDrv_CMD_TIMER_Exit,(MS_U32*)NULL);
1331 #else
1332 	return MDrv_TIMER_Exit_U2K();
1333 #endif
1334 }
1335 
MDrv_WDT_GetConfig(WDT_DbgLv * eLevel)1336 WDT_Result MDrv_WDT_GetConfig(WDT_DbgLv* eLevel)
1337 {
1338 	MS_U32 u32Ret=0;
1339 #if (WDT_UTOPIA20)
1340 	  if (FALSE == _MDrv_WDT_CheckUtopia20Open(&pInstantWdt, 0, pAttributeWdt))
1341 		  return 0;
1342 	  u32Ret = UtopiaIoctl(pInstantWdt,MDrv_CMD_WDT_GetConfig,eLevel);
1343 #else
1344 #endif
1345   return (WDT_Result)u32Ret;
1346 }