xref: /utopia/UTPA2-700.0.x/modules/mspi/drv/mspi/drvMSPI.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 "drvMSPI.h"
34 #include "MsOS.h"
35 
36 #include "ULog.h"
37 
38 // Internal Definition
39 #include "regMSPI.h"
40 #include "halMSPI.h"
41 #include "drvMMIO.h"
42 #include "utopia.h"
43 #include "drvGPIO.h"
44 //-------------------------------------------------------------------------------------------------
45 //  Driver Compiler Options
46 //-------------------------------------------------------------------------------------------------
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 // Local & Global Variables
50 ////////////////////////////////////////////////////////////////////////////////
51 MS_U8 gu8MSPIConfig;
52 MS_U8 gu8MSPICurConfig[MSPI_CMD_TYPE];
53 MS_BOOL gbInitFlag = FALSE;
54 MS_BOOL gbInitInUtopia = FALSE;
55 
56 MS_U8 _u8MSPIDbgLevel;
57 static MS_U32 _gu32CsPad = 0xFF;
58 static MS_U32 _gu32DevID = 0xFF;
59 static MS_U32 _gu32MaxClk = 0;
60 static MS_S32 _gs32MSPI_Mutex;
61 //MSPI mutex wait time
62 #define MSPI_MUTEX_WAIT_TIME    3000
63 
64 #define TAG_MSPI "MSPI"
65 
66 //-------------------------------------------------------------------------------------------------
67 //  Debug Functions
68 //-------------------------------------------------------------------------------------------------
69 #define DEBUG_MSPI(debug_level, x)     do { if (_u8MSPIDbgLevel >= (debug_level)) (x); } while(0)
70 
71 
72 //-------------------------------------------------------------------------------------------------
73 //  Local Functions
74 //-------------------------------------------------------------------------------------------------
75 //------------------------------------------------------------------------------
76 /// Description : Set Chip Select
77 /// @return E_MSPI_OK :
78 /// @return >1 : failed to set Chip select
79 //------------------------------------------------------------------------------
_MDrv_ChipSelectSetting(MS_U8 u8Cs)80 static MSPI_ErrorNo _MDrv_ChipSelectSetting(MS_U8 u8Cs)
81 {
82     MS_U8 u8CSMax;
83     MSPI_ErrorNo errnum = E_MSPI_OK;
84 
85     u8CSMax = HAL_MSPI_ChipSelectMax();
86     if(u8Cs > u8CSMax)
87         errnum = E_MSPI_PARAM_OVERFLOW;
88     else
89         HAL_MSPI_SetChipSelect(u8Cs);
90     return errnum;
91 }
92 
93 //------------------------------------------------------------------------------
94 /// Description : Set TrStart timing of DC config
95 /// @return E_MSPI_OK :
96 /// @return >1 : failed to set TrStart timing
97 //------------------------------------------------------------------------------
_MDrv_DC_TrStartSetting(MS_U8 TrStart)98 static MSPI_ErrorNo _MDrv_DC_TrStartSetting(MS_U8 TrStart)
99 {
100     MS_U8 u8TrStartMax;
101     MSPI_ErrorNo errnum = E_MSPI_OK;
102 
103     u8TrStartMax = HAL_MSPI_DCConfigMax(E_MSPI_TRSTART);
104     if(TrStart > u8TrStartMax)
105         errnum = E_MSPI_PARAM_OVERFLOW;
106     else
107         HAL_MSPI_SetDcTiming(E_MSPI_TRSTART ,TrStart);
108     return errnum;
109 }
110 
111 //------------------------------------------------------------------------------
112 /// Description : Set TrEnd timing of DC config
113 /// @return E_MSPI_OK :
114 /// @return >1 : failed to set TrEnd timing
115 //------------------------------------------------------------------------------
_MDrv_DC_TrEndSetting(MS_U8 TrEnd)116 static MSPI_ErrorNo _MDrv_DC_TrEndSetting(MS_U8 TrEnd)
117 {
118     MS_U8 u8TrEndMax;
119     MSPI_ErrorNo errnum = E_MSPI_OK;
120 
121     u8TrEndMax = HAL_MSPI_DCConfigMax(E_MSPI_TREND);
122     if(TrEnd > u8TrEndMax)
123         errnum = E_MSPI_PARAM_OVERFLOW;
124     else
125         HAL_MSPI_SetDcTiming(E_MSPI_TREND ,TrEnd);
126     return errnum;
127 
128 }
129 
130 //------------------------------------------------------------------------------
131 /// Description : Set TB timing of DC config
132 /// @return E_MSPI_OK :
133 /// @return >1 : failed to set TB timing
134 //------------------------------------------------------------------------------
_MDrv_DC_TBSetting(MS_U8 TB)135 static MSPI_ErrorNo _MDrv_DC_TBSetting(MS_U8 TB)
136 {
137     MS_U8 u8TBMax;
138     MSPI_ErrorNo errnum = E_MSPI_OK;
139 
140     u8TBMax = HAL_MSPI_DCConfigMax(E_MSPI_TB);
141     if(TB > u8TBMax)
142         errnum = E_MSPI_PARAM_OVERFLOW;
143     else
144         HAL_MSPI_SetDcTiming(E_MSPI_TB ,TB);
145     return errnum;
146 
147 }
148 
149 //------------------------------------------------------------------------------
150 /// Description : Set TRW timing of DC config
151 /// @return E_MSPI_OK :
152 /// @return >1 : failed to set TRW timging
153 //------------------------------------------------------------------------------
_MDrv_DC_TRWSetting(MS_U8 TRW)154 static MSPI_ErrorNo _MDrv_DC_TRWSetting(MS_U8 TRW)
155 {
156     MS_U8 u8TRWMax;
157     MSPI_ErrorNo errnum = E_MSPI_OK;
158 
159     u8TRWMax = HAL_MSPI_DCConfigMax(E_MSPI_TRW);
160     if(TRW > u8TRWMax)
161         errnum = E_MSPI_PARAM_OVERFLOW;
162     else
163         HAL_MSPI_SetDcTiming(E_MSPI_TRW ,TRW);
164     return errnum;
165 
166 }
167 
168 //------------------------------------------------------------------------------
169 /// Description : Set clock polarity of MSPI
170 /// @return E_MSPI_OK :
171 /// @return >1 : failed to set clock polarity
172 //------------------------------------------------------------------------------
_MDrv_CLK_PolaritySetting(MS_U8 u8Pol)173 static MSPI_ErrorNo _MDrv_CLK_PolaritySetting(MS_U8 u8Pol)
174 {
175     MS_U8 u8PolarityMax;
176     MSPI_ErrorNo errnum = E_MSPI_OK;
177 
178     u8PolarityMax = HAL_MSPI_CLKConfigMax(E_MSPI_POL);
179     if(u8Pol > u8PolarityMax)
180         errnum = E_MSPI_PARAM_OVERFLOW;
181     else
182         HAL_MSPI_SetCLKTiming(E_MSPI_POL ,u8Pol);
183     return errnum;
184 
185 }
186 
187 //------------------------------------------------------------------------------
188 /// Description : Set clock phase of MSPI
189 /// @return E_MSPI_OK :
190 /// @return >1 : failed to set clock phase
191 //------------------------------------------------------------------------------
_MDrv_CLK_PhaseSetting(MS_U8 u8Pha)192 static MSPI_ErrorNo _MDrv_CLK_PhaseSetting(MS_U8 u8Pha)
193 {
194     MS_U8 u8PhaseMax;
195     MSPI_ErrorNo errnum = E_MSPI_OK;
196 
197     u8PhaseMax = HAL_MSPI_CLKConfigMax(E_MSPI_PHA);
198     if(u8Pha > u8PhaseMax)
199         errnum = E_MSPI_PARAM_OVERFLOW;
200     else
201         HAL_MSPI_SetCLKTiming(E_MSPI_PHA ,u8Pha);
202     return errnum;
203 
204 }
205 
206 //------------------------------------------------------------------------------
207 /// Description : Set clock rate of MSPI
208 /// @return E_MSPI_OK :
209 /// @return >1 : failed to set clock rate
210 //------------------------------------------------------------------------------
_MDrv_CLK_ClockSetting(MS_U8 u8Clock)211 static MSPI_ErrorNo _MDrv_CLK_ClockSetting(MS_U8 u8Clock)
212 {
213     MS_U8 u8ClockMax;
214     MSPI_ErrorNo errnum = E_MSPI_OK;
215 
216     u8ClockMax = HAL_MSPI_CLKConfigMax(E_MSPI_CLK);
217     if(u8Clock > u8ClockMax)
218         errnum = E_MSPI_PARAM_OVERFLOW;
219     else
220         HAL_MSPI_SetCLKTiming(E_MSPI_CLK ,u8Clock);
221     return errnum;
222 
223 }
224 
225 //------------------------------------------------------------------------------
226 /// Description : Set transfer bit pre frame for read/write buffer
227 /// @return E_MSPI_OK :
228 /// @return >1 : failed to check paramter
229 //------------------------------------------------------------------------------
_MDrv_Frame_BitSetting(MS_BOOL bDirect,MS_U8 u8Index,MS_U8 u8BitPerFrame)230 static MSPI_ErrorNo _MDrv_Frame_BitSetting(MS_BOOL bDirect, MS_U8 u8Index, MS_U8 u8BitPerFrame)
231 {
232     MS_U8 u8MAxBitPerFrame;
233     MSPI_ErrorNo errnum = E_MSPI_OK;
234 
235     u8MAxBitPerFrame = HAL_MSPI_FrameConfigMax();
236     if(u8BitPerFrame > u8MAxBitPerFrame)
237         errnum = E_MSPI_PARAM_OVERFLOW;
238     else
239         HAL_MSPI_SetPerFrameSize(bDirect,  u8Index, u8BitPerFrame);
240     return errnum;
241 }
242 
243 //-------------------------------------------------------------------------------------------------
244 //  Global Functions
245 //-------------------------------------------------------------------------------------------------
246 
247 //------------------------------------------------------------------------------
248 /// Description : Set detailed level of MSPI driver debug message
249 /// @param u8DbgLevel    \b IN  debug level for Serial Flash driver
250 /// @return TRUE : succeed
251 /// @return FALSE : failed to set the debug level
252 //------------------------------------------------------------------------------
MDrv_MSPI_SetDbgLevel(MS_U8 u8DbgLevel)253 MS_BOOL MDrv_MSPI_SetDbgLevel(MS_U8 u8DbgLevel)
254 {
255     _u8MSPIDbgLevel = u8DbgLevel;
256 
257     return TRUE;
258 }
259 //------------------------------------------------------------------------------
260 /// Description : MSPI initial Ext
261 /// @return E_MSPI_OK :
262 /// @return >1 : failed to initial
263 //------------------------------------------------------------------------------
MDrv_MSPI_Init_Ext(MS_U8 u8HWNum)264 MSPI_ErrorNo MDrv_MSPI_Init_Ext(MS_U8 u8HWNum)
265 {
266     MS_VIRT VirtNONPMBank;
267     MS_PHY  u32NONPMBankSize;
268     MS_VIRT VirtPMBank;
269     MS_PHY  u32PMBankSize;
270 
271     MSPI_ErrorNo errorno = E_MSPI_OK;
272     if(gbInitFlag == TRUE)
273     {
274         ULOGE(TAG_MSPI, "MSPI Driver Already init\r\n");
275         return E_MSPI_INIT_FLOW_ERROR;
276     }
277 
278     _gs32MSPI_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "Mutex MSPI", MSOS_PROCESS_SHARED);
279     MS_ASSERT(_gs32MSPI_Mutex >= 0);
280 
281     _u8MSPIDbgLevel = E_MSPI_DBGLV_NONE;
282     if(!MDrv_MSPI_HW_Support())
283     {
284         gbInitFlag = FALSE;
285         return E_MSPI_HW_NOT_SUPPORT;
286     }
287 
288     if (!MDrv_MMIO_GetBASE( &VirtPMBank, &u32PMBankSize, MS_MODULE_PM))
289     {
290         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_PM_BANK\n"));
291     }
292 
293     if (!MDrv_MMIO_GetBASE( &VirtNONPMBank, &u32NONPMBankSize, MS_MODULE_HW))
294     {
295         DEBUG_MSPI(E_MSPI_DBGLV_INFO, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_NONPM_BANK\n"));
296         errorno = E_MSPI_MMIO_ERROR;
297         return E_MSPI_MMIO_ERROR;
298     }
299     //DEBUG_MSPI(E_MSPI_DBGLV_INFO, ULOGI(TAG_MSPI, "IOMap address PM %x NONPM %x\n",VirtPMBank, VirtNONPMBank));
300 
301     HAL_MSPI_MMIOConfig(VirtPMBank, VirtNONPMBank, u8HWNum);
302 
303     memset(gu8MSPICurConfig, 0, sizeof(gu8MSPICurConfig));
304     // default use CS0 for slave device
305     errorno = _MDrv_ChipSelectSetting(0);
306     //default use polling mode
307     HAL_MSPI_IntEnable(0);
308     HAL_MSPI_Init();
309     if(_gu32MaxClk)
310         HAL_MSPI_CLOCK_Config(_gu32MaxClk);
311     DEBUG_MSPI(E_MSPI_DBGLV_INFO, ULOGI(TAG_MSPI, " MSPI Init complete\n"));
312     gbInitFlag = TRUE;
313     gbInitInUtopia = TRUE;
314     //default clock setting
315     errorno = _MDrv_CLK_PolaritySetting(0);
316     DEBUG_MSPI(E_MSPI_DBGLV_INFO, ULOGI(TAG_MSPI, "PolaritySetting complete\n"));
317     if(errorno != E_MSPI_OK)
318         goto ERROR_HANDLE;
319     errorno = _MDrv_CLK_PhaseSetting(0);
320     if(errorno != E_MSPI_OK)
321         goto ERROR_HANDLE;
322     errorno = _MDrv_CLK_ClockSetting(0);
323     if(errorno != E_MSPI_OK)
324         goto ERROR_HANDLE;
325     return E_MSPI_OK;
326 ERROR_HANDLE:
327     errorno |= E_MSPI_INIT_FLOW_ERROR;
328     DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI CLKconfig error errno =%d\n",errorno));
329     return errorno;
330 }
331 
332 //------------------------------------------------------------------------------
333 /// Description : MSPI initial
334 /// @return E_MSPI_OK :
335 /// @return >1 : failed to initial
336 //------------------------------------------------------------------------------
MDrv_MSPI_Init(MSPI_config * tMSPIConfig,MS_U8 u8HWNum)337 MSPI_ErrorNo MDrv_MSPI_Init(MSPI_config *tMSPIConfig, MS_U8 u8HWNum)
338 {
339     MS_VIRT VirtNONPMBank;
340     MS_PHY  u32NONPMBankSize;
341     MS_VIRT VirtPMBank;
342     MS_PHY  u32PMBankSize;
343     MSPI_ErrorNo errorno = E_MSPI_OK;
344     if(!MDrv_MSPI_HW_Support())
345     {
346         gbInitFlag = FALSE;
347         return E_MSPI_HW_NOT_SUPPORT;
348     }
349     _u8MSPIDbgLevel = E_MSPI_DBGLV_NONE;
350 
351     _gs32MSPI_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "Mutex MSPI", MSOS_PROCESS_SHARED);
352     MS_ASSERT(_gs32MSPI_Mutex >= 0);
353 
354     if (!MDrv_MMIO_GetBASE( &VirtPMBank, &u32PMBankSize, MS_MODULE_PM))
355     {
356         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_PM_BANK\n"));
357     }
358 
359     if (!MDrv_MMIO_GetBASE( &VirtNONPMBank, &u32NONPMBankSize, MS_MODULE_HW))
360     {
361         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_NONPM_BANK\n"));
362         errorno = E_MSPI_MMIO_ERROR;
363     }
364     if(u8HWNum > 2)
365         return E_MSPI_PARAM_OVERFLOW;
366     HAL_MSPI_MMIOConfig(VirtPMBank, VirtNONPMBank, u8HWNum);
367 
368     if(tMSPIConfig->BIntEnable)
369     {
370         //register IRQ handler
371     }
372     memcpy(gu8MSPICurConfig, tMSPIConfig->U8BitMapofConfig, sizeof(gu8MSPICurConfig));
373 
374     errorno = _MDrv_ChipSelectSetting(tMSPIConfig->U8ChipSel);
375     if(errorno != E_MSPI_OK)
376         return errorno;
377     HAL_MSPI_IntEnable(tMSPIConfig->BIntEnable);
378     HAL_MSPI_Init();
379     gbInitFlag = TRUE;
380     gbInitInUtopia = TRUE;
381     return errorno;
382 }
383 
384 //-------------------------------------------------------------------------------------------------
385 /// Description : read data from MSPI
386 /// @param pData \b IN :pointer to receive data from MSPI read buffer
387 /// @param u16Size \ b OTU : read data size
388 /// @return the errorno of operation
389 //-------------------------------------------------------------------------------------------------
MDrv_MSPI_Read(MS_U8 * pData,MS_U16 u16Size)390 MSPI_ErrorNo MDrv_MSPI_Read(MS_U8 *pData, MS_U16 u16Size)
391 {
392     MSPI_ErrorNo errorno = E_MSPI_OK;
393 
394     //check init
395     if(!gbInitFlag)
396         return E_MSPI_INIT_FLOW_ERROR;
397 
398     // check config error
399     if(!(gu8MSPIConfig & MSPI_DC_CONFIG))
400         errorno |= E_MSPI_DCCONFIG_ERROR;
401     if(!(gu8MSPIConfig & MSPI_CLK_CONFIG))
402         errorno |= E_MSPI_CLKCONFIG_ERROR;
403     if(!(gu8MSPIConfig & MSPI_FRAME_CONFIG))
404         errorno |= E_MSPI_FRAMECONFIG_ERROR;
405     if(errorno != E_MSPI_OK)
406     {
407         // reset config
408         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "Read Operation MSPI config error %d\n",errorno));
409     }
410 
411     HAL_MSPI_Read(pData, u16Size);
412     return errorno;
413 }
414 
415 //------------------------------------------------------------------------------
416 /// Description : read data from MSPI
417 /// @param pData \b OUT :pointer to write  data to MSPI write buffer
418 /// @param u16Size \ b OTU : write data size
419 /// @return the errorno of operation
420 //------------------------------------------------------------------------------
MDrv_MSPI_Write(MS_U8 * pData,MS_U16 u16Size)421 MSPI_ErrorNo MDrv_MSPI_Write(MS_U8 *pData, MS_U16 u16Size)
422 {
423     MSPI_ErrorNo errorno = E_MSPI_OK;
424 
425     //check init
426     if(!gbInitFlag)
427         return E_MSPI_INIT_FLOW_ERROR;
428 
429     // check config error
430     if(!(gu8MSPIConfig & MSPI_DC_CONFIG))
431         errorno |= E_MSPI_DCCONFIG_ERROR;
432     if(!(gu8MSPIConfig & MSPI_CLK_CONFIG))
433         errorno |= E_MSPI_CLKCONFIG_ERROR;
434     if(!(gu8MSPIConfig & MSPI_FRAME_CONFIG))
435         errorno |= E_MSPI_FRAMECONFIG_ERROR;
436     if(errorno != E_MSPI_OK)
437     {
438         // reset config
439         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "Write Operation MSPI config error %d\n",errorno));
440     }
441 
442     // write operation
443     HAL_MSPI_Wirte(pData, u16Size);
444     return errorno;
445 }
446 
447 //------------------------------------------------------------------------------
448 /// Description : config spi transfer timing
449 /// @param ptDCConfig    \b OUT  struct pointer of transfer timing config
450 /// @return E_MSPI_OK : succeed
451 /// @return E_MSPI_DCCONFIG_ERROR : failed to config transfer timing
452 //------------------------------------------------------------------------------
MDrv_MSPI_DCConfig(MSPI_DCConfig * ptDCConfig)453 MSPI_ErrorNo MDrv_MSPI_DCConfig(MSPI_DCConfig *ptDCConfig)
454 {
455     MSPI_ErrorNo errnum = E_MSPI_OK;
456     //check init
457     if(!gbInitFlag)
458         return E_MSPI_INIT_FLOW_ERROR;
459 
460     if(ptDCConfig == NULL)
461     {
462         HAL_MSPI_Reset_DCConfig();
463         return E_MSPI_OK;
464     }
465     errnum = _MDrv_DC_TrStartSetting(ptDCConfig->u8TrStart);
466     if(errnum != E_MSPI_OK)
467         goto ERROR_HANDLE;
468     errnum = _MDrv_DC_TrEndSetting(ptDCConfig->u8TrEnd);
469     if(errnum != E_MSPI_OK)
470         goto ERROR_HANDLE;
471     errnum = _MDrv_DC_TBSetting(ptDCConfig->u8TB);
472     if(errnum != E_MSPI_OK)
473         goto ERROR_HANDLE;
474     errnum = _MDrv_DC_TRWSetting(ptDCConfig->u8TRW);
475     if(errnum != E_MSPI_OK)
476         goto ERROR_HANDLE;
477     gu8MSPIConfig |= MSPI_DC_CONFIG;
478     return E_MSPI_OK;
479 
480 ERROR_HANDLE:
481     errnum |= E_MSPI_DCCONFIG_ERROR;
482     DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI DCconfig error errno =%d\n",errnum));
483     return errnum;
484 }
485 
486 //------------------------------------------------------------------------------
487 /// Description : config spi clock setting
488 /// @param ptCLKConfig    \b OUT  struct pointer of clock config
489 /// @return E_MSPI_OK : succeed
490 /// @return E_MSPI_CLKCONFIG_ERROR : failed to config spi clock
491 //------------------------------------------------------------------------------
MDrv_MSPI_CLKConfig(MSPI_CLKConfig * ptCLKConfig)492 MSPI_ErrorNo MDrv_MSPI_CLKConfig(MSPI_CLKConfig *ptCLKConfig)
493 {
494     MSPI_ErrorNo errnum = E_MSPI_OK;
495     //check init
496     if(!gbInitFlag)
497         return E_MSPI_INIT_FLOW_ERROR;
498 
499     if(ptCLKConfig == NULL)
500     {
501         HAL_MSPI_Reset_CLKConfig();
502         return E_MSPI_OK;
503     }
504 
505     errnum = _MDrv_CLK_PolaritySetting(ptCLKConfig->BClkPolarity);
506     if(errnum != E_MSPI_OK)
507         goto ERROR_HANDLE;
508     errnum = _MDrv_CLK_PhaseSetting(ptCLKConfig->BClkPhase);
509     if(errnum != E_MSPI_OK)
510         goto ERROR_HANDLE;
511     errnum = _MDrv_CLK_ClockSetting(ptCLKConfig->U8Clock);
512     if(errnum != E_MSPI_OK)
513         goto ERROR_HANDLE;
514     gu8MSPIConfig |= MSPI_CLK_CONFIG;
515     return E_MSPI_OK;
516 
517 ERROR_HANDLE:
518     errnum |= E_MSPI_CLKCONFIG_ERROR;
519     DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI CLKconfig error errno =%d\n",errnum));
520     return errnum;
521 
522 }
523 
524 
525 //------------------------------------------------------------------------------
526 /// Description : config spi transfer timing
527 /// @param ptDCConfig    \b OUT  struct pointer of bits of buffer tranferred to slave config
528 /// @return E_MSPI_OK : succeed
529 /// @return E_MSPI_FRAMECONFIG_ERROR : failed to config transfered bit per buffer
530 //------------------------------------------------------------------------------
MDrv_MSPI_FRAMEConfig(MSPI_FrameConfig * ptFrameConfig)531 MSPI_ErrorNo MDrv_MSPI_FRAMEConfig(MSPI_FrameConfig *ptFrameConfig)
532 {
533     MSPI_ErrorNo errnum = E_MSPI_OK;
534     MS_U8 u8Index = 0;
535     MS_U8 u8BufSize;
536 
537     //check init
538     if(!gbInitFlag)
539         return E_MSPI_INIT_FLOW_ERROR;
540 
541     if(ptFrameConfig == NULL)
542     {
543         HAL_MSPI_Reset_FrameConfig();
544         return E_MSPI_OK;
545     }
546     // read buffer bit config
547     u8BufSize = sizeof(ptFrameConfig->u8RBitConfig);
548     for(u8Index = 0;u8Index < u8BufSize; u8Index++)
549     {
550         errnum = _MDrv_Frame_BitSetting(MSPI_READ_INDEX, u8Index, ptFrameConfig->u8RBitConfig[u8Index]);
551         if(errnum != E_MSPI_OK)
552         {
553             errnum |= E_MSPI_FRAMECONFIG_ERROR;
554              DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI FRAMEconfig error errno =%d\n",errnum));
555             return errnum;
556         }
557     }
558     //write buffer bit config
559     u8BufSize = sizeof(ptFrameConfig->u8WBitConfig);
560     for(u8Index = 0;u8Index < u8BufSize; u8Index++)
561     {
562         errnum = _MDrv_Frame_BitSetting(MSPI_WRITE_INDEX, u8Index, ptFrameConfig->u8WBitConfig[u8Index]);
563         if(errnum != E_MSPI_OK)
564         {
565             errnum |= E_MSPI_FRAMECONFIG_ERROR;
566             DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI FRAMEconfig error errno =%d\n",errnum));
567             return errnum;
568         }
569     }
570 
571     gu8MSPIConfig |= MSPI_FRAME_CONFIG;
572     return errnum;
573 }
574 
575 //------------------------------------------------------------------------------
576 /// Description : Enable Slave device
577 //------------------------------------------------------------------------------
MDrv_MSPI_SlaveEnable(MS_BOOL Enable)578 void MDrv_MSPI_SlaveEnable(MS_BOOL Enable)
579 {
580     HAL_MSPI_SlaveEnable(Enable);
581     if(_gu32CsPad != 0xFF)
582     {
583         if(Enable)
584         {
585             mdrv_gpio_set_low(_gu32CsPad);
586         }
587         else
588         {
589             mdrv_gpio_set_high(_gu32CsPad);
590         }
591     }
592 }
593 
594 //------------------------------------------------------------------------------
595 /// Description :
596 /// @return TRUE : chip support
597 /// @return FALSE:
598 //------------------------------------------------------------------------------
MDrv_MSPI_HW_Support(void)599 MS_BOOL MDrv_MSPI_HW_Support(void)
600 {
601     return HAL_MSPI_HW_Support();
602 }
603 
604 //-------------------------------------------------------
605 // Description : MSPI Power state
606 //-------------------------------------------------------
MDrv_MSPI_SetPowerState(EN_POWER_MODE enPowerState)607 MS_U32 MDrv_MSPI_SetPowerState(EN_POWER_MODE enPowerState)
608 {
609     static EN_POWER_MODE enPreBDMAPowerState = E_POWER_MECHANICAL;
610     MS_U32 u32Return = UTOPIA_STATUS_FAIL;
611    if(!gbInitInUtopia)
612     {
613         return E_MSPI_INIT_FLOW_ERROR;
614     }
615     if (enPowerState == E_POWER_SUSPEND)
616     {
617         enPreBDMAPowerState = enPowerState;
618         u32Return = UTOPIA_STATUS_SUCCESS;//SUSPEND_OK;
619         gbInitFlag = FALSE;
620 
621     }
622     else if (enPowerState == E_POWER_RESUME)
623     {
624         if (enPreBDMAPowerState == E_POWER_SUSPEND)
625         {
626             MDrv_MSPI_Init_Ext(0);
627 
628             enPreBDMAPowerState = enPowerState;
629             u32Return = UTOPIA_STATUS_SUCCESS;//RESUME_OK;
630         }
631         else
632         {
633             ULOGE(TAG_MSPI, "[%s,%5d]It is not suspended yet. We shouldn't resume\n",__FUNCTION__,__LINE__);
634             u32Return = UTOPIA_STATUS_FAIL;//SUSPEND_FAILED;
635         }
636     }
637     else
638     {
639         ULOGE(TAG_MSPI, "[%s,%5d]Do Nothing: %d\n",__FUNCTION__,__LINE__,enPowerState);
640         u32Return = UTOPIA_STATUS_FAIL;
641     }
642 
643     return u32Return;// for success
644 }
645 
646 //------------------------------------------------------------------------------
647 /// Description : MSPI initial
648 /// @return E_MSPI_OK :
649 /// @return >1 : failed to initial
650 //------------------------------------------------------------------------------
MDrv_MasterSPI_Init(MSPI_config * tMSPIConfig,MS_U8 u8HWNum)651 MSPI_ErrorNo MDrv_MasterSPI_Init(MSPI_config *tMSPIConfig, MS_U8 u8HWNum)
652 {
653     MS_VIRT VirtNONPMBank;
654     MS_PHY  u32NONPMBankSize;
655     MS_VIRT VirtPMBank;
656     MS_PHY  u32PMBankSize;
657     MSPI_ErrorNo errorno = E_MSPI_OK;
658     if(!MDrv_MSPI_HW_Support())
659     {
660         gbInitFlag = FALSE;
661         return E_MSPI_HW_NOT_SUPPORT;
662     }
663 
664     _gs32MSPI_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "Mutex MSPI", MSOS_PROCESS_SHARED);
665     MS_ASSERT(_gs32MSPI_Mutex >= 0);
666 
667     _u8MSPIDbgLevel = E_MSPI_DBGLV_NONE;
668 
669     if (!MDrv_MMIO_GetBASE( &VirtPMBank, &u32PMBankSize, MS_MODULE_PM))
670     {
671         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_PM_BANK\n"));
672     }
673 
674     if (!MDrv_MMIO_GetBASE( &VirtNONPMBank, &u32NONPMBankSize, MS_MODULE_HW))
675     {
676         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_NONPM_BANK\n"));
677         errorno = E_MSPI_MMIO_ERROR;
678     }
679     if(u8HWNum > 2)
680         return E_MSPI_PARAM_OVERFLOW;
681     HAL_MSPI_MMIOConfig(VirtPMBank, VirtNONPMBank, u8HWNum);
682 
683     if(tMSPIConfig->BIntEnable)
684     {
685         //register IRQ handler
686     }
687     memcpy(gu8MSPICurConfig, tMSPIConfig->U8BitMapofConfig, sizeof(gu8MSPICurConfig));
688 
689     errorno = _MDrv_ChipSelectSetting(tMSPIConfig->U8ChipSel);
690     if(errorno != E_MSPI_OK)
691         return errorno;
692     HAL_MSPI_IntEnable(tMSPIConfig->BIntEnable);
693     HAL_MSPI_Init();
694     gbInitFlag = TRUE;
695     return errorno;
696 }
697 
698 //-------------------------------------------------------------------------------------------------
699 /// Description : read data from MSPI
700 /// @param u32DevID IN: device id of slave device
701 /// @param pData \b IN :pointer to receive data from MSPI read buffer
702 /// @param u16Size \ b OTU : read data size
703 /// @return the errorno of operation
704 //-------------------------------------------------------------------------------------------------
MDrv_MasterSPI_Read(MS_U32 u32DevID,MS_U8 * pData,MS_U16 u16Size)705 MSPI_ErrorNo MDrv_MasterSPI_Read(MS_U32 u32DevID, MS_U8 *pData, MS_U16 u16Size)
706 {
707     MSPI_ErrorNo errorno = E_MSPI_OK;
708 
709     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
710     {
711         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
712         return E_MSPI_OPERATION_ERROR;
713     }
714 
715     //check init
716     if(!gbInitFlag)
717     {
718         errorno = E_MSPI_INIT_FLOW_ERROR;
719         goto MDrv_MasterSPI_Read_return;
720     }
721     if(_gu32DevID != u32DevID)
722     {
723         ULOGE(TAG_MSPI, "%s FAIL please call MDrv_MasterSPI_CsPadConfig first !!!\r\n",__FUNCTION__);
724         errorno = E_MSPI_INIT_FLOW_ERROR;
725         goto MDrv_MasterSPI_Read_return;
726     }
727 
728     // check config error
729     if(!(gu8MSPIConfig & MSPI_DC_CONFIG))
730         errorno |= E_MSPI_DCCONFIG_ERROR;
731     if(!(gu8MSPIConfig & MSPI_CLK_CONFIG))
732         errorno |= E_MSPI_CLKCONFIG_ERROR;
733     if(!(gu8MSPIConfig & MSPI_FRAME_CONFIG))
734         errorno |= E_MSPI_FRAMECONFIG_ERROR;
735     if(errorno != E_MSPI_OK)
736     {
737         // reset config
738         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "Read Operation MSPI config error %d\n",errorno));
739     }
740 
741     HAL_MSPI_Read(pData, u16Size);
742 MDrv_MasterSPI_Read_return:
743 	MsOS_ReleaseMutex(_gs32MSPI_Mutex);
744     return errorno;
745 }
746 
747 //------------------------------------------------------------------------------
748 /// Description : write data from MSPI
749 /// @param u32DevID IN: device id of slave device
750 /// @param pData \b OUT :pointer to write  data to MSPI write buffer
751 /// @param u16Size \ b OTU : write data size
752 /// @return the errorno of operation
753 //------------------------------------------------------------------------------
MDrv_MasterSPI_Write(MS_U32 u32DevID,MS_U8 * pData,MS_U16 u16Size)754 MSPI_ErrorNo MDrv_MasterSPI_Write(MS_U32 u32DevID, MS_U8 *pData, MS_U16 u16Size)
755 {
756     MSPI_ErrorNo errorno = E_MSPI_OK;
757 
758     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
759     {
760         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
761         return E_MSPI_OPERATION_ERROR;
762     }
763 
764     //check init
765     if(!gbInitFlag)
766     {
767         errorno = E_MSPI_INIT_FLOW_ERROR;
768         goto MDrv_MasterSPI_Write_return;
769     }
770 
771     if(_gu32DevID != u32DevID)
772     {
773         ULOGE(TAG_MSPI, "%s FAIL please call MDrv_MasterSPI_CsPadConfig first !!!\r\n",__FUNCTION__);
774         errorno = E_MSPI_INIT_FLOW_ERROR;
775         goto MDrv_MasterSPI_Write_return;
776     }
777 
778     // check config error
779     if(!(gu8MSPIConfig & MSPI_DC_CONFIG))
780         errorno |= E_MSPI_DCCONFIG_ERROR;
781     if(!(gu8MSPIConfig & MSPI_CLK_CONFIG))
782         errorno |= E_MSPI_CLKCONFIG_ERROR;
783     if(!(gu8MSPIConfig & MSPI_FRAME_CONFIG))
784         errorno |= E_MSPI_FRAMECONFIG_ERROR;
785     if(errorno != E_MSPI_OK)
786     {
787         // reset config
788         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "Write Operation MSPI config error %d\n",errorno));
789     }
790 
791     // write operation
792     HAL_MSPI_Wirte(pData, u16Size);
793 MDrv_MasterSPI_Write_return:
794     MsOS_ReleaseMutex(_gs32MSPI_Mutex);
795     return errorno;
796 }
797 
798 //------------------------------------------------------------------------------
799 /// Description : config spi transfer timing DCConfig==Delay Clock Config
800 /// @param u32DevID      \b IN device id of slave device
801 /// @param ptDCConfig    \b OUT  struct pointer of transfer timing config
802 /// @return E_MSPI_OK : succeed
803 /// @return E_MSPI_DCCONFIG_ERROR : failed to config transfer timing
804 //------------------------------------------------------------------------------
MDrv_MasterSPI_DCConfig(MS_U32 u32DevID,MSPI_DCConfig * ptDCConfig)805 MSPI_ErrorNo MDrv_MasterSPI_DCConfig(MS_U32 u32DevID, MSPI_DCConfig *ptDCConfig)
806 {
807     MSPI_ErrorNo errnum = E_MSPI_OK;
808 
809     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
810     {
811         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
812         return E_MSPI_OPERATION_ERROR;
813     }
814 
815     //check init
816     if(!gbInitFlag)
817     {
818         errnum = E_MSPI_INIT_FLOW_ERROR;
819         goto MDrv_MasterSPI_DCConfig_return;
820     }
821     if(_gu32DevID != u32DevID)
822     {
823         ULOGE(TAG_MSPI, "%s FAIL please call MDrv_MasterSPI_CsPadConfig first !!!\r\n",__FUNCTION__);
824         errnum =  E_MSPI_INIT_FLOW_ERROR;
825         goto MDrv_MasterSPI_DCConfig_return;
826     }
827 
828     if(ptDCConfig == NULL)
829     {
830         HAL_MSPI_Reset_DCConfig();
831         goto MDrv_MasterSPI_DCConfig_return;
832     }
833     errnum = _MDrv_DC_TrStartSetting(ptDCConfig->u8TrStart);
834     if(errnum != E_MSPI_OK)
835     {
836         errnum |= E_MSPI_DCCONFIG_ERROR;
837         goto MDrv_MasterSPI_DCConfig_return;
838     }
839     errnum = _MDrv_DC_TrEndSetting(ptDCConfig->u8TrEnd);
840     if(errnum != E_MSPI_OK)
841     {
842         errnum |= E_MSPI_DCCONFIG_ERROR;
843         goto MDrv_MasterSPI_DCConfig_return;
844     }
845     errnum = _MDrv_DC_TBSetting(ptDCConfig->u8TB);
846     if(errnum != E_MSPI_OK)
847     {
848         errnum |= E_MSPI_DCCONFIG_ERROR;
849         goto MDrv_MasterSPI_DCConfig_return;
850     }
851     errnum = _MDrv_DC_TRWSetting(ptDCConfig->u8TRW);
852     if(errnum != E_MSPI_OK)
853     {
854         errnum |= E_MSPI_DCCONFIG_ERROR;
855         goto MDrv_MasterSPI_DCConfig_return;
856     }
857     gu8MSPIConfig |= MSPI_DC_CONFIG;
858 
859 MDrv_MasterSPI_DCConfig_return:
860 
861     DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI DCconfig error errno =%d\n",errnum));
862     MsOS_ReleaseMutex(_gs32MSPI_Mutex);
863     return errnum;
864 }
865 
866 //------------------------------------------------------------------------------
867 /// Description : config spi clock setting
868 /// @param u32DevID       \b IN  device id of slave device
869 /// @param ptCLKConfig    \b OUT  struct pointer of clock config
870 /// @return E_MSPI_OK : succeed
871 /// @return E_MSPI_CLKCONFIG_ERROR : failed to config spi clock
872 //------------------------------------------------------------------------------
MDrv_MasterSPI_CLKConfig(MS_U32 u32DevID,MSPI_CLKConfig * ptCLKConfig)873 MSPI_ErrorNo MDrv_MasterSPI_CLKConfig(MS_U32 u32DevID, MSPI_CLKConfig *ptCLKConfig)
874 {
875     MSPI_ErrorNo errnum = E_MSPI_OK;
876     MS_BOOL bRet = FALSE;
877     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
878     {
879         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
880         return E_MSPI_OPERATION_ERROR;
881     }
882 
883     //check init
884     if(!gbInitFlag)
885     {
886         errnum = E_MSPI_INIT_FLOW_ERROR;
887         goto MDrv_MasterSPI_CLKConfig_return;
888     }
889     if(_gu32DevID != u32DevID)
890     {
891         ULOGE(TAG_MSPI, "%s FAIL please call MDrv_MasterSPI_CsPadConfig first !!!\r\n",__FUNCTION__);
892         errnum = E_MSPI_INIT_FLOW_ERROR;
893         goto MDrv_MasterSPI_CLKConfig_return;
894     }
895 
896     if(ptCLKConfig == NULL)
897     {
898         HAL_MSPI_Reset_CLKConfig();
899         goto MDrv_MasterSPI_CLKConfig_return;
900     }
901 
902     errnum = _MDrv_CLK_PolaritySetting(ptCLKConfig->BClkPolarity);
903     if(errnum != E_MSPI_OK)
904     {
905         errnum |= E_MSPI_CLKCONFIG_ERROR;
906         goto MDrv_MasterSPI_CLKConfig_return;
907     }
908     errnum = _MDrv_CLK_PhaseSetting(ptCLKConfig->BClkPhase);
909     if(errnum != E_MSPI_OK)
910     {
911         errnum |= E_MSPI_CLKCONFIG_ERROR;
912         goto MDrv_MasterSPI_CLKConfig_return;
913     }
914     bRet = HAL_MSPI_CLOCK_Config(ptCLKConfig->u32MAXClk);
915     if(bRet)
916         errnum = E_MSPI_OK;
917     else
918         errnum = E_MSPI_OPERATION_ERROR;
919     if(errnum != E_MSPI_OK)
920     {
921         errnum |= E_MSPI_CLKCONFIG_ERROR;
922         goto MDrv_MasterSPI_CLKConfig_return;
923     }
924     gu8MSPIConfig |= MSPI_CLK_CONFIG;
925 
926 MDrv_MasterSPI_CLKConfig_return:
927 	DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI CLKconfig error errno =%d\n",errnum));
928     MsOS_ReleaseMutex(_gs32MSPI_Mutex);
929     return errnum;
930 
931 }
932 
933 
934 //------------------------------------------------------------------------------
935 /// Description : config spi transfer timing
936 /// @param u32DevID      \b IN   device id of slave device
937 /// @param ptDCConfig    \b OUT  struct pointer of bits of buffer tranferred to slave config
938 /// @return E_MSPI_OK : succeed
939 /// @return E_MSPI_FRAMECONFIG_ERROR : failed to config transfered bit per buffer
940 //------------------------------------------------------------------------------
MDrv_MasterSPI_FRAMEConfig(MS_U32 u32DevID,MSPI_FrameConfig * ptFrameConfig)941 MSPI_ErrorNo MDrv_MasterSPI_FRAMEConfig(MS_U32 u32DevID, MSPI_FrameConfig *ptFrameConfig)
942 {
943     MSPI_ErrorNo errnum = E_MSPI_OK;
944     MS_U8 u8Index = 0;
945     MS_U8 u8BufSize;
946 
947     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
948     {
949         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
950         return E_MSPI_OPERATION_ERROR;
951     }
952 
953     //check init
954     if(!gbInitFlag)
955     {
956         errnum = E_MSPI_INIT_FLOW_ERROR;
957         goto MDrv_MasterSPI_FRAMEConfigreturn;
958     }
959     if(_gu32DevID != u32DevID)
960     {
961         ULOGE(TAG_MSPI, "%s FAIL please call MDrv_MasterSPI_CsPadConfig first !!!\r\n",__FUNCTION__);
962         errnum = E_MSPI_INIT_FLOW_ERROR;
963         goto MDrv_MasterSPI_FRAMEConfigreturn;
964     }
965 
966     if(ptFrameConfig == NULL)
967     {
968         HAL_MSPI_Reset_FrameConfig();
969         goto MDrv_MasterSPI_FRAMEConfigreturn;
970     }
971     // read buffer bit config
972     u8BufSize = sizeof(ptFrameConfig->u8RBitConfig);
973     for(u8Index = 0;u8Index < u8BufSize; u8Index++)
974     {
975         errnum = _MDrv_Frame_BitSetting(MSPI_READ_INDEX, u8Index, ptFrameConfig->u8RBitConfig[u8Index]);
976         if(errnum != E_MSPI_OK)
977         {
978             errnum |= E_MSPI_FRAMECONFIG_ERROR;
979             DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI FRAMEconfig error errno =%d\n",errnum));
980             goto MDrv_MasterSPI_FRAMEConfigreturn;
981         }
982     }
983     //write buffer bit config
984     u8BufSize = sizeof(ptFrameConfig->u8WBitConfig);
985     for(u8Index = 0;u8Index < u8BufSize; u8Index++)
986     {
987         errnum = _MDrv_Frame_BitSetting(MSPI_WRITE_INDEX, u8Index, ptFrameConfig->u8WBitConfig[u8Index]);
988         if(errnum != E_MSPI_OK)
989         {
990             errnum |= E_MSPI_FRAMECONFIG_ERROR;
991             DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "MSPI FRAMEconfig error errno =%d\n",errnum));
992             goto MDrv_MasterSPI_FRAMEConfigreturn;
993         }
994     }
995 MDrv_MasterSPI_FRAMEConfigreturn:
996     MsOS_ReleaseMutex(_gs32MSPI_Mutex);
997     gu8MSPIConfig |= MSPI_FRAME_CONFIG;
998     return errnum;
999 }
1000 
1001 //------------------------------------------------------------------------------
1002 /// Description : Enable Slave device
1003 //------------------------------------------------------------------------------
MDrv_MasterSPI_SlaveEnable(MS_U32 u32DevID,MS_BOOL Enable)1004 MSPI_ErrorNo MDrv_MasterSPI_SlaveEnable(MS_U32 u32DevID, MS_BOOL Enable)
1005 {
1006     MSPI_ErrorNo errnum = E_MSPI_OK;
1007     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
1008     {
1009         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
1010         return E_MSPI_OPERATION_ERROR;
1011     }
1012 
1013     if(_gu32DevID != u32DevID)
1014     {
1015         ULOGE(TAG_MSPI, "%s FAIL please call MDrv_MasterSPI_CsPadConfig first !!!\r\n",__FUNCTION__);
1016         errnum = E_MSPI_INIT_FLOW_ERROR;
1017         goto MDrv_MasterSPI_SlaveEnablereturn;
1018     }
1019 
1020     HAL_MSPI_SlaveEnable(Enable);
1021     if(Enable && _gu32CsPad != 0xFF)
1022     {
1023         mdrv_gpio_set_low(_gu32CsPad);
1024     }
1025     else if(_gu32CsPad != 0xFF)
1026     {
1027         mdrv_gpio_set_high(_gu32CsPad);
1028     }
1029 MDrv_MasterSPI_SlaveEnablereturn:
1030     MsOS_ReleaseMutex(_gs32MSPI_Mutex);
1031     return errnum;
1032 
1033 }
1034 
MDrv_MasterSPI_CsPadConfig(MS_U32 u32DevID,MS_U32 u32CsPad)1035 MS_BOOL MDrv_MasterSPI_CsPadConfig(MS_U32 u32DevID, MS_U32 u32CsPad)
1036 {
1037     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
1038     {
1039         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
1040         return E_MSPI_OPERATION_ERROR;
1041     }
1042 
1043     _gu32CsPad =  u32CsPad;
1044     _gu32DevID =  u32DevID;
1045     if(_gu32CsPad != 0xFF)
1046         mdrv_gpio_set_high(_gu32CsPad);
1047     MsOS_ReleaseMutex(_gs32MSPI_Mutex);
1048     return TRUE;
1049 }
1050 
MDrv_MasterSPI_MaxClkConfig(MS_U32 u32DevID,MS_U32 u32MaxClk)1051 MS_BOOL MDrv_MasterSPI_MaxClkConfig(MS_U32 u32DevID, MS_U32 u32MaxClk)
1052 {
1053     MS_BOOL bRet = FALSE;
1054     if (FALSE == MsOS_ObtainMutex(_gs32MSPI_Mutex, MSPI_MUTEX_WAIT_TIME))
1055     {
1056         ULOGE(TAG_MSPI, "%s ENTRY fails!\n", __FUNCTION__);
1057         return E_MSPI_OPERATION_ERROR;
1058     }
1059 
1060     if(_gu32DevID != u32DevID)
1061     {
1062         ULOGE(TAG_MSPI, "%s FAIL please call MDrv_MasterSPI_CsPadConfig first !!!\r\n",__FUNCTION__);
1063         goto MDrv_MasterSPI_MaxClkConfigreturn;
1064     }
1065     _gu32MaxClk = u32MaxClk;
1066     bRet = HAL_MSPI_CLOCK_Config(u32MaxClk);
1067 
1068 MDrv_MasterSPI_MaxClkConfigreturn:
1069     MsOS_ReleaseMutex(_gs32MSPI_Mutex);
1070     return bRet;
1071 }
1072 
1073 //------------------------------------------------------------------------------
1074 /// Description : config mspi info for local dimming
1075 /// @param stMspi_Info      \b IN   mspi info struct
1076 /// @return E_MSPI_OK : succeed
1077 /// @return E_MSPI_FRAMECONFIG_ERROR : failed to config mspi
1078 //------------------------------------------------------------------------------
MDrv_MSPI_Info_Config(ST_DRV_MSPI_INFO * stMspi_Info)1079 MSPI_ErrorNo MDrv_MSPI_Info_Config(ST_DRV_MSPI_INFO *stMspi_Info)
1080 {
1081     MSPI_ErrorNo errorno = E_MSPI_OK;
1082     MS_VIRT VirtNONPMBank = 0;
1083     MS_PHY  u32NONPMBankSize = 0;
1084     MS_VIRT VirtPMBank = 0;
1085     MS_PHY  u32PMBankSize = 0;
1086     MSPI_FrameConfig stFramconfig;
1087     if(stMspi_Info == NULL)
1088     {
1089         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI,"[Lwc Debug][%s__%d]arg error!\n",__FUNCTION__,__LINE__));
1090         return E_MSPI_OPERATION_ERROR;
1091     }
1092 
1093     if(!MDrv_MSPI_HW_Support())
1094     {
1095         gbInitFlag = FALSE;
1096         return E_MSPI_HW_NOT_SUPPORT;
1097     }
1098 //step 1: config mspi register base
1099     _gs32MSPI_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "Mutex MSPI", MSOS_PROCESS_SHARED);
1100     MS_ASSERT(_gs32MSPI_Mutex >= 0);
1101 
1102     //_u8MSPIDbgLevel = E_MSPI_DBGLV_DEBUG;
1103 
1104     if (!MDrv_MMIO_GetBASE( &VirtPMBank, &u32PMBankSize, MS_MODULE_PM))
1105     {
1106         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_PM_BANK\n"));
1107     }
1108 
1109     if (!MDrv_MMIO_GetBASE( &VirtNONPMBank, &u32NONPMBankSize, MS_MODULE_HW))
1110     {
1111         DEBUG_MSPI(E_MSPI_DBGLV_ERR, ULOGE(TAG_MSPI, "IOMap failure to get DRV_MMIO_NONPM_BANK\n"));
1112         errorno = E_MSPI_MMIO_ERROR;
1113     }
1114     if(stMspi_Info->u8MspiChanel > 2)
1115         return E_MSPI_PARAM_OVERFLOW;
1116     HAL_MSPI_MMIOConfig(VirtPMBank, VirtNONPMBank, stMspi_Info->u8MspiChanel);
1117 
1118 //step 2 : init mspi
1119     // default use CS0 for slave device && default use polling mode
1120     HAL_MSPI_IntEnable(0);
1121     HAL_MSPI_Init();
1122     errorno = _MDrv_ChipSelectSetting(0);
1123     gbInitFlag = TRUE;
1124 
1125 //step 3 :set mspi mode
1126     switch(stMspi_Info->u8MspiMode)
1127     {
1128         case 0 :    // CPOL = 0,CPHA =0
1129             _MDrv_CLK_PolaritySetting(0);
1130             _MDrv_CLK_PhaseSetting(0);
1131             break;
1132         case 1 :    // CPOL = 0,CPHA =1
1133             _MDrv_CLK_PolaritySetting(0);
1134             _MDrv_CLK_PhaseSetting(1);
1135             break;
1136         case 2 :    // CPOL = 1,CPHA =0
1137             _MDrv_CLK_PolaritySetting(1);
1138             _MDrv_CLK_PhaseSetting(0);
1139             break;
1140         case 3 :    // CPOL = 1,CPHA =1
1141             _MDrv_CLK_PolaritySetting(1);
1142             _MDrv_CLK_PhaseSetting(1);
1143             break;
1144         default:
1145             _MDrv_CLK_PolaritySetting(0);
1146             _MDrv_CLK_PhaseSetting(0);
1147             break;
1148     }
1149 //step 4 : set mspi clk
1150     HAL_MSPI_CLK_Config(stMspi_Info->u8MspiChanel,stMspi_Info->u32MspiClk);
1151 
1152 //step 5 : config DC timming
1153     HAL_MSPI_Reset_DCConfig();
1154 
1155     _MDrv_DC_TrStartSetting(stMspi_Info->u8TrStart);
1156 
1157     _MDrv_DC_TrEndSetting(stMspi_Info->u8TrEnd);
1158 
1159     _MDrv_DC_TBSetting(stMspi_Info->u8TB);
1160 
1161     _MDrv_DC_TRWSetting(stMspi_Info->u8TRW);
1162 
1163 //step 6: reset per byte bits
1164     memcpy(&stFramconfig.u8RBitConfig,stMspi_Info->u8RBitConfig,8);
1165     memcpy(&stFramconfig.u8WBitConfig,stMspi_Info->u8WBitConfig,8);
1166     MDrv_MSPI_FRAMEConfig(&stFramconfig);
1167     DEBUG_MSPI(E_MSPI_DBGLV_INFO, ULOGE(TAG_MSPI, "[Lwc Debug] MDrv_MSPI_LD_Config Pass!\n"));
1168     return errorno;
1169 }
1170 //-------------------------------------------------------------------------------------------------
1171 /// MOBF Encrypt
1172 /// @ingroup G_MSPI_ToBeRemove
1173 /// @param Direct \b IN: read or write flag 0:read 1:write
1174 /// @param u8Bytes \b IN: set read/write trigger buffer bytes size
1175 /// @return DRVAESDMA_OK : Success
1176 /// @return Others : Fail
1177 //-------------------------------------------------------------------------------------------------
MDrv_MSPI_RWBytes(MS_BOOL Direct,MS_U8 u8Bytes)1178 MSPI_ErrorNo MDrv_MSPI_RWBytes(MS_BOOL Direct, MS_U8 u8Bytes)
1179 {
1180     return HAL_MSPI_RWBytes(Direct,u8Bytes);
1181 }
1182