xref: /utopia/UTPA2-700.0.x/modules/dscmb/hal/k6lite/tcf/halTCF.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) 2006-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 // file   halTCF.c
97 // @brief  TCF HAL
98 // @author MStar Semiconductor,Inc.
99 ////////////////////////////////////////////////////////////////////////////////////////////////////
100 
101 #include "MsCommon.h"
102 #include "halTCF.h"
103 #include "regTCF.h"
104 
105 //--------------------------------------------------------------------------------------------------
106 //  Debug Function
107 //--------------------------------------------------------------------------------------------------
108 #define _TCF_DBG(fmt, args...)  printf("\033[32m""[DBG]"fmt"\033[m", ## args)
109 #define _TCF_ERR(fmt, args...)  printf("\033[31m""[ERR]"fmt"\033[m", ## args)
110 
111 
112 //--------------------------------------------------------------------------------------------------
113 //  Internal Variable
114 //--------------------------------------------------------------------------------------------------
115 static MS_VIRT              _u32NonPmBankAddr = NULL;
116 static REG_CFCtrl           *_CFCtrl  = (REG_CFCtrl*)REG_CF_BASE;
117 static REG_CFBCtrl          *_CFBCtrl = (REG_CFBCtrl*)REG_CFB_BASE;
118 
119 
120 //--------------------------------------------------------------------------------------------------
121 //  Macro of bit operations
122 //--------------------------------------------------------------------------------------------------
_REG32_W(REG32 * reg,MS_U32 value)123 void _REG32_W(REG32 *reg, MS_U32 value)
124 {
125     (*((volatile MS_U32*)(reg))) = value;
126 }
127 
_REG32_R(REG32 * reg)128 MS_U32 _REG32_R(REG32 *reg)
129 {
130     MS_U32 value = (*(volatile MS_U32*)(reg));
131     return value;
132 }
133 
134 
135 //--------------------------------------------------------------------------------------------------
136 //  Inline Function
137 //--------------------------------------------------------------------------------------------------
HAL_CF_SetBank(MS_VIRT u32NonPmBankAddr)138 void HAL_CF_SetBank(MS_VIRT u32NonPmBankAddr)
139 {
140     _u32NonPmBankAddr = u32NonPmBankAddr;
141     /// CF
142     _CFCtrl  = (REG_CFCtrl*)  (_u32NonPmBankAddr + REG_CF_BASE);
143     /// CFB
144     _CFBCtrl = (REG_CFBCtrl*) (_u32NonPmBankAddr + REG_CFB_BASE);
145 }
146 
HAL_CF_Clk(MS_BOOL bEnable)147 void HAL_CF_Clk(MS_BOOL bEnable)
148 {
149     REG32 *Reg32_Clk_CF = (REG32*)(_u32NonPmBankAddr + REG_CLK_CF_BASE + REG_CLK_CF);
150     REG32 *Reg32_Clk_cftop_p_sel = (REG32*)(_u32NonPmBankAddr + REG_CLK_GEN0_BASE + REG_CLK_CFTOP_P_SEL);
151 
152     if(bEnable)
153     {
154         _TCF_DBG("[%s] Enable CF Clock!!\n", __FUNCTION__);
155         _REG32_W( Reg32_Clk_CF, (_REG32_R( Reg32_Clk_CF)&~ENABLE_48_MHZ_CF_CLK_MASK) | ENABLE_48_MHZ_CF_CLK);
156     }
157     else
158     {
159         _TCF_DBG("[%s] Disable CF Clock!!\n", __FUNCTION__);
160         _REG32_W( Reg32_Clk_CF, (_REG32_R( Reg32_Clk_CF)|ENABLE_48_MHZ_CF_CLK_MASK));
161     }
162 
163 #if 1//CLK_54M
164     //Set CF top clock to 54MHz, for functional mode
165     _REG32_W( Reg32_Clk_cftop_p_sel, (_REG32_R( Reg32_Clk_cftop_p_sel)&~ENABLE_CFTOP_P_SEL_MASK) | ENABLE_CFTOP_P_54M_CLK);
166     _REG32_W( &_CFBCtrl[0].Cfb_TcfProductionMode, 0);
167 #else
168     //Set CF top clock to 12MHz, for production mode
169     _REG32_W( Reg32_Clk_cftop_p_sel, (_REG32_R( Reg32_Clk_cftop_p_sel)&~ENABLE_CFTOP_P_SEL_MASK) | ENABLE_CFTOP_P_12M_CLK);
170     _REG32_W( &_CFBCtrl[0].Cfb_TcfProductionMode, 1);
171 #endif
172     // TODO: TSP Clock checking
173     //REG32 *Reg32_Clk_TSP= (REG32*)(_u32NonPmBankAddr + REG_CLK_TSP_BASE + REG_CLK_TSP);
174     //_REG32_W( Reg32_Clk_TSP, (_REG32_R( Reg32_Clk_TSP)&~SET_192_MHZ_TSP_CLK_MASK) | SET_192_MHZ_TSP_CLK );  // change to 172
175 }
176 
HAL_CF_Version_Info(HAL_CF_VERSION_INFO * _pstTransStatus)177 void HAL_CF_Version_Info(HAL_CF_VERSION_INFO *_pstTransStatus)
178 {
179     MS_U32 u32Value= _REG32_R( &_CFCtrl[0].Cf_Version);
180 
181     _pstTransStatus->_u8ManufacturerId = ((u32Value & CF_VERSION_MANUFACTURER_ID_MASK) >> CF_VERSION_MANUFACTURER_ID_POS);
182     _pstTransStatus->_u8NetlistVersion = ((u32Value & CF_VERSION_NETLIST_VERSION_MASK) >> CF_VERSION_NETLIST_VERSION_POS);
183     _pstTransStatus->_u8VersionEpoch   = ((u32Value & CF_VERSION_VERSION_EPOCH_MASK) >> CF_VERSION_VERSION_EPOCH_POS);
184     _pstTransStatus->_u8VersionBuildId = ((u32Value & CF_VERSION_BUILD_ID_MASK) >> CF_VERSION_BUILD_ID_POS);
185 }
186 
HAL_CF_Trans_Status(HAL_CF_TRANS_STATUS * _pstStatus)187 void HAL_CF_Trans_Status(HAL_CF_TRANS_STATUS *_pstStatus)
188 {
189     MS_U32 u32Value= _REG32_R( &_CFCtrl[0].Cf_Status);
190     _pstStatus->_eTransStatus    = ((u32Value & CF_TRANS_STATUS_MASK) >> CF_TRANS_STATUS_POS);
191     _pstStatus->_u8UseNvmKey      = ((u32Value & CF_USE_NVMKEY_MASK) >> CF_USE_NVMKEY_POS);
192     _pstStatus->_eOperationType  = ((u32Value & CF_OPERATION_TYPE_MASK) >> CF_OPERATION_TYPE_POS);
193     _pstStatus->_eDecmSrc        = ((u32Value & CF_DECM_SOURCE_MASK) >> CF_DECM_SOURCE_POS);
194     _pstStatus->_eOutputUsage    = ((u32Value & CF_OUTPUT_USAGE_MASK) >> CF_OUTPUT_USAGE_POS);
195     _pstStatus->_u8ProductRange  = ((u32Value & CF_PRODUCT_RANGE_MASK) >> CF_PRODUCT_RANGE_POS);
196     _pstStatus->_u8ProductOffset = ((u32Value & CF_PRODUCT_OFFSET_MASK) >> CF_PRODUCT_OFFSET_POS);
197 }
198 
HAL_CF_Cf_Status(HAL_CF_CF_STATUS * _pstCfStatus)199 void HAL_CF_Cf_Status(HAL_CF_CF_STATUS *_pstCfStatus)
200 {
201     MS_U32 u32Value= _REG32_R( &_CFCtrl[0].Cf_Status);
202     u32Value= _REG32_R( &_CFCtrl[0].Cf_Status);
203     _pstCfStatus->_eNvmStatus        = ((u32Value & CF_NVM_STATUS_MASK) >> CF_NVM_STATUS_POS);
204     _pstCfStatus->_eDiffStatus       = ((u32Value & CF_DIFF_STATUS_MASK) >> CF_DIFF_STATUS_POS);
205 
206     u32Value= _REG32_R( &_CFCtrl[0].Cf_Platform);
207     if( ((_REG32_R( &_CFCtrl[0].Cf_Version) & CF_VERSION_NETLIST_VERSION_MASK) >> CF_VERSION_NETLIST_VERSION_POS) == 0 ) // Netlist Version 0
208     {
209         _pstCfStatus->_u8RecentReset     = ((u32Value & CF_PLATFORM_RECENT_RESET_MASK) >> CF_PLATFORM_RECENT_RESET_POS);
210         _pstCfStatus->_u8CfAlert         = ((u32Value & CF_PLATFORM_CF_ALERT_MASK) >> CF_PLATFORM_CF_ALERT_POS);
211         // Netlist Version 0 has no development mode bit.
212         _pstCfStatus->_u8DevelopmentMode = 0x0;
213         // Netlist Version 0 has a different Fuse Activate and Fuse Block positions.
214         _pstCfStatus->_u8FuseActivate    = ((u32Value & 0x00200000) >> 21);
215         _pstCfStatus->_u8FuseBlock       = ((u32Value & 0x00100000) >> 20);
216     }
217     else
218     {
219         _pstCfStatus->_u8RecentReset     = ((u32Value & CF_PLATFORM_RECENT_RESET_MASK) >> CF_PLATFORM_RECENT_RESET_POS);
220         _pstCfStatus->_u8CfAlert         = ((u32Value & CF_PLATFORM_CF_ALERT_MASK) >> CF_PLATFORM_CF_ALERT_POS);
221         _pstCfStatus->_u8DevelopmentMode = ((u32Value & CF_PLATFORM_DEVELOPMENT_MODE_MASK) >> CF_PLATFORM_DEVELOPMENT_MODE_POS);
222         _pstCfStatus->_u8FuseActivate    = ((u32Value & CF_PLATFORM_CF_ACTIVATED_MASK) >> CF_PLATFORM_CF_ACTIVATED_POS);
223         _pstCfStatus->_u8FuseBlock       = ((u32Value & 0x00000080) >> 7);
224     }
225 }
226 
HAL_CF_Cf_FeatureVector(HAL_CF_FEATURE_VECTOR * _pstCfFeature)227 void HAL_CF_Cf_FeatureVector(HAL_CF_FEATURE_VECTOR *_pstCfFeature)
228 {
229     _pstCfFeature->_u32FeatureVector= _REG32_R( &_CFCtrl[0].Cf_Feature);
230 }
231 
HAL_CF_Get_Trans_Status(void)232 MS_U8 HAL_CF_Get_Trans_Status(void)
233 {
234     MS_U32 u32Value= _REG32_R( &_CFCtrl[0].Cf_Status);
235     //_TCF_DBG("[%s] ===  Trans_Status= 0x%08lx  ===\n", __FUNCTION__, u32Value);
236     return (MS_U8)((u32Value & CF_TRANS_STATUS_MASK) >> CF_TRANS_STATUS_POS);
237 }
238 
HAL_CF_Get_Operation_Type(void)239 MS_U8 HAL_CF_Get_Operation_Type(void)
240 {
241     MS_U32 u32Value= _REG32_R( &_CFCtrl[0].Cf_Status);
242 
243     return (MS_U8)((u32Value & CF_OPERATION_TYPE_MASK) >> CF_OPERATION_TYPE_POS);
244 }
245 
HAL_CF_Get_CwcValid(void)246 MS_U8 HAL_CF_Get_CwcValid(void)
247 {
248     MS_U32 u32Value= _REG32_R( &_CFCtrl[0].Cf_Platform);
249 
250     return (MS_U8)((u32Value & CF_PLATFORM_CWC_VALID_MASK) >> CF_PLATFORM_CWC_VALID_POS);
251 }
252 
HAL_CF_Read_Input(void)253 MS_U32 HAL_CF_Read_Input(void)
254 {
255     return _REG32_R( &_CFCtrl[0].Cf_Input);
256 }
257 
HAL_CF_Read_Output(void)258 MS_U32 HAL_CF_Read_Output(void)
259 {
260     return _REG32_R( &_CFCtrl[0].Cf_Output);
261 }
262 
HAL_CF_Write_Input(MS_U32 u32Cmd)263 void HAL_CF_Write_Input(MS_U32 u32Cmd)
264 {
265     _REG32_W( &_CFCtrl[0].Cf_Input, u32Cmd);
266 }
267 
HAL_CF_Write_Output(MS_U32 u32Cmd)268 void HAL_CF_Write_Output(MS_U32 u32Cmd)
269 {
270     _REG32_W( &_CFCtrl[0].Cf_Output, u32Cmd);
271 }
272 
273 #define _CF_TRANS_STATUS_BUSY_OP    0x4
HAL_CF_Get_CF_IsFinished(void)274 MS_U8 HAL_CF_Get_CF_IsFinished(void)
275 {
276     if( HAL_CF_Get_Trans_Status()==_CF_TRANS_STATUS_BUSY_OP )
277     {
278         return FALSE;
279     }
280     else{
281         return TRUE;
282     }
283 }
284 
285 
286 
HAL_CFB_Init(void)287 MS_U8 HAL_CFB_Init(void)
288 {
289     MS_U32 u32Value= 0;
290 
291     u32Value = _REG32_R( &_CFBCtrl[0].Cfb_Use_Case);
292     u32Value = u32Value | ( REG_CFB_INT_CLR | REG_INITIAL_SM | REG_RESET_CFB );
293     _TCF_DBG("[%s][%d] Reg_0[6:4]= b'%u%u%u\n", __FUNCTION__, __LINE__, (MS_U8)(u32Value&REG_CFB_INT_CLR)>>6, (MS_U8)(u32Value&REG_INITIAL_SM)>>5, (MS_U8)(u32Value&REG_RESET_CFB)>>4);
294     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value);
295     MsOS_DelayTask(10);
296     u32Value = (u32Value | (REG_INITIAL_SM)) & (~REG_CFB_INT_CLR) & (~REG_RESET_CFB);
297     _TCF_DBG("[%s][%d] Reg_0[6:4]= b'%u%u%u\n", __FUNCTION__, __LINE__, (MS_U8)(u32Value&REG_CFB_INT_CLR)>>6, (MS_U8)(u32Value&REG_INITIAL_SM)>>5, (MS_U8)(u32Value&REG_RESET_CFB)>>4);
298     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value);
299 
300     return TRUE;
301 }
HAL_CFB_Enable(void)302 MS_U8 HAL_CFB_Enable(void)
303 {
304     MS_U32 u32Value= 0;
305 
306     // REG_RESET_CFB= 0, REG_INITIAL_SM= 0
307     u32Value = _REG32_R( &_CFBCtrl[0].Cfb_Use_Case);
308     _TCF_DBG("[%s][%d] Reg_0[5:4]= b'%u%u\n", __FUNCTION__, __LINE__, (MS_U8)(u32Value&REG_INITIAL_SM)>>5, (MS_U8)(u32Value&REG_RESET_CFB)>>4);
309     u32Value = u32Value & (~REG_RESET_CFB);
310     u32Value = u32Value & (~REG_INITIAL_SM);
311     _TCF_DBG("[%s][%d] Reg_0[5:4]= b'%u%u\n", __FUNCTION__, __LINE__, (MS_U8)(u32Value&REG_INITIAL_SM)>>5, (MS_U8)(u32Value&REG_RESET_CFB)>>4);
312     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value);
313 
314     return TRUE;
315 }
HAL_CFB_Reset(void)316 MS_U8 HAL_CFB_Reset(void)
317 {
318     MS_U32 u32Value= 0;
319 
320     u32Value = _REG32_R( &_CFBCtrl[0].Cfb_Use_Case);
321     _TCF_DBG("[%s][%d] Reg_0[5:4]= b'%u%u\n", __FUNCTION__, __LINE__, (MS_U8)(u32Value&REG_INITIAL_SM)>>5, (MS_U8)(u32Value&REG_RESET_CFB)>>4);
322 
323     // Set REG_RESET_CFB= 1, REG_INITIAL_SM= 1
324     u32Value = u32Value | (REG_RESET_CFB);
325     u32Value = u32Value | (REG_INITIAL_SM);
326     _TCF_DBG("[%s][%d] Reg_0[5:4]= b'%u%u\n", __FUNCTION__, __LINE__, (MS_U8)(u32Value&REG_INITIAL_SM)>>5, (MS_U8)(u32Value&REG_RESET_CFB)>>4);
327     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value);
328 
329     // Set REG_RESET_CFB= 0, REG_INITIAL_SM= 1
330     u32Value = u32Value & (~REG_RESET_CFB);
331     u32Value = u32Value | (REG_INITIAL_SM);
332     _TCF_DBG("[%s][%d] Reg_0[5:4]= b'%u%u\n", __FUNCTION__, __LINE__, (MS_U8)(u32Value&REG_INITIAL_SM)>>5, (MS_U8)(u32Value&REG_RESET_CFB)>>4);
333     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value);
334 
335     return TRUE;
336 }
337 
HAL_CFB_Set_Use_Case(MS_U8 u8UseCase)338 void HAL_CFB_Set_Use_Case(MS_U8 u8UseCase)
339 {
340     MS_U32 u32Value= 0;
341 
342     u32Value = _REG32_R( &_CFBCtrl[0].Cfb_Use_Case);
343     u32Value = (u32Value&(~REG_USE_CASE_MASK)) | (u8UseCase&REG_USE_CASE_MASK);
344     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value );
345 }
346 
HAL_CFB_Set_Otp_Key(MS_U8 u8OtpKeyIdx)347 void HAL_CFB_Set_Otp_Key(MS_U8 u8OtpKeyIdx)
348 {
349     MS_U32 u32Value= 0;
350 
351     u32Value = _REG32_R( &_CFBCtrl[0].Cfb_Use_Case);
352     u32Value = (u32Value&(~REG_TCF_KEY_SEL_MASK)) | ((u8OtpKeyIdx<<8)&REG_TCF_KEY_SEL_MASK);
353     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value);
354 }
355 
HAL_CFB_Set_CACWC_Src(MS_U8 u8CAcwcSrc)356 void HAL_CFB_Set_CACWC_Src(MS_U8 u8CAcwcSrc)
357 {
358     MS_U32 u32Value= 0;
359 
360     u32Value = _REG32_R( &_CFBCtrl[0].Cfb_Use_Case);
361     u32Value = (u32Value&(~REG_CFB_CACWC_SEL)) | ((u8CAcwcSrc<<16)&REG_CFB_CACWC_SEL);
362     _REG32_W( &_CFBCtrl[0].Cfb_Use_Case, u32Value);
363 }
364 
HAL_CFB_SET_CACWC(MS_U32 * pCACWC)365 void HAL_CFB_SET_CACWC(MS_U32 *pCACWC)
366 {
367     _REG32_W( &_CFBCtrl[0].Cfb_Cacwc0, pCACWC[3]);
368     _REG32_W( &_CFBCtrl[0].Cfb_Cacwc1, pCACWC[2]);
369     _REG32_W( &_CFBCtrl[0].Cfb_Cacwc2, pCACWC[1]);
370     _REG32_W( &_CFBCtrl[0].Cfb_Cacwc3, pCACWC[0]);
371 }
372 
HAL_CFB_SET_EPK(MS_U32 * pEPK)373 void HAL_CFB_SET_EPK(MS_U32 *pEPK)
374 {
375     _REG32_W( &_CFBCtrl[0].Cfb_Epk0, pEPK[3]);
376     _REG32_W( &_CFBCtrl[0].Cfb_Epk1, pEPK[2]);
377     _REG32_W( &_CFBCtrl[0].Cfb_Epk2, pEPK[1]);
378     _REG32_W( &_CFBCtrl[0].Cfb_Epk3, pEPK[0]);
379 }
380 
HAL_CFB_SET_EFUV(MS_U32 * pEFUV)381 void HAL_CFB_SET_EFUV(MS_U32 *pEFUV)
382 {
383     _REG32_W( &_CFBCtrl[0].Cfb_Efuv0, pEFUV[3]);
384     _REG32_W( &_CFBCtrl[0].Cfb_Efuv1, pEFUV[2]);
385     _REG32_W( &_CFBCtrl[0].Cfb_Efuv2, pEFUV[1]);
386     _REG32_W( &_CFBCtrl[0].Cfb_Efuv3, pEFUV[0]);
387 }
388 
HAL_CFB_GetCwc_Dst(MS_U8 eCwcDst)389 MS_U16 HAL_CFB_GetCwc_Dst(MS_U8 eCwcDst)
390 {
391     switch (eCwcDst)
392     {
393         case E_CFB_DST_KT:
394             return E_HAL_CFB_DST_KT;
395         case E_CFB_DST_DMA_SK0:
396             return E_HAL_CFB_DST_DMA_SK0;
397         case E_CFB_DST_DMA_SK1:
398             return E_HAL_CFB_DST_DMA_SK1;
399         case E_CFB_DST_DMA_SK2:
400             return E_HAL_CFB_DST_DMA_SK2;
401         case E_CFB_DST_DMA_SK3:
402             return E_HAL_CFB_DST_DMA_SK3;
403         case E_CFB_DST_TSIO:
404             return E_HAL_CFB_DST_TSIO;
405         default:
406             return E_HAL_CFB_DST_KT;
407     }
408 }
409 
HAL_CFB_Set_Cwc_Cfg(HAL_CFB_CWCCFG * pstCwcCfg)410 void HAL_CFB_Set_Cwc_Cfg(HAL_CFB_CWCCFG *pstCwcCfg)
411 {
412     MS_U32 u32Value= 0;
413     MS_U16 u16CwcDst = HAL_CFB_GetCwc_Dst(pstCwcCfg->_u8CwcDst);
414 
415     u32Value = _REG32_R( &_CFBCtrl[0].Cfb_Tcf_Key_Otp);
416 
417     // [31:24]: reg_tcf_cwc_pid (8)
418     u32Value = (u32Value&(~REG_TCF_CWC_PID))  | ( ((pstCwcCfg->_u8CwcPid)<<24) & REG_TCF_CWC_PID );
419     // [21:20]: reg_tcf_cwc_fld (2)
420     u32Value = (u32Value&(~REG_TCF_CWC_FLD))  | ( ((pstCwcCfg->_u8CwcFld)<<20) & REG_TCF_CWC_FLD );
421     // [19:18]: reg_tcf_cwc_fscb (2)
422     u32Value = (u32Value&(~REG_TCF_CWC_FSCB)) | ( ((pstCwcCfg->_u8CwcFscb)<<18) & REG_TCF_CWC_FSCB );
423     // [17:16]: reg_tcf_cwc_scb (2)
424     u32Value = (u32Value&(~REG_TCF_CWC_SCB))  | ( ((pstCwcCfg->_u8CwcScb)<<16) & REG_TCF_CWC_SCB );
425     // [11:8]: reg_tcf_cwc_tsid (4)
426     u32Value = (u32Value&(~REG_TCF_CWC_TSID)) | ( ((pstCwcCfg->_u8CwcTsid)<<8) & REG_TCF_CWC_TSID );
427 
428     _REG32_W( &_CFBCtrl[0].Cfb_Tcf_Key_Otp, u32Value);
429     _REG32_W( &_CFBCtrl[0].Cfb_Key_Dst, u16CwcDst);
430 }
431 
HAL_CFB_Is_Ready(void)432 MS_BOOL HAL_CFB_Is_Ready(void)
433 {
434     if( (_REG32_R( &_CFBCtrl[0].Cfb_Status)&REG_CFB_READY) == REG_CFB_READY ) // ready
435     {
436         return TRUE;
437     }
438     else
439     {
440         printf(">>>\tNOT Ready!!\n");
441         return FALSE;
442     }
443 }
444 
HAL_CFB_Is_Done(void)445 MS_BOOL HAL_CFB_Is_Done(void)
446 {
447     if( (_REG32_R( &_CFBCtrl[0].Cfb_Status)&REG_CFB_DONE) == REG_CFB_DONE ) // done
448     {
449         return TRUE;
450     }
451     else
452     {
453         //printf(">>>\tNOT Done!!\n");
454         return FALSE;
455     }
456 }
457 
458 //////
459 
HAL_CFB_DBG_KT_Response(void)460 MS_BOOL HAL_CFB_DBG_KT_Response(void)
461 {
462     MS_U8 u8Val = _REG32_R( &_CFBCtrl[0].Cfb_Status);
463 
464     printf("\033[32m""\n[%s][DBG] control word has been transtered to key table:\n""\033[m", __FUNCTION__);
465     if( (u8Val&REG_TCF_CWC_WR_DONE_LTH)>>6 )
466         printf("\tTRUE\n");
467     else
468         printf("\tFalse!\n");
469 
470     printf("\033[32m""[%s][DBG] key table response:\n""\033[m", __FUNCTION__);
471     switch( (u8Val&REG_TCF_CWC_WR_RESP_LTH)>>8 )
472     {
473         case 0x1F:
474             printf("\tno_slot (5'b11111)\n");
475             break;
476         case 0x19:
477             printf("\tno_allow_wkey (5'b11001)\n");
478             break;
479         case 0x02:
480             printf("\tis_bad_key (2'b10)\n");
481             break;
482         case 0x00:
483             printf("\tok (5'b0000)\n");
484             break;
485         default:
486             break;
487     }
488 
489     return TRUE;
490 }
491 
HAL_CFB_DBG_CFB_FSM(void)492 MS_U8 HAL_CFB_DBG_CFB_FSM(void)
493 {
494     MS_U8 u8Val = (_REG32_R( &_CFBCtrl[0].Cfb_State))&REG_CF_STATE;
495     printf("\033[32m""\n[%s][DBG] cfb FSM: 0x%X\n""\033[m", __FUNCTION__, u8Val);
496 
497     return u8Val;
498 }
499