xref: /utopia/UTPA2-700.0.x/modules/audio/hal/k6lite/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 #ifdef MSOS_TYPE_LINUX_KERNEL
100 #include <linux/string.h>
101 #else
102 #include <string.h>
103 #endif
104 
105 #include "MsCommon.h"
106 #include "MsIRQ.h"
107 #include "MsOS.h"
108 #include "MsTypes.h"
109 
110 #include "drvBDMA.h"
111 #include "drvSERFLASH.h"
112 
113 #include "regCHIP.h"
114 
115 #include "halAUDSP.h"
116 #include "drvAUDIO.h"
117 #include "regAUDIO.h"
118 #include "halAUDIO.h"
119 #include "halMAD2.h"
120 #include "halSIF.h"
121 #include "halADVAUDIO.h"
122 #include "ddr_config.h"
123 #include "r2_shm_comm.h"
124 #include "decR2_shm.h"
125 #if ASND_R2_SUPPORT
126 #include "sndR2_shm.h"
127 #endif
128 
129 #ifdef CONFIG_MBOOT//Mboot Mini system
130     #include "dspcode_s/aucode_mboot_r2.c"
131 #else
132     #include "dspcode_s/aucode_adec_r2.c"
133 #if ASND_R2_SUPPORT
134     #include "dspcode_s/aucode_asnd_r2.c"
135 #endif
136 #endif
137 
138 
139 
140 #if defined(MSOS_TYPE_NUTTX)
141 extern int lib_lowprintf(const char *format, ...);
142 #define DBG_PREFIX lib_lowprintf
143 #else
144 #define DBG_PREFIX printf
145 #endif
146 
147 #if (defined ANDROID)
148 #include <sys/mman.h>
149 #include <cutils/ashmem.h>
150 #include <cutils/log.h>
151 
152 #ifndef LOGI // android 4.1 rename LOGx to ALOGx
153 #define LOGI ALOGI
154 #endif
155 
156 #ifndef LOGE // android 4.1 rename LOGx to ALOGx
157 #define LOGE ALOGE
158 #endif
159 
160 #define HALAUDSP_PRINT(fmt, args...)    LOGI("<<android>>      " fmt, ## args)
161 #define HALAUDSP_ERROR(fmt, args...)    LOGE("<<android>>      " fmt, ## args)
162 #else
163 #define HALAUDSP_PRINT(fmt, args...)    DBG_PREFIX("[[utopia]]      " fmt, ## args)
164 #define HALAUDSP_ERROR(fmt, args...)    DBG_PREFIX("[[utopia]]      " fmt, ## args)
165 #endif
166 
167 #define HALAUDSP_CHECK_SHM_INIT \
168     do { \
169         if (g_AudioVars2 == NULL) \
170         { \
171             HALAUDSP_ERROR("%s() : Warning! g_AudioVars2 should not be NULL !!!\n", __FUNCTION__); \
172             HALAUDSP_ERROR("%s() : Perform SHM Init here !!!\n", __FUNCTION__); \
173             if (HAL_AUDIO_InitialVars() == FALSE) \
174             { \
175                 MS_ASSERT(0); \
176             } \
177         } \
178     } while(0)
179 
180 //-------------------------------------------------------------------------------------------------
181 //  Driver Compiler Options
182 //-------------------------------------------------------------------------------------------------
183 
184 #define DBG_AUDSP_LOAD(args...)   //DBG_PREFIX(args)
185 #define DBG_AUDSP(args...)        //DBG_PREFIX(args)
186 #define DBG_AUDSP_ERROR(args...)  //DBG_PREFIX(args);
187 
188 #ifdef CONFIG_MBOOT //mboot Speed up
189   #define DSP_VERIFY_DSP_CODE     0   // 0: don't verify dsp code (for speed)
190                                     // 1: verify dsp code (for debug)
191   #define DSP_IDMA_CHK_READY      1   // 0: don't check IDMA ready (for speed)
192                                     // 1: check IDMA ready (for debug)
193 #else
194   #define DSP_VERIFY_DSP_CODE     1   // 0: don't verify dsp code (for speed)
195                                     // 1: verify dsp code (for debug)
196   #define DSP_IDMA_CHK_READY      1   // 0: don't check IDMA ready (for speed)
197                                     // 1: check IDMA ready (for debug)
198 #endif
199 
200 #define AU_NULL  0
201 
202 #ifdef CONFIG_MBOOT
203     #define FLASH_COPY(x,y,z,i,j)   TRUE
204     #define FLASH_CHKDONE()         TRUE
205 #else
206     #define FLASH_COPY(x,y,z,i,j)   MDrv_SERFLASH_CopyHnd(x,y,z,i,j)
207     #define FLASH_CHKDONE()         MDrv_FLASH_CheckWriteDone()
208 #endif
209 
210 LOAD_CODE_INFO g_loadcodeinfo;
211 MS_BOOL        g_bDSPLoadCode = FALSE;
212 
213 #ifndef CONFIG_MBOOT
214 AUDIO_ALG_INFO dsp_info[]=
215 {
216     // ---------- Decoder2 DSP Code ----------
217     //AU_DVB_SYS_NONE[0]
218     {
219         AU_NULL, AU_NULL, AU_NULL,
220         AU_NULL, AU_NULL, AU_NULL,
221         AU_NULL, AU_NULL, AU_NULL,
222         AU_NULL, AU_NULL, AU_NULL,
223         "DVB2_NONE",
224         AU_DVB2_NONE,
225     },
226     // SE System
227     {
228         AU_NULL, AU_NULL, AU_NULL,
229         AU_NULL, AU_NULL, AU_NULL,
230         AU_NULL, AU_NULL, AU_NULL,
231         AU_NULL, AU_NULL, AU_NULL,
232         "SE SYSTEM",
233         AU_SE_SYSTEM,
234     },
235     //DDE
236     {
237         AU_NULL, AU_NULL, AU_NULL,
238         AU_NULL, AU_NULL, AU_NULL,
239         AU_NULL, AU_NULL, AU_NULL,
240         AU_NULL, AU_NULL, AU_NULL,
241         "dde",
242         AU_DVB2_STANDARD_DDE,
243     },
244     //BTSC
245     {
246         AU_NULL, AU_NULL, AU_NULL,
247         AU_NULL, AU_NULL, AU_NULL,
248         AU_NULL, AU_NULL, AU_NULL,
249         AU_NULL, AU_NULL, AU_NULL,
250         "SIF BTSC",
251         AU_SIF_BTSC,
252     },
253     // PALSUM
254     {
255         AU_NULL, AU_NULL, AU_NULL,
256         AU_NULL, AU_NULL, AU_NULL,
257         AU_NULL, AU_NULL, AU_NULL,
258         AU_NULL, AU_NULL, AU_NULL,
259         "SIF PALSUM",
260         AU_SIF_PALSUM,
261     },
262     // audio_adv_sndeff_info
263     //DBX
264     {
265         AU_NULL, AU_NULL, AU_NULL,
266         AU_NULL, AU_NULL, AU_NULL,
267         AU_NULL, AU_NULL, AU_NULL,
268         AU_NULL, AU_NULL, AU_NULL,
269         "ADVSND DBX",
270         AU_DVB2_ADVSND_DBX,
271     },
272      //PURESND
273     {
274         AU_NULL, AU_NULL, AU_NULL,
275         AU_NULL, AU_NULL, AU_NULL,
276         AU_NULL, AU_NULL, AU_NULL,
277         AU_NULL, AU_NULL, AU_NULL,
278         "ADVSND PURESND",
279         AU_DVB2_ADVSND_PURESND,
280     },
281     //DPS
282     {
283         AU_NULL, AU_NULL, AU_NULL,
284         AU_NULL, AU_NULL, AU_NULL,
285         AU_NULL, AU_NULL, AU_NULL,
286         AU_NULL, AU_NULL, AU_NULL,
287         "ADVSND BONGIOVI_DPS",
288         AU_DVB2_ADVSND_BONGIOVI_DPS,
289     },
290     //DTSE
291     {
292         AU_NULL, AU_NULL, AU_NULL,
293         AU_NULL, AU_NULL, AU_NULL,
294         AU_NULL, AU_NULL, AU_NULL,
295         AU_NULL, AU_NULL, AU_NULL,
296         "DTSE",
297         AU_DVB2_ENCODE_DTSE,
298     },
299     //DDE
300     {
301         AU_NULL, AU_NULL, AU_NULL,
302         AU_NULL, AU_NULL, AU_NULL,
303         AU_NULL, AU_NULL, AU_NULL,
304         AU_NULL, AU_NULL, AU_NULL,
305         "DDE",
306         AU_DVB2_ENCODE_DDE,
307     },
308     //MS10-DDE
309     {
310         AU_NULL, AU_NULL, AU_NULL,
311         AU_NULL, AU_NULL, AU_NULL,
312         AU_NULL, AU_NULL, AU_NULL,
313         AU_NULL, AU_NULL, AU_NULL,
314         "MS10-DDE",
315         AU_DVB2_ENCODE_MS10_DDE,
316     },
317 
318 };
319 #else //=======================CONFIG_MBOOT=========================================//
320 AUDIO_ALG_INFO dsp_info[]=
321 {
322     // ---------- Decoder2 DSP Code ----------
323     //AU_DVB_SYS_NONE[0]
324     {
325         AU_NULL, AU_NULL, AU_NULL,
326         AU_NULL, AU_NULL, AU_NULL,
327         AU_NULL, AU_NULL, AU_NULL,
328         AU_NULL, AU_NULL, AU_NULL,
329         "DVB2_NONE",
330         AU_DVB2_NONE,
331     },
332     // SE System
333     {
334         AU_NULL, AU_NULL, AU_NULL,
335         AU_NULL, AU_NULL, AU_NULL,
336         AU_NULL, AU_NULL, AU_NULL,
337         AU_NULL, AU_NULL, AU_NULL,
338         "SE SYSTEM",
339         AU_SE_SYSTEM,
340     },
341 };
342 #endif
343 
344 #ifndef MSOS_TYPE_NOS
345 void* MDrv_MPool_PA2KSEG1(void* pAddrPhys);
346 #endif
347 //-------------------------------------------------------------------------------------------------
348 //  Local Defines
349 //-------------------------------------------------------------------------------------------------
350 #define AUDIO_HAL_ERR(x, args...)        //{printf(x, ##args);}
351 #define LOU8(MS_U16Val)  ( (MS_U8)(MS_U16Val) )
352 #define HIU8(MS_U16Val)  ( (MS_U8)((MS_U16Val) >> 8) )
353 
354 //-------------------------------------------------------------------------------------------------
355 //  Local Structures
356 //-------------------------------------------------------------------------------------------------
357 
358 
359 //-------------------------------------------------------------------------------------------------
360 //  Global Variables
361 //-------------------------------------------------------------------------------------------------
362 extern MS_BOOL g_bAudio_loadcode_from_dram;
363 extern AUDIO_SHARED_VARS2 * g_AudioVars2;
364 
365 extern MS_S32  _s32AUDIOMutexIDMA;
366 
367 #ifndef MSOS_TYPE_NUTTX
368 extern AUDIO_TEE_INFO_SHARE_MEM   *pAudioTeeInfoShm;
369 #endif
370 
371 
372 //-------------------------------------------------------------------------------------------------
373 //  Local Variables
374 //-------------------------------------------------------------------------------------------------
375 static AUDIO_DSP_CODE_TYPE g_DspCodeTypeLoaded = AU_DVB_NONE;
376 
377 //-------------------------------------------------------------------------------------------------
378 //  Debug Functions
379 //-------------------------------------------------------------------------------------------------
380 
381 
382 //-------------------------------------------------------------------------------------------------
383 //  Local Functions
384 //-------------------------------------------------------------------------------------------------
385 
386 
387 //-------------------------------------------------------------------------------------------------
388 //  Global Functions
389 //-------------------------------------------------------------------------------------------------
390 ////////////////////////////////////////////////////////////////////////////////
391 /// @brief \b Function \b Name: MDrv_AUDIO_DspLoadCodeKernel()
392 /// @brief \b Function \b Description:  load CM/PM/cache/prefetch DSP code
393 /// @param <IN>        \b \b u8Type    :      -- DSP load code type
394 ///                                        DSP_segment -- select DSP load code seg
395 ///                                     DSP_select -- select DSP1 or DSP2
396 /// @param <OUT>       \b NONE    :
397 /// @param <RET>       \b NONE    :
398 /// @param <GLOBAL>    \b NONE    :
399 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_DspLoadCode(AUDIO_DSP_CODE_TYPE dspCodeType)400 MS_BOOL HAL_AUDSP_DspLoadCode(AUDIO_DSP_CODE_TYPE dspCodeType)
401 {
402 #ifndef MSOS_TYPE_NUTTX
403    REE_TO_TEE_MBX_MSG_TYPE msg_type = REE_TO_TEE_MBX_MSG_NULL;
404 #endif
405     //MS_U8   DSP_select= DSP_SE;
406     //MS_U32 DEC_R2_ADDR, SND_R2_ADDR;
407     AUDIO_ALG_INFO *pau_info=NULL;
408     MS_U8   DSP_select=0;
409     MS_U32  MIU_addr;
410     int ii;
411 
412     HALAUDSP_CHECK_SHM_INIT;
413 
414     DBG_AUDSP_LOAD("@@@@@@@@@@    ====== HAL_AUDSP_DspLoadCode: 0x%x ======\r\n",u8Type);
415 
416     if (IS_AUDIO_DSP_CODE_TYPE_DEC1_SYSTEM(dspCodeType))
417     {
418         return  TRUE;    // Decoder in DEC-R2
419     }
420     else if (IS_AUDIO_DSP_CODE_TYPE_DEC_SYSTEM(dspCodeType)) //AU_DEC_SYSTEM
421     {
422         return  TRUE;    // Decoder in DEC-R2
423     }
424     else if ((!IS_AUDIO_DSP_CODE_TYPE_DEC2_SYSTEM(dspCodeType)) ||
425              (!IS_AUDIO_DSP_CODE_TYPE_SE_SYSTEM(dspCodeType)) ||
426              (!IS_AUDIO_DSP_CODE_TYPE_SIF(dspCodeType)) ||
427              (!IS_AUDIO_DSP_CODE_TYPE_ADVSND(dspCodeType)) ||
428              (!IS_AUDIO_DSP_CODE_TYPE_SE_ENC(dspCodeType)))
429     {
430         for (ii=0; ii< sizeof(dsp_info)/sizeof(AUDIO_ALG_INFO);ii++)
431         {
432             if (dspCodeType == dsp_info[ii].dspCodeType)
433                 break;
434         }
435         if (ii < (sizeof(dsp_info)/sizeof(AUDIO_ALG_INFO)))
436         {
437             pau_info = &dsp_info[ii];
438             DSP_select = DSP_SE;
439         }
440         else
441         {
442             DBG_AUDSP_ERROR(AUDIO_DEBUG_LEVEL_ERROR, "    [HAL_AUDSP_DspLoadCode]:======  Loading the wrong DSP code type!======\r\n");
443             return FALSE;
444         }
445     }
446 
447 #ifndef MSOS_TYPE_NUTTX
448     AUDIO_TEE_INFO_SHM_CHECK_NULL;
449     REE_TO_TEE_MBX_MSG_SET_PARAM(0, (dspCodeType&0xFF));
450     REE_TO_TEE_MBX_MSG_SET_PARAM_COUNT(1);
451     msg_type = REE_TO_TEE_MBX_MSG_HAL_AUDSP_DspLoadCode;
452 
453     msg_type = msg_type | REE_TO_TEE_MBX_MSG_TYPE_SE;
454 
455     if ( TEE_TO_REE_MBX_ACK_MSG_NO_TEE != (mbx_msg_ack_status = HAL_AUDIO_SendMBXMsg(msg_type)))
456     {
457         if (mbx_msg_ack_status != TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS)
458             return FALSE;
459         HAL_AUDSP_SetDspCodeTypeLoaded(dspCodeType);
460         return TRUE;
461     }
462 #endif
463 
464 
465   // Load  SE-DSP  system code
466     if (IS_AUDIO_DSP_CODE_TYPE_SE_SYSTEM(dspCodeType))
467     {
468         HAL_AUR2_WriteByte(REG_DECR2_RESET_CTRL, 0x00);               // STOP DEC-R2
469         HAL_AUR2_WriteMaskReg(REG_DECR2_SYSTEM_START, 0xFFFF, 0x0000); // clear DEC-R2 start cmd register
470 #if ASND_R2_SUPPORT
471         HAL_AUR2_WriteByte(REG_SNDR2_RESET_CTRL, 0x00);               // STOP SND-R2
472         HAL_AUR2_WriteMaskReg(REG_SNDR2_SYSTEM_START, 0xFFFF, 0x0000); //clear SND-R2 start cmd register
473 #endif
474         HAL_AUDIO_WriteMaskReg(0x2DDC, 0xFFFF, 0x0000); //clear  SE-DSP start cmd register
475         AUDIO_DELAY1MS(1);
476 
477         HAL_AUR2_WriteMaskReg(REG_SDR_SWITCH_CTRL, 0xFFFF, 0x3213);    //use SDR2 address
478 
479       #ifdef  CONFIG_MBOOT
480       //============ Load R2 code ===============
481       // Memory sequence : Mboot-R2(DEC-R2)
482       //====================================
483         MIU_addr = HAL_AUDIO_GetDspMadBaseAddr(DSP_ADV);
484 
485         memcpy((void*)(MS_PA2KSEG1(MIU_addr)), (void*)((mst_codec_r2)), DEC_R2_SHM_DDR_OFFSET);
486         memset((void*)(MS_PA2KSEG1(MIU_addr)+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE), 0, ADEC__R2_DDR_SIZE - DEC_R2_SHM_DDR_OFFSET - DEC_R2_SHM_DDR_SIZE);
487         memcpy((void*)(MS_PA2KSEG1(MIU_addr)+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE),
488                       (void*)(mst_codec_r2+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE),
489                       MST_CODEC_R2_AUDIO_SIZE - DEC_R2_SHM_DDR_OFFSET - DEC_R2_SHM_DDR_SIZE);
490         HAL_DEC_R2_init_SHM_param();
491         AUDIO_DELAY1MS(1);
492         MsOS_FlushMemory();
493 
494         HAL_DEC_R2_EnableR2(TRUE);               // Enable DEC-R2 after load R2 code
495       #else
496       //============ Load R2 code ===============
497       // Memory sequence : DEC-R2 ==> SND-R2 ==> SE-DSP
498       //====================================
499 
500       // Load DEC-R2
501         MIU_addr = HAL_AUDIO_GetDspMadBaseAddr(DSP_ADV);
502         memcpy((void*)(MS_PA2KSEG1(MIU_addr)), (void*)((mst_codec_r2)), DEC_R2_SHM_DDR_OFFSET);
503         memset((void*)(MS_PA2KSEG1(MIU_addr)+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE), 0, ADEC__R2_DDR_SIZE - DEC_R2_SHM_DDR_OFFSET - DEC_R2_SHM_DDR_SIZE);
504         memcpy((void*)(MS_PA2KSEG1(MIU_addr)+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE),
505                       (void*)(mst_codec_r2+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE),
506                       MST_CODEC_R2_AUDIO_SIZE - DEC_R2_SHM_DDR_OFFSET - DEC_R2_SHM_DDR_SIZE);
507         HAL_DEC_R2_init_SHM_param();
508         AUDIO_DELAY1MS(1);
509         MsOS_FlushMemory();
510 
511 #if ASND_R2_SUPPORT
512      // Load SND-R2
513         MIU_addr = HAL_AUDIO_GetDspMadBaseAddr(DSP_ADV)+ADEC__R2_DDR_SIZE;
514         memcpy((void*)(MS_PA2KSEG1(MIU_addr)), (void*)((mst_snd_r2)), SND_R2_SHM_DDR_OFFSET);
515         memset((void*)(MS_PA2KSEG1(MIU_addr)+SND_R2_SHM_DDR_OFFSET+SND_R2_SHM_DDR_SIZE), 0, ASND__R2_DDR_SIZE - SND_R2_SHM_DDR_OFFSET - SND_R2_SHM_DDR_SIZE);
516         memcpy((void*)(MS_PA2KSEG1(MIU_addr)+SND_R2_SHM_DDR_OFFSET+SND_R2_SHM_DDR_SIZE),
517                       (void*)(mst_snd_r2+SND_R2_SHM_DDR_OFFSET+SND_R2_SHM_DDR_SIZE),
518                       MST_SND_R2_AUDIO_SIZE - SND_R2_SHM_DDR_OFFSET - SND_R2_SHM_DDR_SIZE);
519         AUDIO_DELAY1MS(1);
520         MsOS_FlushMemory();
521 
522         HAL_SND_R2_EnableR2(TRUE);               // Enable SND-R2 after load R2 code
523 #endif
524         HAL_DEC_R2_EnableR2(TRUE);               // Enable DEC-R2 after load R2 code
525       #endif
526 
527 #if 0  //secure boot(Reserved)
528         HAL_AUR2_WriteMaskReg(REG_SDR_SWITCH_CTRL, 0xFFFF, 0x3210);    //use SDR1 address
529 
530       #ifdef  CONFIG_MBOOT
531       //============ Load R2 code ===============
532       // Memory sequence : Mboot-R2(DEC-R2)
533       //====================================
534         DEC_R2_ADDR = (MS_U32) MsOS_MPool_VA2PA((MS_VIRT) &mst_codec_r2);
535 
536         HAL_AUR2_WriteReg(REG_DECR2_ICMEM_BASE_LO, (MS_U16)(DEC_R2_ADDR & 0xFFFF));
537         HAL_AUR2_WriteReg(REG_DECR2_ICMEM_BASE_HI, (MS_U16)((DEC_R2_ADDR >> 16) & 0xFFFF));
538         HAL_AUR2_WriteReg(REG_DECR2_DCMEM_BASE_LO, (MS_U16)(DEC_R2_ADDR & 0xFFFF));
539         HAL_AUR2_WriteReg(REG_DECR2_DCMEM_BASE_HI, (MS_U16)((DEC_R2_ADDR >> 16) & 0xFFFF));
540         AUDIO_DELAY1MS(1);
541         HAL_DEC_R2_EnableR2(TRUE);               // Enable DEC-R2 after load R2 code
542       #else
543       //============ Load R2 code ===============
544       // Memory sequence : DEC-R2 ==> SND-R2 ==> SE-DSP
545       //====================================
546         DEC_R2_ADDR =  (MS_U32) MsOS_MPool_VA2PA((MS_VIRT) &mst_codec_r2);
547         SND_R2_ADDR =  (MS_U32) MsOS_MPool_VA2PA((MS_VIRT) &mst_snd_r2);
548 
549         HAL_AUR2_WriteReg(REG_DECR2_ICMEM_BASE_LO, (MS_U16)(DEC_R2_ADDR & 0xFFFF));
550         HAL_AUR2_WriteReg(REG_DECR2_ICMEM_BASE_HI, (MS_U16)((DEC_R2_ADDR >> 16) & 0xFFFF));
551         HAL_AUR2_WriteReg(REG_DECR2_DCMEM_BASE_LO, (MS_U16)(DEC_R2_ADDR & 0xFFFF));
552         HAL_AUR2_WriteReg(REG_DECR2_DCMEM_BASE_HI, (MS_U16)((DEC_R2_ADDR >> 16) & 0xFFFF));
553         HAL_AUR2_WriteReg(REG_SNDR2_ICMEM_BASE_LO, (MS_U16)(SND_R2_ADDR & 0xFFFF));
554         HAL_AUR2_WriteReg(REG_SNDR2_ICMEM_BASE_HI, (MS_U16)((SND_R2_ADDR >> 16) & 0xFFFF));
555         HAL_AUR2_WriteReg(REG_SNDR2_DCMEM_BASE_LO, (MS_U16)(SND_R2_ADDR & 0xFFFF));
556         HAL_AUR2_WriteReg(REG_SNDR2_DCMEM_BASE_HI, (MS_U16)((SND_R2_ADDR >> 16) & 0xFFFF));
557         AUDIO_DELAY1MS(1);
558         HAL_SND_R2_EnableR2(TRUE);               // Enable SND-R2 after load R2 code
559         HAL_DEC_R2_EnableR2(TRUE);               // Enable DEC-R2 after load R2 code
560 #endif
561       #endif
562     }
563 
564     HAL_AUDSP_SetDspCodeTypeLoaded(dspCodeType);
565     DBG_AUDSP_LOAD("HAL_AUDSP_DspLoadCode finished(type=(0x%x))\r\n" , u8dspCodeType);
566 
567     return TRUE;
568 }
569 
HAL_AUDSP_DspLoadCode2(MS_U8 DSP_select,void * info)570 MS_BOOL HAL_AUDSP_DspLoadCode2(MS_U8 DSP_select, void * info)
571 {
572     return TRUE;
573 }
574 
HAL_AUDSP_DspLoadSystemCode2(MS_U8 DSP_select,AUDIO_ALG_INFO * pau_info)575 MS_BOOL HAL_AUDSP_DspLoadSystemCode2(MS_U8 DSP_select, AUDIO_ALG_INFO * pau_info)
576 {
577     return TRUE;
578 }
579 
580 ////////////////////////////////////////////////////////////////////////////////
581 /// @brief \b Function \b Name: HAL_AUDIO_DspLoadCodeSegment()
582 /// @brief \b Function \b Description: This routine is used to load DSP code
583 /// @param <IN>        \b dsp_addr    :
584 /// @param <IN>        \b dspCode_buf    :
585 /// @param <IN>        \b dspCode_buflen    :
586 /// @param <OUT>       \b NONE    :
587 /// @param <RET>       \b  BOOL    :    TRUE --DSP Load code okay
588 ///                                    FALSE--DSP Load code fail
589 /// @param <GLOBAL>    \b NONE    :
590 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_DspLoadCodeSegment(MS_U32 dsp_addr,MS_U8 * dspCode_buf,MS_U32 dspCode_buflen,MS_U8 DSP_select)591 MS_BOOL HAL_AUDSP_DspLoadCodeSegment(MS_U32 dsp_addr, MS_U8  *dspCode_buf, MS_U32 dspCode_buflen, MS_U8 DSP_select)
592 {
593     g_bDSPLoadCode = TRUE;
594     return TRUE;     // secure boot: MCU can't use IDMA
595 }
596 
HAL_AUDSP_DspVerifySegmentCode(MS_U32 dsp_addr,MS_U8 * dspCode_buf,MS_U32 dspCode_buflen,MS_U8 DSP_select)597 MS_BOOL HAL_AUDSP_DspVerifySegmentCode(MS_U32 dsp_addr, MS_U8 *dspCode_buf, MS_U32 dspCode_buflen, MS_U8 DSP_select)
598 {
599     g_bDSPLoadCode = TRUE;
600     return TRUE;     // secure boot: MCU can't use IDMA
601 }
602 
603 ////////////////////////////////////////////////////////////////////////////////
604 /// @brief \b Function \b Name: HAL_AUDSP_CheckDecIdmaReady()
605 /// @brief \b Function \b Description:  This routine is used to check if the Dec-DSP IDMA is ready or not.
606 /// @param <IN>        \b IdmaChk_type    :
607 ///                                    0x10 : check write ready
608 ///                                    0x80 : check read  ready
609 /// @param <OUT>       \b NONE    :
610 /// @param <RET>       \b MS_BOOL    : TRUE--IDMA is ready
611 ///                                      FALSE--IDMA not ready
612 /// @param <GLOBAL>    \b NONE    :
613 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_CheckDecIdmaReady(MS_U8 u8IdmaChk_type)614 MS_BOOL HAL_AUDSP_CheckDecIdmaReady(MS_U8 u8IdmaChk_type )
615 {
616     return FALSE;  // secure boot: MCU can't use IDMA
617 }
618 
619 ////////////////////////////////////////////////////////////////////////////////
620 /// @brief \b Function \b Name: HAL_AUDIO_CheckSeIdmaReady()
621 /// @brief \b Function \b Description:  This routine is used to check if the Se-DSP IDMA is ready or not.
622 /// @param <IN>        \b IdmaChk_type    :
623 ///                                    0x10 : check write ready
624 ///                                    0x80 : check read  ready
625 /// @param <OUT>       \b NONE    :
626 /// @param <RET>       \b MS_BOOL    : TRUE--IDMA is ready
627 ///                                      FALSE--IDMA not ready
628 /// @param <GLOBAL>    \b NONE    :
629 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_CheckSeIdmaReady(MS_U8 u8IdmaChk_type)630 MS_BOOL HAL_AUDSP_CheckSeIdmaReady(MS_U8 u8IdmaChk_type)
631 {
632     MS_U8  j = 0;
633 
634     while(j<200)
635     {
636         j++;
637         if( (HAL_AUDIO_ReadByte(REG_SE_IDMA_CTRL0)& u8IdmaChk_type) == 0 )
638             return TRUE;
639     }
640 
641     DBG_AUDSP_ERROR("XBox check data trans fail!(%d)\r\n",j);
642     return FALSE;
643 }
644 
645 ////////////////////////////////////////////////////////////////////////////////
646 /// @brief \b Function \b Name: HAL_AUDSP_SetDspCodeTypeLoaded()
647 /// @brief \b Function \b Description:  This function is used to set the DSP code type.
648 /// @param <IN>        \b NONE    :
649 /// @param <OUT>       \b NONE    :
650 /// @param <RET>       \b MS_U8: DSP code type.
651 /// @param <GLOBAL>    \b NONE    :
652 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_SetDspCodeTypeLoaded(AUDIO_DSP_CODE_TYPE dspCodeType)653 void HAL_AUDSP_SetDspCodeTypeLoaded(AUDIO_DSP_CODE_TYPE dspCodeType)
654 {
655     g_DspCodeTypeLoaded = dspCodeType;
656 }
657 
658 ////////////////////////////////////////////////////////////////////////////////
659 /// @brief \b Function \b Name: HAL_AUDSP_GetDspCodeTypeLoaded()
660 /// @brief \b Function \b Description:  This function is used to get the MAD base address.
661 /// @param <IN>        \b NONE    :
662 /// @param <OUT>       \b NONE    :
663 /// @param <RET>       \b MS_U8: DSP code type.
664 /// @param <GLOBAL>    \b NONE    :
665 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_GetDspCodeTypeLoaded(void)666 AUDIO_DSP_CODE_TYPE HAL_AUDSP_GetDspCodeTypeLoaded(void)
667 {
668     return g_DspCodeTypeLoaded;
669 }
670 
HAL_AUDSP_SetDspLoadCodeInfo(AUDIO_ALG_INFO * pau_info,MS_U8 DSP_select)671 void HAL_AUDSP_SetDspLoadCodeInfo(AUDIO_ALG_INFO *pau_info, MS_U8 DSP_select)
672 {
673     g_loadcodeinfo.pau_info = pau_info;
674     g_loadcodeinfo.DSP_select= DSP_select;
675 }
676 
677 ////////////////////////////////////////////////////////////////////////////////
678 /// @brief \b Function \b Name: HAL_AUDSP_GetDspCodeTypeLoaded()
679 /// @brief \b Function \b Description:  This function is used to get the MAD base address.
680 /// @param <IN>        \b NONE    :
681 /// @param <OUT>       \b NONE    :
682 /// @param <RET>       \b MS_U8: DSP code type.
683 /// @param <GLOBAL>    \b NONE    :
684 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_GetDspLoadCodeInfo(void)685 LOAD_CODE_INFO* HAL_AUDSP_GetDspLoadCodeInfo(void)
686 {
687     return &g_loadcodeinfo;
688 }
689 ////////////////////////////////////////////////////////////////////////////////
690 /// @brief \b Function \b Name: HAL_AUDSP_DECR2LoadCode()
691 /// @brief \b Function \b Description:  Load DEC-R2 code to DDR
692 /// @param <IN>        \b NONE    :
693 /// @param <OUT>       \b NONE    :
694 /// @param <RET>       \b NONE    :
695 /// @param <GLOBAL>    \b NONE    :
696 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_DECR2LoadCode(void)697 void HAL_AUDSP_DECR2LoadCode(void)
698 {
699     MS_U32 MIU_addr;
700 
701     printf("======audio: start DEC-R2 load code======\n");
702 
703     HAL_AUR2_WriteByte(REG_DECR2_RESET_CTRL, 0x00);               // STOP DEC-R2
704     HAL_AUR2_WriteMaskReg(REG_DECR2_SYSTEM_START, 0xFFFF, 0x0000); //clear DEC-R2 start cmd register
705     AUDIO_DELAY1MS(1);
706 
707   //============ Load R2 code ===============
708   // Memory sequence : DEC-R2 ==> SND-R2 ==> SE-DSP
709   //====================================
710 
711   // Load DEC-R2
712     MIU_addr = HAL_AUDIO_GetDspMadBaseAddr(DSP_ADV);
713 
714     memcpy((void*)(MS_PA2KSEG1(MIU_addr)), (void*)((mst_codec_r2)), DEC_R2_SHM_DDR_OFFSET);
715     memset((void*)(MS_PA2KSEG1(MIU_addr)+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE), 0, ADEC__R2_DDR_SIZE - DEC_R2_SHM_DDR_OFFSET - DEC_R2_SHM_DDR_SIZE);
716     memcpy((void*)(MS_PA2KSEG1(MIU_addr)+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE),
717                   (void*)(mst_codec_r2+DEC_R2_SHM_DDR_OFFSET+DEC_R2_SHM_DDR_SIZE),
718                   MST_CODEC_R2_AUDIO_SIZE - DEC_R2_SHM_DDR_OFFSET - DEC_R2_SHM_DDR_SIZE);
719     HAL_DEC_R2_init_SHM_param();
720     AUDIO_DELAY1MS(1);
721     MsOS_FlushMemory();
722 
723     HAL_DEC_R2_EnableR2(TRUE);               // Enable DEC-R2 after load R2 code
724     HAL_MAD2_SetMcuCmd(0xF3);
725 
726     printf("======audio: end DEC-R2 load code======\n");
727 }
728 
729 ////////////////////////////////////////////////////////////////////////////////
730 /// @brief \b Function \b Name: HAL_AUDSP_CheckDecIdmaReady()
731 /// @brief \b Function \b Description:  This routine is used to check if the XBox data transport is ready or not.
732 /// @param <OUT>       \b NONE    :
733 /// @param <RET>       \b MS_BOOL    : TRUE--IDMA is ready
734 ///                                      FALSE--IDMA not ready
735 /// @param <GLOBAL>    \b NONE    :
736 ////////////////////////////////////////////////////////////////////////////////
HAL_AUDSP_Check_XBox_TransData_Ready(void)737 MS_BOOL HAL_AUDSP_Check_XBox_TransData_Ready(void)
738 {
739     MS_U8  j = 0;
740     while(j<200)
741     {
742         j++;
743         if( (HAL_AUDIO_ReadByte(REG_DSP_XBOX_MISC)& 0x08) == 0x00 )  //bit[3] = 1 ,busy
744         return TRUE;
745     }
746     DBG_AUDSP_ERROR("XBox check data trans fail!(%d)\r\n",j);
747     return FALSE;
748 }
749 
750