xref: /utopia/UTPA2-700.0.x/modules/vdec_v1/drv/mjpeg/drvMJPEG.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 ////////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    drvMJPEG.c
98 /// @brief  MJPEG Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 //  Include Files
105 //-------------------------------------------------------------------------------------------------
106 // Common Definition
107 #include "MsCommon.h"
108 #include "MsVersion.h"
109 #include "MsOS.h"
110 #include "halCHIP.h"
111 
112 #include "drvMJPEG.h"
113 //#include "regJPD.h"
114 #include "halMJPEG.h"
115 #include "halVPU.h"
116 
117 #define _JPD_DECODER_RENDER_ARCH 1
118 
119 #define JPD_DEBUG_DRV_MSG(format, args...)  do{if(u8JPD_DrvDbgLevel & E_JPD_DEBUG_DRV_MSG) printf(format, ##args);}while(0)
120 #define JPD_DEBUG_DRV_ERR(format, args...)  do{if(u8JPD_DrvDbgLevel & E_JPD_DEBUG_DRV_ERR) printf(format, ##args);}while(0)
121 
122 
123 
124 //-------------------------------------------------------------------------------------------------
125 //  Global Variables
126 //-------------------------------------------------------------------------------------------------
127 MS_BOOL gbEnableTurboFWMode = FALSE;
128 
129 static MS_U32 __u32FwAddr = 0xFFFFFFFF;
130 
131 static MS_U8 u8JPD_DrvDbgLevel = E_JPD_DEBUG_DRV_NONE;
132 
133 /* =============================== MJPEG =====================================*/
MDrv_MJPEG_SetDbgLevel(MS_U8 u8DbgLevel)134 void MDrv_MJPEG_SetDbgLevel(MS_U8 u8DbgLevel)
135 {
136     u8JPD_DrvDbgLevel = u8DbgLevel;
137     HAL_MJPEG_SetDbgLevel(u8DbgLevel);
138 }
139 
140 /******************************************************************************/
141 /// Stop VPU
142 /******************************************************************************/
MDrv_JPD_StopVPU(void)143 void MDrv_JPD_StopVPU(void)
144 {
145     HAL_JPD_StopVPU();
146 }
147 
148 /******************************************************************************/
149 /// Reset VPU
150 ///@return success or fail
151 /******************************************************************************/
MDrv_JPD_RstVPU(void)152 MS_BOOL MDrv_JPD_RstVPU(void)
153 {
154     return HAL_JPD_RstVPU();
155 }
156 
157 /******************************************************************************/
158 /// Release VPU
159 /******************************************************************************/
MDrv_JPD_ReleaseVPU(void)160 void MDrv_JPD_ReleaseVPU(void)
161 {
162     HAL_JPD_ReleaseVPU();
163 }
164 
165 /******************************************************************************/
166 ///Load JPD Firmware for MJPEG
167 ///@param u32Addr \b IN address to load firmware
168 ///@return success or fail
169 /******************************************************************************/
MDrv_JPD_LoadCode(MS_PHYADDR u32Addr,MS_U32 u32Size,MS_U32 u32BinAddr,MS_U8 u8FwSrcType)170 MS_BOOL MDrv_JPD_LoadCode(MS_PHYADDR u32Addr, MS_U32 u32Size, MS_U32 u32BinAddr, MS_U8 u8FwSrcType)
171 {
172     __u32FwAddr = u32Addr;
173     return HAL_JPD_LoadCode(u32Addr, u32Size, u32BinAddr, u8FwSrcType);
174 }
175 
176 /******************************************************************************/
177 ///Config SVD
178 ///@param u32StAddr \b IN: CPU binary code base address in DRAM.
179 ///@param u8dlend_en \b IN: endian
180 ///     - 1, little endian
181 ///     - 0, big endian
182 /******************************************************************************/
MDrv_JPD_ConfigVPU(MS_PHYADDR u32StAddr,MS_U8 u8dlend_en)183 void MDrv_JPD_ConfigVPU(MS_PHYADDR u32StAddr, MS_U8 u8dlend_en)
184 {
185     HAL_JPD_ConfigVPU(u32StAddr, u8dlend_en);
186 }
187 
188 /******************************************************************************/
189 ///Check if MJPEG firmware ready or not
190 ///@return TRUE or FALSE
191 /******************************************************************************/
MDrv_JPD_IsFirmwareReady(void)192 MS_BOOL MDrv_JPD_IsFirmwareReady(void)
193 {
194     MS_U32 u32FirmVer = 0, u32Timeout = 50000;
195 
196     // read VPU_RISC_MBOX0 to get firmware version
197     while(u32Timeout)
198     {
199         if(HAL_JPD_IsVPUMBoxReady(VPU_RISC_MBOX0))
200         {
201             HAL_JPD_ReadVPUMBox(VPU_RISC_MBOX0, (MS_U32 *)&u32FirmVer);
202             HAL_JPD_ClearVPUMBox(VPU_RISC_MBOX0, TRUE);
203             break;
204         }
205         u32Timeout--;
206     }
207     if(u32Timeout > 0)
208     {
209         JPD_DEBUG_DRV_MSG("MJPEG firmware version 0x%lx\n", u32FirmVer);
210     }
211     else
212     {
213         JPD_DEBUG_DRV_ERR("Cannot get MJPEG firmware version !! \n");
214         return FALSE;
215     }
216 
217     return TRUE;
218 }
219 
220 /******************************************************************************/
221 ///Set VPU Command
222 ///@return TRUE or FALSE
223 ///@param u8Cmd \b IN: VPU Command
224 ///     - JPD_CMD_SET_FRAME_BUFF_ADDR, Set frame buffer address
225 ///     - JPD_CMD_SET_FRAME_BUFF_SIZE, Set frame buffer size
226 /******************************************************************************/
MDrv_JPD_SetVPUCmd(MS_U32 u32Cmd)227 static MS_BOOL MDrv_JPD_SetVPUCmd(MS_U32 u32Cmd)
228 {
229     //currently we only use VPU_HI_MBOX0 to send command.
230     //MS_U32 u32TimeOut = MsOS_GetSystemTime() + _u32FrameTime;
231     MS_U32 u32TimeOut = 20000;
232     MS_BOOL bResult = FALSE;
233 
234     JPD_DEBUG_DRV_MSG("Send CMD 0x%lx to JPD firmware\n", u32Cmd);
235     //while(MsOS_GetSystemTime() < u32TimeOut)
236     while(u32TimeOut)
237     {
238         if(HAL_JPD_IsVPUMBoxReady(VPU_HI_MBOX0))
239         {
240             bResult = HAL_JPD_SendVPUMBox(VPU_HI_MBOX0, u32Cmd);
241             break;
242         }
243         u32TimeOut--;
244     }
245     if(u32TimeOut <= 0)
246     {
247         JPD_DEBUG_DRV_ERR("MJPEG send command timeout !! \n");
248         bResult = FALSE;
249     }
250 
251     return bResult;
252 }
253 
254 /******************************************************************************/
255 ///Set VPU command argument
256 ///@return TRUE or FALSE
257 ///@param u32Arg \b IN: VPU command argument
258 /******************************************************************************/
MDrv_JPD_SetVPUCmdArg(MS_U32 u32Arg)259 static MS_BOOL MDrv_JPD_SetVPUCmdArg(MS_U32 u32Arg)
260 {
261     // we use VPU_HI_MBOX1 to send command argument
262     //U16 u16TimeOut = 0xFFFF;
263     MS_U16 u16TimeOut = 20000;
264     MS_BOOL bResult = FALSE;
265 
266     JPD_DEBUG_DRV_MSG("Send argument 0x%lx to VPU \n", u32Arg);
267     while(--u16TimeOut)
268     {
269         if( HAL_JPD_IsVPUMBoxReady(VPU_HI_MBOX0)&& HAL_JPD_IsVPUMBoxReady(VPU_HI_MBOX1))
270         {
271             bResult = HAL_JPD_SendVPUMBox(VPU_HI_MBOX1, u32Arg);
272             break;
273         }
274     }
275     if(u16TimeOut <= 0)
276     {
277         JPD_DEBUG_DRV_ERR("MJPEG send command argument timeout !! \n");
278         bResult = FALSE;
279     }
280 
281     return bResult;
282 }
283 
284 /******************************************************************************/
285 ///Check VPU status
286 ///@param in \b IN in parameter
287 ///@param out \b OUT out parameter
288 ///@return status
289 /******************************************************************************/
MDrv_JPD_CheckVPUStatus(MS_U32 u32Cmd)290 static MS_U32 MDrv_JPD_CheckVPUStatus(MS_U32 u32Cmd)
291 {
292 //    MS_BOOL bResult = FALSE;
293     MS_U32 u32Status = 0, u32Timeout = 20000;
294 
295     JPD_DEBUG_DRV_MSG("Firmware status check:  CMD 0x%lx\n", u32Cmd);
296     while (u32Timeout) // Need timeout protection
297     {
298         if(HAL_JPD_IsVPUMBoxReady(VPU_HI_MBOX0))
299         {
300 //            bResult = HAL_JPD_SendVPUMBox(VPU_HI_MBOX0, u32Cmd);
301             HAL_JPD_SendVPUMBox(VPU_HI_MBOX0, u32Cmd);
302             break;
303         }
304         u32Timeout--;
305     }
306 
307     // Get firmware return value
308     u32Timeout = 20000;
309     while(u32Timeout)
310     {
311         if(HAL_JPD_IsVPUMBoxReady(VPU_RISC_MBOX0))
312         {
313             HAL_JPD_ReadVPUMBox(VPU_RISC_MBOX0, (MS_U32 *)&u32Status);
314             HAL_JPD_ClearVPUMBox(VPU_RISC_MBOX0, TRUE);
315             break;
316         }
317         u32Timeout--;
318     }
319 
320     JPD_DEBUG_DRV_MSG("Firmware status : 0x%lX\n", u32Status);
321     return u32Status;
322 }
323 
324 /******************************************************************************/
325 ///Set VPU command argument
326 ///@return TRUE or FALSE
327 ///@param u32Arg \b IN: VPU command argument
328 /******************************************************************************/
MDrv_JPD_SendVPUCommand(MJPEG_User_Cmd eCmd,MS_U32 u32Arg)329 MS_U32 MDrv_JPD_SendVPUCommand(MJPEG_User_Cmd eCmd, MS_U32 u32Arg)
330 {
331     MS_U32 u32Ret = 0;
332 
333 
334     switch (eCmd)
335     {
336     case E_MJPEG_CMD_GO:
337         MDrv_JPD_SetVPUCmd(E_JPD_CMD_GO);
338         break;
339 
340     case E_MJPEG_CMD_SET_FRAME_BUFF_START_ADDR:
341         MDrv_JPD_SetVPUCmdArg(u32Arg);
342         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_BUFF_START_ADDR);
343         break;
344 
345     case E_MJPEG_CMD_SET_FRAME_BUFF_UNIT_SIZE:
346         MDrv_JPD_SetVPUCmdArg(u32Arg);
347         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_BUFF_UNIT_SIZE);
348         break;
349 
350     case E_MJPEG_CMD_SET_FRAME_BUFF_TOTAL_NUM:
351         MDrv_JPD_SetVPUCmdArg(u32Arg);
352         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_BUFF_TOTAL_NUM);
353         break;
354 
355     case E_MJPEG_CMD_SET_FRAME_BUFF_IDX:
356         MDrv_JPD_SetVPUCmdArg(u32Arg);
357         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_BUFF_IDX);
358         break;
359 
360     case E_MJPEG_CMD_SET_FRAME_BUFF_IDX_READY:
361         MDrv_JPD_SetVPUCmdArg(u32Arg);
362         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_BUFF_IDX_READY);
363         break;
364 
365     case E_MJPEG_CMD_SET_WIDTH:
366         MDrv_JPD_SetVPUCmdArg(u32Arg);
367         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_WIDTH);
368         break;
369 
370     case E_MJPEG_CMD_SET_HEIGHT:
371         MDrv_JPD_SetVPUCmdArg(u32Arg);
372         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_HEIGHT);
373         break;
374 
375     case E_MJPEG_CMD_SET_PITCH:
376         MDrv_JPD_SetVPUCmdArg(u32Arg);
377         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_PITCH);
378         break;
379 
380     case E_MJPEG_CMD_SET_FRAME_ID_L:
381         MDrv_JPD_SetVPUCmdArg(u32Arg);
382         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_ID_L);
383         break;
384 
385     case E_MJPEG_CMD_SET_FRAME_ID_H:
386         MDrv_JPD_SetVPUCmdArg(u32Arg);
387         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_ID_H);
388         break;
389 
390     case E_MJPEG_CMD_SET_TIMESTAMP:
391         MDrv_JPD_SetVPUCmdArg(u32Arg);
392         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_TIMESTAMP);
393         break;
394 
395     case E_MJPEG_CMD_SET_FRAMERATE:
396         MDrv_JPD_SetVPUCmdArg(u32Arg);
397         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAMERATE);
398         break;
399 
400     case E_MJPEG_CMD_SET_FRAMERATE_BASE:
401         MDrv_JPD_SetVPUCmdArg(u32Arg);
402         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAMERATE_BASE);
403         break;
404 
405     case E_MJPEG_CMD_SET_FRAME_BUFF_IDX_VALID:
406         MDrv_JPD_SetVPUCmdArg(u32Arg);
407         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_FRAME_BUFF_IDX_VALID);
408         break;
409 
410     case E_MJPEG_CMD_SET_CHIP_ID:
411          MDrv_JPD_SetVPUCmdArg(u32Arg);
412          MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_CHIP_ID);
413          break;
414 
415     case E_MJPEG_CMD_PLAY:
416         u32Ret = (MS_U32)MDrv_JPD_SetVPUCmd(E_JPD_CMD_PLAY);
417         break;
418 
419     case E_MJPEG_CMD_PAUSE:
420         u32Ret = (MS_U32)MDrv_JPD_SetVPUCmd(E_JPD_CMD_PAUSE);
421         break;
422 
423     case E_MJPEG_CMD_RESUME:
424         u32Ret = (MS_U32)MDrv_JPD_SetVPUCmd(E_JPD_CMD_RESUME);
425         break;
426 
427     case E_MJPEG_CMD_STEP_PLAY:
428         u32Ret = (MS_U32)MDrv_JPD_SetVPUCmd(E_JPD_CMD_STEP_PLAY);
429         break;
430 
431     case E_MJPEG_CMD_SET_SPEED_TYPE:
432         MDrv_JPD_SetVPUCmdArg(u32Arg);
433         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_SPEED_TYPE);
434         break;
435 
436     case E_MJPEG_CMD_SET_SPEED:
437         MDrv_JPD_SetVPUCmdArg(u32Arg);
438         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_SPEED);
439         break;
440 
441     case E_MJPEG_CMD_FLUSH_DISP_QUEUE:
442         u32Ret = (MS_U32)MDrv_JPD_SetVPUCmd(E_JPD_CMD_FLUSH_DISP_QUEUE);
443         break;
444 
445     case E_MJPEG_CMD_FREEZE_DISP:
446         MDrv_JPD_SetVPUCmdArg(u32Arg);
447         MDrv_JPD_SetVPUCmd(E_JPD_CMD_FREEZE_DISP);
448         break;
449 
450     case E_MJPEG_CMD_ENABLE_AVSYNC:
451         MDrv_JPD_SetVPUCmdArg(u32Arg);
452         MDrv_JPD_SetVPUCmd(E_JPD_CMD_ENABLE_AVSYNC);
453         break;
454 
455     case E_MJPEG_CMD_SET_AVSYNC_DELAY:
456         MDrv_JPD_SetVPUCmdArg(u32Arg);
457         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_AVSYNC_DELAY);
458         break;
459 
460     case E_MJPEG_CMD_SET_AVSYNC_TOLERENCE:
461         MDrv_JPD_SetVPUCmdArg(u32Arg);
462         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_AVSYNC_TOLERENCE);
463         break;
464 
465     case E_MJPEG_CMD_SET_PTS_BASE:
466         MDrv_JPD_SetVPUCmdArg(u32Arg);
467         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_PTS_BASE);
468         break;
469 
470     case E_MJPEG_CMD_SET_STC_BASE:
471         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_STC_BASE);
472         break;
473 
474     case E_MJPEG_CMD_SET_BLUE_SCREEN:
475         MDrv_JPD_SetVPUCmdArg(u32Arg);
476         MDrv_JPD_SetVPUCmd(E_JPD_CMD_SET_BLUE_SCREEN);
477         break;
478 
479     case E_MJPEG_CMD_GET_NEXT_FREE_FRAME_BUFF_IDX:
480         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_GET_NEXT_FREE_FRAME_BUFF_IDX);
481         break;
482 
483     case E_MJPEG_CMD_COMPENSATE_PTS:
484         MDrv_JPD_SetVPUCmdArg(u32Arg);
485         MDrv_JPD_SetVPUCmd(E_JPD_CMD_COMPENSATE_PTS);
486         break;
487 
488     // Display Command Queue -- BEGIN
489     case E_MJPEG_CMD_ENABLE_DISP_CMD_QUEUE:
490 #if _JPD_DECODER_RENDER_ARCH
491         MDrv_JPD_SetVPUCmdArg(u32Arg);
492 #endif
493         MDrv_JPD_SetVPUCmd(E_JPD_CMD_ENABLE_DISP_CMD_QUEUE);
494         break;
495 
496     case E_MJPEG_CMD_PUSH_DISP_CMD:
497         MDrv_JPD_SetVPUCmdArg(u32Arg);
498         MDrv_JPD_SetVPUCmd(E_JPD_CMD_PUSH_DISP_CMD);
499         break;
500 
501     case E_MJPEG_CMD_GET_DISP_CMD_Q_VACANCY:
502         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_GET_DISP_CMD_Q_VACANCY);
503         break;
504     // Display Command Queue -- END
505 
506     case E_MJPEG_CMD_IS_STEP_PLAY_DONE:
507         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_IS_STEP_PLAY_DONE);
508         break;
509 
510     case E_MJPEG_CMD_IS_DISP_FINISH:
511         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_IS_DISP_FINISH);
512         break;
513 
514     case E_MJPEG_CMD_IS_PLAYING:
515         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_IS_PLAYING);
516         break;
517 
518     case E_MJPEG_CMD_IS_DISPLAY_QUEUE_FULL:
519         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_IS_DISPLAY_QUEUE_FULL);
520         break;
521 
522     case E_MJPEG_CMD_IS_AVSYNC_ON:
523         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_IS_AVSYNC_ON);
524         break;
525 
526     case E_MJPEG_CMD_IS_REACH_AVSYNC:
527         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_IS_REACH_AVSYNC);
528         break;
529 
530     case E_MJPEG_CMD_IS_FLUSH_DONE:
531         u32Ret = MDrv_JPD_CheckVPUStatus(E_JPD_CMD_IS_FLUSH_DONE);
532         break;
533 
534     default:
535         printf("    !!! Unknown MJPEG Command !!!\n");
536         u32Ret = 0xFFFFFFFF;
537         break;
538     }
539 
540 
541     return u32Ret;
542 }
543 
544 /******************************************************************************/
545 ///
546 ///@param value \b IN
547 ///@param value \b OUT
548 ///@return status
549 /******************************************************************************/
MDrv_JPD_IsVPUOnMiu1(void)550 MS_BOOL MDrv_JPD_IsVPUOnMiu1(void)
551 {
552     return HAL_JPD_IsVPUOnMiu1();
553 }
554 
555 /******************************************************************************/
556 ///
557 ///@param value \b IN
558 ///@param value \b OUT
559 ///@return status
560 /******************************************************************************/
MDrv_JPD_GetMiuBase(void)561 MS_U32 MDrv_JPD_GetMiuBase(void)
562 {
563     return HAL_JPD_GetMiuBase();
564 }
565 
566 /******************************************************************************/
567 ///Get ShareMemory address
568 ///@param u32Value \b IN new PTS value
569 /******************************************************************************/
MDrv_JPD_GetShareMemAddr(void)570 MS_U32 MDrv_JPD_GetShareMemAddr(void)
571 {
572     MS_U32 u32ShareMemAddr = 0xFFFFFFFF;
573 
574     if (__u32FwAddr != 0xFFFFFFFF)
575         u32ShareMemAddr = __u32FwAddr + JPD_SHARE_MEM_ST_OFFSET;
576     else
577         return 0xFFFFFFFF;
578 
579     u32ShareMemAddr = MS_PA2KSEG1((MS_U32)u32ShareMemAddr); //PhysicalAddr to VirtualAddr
580 
581     return u32ShareMemAddr;
582 }
583 
584 /******************************************************************************/
585 ///Description
586 ///@param in \b IN in parameter
587 ///@param out \b OUT out parameter
588 ///@return status
589 /******************************************************************************/
MDrv_JPD_ClearShareMemory(void)590 void MDrv_JPD_ClearShareMemory(void)
591 {
592     MS_U32 u32Idx = 0, u32Size;
593     volatile MS_U8 *u8Ptr = 0;
594 
595     u8Ptr = (volatile MS_U8 *)MDrv_JPD_GetShareMemAddr();
596     if((MS_U32)u8Ptr==0xFFFFFFFF)
597     {
598         JPD_DEBUG_DRV_ERR("Cannot get pShareMem correctly!!!\n");
599         return;
600     }
601 
602     u32Size = sizeof(JPD_ShareMem);
603 
604     for (u32Idx = 0; u32Idx < u32Size; u32Idx++)
605     {
606         *u8Ptr = 0;
607         u8Ptr++;
608     }
609     return;
610 }
611 
612 /******************************************************************************/
613 ///Get PTS table address
614 ///@param u32Value \b IN new PTS value
615 /******************************************************************************/
MDrv_JPD_GetPtsTableAddr(void)616 MS_U32 MDrv_JPD_GetPtsTableAddr(void)
617 {
618     MS_U32 u32PtsAddr = 0xFFFFFFFF;
619 
620     if (__u32FwAddr != 0xFFFFFFFF)
621         u32PtsAddr = __u32FwAddr + JPD_PTS_TABLE_ST_OFFSET;
622     else
623         return 0xFFFFFFFF;
624 
625     u32PtsAddr = MS_PA2KSEG1((MS_U32)u32PtsAddr); //PhysicalAddr to VirtualAddr
626 
627     return u32PtsAddr;
628 }
629 
630 /******************************************************************************/
631 ///Reset PTS value
632 ///@param u32Value \b IN new PTS value
633 /******************************************************************************/
MDrv_JPD_ResetPTS(MS_U32 u32PtsBase)634 MS_BOOL MDrv_JPD_ResetPTS(MS_U32 u32PtsBase)
635 {
636 
637     MDrv_JPD_SendVPUCommand(E_MJPEG_CMD_SET_PTS_BASE, u32PtsBase);
638 
639     return TRUE;
640 }
641 
642 /******************************************************************************/
643 ///Return PTS of current displayed frame
644 ///@return PTS
645 /******************************************************************************/
MDrv_JPD_GetPTS(void)646 MS_U32 MDrv_JPD_GetPTS(void)
647 {
648     volatile JPD_ShareMem *pShareMem = NULL;
649 
650     pShareMem = (JPD_ShareMem *)MDrv_JPD_GetShareMemAddr();
651     if((MS_U32)pShareMem==0xFFFFFFFF)
652     {
653         JPD_DEBUG_DRV_ERR("Cannot get pShareMem correctly!!!\n");
654         return NULL;
655     }
656 
657     return pShareMem->infoDispFrame.u32TimeStamp;
658 }
659 
660 /******************************************************************************/
661 ///Description
662 ///@param in \b IN in parameter
663 ///@param out \b OUT out parameter
664 ///@return status
665 /******************************************************************************/
MDrv_JPD_DispFrame(MS_U32 u32DispIdx)666 void MDrv_JPD_DispFrame(MS_U32 u32DispIdx)
667 {
668 #if _JPD_DECODER_RENDER_ARCH
669     volatile JPD_ShareMem *pShareMem = NULL;
670     pShareMem = (JPD_ShareMem *)MDrv_JPD_GetShareMemAddr();
671     if((MS_U32)pShareMem==0xFFFFFFFF)
672     {
673         JPD_DEBUG_DRV_ERR("Cannot get pShareMem correctly!!!\n");
674         return;
675     }
676 
677     if (pShareMem->DispQueue[u32DispIdx].u32Status == E_JPD_DISPQ_STATUS_VIEW)
678     {
679         pShareMem->DispQueue[u32DispIdx].u32Status = E_JPD_DISPQ_STATUS_DISP;
680     }
681 #endif
682 }
683 
684 /******************************************************************************/
685 ///Description
686 ///@param in \b IN in parameter
687 ///@param out \b OUT out parameter
688 ///@return status
689 /******************************************************************************/
MDrv_JPD_FreeFrame(MS_U32 u32DispIdx)690 void MDrv_JPD_FreeFrame(MS_U32 u32DispIdx)
691 {
692 #if _JPD_DECODER_RENDER_ARCH
693     volatile JPD_ShareMem *pShareMem = NULL;
694     pShareMem = (JPD_ShareMem *)MDrv_JPD_GetShareMemAddr();
695     if((MS_U32)pShareMem==0xFFFFFFFF)
696     {
697         JPD_DEBUG_DRV_ERR("Cannot get pShareMem correctly!!!\n");
698         return;
699     }
700 
701     if (pShareMem->DispQueue[u32DispIdx].u32Status == E_JPD_DISPQ_STATUS_VIEW)
702     {
703         pShareMem->DispQueue[u32DispIdx].u32Status = E_JPD_DISPQ_STATUS_FREE;
704     }
705 #endif
706 }
707 
708 /******************************************************************************/
709 ///Description
710 ///@param in \b IN in parameter
711 ///@param out \b OUT out parameter
712 ///@return status
713 /******************************************************************************/
714 #define JPD_DISPQ_PREFETCH_COUNT        2
715 static MS_U16 _u16DispQPtr = 0;
MDrv_JPD_GetNextDispFrame()716 void* MDrv_JPD_GetNextDispFrame()
717 {
718 #if _JPD_DECODER_RENDER_ARCH
719     volatile JPD_ShareMem *pShareMem = NULL;
720     pShareMem = (JPD_ShareMem *)MDrv_JPD_GetShareMemAddr();
721     if((MS_U32)pShareMem==0xFFFFFFFF)
722     {
723         JPD_DEBUG_DRV_ERR("Cannot get pShareMem correctly!!!\n");
724         return NULL;
725     }
726     MS_U16 u16QNum = pShareMem->u16DispQNumb;
727     MS_U16 u16QPtr = pShareMem->u16DispQPtr;
728     MJPG_FrameInfo *pFrm;
729 
730 #if 0
731     if (u16QNum > JPD_DISPQ_PREFETCH_COUNT) u16QNum = JPD_DISPQ_PREFETCH_COUNT;
732 #endif
733 
734     while (u16QNum != 0)
735     {
736         pFrm = (MJPG_FrameInfo*)&pShareMem->DispQueue[u16QPtr];
737 
738         if (pFrm->u32Status == E_JPD_DISPQ_STATUS_INIT)
739         {
740             _u16DispQPtr = u16QPtr;
741             pFrm->u32Status = E_JPD_DISPQ_STATUS_VIEW; /////Change its state!!
742             JPD_DEBUG_DRV_MSG("FrameDone: %d, pFrm=0x%lx, timestamp=%ld\n", u16QPtr, (MS_U32)pFrm, pFrm->u32TimeStamp);
743             return pFrm;
744         }
745         u16QNum--;
746         u16QPtr++;
747         if (u16QPtr == pShareMem->u16DispQSize) u16QPtr = 0; //wrap to the begin
748     }
749 #endif
750     return NULL;
751 }
752 
753 /******************************************************************************/
754 ///Description
755 ///@param in \b IN in parameter
756 ///@param out \b OUT out parameter
757 ///@return status
758 /******************************************************************************/
MDrv_JPD_GetNextDispQPtr()759 MS_U32 MDrv_JPD_GetNextDispQPtr()
760 {
761     return _u16DispQPtr;
762 }
763 
764 /******************************************************************************/
765 ///Description
766 ///@param in \b IN in parameter
767 ///@param out \b OUT out parameter
768 ///@return status
769 /******************************************************************************/
MDrv_JPD_GetDispFrameInfo(void * pInfo)770 void MDrv_JPD_GetDispFrameInfo(void *pInfo)
771 {
772     MJPG_FrameInfo *pFrmInfo = (MJPG_FrameInfo *)pInfo;
773 
774     volatile JPD_ShareMem *pShareMem = NULL;
775     pShareMem = (JPD_ShareMem *)MDrv_JPD_GetShareMemAddr();
776     if((MS_U32)pShareMem==0xFFFFFFFF)
777     {
778         JPD_DEBUG_DRV_ERR("Cannot get pShareMem correctly!!!\n");
779         return;
780     }
781 
782     pFrmInfo->u32LumaAddr = pShareMem->infoDispFrame.u32LumaAddr;
783     pFrmInfo->u32ChromaAddr = pShareMem->infoDispFrame.u32ChromaAddr;
784     pFrmInfo->u32TimeStamp = pShareMem->infoDispFrame.u32TimeStamp;
785     pFrmInfo->u32ID_L = pShareMem->infoDispFrame.u32ID_L;
786     pFrmInfo->u32ID_H = pShareMem->infoDispFrame.u32ID_H;
787 }
788 
MDrv_JPD_EnableTurboFWMode(MS_BOOL bEnable)789 void MDrv_JPD_EnableTurboFWMode(MS_BOOL bEnable)
790 {
791     gbEnableTurboFWMode = bEnable;
792 }
793 
MDrv_JPD_InitFw(MS_PHYADDR u32Addr,MS_U32 u32Size,MS_PHYADDR u32BinAddr,MS_U8 u8FwSrcType)794 JPD_Return MDrv_JPD_InitFw(MS_PHYADDR u32Addr, MS_U32 u32Size, MS_PHYADDR u32BinAddr, MS_U8 u8FwSrcType)
795 {
796     MS_BOOL fw_on_miu1 = FALSE;
797 
798     JPD_DEBUG_DRV_MSG("MDrv_JPD_InitFw u32Addr = 0x%lx, u32Size = 0x%lx, u32BinAddr = 0x%lx, u8FwSrcType = %d\n", u32Addr, u32Size, u32BinAddr, u8FwSrcType);
799 
800     if (u32Addr > JPD_MIU_ADDR_MASK)
801     {
802         fw_on_miu1 = TRUE;
803     }
804 
805 #if !(defined(CHIP_T2) || defined(CHIP_T7))
806     if(FALSE == HAL_JPD_InitVPU(fw_on_miu1))
807 #else
808     if(FALSE == HAL_JPD_InitVPU())
809 #endif
810     {
811         JPD_DEBUG_DRV_ERR("HAL_JPD_InitVPU failed!!!\n");
812         return E_JPD_INVALID_PARAM;
813     }
814 
815     // Reset VPU
816     if(!HAL_JPD_RstVPU())
817     {
818         JPD_DEBUG_DRV_ERR("HAL_JPD_RstVPU failed!!!\n");
819         return E_JPD_FAILED;
820     }
821 
822     // Load JPD firmware
823     __u32FwAddr = u32Addr;
824     if(FALSE == HAL_JPD_LoadCode(u32Addr, u32Size, u32BinAddr, u8FwSrcType))
825     {
826         JPD_DEBUG_DRV_ERR("HAL_JPD_LoadCode failed!!!\n");
827         return E_JPD_FAILED;
828     }
829 
830     // Set VPU base and endian
831     HAL_JPD_ConfigVPU(u32Addr, 1); // 1 for little endian
832 
833     // Clear ShareMemory before releasing VPU
834     MDrv_JPD_ClearShareMemory();
835 
836     // Release VPU
837     HAL_JPD_ReleaseVPU();
838     return E_JPD_OK;
839 }
840 
MDrv_JPD_Get_Chip_ID(void)841 MJPEG_MSTAR_CHIP_ID MDrv_JPD_Get_Chip_ID(void)
842 {
843     return HAL_JPD_Get_Chip_ID();
844 }
845 
846 
847