xref: /utopia/UTPA2-700.0.x/modules/hdmi/hal/maxim/hdcp/halHDCP.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   halHDCP.c
97 // @brief  HDCP HAL
98 // @author MStar Semiconductor,Inc.
99 ////////////////////////////////////////////////////////////////////////////////////////////////////
100 /*********************************************************************/
101 /*                                                                                                                     */
102 /*                                                   Includes                                                      */
103 /*                                                                                                                     */
104 /*********************************************************************/
105 #include <stdio.h>
106 #include <string.h>
107 #include "MsCommon.h"
108 #include "MsTypes.h"
109 #include "regHDCP.h"
110 #include "halHDCP.h"
111 #include "drvCPU.h"
112 
113 #ifndef HAL_HDCP_C
114 #define HAL_HDCP_C
115 
116 /*********************************************************************/
117 /*                                                                                                                     */
118 /*                                                      Defines                                                    */
119 /*                                                                                                                     */
120 /*********************************************************************/
121 #define DEF_HDCP_TX_FUNC_EN     1
122 
123 #if(defined(CONFIG_MLOG))
124 #include "ULog.h"
125 
126 #define HalHDCPLogInfo(format, args...)       ULOGI("HDCP", format, ##args)
127 #define HalHDCPLogWarning(format, args...)    ULOGW("HDCP", format, ##args)
128 #define HalHDCPLogDebug(format, args...)      ULOGD("HDCP", format, ##args)
129 #define HalHDCPLogError(format, args...)      ULOGE("HDCP", format, ##args)
130 #define HalHDCPLogFatal(format, args...)      ULOGF("HDCP", format, ##args)
131 
132 #else
133 
134 #define HalHDCPLogInfo(format, args...)       printf(format, ##args)
135 #define HalHDCPLogWarning(format, args...)    printf(format, ##args)
136 #define HalHDCPLogDebug(format, args...)      printf(format, ##args)
137 #define HalHDCPLogError(format, args...)      printf(format, ##args)
138 #define HalHDCPLogFatal(format, args...)      printf(format, ##args)
139 
140 #endif
141 
142 
143 #define DEF_SIZE_OF_KSXORLC128  16
144 #define DEF_SIZE_OF_RIV         8
145 #define DEF_SIZE_OF_HDCP1X_KEY  304
146 
147 MS_VIRT _gHDCPRegBase = 0x00U;
148 MS_VIRT _gHDCPPMRegBase = 0x00U;
149 
150 #define HDCPREG(bank, addr)     (*((volatile MS_U16 *)((_gHDCPRegBase + (bank << 1U)) + (addr << 2U))))
151 #define HDCPPMREG(bank, addr)   (*((volatile MS_U16 *)((_gHDCPPMRegBase + (bank << 1U)) + (addr << 2U))))
152 
153 #define DEF_HDCP14_M0_SIZE          64U //bytes
154 
155 /*********************************************************************/
156 /*                                                                                                                     */
157 /*                                                    Global                                                        */
158 /*                                                                                                                     */
159 /*********************************************************************/
160 MS_U8 gu8Hdcp1xKey[DEF_SIZE_OF_HDCP1X_KEY]= {0x00};
161 static MS_BOOL gbIsKmNewMode = FALSE;
162 
163 /*********************************************************************/
164 /*                                                                                                                     */
165 /*                                                    Functions                                                    */
166 /*                                                                                                                     */
167 /*********************************************************************/
168 /*********************************************************************/
169 /*                                                                                                                     */
170 /*                                                    Internal                                                      */
171 /*                                                                                                                     */
172 /*********************************************************************/
173 
MHalHdcpRegRead(MS_U32 bank,MS_U16 address)174 MS_U16 MHalHdcpRegRead(MS_U32 bank, MS_U16 address)
175 {
176     return HDCPREG(bank, address);
177 }
178 
MHalHdcpRegWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_data)179 void MHalHdcpRegWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_data)
180 {
181     HDCPREG(bank, address) = reg_data;
182 }
183 
MHalHdcpRegMaskWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_mask,MS_U16 reg_data)184 void MHalHdcpRegMaskWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_mask, MS_U16 reg_data)
185 {
186     MS_U16 reg_value;
187 
188     reg_value = (HDCPREG(bank, address) & (~reg_mask)) | (reg_data & reg_mask);
189     HDCPREG(bank, address) = reg_value;
190 }
191 
MHalHdcpPMRegRead(MS_U32 bank,MS_U16 address)192 MS_U16 MHalHdcpPMRegRead(MS_U32 bank, MS_U16 address)
193 {
194     return HDCPPMREG(bank, address);
195 }
196 
MHalHdcpPMRegWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_data)197 void MHalHdcpPMRegWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_data)
198 {
199     HDCPPMREG(bank, address) = reg_data;
200 }
201 
MHalHdcpPMRegMaskWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_mask,MS_U16 reg_data)202 void MHalHdcpPMRegMaskWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_mask, MS_U16 reg_data)
203 {
204     MS_U16 reg_value;
205 
206     reg_value = (HDCPPMREG(bank, address) & (~reg_mask)) | (reg_data & reg_mask);
207     HDCPPMREG(bank, address) = reg_value;
208 }
209 
210 /*********************************************************************/
211 /*                                                                                                                     */
212 /*                                                    External                                                     */
213 /*                                                                                                                     */
214 /*********************************************************************/
MHal_HDCP_HDCP14TxInitHdcp(MS_U8 u8PortIdx)215 void MHal_HDCP_HDCP14TxInitHdcp(MS_U8 u8PortIdx)
216 {
217     u8PortIdx &= 0x0F;
218 
219     //TBD: get bank offset by port index
220     MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0001, 0x8000, 0x8000); // Enable HDCP encryption
221     MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x001C, 0x0000); //[4]: 1: km new mode; 0: km old mode
222 }
223 
MHal_HDCP_HDCP14TxLoadKey(MS_U8 * pu8KeyData,MS_BOOL bUseKmNewMode)224 void MHal_HDCP_HDCP14TxLoadKey(MS_U8* pu8KeyData, MS_BOOL bUseKmNewMode)
225 {
226     gbIsKmNewMode = bUseKmNewMode;
227     if (pu8KeyData != NULL)
228         memcpy(gu8Hdcp1xKey, pu8KeyData, DEF_SIZE_OF_HDCP1X_KEY);
229 }
230 
MHal_HDCP_HDCP14TxSetAuthPass(MS_U8 u8PortIdx)231 void MHal_HDCP_HDCP14TxSetAuthPass(MS_U8 u8PortIdx)
232 {
233     u8PortIdx &= 0x0F;
234 
235     //TBD: get bank offset by port index
236     MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x000F, 0x000C);
237 }
238 
MHal_HDCP_HDCP14TxEnableENC_EN(MS_U8 u8PortIdx,MS_BOOL bEnable)239 void MHal_HDCP_HDCP14TxEnableENC_EN(MS_U8 u8PortIdx, MS_BOOL bEnable)
240 {
241     u8PortIdx &= 0x0F;
242 
243     if (bEnable == TRUE)
244         MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x0008, 0x0008);
245     else
246         MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x0008, 0x0000);
247 }
248 
MHal_HDCP_HDCP14TxProcessAn(MS_U8 u8PortIdx,MS_BOOL bUseInternalAn,MS_U8 * pu8An)249 void MHal_HDCP_HDCP14TxProcessAn(MS_U8 u8PortIdx, MS_BOOL bUseInternalAn, MS_U8* pu8An)
250 {
251     MS_U8 i = 0;
252     u8PortIdx &= 0x0F;
253 
254     if (bUseInternalAn == TRUE)
255     {
256         MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0001, 0x0100, 0x0100);
257         MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x0002, 0x0002);
258         MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x0002, 0x0000);
259 
260         MsOS_DelayTaskUs(1);
261 
262         for ( i = 0; i < 4; i++ )
263         {
264             *(pu8An + 2*i) = MHalHdcpRegRead(DEF_HDCP14_TX_REG_BANK, 0x0008 + i) & 0x00FF;
265             *(pu8An + 2*i + 1) = (MHalHdcpRegRead(DEF_HDCP14_TX_REG_BANK, 0x0008 + i) & 0xFF00) >> 8;
266         }
267     }
268     else
269     {
270         MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0001, 0x0100, 0x0000);
271 
272         for ( i = 0; i < 4; i++ )
273         {
274             MHalHdcpRegWrite(DEF_HDCP14_TX_REG_BANK, 0x0008 + i, ((*(pu8An + 2*i + 1) << 8) | (*(pu8An + 2*i))));
275         }
276     }
277 }
278 
279 
MHal_HDCP_HDCP14TxGetAKSV(MS_U8 u8PortIdx,MS_U8 * pu8Aksv)280 void MHal_HDCP_HDCP14TxGetAKSV(MS_U8 u8PortIdx, MS_U8* pu8Aksv)
281 {
282     MS_U8 u8ByteCnt = 0;
283 
284     u8PortIdx &= 0x0F;
285 
286     for (u8ByteCnt = 0; u8ByteCnt < 5; u8ByteCnt++ )
287     {
288         *(pu8Aksv + u8ByteCnt) = gu8Hdcp1xKey[u8ByteCnt];
289     }
290 }
291 
292 
MHal_HDCP_HDCP14TxCompareRi(MS_U8 u8PortIdx,MS_U8 * pu8SinkRi)293 MS_BOOL MHal_HDCP_HDCP14TxCompareRi(MS_U8 u8PortIdx, MS_U8* pu8SinkRi)
294 {
295     MS_BOOL bRet = FALSE;
296     MS_U16 u16SrcRi = 0x0000;
297     MS_U16 ulSinkRi = *pu8SinkRi;
298     u8PortIdx &= 0x0F;
299 
300     do
301     {
302         u16SrcRi = MHalHdcpRegRead(DEF_HDCP14_TX_REG_BANK, 0x0000);
303 
304         if(u16SrcRi == ulSinkRi)
305         {
306             bRet = TRUE;
307             break;
308         }
309 
310         u16SrcRi = MHalHdcpRegRead(DEF_HDCP14_TX_REG_BANK, 0x0000);
311 
312         if(u16SrcRi == ulSinkRi)
313         {
314             bRet = TRUE;
315             break;
316         }
317 
318     } while (FALSE);
319 
320     return bRet;
321 }
322 
323 
MHal_HDCP_HDCP14TxConfigMode(MS_U8 u8PortIdx,MS_U8 u8Mode)324 void MHal_HDCP_HDCP14TxConfigMode(MS_U8 u8PortIdx, MS_U8 u8Mode)
325 {
326     u8PortIdx &= 0x0F;
327 
328     MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0001, 0x0E00, u8Mode << 8);
329 }
330 
331 
MHal_HDCP_HDCP14TxGenerateCipher(MS_U8 u8PortIdx,MS_U8 * pu8Bksv)332 void MHal_HDCP_HDCP14TxGenerateCipher(MS_U8 u8PortIdx, MS_U8* pu8Bksv)
333 {
334 #define DEF_HDCP1X_KEY_OFFSET 8U
335 
336     MS_U8 u8Lm[7] = {0};
337     MS_U8 u8ByteCnt = 0;
338     MS_U8 u8BitCnt = 0;
339     MS_U8 u8LmCnt = 0;
340     MS_U8 u8CarryBit = 0;
341     MS_U8 u8Seed = 0;
342     MS_U8 u8Tmp = 0;
343     MS_U16 u16Offset = 0;
344 
345     for (u8ByteCnt = 0; u8ByteCnt < 5; u8ByteCnt++)
346     {
347         for (u8BitCnt = 0; u8BitCnt < 8; u8BitCnt++)
348         {
349             if (*(pu8Bksv + u8ByteCnt) & (1 << u8BitCnt))
350             {
351                 u8CarryBit = 0;
352                 u16Offset = (u8ByteCnt * 8 + u8BitCnt) * 7 + DEF_HDCP1X_KEY_OFFSET;
353 
354                 for (u8LmCnt = 0; u8LmCnt < 7; u8LmCnt++)
355                 {
356                     u8Seed = (u8LmCnt + gu8Hdcp1xKey[7]) % 7;
357                     u8Tmp = gu8Hdcp1xKey[u16Offset + u8LmCnt] ^ gu8Hdcp1xKey[u8Seed];
358 
359                     u8Lm[u8LmCnt] = u8Lm[u8LmCnt] + u8Tmp + u8CarryBit;
360                     if (((u8CarryBit == 0) && (u8Lm[u8LmCnt] >= u8Tmp)) || ((u8CarryBit == 1) && (u8Lm[u8LmCnt] > u8Tmp)))
361                         u8CarryBit = 0;
362                     else
363                         u8CarryBit = 1;
364                 }
365             }
366         }
367     }
368 
369     u8Tmp = gu8Hdcp1xKey[288];
370 
371     for (u8LmCnt = 0; u8LmCnt < 7; u8LmCnt++)
372     {
373         if (u8LmCnt < 6)
374             u8Lm[u8LmCnt] = u8Lm[u8LmCnt] ^ u8Tmp;
375         else
376             u8Lm[u8LmCnt] = u8Lm[u8LmCnt] ^ (~u8Tmp);
377 
378         if (u8LmCnt % 2 != 0)
379             MHalHdcpRegWrite(DEF_HDCP14_TX_REG_BANK, 0x0004 + (u8LmCnt/2), (u8Lm[u8LmCnt] << 8) | u8Lm[u8LmCnt - 1]);
380     }
381 
382     MHalHdcpRegWrite(DEF_HDCP14_TX_REG_BANK, 0x0007, (u8Tmp << 8) | u8Lm[6]);
383 
384 #undef DEF_HDCP1X_KEY_OFFSET
385 
386 }
387 
MHal_HDCP_HDCP14TxProcessR0(MS_U8 u8PortIdx)388 MS_BOOL MHal_HDCP_HDCP14TxProcessR0(MS_U8 u8PortIdx)
389 {
390     MS_U8 u8Cnt = 0;
391 
392     MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x000F, 0x0000);
393     MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x000F, 0x0001);
394     MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, 0x0002, 0x000F, 0x0000);
395 
396     while (u8Cnt-- && !(MHalHdcpRegRead(DEF_HDCP14_TX_REG_BANK, 0x0002) & 0x0100));
397 
398     return ((u8Cnt == 0) ? FALSE : TRUE);
399 }
400 
MHal_HDCP_HDCP14TxGetM0(MS_U8 u8PortIdx,MS_U8 * pu8M0)401 void MHal_HDCP_HDCP14TxGetM0(MS_U8 u8PortIdx, MS_U8* pu8M0)
402 {
403 #define DEF_HDCP1X_M0_SIZE 8
404     unsigned char u8DataCnt = 0;
405     MS_U16 u16BKOffset = 0x00;
406     MS_U16 u16RegVal = 0x00;
407 
408     u8PortIdx &= 0x0F;
409 
410     for ( u8DataCnt = 0; u8DataCnt < (DEF_HDCP1X_M0_SIZE>>1); u8DataCnt++ )
411     {
412         u16RegVal = MHalHdcpRegRead(DEF_HDCP14_TX_REG_BANK + u16BKOffset, 0x0C + u8DataCnt);
413         *(pu8M0 + 2*u8DataCnt) = (MS_U8)(u16RegVal & 0x00FF);
414         *(pu8M0 + 2*u8DataCnt + 1) = (MS_U8)((u16RegVal & 0xFF00)>>8);
415 
416     }
417 #undef DEF_HDCP1X_M0_SIZE
418 }
419 
MHal_HDCP_HDCP14GetM0(MS_U8 u8PortIdx,MS_U8 * pu8Data)420 void MHal_HDCP_HDCP14GetM0(MS_U8 u8PortIdx, MS_U8 *pu8Data)
421 {
422     MS_U8 cnt = 0x00;
423     MS_U16 u16BKOffset = 0x00;
424 
425     u8PortIdx &= 0x0F;
426     u16BKOffset = u8PortIdx * 0x300;
427 
428     for ( cnt = 0; cnt < (DEF_HDCP14_M0_SIZE >> 4); cnt++ )
429     {
430         MS_U16 u16tmpData = 0x00;
431 
432         u16tmpData = MHalHdcpRegRead(DEF_HDCP14_RX_REG_BANK + u16BKOffset, 0x0E + cnt);
433 
434         *(pu8Data + cnt*2) = (MS_U8)(u16tmpData & 0x00FF);
435         *(pu8Data + cnt*2 + 1) = (MS_U8)((u16tmpData & 0xFF00) >> 8);
436     }
437 }
438 
MHal_HDCP_HDCP14FillBksv(MS_U8 * pu8BksvData)439 void MHal_HDCP_HDCP14FillBksv(MS_U8 *pu8BksvData)
440 {
441     MS_U8 uctemp = 0;
442     MS_U8 ucPortSelect = 0;
443     MS_U32 ulMACBankOffset = 0;
444 
445     for(ucPortSelect = HDMI_RX_SELECT_PORTA; ucPortSelect < HDMI_RX_SELECT_MASK; ucPortSelect++)
446     {
447         switch(ucPortSelect)
448         {
449             case HDMI_RX_SELECT_PORTA:
450                 ulMACBankOffset = 0;
451                 break;
452 
453             case HDMI_RX_SELECT_PORTB:
454                 ulMACBankOffset = 0x300;
455                 break;
456 
457             case HDMI_RX_SELECT_PORTC:
458                 ulMACBankOffset = 0x600;
459                 break;
460 
461             case HDMI_RX_SELECT_PORTD:
462                 ulMACBankOffset = 0x900;
463                 break;
464 
465             default:
466                 break;
467         };
468 
469         // Bksv
470         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x17, BIT(10), BIT(10));
471         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19, BIT(15)|BIT(14), BIT(15)); // [15]: CPU write disable, [14]: 0: 74 RAM, 1 :HDCP RAM
472 
473         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x17, BMASK(9:0), 0x00); // address
474         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19, BIT(5), BIT(5));
475 
476         for(uctemp = 0; uctemp < 5; uctemp++)
477         {
478             MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x18, BMASK(7:0), pu8BksvData[uctemp]); // data
479             MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19, BIT(4), BIT(4)); // trigger latch data
480 
481             while(MHalHdcpRegRead(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19) & BIT(7)); // wait write ready
482             while(MHalHdcpRegRead(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19) & BIT(7)); // wait write ready for SW patch
483         }
484 
485         // Bcaps = 0x80
486         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x17, BMASK(9:0), 0x40); // address
487         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19, BIT(5), BIT(5));
488 
489         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x18, BMASK(7:0), 0x80); // data
490         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19, BIT(4), BIT(4)); // trigger latch data
491 
492         while(MHalHdcpRegRead(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19) & BIT(7)); // wait write ready
493 
494         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x19, BIT(15)|BIT(14), 0); // [15]: CPU write disable, [14]: 0: 74 RAM, 1 :HDCP RAM
495 
496         // [10:8]: 3'b111 determine Encrp_En during Vblank in DVI mode; [5]:HDCP enable; [0]: EESS mode deglitch Vsync mode
497         MHalHdcpRegMaskWrite(DEF_HDCP14_RX_REG_BANK +ulMACBankOffset, 0x00, BIT(10)|BIT(9)|BIT(8)|BIT(5)|BIT(0), BIT(10)|BIT(9)|BIT(8)|BIT(5)|BIT(0));
498     }
499 }
500 
MHal_HDCP_HDCP14FillKey(MS_U8 * pu8KeyData)501 void MHal_HDCP_HDCP14FillKey(MS_U8 *pu8KeyData)
502 {
503     MS_U16 ustemp = 0;
504 
505     MHalHdcpRegMaskWrite(DEF_HDCPKEY_REG_BANK, REG_HDCPKEY_BANK_02_L, BIT(8), BIT(8));
506 
507     // HDCP key
508     MHalHdcpRegMaskWrite(DEF_COMBO_GP_TOP_REG_BANK, REG_COMBO_GP_TOP_40_L, BIT(3)|BIT(2)|BIT(0), BIT(3)|BIT(2)|BIT(0)); // [2]: CPU write enable, [3]: 0: 74 RAM, 1 :HDCP RAM
509     // burst write from address 0x05
510     MHalHdcpRegMaskWrite(DEF_HDCPKEY_REG_BANK, REG_HDCPKEY_BANK_00_L, BMASK(9:0), 0x05); // address
511 
512     for(ustemp = 0; ustemp < 284; ustemp++)
513     {
514         MHalHdcpRegMaskWrite(DEF_HDCPKEY_REG_BANK, REG_HDCPKEY_BANK_01_L, BMASK(7:0), *(pu8KeyData +ustemp)); // data
515     }
516 
517     MHalHdcpRegMaskWrite(DEF_COMBO_GP_TOP_REG_BANK, REG_COMBO_GP_TOP_40_L, BIT(3)|BIT(2)|BIT(0), 0); // [2]: CPU write enable, [3]: 0: 74 RAM, 1 :HDCP RAM
518 }
519 
MHal_HDCP_SetBank(MS_U32 u32NonPmBankAddr,MS_U32 u32PmBankAddr)520 void MHal_HDCP_SetBank(MS_U32 u32NonPmBankAddr, MS_U32 u32PmBankAddr)
521 {
522     HalHDCPLogInfo("u32NonPmBankAddr = 0x%X, u32PmBankAddr = 0x%X\r\n", (unsigned int)u32NonPmBankAddr, (unsigned int)u32PmBankAddr);
523     _gHDCPRegBase = u32NonPmBankAddr;
524     _gHDCPPMRegBase = u32PmBankAddr;
525 }
526 
MHal_HDCP_HDCP2TxInit(MS_U8 u8PortIdx,MS_BOOL bEnable)527 void MHal_HDCP_HDCP2TxInit(MS_U8 u8PortIdx, MS_BOOL bEnable)
528 {
529     MS_U16 u16BKOffset = 0x00;
530 
531     MHalHdcpRegMaskWrite(DEF_HDCP22_TX_REG_BANK + u16BKOffset, 0x0000, 0x11, bEnable ? 0x11 : 0x00); // bit 0: enable hdcp22; bit 4: enable EESS
532     if (bEnable)
533     {
534         MHalHdcpRegMaskWrite(DEF_HDCP22_TX_REG_BANK + u16BKOffset, 0x0000, 0x02, 0x02); //reset hdcp22 FSM
535         MHalHdcpRegMaskWrite(DEF_HDCP22_TX_REG_BANK + u16BKOffset, 0x0000, 0x02, 0x00);
536     }
537 }
538 
MHal_HDCP_HDCP2TxEnableEncrypt(MS_U8 u8PortIdx,MS_BOOL bEnable)539 void MHal_HDCP_HDCP2TxEnableEncrypt(MS_U8 u8PortIdx, MS_BOOL bEnable)
540 {
541     MS_U16 u16BKOffset = 0x00;
542 
543     MHalHdcpRegMaskWrite(DEF_HDCP22_TX_REG_BANK + u16BKOffset, 0x0000, 0x04, bEnable ? 0x04 : 0x00); //bit 2: authentication pass
544     MHalHdcpRegMaskWrite(DEF_HDCP22_TX_REG_BANK + u16BKOffset, 0x0000, 0x08, bEnable ? 0x08 : 0x00); //bit 3: enable hdcp22 to issue encryption enable signal
545 }
546 
MHal_HDCP_HDCP2TxFillCipherKey(MS_U8 u8PortIdx,MS_U8 * pu8Riv,MS_U8 * pu8KsXORLC128)547 void MHal_HDCP_HDCP2TxFillCipherKey(MS_U8 u8PortIdx, MS_U8 *pu8Riv, MS_U8 *pu8KsXORLC128)
548 {
549     MS_U8 cnt = 0;
550     MS_U16 u16BKOffset = 0x00;
551     //MS_U16 u16RegOffset = 0x00;
552 
553 
554     //Ks^LC128
555     for ( cnt = 0; cnt < (DEF_SIZE_OF_KSXORLC128>>1); cnt++)
556         MHalHdcpRegWrite(DEF_HDCP22_TX_KEY_REG_BANK + u16BKOffset, 0x60 + (DEF_SIZE_OF_KSXORLC128 >> 1) - 1 - cnt, *(pu8KsXORLC128 + cnt*2 + 1)|(*(pu8KsXORLC128 + cnt*2)<<8));
557 
558     //Riv
559     for ( cnt = 0; cnt < (DEF_SIZE_OF_RIV>>1); cnt++)
560         MHalHdcpRegWrite(DEF_HDCP22_TX_KEY_REG_BANK + u16BKOffset, 0x68 + (DEF_SIZE_OF_RIV >> 1) - 1 - cnt, *(pu8Riv + cnt*2 + 1)|(*(pu8Riv + cnt*2)<<8));
561 
562 }
563 
MHal_HDCP_HDCP2TxGetCipherState(MS_U8 u8PortIdx,MS_U8 * pu8State)564 void MHal_HDCP_HDCP2TxGetCipherState(MS_U8 u8PortIdx, MS_U8 *pu8State)
565 {
566     MS_U16 u16BKOffset = 0x00;
567     //MS_U16 u16RegOffset = 0x00;
568 
569     *pu8State = MHalHdcpRegRead(DEF_HDCP22_TX_REG_BANK + u16BKOffset, 0x00) & 0x0C;
570 }
571 
MHal_HDCP_HDCP2TxSetAuthPass(MS_U8 u8PortIdx,MS_BOOL bEnable)572 void MHal_HDCP_HDCP2TxSetAuthPass(MS_U8 u8PortIdx, MS_BOOL bEnable)
573 {
574     MS_U16 u16BKOffset = 0x00;
575 
576     MHalHdcpRegMaskWrite(DEF_HDCP22_TX_REG_BANK + u16BKOffset, 0x0000, 0x04, bEnable ? 0x04 : 0x00); //bit 2: authentication pass
577 }
578 
MHal_HDCP_HDCP2RxInit(MS_U8 u8PortIdx)579 void MHal_HDCP_HDCP2RxInit(MS_U8 u8PortIdx)
580 {
581     MS_U16 u16BKOffset = 0x00;
582 
583     u16BKOffset = u8PortIdx * 0x300;
584     // [1] Enable auto-clear SKE status when receiving ake_init; [2] Enable auto-clear SKE status when no hdcp22 capability
585     MHalHdcpRegMaskWrite(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E, 0x0006, 0x0006);
586 }
587 
MHal_HDCP_HDCP2RxProcessCipher(MS_U8 u8PortIdx,MS_U8 * pu8Riv,MS_U8 * pu8ContentKey)588 void MHal_HDCP_HDCP2RxProcessCipher(MS_U8 u8PortIdx, MS_U8* pu8Riv, MS_U8 *pu8ContentKey)
589 {
590     MS_U8 cnt = 0;
591     MS_U16 u16BKOffset = 0x00;
592     MS_U16 u16RegOffset = 0x00;
593 
594     u16BKOffset = u8PortIdx * 0x300;
595     u16RegOffset = u8PortIdx * 0x0C;
596 
597     HalHDCPLogDebug("%s:: PortIdx : BKOffset : RegOffset = 0x%X : 0x%X : 0x%X\r\n", __FUNCTION__, u8PortIdx, u16BKOffset, u16RegOffset);
598 
599     //Ks^LC128
600     for ( cnt = 0; cnt < (DEF_SIZE_OF_KSXORLC128>>1); cnt++)
601         MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK, u16RegOffset + 0x30 + (DEF_SIZE_OF_KSXORLC128 >> 1) - 1 - cnt, *(pu8ContentKey + cnt*2 + 1)|(*(pu8ContentKey + cnt*2)<<8));
602 
603     //Riv
604     for ( cnt = 0; cnt < (DEF_SIZE_OF_RIV>>1); cnt++)
605         MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK, u16RegOffset + 0x38 + (DEF_SIZE_OF_RIV >> 1) - 1 - cnt, *(pu8Riv + cnt*2 + 1)|(*(pu8Riv + cnt*2)<<8));
606 
607     //Set SKE successful
608     MHalHdcpRegMaskWrite(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E, 0x0001, 0x0001);
609 }
610 
MHal_HDCP_HDCP2RxSetSKEPass(MS_U8 u8PortIdx,MS_BOOL bEnable)611 void MHal_HDCP_HDCP2RxSetSKEPass(MS_U8 u8PortIdx, MS_BOOL bEnable)
612 {
613     MS_U16 u16BKOffset = 0x00;
614 
615     u16BKOffset = u8PortIdx * 0x300;
616     //Set SKE successful
617     MHalHdcpRegMaskWrite(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E, 0x0001, bEnable ? 0x0001 : 0x0000);
618 }
619 
MHal_HDCP_HDCP2RxFillCipherKey(MS_U8 u8PortIdx,MS_U8 * pu8Riv,MS_U8 * pu8ContentKey)620 void MHal_HDCP_HDCP2RxFillCipherKey(MS_U8 u8PortIdx, MS_U8* pu8Riv, MS_U8 *pu8ContentKey)
621 {
622     MS_U8 cnt = 0;
623     MS_U16 u16BKOffset = 0x00;
624     MS_U16 u16RegOffset = 0x00;
625 
626     //Ks^LC128
627     for ( cnt = 0; cnt < (DEF_SIZE_OF_KSXORLC128>>1); cnt++)
628         MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK + u16BKOffset, u16RegOffset + 0x30 + (DEF_SIZE_OF_KSXORLC128 >> 1) - 1 - cnt, *(pu8ContentKey + cnt*2 + 1)|(*(pu8ContentKey + cnt*2)<<8));
629 
630     //Riv
631     for ( cnt = 0; cnt < (DEF_SIZE_OF_RIV>>1); cnt++)
632         MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK + u16BKOffset, u16RegOffset + 0x38 + (DEF_SIZE_OF_RIV >> 1) - 1 - cnt, *(pu8Riv + cnt*2 + 1)|(*(pu8Riv + cnt*2)<<8));
633 }
634 
MHal_HDCP_HDCP2RxGetCipherState(MS_U8 u8PortIdx,MS_U8 * pu8State)635 void MHal_HDCP_HDCP2RxGetCipherState(MS_U8 u8PortIdx, MS_U8 *pu8State)
636 {
637     MS_U16 u16BKOffset = 0x00;
638     //MS_U16 u16RegOffset = 0x00;
639 
640     u16BKOffset = u8PortIdx * 0x300;
641 
642     *pu8State = MHalHdcpRegRead(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E) & 0x01;
643 }
644 
MHal_HDCP_HDCP1TxEncrytionStatus(MS_U8 u8PortIdx,MS_U8 u8SetStatusFlag,MS_U32 u32SetStatus)645 MS_U32 MHal_HDCP_HDCP1TxEncrytionStatus(MS_U8 u8PortIdx, MS_U8 u8SetStatusFlag, MS_U32 u32SetStatus)
646 {
647     MS_U32 u32GetStatus = 0;
648 
649     if(u8SetStatusFlag) // Set HDCP1 encrytion status
650     {
651         MHalHdcpRegMaskWrite(DEF_HDCP14_TX_REG_BANK, REG_HDCP14_TX_02_L, BIT(3), u32SetStatus? BIT(3): 0); //bit 3: enable hdcp14 to issue encryption enable signal
652     }
653 
654     // Get HDCP1 encrytion status
655     if(MHalHdcpRegRead(DEF_HDCP14_TX_REG_BANK, REG_HDCP14_TX_02_L) &BIT(3))
656     {
657         u32GetStatus = TRUE;
658     }
659 
660     return u32GetStatus;
661 }
662 
MHal_HDCP_HDCP2TxEncrytionStatus(MS_U8 u8PortIdx,MS_U8 u8SetStatusFlag,MS_U32 u32SetStatus)663 MS_U32 MHal_HDCP_HDCP2TxEncrytionStatus(MS_U8 u8PortIdx, MS_U8 u8SetStatusFlag, MS_U32 u32SetStatus)
664 {
665     MS_U32 u32GetStatus = 0;
666 
667     if(u8SetStatusFlag) // Set HDCP2 encrytion status
668     {
669         MHalHdcpRegMaskWrite(DEF_HDCP22_TX_REG_BANK, REG_HDCP22_TX_00_L, BIT(3), u32SetStatus? BIT(3): 0); //bit 3: enable hdcp22 to issue encryption enable signal
670     }
671 
672     // Get HDCP2 encrytion status
673     if(MHalHdcpRegRead(DEF_HDCP22_TX_REG_BANK, REG_HDCP22_TX_00_L) &BIT(3))
674     {
675         u32GetStatus = TRUE;
676     }
677 
678     return u32GetStatus;
679 }
680 
MHal_HDCP_HDCPTxHDMIStatus(MS_U8 u8PortIdx,MS_U8 u8SetStatusFlag,MS_U32 u32SetStatus)681 MS_U32 MHal_HDCP_HDCPTxHDMIStatus(MS_U8 u8PortIdx, MS_U8 u8SetStatusFlag, MS_U32 u32SetStatus)
682 {
683     MS_U32 u32GetStatus = 0;
684 
685     if(u8SetStatusFlag) // Set HDNI status
686     {
687         MHalHdcpRegMaskWrite(DEF_HDMITX_PHY_REG_BANK, REG_HDMITX_PHY_39_L, 0xFFFF, u32SetStatus? 0xF000: 0xFFFF);
688         MHalHdcpRegMaskWrite(DEF_HDMITX_PHY_REG_BANK, REG_HDMITX_PHY_2E_L, 0xE800, u32SetStatus? 0xE800: 0x0000);
689     }
690 
691     // Get HDNI status
692     if((MHalHdcpRegRead(DEF_HDMITX_PHY_REG_BANK, REG_HDMITX_PHY_2E_L) &0xE800) == 0xE800)
693     {
694         u32GetStatus = TRUE;
695     }
696 
697     return u32GetStatus;
698 }
699 
700 #endif //#ifndef HAL_HDCP_C
701