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 0
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
146 MS_VIRT _gHDCPRegBase = 0x00U;
147 MS_VIRT _gHDCPPMRegBase = 0x00U;
148
149 #define HDCPREG(bank, addr) (*((volatile MS_U16 *)((_gHDCPRegBase + (bank << 1U)) + (addr << 2U))))
150 #define HDCPPMREG(bank, addr) (*((volatile MS_U16 *)((_gHDCPPMRegBase + (bank << 1U)) + (addr << 2U))))
151
152 #define DEF_HDCP14_M0_SIZE 64U //bytes
153
154 /*********************************************************************/
155 /* */
156 /* Functions */
157 /* */
158 /*********************************************************************/
159 /*********************************************************************/
160 /* */
161 /* Internal */
162 /* */
163 /*********************************************************************/
164
MHalHdcpRegRead(MS_U32 bank,MS_U16 address)165 MS_U16 MHalHdcpRegRead(MS_U32 bank, MS_U16 address)
166 {
167 return HDCPREG(bank, address);
168 }
169
MHalHdcpRegWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_data)170 void MHalHdcpRegWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_data)
171 {
172 HDCPREG(bank, address) = reg_data;
173 }
174
MHalHdcpRegMaskWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_mask,MS_U16 reg_data)175 void MHalHdcpRegMaskWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_mask, MS_U16 reg_data)
176 {
177 MS_U16 reg_value;
178
179 reg_value = (HDCPREG(bank, address) & (~reg_mask)) | (reg_data & reg_mask);
180 HDCPREG(bank, address) = reg_value;
181 }
182
MHalHdcpPMRegRead(MS_U32 bank,MS_U16 address)183 MS_U16 MHalHdcpPMRegRead(MS_U32 bank, MS_U16 address)
184 {
185 return HDCPPMREG(bank, address);
186 }
187
MHalHdcpPMRegWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_data)188 void MHalHdcpPMRegWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_data)
189 {
190 HDCPPMREG(bank, address) = reg_data;
191 }
192
MHalHdcpPMRegMaskWrite(MS_U32 bank,MS_U16 address,MS_U16 reg_mask,MS_U16 reg_data)193 void MHalHdcpPMRegMaskWrite(MS_U32 bank, MS_U16 address, MS_U16 reg_mask, MS_U16 reg_data)
194 {
195 MS_U16 reg_value;
196
197 reg_value = (HDCPPMREG(bank, address) & (~reg_mask)) | (reg_data & reg_mask);
198 HDCPPMREG(bank, address) = reg_value;
199 }
200
201 /*********************************************************************/
202 /* */
203 /* External */
204 /* */
205 /*********************************************************************/
MHal_HDCP_HDCP14TxInitHdcp(MS_U8 u8PortIdx)206 void MHal_HDCP_HDCP14TxInitHdcp(MS_U8 u8PortIdx)
207 {
208 return; //this project has no tx port
209 }
210
MHal_HDCP_HDCP14TxLoadKey(MS_U8 * pu8KeyData,MS_BOOL bUseKmNewMode)211 void MHal_HDCP_HDCP14TxLoadKey(MS_U8* pu8KeyData, MS_BOOL bUseKmNewMode)
212 {
213 return; //this project has no tx port
214 }
215
MHal_HDCP_HDCP14TxSetAuthPass(MS_U8 u8PortIdx)216 void MHal_HDCP_HDCP14TxSetAuthPass(MS_U8 u8PortIdx)
217 {
218 return; //this project has no tx port
219 }
220
MHal_HDCP_HDCP14TxEnableENC_EN(MS_U8 u8PortIdx,MS_BOOL bEnable)221 void MHal_HDCP_HDCP14TxEnableENC_EN(MS_U8 u8PortIdx, MS_BOOL bEnable)
222 {
223 return; //this project has no tx port
224 }
225
MHal_HDCP_HDCP14TxProcessAn(MS_U8 u8PortIdx,MS_BOOL bUseInternalAn,MS_U8 * pu8An)226 void MHal_HDCP_HDCP14TxProcessAn(MS_U8 u8PortIdx, MS_BOOL bUseInternalAn, MS_U8* pu8An)
227 {
228 return; //this project has no tx port
229 }
230
231
MHal_HDCP_HDCP14TxGetAKSV(MS_U8 u8PortIdx,MS_U8 * pu8Aksv)232 void MHal_HDCP_HDCP14TxGetAKSV(MS_U8 u8PortIdx, MS_U8* pu8Aksv)
233 {
234 return; //this project has no tx port
235 }
236
237
MHal_HDCP_HDCP14TxCompareRi(MS_U8 u8PortIdx,MS_U8 * pu8SinkRi)238 MS_BOOL MHal_HDCP_HDCP14TxCompareRi(MS_U8 u8PortIdx, MS_U8* pu8SinkRi)
239 {
240 return FALSE; //this project has no tx port
241 }
242
243
MHal_HDCP_HDCP14TxConfigMode(MS_U8 u8PortIdx,MS_U8 u8Mode)244 void MHal_HDCP_HDCP14TxConfigMode(MS_U8 u8PortIdx, MS_U8 u8Mode)
245 {
246 return; //this project has no tx port
247 }
248
249
MHal_HDCP_HDCP14TxGenerateCipher(MS_U8 u8PortIdx,MS_U8 * pu8Bksv)250 void MHal_HDCP_HDCP14TxGenerateCipher(MS_U8 u8PortIdx, MS_U8* pu8Bksv)
251 {
252 return; //this project has no tx port
253
254
255 }
256
MHal_HDCP_HDCP14TxProcessR0(MS_U8 u8PortIdx)257 MS_BOOL MHal_HDCP_HDCP14TxProcessR0(MS_U8 u8PortIdx)
258 {
259 return FALSE; //this project has no tx port
260 }
261
MHal_HDCP_HDCP14TxGetM0(MS_U8 u8PortIdx,MS_U8 * pu8M0)262 void MHal_HDCP_HDCP14TxGetM0(MS_U8 u8PortIdx, MS_U8* pu8M0)
263 {
264 return;
265 }
266
MHal_HDCP_HDCP14GetM0(MS_U8 u8PortIdx,MS_U8 * pu8Data)267 void MHal_HDCP_HDCP14GetM0(MS_U8 u8PortIdx, MS_U8 *pu8Data)
268 {
269 MS_U8 cnt = 0x00;
270 MS_U16 u16BKOffset = 0x00;
271
272 u8PortIdx &= 0x0F;
273 u16BKOffset = u8PortIdx * 0x300;
274
275 for ( cnt = 0; cnt < (DEF_HDCP14_M0_SIZE >> 4); cnt++ )
276 {
277 MS_U16 u16tmpData = 0x00;
278
279 u16tmpData = MHalHdcpRegRead(DEF_HDCP14_RX_REG_BANK + u16BKOffset, 0x0E + cnt);
280
281 *(pu8Data + cnt*2) = (MS_U8)(u16tmpData & 0x00FF);
282 *(pu8Data + cnt*2 + 1) = (MS_U8)((u16tmpData & 0xFF00) >> 8);
283 }
284 }
285
MHal_HDCP_HDCP14FillKey(MS_U8 * pu8KeyData)286 void MHal_HDCP_HDCP14FillKey(MS_U8 *pu8KeyData)
287 {
288 MS_U16 ustemp = 0;
289
290 MHalHdcpRegMaskWrite(DEF_HDCPKEY_REG_BANK, REG_HDCPKEY_BANK_02_L, BIT(8), BIT(8));
291
292 // HDCP key
293 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
294 // burst write from address 0x05
295 MHalHdcpRegMaskWrite(DEF_HDCPKEY_REG_BANK, REG_HDCPKEY_BANK_00_L, 0x05, BMASK(9:0)); // address
296
297 for(ustemp = 0; ustemp < 284; ustemp++)
298 {
299 MHalHdcpRegMaskWrite(DEF_HDCPKEY_REG_BANK, REG_HDCPKEY_BANK_01_L, *(pu8KeyData +ustemp), BMASK(7:0)); // data
300 }
301
302 MHalHdcpRegMaskWrite(DEF_COMBO_GP_TOP_REG_BANK, REG_COMBO_GP_TOP_40_L, 0, BIT(3)|BIT(2)|BIT(0)); // [2]: CPU write enable, [3]: 0: 74 RAM, 1 :HDCP RAM
303 }
304
MHal_HDCP_SetBank(MS_U32 u32NonPmBankAddr,MS_U32 u32PmBankAddr)305 void MHal_HDCP_SetBank(MS_U32 u32NonPmBankAddr, MS_U32 u32PmBankAddr)
306 {
307 HalHDCPLogInfo("u32NonPmBankAddr = 0x%X, u32PmBankAddr = 0x%X\r\n", (unsigned int)u32NonPmBankAddr, (unsigned int)u32PmBankAddr);
308 _gHDCPRegBase = u32NonPmBankAddr;
309 _gHDCPPMRegBase = u32PmBankAddr;
310 }
311
MHal_HDCP_HDCP2TxInit(MS_U8 u8PortIdx,MS_BOOL bEnable)312 void MHal_HDCP_HDCP2TxInit(MS_U8 u8PortIdx, MS_BOOL bEnable)
313 {
314 return; //this project has no tx port
315 }
316
MHal_HDCP_HDCP2TxEnableEncrypt(MS_U8 u8PortIdx,MS_BOOL bEnable)317 void MHal_HDCP_HDCP2TxEnableEncrypt(MS_U8 u8PortIdx, MS_BOOL bEnable)
318 {
319 return;
320 }
321
MHal_HDCP_HDCP2TxFillCipherKey(MS_U8 u8PortIdx,MS_U8 * pu8Riv,MS_U8 * pu8KsXORLC128)322 void MHal_HDCP_HDCP2TxFillCipherKey(MS_U8 u8PortIdx, MS_U8 *pu8Riv, MS_U8 *pu8KsXORLC128)
323 {
324 return;
325 }
326
MHal_HDCP_HDCP2TxGetCipherState(MS_U8 u8PortIdx,MS_U8 * pu8State)327 void MHal_HDCP_HDCP2TxGetCipherState(MS_U8 u8PortIdx, MS_U8 *pu8State)
328 {
329 return;
330 }
331
MHal_HDCP_HDCP2TxSetAuthPass(MS_U8 u8PortIdx,MS_BOOL bEnable)332 void MHal_HDCP_HDCP2TxSetAuthPass(MS_U8 u8PortIdx, MS_BOOL bEnable)
333 {
334 return;
335 }
336
MHal_HDCP_HDCP2RxInit(MS_U8 u8PortIdx)337 void MHal_HDCP_HDCP2RxInit(MS_U8 u8PortIdx)
338 {
339 MS_U16 u16BKOffset = 0x00;
340
341
342 // [1] Enable auto-clear SKE status when receiving ake_init; [2] Enable auto-clear SKE status when no hdcp22 capability
343 MHalHdcpRegMaskWrite(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E, 0x0006, 0x0006);
344 }
345
MHal_HDCP_HDCP2RxProcessCipher(MS_U8 u8PortIdx,MS_U8 * pu8Riv,MS_U8 * pu8ContentKey)346 void MHal_HDCP_HDCP2RxProcessCipher(MS_U8 u8PortIdx, MS_U8* pu8Riv, MS_U8 *pu8ContentKey)
347 {
348 MS_U8 cnt = 0;
349 MS_U16 u16BKOffset = 0x00;
350 MS_U16 u16RegOffset = 0x00;
351
352 //Ks^LC128
353 for ( cnt = 0; cnt < (DEF_SIZE_OF_KSXORLC128>>1); cnt++)
354 MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK, u16RegOffset + 0x30 + (DEF_SIZE_OF_KSXORLC128 >> 1) - 1 - cnt, *(pu8ContentKey + cnt*2 + 1)|(*(pu8ContentKey + cnt*2)<<8));
355
356 //Riv
357 for ( cnt = 0; cnt < (DEF_SIZE_OF_RIV>>1); cnt++)
358 MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK, u16RegOffset + 0x38 + (DEF_SIZE_OF_RIV >> 1) - 1 - cnt, *(pu8Riv + cnt*2 + 1)|(*(pu8Riv + cnt*2)<<8));
359
360 //Set SKE successful
361 MHalHdcpRegMaskWrite(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E, 0x0001, 0x0001);
362 }
363
MHal_HDCP_HDCP2RxSetSKEPass(MS_U8 u8PortIdx,MS_BOOL bEnable)364 void MHal_HDCP_HDCP2RxSetSKEPass(MS_U8 u8PortIdx, MS_BOOL bEnable)
365 {
366 MS_U16 u16BKOffset = 0x00;
367 MS_U16 u16RegOffset = 0x00;
368
369 //Set SKE successful
370 MHalHdcpRegMaskWrite(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E, 0x0001, bEnable ? 0x0001 : 0x0000);
371 }
372
MHal_HDCP_HDCP2RxFillCipherKey(MS_U8 u8PortIdx,MS_U8 * pu8Riv,MS_U8 * pu8ContentKey)373 void MHal_HDCP_HDCP2RxFillCipherKey(MS_U8 u8PortIdx, MS_U8* pu8Riv, MS_U8 *pu8ContentKey)
374 {
375 MS_U8 cnt = 0;
376 MS_U16 u16BKOffset = 0x00;
377 MS_U16 u16RegOffset = 0x00;
378
379 //Ks^LC128
380 for ( cnt = 0; cnt < (DEF_SIZE_OF_KSXORLC128>>1); cnt++)
381 MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK, u16RegOffset + 0x30 + (DEF_SIZE_OF_KSXORLC128 >> 1) - 1 - cnt, *(pu8ContentKey + cnt*2 + 1)|(*(pu8ContentKey + cnt*2)<<8));
382
383 //Riv
384 for ( cnt = 0; cnt < (DEF_SIZE_OF_RIV>>1); cnt++)
385 MHalHdcpRegWrite(DEF_HDCP22_RX_KEY_REG_BANK, u16RegOffset + 0x38 + (DEF_SIZE_OF_RIV >> 1) - 1 - cnt, *(pu8Riv + cnt*2 + 1)|(*(pu8Riv + cnt*2)<<8));
386 }
387
MHal_HDCP_HDCP2RxGetCipherState(MS_U8 u8PortIdx,MS_U8 * pu8State)388 void MHal_HDCP_HDCP2RxGetCipherState(MS_U8 u8PortIdx, MS_U8 *pu8State)
389 {
390 MS_U16 u16BKOffset = 0x00;
391 //MS_U16 u16RegOffset = 0x00;
392
393 u16BKOffset = u8PortIdx * 0x300;
394
395 *pu8State = MHalHdcpRegRead(DEF_HDCP22_RX_REG_BANK + u16BKOffset, 0x4E) & 0x01;
396 }
397
MHal_HDCP_HDCP1TxEncrytionStatus(MS_U8 u8PortIdx,MS_U8 u8SetStatusFlag,MS_U32 u32SetStatus)398 MS_U32 MHal_HDCP_HDCP1TxEncrytionStatus(MS_U8 u8PortIdx, MS_U8 u8SetStatusFlag, MS_U32 u32SetStatus)
399 {
400 MS_U32 u32GetStatus = 0;
401
402 if(u8SetStatusFlag) // Set HDCP1 encrytion status
403 {
404
405 }
406
407 // Get HDCP1 encrytion status
408
409 return u32GetStatus;
410 }
411
MHal_HDCP_HDCP2TxEncrytionStatus(MS_U8 u8PortIdx,MS_U8 u8SetStatusFlag,MS_U32 u32SetStatus)412 MS_U32 MHal_HDCP_HDCP2TxEncrytionStatus(MS_U8 u8PortIdx, MS_U8 u8SetStatusFlag, MS_U32 u32SetStatus)
413 {
414 MS_U32 u32GetStatus = 0;
415
416 if(u8SetStatusFlag) // Set HDCP2 encrytion status
417 {
418
419 }
420
421 // Get HDCP2 encrytion status
422
423 return u32GetStatus;
424 }
425
MHal_HDCP_HDCPTxHDMIStatus(MS_U8 u8PortIdx,MS_U8 u8SetStatusFlag,MS_U32 u32SetStatus)426 MS_U32 MHal_HDCP_HDCPTxHDMIStatus(MS_U8 u8PortIdx, MS_U8 u8SetStatusFlag, MS_U32 u32SetStatus)
427 {
428 MS_U32 u32GetStatus = 0;
429
430 if(u8SetStatusFlag) // Set HDNI status
431 {
432
433 }
434
435 // Get HDNI status
436
437 return u32GetStatus;
438 }
439
440 #endif //#ifndef HAL_HDCP_C
441