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 /// @file halWDT.h
97 /// @author MStar Semiconductor Inc.
98 /// @brief Piu Watch Dog Timer hal
99 ////////////////////////////////////////////////////////////////////////////////
100
101 ////////////////////////////////////////////////////////////////////////////////
102 // Include Files
103 ////////////////////////////////////////////////////////////////////////////////
104 #include "MsCommon.h"
105 #include "MsTypes.h"
106 #include "halWDT.h"
107 #include "regWDT.h"
108
109 ////////////////////////////////////////////////////////////////////////////////
110 // Driver Compiler Options
111 ////////////////////////////////////////////////////////////////////////////////
112
113 ////////////////////////////////////////////////////////////////////////////////
114 // Global Variables
115 ////////////////////////////////////////////////////////////////////////////////
116 static MS_VIRT _gMIO_MapBase = 0;
117
118 ////////////////////////////////////////////////////////////////////////////////
119 // Global Functions
120 ////////////////////////////////////////////////////////////////////////////////
121
122 ////////////////////////////////////////////////////////////////////////////////
123 /// @brief \b Function \b Name: HAL_WDT_ReadByte
124 /// @brief \b Function \b Description: read 1 Byte data
125 /// @param <IN> \b u32RegAddr: register address
126 /// @param <OUT> \b None :
127 /// @param <RET> \b MS_U8
128 /// @param <GLOBAL> \b None :
129 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_ReadByte(MS_U32 u32RegAddr)130 MS_U8 HAL_WDT_ReadByte(MS_U32 u32RegAddr)
131 {
132 return ((volatile MS_U8*)(_gMIO_MapBase))[(u32RegAddr << 1) - (u32RegAddr & 1)];
133 }
134
135 ////////////////////////////////////////////////////////////////////////////////
136 /// @brief \b Function \b Name: HAL_WDT_Read4Byte
137 /// @brief \b Function \b Description: read 2 Byte data
138 /// @param <IN> \b u32RegAddr: register address
139 /// @param <OUT> \b None :
140 /// @param <RET> \b MS_U16
141 /// @param <GLOBAL> \b None :
142 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_Read2Byte(MS_U32 u32RegAddr)143 MS_U16 HAL_WDT_Read2Byte(MS_U32 u32RegAddr)
144 {
145 return ((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr];
146 }
147
148 ////////////////////////////////////////////////////////////////////////////////
149 /// @brief \b Function \b Name: HAL_WDT_Read4Byte
150 /// @brief \b Function \b Description: read 4 Byte data
151 /// @param <IN> \b u32RegAddr: register address
152 /// @param <OUT> \b None :
153 /// @param <RET> \b MS_U32
154 /// @param <GLOBAL> \b None :
155 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_Read4Byte(MS_U32 u32RegAddr)156 MS_U32 HAL_WDT_Read4Byte(MS_U32 u32RegAddr)
157 {
158 return (HAL_WDT_Read2Byte(u32RegAddr) | (HAL_WDT_Read2Byte(u32RegAddr+2) << 16));
159 }
160
161 ////////////////////////////////////////////////////////////////////////////////
162 /// @brief \b Function \b Name: HAL_WDT_WriteByte
163 /// @brief \b Function \b Description: write 1 Byte data
164 /// @param <IN> \b u32RegAddr: register address
165 /// @param <IN> \b u8Val : 1 byte data
166 /// @param <OUT> \b None :
167 /// @param <RET> \b TRUE: Ok FALSE: Fail
168 /// @param <GLOBAL> \b None :
169 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_WriteByte(MS_U32 u32RegAddr,MS_U8 u8Val)170 MS_BOOL HAL_WDT_WriteByte(MS_U32 u32RegAddr, MS_U8 u8Val)
171 {
172 if (!u32RegAddr)
173 {
174 return FALSE;
175 }
176
177 ((volatile MS_U8*)(_gMIO_MapBase))[(u32RegAddr << 1) - (u32RegAddr & 1)] = u8Val;
178 return TRUE;
179 }
180
181 ////////////////////////////////////////////////////////////////////////////////
182 /// @brief \b Function \b Name: HAL_WDT_Write2Byte
183 /// @brief \b Function \b Description: write 2 Byte data
184 /// @param <IN> \b u32RegAddr: register address
185 /// @param <IN> \b u16Val : 2 byte data
186 /// @param <OUT> \b None :
187 /// @param <RET> \b TRUE: Ok FALSE: Fail
188 /// @param <GLOBAL> \b None :
189 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_Write2Byte(MS_U32 u32RegAddr,MS_U16 u16Val)190 MS_BOOL HAL_WDT_Write2Byte(MS_U32 u32RegAddr, MS_U16 u16Val)
191 {
192 if (!u32RegAddr)
193 {
194 return FALSE;
195 }
196
197 ((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr] = u16Val;
198 return TRUE;
199 }
200
201 ////////////////////////////////////////////////////////////////////////////////
202 /// @brief \b Function \b Name: HAL_WDT_Write4Byte
203 /// @brief \b Function \b Description: write 4 Byte data
204 /// @param <IN> \b u32RegAddr: register address
205 /// @param <IN> \b u32Val : 4 byte data
206 /// @param <OUT> \b None :
207 /// @param <RET> \b TRUE: Ok FALSE: Fail
208 /// @param <GLOBAL> \b None :
209 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_Write4Byte(MS_U32 u32RegAddr,MS_U32 u32Val)210 MS_BOOL HAL_WDT_Write4Byte(MS_U32 u32RegAddr, MS_U32 u32Val)
211 {
212 if (!u32RegAddr)
213 {
214 return FALSE;
215 }
216
217 HAL_WDT_Write2Byte(u32RegAddr, u32Val & 0x0000FFFFUL);
218 HAL_WDT_Write2Byte(u32RegAddr+2, u32Val >> 16);
219 return TRUE;
220 }
221
222 ////////////////////////////////////////////////////////////////////////////////
223 /// @brief \b Function \b Name: HAL_WDT_Start
224 /// @brief \b Function \b Description: Stop WDT operations
225 /// @param <IN> \b u8Ch:
226 /// @param <OUT> \b None :
227 /// @param <RET> \b TRUE: Ok FALSE: Fail
228 /// @param <GLOBAL> \b None :
229 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_Start(void)230 MS_BOOL HAL_WDT_Start(void)
231 {
232 return TRUE;
233 }
234
235 ////////////////////////////////////////////////////////////////////////////////
236 /// @brief \b Function \b Name: HAL_WDT_Stop
237 /// @brief \b Function \b Description: Stop WDT operations
238 /// @param <IN> \b u8Ch: Channel 0/1
239 /// @param <OUT> \b None :
240 /// @param <RET> \b TRUE: Ok FALSE: Fail
241 /// @param <GLOBAL> \b None :
242 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_Stop(void)243 MS_BOOL HAL_WDT_Stop(void)
244 {
245 MS_BOOL bRet = TRUE;
246
247 do {
248 bRet = HAL_WDT_Write4Byte(REG_WDT_MAX, WDT_CLOSE_KEY);
249 } while(0);
250
251 return bRet;
252 }
253
254 ////////////////////////////////////////////////////////////////////////////////
255 /// @brief \b Function \b Name: HAL_WDT_Clear
256 /// @brief \b Function \b Description: Clear WDT.
257 /// @param <IN> \b u8Ch:
258 /// @param <OUT> \b None :
259 /// @param <RET> \b TRUE: Ok FALSE: Fail
260 /// @param <GLOBAL> \b None :
261 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_Clear(void)262 MS_BOOL HAL_WDT_Clear(void)
263 {
264 MS_BOOL bRet = FALSE;
265
266 do {
267 bRet = HAL_WDT_WriteByte(REG_WDT_CLR, WDT_RST);
268 } while(0);
269
270 return bRet;
271 }
272
273 ////////////////////////////////////////////////////////////////////////////////
274 /// @brief \b Function \b Name: HAL_WDT_ClearRstFlag
275 /// @brief \b Function \b Description: Clear WDT reset flag.
276 /// @param <IN> \b u8Ch:
277 /// @param <OUT> \b None :
278 /// @param <RET> \b TRUE: done ;FALSE: not done.
279 /// @param <GLOBAL> \b None :
280 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_ClearRstFlag(void)281 MS_BOOL HAL_WDT_ClearRstFlag(void)
282 {
283 MS_BOOL bRet = FALSE;
284
285 do {
286 bRet = HAL_WDT_WriteByte(REG_WDT_RST, WDT_CLR_RESET_FLAG);
287 } while(0);
288
289 return bRet;
290 }
291
292 ////////////////////////////////////////////////////////////////////////////////
293 /// @brief \b Function \b Name: HAL_WDT_IsReset
294 /// @brief \b Function \b Description: Read the reset status
295 /// @param <IN> \b u8Ch:
296 /// @param <OUT> \b None :
297 /// @param <RET> \b TRUE: reset at least once, FALSE: no reset occurs.
298 /// @param <GLOBAL> \b None :
299 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_IsReset(void)300 MS_BOOL HAL_WDT_IsReset(void)
301 {
302 MS_BOOL bRet = FALSE;
303
304 do {
305 bRet = ((HAL_WDT_ReadByte(REG_WDT_RST)) & (WDT_RST));
306 } while(0);
307
308 return bRet;
309 }
310
311 ////////////////////////////////////////////////////////////////////////////////
312 /// @brief \b Function \b Name: HAL_WDT_IsEnable
313 /// @brief \b Function \b Description: Check wdt enable or not.
314 /// @param <IN> \b u8Ch:
315 /// @param <OUT> \b None :
316 /// @param <RET> \b TRUE: has started, FALSE: not yet.
317 /// @param <GLOBAL> \b None :
318 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_IsEnable(void)319 MS_BOOL HAL_WDT_IsEnable(void)
320 {
321 MS_BOOL bRet = FALSE;
322
323 if (HAL_WDT_Read4Byte(REG_WDT_MAX) != WDT_CLOSE_KEY) {
324 bRet = TRUE;
325 }
326
327 return bRet;
328 }
329
330 ////////////////////////////////////////////////////////////////////////////////
331 /// @brief \b Function \b Name: HAL_WDT_SetTimer
332 /// @brief \b Function \b Description: wdt interval 65536*(65536-wdt_sel) cycles
333 /// @param <IN> \b u8Ch:
334 /// @param <OUT> \b None :
335 /// @param <RET> \b TRUE: done, FALSE: not done.
336 /// @param <GLOBAL> \b None :
337 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_SetTimer(MS_U16 sec)338 MS_BOOL HAL_WDT_SetTimer(MS_U16 sec)
339 {
340 MS_BOOL bRet = FALSE;
341
342 do {
343 bRet = HAL_WDT_Write4Byte(REG_WDT_MAX, HAL_WDTCycles(sec));
344 } while(0);
345
346 return bRet;
347 }
348
349 ////////////////////////////////////////////////////////////////////////////////
350 /// @brief \b Function \b Name: HAL_WDT_SetIntTimer
351 /// @brief \b Function \b Description: when wdt_cnt[31:16]>reg_wdt_int_sel, wdt_int occurs
352 /// @param <IN> \b u8Ch:
353 /// @param <OUT> \b None :
354 /// @param <RET> \b TRUE: done, FALSE: not done.
355 /// @param <GLOBAL> \b None :
356 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_SetIntTimer(MS_U16 sec)357 MS_BOOL HAL_WDT_SetIntTimer(MS_U16 sec)
358 {
359 MS_BOOL bRet = FALSE;
360
361 do {
362 bRet = HAL_WDT_Write2Byte(REG_WDT_INT, HAL_WDTCycles(sec)>>16);
363 } while(0);
364
365 return bRet;
366 }
367
368 ////////////////////////////////////////////////////////////////////////////////
369 /// @brief \b Function \b Name: HAL_WDT_SetTimer
370 /// @brief \b Function \b Description: wdt interval 65536*(65536-wdt_sel) cycles
371 /// @param <IN> \b u8Ch:
372 /// @param <OUT> \b None :
373 /// @param <RET> \b TRUE: done, FALSE: not done.
374 /// @param <GLOBAL> \b None :
375 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_SetTimer_ms(MS_U16 msec)376 MS_BOOL HAL_WDT_SetTimer_ms(MS_U16 msec)
377 {
378 MS_BOOL bRet = FALSE;
379
380 do {
381 bRet = HAL_WDT_Write4Byte(REG_WDT_MAX, HAL_WDTCyclesM(msec));
382 } while(0);
383
384 return bRet;
385 }
386
387 ////////////////////////////////////////////////////////////////////////////////
388 /// @brief \b Function \b Name: HAL_WDT_SetTimer
389 /// @brief \b Function \b Description: wdt interval 65536*(65536-wdt_sel) cycles
390 /// @param <IN> \b u8Ch:
391 /// @param <OUT> \b None :
392 /// @param <RET> \b TRUE: done, FALSE: not done.
393 /// @param <GLOBAL> \b None :
394 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_SetTimer_us(MS_U16 usec)395 MS_BOOL HAL_WDT_SetTimer_us(MS_U16 usec)
396 {
397 MS_BOOL bRet = FALSE;
398
399 do {
400 bRet = HAL_WDT_Write4Byte(REG_WDT_MAX, HAL_WDTCyclesU(usec));
401 } while(0);
402
403 return bRet;
404 }
405
406 ////////////////////////////////////////////////////////////////////////////////
407 /// @brief \b Function \b Name: HAL_WDT_SetIOMapBase
408 /// @brief \b Function \b Description: Set IO Map base
409 /// @param <IN> \b None :
410 /// @param <OUT> \b None :
411 /// @param <RET> \b None :
412 /// @param <GLOBAL> \b None :
413 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_SetIOMapBase(MS_VIRT virtBase)414 void HAL_WDT_SetIOMapBase(MS_VIRT virtBase)
415 {
416 _gMIO_MapBase = virtBase;
417 }
418
419 ////////////////////////////////////////////////////////////////////////////////
420 /// @brief \b Function \b Name: HAL_WDT_Get_IOMap_Base
421 /// @brief \b Function \b Description: Get IO Map base
422 /// @param <IN> \b None :
423 /// @param <OUT> \b None :
424 /// @param <RET> \b None :
425 /// @param <GLOBAL> \b None :
426 ////////////////////////////////////////////////////////////////////////////////
HAL_WDT_GetIOMapBase(void)427 MS_VIRT HAL_WDT_GetIOMapBase(void)
428 {
429 return _gMIO_MapBase;
430 }
431