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 #include "MsDevice.h"
107 #include "MsVersion.h"
108 #include "MsCommon.h"
109 #include "MsIRQ.h"
110 #include "drvMMIO.h"
111 #include "drvRTC.h"
112 #include "halRTC.h"
113 #include "MsOS.h"
114 #include "utopia_dapi.h"
115
116 #include "ULog.h"
117
118 #define RTC_DEBUG 1
119
120 #ifdef RTC_DEBUG
121 #define RTC_DBG_FUNC() if (_rtcDbfLevel >= E_RTC_DBGLV_ALL) \
122 {ULOGE("RTC","\t==== %s ====\n", __FUNCTION__);}
123 #define RTC_DBG_INFO(x, args...) if (_rtcDbfLevel >= E_RTC_DBGLV_INFO ) \
124 {ULOGE("RTC", x, ##args);}
125 #define RTC_DBG_ERR(x, args...) if (_rtcDbfLevel >= E_RTC_DBGLV_ERR_ONLY) \
126 {ULOGE("RTC", x, ##args);}
127 #else
128 #define RTC_DBG_FUNC() while (0)
129 #define RTC_DBG_INFO(x, args...) while (0)
130 #define RTC_DBG_ERR(x, args...) while (0)
131 #endif
132
133 #define RTC_MUTEX_LOCK()
134 #define RTC_MUTEX_UNLOCK()
135 #define RTC_MUTEX_DELETE()
136
137 ////////////////////////////////////////////////////////////////////////////////
138 // Local & Global Variables
139 ////////////////////////////////////////////////////////////////////////////////
140 static MS_BOOL _gbInitRTC = FALSE;
141 static RTC_DbgLv _rtcDbfLevel = E_RTC_DBGLV_ERR_ONLY;
142 static MSIF_Version _drv_rtc_version = {
143 .DDI = { RTC_DRV_VERSION },
144 };
145 // for Fastboot
146 static E_MS_DRV_RTC g_eRtc = 0;
147 static MS_U32 g_u32Freq = 0;
148 static MS_U32 g_u32Xtal =0;
149 static MS_U32 g_u32Sec = 0;
150 static InterruptCb g_pIntCb = NULL;
151
MDrv_RTC_Int_Mode(E_MS_DRV_RTC eRtc,InterruptCb pIntCb)152 static void MDrv_RTC_Int_Mode(E_MS_DRV_RTC eRtc, InterruptCb pIntCb)
153 {
154 if(pIntCb)
155 {
156 HAL_RTC_IntMask((E_MS_RTC)eRtc, DISABLE);
157 MsOS_AttachInterrupt(E_INT_IRQ_RTC,pIntCb);
158 MsOS_EnableInterrupt(E_INT_IRQ_RTC);
159 }
160 else
161 {
162 HAL_RTC_IntMask((E_MS_RTC)eRtc, ENABLE);
163 MsOS_DetachInterrupt(E_INT_IRQ_RTC);
164 MsOS_DisableInterrupt(E_INT_IRQ_RTC);
165 }
166 }
167
MDrv_RTC_GetLibVer(const MSIF_Version ** ppVersion)168 MS_BOOL MDrv_RTC_GetLibVer(const MSIF_Version **ppVersion)
169 {
170 RTC_DBG_FUNC();
171
172 if (!ppVersion)
173 return FALSE;
174
175 *ppVersion = &_drv_rtc_version;
176 return TRUE;
177 }
178
179
MDrv_RTC_Init(E_MS_DRV_RTC eRtc,MS_U32 u32Freq,MS_U32 u32Xtal,MS_U32 u32Sec,InterruptCb pIntCb)180 MS_BOOL MDrv_RTC_Init(E_MS_DRV_RTC eRtc, MS_U32 u32Freq, MS_U32 u32Xtal, MS_U32 u32Sec,InterruptCb pIntCb)
181 {
182 if(u32Xtal<u32Freq || u32Freq==0)
183 return FALSE;
184 // store for FastBoot
185 g_eRtc = eRtc;
186 g_u32Freq = u32Freq;
187 g_u32Xtal = u32Xtal;
188 g_u32Sec = u32Sec;
189 g_pIntCb = pIntCb;
190 if (!(_gbInitRTC& (1<<eRtc)))
191 {
192 MS_VIRT virtBaseAddr;
193 MS_PHY u32BaseSize;
194 if(!MDrv_MMIO_GetBASE(&virtBaseAddr, &u32BaseSize, MS_MODULE_PM))
195 {
196 RTC_DBG_ERR("Get IOMAP Base faill!\n");
197 return FALSE;
198 }
199 HAL_RTC_SetIOMapBase(virtBaseAddr);
200 //enable rtc
201 HAL_RTC_RESET((E_MS_RTC)eRtc, ENABLE);
202 //set control word
203 HAL_RTC_Set_Frequency((E_MS_RTC)eRtc,u32Freq, u32Xtal);
204 // RTC_DBG_INFO("u32Freq*u32Freq:%lx\n",u32Freq*u32Freq);
205 //set the initialized value
206 HAL_RTC_Set_Counter((E_MS_RTC)eRtc,0); //start from 0
207 HAL_RTC_Loading ((E_MS_RTC)eRtc, ENABLE);//enabling this, the initialized value will be fed in
208 //set the target value
209 HAL_RTC_Match_Counter((E_MS_RTC)eRtc,u32Sec*u32Freq);
210 //set callback function
211 MDrv_RTC_Int_Mode(eRtc,pIntCb);
212 //start counting
213 HAL_RTC_Reading((E_MS_RTC)eRtc, ENABLE);
214 HAL_RTC_Counter((E_MS_RTC)eRtc, ENABLE);
215 HAL_RTC_Wrap_Count ((E_MS_RTC)eRtc, ENABLE);
216 _gbInitRTC|=(1<<eRtc);
217 return TRUE;
218 }
219 else
220 return FALSE;
221 }
222
MDrv_RTC_GetCount(E_MS_DRV_RTC eRtc)223 MS_U32 MDrv_RTC_GetCount(E_MS_DRV_RTC eRtc)
224 {
225 if(_gbInitRTC)
226 return HAL_RTC_Read_Counter((E_MS_RTC)eRtc);
227 else
228 return 0;
229 }
230
MDrv_RTC_DeInit(E_MS_DRV_RTC eRtc)231 void MDrv_RTC_DeInit(E_MS_DRV_RTC eRtc)
232 {
233 //if(_gbInitRTC&(1<<eRtc))
234 //{
235 HAL_RTC_RESET((E_MS_RTC)eRtc, DISABLE);
236 HAL_RTC_Counter((E_MS_RTC)eRtc, DISABLE);
237 // _gbInitRTC&=(~(1<<eRtc));
238 //}
239 }
240
241 //--------------------------------------------------------------------------------------------------
242 // Utopia2.0 will call this function to register RTC module
243 //--------------------------------------------------------------------------------------------------
244 enum
245 {
246 RTC_POOL_ID_RTC0=0
247 } eRTC_PoolID;
248
RTCRegisterToUtopia(FUtopiaOpen ModuleType)249 void RTCRegisterToUtopia(FUtopiaOpen ModuleType)
250 {
251 // 1. create a module(module_name, SHM_size), and register to utopia2.0
252 void* pUtopiaModule = NULL;
253 UtopiaModuleCreate(MODULE_RTC, 0, &pUtopiaModule);
254 UtopiaModuleRegister(pUtopiaModule);
255
256 // register func for module, after register here, then ap call UtopiaOpen/UtopiaIoctl/UtopiaClose can call to these registered standard func
257 UtopiaModuleSetupFunctionPtr(pUtopiaModule, (FUtopiaOpen)RTCOpen, (FUtopiaClose)RTCClose, (FUtopiaIOctl)RTCIoctl);
258
259 // 2. Resource register
260 void* psResource = NULL;
261
262 // start func to add resources of a certain Resource_Pool
263 UtopiaModuleAddResourceStart(pUtopiaModule, RTC_POOL_ID_RTC0);
264
265 // create a resouce and regiter it to a certain Resource_Pool, resource can alloc private for internal use
266 UtopiaResourceCreate("rtc0", sizeof(RTC_RESOURCE_PRIVATE), &psResource);
267 UtopiaResourceRegister(pUtopiaModule, psResource, RTC_POOL_ID_RTC0);
268
269 UtopiaModuleAddResourceEnd(pUtopiaModule, RTC_POOL_ID_RTC0);
270
271
272 }
273
274 //--------------------------------------------------------------------------------------------------
275 // Utopia2.0 will call this function to get a instance to use RTC
276 // @ \b in: 32ModuleVersion => this is for checking if API version is the same
277 //--------------------------------------------------------------------------------------------------
RTCOpen(void ** ppInstance,MS_U32 u32ModuleVersion,void * pAttribute)278 MS_U32 RTCOpen(void** ppInstance, MS_U32 u32ModuleVersion, void* pAttribute)
279 {
280 MS_VIRT virtRetAddr;
281 MS_U32 u32RetShmID, u32RetShmSize;
282 MS_U32 u32Ret, u32RetTmp;
283
284 //Check DRV_REVISION
285 if(u32ModuleVersion != RTC_VERSION)
286 {
287 RTC_DBG_ERR("\033[35mFunction = %s, Line = %d, ERROR, DRV_VERSION is not RTC_VERSION\033[m\n", __PRETTY_FUNCTION__, __LINE__);
288 //return FALSE;
289 }
290
291 // for multi-process safe, check if already other Instance exist
292 RTC_MUTEX_LOCK();
293 u32Ret = MsOS_SHM_GetId((MS_U8*)"RTC_Instance", sizeof(MS_U32), &u32RetShmID, &virtRetAddr, &u32RetShmSize, MSOS_SHM_QUERY);
294 if(u32Ret == 0)
295 {
296 // first create, set value as 0
297 u32RetTmp = MsOS_SHM_GetId((MS_U8*)"RTC_Instance", sizeof(MS_U32), &u32RetShmID, &virtRetAddr, &u32RetShmSize, MSOS_SHM_CREATE);
298
299 if (u32RetTmp == 0)
300 {
301 RTC_DBG_ERR("[%s error] something wrong in MsOS_SHM_GetId\n", __FUNCTION__);
302 RTC_DBG_ERR("is SHM_SIZE reached?\n");
303 RTC_DBG_ERR("is MAX_SHM_CLIENT_NUM reached?\n");
304 MS_ASSERT(0);
305 }
306 *(MS_VIRT *)virtRetAddr = 0;
307 }
308
309 if(*(MS_VIRT *)virtRetAddr == 1)
310 {
311 RTC_DBG_ERR("\033[35mFunction = %s, Line = %d, [Multi-process Safe] A Instance is existed!!\033[m\n", __PRETTY_FUNCTION__, __LINE__);
312 RTC_MUTEX_UNLOCK();
313 return FALSE;
314 }
315 else
316 {
317 *(MS_VIRT *)virtRetAddr = 1;
318 RTC_MUTEX_UNLOCK();
319 }
320
321 RTC_INSTANT_PRIVATE *pRTCPri = NULL;
322
323 // instance is allocated here, also can allocate private for internal use, ex, RTC_INSTANT_PRIVATE, ppInstance will point to a pointer of the created UTOPIA_INSTANCE
324 UtopiaInstanceCreate(sizeof(RTC_INSTANT_PRIVATE), ppInstance);
325 // set the pRTCPri point to the private of UTOPIA_INSTANCE
326 UtopiaInstanceGetPrivate(*ppInstance, (void**)&pRTCPri);
327
328 RTC_INIT_PARAM *tmp = (RTC_INIT_PARAM*)pAttribute;
329
330 MDrv_RTC_Init(tmp->eRtc, tmp->u32Freq, tmp->u32Xtal, tmp->u32Sec, tmp->pIntCb);
331
332 return TRUE;
333 }
334
RTCIoctl(void * pInstance,MS_U32 u32Cmd,void * pArgs)335 MS_U32 RTCIoctl(void* pInstance, MS_U32 u32Cmd, void* pArgs)
336 {
337 return 0; // FIXME: error code
338 }
339
RTCClose(void * pInstance)340 MS_U32 RTCClose(void* pInstance)
341 {
342 MS_VIRT virtRetAddr;
343 MS_U32 u32RetShmID, u32RetShmSize;
344 MS_U32 u32Ret;
345
346 virtRetAddr = 0 ; //fix coverity isue
347 UtopiaInstanceDelete(pInstance);
348
349 u32Ret = MsOS_SHM_GetId((MS_U8*)"RTC_Instance", sizeof(MS_U32), &u32RetShmID, &virtRetAddr, &u32RetShmSize, MSOS_SHM_QUERY);
350
351 if( (u32Ret == 0) || (*(MS_VIRT *)virtRetAddr == 0) )
352 {
353 return FALSE;
354 }
355 else
356 {
357 RTC_MUTEX_LOCK();
358 *(MS_VIRT *)virtRetAddr = 0;
359 RTC_MUTEX_UNLOCK();
360 return TRUE;
361 }
362 }
363
364
MDrv_RTC_SetPowerState(EN_POWER_MODE u16PowerState)365 MS_U16 MDrv_RTC_SetPowerState(EN_POWER_MODE u16PowerState)
366 {
367 static EN_POWER_MODE _prev_u16PowerState = E_POWER_MECHANICAL;
368 MS_U16 u16Return = FALSE;
369
370 if (u16PowerState == E_POWER_SUSPEND)
371 {
372 _prev_u16PowerState = u16PowerState;
373 _gbInitRTC = FALSE;
374 u16Return = 2;//SUSPEND_OK;
375 }
376 else if (u16PowerState == E_POWER_RESUME)
377 {
378
379 if (_prev_u16PowerState == E_POWER_SUSPEND)
380 {
381 MDrv_RTC_Init( g_eRtc, g_u32Freq, g_u32Xtal, g_u32Sec,g_pIntCb);
382
383 _prev_u16PowerState = u16PowerState;
384 u16Return = 1;//RESUME_OK;
385 }
386 else
387 {
388 RTC_DBG_ERR("[%s,%5d]It is not suspended yet. We shouldn't resume\n",__FUNCTION__,__LINE__);
389 u16Return = 3;//SUSPEND_FAILED;
390 }
391 }
392 else
393 {
394 RTC_DBG_ERR("[%s,%5d]Do Nothing: %d\n",__FUNCTION__,__LINE__,u16PowerState);
395 u16Return = FALSE;
396 }
397
398 return u16Return;// for success
399 }
400
401
402
403
404
405