xref: /utopia/UTPA2-700.0.x/modules/ldm/drv/ldma/drvLDMA.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
4 // All rights reserved.
5 //
6 // Unless otherwise stipulated in writing, any and all information contained
7 // herein regardless in any format shall remain the sole proprietary of
8 // MStar Semiconductor Inc. and be kept in strict confidence
9 // ("MStar Confidential Information") by the recipient.
10 // Any unauthorized act including without limitation unauthorized disclosure,
11 // copying, use, reproduction, sale, distribution, modification, disassembling,
12 // reverse engineering and compiling of the contents of MStar Confidential
13 // Information is unlawful and strictly prohibited. MStar hereby reserves the
14 // rights to any and all damages, losses, costs and expenses resulting therefrom.
15 //
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 ///
20 /// file    drvMSPI.c
21 /// @brief  Master SPI Driver Interface
22 /// @author MStar Semiconductor Inc.
23 ///////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 
26 //-------------------------------------------------------------------------------------------------
27 //  Include Files
28 //-------------------------------------------------------------------------------------------------
29 #include <string.h>
30 // Common Definition
31 #include "MsCommon.h"
32 #include "MsVersion.h"
33 #include "drvLDMA.h"
34 #include "MsOS.h"
35 #include "ULog.h"
36 
37 // Internal Definition
38 #include "regLDMA.h"
39 #include "halLDMA.h"
40 #include "drvMMIO.h"
41 #include "utopia.h"
42 
43 #define TAG_LDMA "LDMA"
44 
45 static MS_S32 _gs32LDMA_Mutex;
46 //DMA0 DMA1
47 static MS_BOOL bInited[2]= {false,false};
48 MS_U8 _u8LDMADbgLevel;
49 
50 //-------------------------------------------------------------------------------------------------
51 //  Debug Functions
52 //-------------------------------------------------------------------------------------------------
53 #define DEBUG_LDMA(debug_level, x)     do { if (_u8LDMADbgLevel >= (debug_level)) (x); } while(0)
54 
55 
56 //------------------------------------------------------------------------------
57 /// Description : Set detailed level of LDMA driver debug message
58 /// @param u8DbgLevel    \b IN  debug level for local dimming LDMA
59 /// @return TRUE : succeed
60 /// @return FALSE : failed to set the debug level
61 //------------------------------------------------------------------------------
MDrv_LDMA_SetDbgLevel(MS_U8 u8DbgLevel)62 MS_BOOL MDrv_LDMA_SetDbgLevel(MS_U8 u8DbgLevel)
63 {
64     _u8LDMADbgLevel = u8DbgLevel;
65 
66     return TRUE;
67 }
68 //-------------------------------------------------------------------------------------------------
69 /// Description : Set Menuload Number [if need transfer 100 data, then u32MenuldNum =100 ]
70 /// @param u8HWNum \b IN: LDMA Hw number index
71 /// @param u32MenuldNum \b IN: set Dma load to mspi data number
72 /// @return DRVAESDMA_OK : Success
73 /// @return Others : Fail
74 //-------------------------------------------------------------------------------------------------
MDrv_LDMA_SetMenuloadNumber(MS_U8 u8HWNum,MS_U32 u32MenuldNum)75 MS_S8 MDrv_LDMA_SetMenuloadNumber(MS_U8 u8HWNum, MS_U32 u32MenuldNum )
76 {
77     if(bInited[u8HWNum] == false)
78     {
79         DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"LDMA Channel %d has not initiated, please check init first!\n",u8HWNum));
80         return -1;
81     }
82     return HAL_LDMA_SetMenuloadNumber(u8HWNum,  u32MenuldNum);
83 }
84 //-------------------------------------------------------------------------------------------------
85 /// Description : Set MSPI Command Format
86 /// @param u8HWNum \b IN: LDMA Hw number index
87 /// @param u8CmdLen \b IN: set mspi spec cmd length
88 /// @param pu16CmdBuf \b IN: mspi spec cmds
89 /// @return DRVAESDMA_OK : Success
90 /// @return Others : Fail
91 //-------------------------------------------------------------------------------------------------
MDrv_LDMA_SetSPICommandFormat(MS_U8 u8HWNum,MS_U8 u8CmdLen,MS_U16 * pu16CmdBuf)92 MS_S8 MDrv_LDMA_SetSPICommandFormat( MS_U8 u8HWNum,MS_U8 u8CmdLen, MS_U16* pu16CmdBuf )
93 {
94     if(bInited[u8HWNum] == false)
95     {
96         DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"LDMA Channel %d has not initiated, please check init first!\n",u8HWNum));
97         return -1;
98     }
99 
100     return  HAL_LDMA_SetSPICommandFormat(u8HWNum, u8CmdLen,  pu16CmdBuf  );
101 }
102 //-------------------------------------------------------------------------------------------------
103 /// Description : Set MSPI check sum mode
104 /// @param u8HWNum \b IN: LDMA Hw number index
105 /// @param u8SumMode \b IN: set spi data check summode [000: All (start + command + data) 001: Command + data 010: Data only]
106 /// @return DRVAESDMA_OK : Success
107 /// @return Others : Fail
108 //-------------------------------------------------------------------------------------------------
MDrv_LDMA_SetCheckSumMode(MS_U8 u8HWNum,MS_U8 u8SumMode)109 MS_S8 MDrv_LDMA_SetCheckSumMode(MS_U8 u8HWNum,MS_U8 u8SumMode)
110 {
111     if(bInited[u8HWNum] == false)
112     {
113         DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"LDMA Channel %d has not initiated, please check init first!\n",u8HWNum));
114         return -1;
115     }
116 
117     return  HAL_LDMA_SetCheckSumMode(u8HWNum, u8SumMode);
118 }
119 //-------------------------------------------------------------------------------------------------
120 /// Description : Set MSPI trigger mode
121 /// @param u8HWNum \b IN: LDMA Hw number index
122 /// @param u8TriggerMode \b IN: set DMA trigger mspi mode [0:trigger stop 1: one shot real time trigger 2: HW real time trigger]
123 /// @return DRVAESDMA_OK : Success
124 /// @return Others : Fail
125 //-------------------------------------------------------------------------------------------------
MDrv_LDMA_SetSpiTriggerMode(MS_U8 u8HWNum,MS_U8 u8TriggerMode)126 MS_S8 MDrv_LDMA_SetSpiTriggerMode( MS_U8 u8HWNum, MS_U8 u8TriggerMode )
127 {
128     if(bInited[u8HWNum] == false)
129     {
130         DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"LDMA Channel %d has not initiated, please check init first!\n",u8HWNum));
131         return -1;
132     }
133 
134     return HAL_LDMA_SetSPITriggerMode(u8HWNum,  u8TriggerMode);
135 }
136 //-------------------------------------------------------------------------------------------------
137 /// Description : Set MSPI Trigger delay time
138 /// @param u8HWNum \b IN: LDMA Hw number index
139 /// @param pu16TrigDelay \b IN: set spi clk delay time while trigger done
140 /// @return DRVAESDMA_OK : Success
141 /// @return Others : Fail
142 //-------------------------------------------------------------------------------------------------
MDrv_LDMA_SetTrigDelay(MS_U8 u8HWNum,MS_U16 * pu16TrigDelay)143 MS_S8 MDrv_LDMA_SetTrigDelay(MS_U8 u8HWNum,MS_U16 *pu16TrigDelay)
144 {
145     if(bInited[u8HWNum] == false ||(pu16TrigDelay == NULL))
146     {
147         DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"LDMA Channel %d has not initiated, please check init first!\n",u8HWNum));
148         return -1;
149     }
150 
151     return  HAL_LDMA_SetTrigDelayCnt(u8HWNum, pu16TrigDelay);
152 }
153 //-------------------------------------------------------------------------------------------------
154 /// Description : Enable ldma cs
155 /// @param u8HWNum \b IN: LDMA Hw number index
156 /// @param bEnable \b IN: set DMA for local dimming enable or disable cs
157 /// @return DRVAESDMA_OK : Success
158 /// @return Others : Fail
159 //-------------------------------------------------------------------------------------------------
MDrv_LDMA_EnableCS(MS_U8 u8HWNum,MS_BOOL bEnable)160 MS_S8 MDrv_LDMA_EnableCS(MS_U8 u8HWNum, MS_BOOL bEnable)
161 {
162     if(bInited[u8HWNum] == false)
163     {
164         DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"LDMA Channel %d has not initiated, please check init first!\n",u8HWNum));
165         return -1;
166     }
167 
168     return  HAL_LDMA_EnableCS(u8HWNum,  bEnable);
169 }
170 //-------------------------------------------------------------------------------------------------
171 /// Description : LDMA Init function to set iobase [should call first]
172 /// @param u8HWNum \b IN: LDMA Hw number index
173 /// @param u8ClkHz \b IN: select local dimming period for control LD bank
174 /// @return DRVAESDMA_OK : Success
175 /// @return Others : Fail
176 //-------------------------------------------------------------------------------------------------
MDrv_LDMA_Init(MS_U8 u8HWNum,MS_U8 u8ClkHz)177 MS_S8 MDrv_LDMA_Init(MS_U8 u8HWNum,MS_U8 u8ClkHz)
178 {
179     MS_S8 ret = 0;
180     MS_VIRT VirtNONPMBank = 0;
181     MS_PHY  u32NONPMBankSize = 0;
182 
183     if(bInited[u8HWNum])
184     {
185          DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"LDMA Channel %d has been  initiated \n",u8HWNum));
186         return -1;
187     }
188     //config LDM_DMA register base
189     _gs32LDMA_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "Mutex LDMA", MSOS_PROCESS_SHARED);
190     MS_ASSERT(_gs32LDMA_Mutex >= 0);
191 
192     if (!MDrv_MMIO_GetBASE( &VirtNONPMBank, &u32NONPMBankSize, MS_MODULE_HW))
193     {
194          DEBUG_LDMA(E_LDMA_DBGLV_ERR,ULOGE(TAG_LDMA,"IOMap failure to get DRV_MMIO_NONPM_BANK\n"));
195         return -1;
196     }
197 
198     HAL_LDMA_MMIOConfig(VirtNONPMBank,u8HWNum);
199 
200     bInited[u8HWNum] = true;
201 
202     return ret;
203 }
204