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 #define _HAL_RTC_C
95
96 ////////////////////////////////////////////////////////////////////////////////
97 /// @file halWDT.h
98 /// @author MStar Semiconductor Inc.
99 /// @brief Piu Watch Dog Timer hal
100 ////////////////////////////////////////////////////////////////////////////////
101
102 ////////////////////////////////////////////////////////////////////////////////
103 // Include Files
104 ////////////////////////////////////////////////////////////////////////////////
105 #include "MsCommon.h"
106 #include "MsTypes.h"
107 #include "halRTC.h"
108 #include "regRTC.h"
109
110 ////////////////////////////////////////////////////////////////////////////////
111 // Driver Compiler Options
112 ////////////////////////////////////////////////////////////////////////////////
113
114 ////////////////////////////////////////////////////////////////////////////////
115 // Global Variables
116 ////////////////////////////////////////////////////////////////////////////////
117 static MS_U32 _gMIO_MapBase = 0;
118
119 ////////////////////////////////////////////////////////////////////////////////
120 // Global Functions
121 ////////////////////////////////////////////////////////////////////////////////
122
123 #if 0
124 ////////////////////////////////////////////////////////////////////////////////
125 /// @brief \b Function \b Name: HAL_WDT_ReadByte
126 /// @brief \b Function \b Description: read 1 Byte data
127 /// @param <IN> \b u32RegAddr: register address
128 /// @param <OUT> \b None :
129 /// @param <RET> \b MS_U8
130 /// @param <GLOBAL> \b None :
131 ////////////////////////////////////////////////////////////////////////////////
132 static MS_U8 HAL_RTC_ReadByte(MS_U32 u32RegAddr)
133 {
134 return ((volatile MS_U8*)(_gMIO_MapBase))[(u32RegAddr << 1) - (u32RegAddr & 1)];
135 }
136 #endif
137
138 ////////////////////////////////////////////////////////////////////////////////
139 /// @brief \b Function \b Name: HAL_WDT_Read4Byte
140 /// @brief \b Function \b Description: read 2 Byte data
141 /// @param <IN> \b u32RegAddr: register address
142 /// @param <OUT> \b None :
143 /// @param <RET> \b MS_U16
144 /// @param <GLOBAL> \b None :
145 ////////////////////////////////////////////////////////////////////////////////
HAL_RTC_Read2Byte(MS_U32 u32RegAddr)146 static MS_U16 HAL_RTC_Read2Byte(MS_U32 u32RegAddr)
147 {
148 return ((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr];
149 }
150
HAL_RTC_Read4Byte(MS_U32 u32RegAddr)151 static MS_U32 HAL_RTC_Read4Byte(MS_U32 u32RegAddr)
152 {
153 MS_U32 u32Val =((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr+2];
154 u32Val<<=16;
155 u32Val|=((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr];
156 return u32Val;
157 }
158
159 #if 0
160 ////////////////////////////////////////////////////////////////////////////////
161 /// @brief \b Function \b Name: HAL_WDT_WriteByte
162 /// @brief \b Function \b Description: write 1 Byte data
163 /// @param <IN> \b u32RegAddr: register address
164 /// @param <IN> \b u8Val : 1 byte data
165 /// @param <OUT> \b None :
166 /// @param <RET> \b TRUE: Ok FALSE: Fail
167 /// @param <GLOBAL> \b None :
168 ////////////////////////////////////////////////////////////////////////////////
169 static MS_BOOL HAL_RTC_WriteByte(MS_U32 u32RegAddr, MS_U8 u8Val)
170 {
171 if (!u32RegAddr)
172 {
173 return FALSE;
174 }
175
176 ((volatile MS_U8*)(_gMIO_MapBase))[(u32RegAddr << 1) - (u32RegAddr & 1)] = u8Val;
177 return TRUE;
178 }
179 #endif
180
181 ////////////////////////////////////////////////////////////////////////////////
182 /// @brief \b Function \b Name: HAL_RTC_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_RTC_Write2Byte(MS_U32 u32RegAddr,MS_U16 u16Val)190 static MS_BOOL HAL_RTC_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
HAL_RTC_Write4Byte(MS_U32 u32RegAddr,MS_U32 u32Val)201 static void HAL_RTC_Write4Byte(MS_U32 u32RegAddr, MS_U32 u32Val)
202 {
203 HAL_RTC_Write2Byte(u32RegAddr,(MS_U16)(u32Val&0xFFFF));
204 HAL_RTC_Write2Byte(u32RegAddr+2,(MS_U16)(u32Val>>16));
205 }
206
HAL_RTC_WriteBit(MS_U32 u32RegAddr,MS_U16 u16Val,MS_BOOL bEnable)207 static MS_BOOL HAL_RTC_WriteBit(MS_U32 u32RegAddr, MS_U16 u16Val, MS_BOOL bEnable)
208 {
209 MS_U16 u16Reg;
210
211 if (!u32RegAddr)
212 {
213 return FALSE;
214 }
215
216 u16Reg = ((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr];
217 if(bEnable)
218 u16Reg|=u16Val;
219 else
220 u16Reg&=(~u16Val);
221 ((volatile MS_U16*)(_gMIO_MapBase))[u32RegAddr] = u16Reg;
222 return TRUE;
223 }
224
HAL_RTC_GET_BASE(E_MS_RTC eRtc)225 static MS_U32 HAL_RTC_GET_BASE(E_MS_RTC eRtc)
226 {
227 MS_U32 u32RegAddr=0;
228 switch(eRtc)
229 {
230 case E_RTC_0:
231 u32RegAddr=REG_RTC_BASE_0;
232 break;
233 case E_RTC_2:
234 u32RegAddr=REG_RTC_BASE_2;
235 break;
236 }
237 return u32RegAddr;
238 }
239
HAL_RTC_RESET(E_MS_RTC eRtc,MS_BOOL bEnable)240 void HAL_RTC_RESET(E_MS_RTC eRtc, MS_BOOL bEnable)
241 {
242 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_SOFT_RSTZ_BIT,bEnable);
243 }
244
HAL_RTC_Counter(E_MS_RTC eRtc,MS_BOOL bEnable)245 void HAL_RTC_Counter(E_MS_RTC eRtc, MS_BOOL bEnable)
246 {
247 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_CNT_EN_BIT,bEnable);
248 }
249
HAL_RTC_Wrap_Count(E_MS_RTC eRtc,MS_BOOL bEnable)250 void HAL_RTC_Wrap_Count (E_MS_RTC eRtc, MS_BOOL bEnable)
251 {
252 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_WRAP_EN_BIT,bEnable);
253 }
254
HAL_RTC_Loading(E_MS_RTC eRtc,MS_BOOL bEnable)255 void HAL_RTC_Loading (E_MS_RTC eRtc, MS_BOOL bEnable)
256 {
257 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_LOAD_EN_BIT,bEnable);
258 }
259
HAL_RTC_Reading(E_MS_RTC eRtc,MS_BOOL bEnable)260 void HAL_RTC_Reading(E_MS_RTC eRtc, MS_BOOL bEnable)
261 {
262 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_READ_EN_BIT,bEnable);
263 }
264
HAL_RTC_IntMask(E_MS_RTC eRtc,MS_BOOL bEnable)265 void HAL_RTC_IntMask(E_MS_RTC eRtc, MS_BOOL bEnable)
266 {
267 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_INT_MASK_BIT,bEnable);
268 }
269
HAL_RTC_IntForce(E_MS_RTC eRtc)270 void HAL_RTC_IntForce(E_MS_RTC eRtc)
271 {
272 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_INT_FORCE_BIT,ENABLE);
273 }
274
HAL_RTC_IntClear(E_MS_RTC eRtc)275 void HAL_RTC_IntClear(E_MS_RTC eRtc)
276 {
277 HAL_RTC_WriteBit(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG,RTC_INT_CLEAR_BIT,ENABLE);
278 }
279
HAL_RTC_Set_Frequency(E_MS_RTC eRtc,MS_U32 u32Freq,MS_U32 u32Xtal)280 void HAL_RTC_Set_Frequency(E_MS_RTC eRtc,MS_U32 u32Freq, MS_U32 u32Xtal)
281 {
282 if(u32Freq)
283 {
284 MS_U32 u32Reg = u32Xtal/u32Freq;
285 HAL_RTC_Write4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_FREQ_CW, u32Reg);
286 }
287 }
288
HAL_RTC_Set_CW_Frequency(E_MS_RTC eRtc,MS_U32 u32Freq)289 void HAL_RTC_Set_CW_Frequency(E_MS_RTC eRtc,MS_U32 u32Freq)
290 {
291 HAL_RTC_Write4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_FREQ_CW, u32Freq);
292 }
293
HAL_RTC_Set_Counter(E_MS_RTC eRtc,MS_U32 u32Val)294 void HAL_RTC_Set_Counter(E_MS_RTC eRtc,MS_U32 u32Val)
295 {
296 HAL_RTC_Write4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_LOAD_VAL, u32Val);
297 HAL_RTC_Write4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_LOAD_VAL_H32, 0);
298 }
299
HAL_RTC_Match_Counter(E_MS_RTC eRtc,MS_U32 u32Val)300 void HAL_RTC_Match_Counter(E_MS_RTC eRtc,MS_U32 u32Val)
301 {
302 HAL_RTC_Write4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_MATCH_VAL, u32Val);
303 HAL_RTC_Write4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_MATCH_VAL_H32, 0);
304 }
305
HAL_RTC_Get_Match_Counter(E_MS_RTC eRtc)306 MS_U32 HAL_RTC_Get_Match_Counter(E_MS_RTC eRtc)
307 {
308 return HAL_RTC_Read4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_MATCH_VAL);
309 }
310
HAL_RTC_ClearCTRL(E_MS_RTC eRtc)311 void HAL_RTC_ClearCTRL(E_MS_RTC eRtc)
312 {
313 HAL_RTC_Write2Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CTRL_REG, 0x0000);
314 }
315
HAL_RTC_INT_Status(E_MS_RTC eRtc)316 MS_U16 HAL_RTC_INT_Status(E_MS_RTC eRtc)
317 {
318 return HAL_RTC_Read2Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_INT);
319 }
320
HAL_RTC_Read_Counter(E_MS_RTC eRtc)321 MS_U32 HAL_RTC_Read_Counter(E_MS_RTC eRtc)
322 {
323 MS_U32 u32Reg;
324 MS_U16 u16Dummy=100;
325 HAL_RTC_Reading(eRtc, ENABLE);
326 while(u16Dummy--); //wait for HW latch bits okay, otherwise sometimes it read wrong value
327 u32Reg = HAL_RTC_Read4Byte(HAL_RTC_GET_BASE(eRtc)+REG_RTC_CNT);
328 HAL_RTC_Reading(eRtc, DISABLE);
329 return u32Reg;
330 }
331
HAL_RTC_SetIOMapBase(MS_U32 u32Base)332 void HAL_RTC_SetIOMapBase(MS_U32 u32Base)
333 {
334 _gMIO_MapBase = u32Base;
335 }
336
HAL_RTC_GetIOMapBase(void)337 MS_U32 HAL_RTC_GetIOMapBase(void)
338 {
339 return _gMIO_MapBase;
340 }
341