xref: /utopia/UTPA2-700.0.x/modules/audio/hal/mainz/audsp/halAUDSP.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 //  Include Files
97 //-------------------------------------------------------------------------------------------------
98 // Common Definition
99 #include <string.h>
100 #include "ULog.h"
101 #include "MsCommon.h"
102 #include "MsIRQ.h"
103 #include "MsOS.h"
104 #include "MsTypes.h"
105 
106 #include "drvBDMA.h"
107 #include "drvSERFLASH.h"
108 
109 #include "regCHIP.h"
110 
111 #include "halAUDSP.h"
112 #include "drvAUDIO.h"
113 #include "../audio/regAUDIO.h"
114 #include "../audio/halAUDIO.h"
115 #include "../audio/halSIF.h"
116 #include "../audio/halADVAUDIO.h"
117 #include "../audio/ddr_config.h"
118 #include "DecoderInfo.h"
119 
120 #define LOAD_CODE_DEBUG  0
121 
122 #define HALAUDSP_CHECK_SHM_INIT \
123     do { \
124         if (g_AudioVars2 == NULL) \
125         { \
126             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_ERROR, "%s() : Warning! g_AudioVars2 should not be NULL !!!\n", __FUNCTION__); \
127             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_ERROR, "%s() : Perform SHM Init here !!!\n", __FUNCTION__); \
128             if (HAL_AUDIO_InitialVars() == FALSE) \
129             { \
130                 MS_ASSERT(0); \
131             } \
132         } \
133     } while(0)
134 
135 //-------------------------------------------------------------------------------------------------
136 //  Driver Compiler Options
137 //-------------------------------------------------------------------------------------------------
138 #ifdef CONFIG_MBOOT //mboot Speed up
139   #define DSP_VERIFY_DSP_CODE     0   // 0: don't verify dsp code (for speed)
140                                     // 1: verify dsp code (for debug)
141   #define DSP_IDMA_CHK_READY      1   // 0: don't check IDMA ready (for speed)
142                                     // 1: check IDMA ready (for debug)
143 #else
144   #define DSP_VERIFY_DSP_CODE     1   // 0: don't verify dsp code (for speed)
145                                     // 1: verify dsp code (for debug)
146   #define DSP_IDMA_CHK_READY      1   // 0: don't check IDMA ready (for speed)
147                                     // 1: check IDMA ready (for debug)
148 #endif
149 
150 #define AU_NULL  0
151 
152 LOAD_CODE_INFO g_loadcodeinfo;
153 MS_BOOL        g_bDSPLoadCode = FALSE;
154 
155 #ifndef MSOS_TYPE_NOS
156 void* MDrv_MPool_PA2KSEG1(void* pAddrPhys);
157 #endif
158 //-------------------------------------------------------------------------------------------------
159 //  Local Defines
160 //-------------------------------------------------------------------------------------------------
161 #define LOU8(MS_U16Val)  ( (MS_U8)(MS_U16Val) )
162 #define HIU8(MS_U16Val)  ( (MS_U8)((MS_U16Val) >> 8) )
163 
164 //-------------------------------------------------------------------------------------------------
165 //  Local Structures
166 //-------------------------------------------------------------------------------------------------
167 
168 //-------------------------------------------------------------------------------------------------
169 //  Global Variables
170 //-------------------------------------------------------------------------------------------------
171 extern MS_BOOL g_bAudio_loadcode_from_dram;
172 extern AUDIO_SHARED_VARS2 * g_AudioVars2;
173 
174 extern MS_S32  _s32AUDIOMutexIDMA;
175 
176 #ifndef MSOS_TYPE_NUTTX
177 extern AUDIO_TEE_INFO_SHARE_MEM   *pAudioTeeInfoShm;
178 #endif
179 
180 
181 //-------------------------------------------------------------------------------------------------
182 //  Local Variables
183 //-------------------------------------------------------------------------------------------------
184 static MS_U8 g_u8DspCodeTypeLoaded = 0;
185 
186 
187 //-------------------------------------------------------------------------------------------------
188 //  Debug Functions
189 //-------------------------------------------------------------------------------------------------
190 
191 
192 //-------------------------------------------------------------------------------------------------
193 //  Local Functions
194 //-------------------------------------------------------------------------------------------------
195 
196 
197 //-------------------------------------------------------------------------------------------------
198 //  Global Functions
199 //-------------------------------------------------------------------------------------------------
200 ////////////////////////////////////////////////////////////////////////////////
201 /// @brief \b Function \b Name: HAL_AUDSP_DspLoadCode()
202 /// @brief \b Function \b Description:  load CM/PM/cache/prefetch DSP code
203 /// @param <IN>        \b \b u8Type    :      -- DSP load code type
204 ///                                        DSP_segment -- select DSP load code seg
205 /// @param <OUT>       \b NONE    :
206 /// @param <RET>       \b NONE    :
207 /// @param <GLOBAL>    \b NONE    :
208 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_DspLoadCode(MS_U8 u8Type)209 MS_BOOL HAL_AUDSP_DspLoadCode(MS_U8 u8Type)
210 {
211 #ifndef MSOS_TYPE_NUTTX
212    REE_TO_TEE_MBX_MSG_TYPE msg_type = REE_TO_TEE_MBX_MSG_NULL;
213 #endif
214     MS_PHY MIU_addr;
215     AUDIO_ALG_INFO *pCodeTableinfo=NULL;
216     LOAD_CODE_ARCH_TYPE eArchType = LOAD_INVALID;
217 
218    HALAUDSP_CHECK_SHM_INIT;
219 
220     switch(u8Type)
221     {
222 
223         case AU_DEC_SYSTEM:
224             break;
225 
226         case AU_SE_SYSTEM:
227             pCodeTableinfo = &SE_SYSTEM_LoadCodeTable[0];
228             eArchType = LOAD_SYSTEM;
229             break;
230 
231         case AU_DVB_DEC_NONE:
232         case AU_DVB2_NONE:
233             pCodeTableinfo = &DEC_LoadCodeTable[0];
234             eArchType = LOAD_DECODER;
235             break;
236 
237         case AU_DVB_STANDARD_MPEG:
238         case AU_DVB2_STANDARD_MPEG_AD:
239             pCodeTableinfo = &MPEG_LoadCodeTable[0];
240             eArchType = LOAD_DECODER;
241             break;
242 
243         case AU_DVB_STANDARD_AAC:
244         case AU_DVB_STANDARD_MS10_DDT:
245         case AU_DVB2_STANDARD_GAAC_AD:
246             pCodeTableinfo = &GAAC_LoadCodeTable[0];
247             eArchType = LOAD_DECODER;
248             break;
249 
250         case AU_DVB_STANDARD_MS10_DDC:
251         case AU_DVB_STANDARD_AC3:
252         case AU_DVB_STANDARD_AC3P:
253         case AU_DVB2_STANDARD_AC3_AD:
254         case AU_DVB2_STANDARD_AC3P_AD:
255             pCodeTableinfo = &AC3P_LoadCodeTable[0];
256             eArchType = LOAD_DECODER;
257             break;
258 
259         case AU_DVB_STANDARD_MP3:
260         case AU_DVB2_STANDARD_MP3:
261             pCodeTableinfo = &MP3_LoadCodeTable[0];
262             eArchType = LOAD_DECODER;
263             break;
264 
265         case AU_DVB_STANDARD_WMA:
266         case AU_DVB_STANDARD_WMA_PRO:
267             pCodeTableinfo = &WMA_PRO_LoadCodeTable[0];
268             eArchType = LOAD_DECODER;
269             break;
270 
271         case AU_DVB_STANDARD_XPCM:
272         case AU_DVB2_STANDARD_XPCM:
273             pCodeTableinfo = &XPCM_LoadCodeTable[0];
274             eArchType = LOAD_DECODER;
275             break;
276 
277         case AU_DVB_STANDARD_RA8LBR:
278             pCodeTableinfo = &RA8LBR_LoadCodeTable[0];
279             eArchType = LOAD_DECODER;
280            break;
281 
282         case AU_DVB_STANDARD_FLAC:
283             pCodeTableinfo = &FLAC_LoadCodeTable[0];
284             eArchType = LOAD_DECODER;
285             break;
286 
287         case AU_DVB_STANDARD_VORBIS:
288             pCodeTableinfo = &VORBIS_LoadCodeTable[0];
289             eArchType = LOAD_DECODER;
290             break;
291 
292         case AU_DVB_STANDARD_DTS:
293         case AU_DVB2_STANDARD_DTS:
294             pCodeTableinfo = &DTS_DMP_LoadCodeTable[0];
295             eArchType = LOAD_DECODER;
296             break;
297 
298         case AU_DVB_STANDARD_DTSLBR:
299             pCodeTableinfo = &DTS_LBR_LoadCodeTable[0];
300             eArchType = LOAD_DECODER;
301             break;
302 
303         case AU_DVB_STANDARD_DRA:
304             pCodeTableinfo = &DRAD_LoadCodeTable[0];
305             eArchType = LOAD_DECODER;
306             break;
307 
308         case AU_DVB_STANDARD_AMR_NB:
309             pCodeTableinfo = &AMR_NB_LoadCodeTable[0];
310             eArchType = LOAD_DECODER;
311             break;
312 
313         case AU_DVB_STANDARD_AMR_WB:
314             pCodeTableinfo = &AMR_WB_LoadCodeTable[0];
315             eArchType = LOAD_DECODER;
316             break;
317 
318         case AU_SIF_PALSUM:
319             pCodeTableinfo = &PALSUM_LoadCodeTable[0];
320             eArchType = LOAD_DECODER;
321             break;
322 
323         case AU_SIF_BTSC:
324             pCodeTableinfo = &BTSC_LoadCodeTable[0];
325             eArchType = LOAD_DECODER;
326             break;
327 
328         case AU_DVB2_ADVSND_TSHD:
329             pCodeTableinfo = &TSHD_LoadCodeTable[0];
330             eArchType = LOAD_ADVSOUND;
331             break;
332 
333         case AU_DVB2_ADVSND_SRS:
334             pCodeTableinfo = &TSXT_LoadCodeTable[0];
335             eArchType = LOAD_ADVSOUND;
336             break;
337 
338         case AU_DVB2_ADVSND_PURESND:
339             pCodeTableinfo = &PURESND_LoadCodeTable[0];
340             eArchType = LOAD_ADVSOUND;
341             break;
342 
343         case AU_DVB2_ADVSND_DBX:
344             pCodeTableinfo = &DBX_LoadCodeTable[0];
345             eArchType = LOAD_ADVSOUND;
346             break;
347 
348         case AU_DVB2_ADVSND_BONGIOVI_DPS:
349             pCodeTableinfo = &BONGIOVIDPS_LoadCodeTable[0];
350             eArchType = LOAD_ADVSOUND;
351             break;
352 
353         default :
354             eArchType = LOAD_INVALID;
355             return FALSE;
356     };
357 
358 #ifndef MSOS_TYPE_NUTTX
359     AUDIO_TEE_INFO_SHM_CHECK_NULL;
360     REE_TO_TEE_MBX_MSG_SET_PARAM(0, (MS_U8)u8Type);
361     REE_TO_TEE_MBX_MSG_SET_PARAM_COUNT(1);
362     msg_type = REE_TO_TEE_MBX_MSG_HAL_AUDSP_DspLoadCode;
363         msg_type = msg_type | REE_TO_TEE_MBX_MSG_TYPE_SE;
364 
365     if ( TEE_TO_REE_MBX_ACK_MSG_NO_TEE != (mbx_msg_ack_status = HAL_AUDIO_SendMBXMsg(msg_type)))
366     {
367         if (mbx_msg_ack_status != TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS)
368             return FALSE;
369         HAL_AUDSP_SetDspCodeTypeLoaded(u8Type);
370         return TRUE;
371     }
372 #endif
373 
374     if(pCodeTableinfo == NULL)
375     {
376         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "\r\n=======NULL return==========\r\n");
377         return FALSE;
378     }
379     else if(pCodeTableinfo->cm_len == 0)
380     {
381         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "\r\n========cm length =0=========\r\n");
382         return FALSE;
383     }
384 
385     HAL_AUDSP_SetDspLoadCodeInfo(pCodeTableinfo, DSP_SE);
386     HAL_AUDIO_WriteMaskByte(REG_FD230_SELECT, 0x01, 0x01);
387 
388 //======== Load DSP PM ========
389 #if(LOAD_CODE_DEBUG)
390         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PM addr: 0x%x\r\n", pCodeTableinfo->pm_addr);
391     #if defined (__aarch64__)
392         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PM buf addr: 0x%lx\r\n", (MS_VIRT)pCodeTableinfo->pm_buf);
393     #else
394         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PM buf addr: 0x%x\r\n", (MS_VIRT)pCodeTableinfo->pm_buf);
395     #endif
396         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PM size: 0x%x\r\n",pCodeTableinfo->pm_len);
397 #endif
398 
399     if(!HAL_AUDSP_DspLoadCodeSegment(pCodeTableinfo->pm_addr, pCodeTableinfo->pm_buf+6, pCodeTableinfo->pm_len, DSP_SE))
400         return FALSE;
401     if(!HAL_AUDSP_DspVerifySegmentCode(pCodeTableinfo->pm_addr, pCodeTableinfo->pm_buf+6, pCodeTableinfo->pm_len, DSP_SE))
402         return FALSE;
403 
404  //======== Load DSP Prefetch ========
405 #if(LOAD_CODE_DEBUG)
406         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PreFetch PM addr: 0x%x\r\n", pCodeTableinfo->prefetch_addr);
407     #if defined (__aarch64__)
408         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PreFetch PM buf addr: 0x%lx\r\n", (MS_VIRT)pCodeTableinfo->prefetch_buf);
409     #else
410         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PreFetch PM buf addr: 0x%x\r\n", (MS_VIRT)pCodeTableinfo->prefetch_buf);
411     #endif
412         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    PreFetch PM size: 0x%x\r\n", pCodeTableinfo->prefetch_len);
413 #endif
414 
415     if(pCodeTableinfo->prefetch_len != 0)
416     {
417         MIU_addr = pCodeTableinfo->prefetch_addr * 3 + HAL_AUDIO_GetDspMadBaseAddr(DSP_SE);
418 #if defined (__aarch64__)
419         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    MIU of PreFetch: 0x%08lx\r\n", (unsigned int)(MS_VIRT)MIU_addr);
420 #else
421         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    MIU of PreFetch: 0x%08x\r\n", (unsigned int)(MS_VIRT)MIU_addr);
422 #endif
423         memcpy((void*)(MS_PA2KSEG1(MIU_addr)), (void*)((pCodeTableinfo->prefetch_buf+6)), pCodeTableinfo->prefetch_len);
424     }
425 
426 //======== Load DSP Cache ========
427 #if(LOAD_CODE_DEBUG)
428         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    Cache PM addr: 0x%x\r\n", pCodeTableinfo->cache_addr);
429     #if defined (__aarch64__)
430         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    Cache PM buf addr: 0x%lx\r\n", (MS_VIRT)pCodeTableinfo->cache_buf);
431     #else
432         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    Cache PM buf addr: 0x%x\r\n", (MS_VIRT)pCodeTableinfo->cache_buf);
433     #endif
434         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    Cache PM size: 0x%x\r\n", pCodeTableinfo->cache_len);
435 #endif
436 
437     if(pCodeTableinfo->cache_len != 0)
438     {
439         MIU_addr = pCodeTableinfo->cache_addr * 3 + HAL_AUDIO_GetDspMadBaseAddr(DSP_SE);
440 #if defined (__aarch64__)
441         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    MIU of Cache: 0x%08lx\r\n", (unsigned int)(MS_VIRT)MIU_addr);
442 #else
443         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    MIU of Cache: 0x%08x\r\n", (unsigned int)(MS_VIRT)MIU_addr);
444 #endif
445         memcpy((void*)(MS_PA2KSEG1(MIU_addr)), (void*)((pCodeTableinfo->cache_buf+6)), pCodeTableinfo->cache_len);
446     }
447 
448 //======== Load DSP CM ========
449 #if(LOAD_CODE_DEBUG)
450         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    CM addr: 0x%x\r\n", pCodeTableinfo->cm_addr);
451     #if defined (__aarch64__)
452         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    CM buf addr: 0x%lx\r\n", (MS_VIRT)pCodeTableinfo->cm_buf);
453     #else
454         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    CM buf addr: 0x%x\r\n", (MS_VIRT)pCodeTableinfo->cm_buf);
455     #endif
456         AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "    CM size: 0x%x\r\n", pCodeTableinfo->cm_len);
457 #endif
458 
459     if(eArchType==LOAD_SYSTEM)
460     {
461         // First 2 words will be loaded later
462         if(!HAL_AUDSP_DspLoadCodeSegment(0x02, pCodeTableinfo->cm_buf+12, pCodeTableinfo->cm_len-6, DSP_SE))
463             return FALSE;
464         if(!HAL_AUDSP_DspVerifySegmentCode(0x02, pCodeTableinfo->cm_buf+12, pCodeTableinfo->cm_len-6, DSP_SE))
465             return FALSE;
466 
467         // When Load  SE-DSP  system code , write address 0  to reset DSP
468         if(!HAL_AUDSP_DspLoadCodeSegment(0x0001,  mst_codec_pm1+6+3, 21, DSP_SE)) return FALSE;
469         if(!HAL_AUDSP_DspVerifySegmentCode(0x0001,  mst_codec_pm1+6+3, 21, DSP_SE)) return FALSE;
470         if(!HAL_AUDSP_DspLoadCodeSegment(0x0000,  mst_codec_pm1+6, 3, DSP_SE)) return FALSE;
471         if(!HAL_AUDSP_DspVerifySegmentCode(0x0000,  mst_codec_pm1+6, 3, DSP_SE)) return FALSE;
472     }
473     else
474     {
475         if(!HAL_AUDSP_DspLoadCodeSegment(pCodeTableinfo->cm_addr, pCodeTableinfo->cm_buf+6, pCodeTableinfo->cm_len, DSP_SE))
476             return FALSE;
477         if(!HAL_AUDSP_DspVerifySegmentCode(pCodeTableinfo->cm_addr, pCodeTableinfo->cm_buf+6, pCodeTableinfo->cm_len, DSP_SE))
478             return FALSE;
479     }
480 
481     MsOS_FlushMemory();
482 
483     HAL_AUDSP_SetDspCodeTypeLoaded(u8Type);
484     AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "HAL_AUDSP_DspLoadCode finished(type=%s(0x%x))\r\n", pCodeTableinfo->AlgName, u8Type);
485     return TRUE;
486 }
487 
488 ////////////////////////////////////////////////////////////////////////////////
489 /// @brief \b Function \b Name: HAL_AUDIO_DspLoadCodeSegment()
490 /// @brief \b Function \b Description: This routine is used to load DSP code
491 /// @param <IN>        \b dsp_addr    :
492 /// @param <IN>        \b dspCode_buf    :
493 /// @param <IN>        \b dspCode_buflen    :
494 /// @param <OUT>       \b NONE    :
495 /// @param <RET>       \b  BOOL    :    TRUE --DSP Load code okay
496 ///                                    FALSE--DSP Load code fail
497 /// @param <GLOBAL>    \b NONE    :
498 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_DspLoadCodeSegment(MS_U32 dsp_addr,MS_U8 * dspCode_buf,MS_U32 dspCode_buflen,MS_U8 DSP_select)499 MS_BOOL HAL_AUDSP_DspLoadCodeSegment(MS_U32 dsp_addr, MS_U8  *dspCode_buf, MS_U32 dspCode_buflen, MS_U8 DSP_select)
500 {
501     MS_U32 i,j;
502     MS_U32 idma_wrbase_addr_l, dsp_brg_data_l, dsp_brg_data_h, bdma_mode_addr;
503 
504     HALAUDSP_CHECK_SHM_INIT;
505 
506     OS_OBTAIN_MUTEX(_s32AUDIOMutexIDMA, MSOS_WAIT_FOREVER);
507 
508     g_bDSPLoadCode = TRUE;
509 
510     if(DSP_select == DSP_DEC)
511     {
512         idma_wrbase_addr_l = REG_DEC_IDMA_WRBASE_ADDR_L;
513         dsp_brg_data_l = REG_DEC_DSP_BRG_DATA_L;
514         dsp_brg_data_h = REG_DEC_DSP_BRG_DATA_H;
515         bdma_mode_addr = REG_DEC_BDMA_CFG;
516         HAL_AUDIO_WriteMaskByte(REG_FD230_SELECT, 0x01, 0x00);
517     }
518     else
519     {
520         idma_wrbase_addr_l = REG_SE_IDMA_WRBASE_ADDR_L;
521         dsp_brg_data_l = REG_SE_DSP_BRG_DATA_L;
522         dsp_brg_data_h = REG_SE_DSP_BRG_DATA_H;
523         bdma_mode_addr = REG_SE_BDMA_CFG;
524         HAL_AUDIO_WriteMaskByte(REG_FD230_SELECT, 0x01, 0x01);
525     }
526 
527     HAL_AUDIO_WriteMaskReg(bdma_mode_addr, 0x8080, 0x0000); // disable bdma
528 
529     if(dspCode_buflen>0)
530     {
531         /* set iDMA addr */
532         HAL_AUDIO_WriteReg(idma_wrbase_addr_l, dsp_addr);
533 
534         AUDIO_DELAY1MS(1);
535 
536         for( i=0; i<dspCode_buflen; i+=3)
537         {
538             HAL_AUDIO_WriteByte(dsp_brg_data_l,*(dspCode_buf+i+1));
539             HAL_AUDIO_WriteByte(dsp_brg_data_h,*(dspCode_buf+i+2));
540 
541             for(j=0;j<2;j++);//for delay only
542 
543             HAL_AUDIO_WriteByte(dsp_brg_data_l,*(dspCode_buf+i));
544             HAL_AUDIO_WriteByte(dsp_brg_data_h,0x00);
545 
546             if(DSP_select == DSP_DEC)
547             {
548                 if (HAL_AUDSP_CheckDecIdmaReady(AUD_CHK_DSP_WRITE_RDY)==FALSE)
549                 {
550                     g_bDSPLoadCode = FALSE;
551                     OS_RELEASE_MUTEX(_s32AUDIOMutexIDMA);
552                     return FALSE;
553                 }
554             }
555             else
556             {
557                 if (HAL_AUDSP_CheckSeIdmaReady(AUD_CHK_DSP_WRITE_RDY)==FALSE)
558                 {
559                     g_bDSPLoadCode = FALSE;
560                     OS_RELEASE_MUTEX(_s32AUDIOMutexIDMA);
561                     return FALSE;
562                 }
563             }
564         }
565     }
566 
567     g_bDSPLoadCode = FALSE;
568 
569     OS_RELEASE_MUTEX(_s32AUDIOMutexIDMA);
570 
571     return TRUE;
572 }
573 
HAL_AUDSP_DspVerifySegmentCode(MS_U32 dsp_addr,MS_U8 * dspCode_buf,MS_U32 dspCode_buflen,MS_U8 DSP_select)574 MS_BOOL HAL_AUDSP_DspVerifySegmentCode(MS_U32 dsp_addr, MS_U8 *dspCode_buf, MS_U32 dspCode_buflen, MS_U8 DSP_select)
575 {
576     MS_U32 i;
577     MS_U8 dat[3];
578     MS_U32 idma_rdbase_addr_l, idma_ctrl0, idma_rddata_h_0, idma_rddata_h_1, idma_rddata_l, bdma_mode_addr;
579 
580 #if (DSP_VERIFY_DSP_CODE==0)
581     return TRUE;                        //don't verify just return;
582 #endif
583 
584     HALAUDSP_CHECK_SHM_INIT;
585 
586     OS_OBTAIN_MUTEX(_s32AUDIOMutexIDMA, MSOS_WAIT_FOREVER);
587 
588     g_bDSPLoadCode = TRUE;
589 
590     if ( DSP_select == DSP_DEC )
591     {
592         idma_rdbase_addr_l = REG_DEC_IDMA_RDBASE_ADDR_L;
593         idma_ctrl0 = REG_DEC_IDMA_CTRL0;
594         idma_rddata_h_0 = REG_DEC_IDMA_RDDATA_H_0;
595         idma_rddata_h_1 = REG_DEC_IDMA_RDDATA_H_1;
596         idma_rddata_l = REG_DEC_IDMA_RDDATA_L;
597         bdma_mode_addr = REG_DEC_BDMA_CFG;
598         HAL_AUDIO_WriteMaskByte(REG_FD230_SELECT, 0x01, 0x00);
599     }
600     else
601     {
602         idma_rdbase_addr_l = REG_SE_IDMA_RDBASE_ADDR_L;
603         idma_ctrl0 = REG_SE_IDMA_CTRL0;
604         idma_rddata_h_0 = REG_SE_IDMA_RDDATA_H_0;
605         idma_rddata_h_1 = REG_SE_IDMA_RDDATA_H_1;
606         idma_rddata_l = REG_SE_IDMA_RDDATA_L;
607         bdma_mode_addr = REG_SE_BDMA_CFG;
608         HAL_AUDIO_WriteMaskByte(REG_FD230_SELECT, 0x01, 0x01);
609     }
610 
611     //HAL_AUDIO_WriteMaskByte(0x3CEC, 0x40, 0x00);
612     HAL_AUDIO_WriteMaskReg(bdma_mode_addr, 0x8080, 0x0000); // disable bdma (enable idma)
613     HAL_AUDIO_WriteReg(idma_rdbase_addr_l, dsp_addr);
614     AUDIO_DELAY1MS(1);
615 
616     for (i=0; i<dspCode_buflen; i+=3)
617     {
618         HAL_AUDIO_WriteMaskByte(idma_ctrl0, 0x08, 0x08 );
619 #if (DSP_IDMA_CHK_READY == 1)
620      if(DSP_select == DSP_DEC)
621         {
622          if (HAL_AUDSP_CheckDecIdmaReady(AUD_CHK_DSP_READ_RDY)==FALSE)
623              {
624                 g_bDSPLoadCode = FALSE;
625 
626                 OS_RELEASE_MUTEX(_s32AUDIOMutexIDMA);
627 
628                 return FALSE;
629              }
630         }
631         else
632         {
633             if (HAL_AUDSP_CheckSeIdmaReady(AUD_CHK_DSP_READ_RDY)==FALSE)
634             {
635                 g_bDSPLoadCode = FALSE;
636 
637                 OS_RELEASE_MUTEX(_s32AUDIOMutexIDMA);
638 
639                 return FALSE;
640             }
641         }
642 #endif
643         dat[1] = HAL_AUDIO_ReadByte(idma_rddata_h_0);
644         dat[2] = HAL_AUDIO_ReadByte(idma_rddata_h_1);
645         dat[0] = HAL_AUDIO_ReadByte(idma_rddata_l);
646 
647         //AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "@@%x\n", (dat[2] << 16) | (dat[1] << 8) | (dat[0]));
648 
649         if ( (dat[0]!=dspCode_buf[i]) || (dat[1]!=dspCode_buf[i+1]) ||
650                 (dat[2]!=dspCode_buf[i+2]))
651         {
652 #if defined (CONFIG_UTOPIA_ANDROID_L)
653             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "check data %x\n", i);
654 #else
655             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "check data %x\n", (unsigned int)i);
656 #endif
657             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "dat0 %X <===> ",dspCode_buf[i]);
658             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "%x \n", dat[0]);
659             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "dat1 %X <===> ",dspCode_buf[i+1]);
660             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "%x \n", dat[1]);
661             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "dat2 %x <===> ",dspCode_buf[i+2]);
662             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "%X \n", dat[2]);
663             AUDIO_PRINT(AUDIO_DEBUG_LEVEL_INFO, "  Dsp code verify error!!\r\n");
664 
665             g_bDSPLoadCode = FALSE;
666 
667             OS_RELEASE_MUTEX(_s32AUDIOMutexIDMA);
668 
669             return FALSE;
670         }
671     }
672 
673     AUDIO_PRINT(AUDIO_DEBUG_LEVEL_DEBUG, "  Dsp code verify ok!!\n\r");
674 
675     g_bDSPLoadCode = FALSE;
676 
677     OS_RELEASE_MUTEX(_s32AUDIOMutexIDMA);
678 
679     return TRUE;
680 }
681 
682 ////////////////////////////////////////////////////////////////////////////////
683 /// @brief \b Function \b Name: HAL_AUDSP_CheckDecIdmaReady()
684 /// @brief \b Function \b Description:  This routine is used to check if the Dec-DSP IDMA is ready or not.
685 /// @param <IN>        \b IdmaChk_type    :
686 ///                                    0x10 : check write ready
687 ///                                    0x80 : check read  ready
688 /// @param <OUT>       \b NONE    :
689 /// @param <RET>       \b MS_BOOL    : TRUE--IDMA is ready
690 ///                                      FALSE--IDMA not ready
691 /// @param <GLOBAL>    \b NONE    :
692 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_CheckDecIdmaReady(MS_U8 u8IdmaChk_type)693 MS_BOOL HAL_AUDSP_CheckDecIdmaReady(MS_U8 u8IdmaChk_type )
694 {
695     MS_U8  j = 0;
696 
697     while(j<200)
698     {
699         j++;
700         if( (HAL_AUDIO_ReadByte(REG_DEC_IDMA_CTRL0)& u8IdmaChk_type) == 0 )
701             return TRUE;
702     }
703 
704     AUDIO_PRINT(AUDIO_DEBUG_LEVEL_DEBUG, "DSP DEC Idma check ready fail!(%d)\r\n",j);
705     return FALSE;
706 }
707 
708 ////////////////////////////////////////////////////////////////////////////////
709 /// @brief \b Function \b Name: HAL_AUDIO_CheckSeIdmaReady()
710 /// @brief \b Function \b Description:  This routine is used to check if the Se-DSP IDMA is ready or not.
711 /// @param <IN>        \b IdmaChk_type    :
712 ///                                    0x10 : check write ready
713 ///                                    0x80 : check read  ready
714 /// @param <OUT>       \b NONE    :
715 /// @param <RET>       \b MS_BOOL    : TRUE--IDMA is ready
716 ///                                      FALSE--IDMA not ready
717 /// @param <GLOBAL>    \b NONE    :
718 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_CheckSeIdmaReady(MS_U8 u8IdmaChk_type)719 MS_BOOL HAL_AUDSP_CheckSeIdmaReady(MS_U8 u8IdmaChk_type)
720 {
721     MS_U8  j = 0;
722 
723     while(j<200)
724     {
725         j++;
726         if( (HAL_AUDIO_ReadByte(REG_SE_IDMA_CTRL0)& u8IdmaChk_type) == 0 )
727             return TRUE;
728     }
729 
730     AUDIO_PRINT(AUDIO_DEBUG_LEVEL_DEBUG, "DSP SE Idma check ready fail!(%d)\r\n",j);
731     return FALSE;
732 }
733 
734 ////////////////////////////////////////////////////////////////////////////////
735 /// @brief \b Function \b Name: HAL_AUDSP_SetDspCodeTypeLoaded()
736 /// @brief \b Function \b Description:  This function is used to set the DSP code type.
737 /// @param <IN>        \b NONE    :
738 /// @param <OUT>       \b NONE    :
739 /// @param <RET>       \b MS_U8: DSP code type.
740 /// @param <GLOBAL>    \b NONE    :
741 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_SetDspCodeTypeLoaded(MS_U8 u8Type)742 void HAL_AUDSP_SetDspCodeTypeLoaded(MS_U8 u8Type)
743 {
744     g_u8DspCodeTypeLoaded=u8Type;
745 }
746 
747 ////////////////////////////////////////////////////////////////////////////////
748 /// @brief \b Function \b Name: HAL_AUDSP_GetDspCodeTypeLoaded()
749 /// @brief \b Function \b Description:  This function is used to get the MAD base address.
750 /// @param <IN>        \b NONE    :
751 /// @param <OUT>       \b NONE    :
752 /// @param <RET>       \b MS_U8: DSP code type.
753 /// @param <GLOBAL>    \b NONE    :
754 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_GetDspCodeTypeLoaded(void)755 MS_U8 HAL_AUDSP_GetDspCodeTypeLoaded(void)
756 {
757     return g_u8DspCodeTypeLoaded;
758 }
759 
HAL_AUDSP_SetDspLoadCodeInfo(AUDIO_ALG_INFO * pau_info,MS_U8 DSP_select)760 void HAL_AUDSP_SetDspLoadCodeInfo(AUDIO_ALG_INFO *pau_info, MS_U8 DSP_select)
761 {
762     g_loadcodeinfo.pau_info = pau_info;
763     g_loadcodeinfo.DSP_select= DSP_select;
764 }
765 
766 ////////////////////////////////////////////////////////////////////////////////
767 /// @brief \b Function \b Name: HAL_AUDSP_GetDspCodeTypeLoaded()
768 /// @brief \b Function \b Description:  This function is used to get the MAD base address.
769 /// @param <IN>        \b NONE    :
770 /// @param <OUT>       \b NONE    :
771 /// @param <RET>       \b MS_U8: DSP code type.
772 /// @param <GLOBAL>    \b NONE    :
773 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_GetDspLoadCodeInfo(void)774 LOAD_CODE_INFO* HAL_AUDSP_GetDspLoadCodeInfo(void)
775 {
776     return &g_loadcodeinfo;
777 }
778