xref: /utopia/UTPA2-700.0.x/modules/clkm/drv/clkm/drvCLKM.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 #define _DRVBYTEDMA_C
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// file drvBDMA.c
97 /// @author MStar Semiconductor Inc.
98 /// @brief DRAM byte DMA control driver
99 ////////////////////////////////////////////////////////////////////////////////
100 //------------------------------------------------------------------------------
101 // Header Files
102 //------------------------------------------------------------------------------
103 #include "MsCommon.h"
104 #include "MsVersion.h"
105 #include "drvCLKM.h"
106 #include "drvclkm_io.h"
107 //#include "halclkm.h"
108 #include "drvMMIO.h"
109 #include "MsOS.h"
110 #include "asmCPU.h"
111 #include "halCHIP.h"
112 #include "utopia.h"
113 #include "drvSEM.h"
114 #include "MsTypes.h"
115 #include <stdio.h>
116 #include <string.h>
117 #include <errno.h>
118 #include <fcntl.h>
119 #include <sys/ioctl.h>
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 // Local defines & local structures
123 ////////////////////////////////////////////////////////////////////////////////
124 
125 static CLKM_DbgLvl _gCLKMDbgLevel = E_CLKM_DBGLVL_ERROR;
126 
127 #define CLKM_UTOPIA20       (1)
128 
129 #define ERR_HANDLE (-1)
130 #define NOT_FOUND (-1)
131 
132 #define SUCCESS_SET_SRC 0
133 #define ERR_NOT_FOUND_TABLE -1
134 #define ERR_NOT_SUPPORT_SRC -2
135 
136 #define CLK_GATE_ENABLE 1
137 #define CLK_GATE_DISABLE 0
138 
139 
140 #define CLKM_DBG_FUNC()               if (_gCLKMDbgLevel >= E_CLKM_DBGLVL_ALL) \
141                                         {MS_DEBUG_MSG(printf("\t====   %s   ====\n", __FUNCTION__));}
142 #define CLKM_DBG_INFO(x, args...)     if (_gCLKMDbgLevel >= E_CLKM_DBGLVL_INFO ) \
143                                         {MS_DEBUG_MSG(printf("[%s]: ", __FUNCTION__); printf(x, ##args));}
144 #define CLKM_DBG_ERR(x, args...)      if (_gCLKMDbgLevel >= E_CLKM_DBGLVL_ERROR) \
145                                         {MS_DEBUG_MSG(printf("[%s]: ", __FUNCTION__); printf(x, ##args);)}
146 #define CLKM_DBG_WARN(x, args...)     if (_gCLKMDbgLevel >= E_CLKM_DBGLVL_WARNING) \
147                                         {MS_DEBUG_MSG(printf("[%s]: ", __FUNCTION__); printf(x, ##args);)}
148 
149 #define CLKM_OFFSET(x)       ((x)<<1)
150 
151 #define CHK_BIT(x, n) ( ((x) & (1<< (n)))!=0 )
152 
153 #define CLKM_MUTEX_WAIT_TIME        (5000)
154 
155 
156 static MSIF_Version _clkmVersion = {
157     .DDI = { CLKM_DRV_VERSION },
158 };
159 
160 static CLKM_Info _gsInfo;
161 
162 #define CLKM_MODULE_KERNAL_NAME       "/dev/clkm"
163 
Drv_Clkm_Init(void)164 MS_BOOL Drv_Clkm_Init(void)
165 {
166 
167     MS_PHY u32BaseSize = 0;
168 
169      if (_gsInfo.bInit)
170      {
171          return _gsInfo.bInit;
172      }
173 
174       _gsInfo.s32Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "Mutex CLKM", MSOS_PROCESS_SHARED);
175       MS_ASSERT(_gsInfo.s32Mutex >= 0);
176 
177       _gsInfo.s32fd = open(CLKM_MODULE_KERNAL_NAME, O_RDWR);
178 
179       if( _gsInfo.s32fd < 0)
180       {
181            CLKM_DBG_ERR("Fail to open CLKM Kernal Module!\n");
182 	    _gsInfo.bInit = FALSE;
183            return E_CLKM_FAIL;
184        }
185 
186 
187 
188     _gsInfo.bInit = TRUE;
189 
190 
191     return true;
192 }
193 
194 
_Drv_CLKM_Is_Init(void)195 static MS_BOOL _Drv_CLKM_Is_Init(void)
196 {
197 
198     if (!_gsInfo.bInit)
199         CLKM_DBG_ERR("Call Drv_Clkm_Init first!\n");
200     return _gsInfo.bInit;
201 }
202 
203 
Drv_CLKM_Exit()204 CLKM_Result Drv_CLKM_Exit()
205 {
206 
207      if (!_gsInfo.bInit)
208      {
209         CLKM_DBG_INFO("CLKM not init!\n");
210         return E_CLKM_FAIL;
211      }
212 
213       close(_gsInfo.s32fd);
214       MsOS_DeleteMutex(_gsInfo.s32Mutex);
215        _gsInfo.bInit = FALSE;
216 
217       return E_CLKM_OK;
218 
219 
220 }
221 
Drv_CLKM_GetLibVer(const MSIF_Version ** ppVersion)222 CLKM_Result Drv_CLKM_GetLibVer(const MSIF_Version **ppVersion)
223 {
224     // No mutex check, it can be called before Init
225     if (!ppVersion)
226     {
227         return E_CLKM_FAIL;
228     }
229 
230     *ppVersion = &_clkmVersion;
231 
232     return E_CLKM_OK;
233 }
234 
235 
236 
237 
Drv_Clkm_Get_Handle(char * name)238  MS_S32 Drv_Clkm_Get_Handle(char *name)
239 {
240 
241        CLKM_GetHandle_PARAM gethndleparam;
242 
243        MS_S32 s32Handle = ERR_HANDLE;
244 
245 	int iResult;
246 
247 	if (!_Drv_CLKM_Is_Init())
248        {
249             Drv_Clkm_Init();
250        }
251 
252 	 if (FALSE == MsOS_ObtainMutex(_gsInfo.s32Mutex, CLKM_MUTEX_WAIT_TIME))
253         {
254              CLKM_DBG_ERR("%s ENTRY fails!\n", __FUNCTION__);
255              return E_CLKM_FAIL;
256         }
257 
258 	 gethndleparam.s8_Handle_Name = name;
259 
260 	 if(ioctl(_gsInfo.s32fd, CMD_CLKM_GET_HANDLE, &gethndleparam) < 0)
261         {
262            CLKM_DBG_ERR("[%s][%d] Fail!!!!\n",__FUNCTION__,__LINE__);
263 
264         }
265 
266 	  s32Handle = gethndleparam.s32_Handle;
267 
268 	  MsOS_ReleaseMutex(_gsInfo.s32Mutex);
269 
270 	 return s32Handle;
271 }
272 
273 
274 
275 
Drv_Clkm_Clk_Gate_Disable(MS_S32 s32Handle)276 void Drv_Clkm_Clk_Gate_Disable(MS_S32 s32Handle)
277 {
278 
279 
280 	CLKM_CLK_GATE_DISABLE_PARAM clkgatedisable;
281 
282 	 if (!_Drv_CLKM_Is_Init())
283         {
284             Drv_Clkm_Init();
285         }
286 
287 
288 	 if (FALSE == MsOS_ObtainMutex(_gsInfo.s32Mutex, CLKM_MUTEX_WAIT_TIME))
289         {
290              CLKM_DBG_ERR("%s ENTRY fails!\n", __FUNCTION__);
291              return E_CLKM_FAIL;
292         }
293 
294         clkgatedisable.s32_Handle = s32Handle;
295 
296         if(ioctl(_gsInfo.s32fd, CMD_CLKM_CLK_GATE_DISABLE, &clkgatedisable) <0 )
297         {
298            CLKM_DBG_ERR("[%s][%d] Fail!!!!\n",__FUNCTION__,__LINE__);
299          }
300 
301         MsOS_ReleaseMutex(_gsInfo.s32Mutex);
302 
303 }
304 
305 
Drv_Clkm_Set_Clk_Source(MS_S32 s32Handle,char * clk_src_name)306 void Drv_Clkm_Set_Clk_Source(MS_S32 s32Handle,char *clk_src_name)
307 {
308        MS_BOOL ret = TRUE;
309        char *s8_Src_Name;
310        MS_S32 src_offset;
311        s8_Src_Name = clk_src_name;
312        MS_U32 u32RegAddr;
313        CLKM_SET_CLK_SRC_PARAM setclksrcparam;
314 
315        if (!_Drv_CLKM_Is_Init())
316        {
317            Drv_Clkm_Init();
318        }
319 
320        if (FALSE == MsOS_ObtainMutex(_gsInfo.s32Mutex, CLKM_MUTEX_WAIT_TIME))
321        {
322            CLKM_DBG_ERR("%s ENTRY fails!\n", __FUNCTION__);
323            return E_CLKM_FAIL;
324        }
325 
326        setclksrcparam.s32_Handle = s32Handle;
327        setclksrcparam.clk_src_name = clk_src_name;
328 
329        if(ioctl(_gsInfo.s32fd, CMD_CLKM_SET_CLK_SOURCE, &setclksrcparam) < 0)
330        {
331            CLKM_DBG_ERR("[%s][%d] Fail!!!!\n",__FUNCTION__,__LINE__);
332        }
333 
334 
335 	MsOS_ReleaseMutex(_gsInfo.s32Mutex);
336 
337 	return ret;
338 }
339 
340 
Drv_Get_Sram_Sd_Info(char * name)341 MS_U16 Drv_Get_Sram_Sd_Info(char* name)
342 {
343      /*
344        MS_U32 u32i;
345 	sram_sd_table *ptr = sram_sd_param->sram_sd_table_array_ref;
346        sram_sd_table *p_sd_table = NULL;
347        MS_U32 u32_regaddr ;
348 	MS_U16 u16Value;
349 	MS_S8* s8tmpname = name;
350 
351 	if (!_Drv_CLKM_Is_Init())
352        {
353            Drv_Clkm_Init();
354         }
355 
356 
357 	return u16Value;
358 */
359 
360 return 0;
361 
362 }
363 
364 
365