xref: /utopia/UTPA2-700.0.x/modules/dscmb/hal/k7u/nsk2/halNSK2.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 #define _HAL_NSK2_C
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// @file halEMMflt.c
97 /// @author MStar Semiconductor Inc.
98 /// @brief
99 ////////////////////////////////////////////////////////////////////////////////
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 // Header Files
103 ////////////////////////////////////////////////////////////////////////////////
104 #ifdef MSOS_TYPE_LINUX_KERNEL
105 #include <linux/string.h>
106 #else
107 #include "string.h"
108 #endif
109 
110 #include "MsCommon.h"
111 #include "MsTypes.h"
112 #include "drvSYS.h"
113 
114 #include "halNSK2.h"
115 #include "regNSK2.h"
116 #include "MsOS.h"
117 
118 #include "regTSP.h" // todo
119 #include "halTSP.h" // todo
120 #include "drvDSCMB.h"
121 #include "halDSCMB.h" // todo
122 
123 #include "drvNSK2Type.h"
124 #include "drvCA.h"
125 
126 #include "nsk_282.h"
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 // Define & data type
130 ///////////////////////////////////////////////////////////////////////////////
131 
132 //#define SlowClockTest
133 
134 #define POLLING_CNT     100
135 
136 #ifdef SlowClockTest
137 #define BUSYCHECK_CNT   0x1000000
138 #else
139 #define BUSYCHECK_CNT   100
140 #endif
141 
142 static MS_U32 _gBasicAddr = 0;
143 static MS_U32 _gNSK2_Addr = 0;
144 static MS_U32 _gOTP_Addr = 0;
145 static MS_U32 _gOTP_CTRL_Addr = 0;
146 static MS_U32 _gNI_Addr = 0;
147 static MS_U32 _gRSA_Addr = 0;
148 static MS_U32 _gKeyTable_Addr = 0;
149 static MS_U32 _gNDSJTagPwd_Addr = 0;
150 static MS_U32 _gCipherCH0_Addr = 0;
151 static MS_U32 _gCryptoDMA_Addr = 0;
152 
153 static MS_U32 _g32NSK2HalDbgLv = NSK2_DBGLV_DEBUG;
154 
155 #define HALNSK2_DBG(lv, x, args...)   if (lv <= _g32NSK2HalDbgLv ) \
156                                         {printf(x, ##args);}
157 
158 //bank 0x1700
159 #define OTP_REG(addr)           (*((volatile MS_U32*)(_gOTP_Addr + addr )))
160 
161 //bank 0x1a13
162 #define OTP_CTRL_REG(addr)      (*((volatile MS_U32*)(_gOTP_CTRL_Addr + (addr<<2) )))
163 
164 //bank 0x1620
165 #define NI_REG(addr)            (*((volatile MS_U32*)(_gNI_Addr + (addr<<2) )))
166 
167 //bank 1630
168 #define RSA_REG(addr)           (*((volatile MS_U32*)(_gRSA_Addr + (addr<<2) )))
169 
170 //bank 1626
171 #define KeyTable_REG(addr)      (*((volatile MS_U32*)(_gKeyTable_Addr + (addr<<2) )))
172 
173 //bank xxxxxx
174 #define NDSJTagPwd_REG(addr)    (*((volatile MS_U16*)(_gNDSJTagPwd_Addr + (addr<<2) )))
175 
176 //bank 1621
177 #define CMCHANNEL0_REG(addr)    (*((volatile MS_U32*)(_gCipherCH0_Addr + (addr<<2) )))
178 
179 
180 //#define FPGAMode
181 //#define NSK2SelfTest
182 
183 //#define TestGenIn
184 //#define ReadSwitchInfoNSK2
185 
186 
187 #define StatusCheck(status)   do { if(status == FALSE) \
188                                      { \
189                                          printf("status error %s, %d\n",__FUNCTION__,__LINE__); \
190                                          return status;   \
191                                      } \
192                                  } while(0);
193 
194 #define NSK2HDI_CMCHANNEL_WITH_ALL_PARITY   ( NSK2HDI_CMCHANNEL_CLEAR_PARITY | NSK2HDI_CMCHANNEL_EVEN_PARITY |  NSK2HDI_CMCHANNEL_EVEN_CLEAR_PARITY | NSK2HDI_CMCHANNEL_ODD_PARITY | \
195                                               NSK2HDI_CMCHANNEL_ODD_CLEAR_PARITY | NSK2HDI_CMCHANNEL_ODD_EVEN_PARITY | NSK2HDI_CMCHANNEL_ODD_EVEN_CLEAR_PARITY )
196 
197 #define CMCHANNEL_NSK_All_CAPABILITY        (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG)
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 // Local variable
201 ////////////////////////////////////////////////////////////////////////////////
202 
203 static MS_BOOL _gReset = FALSE;
204 static MS_BOOL _gCheckBusyFlag = FALSE;
205 static MS_U32 dead_polling_cnt = 1;
206 
207 static cmchannel_group_capability_descriptor_t cm_capb =
208 {
209     .descriptor_tag = NSK2HDI_CMCHANNELGROUP_CAPABILITY_DESC_TAG,
210     .descriptor_length = sizeof(cmchannel_group_capability_descriptor_t) - 2,
211     .number_of_channels[3] = 40,
212     .switch_combination_bitmap[0] = 0xff,
213     .user_context[0] = 'M',
214     .user_context[1] = 'S',
215     .user_context[2] = 't',
216     .user_context[3] = 'a',
217     .user_context[4] = 'r',
218     .user_context[5] = 0x00,
219     .user_context[6] = 0x00,
220     .user_context[7] = 0x00,
221 };
222 
223 static cmchannel_group_algorithm_record_descriptor_t cm_algo[] =
224 {
225 
226 //-------------------------------LSA---------------------------------------//
227     {
228         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
229         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
230         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
231         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDI_CBC,
232         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
233         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
234         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
235     },
236 
237     {
238         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
239         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
240         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
241         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDI_RCBC,
242         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
243         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
244         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
245     },
246 
247     {
248         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
249         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
250         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
251         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDD_CBC,
252         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
253         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
254         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
255     },
256 
257     {
258         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
259         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
260         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
261         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDD_RCBC,
262         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
263         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
264         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
265     },
266 
267     {
268         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
269         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
270         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
271         .algorithm = NSK2HDI_SPROFILE_SYNAMEDIA_AES,
272         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
273         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG ,
274     },
275 
276     {
277         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
278         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
279         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
280         .algorithm = NSK2HDI_SPROFILE_AES_ECB_CLEARTAIL,
281         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
282         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
283     },
284 
285     {
286         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
287         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
288         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
289         .algorithm = NSK2HDI_SPROFILE_CIPLUS_AES,
290         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
291         //.capability = CMCHANNEL_NSK_All_CAPABILITY,
292         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
293     },
294 
295     {
296         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
297         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
298         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
299         .algorithm = NSK2HDI_SPROFILE_SCTE41_DES,
300         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
301         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG ,
302     },
303 
304     {
305         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
306         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
307         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
308         .algorithm = NSK2HDI_SPROFILE_SCTE52_DES,
309         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
310         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG ,
311         .capability = CMCHANNEL_NSK_All_CAPABILITY,
312     },
313 
314     {
315         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
316         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
317         .algorithm_type = NSK2HDI_CMCHANNEL_LSA_ALGORITHM_TYPE,
318         .algorithm = NSK2HDI_SPROFILE_TDES_ECB_CLEARTAIL,
319         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
320         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG ,
321     },
322 
323 
324 //-------------------------------LSA---------------------------------------//
325 //10
326 
327 
328 
329 //-------------------------------ESA---------------------------------------//
330     {
331         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
332         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
333         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
334         .algorithm = NSK2HDI_SPROFILE_DVB_CSA2,
335         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
336         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
337         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
338     },
339 
340 
341     {
342         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
343         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
344         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
345         .algorithm = NSK2HDI_SPROFILE_DVB_CSA_CONFORMANCE,
346         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
347         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
348         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
349     },
350 
351     {
352         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
353         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
354         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
355         .algorithm = NSK2HDI_SPROFILE_DVB_CSA3,
356         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
357         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
358         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
359     },
360 
361 
362     {
363         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
364         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
365         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
366         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDI_CBC,
367         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
368         //.capability = CMCHANNEL_NSK_All_CAPABILITY,
369         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
370     },
371 
372     {
373         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
374         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
375         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
376         .algorithm = NSK2HDI_SPROFILE_SYNAMEDIA_AES,
377         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
378         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
379         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
380     },
381 
382     {
383         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
384         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
385         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
386         .algorithm = NSK2HDI_SPROFILE_AES_ECB_CLEARTAIL,
387         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
388         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
389         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
390     },
391 
392 
393     {
394         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
395         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
396         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
397         .algorithm = NSK2HDI_SPROFILE_CIPLUS_AES,
398         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
399         //.capability = CMCHANNEL_NSK_All_CAPABILITY,
400         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
401     },
402 
403     {
404         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
405         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
406         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
407         .algorithm = NSK2HDI_SPROFILE_SCTE41_DES,
408         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
409         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
410         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
411     },
412 
413     {
414         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
415         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
416         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
417         .algorithm = NSK2HDI_SPROFILE_SCTE52_DES,
418         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
419         .capability = CMCHANNEL_NSK_All_CAPABILITY,
420     },
421 
422     {
423         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
424         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
425         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
426         .algorithm = NSK2HDI_SPROFILE_TDES_ECB_CLEARTAIL,
427         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
428         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
429         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
430     },
431 
432 
433     {
434         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
435         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
436         .algorithm_type = NSK2HDI_CMCHANNEL_ESA_ALGORITHM_TYPE,
437         .algorithm = NSK2HDI_SPROFILE_MULTI2_TS,
438         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
439         //.capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV2_CONFIGURE_CAPABILITY_FLAG),
440         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
441     },
442 //-------------------------------ESA---------------------------------------//
443 //24
444 
445 //-------------------------------LDA---------------------------------------//
446     {
447         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
448         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
449         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
450         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDI_CBC,
451         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
452         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
453         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
454     },
455 
456         {
457         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
458         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
459         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
460         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDI_RCBC,
461         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
462         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
463         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
464     },
465 
466     {
467         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
468         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
469         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
470         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDD_CBC,
471         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
472         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
473         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
474     },
475 
476     {
477         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
478         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
479         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
480         .algorithm = NSK2HDI_SPROFILE_CPCM_LSA_MDD_RCBC,
481         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
482         //.capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
483         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
484     },
485 
486     {
487         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
488         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
489         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
490         .algorithm = NSK2HDI_SPROFILE_SYNAMEDIA_AES,
491         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
492         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG ,
493     },
494 
495     {
496         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
497         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
498         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
499         .algorithm = NSK2HDI_SPROFILE_AES_ECB_CLEARTAIL,
500         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
501         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG,
502     },
503 
504     {
505         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
506         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
507         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
508         .algorithm = NSK2HDI_SPROFILE_CIPLUS_AES,
509         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
510         //.capability = CMCHANNEL_NSK_All_CAPABILITY,
511         .capability = (NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV1_CONFIGURE_CAPABILITY_FLAG | NSK2HDI_CMCHANNEL_IV_WRITEKEY_CAPABILITY_FLAG),
512     },
513 
514     {
515         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
516         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
517         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
518         .algorithm = NSK2HDI_SPROFILE_SCTE41_DES,
519         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
520         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG ,
521     },
522 
523     {
524         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
525         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
526         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
527         .algorithm = NSK2HDI_SPROFILE_SCTE52_DES,
528         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
529         .capability = CMCHANNEL_NSK_All_CAPABILITY,
530     },
531 
532     {
533         .descriptor_tag = NSK2HDI_CMCHANNELGROUP_ALGORITHM_RECORD_DESC_TAG,
534         .descriptor_length = sizeof(cmchannel_group_algorithm_record_descriptor_t) - 2,
535         .algorithm_type = NSK2HDI_CMCHANNEL_LDA_ALGORITHM_TYPE,
536         .algorithm = NSK2HDI_SPROFILE_TDES_ECB_CLEARTAIL,
537         .parity_combination_bitmap = NSK2HDI_CMCHANNEL_WITH_ALL_PARITY,
538         .capability = NSK2HDI_CMCHANNEL_NSK_CAPABILITY_FLAG ,
539     },
540 
541 //-------------------------------LDA---------------------------------------//
542 //34
543 
544 };
545 
546 static M2MChGr_Capa_Desc_t m2m_capa_desc = {
547     .descriptor_tag = NSK2HDI_M2MCHANNELGROUP_CAPABILITY_DESC_TAG,
548     .descriptor_length = sizeof(M2MChGr_Capa_Desc_t) - 2,
549     .number_of_channels[3] = 0x1,
550     .user_context[0] = 0x4e,
551     .user_context[1] = 0x44,
552     .user_context[2] = 0x53,
553     .user_context[3] = 0x5f,
554     .user_context[4] = 0x44,
555     .user_context[5] = 0x52,
556     .user_context[6] = 0x4d,
557     .user_context[7] = 0x00,
558 };
559 
560 static M2MChGr_AlgoRecord_Desc_t m2m_algo[] =
561 {
562     {
563         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
564         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
565         .algorithm = NSK2HDI_SPROFILE_M2M_DES_ECB_CLR_CLR,
566         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
567         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
568     },
569 
570     {
571         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
572         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
573         .algorithm = NSK2HDI_SPROFILE_M2M_DES_CBC_SCTE52_IV1,
574         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
575         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
576     },
577 
578     {
579         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
580         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
581         .algorithm = NSK2HDI_SPROFILE_M2M_DES_CBC_SCTE52_IV2,
582         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
583         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
584     },
585 
586     {
587         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
588         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
589         .algorithm = NSK2HDI_SPROFILE_M2M_DES_CBC_CLR_CLR,
590         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
591         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
592     },
593 
594     {
595         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
596         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
597         .algorithm = NSK2HDI_SPROFILE_M2M_TDES_ECB_CLR_CLR,
598         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
599         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
600     },
601 
602     {
603         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
604         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
605         .algorithm = NSK2HDI_SPROFILE_M2M_TDES_CBC_SCTE52_IV1,
606         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
607         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
608     },
609 
610     {
611         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
612         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
613         .algorithm = NSK2HDI_SPROFILE_M2M_TDES_CBC_SCTE52_IV2,
614         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
615         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
616     },
617 
618     {
619         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
620         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
621         .algorithm = NSK2HDI_SPROFILE_M2M_TDES_CBC_CLR_CLR,
622         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
623         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
624     },
625 
626     {
627         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
628         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
629         .algorithm = NSK2HDI_SPROFILE_M2M_AES_ECB_CLR_CLR,
630         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
631         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
632     },
633 
634     {
635         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
636         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
637         .algorithm = NSK2HDI_SPROFILE_M2M_AES_CBC_CTS_IV1,
638         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
639         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
640     },
641 
642     {
643         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
644         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
645         .algorithm = NSK2HDI_SPROFILE_M2M_AES_CBC_CTS_CLR,
646         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
647         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
648     },
649 
650     {
651         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
652         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
653         .algorithm = NSK2HDI_SPROFILE_M2M_AES_CBC_SCTE52_IV1,
654         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
655         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
656     },
657 
658     {
659         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
660         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
661         .algorithm = NSK2HDI_SPROFILE_M2M_AES_CBC_SCTE52_CLR,
662         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
663         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
664     },
665 
666     {
667         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
668         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
669         .algorithm = NSK2HDI_SPROFILE_M2M_AES_CBC_CLR_CLR,
670         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
671         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
672     },
673 
674     {
675         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
676         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
677         .algorithm = NSK2HDI_SPROFILE_M2M_RC4_64,
678         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
679         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
680     },
681 
682     {
683         .descriptor_tag = NSK2HDI_M2MCHANNEL_OPERATION_DESC_TAG,
684         .descriptor_length = sizeof(M2MChGr_AlgoRecord_Desc_t) - 2,
685         .algorithm = NSK2HDI_SPROFILE_M2M_AES_CTR,
686         .capability[0] = (MS_U8)(NSK2HDI_M2MCHANNELGROUP_NSK_CAPABILITY_FLAG>>24),
687         .capability[3] = NSK2HDI_M2MCHANNELGROUP_NON_NSK_CAPABILITY_FLAG,
688     },
689 };
690 
691 
692 static DMA_Capa_Desc_t dma_capa_desc = {
693     .descriptor_tag = NSK2HDI_DMA_CAPABILITY_DESC_TAG,
694     .descriptor_length = sizeof(DMA_Capa_Desc_t) - 2,
695     .maximum_data_size[0] = 0,
696     .maximum_data_size[1] = 0,
697     .maximum_data_size[2] = 0,
698     .maximum_data_size[3] = 1,
699     .minimum_data_size[0] = 0x01,
700     .data_size_granularity[0] = 0x4,
701     .data_alignment[0] = 0x4,
702     .capability[0] = NSK2HDI_DMA_CONTIGUOUS_MEMORY_TYPE,
703 };
704 ////////////////////////////////////////////////////////////////////////////////
705 // Global variable
706 ////////////////////////////////////////////////////////////////////////////////
707 
708 
709 
710 ////////////////////////////////////////////////////////////////////////////////
711 // Extern Function
712 ////////////////////////////////////////////////////////////////////////////////
713 
714 extern MS_U32 TSP32_IdrR(TSP32 *preg);
715 extern void TSP32_IdrW(TSP32 *preg, MS_U32 value);
716 extern int ChkForNskTest(volatile unsigned int *OtpMemBase, volatile unsigned int *RSABase,
717                          volatile unsigned int *NIBase, volatile unsigned int *CCh0Base);
718 extern int HW_CompareKTvalid(unsigned int pid_no, unsigned int scb, unsigned int compare, volatile unsigned int *KTBase);
719 extern void acpu_w_pidslotmap (unsigned char indx, unsigned char wmux, unsigned char wdata, volatile unsigned int *KTBase);
720 extern void acpu_r_keyslot (unsigned char key_indx, unsigned char key_field, volatile unsigned int *KTBase);
721 
722 
723 ////////////////////////////////////////////////////////////////////////////////
724 // Function Declaration
725 ////////////////////////////////////////////////////////////////////////////////
726 
727 ////////////////////////////////////////////////////////////////////////////////
728 // Local Function
729 ////////////////////////////////////////////////////////////////////////////////
730 
731 
HAL_NSK2_ReadReg(MS_U32 u32RegAddr)732 static MS_U32 HAL_NSK2_ReadReg(MS_U32 u32RegAddr)
733 {
734     if(_gReset == FALSE)
735         return 0;
736     MS_U32 u32reg;
737     MS_U32 u32Data;
738     u32reg = u32RegAddr + _gNSK2_Addr;
739     u32Data = (*(volatile MS_U32*)(u32reg));
740 
741     HALNSK2_DBG(NSK2_DBGLV_ARRAY, "read NSK2 %x = %x\n",u32RegAddr,u32Data);
742 
743     return u32Data;
744 }
745 
HAL_NSK2_WriteReg(MS_U32 u32RegAddr,MS_U32 u32Data)746 static void HAL_NSK2_WriteReg(MS_U32 u32RegAddr,MS_U32 u32Data)
747 {
748     if(_gReset == FALSE)
749         return ;
750     MS_U32 u32reg;
751     u32reg = u32RegAddr + _gNSK2_Addr;
752     (*(volatile MS_U32*)(u32reg)) = u32Data;
753 
754     HALNSK2_DBG(NSK2_DBGLV_ARRAY, "write NSK2 %x = %x\n",u32RegAddr,u32Data);
755 }
756 
HAL_NSK2_KIW_BusyPolling(void)757 static MS_U32 HAL_NSK2_KIW_BusyPolling(void)
758 {
759     MS_U32 xiu_rdata;
760     MS_U32 cnt = 0;
761 
762     xiu_rdata = NI_REG(REG_NI_STATUS);
763     while( (xiu_rdata & NI_KIW_BUSY) && (cnt < POLLING_CNT) )
764     {
765         xiu_rdata = NI_REG(REG_NI_STATUS);
766         HALNSK2_DBG(NSK2_DBGLV_INFO," read NI (STATUS = %x)\n",xiu_rdata);
767 
768         cnt ++;
769         MsOS_DelayTask(1);
770     }
771 
772     if(cnt >= POLLING_CNT)
773     {
774         HALNSK2_DBG(NSK2_DBGLV_ERR,"KIW_BusyPolling TimeOut\n");
775         return FALSE;
776     }
777     else
778     {
779         return TRUE;
780     }
781 }
782 
HAL_NSK2_CheckBusy(void)783 static MS_U32 HAL_NSK2_CheckBusy(void)
784 {
785     //printf("_gCheckBusyFlag = %x\n",_gCheckBusyFlag);
786 	if(_gCheckBusyFlag == TRUE)
787 	{
788         MS_U32 cnt = 0;
789 
790     #ifdef SlowClockTest
791         while( (cnt < BUSYCHECK_CNT) && (HAL_NSK2_ReadReg(REG_NSK2_ACPU_WARNING) & NSK2_ACPU_BUSY) )
792         {
793             cnt ++;
794             //MsOS_DelayTaskUs(1);
795         }
796     #else
797         while( (cnt < BUSYCHECK_CNT) && (HAL_NSK2_ReadReg(REG_NSK2_ACPU_WARNING) & NSK2_ACPU_BUSY) )
798         {
799             cnt ++;
800             MsOS_DelayTask(1);
801         }
802     #endif
803 
804         if(cnt == BUSYCHECK_CNT)
805         {
806             HALNSK2_DBG(NSK2_DBGLV_ERR, "NSK2 is still busy\n");
807             return FALSE;
808         }
809 	}
810 	else
811 	{
812 		HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NSK2 does't check busy\n");
813 	}
814     return TRUE;
815 }
816 
817 
HAL_NSK2_DeadPolling(void)818 static void HAL_NSK2_DeadPolling(void)
819 {
820 #if 0
821     MS_U32 polling_cnt = dead_polling_cnt;
822     while(polling_cnt){
823         MsOS_DelayTask(10);
824         HALNSK2_DBG(NSK2_DBGLV_ERR, "Error, Dead Polling\n");
825         polling_cnt --;
826     }
827 
828     //read ACPU Error....
829     HALNSK2_DBG(NSK2_DBGLV_INFO,"ACPU Error = %x\n", HAL_NSK2_ReadReg(REG_NSK2_ACPU_ERROR));
830 #endif
831 }
832 
833 
HAL_NSK2_OTP_Get(MS_U32 Addr,MS_U8 Msb,MS_U8 Lsb,MS_U32 * pValue)834 void HAL_NSK2_OTP_Get(MS_U32 Addr, MS_U8 Msb, MS_U8 Lsb, MS_U32 *pValue)
835 {
836     MS_U32 u32Data;
837 
838     u32Data = OTP_REG(Addr);
839 
840     //read back first....
841     //HALNSK2_DBG(NSK2_DBGLV_DEBUG, "read OTP %x = %x\n",Addr,u32Data);
842 
843     //write value next
844 
845     if( (Msb == 31) && (Lsb == 0) )
846     {
847         *pValue = u32Data;
848     }
849     else
850     {
851         *pValue = ((u32Data & BMASK(Msb:Lsb) ) >> Lsb);
852     }
853 
854 }
855 
856 ////////////////////////////////////////////////////////////////////////////////
857 // Global Function
858 ////////////////////////////////////////////////////////////////////////////////
859 
HAL_NSK2_Init(void)860 MS_U32 HAL_NSK2_Init(void)
861 {
862 #if 0
863     MS_U32 u32Data;
864     {
865         RSA_REG(REG_RSA_CLK_ENABLE) |= RSA_PM_NSKCLK_ENABLE;
866         MsOS_DelayTaskUs(1);
867 
868         u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
869         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA 0x1 = %x\n",u32Data);
870     }
871 #endif
872 
873 
874 //self test....
875 #ifdef NSK2SelfTest
876     NI_REG(REG_NI_NSK2_FREERUN) |= NI_NSK2_FREERUN_ENABLE;
877 
878 
879     MsOS_DelayTaskUs(1);
880     u32Data = NI_REG(REG_NI_NSK2_FREERUN);
881     HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI 0x1 = %x\n",u32Data);
882 #endif
883 
884 
885     //set OTP timing...
886     (*((volatile MS_U8*)(_gBasicAddr + (0x1A27CC<<1)))) = 0xd;
887     (*((volatile MS_U8*)(_gBasicAddr + (0x1A27CD<<1)))) = 0x17;
888     (*((volatile MS_U8*)(_gBasicAddr + (0x1A27CE<<1)))) = 0x01;
889     MsOS_DelayTask(1);
890     (*((volatile MS_U8*)(_gBasicAddr + (0x1A27CE<<1)))) = 0x00;
891 
892     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"finish NSK2.1 init\n");
893 
894     return TRUE;
895 }
896 
HAL_NSK2_Exit(void)897 MS_U32 HAL_NSK2_Exit(void)
898 {
899     NI_REG(REG_NI_NSK2_CTRL) &= (~NI_NSK2_RESET_DISABLE);
900     return TRUE;
901 }
902 
HAL_NSK2_UnlockOTPCtrl(void)903 MS_U32 HAL_NSK2_UnlockOTPCtrl(void)
904 {
905     HALNSK2_DBG(NSK2_DBGLV_INFO, "OTP control to unlock NDS secret key\n");
906     OTP_CTRL_REG(0x10) = 0x99885a5a;
907     OTP_CTRL_REG(0x11) = 0x00114433;
908     OTP_CTRL_REG(0x12) = 0x23456789;
909     OTP_CTRL_REG(0x13) = 0xabcdef01;
910 
911     return TRUE;
912 }
913 
HAL_NSK2_ColdReset(void)914 MS_U32 HAL_NSK2_ColdReset(void)
915 {
916     MS_U32 u32Data;
917 
918     HALNSK2_DBG(NSK2_DBGLV_DEBUG, "%s \n",__FUNCTION__);
919 
920     {
921         RSA_REG(REG_RSA_CLK_ENABLE) |= RSA_PM_NSKCLK_ENABLE;
922         MsOS_DelayTaskUs(1);
923 
924         u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
925         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA 0x1 = %x\n",u32Data);
926     }
927 
928     u32Data = NI_REG(REG_NI_NSK2_CTRL);
929 
930     NI_REG(REG_NI_NSK2_CTRL) = (u32Data & (~NI_NSK2_RESET_DISABLE));
931     MsOS_DelayTaskUs(1);
932 
933     u32Data &= (~NI_N2ROM_PD);
934     //u32Data &= (~NI_N2ROM_PD);
935 
936 //bit 0 set to 1...
937     NI_REG(REG_NI_NSK2_CTRL) = u32Data | NI_TS2NSK_ENABLE | NI_NSK2_CLK_ENABLE | NI_NSK2_RESET_DISABLE;
938 
939     //MsOS_DelayTaskUs(1);
940 
941     u32Data = NI_REG(REG_NI_NSK2_CTRL);
942     HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI 0x0 = %x\n",u32Data);
943 
944     _gReset = TRUE;
945     _gCheckBusyFlag = TRUE;
946 
947     HAL_NSK2_EnableInt();
948     return TRUE;
949 }
950 
951 
HAL_NSK2_EndSubtest(void)952 MS_U32 HAL_NSK2_EndSubtest(void)
953 {
954     MS_U32 u32Data;
955 
956     HALNSK2_DBG(NSK2_DBGLV_DEBUG, "%s \n",__FUNCTION__);
957 
958     HAL_NSK2_DisableInt();
959 
960     u32Data = NI_REG(REG_NI_NSK2_CTRL);
961     HALNSK2_DBG(NSK2_DBGLV_DEBUG, "REG_NI_NSK2_CTRL = %x \n",u32Data);
962 
963     if(u32Data & NI_NSK2_RESET_DISABLE)
964     {
965 
966         NI_REG(REG_NI_NSK2_CTRL) = (u32Data & (~NI_NSK2_RESET_DISABLE));
967         //NI_REG(REG_NI_NSK2_CTRL) = 0;
968         //printf("wait here\n");
969         //while(1);
970         MsOS_DelayTaskUs(1);
971     }
972 
973     _gReset = FALSE;
974     _gCheckBusyFlag = FALSE;
975     return TRUE;
976 }
977 
978 
HAL_NSK2_SetBase(MS_U32 u32Base)979 void HAL_NSK2_SetBase(MS_U32 u32Base)
980 {
981     HALNSK2_DBG(NSK2_DBGLV_INFO, "u32Base = %x\n",u32Base);
982 
983     _gBasicAddr = u32Base;
984 
985     _gOTP_Addr  = _gBasicAddr + REG_OTP_BASE;
986     _gRSA_Addr  = _gBasicAddr + REG_RSA_BASE;
987     _gOTP_CTRL_Addr  = _gBasicAddr + REG_OTP_CTRL_BASE;
988     _gNSK2_Addr = _gBasicAddr + REG_NSK2_BASE;
989 
990     _gNI_Addr   = _gBasicAddr + REG_NI_BASE;
991 
992     _gKeyTable_Addr  = _gBasicAddr + REG_KEY_TABLE_BASE;
993     _gNDSJTagPwd_Addr= _gBasicAddr + REG_JTAG_PWD_BASE;
994     _gCipherCH0_Addr = _gBasicAddr + REG_CIPHER_CH0_BASE;
995     _gCryptoDMA_Addr = _gBasicAddr + REG_CRYPTO_DMA_BASE;
996     HALNSK2_DBG(NSK2_DBGLV_INFO, "%s _gNSK2_Addr : %x\n", __FUNCTION__, _gNSK2_Addr);
997 
998     //OTP_CTRL_REG(0x3A) = 0x0;
999 }
1000 
1001 
1002 
1003 
1004 //compare the contect of a buffer in the NSK2's memory map to an expected value.
HAL_NSK2_CompareMem(MS_U32 reserved,MS_U32 StartAddr,MS_U32 CompareLens,MS_U32 CompareSim,MS_U32 ExpectResult,void * pGolden)1005 MS_U32 HAL_NSK2_CompareMem(MS_U32 reserved, MS_U32 StartAddr, MS_U32 CompareLens,
1006                            MS_U32 CompareSim, MS_U32 ExpectResult, void *pGolden)
1007 {
1008     MS_U32 u32ReadData, u32GoldenData;
1009     MS_U32 u32Addr = StartAddr;
1010     MS_U32 *pGoldenData = (MS_U32 *)pGolden;
1011     MS_U32 u32CompLens = CompareLens;
1012     MS_U32 status = TRUE;
1013     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"%s \n",__FUNCTION__);
1014     //check lengths
1015     if(u32CompLens == 0)
1016     {
1017         HALNSK2_DBG(NSK2_DBGLV_DEBUG,"%s CompareLens = %x\n",__FUNCTION__,u32CompLens);
1018     }
1019 
1020     do
1021     {
1022         HAL_NSK2_CheckBusy();
1023 
1024         u32ReadData = HAL_NSK2_ReadReg(u32Addr);
1025         u32GoldenData =  *pGoldenData;
1026 
1027         if(u32ReadData != u32GoldenData)
1028         {
1029             HALNSK2_DBG(NSK2_DBGLV_ERR,"%s, Addr = %x, u32ReadData = %x, u32GoldenData = %x\n",__FUNCTION__,u32Addr, u32ReadData, u32GoldenData);
1030             HAL_NSK2_DeadPolling();
1031             //return FALSE;
1032             status = FALSE;
1033         }
1034         else
1035         {
1036             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"Data Correct, Addr = %x, u32GoldenData = %x\n",u32Addr, u32GoldenData);
1037         }
1038 
1039         pGoldenData ++;
1040         u32Addr += 4;
1041         u32CompLens --; //32 bit bus
1042 
1043     } while(u32CompLens > 0);
1044 
1045     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"%s, successful = %x\n",__FUNCTION__,StartAddr);
1046     return status;
1047 }
1048 
HAL_NSK2_Compare(MS_U32 StartAddr,MS_U32 CompareLens,MS_U32 CompareSim,MS_U32 Mask,MS_U32 ExpectResult)1049 MS_U32 HAL_NSK2_Compare(MS_U32 StartAddr, MS_U32 CompareLens, MS_U32 CompareSim,
1050                         MS_U32 Mask, MS_U32 ExpectResult)
1051 {
1052 
1053     MS_U32 u32ReadData, u32GoldenData = ExpectResult;
1054     MS_U32 u32Addr = StartAddr;
1055 
1056     StatusCheck(HAL_NSK2_CheckBusy());
1057 
1058     u32ReadData = HAL_NSK2_ReadReg(u32Addr);
1059     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"Mask = %x\n",Mask);
1060     u32ReadData &= Mask;
1061 
1062     if(u32ReadData != u32GoldenData)
1063     {
1064         HALNSK2_DBG(NSK2_DBGLV_ERR,"%s fail, Addr = %x, u32ReadData = %x, u32GoldenData = %x\n",__FUNCTION__,u32Addr, u32ReadData, u32GoldenData);
1065         HAL_NSK2_DeadPolling();
1066         return FALSE;
1067     }
1068     else
1069     {
1070         HALNSK2_DBG(NSK2_DBGLV_DEBUG,"compare data correct, Addr = %x, u32GoldenData = %x\n",u32Addr,u32GoldenData);
1071     }
1072 
1073     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"%s, successful\n",__FUNCTION__);
1074     return TRUE;
1075 }
1076 
HAL_NSK2_WriteMem(MS_U32 reserved,MS_U32 StartAddr,MS_U32 WriteLens,void * pWriteData)1077 MS_U32 HAL_NSK2_WriteMem(MS_U32 reserved, MS_U32 StartAddr, MS_U32 WriteLens,
1078                          void *pWriteData)
1079 {
1080 
1081     MS_U32 *pWriteD = (MS_U32 *)pWriteData;
1082     MS_U32 u32Data;
1083     while(WriteLens > 0)
1084     {
1085         StatusCheck(HAL_NSK2_CheckBusy());
1086 
1087         u32Data = *pWriteD ;
1088 
1089         HAL_NSK2_WriteReg(StartAddr,u32Data);
1090         HALNSK2_DBG(NSK2_DBGLV_ARRAY,"WriteMem (Addr,Data) = (%x,%x)\n",StartAddr,u32Data);
1091 
1092         WriteLens --;
1093         StartAddr += 4;
1094         pWriteD ++;
1095     }
1096 
1097     StatusCheck(HAL_NSK2_CheckBusy());
1098     return TRUE;
1099 }
1100 
HAL_NSK2_WriteSFR(MS_U32 StartAddr,MS_U32 Data)1101 MS_U32 HAL_NSK2_WriteSFR(MS_U32 StartAddr, MS_U32 Data)
1102 {
1103     StatusCheck(HAL_NSK2_CheckBusy());
1104     HALNSK2_DBG(NSK2_DBGLV_ARRAY,"WriteSFR (Addr,Data) = (%x,%x)\n",StartAddr,Data);
1105     HAL_NSK2_WriteReg(StartAddr,Data);
1106     StatusCheck(HAL_NSK2_CheckBusy());
1107     return TRUE;
1108 }
1109 
HAL_NSK2_NSKBasicInitializationComplete(void)1110 MS_U32 HAL_NSK2_NSKBasicInitializationComplete(void)
1111 {
1112     //$display($time,"NS  NSKBasicInitializationComplete ");
1113     //xiu_w_ni(16'h6,4'b0001,32'h0000000F);
1114 
1115     MS_U32 data;
1116     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_NSKBasicInitializationComplete\n");
1117     StatusCheck(HAL_NSK2_CheckBusy());
1118 
1119     data = NI_REG(REG_NI_COMMAND);
1120     //NI_REG(REG_NI_COMMAND) = (data | NI_NSKBIComplete | NI_COMMAND_START);
1121     NI_REG(REG_NI_COMMAND) = (NI_NSKBIComplete | NI_COMMAND_START);
1122 
1123     MsOS_DelayTaskUs(1);
1124     //NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_NopNop);
1125     NI_REG(REG_NI_NSK2_CLK_CSA) = NSK2_EN_CSA_VAR;
1126 
1127 
1128     return TRUE;
1129 }
1130 
HAL_NSK2_SCBTransToHW(MS_U8 ForceSCB)1131 MS_U8 HAL_NSK2_SCBTransToHW(MS_U8 ForceSCB)
1132 {
1133 	MS_U8 NewForceSCB = 0;
1134 
1135 	NewForceSCB = ForceSCB;
1136 	if(ForceSCB == 0)
1137 	{
1138 		NewForceSCB = 1;
1139 	}
1140 	else if(ForceSCB == 1)
1141 	{
1142 		NewForceSCB = 0;
1143 	}
1144 
1145 	return NewForceSCB;
1146 }
1147 
1148 
HAL_NSK2_WriteESA(MS_U8 ESASelect,MS_U8 ESASubSelect,MS_U8 pid_no)1149 MS_U32 HAL_NSK2_WriteESA(MS_U8 ESASelect, MS_U8 ESASubSelect, MS_U8 pid_no)
1150 {
1151 #if 0
1152     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_WriteESA pid_no = %x, ESASelect = %x, ESASubSelect = %x\n",pid_no, ESASelect, ESASubSelect);
1153     //scb = 2'b00; pid_no = 1; ESAselect = 4'h0; ESAsubselect =4'h0;
1154     //wdata = {10'b0,ESAselect[3:0],ESAsubselect[2:0],3'b0,pid_no[12:0]};
1155     //xiu_w_ni(16'h7,4'b1111,wdata[31:0]);
1156     //xiu_w_ni(16'h6,4'b1111,32'h00000003);
1157     //xiu_rdata = 32'h8; while(xiu_rdata[3]===1'b1)begin xiu_r_ni(16'hc); end
1158     StatusCheck(HAL_NSK2_CheckBusy());
1159 
1160     MS_U32 data;
1161 
1162     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_WriteESA pid_no = %x\n",pid_no);
1163     data = (((MS_U32)ESASelect<<NI_WriteESA_ESASel_Shift) & NI_WriteESA_ESASel_MASK) +
1164            (((MS_U32)ESASubSelect<<NI_WriteESA_ESASubSel_Shift) & NI_WriteESA_ESASubSel_MASK)  +
1165            ((MS_U32)pid_no & NI_WriteESA_PidNo);
1166 
1167 
1168     HALNSK2_DBG(NSK2_DBGLV_INFO," write NI (REG_NI_PARAMETERS = %x)\n",data);
1169     NI_REG(REG_NI_PARAMETERS) = data;
1170     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteESA);
1171 
1172     StatusCheck(HAL_NSK2_KIW_BusyPolling());
1173 #endif
1174     return TRUE;
1175 }
1176 
HAL_NSK2_WriteTransportKey(MS_U8 SCB,MS_U8 ForceSCB,void * pLabel,MS_U8 pid_no)1177 MS_U32 HAL_NSK2_WriteTransportKey(MS_U8 SCB, MS_U8 ForceSCB, void *pLabel, MS_U8 pid_no)
1178 {
1179 #if 0
1180     //$display($time,"NS  WriteTransportKey 0134 00 01 IV__0");
1181     //wait_nsk_busy;
1182     //xiu_r_ni(16'hd);
1183     //if((xiu_rdata & 7)==0) begin
1184     //    scb = 2'b00; fscb = 2'b00; pid_no = 1;
1185     //    wdata = {12'b0,fscb[1:0],scb[1:0],3'b0,pid_no[12:0]};
1186     //    xiu_w_ni(16'h7,4'b1111,wdata[31:0]);
1187     //    xiu_w_ni(16'hb,4'b1111,32'h00000000);
1188     //    xiu_w_ni(16'ha,4'b1111,32'h00000000);
1189     //    xiu_w_ni(16'h9,4'b1111,32'h00000000);
1190     //    xiu_w_ni(16'h8,4'b1111,32'h00000000);
1191     //    xiu_w_ni(16'h6,4'b1111,32'h00000005);
1192     //    xiu_rdata = 32'h8; while(xiu_rdata[3]===1'b1)begin xiu_r_ni(16'hc); end
1193     //end
1194     //else $display("WriteTransportKey abnormal ignored: KteDest not zero");
1195 #define N  0xfffffff0
1196 #define N1 0xfffffff1
1197 #define N2 0xfffffff2
1198 
1199     MS_U32 data = 0;
1200     MS_U32 data2 = 0;
1201 	MS_U32 pIV[4] = {0,0,0,0};
1202 
1203 	MS_U32* pIV_copy = (MS_U32*)pLabel;
1204 
1205 	if((pLabel != NULL) && (pLabel != (MS_U32*)N) && (pLabel != (MS_U32*)N1) && (pLabel != (MS_U32*)N2))
1206 	{
1207 		memcpy(pIV,pIV_copy,16);
1208 	}
1209 
1210     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_WriteTransportKey pid_no = %x, SCB = %x, ForceSCB = %x\n",pid_no, SCB, ForceSCB);
1211     StatusCheck(HAL_NSK2_CheckBusy());
1212 
1213     data = NI_REG(REG_NI_KTE_STATUS);
1214     if( ( data & NI_KTE_DEST_MASK ) == 0  )
1215     {
1216 
1217 		ForceSCB = HAL_NSK2_SCBTransToHW(ForceSCB);
1218 
1219         HALNSK2_DBG(NSK2_DBGLV_INFO,"pid_no: %x\n", pid_no);
1220         data2 = pid_no + ( ( (MS_U32)SCB<<NI_WriteTKey_SCB_Shift) & NI_WriteTKey_SCB_MASK)
1221                 + ( ( (MS_U32)ForceSCB<<NI_WriteTKey_FSCB_Shift) & NI_WriteTKey_FSCB_MASK) ;
1222 
1223 
1224         NI_REG(REG_NI_PARAMETERS) = data2;
1225         NI_REG(REG_NI_IV_127_96) = pIV[0];
1226         NI_REG(REG_NI_IV_95_64) = pIV[1];
1227         NI_REG(REG_NI_IV_63_31) = pIV[2];
1228         NI_REG(REG_NI_IV_31_00) = pIV[3];
1229         NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteTransportKey);
1230 
1231         HALNSK2_DBG(NSK2_DBGLV_INFO,"NI 7 : %x, NI 6 = %x\n", NI_REG(REG_NI_PARAMETERS) , NI_REG(REG_NI_COMMAND));
1232 
1233         StatusCheck(HAL_NSK2_KIW_BusyPolling());
1234     }
1235     else
1236     {
1237         HALNSK2_DBG(NSK2_DBGLV_ERR,"WriteTransportKey abnormal ignored: KteDest not zero\n");
1238         return FALSE;
1239     }
1240 
1241     HAL_NSK2_ReadSwitchFromNSK2();
1242 #endif
1243     return TRUE;
1244 }
1245 
HAL_NSK2_wait_kte_valid(void)1246 MS_U32 HAL_NSK2_wait_kte_valid(void)
1247 {
1248     MS_U32 xiu_rdata = HAL_NSK2_ReadReg(REG_NSK2_KTE_VALID);
1249     MS_U32 cnt = 0;
1250     while( ( (xiu_rdata & NSK2_KTE_VALID_TRUE) == 0 ) && (cnt < POLLING_CNT) )
1251     {
1252         xiu_rdata = HAL_NSK2_ReadReg(REG_NSK2_KTE_VALID);
1253         HALNSK2_DBG(NSK2_DBGLV_INFO,"xiu_rdata = %x\n",xiu_rdata);
1254         cnt ++;
1255         MsOS_DelayTask(1);
1256     }
1257 
1258     if(cnt == POLLING_CNT)
1259     {
1260         HALNSK2_DBG(NSK2_DBGLV_ERR,"HAL_NSK2_wait_kte_valid time out\n");
1261         return FALSE;
1262     }
1263 
1264     return TRUE;
1265 }
1266 
HAL_NSK2_CompareKTE(MS_U32 reserved_1,MS_U32 reserved_2,void * pLabel)1267 MS_U32 HAL_NSK2_CompareKTE(MS_U32 reserved_1, MS_U32 reserved_2, void *pLabel)
1268 {
1269 
1270 #ifdef FPGAMode
1271     MS_U32 data;
1272     MS_U32 *pKTEGolden = (MS_U32 *)pLabel;
1273     MS_U32 KTE_Index = REG_KT_KEYS_START_FPGA;
1274     MS_U32 status = TRUE;
1275 
1276     StatusCheck(HAL_NSK2_CheckBusy());
1277     //StatusCheck(HAL_NSK2_wait_kte_valid());
1278     data = NI_REG(REG_NI_NSK2_KTE_VALID_FPGA);
1279     HALNSK2_DBG(NSK2_DBGLV_INFO,"NSK2_KTE_VALID_FPGA = %x\n",data);
1280 
1281     for(KTE_Index = REG_KT_KEYS_END_FPGA; KTE_Index<=REG_KT_KEYS_START_FPGA; KTE_Index--)
1282     {
1283         data = NI_REG(KTE_Index);
1284 
1285         if(data != *pKTEGolden)
1286         {
1287             HALNSK2_DBG(NSK2_DBGLV_ERR," %x, (%x , %x)\n",KTE_Index,data,*pKTEGolden);
1288             status = FALSE;
1289         }
1290 
1291         pKTEGolden ++;
1292     }
1293 
1294     return status;
1295 
1296 #else
1297 
1298 #endif
1299 
1300     return TRUE;
1301 }
1302 
HAL_NSK2_CompareOut(MS_U32 reserved_1,MS_U32 reserved_2,MS_U32 HighDWord,MS_U32 LowDWord)1303 MS_U32 HAL_NSK2_CompareOut(MS_U32 reserved_1, MS_U32 reserved_2, MS_U32 HighDWord, MS_U32 LowDWord)
1304 {
1305 
1306    //wait_nsk_busy;
1307     //$display($time,"NS  CompareOut 1 1 0000001555400000");
1308     //xiu_r_ni(16'd24); genout = xiu_rdata; genout = genout << 32;
1309     //xiu_r_ni(16'd23); genout = genout | {6'b0,xiu_rdata[31:0]};
1310     //$display("CompareOut read PS",genout[37:0]);
1311     //    if(genout!==38'h0000001555400000)begin
1312     //$display("CompareOut Expected=38'h0000001555400000 Read=38'hPS",genout[37:0]);
1313     //    nsk2_GeneralOut_test_fail = 1;
1314     //    #2000;
1315     //$finish;
1316     //end
1317 
1318 
1319     MS_U32 high_data,low_data;
1320     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_CompareOut HighDWord = %x, LowDWord = %x\n",HighDWord, LowDWord);
1321     StatusCheck(HAL_NSK2_CheckBusy());
1322     low_data = NI_REG(REG_NI_COMPARE_GENOUT_L);
1323     high_data = (NI_REG(REG_NI_COMPARE_GENOUT_H)&NI_GENOUT_H_MASK);
1324     HALNSK2_DBG(NSK2_DBGLV_INFO,"NI 24 = %x , 23 = %x\n",high_data,low_data);
1325 
1326     if( (HighDWord != high_data) || (LowDWord!= low_data) )
1327     {
1328         HAL_NSK2_DeadPolling();
1329         return FALSE;
1330     }
1331     else
1332     {
1333         HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_CompareOut successfully\n");
1334     }
1335 
1336     return TRUE;
1337 }
1338 
HAL_NSK2_SetRNG(MS_U32 reserved_1,MS_U32 RNG_Value)1339 MS_U32 HAL_NSK2_SetRNG(MS_U32 reserved_1,MS_U32 RNG_Value)
1340 {
1341 
1342     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_SetRNG = %x\n",RNG_Value);
1343     NI_REG(REG_NI_SW_SET_RNG) = (RNG_Value&NI_SW_RNG_MASK);
1344     return TRUE;
1345 }
1346 
HAL_NSK2_WriteM2MKey(void * pIV,MS_U8 SubAlgo)1347 MS_U32 HAL_NSK2_WriteM2MKey(void *pIV, MS_U8 SubAlgo)
1348 {
1349 #if 0
1350     //wait_nsk_busy;
1351     //WriteM2MKey IV__1  0
1352     //$display($time,"NS  WriteM2MKey IV__1  0");
1353     //wdata = {12'b0,4'h0,16'b0};
1354     //xiu_w_ni(16'h7,4'b1111,wdata[31:0]);
1355     //xiu_w_ni(16'hb,4'b1111,32'h00010203);
1356     //xiu_w_ni(16'ha,4'b1111,32'h04050607);
1357     //xiu_w_ni(16'h9,4'b1111,32'h08090a0b);
1358     //xiu_w_ni(16'h8,4'b1111,32'h0c0d0e0f);
1359     //xiu_w_ni(16'h6,4'b1111,32'h00000007);
1360     //xiu_rdata = 32'h8; while(xiu_rdata[3]===1'b1)begin xiu_r_ni(16'hc); end
1361 
1362     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_WriteM2MKey, SubAlgo = %x \n", SubAlgo);
1363     HAL_NSK2_CheckBusy();
1364 
1365     MS_U32 wdata = 0;
1366     MS_U32 pWIV[4];
1367 
1368     memset(pWIV, 0x0, 4*4);
1369     if(pIV != NULL)
1370     {
1371         memcpy(pWIV,pIV,4*4);
1372     }
1373 
1374 
1375     wdata = ( ((MS_U32)SubAlgo << NI_WriteM2MKey_Shift) & NI_WriteM2MKey_MASK); //write sub algorithm....[19:16]
1376     NI_REG(REG_NI_PARAMETERS) = wdata;
1377 
1378     NI_REG(REG_NI_IV_127_96) = pWIV[0];
1379     NI_REG(REG_NI_IV_95_64)  = pWIV[1];
1380     NI_REG(REG_NI_IV_63_31)  = pWIV[2];
1381     NI_REG(REG_NI_IV_31_00)  = pWIV[3];
1382 
1383     HALNSK2_DBG(NSK2_DBGLV_INFO,"pwIV = (%x, %x, %x, %x)\n",pWIV[0],pWIV[1],pWIV[2],pWIV[3]);
1384     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteM2MKey); //write M2M key and start...
1385 #endif
1386     return TRUE;
1387 }
1388 
HAL_NSK2_WriteSCPUKey(void)1389 MS_U32 HAL_NSK2_WriteSCPUKey(void)
1390 {
1391     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n", __FUNCTION__);
1392     StatusCheck(HAL_NSK2_CheckBusy());
1393     NI_REG(REG_NI_COMMAND) = (NI_WriteSCPUKey | NI_COMMAND_START);
1394     MsOS_DelayTaskUs(1);
1395 
1396     return HAL_NSK2_ReadKTEResp();
1397 }
1398 
1399 static MS_U32 KeyNum = 0;
HAL_NSK2_WriteReservedKey(void)1400 MS_U32 HAL_NSK2_WriteReservedKey(void)
1401 {
1402     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n", __FUNCTION__);
1403     StatusCheck(HAL_NSK2_CheckBusy());
1404     NI_REG(REG_NI_COMMAND) = (NI_WriteReservedKey | NI_COMMAND_START);
1405     MsOS_DelayTaskUs(1);
1406 
1407     HALNSK2_DBG(NSK2_DBGLV_INFO,"write key = %x, %x\n", NI_REG(REG_NI_COMPARE_GENOUT_L), NI_REG(REG_NI_COMPARE_GENOUT_H));
1408     KeyNum = NI_REG(REG_NI_COMPARE_GENOUT_L)>>17;
1409 
1410     return HAL_NSK2_ReadKTEResp();
1411 }
1412 
HAL_NSK2_GetReserveKeyNum(void)1413 MS_U32 HAL_NSK2_GetReserveKeyNum(void)
1414 {
1415     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s KeyNum = %x\n", __FUNCTION__,KeyNum);
1416     return KeyNum;
1417 }
1418 
HAL_NSK2_DriveKteAck(void)1419 MS_U32 HAL_NSK2_DriveKteAck(void)
1420 {
1421     //DriveKteAck
1422     //$display($time,"NS  DriveKteAck");
1423     //xiu_w_ni(16'h6,4'b1111,32'h0000000B);
1424 
1425     StatusCheck(HAL_NSK2_CheckBusy());
1426     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n", __FUNCTION__);
1427 
1428     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_DriveAck);
1429     MsOS_DelayTaskUs(1);
1430     StatusCheck(HAL_NSK2_CheckBusy());
1431     return TRUE;
1432 }
1433 
HAL_NSK2_SetJTagPswd(void)1434 MS_U32 HAL_NSK2_SetJTagPswd(void)
1435 {
1436 
1437 #if 0
1438 #define JTagPwd0Addr 0x64
1439 #define InputPwd0Addr 0x4
1440     MS_U32 JTagPwd0[4];
1441 
1442     NDSJTagPwd_REG(3) = 0x0;
1443     NDSJTagPwd_REG(2) = 0x0;
1444     NDSJTagPwd_REG(1) = 0x0;
1445     NDSJTagPwd_REG(0) = 0x1;
1446 
1447     NDSJTagPwd_REG(0x14) = 0x1;
1448 
1449     JTagPwd0[0] = KeyTable_REG(JTagPwd0Addr);
1450     JTagPwd0[1] = KeyTable_REG((JTagPwd0Addr+1));
1451     JTagPwd0[2] = KeyTable_REG((JTagPwd0Addr+2));
1452     JTagPwd0[3] = KeyTable_REG((JTagPwd0Addr+3));
1453 
1454     HALNSK2_DBG(0,"JTagPwd0 = %x, %x, %x, %x\n",JTagPwd0[0],JTagPwd0[1],JTagPwd0[2],JTagPwd0[3]);
1455 
1456     NDSJTagPwd_REG((InputPwd0Addr+7)) = (MS_U16) ((JTagPwd0[3]>>16)&0xffff);
1457     NDSJTagPwd_REG((InputPwd0Addr+6)) = (MS_U16) (JTagPwd0[3]&0xffff);
1458     NDSJTagPwd_REG((InputPwd0Addr+5)) = (MS_U16) ((JTagPwd0[2]>>16)&0xffff);
1459     NDSJTagPwd_REG((InputPwd0Addr+4)) = (MS_U16) (JTagPwd0[2]&0xffff);
1460     NDSJTagPwd_REG((InputPwd0Addr+3)) = (MS_U16) ((JTagPwd0[1]>>16)&0xffff);
1461     NDSJTagPwd_REG((InputPwd0Addr+2)) = (MS_U16) (JTagPwd0[1]&0xffff);
1462     NDSJTagPwd_REG((InputPwd0Addr+1)) = (MS_U16) ((JTagPwd0[0]>>16)&0xffff);
1463     NDSJTagPwd_REG((InputPwd0Addr+0)) = (MS_U16) (JTagPwd0[0]&0xffff);
1464 
1465     NDSJTagPwd_REG(0x14) = 0x7;
1466 #else
1467     MS_U32 jtag_status;
1468     jtag_status = KeyTable_REG(88);
1469     //88 bit 0 and 1 = 1
1470 
1471     HALNSK2_DBG(NSK2_DBGLV_INFO,"KeyTable 88 jtag_status = %x\n",jtag_status);
1472 #endif
1473 
1474     return TRUE;
1475 }
1476 
1477 //MS_BOOL HAL_CA_OTP_GetCfg(MS_U32 u32Idx, MS_U32 *pu32Value);
1478 
HAL_NSK2_CheckPubOTPConfig(void * pCheck)1479 MS_BOOL HAL_NSK2_CheckPubOTPConfig(void *pCheck)
1480 {
1481 #if 0
1482 OTP bits	OTP name	         function
1483 [127:64]	V_PubOtpUniqueID	 Unique ID, 0x3da8~0x3daf
1484 [63:48]	    V_PubOtpGP	         General Purpose Field (bit-by-bit lock),3b08~3b09
1485 [47:40]	    V_PubOtpMinConfVer	 Minimum Configuration Version,3b14
1486 [39:36]	    V_PubOtpRSAIndex	 RSA Index, 3b10
1487 [35:32]	    V_PubOtpBID	         BlackBox ID, 0x3b0c
1488 [31:16]	    V_PubOtpVID	         Version ID, 0x3b04
1489 [15:0]	    V_PubOtpOID	         Owner ID, 0x3b00
1490 #endif
1491 
1492     MS_U32 *pDataArray;
1493     MS_U32 OTP_PubOtpOID,OTP_PubOtpVID,OTP_PubOtpBID,OTP_PubOtpRsaIndex,OTP_PubOtpMinConfVer ;
1494     MS_U32 OTP_PubOtpGP,OTP_PubOtpUniqueID[2];
1495 
1496     MS_U32 CHK_PubOtpOID,CHK_PubOtpVID,CHK_PubOtpBID,CHK_PubOtpRsaIndex,CHK_PubOtpMinConfVer ;
1497     MS_U32 CHK_PubOtpGP,CHK_PubOtpUniqueID[2];
1498 
1499     MS_BOOL status = TRUE;
1500     if(pCheck == NULL)
1501     {
1502         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, pCheck = NULL\n",__LINE__,__FUNCTION__);
1503         return FALSE;
1504     }
1505     pDataArray = (MS_U32 *)pCheck;
1506 
1507     HAL_NSK2_OTP_Get(0x3da8,31,0, &OTP_PubOtpUniqueID[0]);
1508     HAL_NSK2_OTP_Get(0x3dac,31,0, &OTP_PubOtpUniqueID[1]);
1509 
1510     CHK_PubOtpUniqueID[1] = pDataArray[0];
1511     CHK_PubOtpUniqueID[0] = pDataArray[1];
1512 
1513     HALNSK2_DBG(NSK2_DBGLV_INFO,"uniqueID = (%x%x, %x%x)\n",CHK_PubOtpUniqueID[1],CHK_PubOtpUniqueID[0],OTP_PubOtpUniqueID[1],OTP_PubOtpUniqueID[0]);
1514 
1515     if( (CHK_PubOtpUniqueID[1] != OTP_PubOtpUniqueID[1]) || (CHK_PubOtpUniqueID[0] != OTP_PubOtpUniqueID[0]))
1516     {
1517         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, UniqueID WRONG\n",__LINE__,__FUNCTION__);
1518         status = FALSE;
1519     }
1520 
1521     HAL_NSK2_OTP_Get(0x3b08,15,0, &OTP_PubOtpGP);
1522     CHK_PubOtpGP = (pDataArray[2]>>16)&0xffff;
1523 
1524     if(OTP_PubOtpGP != CHK_PubOtpGP)
1525     {
1526         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, PubOtpGP WRONG\n",__LINE__,__FUNCTION__);
1527         HALNSK2_DBG(NSK2_DBGLV_ERR,"OtpGP = (%x, %x)\n",CHK_PubOtpGP,OTP_PubOtpGP);
1528         status = FALSE;
1529     }
1530 
1531     HAL_NSK2_OTP_Get(0x3b14,7,0, &OTP_PubOtpMinConfVer);
1532     CHK_PubOtpMinConfVer = (pDataArray[2]>>8)&0xff;
1533 
1534     if(OTP_PubOtpMinConfVer != CHK_PubOtpMinConfVer)
1535     {
1536         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, PubOtpMinConfVer WRONG\n",__LINE__,__FUNCTION__);
1537         HALNSK2_DBG(NSK2_DBGLV_ERR,"OtpMinConfVer = (%x, %x)\n",CHK_PubOtpMinConfVer,OTP_PubOtpMinConfVer);
1538         status = FALSE;
1539     }
1540 
1541     HAL_NSK2_OTP_Get(0x3b10,3,0, &OTP_PubOtpRsaIndex);
1542     CHK_PubOtpRsaIndex = (pDataArray[2]>>4)&0xf;
1543 
1544     if(OTP_PubOtpRsaIndex != CHK_PubOtpRsaIndex)
1545     {
1546         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, PubOtpRsaIndex WRONG\n",__LINE__,__FUNCTION__);
1547         HALNSK2_DBG(NSK2_DBGLV_ERR,"OtpRsaIndex = (%x, %x)\n",CHK_PubOtpRsaIndex,OTP_PubOtpRsaIndex);
1548         status = FALSE;
1549     }
1550 
1551     HAL_NSK2_OTP_Get(0x3b0C,3,0, &OTP_PubOtpBID);
1552     CHK_PubOtpBID = (pDataArray[2])&0xf;
1553 
1554     if(OTP_PubOtpBID != CHK_PubOtpBID)
1555     {
1556         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, PubOtpBID WRONG\n",__LINE__,__FUNCTION__);
1557         HALNSK2_DBG(NSK2_DBGLV_ERR,"OtpBID = (%x, %x)\n",CHK_PubOtpBID,OTP_PubOtpBID);
1558         status = FALSE;
1559     }
1560 
1561     HAL_NSK2_OTP_Get(0x3b04,15,0, &OTP_PubOtpVID);
1562     CHK_PubOtpVID = (pDataArray[3]>>16)&0xffff;
1563 
1564     if(OTP_PubOtpVID != CHK_PubOtpVID)
1565     {
1566         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, PubOtpVID WRONG\n",__LINE__,__FUNCTION__);
1567         HALNSK2_DBG(NSK2_DBGLV_ERR,"OtpVID = (%x, %x)\n",CHK_PubOtpVID,CHK_PubOtpVID);
1568         status = FALSE;
1569     }
1570 
1571     HAL_NSK2_OTP_Get(0x3b00,15,0, &OTP_PubOtpOID);
1572     CHK_PubOtpOID = (pDataArray[3])&0xffff;
1573 
1574     if(OTP_PubOtpOID != CHK_PubOtpOID)
1575     {
1576         HALNSK2_DBG(NSK2_DBGLV_ERR,"%d, %s, PubOtpOID WRONG\n",__LINE__,__FUNCTION__);
1577         HALNSK2_DBG(NSK2_DBGLV_ERR,"OtpOID = (%x, %x)\n",CHK_PubOtpOID,OTP_PubOtpOID);
1578         status = FALSE;
1579     }
1580 
1581     MS_U32 KeyValid;
1582     HAL_NSK2_OTP_Get(0x3b1c,31,24, &KeyValid);
1583     HALNSK2_DBG(NSK2_DBGLV_INFO,"KeyValid = %x\n",KeyValid);
1584 
1585     MS_U32 CheckSum[4];
1586     HAL_NSK2_OTP_Get(0x3c30,31,0, &CheckSum[0]);
1587     HAL_NSK2_OTP_Get(0x3c34,31,0, &CheckSum[1]);
1588     HAL_NSK2_OTP_Get(0x3c38,31,0, &CheckSum[2]);
1589     HAL_NSK2_OTP_Get(0x3c3c,31,0, &CheckSum[3]);
1590     HALNSK2_DBG(NSK2_DBGLV_INFO,"CheckSum = %x, %x, %x, %x\n",CheckSum[0],CheckSum[1],CheckSum[2],CheckSum[3]);
1591     return status;
1592 }
1593 
HAL_NSK2_ReadDataTrans(MS_U32 read_data,MS_U8 * data)1594 static void HAL_NSK2_ReadDataTrans(MS_U32 read_data, MS_U8 *data)
1595 {
1596     data[3] = (MS_U8)(read_data & 0xff);
1597     data[2] = (MS_U8)((read_data>>8) & 0xff);
1598     data[1] = (MS_U8)((read_data>>16) & 0xff);
1599     data[0] = (MS_U8)((read_data>>24) & 0xff);
1600 }
1601 
HAL_NSK2_WriteDataTrans(MS_U8 * data)1602 static MS_U32 HAL_NSK2_WriteDataTrans(MS_U8 *data)
1603 {
1604     MS_U32 write_data = 0;
1605 
1606     write_data = (MS_U32)data[3] + ((MS_U32)data[2]<<8) + ((MS_U32)data[1]<<16) + ((MS_U32)data[0]<<24) ;
1607     return write_data;
1608 }
1609 
HAL_NSK2_ReadData(MS_U32 addr_offset,MS_U32 data_size,MS_U8 * data)1610 MS_U32 HAL_NSK2_ReadData(MS_U32 addr_offset, MS_U32 data_size, MS_U8 *data)
1611 {
1612     MS_U32 read_data, read_data_size;
1613     MS_U8  *read_ptr;
1614 
1615     if(data_size < 4)
1616     {
1617         return FALSE;
1618     }
1619 
1620     read_data_size = data_size;
1621     read_ptr = data;
1622 
1623     while(read_data_size >= 4)
1624     {
1625         StatusCheck(HAL_NSK2_CheckBusy());
1626 
1627         read_data = HAL_NSK2_ReadReg(addr_offset);
1628         HAL_NSK2_ReadDataTrans(read_data,read_ptr);
1629 
1630         addr_offset += 4;
1631         read_ptr += 4;
1632         read_data_size -= 4;
1633     }
1634 
1635     return TRUE;
1636 }
1637 
HAL_NSK2_WriteData(MS_U32 addr_offset,MS_U32 data_size,MS_U8 * data)1638 MS_U32 HAL_NSK2_WriteData(MS_U32 addr_offset, MS_U32 data_size, MS_U8 *data)
1639 {
1640     MS_U32 write_data, write_data_size;
1641     MS_U8  *write_ptr;
1642 
1643     if(data_size < 4)
1644     {
1645         return FALSE;
1646     }
1647 
1648     write_data_size = data_size;
1649     write_ptr = data;
1650 
1651     while(write_data_size >= 4)
1652     {
1653         StatusCheck(HAL_NSK2_CheckBusy());
1654 
1655         write_data = HAL_NSK2_WriteDataTrans(write_ptr);
1656         HAL_NSK2_WriteReg(addr_offset,write_data);
1657 
1658         addr_offset += 4;
1659         write_ptr   += 4;
1660         write_data_size -= 4;
1661     }
1662 
1663 
1664     return TRUE;
1665 }
1666 
HAL_NSK2_ReadData8(MS_U32 addr_offset,MS_U32 data_size,MS_U8 * data)1667 MS_U32 HAL_NSK2_ReadData8(MS_U32 addr_offset, MS_U32 data_size, MS_U8 *data)
1668 {
1669     MS_U32 read_data, read_data_size;
1670     MS_U8  *read_ptr;
1671 
1672     if(data_size < 4)
1673     {
1674         return FALSE;
1675     }
1676 
1677     read_data_size = data_size;
1678     read_ptr = data;
1679 
1680     while(read_data_size >= 4)
1681     {
1682         StatusCheck(HAL_NSK2_CheckBusy());
1683 
1684         read_data = HAL_NSK2_ReadReg(addr_offset);
1685         HAL_NSK2_ReadDataTrans(read_data,read_ptr);
1686 
1687         //HALNSK2_DBG(0,"addr = %x, read_data = %x\n",addr_offset,read_data);
1688         addr_offset += 4;
1689         read_ptr += 4;
1690         read_data_size -= 4;
1691     }
1692 
1693     return TRUE;
1694 }
1695 
HAL_NSK2_WriteData8(MS_U32 addr_offset,MS_U32 data_size,MS_U8 * data)1696 MS_U32 HAL_NSK2_WriteData8(MS_U32 addr_offset, MS_U32 data_size, MS_U8 *data)
1697 {
1698     MS_U32 write_data, write_data_size;
1699     MS_U8  *write_ptr;
1700 
1701     if(data_size < 4)
1702     {
1703         return FALSE;
1704     }
1705 
1706     write_data_size = data_size;
1707     write_ptr = data;
1708 
1709     while(write_data_size >= 4)
1710     {
1711         StatusCheck(HAL_NSK2_CheckBusy());
1712 
1713         write_data = HAL_NSK2_WriteDataTrans(write_ptr);
1714         HAL_NSK2_WriteReg(addr_offset,write_data);
1715         //HALNSK2_DBG(0,"addr = %x, write_data = %x\n",addr_offset, write_data);
1716         addr_offset += 4;
1717         write_ptr   += 4;
1718         write_data_size -= 4;
1719     }
1720 
1721     return TRUE;
1722 }
1723 
1724 
HAL_NSK2_ReadData32(MS_U32 addr_offset,MS_U32 data_size,MS_U32 * data)1725 MS_U32 HAL_NSK2_ReadData32(MS_U32 addr_offset, MS_U32 data_size, MS_U32 *data)
1726 {
1727     MS_U32 read_data, read_data_size;
1728     MS_U32 *read_ptr;
1729     //MS_U32 test_read_data;
1730 
1731     if(data_size < 1)
1732     {
1733         return FALSE;
1734     }
1735 
1736     read_data_size = data_size;
1737     read_ptr = data;
1738 
1739     while(read_data_size >= 1)
1740     {
1741         StatusCheck(HAL_NSK2_CheckBusy());
1742 
1743         read_data = HAL_NSK2_ReadReg(addr_offset);
1744         *read_ptr = read_data;
1745 
1746         //HALNSK2_DBG(0,"addr = %x, read_data = %x\n",addr_offset,read_data);
1747         addr_offset += 4;
1748         read_ptr ++;
1749         read_data_size --;
1750     }
1751 
1752     return TRUE;
1753 }
1754 
HAL_NSK2_WriteData32(MS_U32 addr_offset,MS_U32 data_size,MS_U32 * data)1755 MS_U32 HAL_NSK2_WriteData32(MS_U32 addr_offset, MS_U32 data_size, MS_U32 *data)
1756 {
1757     MS_U32 write_data, write_data_size;
1758     MS_U32 *write_ptr;
1759 
1760     if(data_size < 1)
1761     {
1762         return FALSE;
1763     }
1764 
1765     write_data_size = data_size;
1766     write_ptr = data;
1767 
1768     while(write_data_size >= 1)
1769     {
1770         StatusCheck(HAL_NSK2_CheckBusy());
1771 
1772         write_data = *write_ptr;
1773         HAL_NSK2_WriteReg(addr_offset,write_data);
1774         //HALNSK2_DBG(0,"addr = %x, write_data = %x\n",addr_offset, write_data);
1775         addr_offset += 4;
1776         write_ptr   ++;
1777         write_data_size --;
1778     }
1779 
1780 
1781     return TRUE;
1782 }
1783 
HAL_NSK2_EnableInt(void)1784 MS_U32 HAL_NSK2_EnableInt(void)
1785 {
1786     MS_U32 u32IntReg = 0;
1787 /*
1788 #define REG_NSK2_ACPU_INT       0xFC08
1789     #define NSK2_INT_CMD_EXIT           __BIT0
1790     #define NSK2_INT_ASYNC_EVENT        __BIT1
1791     #define NSK2_INT_ILLEGAL_CMD        __BIT2
1792     #define NSK2_INT_ILLEGAL_ACCESS     __BIT3
1793     #define NSK2_INT_RESET              __BIT4
1794     #define NSK2_INT_HANG               __BIT5
1795     #define NSK2_INT_KTE_VALID          __BIT6
1796     #define NSK2_INT_MASK_CLEAR         __BIT31
1797 */
1798 
1799     u32IntReg = (NSK2_INT_CMD_EXIT | NSK2_INT_ASYNC_EVENT | NSK2_INT_ILLEGAL_CMD | \
1800                 NSK2_INT_RESET | NSK2_INT_HANG | NSK2_INT_KTE_VALID | NSK2_INT_MASK_CLEAR);
1801 
1802     HAL_NSK2_WriteReg(REG_NSK2_ACPU_INT,u32IntReg);
1803     return TRUE;
1804 }
1805 
HAL_NSK2_DisableInt(void)1806 MS_U32 HAL_NSK2_DisableInt(void)
1807 {
1808     MS_U32 u32IntReg = 0;
1809 
1810     HAL_NSK2_WriteReg(REG_NSK2_ACPU_INT,u32IntReg);
1811     return TRUE;
1812 }
1813 
1814 
HAL_NSK2_GetIntStatus(void)1815 MS_U32 HAL_NSK2_GetIntStatus(void)
1816 {
1817     MS_U32 u32IntValue;
1818     u32IntValue = HAL_NSK2_ReadReg(REG_NSK2_ACPU_INT);
1819 
1820     //HALNSK2_DBG(NSK2_DBGLV_DEBUG,"Int status = %x\n",u32IntValue);
1821     return u32IntValue;
1822 }
1823 
HAL_NSK2_ClearInt(MS_U32 u32IntValue)1824 MS_U32 HAL_NSK2_ClearInt(MS_U32 u32IntValue)
1825 {
1826     //HALNSK2_DBG(NSK2_DBGLV_DEBUG,"Clear Int = %x\n",u32IntValue);
1827     HAL_NSK2_WriteReg(REG_NSK2_ACPU_INT,u32IntValue);
1828     return TRUE;
1829 }
1830 
1831 
HAL_NSK2_WriteControl(MS_U32 control)1832 MS_U32 HAL_NSK2_WriteControl(MS_U32 control)
1833 {
1834     HAL_NSK2_WriteReg(REG_NSK2_ACPU_CTRL_BLOCK, control);
1835     return TRUE;
1836 }
1837 
1838 
HAL_NSK2_WriteCommand(MS_U32 command)1839 MS_U32 HAL_NSK2_WriteCommand(MS_U32 command)
1840 {
1841     HAL_NSK2_WriteReg(REG_NSK2_ACPU_CMD, command);
1842     return TRUE;
1843 }
1844 
1845 
HAL_NSK2_GetMaxXConn(void)1846 MS_U32 HAL_NSK2_GetMaxXConn(void)
1847 {
1848     return MaximumXConnection;
1849 }
1850 
1851 
HAL_NSK2_CMChannelNum(void)1852 MS_U32 HAL_NSK2_CMChannelNum(void)
1853 {
1854     return 1;
1855 }
1856 
1857 
1858 typedef struct
1859 {
1860     MS_U8   u8Tag;
1861     MS_U8   u8Length;
1862     MS_U32  Offset;
1863     MS_U32  MSB;
1864     MS_U32  LSB;
1865 } ASST_t;
1866 
1867 
1868 static ASST_t K6liteAsst[] =
1869 {
1870     //the first tag is the same called pub otp, we should implement one function to obtain this,
1871     //becasue this the same as HW test, we can check at HW Test also.
1872     {0x20,  9,  0x3DA8,  63,   0}, //U_OTP_v_pubOtpUniqueID1
1873     {0x21, 16,  0x7C00, 127,   0}, //Bulk data 1
1874     {0x22, 16,  0x7C10, 127,   0}, //Bulk data 2
1875     {0x23, 16,  0x7C20, 127,   0}, //Bulk data 3
1876     {0x24, 16,  0x7C30, 127,   0}, //Bulk data 4
1877     {0x25,  8,  0x3B30, 31,    0}, //NV counter....
1878 
1879 
1880     {0x40,  1,  0x3B20,  27,  24}, //U_OTP_allow_illegalNDSFlagChk
1881     {0x41,  1,  0x3B24,   3,   0}, //U_OTP_ena_ACPUUseNSK2
1882     {0x42,  1,  0x3B24,   7,   4}, //U_OTP_ena_DBUSUseNSK2
1883     {0x43,  1,  0x3B24,  11,   8}, //U_OTP_ena_ForceOneMilSec
1884     {0x44,  1,  0x3B24,  15,  12}, //U_OTP_allow_SCCheck
1885     {0x45,  1,  0x3B24,  19,  16}, //U_OTP_ena_TestRCFreq
1886     {0x46,  1,  0x3B24,  23,  20}, //U_OTP_ena_SWRN
1887     {0x47,  1,  0x3B24,  27,  24}, //U_OTP_ena_NSKSeedPRNG
1888     {0x48,  1,  0x3B24,  31,  28}, //U_OTP_OTPWritePWDProtect
1889     {0x49,  1,  0x3B28,  11,   8}, //U_OTP_ena_EMMFilter
1890     {0x4A,  1,  0x3B28,  15,  12}, //U_OTP_ena_TestGenIN
1891     {0x4B,  1,  0x3B28,   7,   6}, //U_OTP_allow_NSK2_PWD_Mode
1892     {0x4C,  1,  0x3B2C,   3,   2}, //U_OTP_allow_NDSSC_ReadFail_BadPkt
1893     {0x4D,  1,  0x3B2C,   5,   4}, //U_OTP_allow_RANDOM_keybus
1894     {0x4E,  1,  0x3B2C,   7,   6}, //U_OTP_allow_RANDOM_byteacc
1895     {0x4F,  1,  0x3B2C,   9,   8}, //U_OTP_allow_NDS_Rd55AA
1896     {0x50,  1,  0x3B2C,  11,  10}, //U_OTP_allow_NDS_Parity_chk
1897     {0x51,  1,  0x3B2C,  15,  14}, //U_OTP_forbid_OTPBuiltInTest
1898     {0x52,  1,  0x3C58,   3,   0}, //U_OTP_ena_ESAAlgo_invalidate
1899     {0x53,  1,  0x3C58,   7,   4}, //U_OTP_ena_LocalAlgo_Invalidate
1900     {0x54,  1,  0x3C58,  11,   8}, //U_OTP_ContentProtEn
1901     {0x55,  1,  0x3C58,  13,  12}, //U_OTP_concurrency_configuration
1902     {0x56,  1,  0x3C58,  15,  14}, //U_OTP_allow_NSK_RNG_ROSC
1903     {0x57,  1,  0x3C58,  16,  16}, //U_OTP_nds_fc_disable
1904     {0x58,  1,  0x3C58,  17,  17}, //U_OTP_NDS_CPNR0_sel
1905     {0x59,  1,  0x3C58,  18,  18}, //U_OTP_NDS_CPNR_off
1906     {0x5A,  1,  0x3C60,   0,   0}, //U_OTP_forbid_CLK_SEED_TEST
1907     {0x5B,  1,  0x3C60,   1,   1}, //U_OTP_MOBF_TOP_use_DES
1908     {0x5C,  1,  0x3C78,   3,   0}, //U_OTP_ena_PVR_secure_protect_0
1909     {0x5D,  1,  0x3C74,   3,   0}, //U_OTP_ena_PVRNS2S
1910     {0x5E,  1,  0x3C80,  17,  16}, //U_OTP_forbid_SW_SPSD_Key
1911     {0x5F,  1,  0x3C80,  19,  18}, //U_OTP_forbid_KL_SPSD_Key
1912     {0x60,  1,  0x3CC0,   1,   0}, //U_OTP_forbid_ACPUWriteOTP
1913     {0x61,  1,  0x3CC0,   3,   2}, //U_OTP_forbid_ACPUReadOTP
1914     {0x62,  1,  0x3CC0,   5,   4}, //U_OTP_forbid_SCPUWriteOTP
1915     {0x63,  1,  0x3CC0,   7,   6}, //U_OTP_forbid_SCPUReadOTP
1916     {0x64,  1,  0x3CC0,   9,   8}, //U_OTP_forbid_DBBUSWriteOTP
1917     {0x65,  1,  0x3CC0,  11,  10}, //U_OTP_forbid_DBBUSReadOTP
1918     {0x66,  1,  0x3CC0,  13,  12}, //U_OTP_allow_RANDOM
1919     {0x67,  1,  0x3CC0,  15,  14}, //U_OTP_allow_NOISE_Rd
1920     {0x68,  1,  0x3CC8,   3,   0}, //U_OTP_SBoot
1921     {0x69,  1,  0x3CC8,   7,   4}, //U_OTP_SecretAreaEnable
1922     {0x6A,  2,  0x3CCC,   8,   0}, //U_OTP_SCAN_MODE
1923     {0x6B,  1,  0x3CCC,  15,  10}, //U_OTP_MBIST_MODE
1924     {0x6C,  1,  0x3CCC,  21,  16}, //U_OTP_I2C_MODE
1925     {0x6D,  1,  0x3CCC,  31,  26}, //U_OTP_EJTAG_MODE
1926     {0x6E,  1,  0x3C68,   3,   0}, //U_OTP_forbid_USBSlaveMode
1927     {0x6F,  1,  0x3CD0,   9,   8}, //U_OTP_allow_DRAM_MOBF
1928     {0x70,  1,  0x3CD4,  13,   8}, //U_OTP_BootMode
1929     {0x71,  1,  0x3CD4,  21,  16}, //U_OTP_SEPBootMode
1930     {0x72,  2,  0x3CD8,  23,  12}, //U_OTP_PostMskAreaRange1
1931     {0x73,  2,  0x3CE0,  23,  16}, //U_OTP_BootDevice
1932     {0x74,  1,  0x3D24,  11,   8}, //U_OTP_ena_NSK2
1933     {0x75,  1,  0x3D24,  15,  14}, //U_OTP_ena_DMA
1934     {0x76,  1,  0x3D24,  19,  16}, //U_OTP_ena_SEP
1935     {0x77,  1,  0x3D40,  21,  18}, //U_OTP_allow_SPSSPDKeyShuffle
1936     {0x78,  1,  0x3D44,   3,   0}, //U_OTP_ena_ACPU2KT
1937     {0x79,  1,  0x3D44,   7,   4}, //U_OTP_ena_NSK2KT
1938     {0x7A,  1,  0x3D44,  27,  24}, //U_OTP_ena_NSKCW2CryptoDMA
1939     {0x7B,  1,  0x3D44,  31,  28}, //U_OTP_ena_ACPU2LSA
1940     {0x7C,  1,  0x3CD0,  31,  26}, //U_OTP_I2C2TEE_MODE
1941     {0x7D,  1,  0x3CC8,  19,  16}, //U_OTP_SecR2_Sboot
1942     {0x7E,  1,  0x3C88,  27,  24}, //U_OTP_NDS_CODE_INTEGRITY_RSA_KEY_SEL
1943     {0x7F,  1,  0x3CC8,  27,  24}, //U_OTP_xCPU_Sboot
1944 };
1945 
1946 
endian_change(MS_U8 * ptr,MS_U32 bytes)1947 void endian_change(MS_U8* ptr, MS_U32 bytes)
1948 {
1949 
1950 	MS_U32 u32halve;
1951 	MS_U32 u32tmp;
1952 	MS_U32 i;
1953 
1954 	u32halve = bytes/2;
1955 	for(i=0; i<u32halve; i++)
1956 	{
1957 		u32tmp = ptr[i];
1958 		ptr[i] = ptr[bytes-i-1];
1959 		ptr[bytes-i-1] = u32tmp;
1960 	}
1961 }
1962 
HAL_NSK2_GetOTPProperties(MS_U32 * desc_size,MS_U8 * desc)1963 MS_U32 HAL_NSK2_GetOTPProperties(MS_U32 *desc_size, MS_U8 *desc)
1964 {
1965     MS_U32 NumOfTag = sizeof(K6liteAsst)/sizeof(ASST_t);
1966     MS_U32 ret_size = 0, i, j;
1967     MS_U8 *pDesc = (MS_U8 *)desc;
1968     MS_U32 OTPValue;
1969     MS_U8  RunLens;
1970     MS_U32 RunOffset;
1971 
1972     ret_size = 0;
1973     for( i=0; i<NumOfTag; i++)
1974     {
1975         ret_size += (K6liteAsst[i].u8Length + 2);
1976     }
1977 
1978     *desc_size = ret_size;
1979 
1980     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"length of OTP Tag = %x\n",ret_size);
1981     if(desc == NULL)
1982     {
1983         return TRUE;
1984     }
1985 
1986     for( i=0; i<NumOfTag; i++)
1987     {
1988         *pDesc++ = K6liteAsst[i].u8Tag;
1989         *pDesc++ = K6liteAsst[i].u8Length;
1990 
1991         if(0x20 == K6liteAsst[i].u8Tag) //pubOTP...special case....
1992         {
1993             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"begin of Tag 0x20\n");
1994             *pDesc++ = 0;
1995             //4 Chip ID
1996             MS_U8 PubOTP[8];
1997             for(j=0;j<8;j+=4)
1998             {
1999                 MS_U32 u32Tmp;
2000                 u32Tmp = MDrv_CA_OTP_Read(K6liteAsst[i].Offset + j);
2001                 memcpy(&PubOTP[j], &u32Tmp, 4);
2002             }
2003 
2004 
2005             endian_change(PubOTP, 8);
2006 
2007             memcpy(pDesc,PubOTP,8);
2008             pDesc += 8;
2009 
2010             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"end of Tag 0x20\n");
2011         }
2012         else if (0x25 == K6liteAsst[i].u8Tag) //NV Counter...special case....
2013         {
2014             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"begin of Tag 0x25\n");
2015             MS_U32 MaxNVCounter = 1024;
2016             *pDesc++ = (MS_U8)((MaxNVCounter>>24)&0xff);
2017             *pDesc++ = (MS_U8)((MaxNVCounter>>16)&0xff);
2018             *pDesc++ = (MS_U8)((MaxNVCounter>>8)&0xff);
2019             *pDesc++ = (MS_U8)(MaxNVCounter&0xff);
2020 
2021             *pDesc++ = 0;
2022             *pDesc++ = 0;
2023             *pDesc++ = 0;
2024             *pDesc++ = 0;
2025 
2026             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"end of Tag 0x25\n");
2027         }
2028         else
2029         {
2030             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"begin of Tag 0x%x\n",K6liteAsst[i].u8Tag);
2031             RunLens = K6liteAsst[i].u8Length;
2032             RunOffset = K6liteAsst[i].Offset;
2033             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"Offset = %x, length = %x\n",RunOffset, RunLens);
2034             if(K6liteAsst[i].u8Length <= 4)
2035             {
2036                 HAL_NSK2_OTP_Get(K6liteAsst[i].Offset, K6liteAsst[i].MSB, K6liteAsst[i].LSB, &OTPValue);
2037                 if(1 == RunLens)
2038                     *pDesc++ = (MS_U8)(OTPValue&0xff);
2039                 else if(2 == RunLens)
2040                 {
2041                     *pDesc++ = (MS_U8)((OTPValue>>8)&0xff);
2042                     *pDesc++ = (MS_U8)(OTPValue&0xff);
2043                 }
2044                 else if(4 == RunLens)
2045                 {
2046                     *pDesc++ = (MS_U8)((OTPValue>>24)&0xff);
2047                     *pDesc++ = (MS_U8)((OTPValue>>16)&0xff);
2048                     *pDesc++ = (MS_U8)((OTPValue>>8)&0xff);
2049                     *pDesc++ = (MS_U8)(OTPValue&0xff);
2050                 }
2051             }
2052             else //length > 4, tmp solution
2053             {
2054                 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"length = %x\n",RunLens);
2055                 MS_U8 BulkData[16];
2056                 for( j=0; j<RunLens; j+=4 )
2057                 {
2058                     HAL_NSK2_OTP_Get(RunOffset + j, 31, 0, &OTPValue);
2059                     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"run offset = %x, Value = %x\n", (RunOffset + j),OTPValue);
2060                     memcpy(&BulkData[j],&OTPValue,4);
2061                 }
2062                 endian_change(BulkData, 16);
2063                 memcpy(pDesc,BulkData,16);
2064                 pDesc += 16;
2065             }
2066 
2067             HALNSK2_DBG(NSK2_DBGLV_DEBUG,"end of Tag 0x%x\n",K6liteAsst[i].u8Tag);
2068         }
2069     }
2070 
2071 #if 0
2072     MS_U32 size = *desc_size;
2073 
2074     for(i=0;i<size;i++)
2075     {
2076         printf(" (%d, %x) \n", i , desc[i]);
2077     }
2078 #endif
2079     return TRUE;
2080 }
2081 
2082 
2083 
2084 typedef struct
2085 {
2086     MS_U32  Offset;
2087     MS_U32  MSB;
2088     MS_U32  LSB;
2089     MS_U32  Bits;
2090 } ChipOTPCfg_t;
2091 
2092 
2093 static ChipOTPCfg_t K6ChipCfg[] =
2094 {
2095     {0x3B20,  7,   0,   8},  //[127:120]	U_OTP_forbid_NSK_wr_sck[7:0]	11111111
2096     {0x3B20, 15,   8,   8},  //[119:112]	U_OTP_UseCheckSum[7:0]	11111111
2097     {0x3B20, 17,  16,   2},  //[111:110]	U_OTP_allow_NDSKey_BlankChk[1:0]	11
2098     {0x3B20, 19,  18,   2},  //[109:108]	U_OTP_allow_ProgFail_RuinNDSKey[1:0]	11
2099     {0x3B20, 21,  20,   2},  //[107:106]	U_OTP_allow_NDSReadKeyWait200ms[1:0]	11
2100     {0x3B20, 23,  22,   2},  //[105:104]	U_OTP_allow_ReadErrorRstOtp[1:0]	11
2101     {0x3B20, 27,  24,   4},  //[103:100]	U_OTP_allow_illegalNDSFlagChk[3:0]	1111
2102     {0x3B20, 31,  28,   4},  //[99:96]	U_OTP_allow_Rst_NDS_SCFlag_ParityFail[3:0]	1111
2103     {0x3B24,  3,   0,   4},  //[95:92]	U_OTP_ena_ACPUUseNSK2[3:0]	0001
2104     {0x3B28,  7,   6,   2},  //[91:90]	U_OTP_allow_NSK2_PWD_Mode[1:0]	11
2105     {0x3B2C, 15,  14,   2},  //[[89:88]	U_OTP_forbid_OTPBuiltInTest[1:0]	11
2106     {0x3B2C, 17,  16,   2},  //[87:86]	U_OTP_forbid_Kilo_ProgRepair[1:0]	11
2107     {0x3C50,  1,   0,   2},  //[85:84]	U_OTP_NDS_ESCK_Key1_obfuscation[1:0]	11
2108     {0x3C50,  3,   2,   2},  //[83:82]	U_OTP_NDS_Key1_integrity_chk[1:0]	11
2109     {0x3C50,  5,   4,   2},  //[81:80]	U_OTP_NDS_ESCK_Key2_obfuscation[1:0]	11
2110     {0x3C50,  7,   6,   2},  //[79:78]	U_OTP_NDS_Key2_integrity_chk[1:0]	11
2111     {0x3C50,  9,   8,   2},  //[77:76]	U_OTP_NDS_ESCK_Key3_obfuscation[1:0]	11
2112     {0x3C50, 11,  10,   2},  //[75:74]	U_OTP_NDS_Key3_integrity_chk[1:0]	11
2113     {0x3C50, 13,  12,   2},  //[73:72]	U_OTP_NDS_ESCK_Key4_obfuscation[1:0]	11
2114     {0x3C50, 15,  14,   2},  //[71:70]	U_OTP_NDS_Key4_integrity_chk[1:0]	11
2115     {0x3C50, 17,  16,   2},  //[69:68]	U_OTP_NDS_ESCK_Key5_obfuscation[1:0]	11
2116     {0x3C50, 19,  18,   2},  //[67:66]	U_OTP_NDS_Key5_integrity_chk[1:0]	11
2117     {0x3C50, 21,  20,   2},  //[65:64]	U_OTP_NDS_ESCK_Key6_obfuscation[1:0]	11
2118     {0x3C50, 23,  22,   2},  //[63:62]	U_OTP_NDS_Key6_integrity_chk[1:0]	11
2119     {0x3C50, 25,  24,   2},  //[61:60]	U_OTP_NDS_ESCK_Key7_obfuscation[1:0]	11
2120     {0x3C50, 27,  26,   2},  //[59:58]	U_OTP_NDS_Key7_integrity_chk[1:0]	11
2121     {0x3C50, 29,  28,   2},  //[57:56]	U_OTP_NDS_ESCK_Key8_obfuscation[1:0]	11
2122     {0x3C50, 31,  30,   2},  //[55:54]	U_OTP_NDS_Key8_integrity_chk[1:0]	11
2123     {0x3C58, 11,   8,   4},  //[53:50]	U_OTP_ContentProtEn[3:0]	1111
2124     {0x3C58, 13,  12,   2},  //[49:48]	U_OTP_concurrency_configuration[1:0]	10
2125     {0x3C58, 15,  14,   2},  //[47:46]	U_OTP_allow_NSK_RNG_ROSC[1:0]	11
2126     {0x3C60,  0,   0,   1},  //[45]	U_OTP_forbid_CLK_SEED_TEST	1
2127     {0x3C60,  3,   2,   2},  //[44:43]	U_OTP_OBFUSCATEVideoStream[1:0]	00
2128     {0x3C78,  3,   0,   4},  //[42:39]	U_OTP_ena_PVR_secure_protect_0[3:0]	0001
2129     {0x3C78, 11,   8,   4},  //[38:35]	U_OTP_dis_TSO[3:0]	0001
2130     {0x3CC0, 13,  12,   2},  //[34:33]	U_OTP_allow_RANDOM[1:0]	11
2131     {0x3CC8,  3,   0,   4},  //[32:29]	U_OTP_SBoot[3:0]	0000
2132     {0x3C68,  3,   0,   4},  //[28:25]	U_OTP_forbid_USBSlaveMode[3:0]	1111
2133     {0x3CD0,  9,   8,   2},  //[24:23]	U_OTP_allow_DRAM_MOBF[1:0]	00
2134     {0x3CD0, 18,  16,   3},  //[22:20]	U_OTP_forbid_clk_otp_sel[2:0]	111
2135     {0x3CE0, 31,  30,   2},  //[19:18]	U_OTP_forbid_STR[1:0]	00
2136     {0x3D24, 11,   8,   4},  //[17:14]	U_OTP_ena_NSK2[3:0]	0001
2137     {0x3D24, 15,  14,   2},  //[13:12]	U_OTP_ena_DMA[1:0]	01
2138     {0x3D24, 19,  16,   4},  //[11:8]	U_OTP_ena_SCPU[3:0]	0001
2139     {0x3D44,  3,   0,   4},  //[7:4]	U_OTP_ena_ACPU2KT[3:0]	0001
2140     {0x3D44,  7,   4,   4},  //[3:0]	U_OTP_ena_NSK2KT[3:0]	0001
2141 
2142     {0x3D44, 19,  16,   4},  //[127:124]	U_OTP_ena_ACPUWrNSKKey2KT[3:0]	0111
2143     {0x3D50,  3,   0,   4},  //[123:120]	U_OTP_ena_LSACPCM[3:0]	0001
2144     {0x3D50,  7,   4,   4},  //[119:116]	U_OTP_ena_AESBasedCipher[3:0]	0001
2145     {0x3D50, 11,   8,   4},  //[115:112]	U_OTP_ena_DESBasedCipher[3:0]	0001
2146     {0x3D50, 15,  12,   4},  //[111:108]	U_OTP_ena_3DESBasedCipher[3:0]	0001
2147     {0x3D50, 19,  16,   4},  //[107:104]	U_OTP_ena_Multi2BasedCipher[3:0]	0001
2148     {0x3D50, 23,  20,   4},  //[103:100]	U_OTP_ena_DVBCSA2ConfCipher[3:0]	0001
2149     {0x3D50, 27,  24,   4},  //[99:96]	U_OTP_ena_DVBCSA2Cipher[3:0]	0001
2150     {0x3D50, 31,  28,   4},  //[95:92]	U_OTP_ena_DVBCSA3BasedCipher[3:0]	0001
2151     {0x3D54, 31,   0,  32},  //[91:28]	U_OTP_Ch_SwitchComb[63:0]	1111111111111111111111111111111111111111111111111111111111111111
2152     {0x3D58, 31,   0,  32},  //[91:28]	U_OTP_Ch_SwitchComb[63:0]	1111111111111111111111111111111111111111111111111111111111111111
2153     {0x3D5C,  3,   0,   4},  //[27:24]	U_OTP_ena_LSAD_MDI[3:0]	0001
2154     {0x3D5C,  7,   4,   4},  //[23:20]	U_OTP_ena_LSAD_MDD[3:0]	0001
2155     {0x3D5C, 11,   8,   4},  //[19:16]	U_OTP_ena_LSAD_CIPLUS_AES[3:0]	0001
2156     {0x3D5C, 15,  12,   4},  //[15:12]	U_OTP_ena_LSAD_AES_ECB_CLEAR[3:0]	0001
2157     {0x3D5C, 19,  16,   4},  //[11:8]	U_OTP_ena_LSAD_SCTE41_SCTE52_DES[3:0]	0001
2158     {0x3D60,  3,   0,   4},  //[7:4]	U_OTP_ena_ESA_CIPLUS_AES[3:0]	0001
2159     {0x3D60,  7,   4,   4},  //[3:0]	U_OTP_ena_ESA_CIPLUS_DES[3:0]	0001
2160 
2161     {0x3D60, 11,   8,   4},  //[127:124]	U_OTP_ena_ESA_SCTE52_DES[3:0]	0001
2162     {0x3D60, 15,  12,   4},  //[123:120]	U_OTP_ena_ESA_tDES_CBC_CLEAR[3:0]	0001
2163     {0x3D64,  3,   0,   4},  //[119:116]	U_OTP_ena_LSAS_MDI[3:0]	0001
2164     {0x3D64,  7,   4,   4},  //[115:112]	U_OTP_ena_LSAS_MDD[3:0]	0001
2165     {0x3D64, 11,   8,   4},  //[111:108]	U_OTP_ena_LSAS_CIPLUS_AES[3:0]	0001
2166     {0x3D64, 15,  12,   4},  //[107:104]	U_OTP_ena_LSAS_AES_ECB_CLEAR[3:0]	0001
2167     {0x3D64, 19,  16,   4},  //[103:100]	U_OTP_ena_LSAS_SCTE41_SCTE52_DES[3:0]	0001
2168     {0x3D68,  7,   4,   4},  //[99:96]	U_OTP_ena_ReviewFailPkt[3:0]	0001
2169     {0x3D68, 11,   8,   4},  //[95:92]	U_OTP_dis_NonSecRangeEncrypt[3:0]	0001
2170     {0x3D6C,  3,   0,   4},  //[91:88]	U_OTP_ena_CA_PVR_secure_protect_0[3:0]	0001
2171     {0x3D6C, 19,  16,   4},  //[87:84]	U_OTP_ena_LowerPathRec[3:0]	0111
2172     {0x3FA0,  9,   8,   2},  //[83:82]	U_OTP_I2C_PWD_obfuscation[1:0]	11
2173     {0x3FA0, 11,  10,   2},  //[81:80]	U_OTP_EJTAG_PWD_obfuscation[1:0]	11
2174     {0x3FA0, 13,  12,   2},  //[79:78]	U_OTP_SCAN_PWD_obfuscation[1:0]	11
2175     {0x3FA0, 15,  14,   2},  //[77:76]	U_OTP_MBIST_PWD_obfuscation[1:0]	11
2176     //[63:0]	0	0000000000000000000000000000000000000000000000000000000000000000
2177 
2178 };
2179 
2180 #define FullChipConfigSize     0x27
2181 
HAL_NSK2_GetFullChipSize(void)2182 MS_U32 HAL_NSK2_GetFullChipSize(void)
2183 {
2184     return FullChipConfigSize;
2185 }
2186 
HAL_NSK2_GetFullChipConfig(MS_U32 * desc_size,MS_U8 * desc)2187 MS_U32 HAL_NSK2_GetFullChipConfig(MS_U32 *desc_size, MS_U8 *desc)
2188 {
2189     *desc_size = FullChipConfigSize;
2190     if(desc == NULL)
2191     {
2192         return TRUE;
2193     }
2194 
2195     MS_U32 i, j, remain_bits, shift_bits;
2196     MS_U32 NumOfConfigs = sizeof(K6ChipCfg)/sizeof(ChipOTPCfg_t);
2197     MS_U32 TotalOTPValue[100];
2198 
2199     memset(desc, 0x0, FullChipConfigSize);
2200     memset(TotalOTPValue, 0x0, 100*sizeof(MS_U32));
2201     //desc[0x40-1] = 0x28;
2202     //desc[0x28] = 0x80;
2203 
2204     for(i=0; i<NumOfConfigs; i++)
2205     {
2206         HAL_NSK2_OTP_Get(K6ChipCfg[i].Offset, K6ChipCfg[i].MSB, K6ChipCfg[i].LSB, &TotalOTPValue[i]);
2207         HALNSK2_DBG(NSK2_DBGLV_INFO,"%d, %x\n",i, TotalOTPValue[i]);
2208     }
2209 
2210     j = 0;
2211     remain_bits = 8;
2212     for(i=0; i<NumOfConfigs; i++)
2213     {
2214         if(K6ChipCfg[i].Bits>remain_bits)
2215         {
2216             if(K6ChipCfg[i].Bits == 32)
2217             {
2218                 if(remain_bits != 8)
2219                 {
2220                     shift_bits = 8-remain_bits;
2221                     desc[j] |= 0xf;
2222                     j++;
2223                     desc[j] = 0xff;
2224                     j++;
2225                     desc[j] = 0xff;
2226                     j++;
2227                     desc[j] = 0xff;
2228                     j++;
2229                     desc[j] = 0xf0;
2230                     remain_bits = 4;
2231                 }
2232                 else
2233                 {
2234                     memcpy(&desc[j], &TotalOTPValue[i], 4);
2235                     j += 4;
2236                     remain_bits = 0;
2237                 }
2238             }
2239             else
2240             {
2241 
2242                 shift_bits = K6ChipCfg[i].Bits-remain_bits;
2243                 //printf("not equal case (%d), shift_bits = %x\n",i,shift_bits);
2244                 desc[j] |= (TotalOTPValue[i] >> shift_bits);
2245 
2246                 j++;
2247 
2248                 remain_bits = (8-shift_bits);
2249                 desc[j] |= (( TotalOTPValue[i] << remain_bits ) & 0xff);            }
2250 
2251         }
2252         else
2253         {
2254             remain_bits -= K6ChipCfg[i].Bits;
2255 
2256             desc[j] |= (TotalOTPValue[i] << (remain_bits));
2257 
2258         }
2259 
2260         if(remain_bits == 0)
2261         {
2262             remain_bits = 8;
2263             j++;
2264         }
2265     }
2266 
2267     return TRUE;
2268 }
2269 
2270 
2271 static NVCounter_Desc_t nvcounter_desc = {
2272     .descriptor_tag = NSK2HDI_OTP_NVCOUNTER_DESC_TAG,
2273     .descriptor_length = 8, //sizeof(NVCounter_Desc_t) - 2,
2274     .max_nvcounter[0] = 0x00,
2275     .max_nvcounter[1] = 0x00,
2276     .max_nvcounter[2] = 0x04,
2277     .max_nvcounter[3] = 0x00,
2278     .left_nvcounter[0] = 0x00,
2279     .left_nvcounter[1] = 0x00,
2280     .left_nvcounter[2] = 0x04,
2281     .left_nvcounter[3] = 0x00,
2282 };
2283 
2284 #define TotalNVNumber       1024
2285 
2286 
2287 
HAL_NSK2_GetNVCounterConfig(MS_U32 * desc_size,MS_U8 * desc)2288 MS_U32 HAL_NSK2_GetNVCounterConfig(MS_U32 *desc_size, MS_U8 *desc)
2289 {
2290     MS_U32 u32NvCounter = 0;
2291     MS_U32 u32LeftNvCounter = 0;
2292 
2293     if(desc == NULL)
2294     {
2295         *desc_size = sizeof(nvcounter_desc);
2296         HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_NVCounterUpdates return desc_size = %x\n",*desc_size);
2297         return TRUE;
2298     }
2299 
2300     *desc_size = sizeof(nvcounter_desc);
2301 // Read NV Counter from register directly
2302 #if 1
2303     u32NvCounter = NI_REG (REG_NI_NSK21_GET_NVCOUNTER);
2304 #else
2305     HAL_NSK2_WriteControl(0x1);
2306     MsOS_DelayTaskUs(10);
2307     HAL_NSK2_WriteCommand(0x73);
2308     MsOS_DelayTaskUs(10);
2309     StatusCheck(HAL_NSK2_CheckBusy());
2310 
2311     MS_U32 NVValue, LeftNVCounter;
2312     HAL_NSK2_ReadData32(0x1C, 0x1, &NVValue);
2313 #endif
2314 
2315     u32LeftNvCounter = TotalNVNumber - u32NvCounter;
2316 
2317     HALNSK2_DBG(NSK2_DBGLV_INFO,"NVValue = %d, LeftNVCounter = %d \n", u32NvCounter, u32LeftNvCounter);
2318 
2319     nvcounter_desc.left_nvcounter[0] = (MS_U8)((u32LeftNvCounter>>24)&0xff);
2320     nvcounter_desc.left_nvcounter[1] = (MS_U8)((u32LeftNvCounter>>16)&0xff);
2321     nvcounter_desc.left_nvcounter[2] = (MS_U8)((u32LeftNvCounter>>8)&0xff);
2322     nvcounter_desc.left_nvcounter[3] = (MS_U8)(u32LeftNvCounter&0xff);
2323 
2324     memcpy(desc,&nvcounter_desc,sizeof(nvcounter_desc));
2325 
2326     return TRUE;
2327 }
2328 
HAL_NSK2_SetDbgLevel(MS_U32 u32Level)2329 void HAL_NSK2_SetDbgLevel(MS_U32 u32Level)
2330 {
2331     _g32NSK2HalDbgLv = u32Level;
2332     HALNSK2_DBG(NSK2_DBGLV_INFO, "%s level: %x\n", __FUNCTION__, u32Level);
2333     return;
2334 }
2335 
HAL_NSK2_SetPollingCnt(MS_U32 u32Cnt)2336 void HAL_NSK2_SetPollingCnt(MS_U32 u32Cnt)
2337 {
2338     dead_polling_cnt = u32Cnt;
2339 }
2340 
2341 #if 0
2342 void HAL_NSK2_BurstLen(MS_U32 u32PVREng, MS_U32 u32BurstMode)
2343 {
2344     HAL_PVR_BurstLen(u32PVREng, u32BurstMode);
2345 }
2346 #endif
2347 
HAL_NSK2_ClockTest(MS_U32 testnum)2348 void HAL_NSK2_ClockTest(MS_U32 testnum)
2349 {
2350 
2351     MS_U32 u32Data;
2352     switch(testnum)
2353     {
2354     case 0:
2355         RSA_REG(REG_RSA_CLK_ENABLE) |= RSA_PM_NSKCLK_ENABLE;
2356         MsOS_DelayTaskUs(1);
2357 
2358         u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2359         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2360 
2361 
2362         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2363         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2364 
2365         //bit 0 set to 1...
2366         NI_REG(REG_NI_NSK2_CTRL) = u32Data | NI_NSK2_RESET_DISABLE | NI_NSK2_CLK_ENABLE;
2367 
2368         MsOS_DelayTaskUs(1);
2369         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2370         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2371         break;
2372 
2373 
2374     case 1:
2375         RSA_REG(REG_RSA_CLK_ENABLE) |= RSA_PM_NSKCLK_ENABLE;
2376         MsOS_DelayTaskUs(1);
2377 
2378         u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2379         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2380 
2381 
2382         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2383         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2384 
2385         //disable nsk2 clock
2386         NI_REG(REG_NI_NSK2_CTRL) = u32Data & ~(NI_NSK2_CLK_ENABLE) ;
2387         MsOS_DelayTask(1);
2388         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2389         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2390 
2391         break;
2392 
2393     case 2:
2394         RSA_REG(REG_RSA_CLK_ENABLE) &= (~RSA_PM_NSKCLK_ENABLE);
2395         MsOS_DelayTaskUs(1);
2396 
2397         u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2398         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2399 
2400 
2401         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2402         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2403 
2404         //bit 0 set to 1...
2405         NI_REG(REG_NI_NSK2_CTRL) = u32Data | NI_NSK2_RESET_DISABLE | NI_NSK2_CLK_ENABLE;
2406 
2407         MsOS_DelayTaskUs(1);
2408         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2409         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2410         break;
2411 
2412     case 3:
2413         RSA_REG(REG_RSA_CLK_ENABLE) &= (~RSA_PM_NSKCLK_ENABLE);
2414         MsOS_DelayTaskUs(1);
2415 
2416         u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2417         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2418 
2419         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2420         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2421 
2422         //disable nsk2 clock
2423         NI_REG(REG_NI_NSK2_CTRL) = u32Data & ~(NI_NSK2_CLK_ENABLE) ;
2424         MsOS_DelayTask(1);
2425         u32Data = NI_REG(REG_NI_NSK2_CTRL);
2426         HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2427         break;
2428     }
2429 }
2430 
HAL_NSK2_GetRNGThroughPut(void * pRngData,MS_U32 u32DataSize,MS_BOOL bDump)2431 void HAL_NSK2_GetRNGThroughPut(void *pRngData, MS_U32 u32DataSize, MS_BOOL bDump)
2432 {
2433 	MS_U32 u32StartTime;
2434     MS_U32 *pRNG_Data = (MS_U32 *)pRngData;
2435     MS_U32 valid = 0;
2436 	MS_U32 i = 0;
2437 
2438     NI_REG(REG_NI_NSK2_CTRL) = NI_REG(REG_NI_NSK2_CTRL) & (~NI_NSK2_RESET_DISABLE);
2439     NI_REG(REG_NI_NSK2_FREERUN) = NI_REG(REG_NI_NSK2_FREERUN) & (~ (NI_NSK2_RANDOM_FREERUN | NI_NSK2_RANDOM_ONEBYONE) );
2440 
2441     u32StartTime = MsOS_GetSystemTime();
2442 
2443 	for(i=0; i< u32DataSize; i++)
2444 	{
2445 		do
2446 		{
2447 			valid = NI_REG(REG_NI_NSK2_TRNG_VALID) & NI_NSK2_TRNG_VALID_MASK; //trng_sw_read_valid_nsk ;
2448 		}while(valid != 1);
2449 
2450 		pRNG_Data[i] = NI_REG(REG_NI_NSK2_TRNG_DATA);//trng_sw_read_data_nsk;
2451 		NI_REG(REG_NI_NSK2_FREERUN) = NI_REG(REG_NI_NSK2_FREERUN) | NI_NSK2_RANDOM_ONEBYONE;  //lfsr_get_go_nsk = 1 ;
2452 	}
2453 	MS_U32 u32EndTime = MsOS_GetSystemTime();
2454 
2455 	HALNSK2_DBG(NSK2_DBGLV_ERR,  "1M bit data size, total Time = %1d ms\n", (MS_U32)(u32EndTime - u32StartTime));
2456 
2457 	if(bDump)
2458 	{
2459 		for(i = 0 ; i < u32DataSize ; i ++)
2460 		{
2461 			HALNSK2_DBG(NSK2_DBGLV_ERR, "%08x\n", (unsigned int)(pRNG_Data[i]));
2462 		}
2463 	}
2464 }
2465 
HAL_NSK2_RunFree(MS_BOOL bRunFree)2466 void HAL_NSK2_RunFree(MS_BOOL bRunFree)
2467 {
2468     HALNSK2_DBG(NSK2_DBGLV_DEBUG, "bRunFree = %d\n", bRunFree);
2469 
2470 	if(bRunFree)
2471 	{
2472 		_gCheckBusyFlag = FALSE;
2473 	}
2474 	else
2475 	{
2476 		_gCheckBusyFlag = TRUE;
2477 	}
2478 }
2479 
2480 
HAL_NSK2_PushSlowClock(MS_BOOL HaltClk,MS_U32 NumOfMs)2481 MS_U32 HAL_NSK2_PushSlowClock ( MS_BOOL HaltClk, MS_U32 NumOfMs)
2482 {
2483     //HALNSK2_DBG(NSK2_DBGLV_DEBUG, "HaltClk = %d NumofTenSecond = %d\n", HaltClk, NumofTenSecond);
2484     _gCheckBusyFlag = TRUE;
2485     StatusCheck(HAL_NSK2_CheckBusy());
2486 	MS_U32 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2487 
2488 	if(HaltClk == TRUE)//close clk
2489 	{
2490 		NI_REG(REG_NI_NSK2_CTRL) = u32Data & (~NI_NSK2_CLK_ENABLE) ;
2491         _gCheckBusyFlag = FALSE;
2492 	}
2493 	else //open clk
2494 	{
2495 	    _gCheckBusyFlag = TRUE;
2496         //_gCheckBusyFlag = FALSE;
2497 	    if( (u32Data & NI_NSK2_CLK_ENABLE) == 0)
2498         {
2499     		NI_REG(REG_NI_NSK2_CTRL) = u32Data | NI_NSK2_CLK_ENABLE ;
2500             //MsOS_DelayTaskUs(1);
2501         }
2502 	}
2503 
2504     //printf("REG_NI_NSK2_CTRL = %x\n",NI_REG(REG_NI_NSK2_CTRL));
2505 
2506     //u32Data = 0;
2507     u32Data = NI_REG(REG_NI_NSK2_CLK_CSA);
2508 
2509 
2510     NI_REG(REG_NI_NSK2_CLK_CSA) = u32Data | NSK2_PUSH_SLOW_CLK;
2511 
2512 
2513     if(NumOfMs <= 2)
2514     {
2515         MsOS_DelayTaskUs(NumOfMs*500);
2516     }
2517     else
2518     {
2519         MsOS_DelayTask(NumOfMs-1);
2520     }
2521 
2522     return TRUE;
2523 }
2524 
2525 
2526 
HAL_NSK2_GetCMProperties(MS_U32 * desc_size,MS_U8 * desc)2527 MS_U32 HAL_NSK2_GetCMProperties(MS_U32 *desc_size, MS_U8 *desc)
2528 {
2529     MS_U32 i;
2530     if(desc == NULL)
2531     {
2532         *desc_size = sizeof(cmchannel_group_capability_descriptor_t) + sizeof(cm_algo) /*sizeof(cmchannel_group_algorithm_record_descriptor_t)*CMChannelDescSize*/ ;
2533         HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetCMProperties return desc_size = %x\n",*desc_size);
2534         return TRUE;
2535     }
2536 
2537     for(i=0;i<32;i++)
2538     {
2539         cm_capb.switch_combination_bitmap[i] = 0xFF;
2540     }
2541 
2542     *desc_size = sizeof(cmchannel_group_capability_descriptor_t) + sizeof(cm_algo);
2543 
2544     memcpy(desc,&cm_capb,sizeof(cmchannel_group_capability_descriptor_t));
2545     memcpy(desc+sizeof(cmchannel_group_capability_descriptor_t), &cm_algo[0] , sizeof(cm_algo) /*sizeof(cmchannel_group_algorithm_record_descriptor_t)*CMChannelDescSize*/ );
2546 
2547     HALNSK2_DBG(NSK2_DBGLV_INFO,"\nHAL_NSK2_GetCMProperties return *desc_size = %x,desc = %x\n",*desc_size,(MS_U32)desc);
2548 
2549     return TRUE;
2550 }
2551 
HAL_NSK2_GetM2MProperties(MS_U32 * desc_size,MS_U8 * desc)2552 MS_U32 HAL_NSK2_GetM2MProperties(MS_U32 *desc_size, MS_U8 *desc)
2553 {
2554     if(desc == NULL)
2555     {
2556         *desc_size = sizeof(m2m_capa_desc) + sizeof(m2m_algo);
2557         HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetM2MProperties #1 return desc_size = %x\n",*desc_size);
2558         return TRUE;
2559     }
2560 
2561     *desc_size = sizeof(m2m_capa_desc) + sizeof(m2m_algo);
2562 
2563     memcpy(desc, &m2m_capa_desc, sizeof(m2m_capa_desc));
2564     memcpy(desc+sizeof(m2m_capa_desc), &m2m_algo[0], sizeof(m2m_algo));
2565 
2566     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetM2MProperties #2 return desc_size = %x\n",*desc_size);
2567     return TRUE;
2568 }
2569 
HAL_NSK2_GetDMAProperties(MS_U32 * desc_size,MS_U8 * desc)2570 MS_U32 HAL_NSK2_GetDMAProperties(MS_U32 *desc_size, MS_U8 *desc)
2571 {
2572     if(desc == NULL)
2573     {
2574         *desc_size = sizeof(dma_capa_desc);
2575         HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetDMAProperties return desc_size = %x\n",*desc_size);
2576         return TRUE;
2577     }
2578 
2579     *desc_size = sizeof(dma_capa_desc);
2580 
2581     memcpy(desc, &dma_capa_desc, *desc_size);
2582 
2583     return TRUE;
2584 }
2585 
2586 //---------------Debug Information----------------------------//
2587 //------------------------------------------------------------//
2588 
HAL_NSK2_ReadTSPInfo(MS_U32 pid_no)2589 void HAL_NSK2_ReadTSPInfo(MS_U32 pid_no)
2590 {
2591     MS_U32 pid_data;
2592     MS_U32 FltPid, TS_SRC, dscmb_key_en, prim, pid_pair;
2593 
2594     FltPid = TS_SRC = dscmb_key_en = prim = pid_pair = 0;
2595     REG_PidFlt *pPidFlt = PPIDFLT0(pid_no);
2596 
2597     pid_data = TSP32_IdrR(pPidFlt);
2598 
2599     HALNSK2_DBG(NSK2_DBGLV_INFO,"pid_data = %x\n",pid_data);
2600 
2601     FltPid = (pid_data & 0x1fff);
2602     TS_SRC = ((pid_data>>13) & 0x7);
2603     dscmb_key_en = ((pid_data>>16) & 0x1);
2604     prim = ((pid_data>>17) & 0x1);
2605     pid_pair = ((pid_data>>18) & 0xf);
2606 
2607     if(dscmb_key_en)
2608     {
2609         HALNSK2_DBG(NSK2_DBGLV_INFO,"(%d) = (FltPid=%x, TS_SRC=%d, prim=%d, pid_pair=%d\n",pid_no,FltPid,TS_SRC,prim,pid_pair);
2610     }
2611 }
2612 
HAL_NSK2_ReadTSPDstInfo(MS_U32 pid_no)2613 void HAL_NSK2_ReadTSPDstInfo(MS_U32 pid_no)
2614 {
2615     MS_U32 pid_data;
2616     REG_PidFlt *pPidFlt = PPIDFLT1(pid_no);
2617 
2618     pid_data = TSP32_IdrR(pPidFlt);
2619 
2620     HALNSK2_DBG(NSK2_DBGLV_INFO,"dst_data = %x\n",pid_data);
2621 }
2622 
HAL_NSK2_AllTSPPidFilter(void)2623 void HAL_NSK2_AllTSPPidFilter(void)
2624 {
2625     MS_U32 i;
2626     for(i=0;i<0x100;i++)
2627     {
2628         HAL_NSK2_ReadTSPInfo(i);
2629     }
2630 
2631     for(i=0;i<0x100;i++)
2632     {
2633         HAL_NSK2_ReadTSPDstInfo(i);
2634     }
2635 }
2636 
HAL_NSK2_ChangePidFilter(MS_U32 pid_no,MS_U32 Data)2637 void HAL_NSK2_ChangePidFilter(MS_U32 pid_no, MS_U32 Data)
2638 {
2639     REG_PidFlt *pPidFlt = PPIDFLT0(pid_no);
2640     TSP32_IdrW(pPidFlt,Data);
2641 }
2642 
2643 
2644 
HAL_NSK2_ReadSwitchFromNSK2(void)2645 MS_U32 HAL_NSK2_ReadSwitchFromNSK2(void)
2646 {
2647 
2648 #if 0
2649 2.	N2_KteValid=1. NI bank offset 13 bit[3].
2650 3.	N2_KteDest=0. NI bank offset 13 bit[2:0].
2651 4.	Read the NSK bank offset 0xfc40-0xfc48 to read the NSK driven switch value. (see NSK-ICD-253 page21).
2652 In the read 96 bits, you can find:
2653 Bit[55] AU
2654 Bit[54] BU
2655 Bit[53] DU
2656 Bit[52] EU
2657 Bit[51] AL
2658 Bit[50] BL
2659 Bit[49] DL
2660 Bit[48] EL
2661 Bit[47:44] LocalSelectD
2662 Bit[43:40] LocalSelectS
2663 Bit[39:36] ESA select
2664 Bit[35:33] ESA subselect
2665 Please print out these fields and send the log to me.
2666 #endif
2667 
2668 #ifdef ReadSwitchInfoNSK2
2669     MS_U32 NI13 = NI_REG(13);
2670     HALNSK2_DBG(0,"NI13 = %x\n",NI13);
2671 
2672     MS_U32 NSK2Debug[3];
2673     NSK2Debug[0] = HAL_NSK2_ReadReg(0xFC40);
2674     NSK2Debug[1] = HAL_NSK2_ReadReg(0xFC44);
2675     NSK2Debug[2] = HAL_NSK2_ReadReg(0xFC48);
2676 
2677     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"NSK2Debug = (%x,%x,%x)\n",NSK2Debug[0],NSK2Debug[1],NSK2Debug[2]);
2678     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"AU=%x,BU=%x,DU=%x,EU=%x \n",(NSK2Debug[1]>>23)&0x1,(NSK2Debug[1]>>22)&0x1,(NSK2Debug[1]>>21)&0x1,(NSK2Debug[1]>>20)&0x1);
2679     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"AL=%x,BL=%x,DL=%x,EL=%x \n",(NSK2Debug[1]>>19)&0x1,(NSK2Debug[1]>>18)&0x1,(NSK2Debug[1]>>17)&0x1,(NSK2Debug[1]>>16)&0x1);
2680     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"LocalSelectD = %x, LocalSelectS = %x\n",(NSK2Debug[1]>>12)&0xf,(NSK2Debug[1]>>8)&0xf);
2681     HALNSK2_DBG(NSK2_DBGLV_DEBUG,"ESA select = %x, ESA subselect = %x\n",(NSK2Debug[1]>>4)&0xf,(NSK2Debug[1]>>0)&0xf);
2682 #endif
2683     return TRUE;
2684 }
2685 
HAL_NSK2_GetSRAlignBit(void)2686 MS_U32 HAL_NSK2_GetSRAlignBit(void)
2687 {
2688     return 16;
2689 }
2690 
2691 /* Temporary, since RSA sec_range drv is non-ready*/
HAL_NSK2_RSA_SetSecureRange(MS_U32 u32SecSet,MS_U32 u32SecStart,MS_U32 u32SecEnd,MS_BOOL bEnable)2692 MS_BOOL HAL_NSK2_RSA_SetSecureRange(MS_U32 u32SecSet, MS_U32 u32SecStart, MS_U32 u32SecEnd, MS_BOOL bEnable)
2693 {
2694     if(u32SecSet > REG_MPROT_SECRANGE_SET  || u32SecEnd <= u32SecStart)
2695         return FALSE;
2696 
2697     *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_START(u32SecSet)) = (u32SecStart & MPROT_SECRANGE_MASK);
2698 
2699     if (TRUE == bEnable)
2700     {
2701         *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_END(u32SecSet))   = REG_MPROT_SECRANGE_ENABLE & ( u32SecEnd   & MPROT_SECRANGE_MASK);
2702     }
2703     else
2704     {
2705         *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_END(u32SecSet))   = (~REG_MPROT_SECRANGE_ENABLE) | ( u32SecEnd   & MPROT_SECRANGE_MASK);
2706     }
2707 
2708     return TRUE;
2709 }
2710 /**
2711  * @brief Configure CA secure range mask
2712  */
HAL_NSK2_RSA_SetSR_Mask(MS_U32 u32DramSize)2713 void HAL_NSK2_RSA_SetSR_Mask(MS_U32 u32DramSize)
2714 {
2715     MS_U32 u32Mask = 0x00000000;
2716 
2717     if (0 != ((u32DramSize >> 16) >> 8))
2718     {
2719         u32Mask = (((u32DramSize >> 16) >> 8) - 1) & 0xFF;
2720 
2721         *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_MASK) = u32Mask;
2722     }
2723 }
2724 //=====================================
2725 //======   NSK2.1 new functions  ======
2726 //=====================================
2727 
2728 
HAL_NSK2_ReadAllOTP(void)2729 void HAL_NSK2_ReadAllOTP(void)
2730 {
2731     MS_U32 u32RetValue, addr;
2732 
2733     for( addr=0 ; addr<0x4000; addr+=4)
2734     {
2735         HAL_NSK2_OTP_Get(addr, 31, 0, &u32RetValue);
2736         //if( (addr&0xf) == 0)
2737         //    printf("\n");
2738 
2739         if(u32RetValue != 0)
2740         {
2741             HALNSK2_DBG(NSK2_DBGLV_INFO,"addr = %x, value = %x \n",addr, u32RetValue);
2742         }
2743     }
2744 }
2745 
HAL_NSK2_ReadKTEResp(void)2746 MS_U32 HAL_NSK2_ReadKTEResp(void)
2747 {
2748     StatusCheck(HAL_NSK2_CheckBusy());
2749 
2750     MS_U32 u32KTEResp;
2751     u32KTEResp = NI_REG(REG_NI_NSK2_KTE_RESP);
2752 
2753     HALNSK2_DBG(NSK2_DBGLV_INFO,"KTE_RESP = %x\n",u32KTEResp);
2754 
2755     return u32KTEResp;
2756 }
2757 
HAL_NSK2_GetPubOTP(MS_U8 * pPubOTP)2758 MS_BOOL HAL_NSK2_GetPubOTP(MS_U8 *pPubOTP)
2759 {
2760 #if 0
2761 Table 7 Public OTP Bits
2762 Bit 	Name 	Description
2763 127:64 	U_OTP_v_pubOtpUniqueID1 	OTP unique ID.
2764 This field is unique chip data that can be shared with other non-NDS modules.
2765 63:48 	U_OTP_v_PubOtpGP 	        General purpose OTP field.
2766 47:40 	U_OTP_v_PubOtpMinConfVer 	OTPpublic minimum configuration version.
2767 39:36 	U_OTP_v_PubOtpRsaIndex   	OTP public RSA index
2768 35:32 	U_OTP_v_PubOtpBID       	OTP public BlackBox ID
2769 31:16 	U_OTP_v_PubOtpVID        	OTP public version ID
2770 15:0 	U_OTP_v_PubOtpOID       	OTP public owner ID
2771 #endif
2772 
2773 //U_OTP_v_pubOtpUniqueID1           0x3DA8  63  0
2774 //U_OTP_v_PubOtpGP                  0x3B08  15  0
2775 //U_OTP_v_PubOtpMinConfVer          0x3B14   7  0
2776 //U_OTP_v_PubOtpRsaIndex            0x3B10   3  0
2777 //U_OTP_v_PubOtpBID                 0x3B0C   3  0
2778 //U_OTP_v_PubOtpVID                 0x3B04  15  0
2779 //U_OTP_v_PubOtpOID                 0x3B00  15  0
2780 
2781     MS_U8 *pRunPubOTP = (MS_U8 *)pPubOTP;
2782     MS_U32 ReadValue,ReadValue2;
2783 
2784     HAL_NSK2_OTP_Get(0x3B00, 15, 0, &ReadValue);
2785     HALNSK2_DBG(NSK2_DBGLV_INFO,"U_OTP_v_PubOtpOID = %x\n",ReadValue);
2786     memcpy(pRunPubOTP,&ReadValue,2);
2787     pRunPubOTP += 2;
2788 
2789     HAL_NSK2_OTP_Get(0x3B04, 15, 0, &ReadValue);
2790     HALNSK2_DBG(NSK2_DBGLV_INFO,"U_OTP_v_PubOtpVID = %x\n",ReadValue);
2791     memcpy(pRunPubOTP,&ReadValue,2);
2792     pRunPubOTP += 2;
2793 
2794     HAL_NSK2_OTP_Get(0x3B0C, 3, 0, &ReadValue);
2795     HALNSK2_DBG(NSK2_DBGLV_INFO,"U_OTP_v_PubOtpBID = %x\n",ReadValue);
2796     HAL_NSK2_OTP_Get(0x3B10, 3, 0, &ReadValue2);
2797     HALNSK2_DBG(NSK2_DBGLV_INFO,"U_OTP_v_PubOtpRsaIndex = %x\n",ReadValue2);
2798     *pRunPubOTP = (MS_U8)(ReadValue&0xF) + (MS_U8)((ReadValue2&0xF)<<4);
2799     pRunPubOTP ++;
2800 
2801     HAL_NSK2_OTP_Get(0x3B14, 7, 0, &ReadValue);
2802     HALNSK2_DBG(NSK2_DBGLV_INFO,"U_OTP_v_PubOtpMinConfVer = %x\n",ReadValue);
2803     memcpy(pRunPubOTP,&ReadValue,1);
2804     pRunPubOTP ++;
2805 
2806     HAL_NSK2_OTP_Get(0x3B08, 15, 0, &ReadValue);
2807     HALNSK2_DBG(NSK2_DBGLV_INFO,"U_OTP_v_PubOtpGP = %x\n",ReadValue);
2808     memcpy(pRunPubOTP,&ReadValue,2);
2809     pRunPubOTP += 2;
2810 
2811     HAL_NSK2_OTP_Get(0x3DA8, 31, 0, &ReadValue);
2812     HALNSK2_DBG(NSK2_DBGLV_INFO,"U_OTP_v_pubOtpUniqueID1 = %x\n",ReadValue);
2813     memcpy(pRunPubOTP,&ReadValue,4);
2814     pRunPubOTP += 4;
2815 
2816     HAL_NSK2_OTP_Get(0x3DAC, 31, 0, &ReadValue);
2817     memcpy(pRunPubOTP,&ReadValue,4);
2818     pRunPubOTP += 4;
2819 
2820     return TRUE;
2821 }
2822 
HAL_NSK21_InvalidCmChannel(MS_U16 PidSlot)2823 MS_U32 HAL_NSK21_InvalidCmChannel(MS_U16 PidSlot)
2824 {
2825     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s PidSlot = %x\n", __FUNCTION__,PidSlot);
2826 
2827     MS_U32 u32PidSlot = 0;
2828     StatusCheck(HAL_NSK2_CheckBusy());
2829 
2830     u32PidSlot = (((MS_U32)PidSlot<<NI_WriteTKey_PidNo_Shift)&NI_WriteTransportKey_PidNo);
2831     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_InvalidateCmChannel | u32PidSlot);
2832 
2833     return TRUE;
2834 }
2835 
2836 typedef enum
2837 {
2838     NSK21_DVBCSA2 = 0,
2839     NSK21_DVBCSA2_CONF,
2840     NSK21_DVBCSA3,
2841     NSK21_CPCM_LSA_MDI_CBC,
2842     NSK21_CPCM_LSA_MDI_RCBC,
2843     NSK21_CPCM_LSA_MDD_CBC,  //5
2844     NSK21_CPCM_LSA_MDD_RCBC,
2845     NSK21_SYNAMEDIA_AES,
2846     NSK21_AES_ECB_CLR,
2847     NSK21_CIPLUS_AES,
2848     NSK21_SCTE41_DES, //10
2849     NSK21_SCTE52_DES,
2850     NSK21_TDES_ECB_CLR,
2851     NSK21_MULTI2_TS,
2852     NSK21_TDES_ECB_CTS,
2853     NSK21_DES_ECB_CTS, //15
2854     NSK21_IDSA_AES,
2855 }NSK21_CMChAlgo_e;
2856 
HAL_NSK21_CfgCmChannel(MS_U16 PidSlot,MS_U16 LocalDAlgo,MS_U16 ESAAlgo,MS_U16 LocalSAlgo,MS_U16 SysKeyIndex)2857 MS_U32 HAL_NSK21_CfgCmChannel(MS_U16 PidSlot, MS_U16 LocalDAlgo, MS_U16  ESAAlgo, MS_U16 LocalSAlgo, MS_U16 SysKeyIndex)
2858 {
2859 
2860 #define Algoffset     100
2861     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_CfgCmChannel PidSlot = %x, LocalDAlgo = %x, ESAAlgo = %x, LocalSAlgo = %x, SysKeyIndex = %x\n",PidSlot, LocalDAlgo, ESAAlgo, LocalSAlgo, SysKeyIndex);
2862     StatusCheck(HAL_NSK2_CheckBusy());
2863     MS_U32 algorithms = 0, u32PidSlot = 0;
2864 
2865     if(LocalDAlgo < Algoffset)
2866         LocalDAlgo = 0;
2867     else
2868         LocalDAlgo -= Algoffset;
2869 
2870     if(ESAAlgo < Algoffset)
2871         ESAAlgo = 0;
2872     else
2873         ESAAlgo -= Algoffset;
2874 
2875     if(LocalSAlgo < Algoffset)
2876         LocalSAlgo = 0;
2877     else
2878         LocalSAlgo -= Algoffset;
2879 
2880 #if 0 //tmp solution...
2881     algorithms = ((LocalDAlgo - 100) & NI_KIW_LSAD_ALGO_MASK) +
2882                  (((ESAAlgo - 100) << NI_KIW_ESA_ALGO_SHIFT) & NI_KIW_ESA_ALGO_MASK) +
2883                  (((LocalSAlgo - 100) << NI_KIW_LSAS_ALGO_SHIFT) & NI_KIW_LSAS_ALGO_MASK) ;
2884 #else
2885     algorithms = (LocalDAlgo & NI_KIW_LSAD_ALGO_MASK) +
2886                  ((ESAAlgo << NI_KIW_ESA_ALGO_SHIFT) & NI_KIW_ESA_ALGO_MASK) +
2887                  ((LocalSAlgo << NI_KIW_LSAS_ALGO_SHIFT) & NI_KIW_LSAS_ALGO_MASK) ;
2888 #endif
2889 
2890     HALNSK2_DBG(NSK2_DBGLV_INFO,"algorithms = %x\n",algorithms);
2891     NI_REG(REG_NI_DSCMB_ALGO) = algorithms;
2892 
2893     u32PidSlot = (((MS_U32)PidSlot<<NI_WriteTKey_PidNo_Shift)&NI_WriteTransportKey_PidNo);
2894     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_ConfigureCmChannel | u32PidSlot);
2895     MsOS_DelayTaskUs(1);
2896     HALNSK2_DBG(NSK2_DBGLV_INFO,"REG_NI_DSCMB_ALGO = %x\n",NI_REG(REG_NI_DSCMB_ALGO));
2897 
2898     return TRUE;
2899 }
2900 
HAL_NSK21_WriteTransportKey(MS_U8 SCB,MS_U8 ForceSCB,void * pLocalDIV1,void * pLocalDIV2,void * pESAIV1,void * pESAIV2,void * pLocalSIV1,void * pLocalSIV2,MS_U16 PidSlot)2901 MS_U32 HAL_NSK21_WriteTransportKey(MS_U8 SCB, MS_U8 ForceSCB, void *pLocalDIV1, void *pLocalDIV2,
2902                                    void *pESAIV1, void *pESAIV2, void *pLocalSIV1, void *pLocalSIV2, MS_U16 PidSlot )
2903 {
2904     HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK21_WriteTransportKey PidSlot = %x, SCB = %x, ForceSCB = %x\n", PidSlot, SCB, ForceSCB);
2905 
2906     MS_U32 data,data2;
2907     //check NULL pointer or not....
2908 
2909     StatusCheck(HAL_NSK2_CheckBusy());
2910 
2911     data = NI_REG(REG_NI_KTE_STATUS);
2912     if( ( data & NI_KTE_DEST_MASK ) == 0  )
2913     {
2914 
2915 		ForceSCB = HAL_NSK2_SCBTransToHW(ForceSCB);
2916 
2917         HALNSK2_DBG(NSK2_DBGLV_INFO,"PidSlot: %x\n", PidSlot);
2918         data2 = (((MS_U32)PidSlot<<NI_WriteTKey_PidNo_Shift)&NI_WriteTransportKey_PidNo) +
2919                 (((MS_U32)SCB<<NI_WriteTKey_SCB_Shift) & NI_WriteTKey_SCB_MASK)
2920                 + ( ( (MS_U32)ForceSCB<<NI_WriteTKey_FSCB_Shift) & NI_WriteTKey_FSCB_MASK) ;
2921 
2922     #if 1
2923         NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteTransportKey | data2 | NI_OTP_ACK_NSK2 | NI_ERR_INVALID_SLOT);
2924     #else
2925         NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteTransportKey | data2);
2926     #endif
2927         HALNSK2_DBG(NSK2_DBGLV_INFO,"NI 6 = %x\n", NI_REG(REG_NI_COMMAND));
2928 
2929         StatusCheck(HAL_NSK2_KIW_BusyPolling());
2930     }
2931     else
2932     {
2933         HALNSK2_DBG(NSK2_DBGLV_ERR,"WriteTransportKey abnormal ignored: KteDest not zero\n");
2934         return HAL_NSK2_ReadKTEResp();
2935     }
2936 
2937     return HAL_NSK2_ReadKTEResp();
2938 }
2939 
HAL_NSK21_WriteM2MKey(MS_U32 M2MAlgo)2940 MS_U32 HAL_NSK21_WriteM2MKey(MS_U32 M2MAlgo)
2941 {
2942     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s M2MAlgo = %x\n",__FUNCTION__,M2MAlgo);
2943 
2944     MS_U32 algorithms;
2945 
2946     StatusCheck(HAL_NSK2_CheckBusy());
2947 
2948     algorithms = (M2MAlgo & NI_KIW_LSAD_ALGO_MASK) +
2949                  ((M2MAlgo << NI_KIW_LSAS_ALGO_SHIFT) & NI_KIW_LSAS_ALGO_MASK) ;
2950 
2951     HALNSK2_DBG(NSK2_DBGLV_INFO,"algorithms = %x\n",algorithms);
2952     NI_REG(REG_NI_DSCMB_ALGO) = algorithms;
2953     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteM2MKey);
2954 
2955     MsOS_DelayTaskUs(1);
2956 
2957     return HAL_NSK2_ReadKTEResp();
2958 }
2959 
HAL_NSK21_ModifyGenIn(MS_U32 MaskVal,MS_U32 XorVal)2960 MS_U32 HAL_NSK21_ModifyGenIn(MS_U32 MaskVal,MS_U32 XorVal)
2961 {
2962     MS_U32 GenIn = 0, WriteGenIn = 0;
2963 
2964     StatusCheck(HAL_NSK2_CheckBusy());
2965 
2966     GenIn = NI_REG(REG_NI_NSK2_REG_GENIN);
2967 
2968     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s GenIn = %x\n",__FUNCTION__,GenIn);
2969 
2970     GenIn = (GenIn&MaskVal);
2971 
2972     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s GenIn = %x, XorVal = %x\n",__FUNCTION__,GenIn,XorVal);
2973 
2974     WriteGenIn = GenIn^XorVal;
2975     HALNSK2_DBG(NSK2_DBGLV_INFO,"WriteGenIn = %x\n",WriteGenIn);
2976 
2977 #ifdef TestGenIn
2978     NI_REG(REG_NI_NSK21_GENIN) = WriteGenIn;
2979     MsOS_DelayTaskUs(10);
2980 #else
2981 
2982     //NI_REG(REG_NI_NSK21_GENIN) = ( (WriteGenIn & BMASK(9:0)) | (WriteGenIn & __BIT(13)) );
2983     NI_REG(REG_NI_NSK21_GENIN) = WriteGenIn;
2984 
2985     NI_REG(REG_NI_NSK21_CONCURR_PROT_EN) = ((WriteGenIn>>10) & BMASK(1:0));
2986     NI_REG(REG_NI_NSK21_CONCURR_SET) = ((WriteGenIn>>12) & BMASK(0:0));
2987     NI_REG(REG_NI_NSK21_GEN_SHOT) = ((WriteGenIn>>14) & BMASK(3:0));
2988 
2989     MsOS_DelayTaskUs(10);
2990 
2991     HALNSK2_DBG(NSK2_DBGLV_INFO,"CONCURR_PROT_EN = %x, CONCURR_SET = %x, GEN_SHOT = %x\n", NI_REG(REG_NI_NSK21_CONCURR_PROT_EN), NI_REG(REG_NI_NSK21_CONCURR_SET), NI_REG(REG_NI_NSK21_GEN_SHOT));
2992 #endif
2993     GenIn = NI_REG(REG_NI_NSK2_REG_GENIN);
2994 
2995     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s GenIn = %x\n",__FUNCTION__,GenIn);
2996 
2997     if(GenIn == WriteGenIn)
2998         return TRUE;
2999     else
3000         return FALSE;
3001 
3002 }
3003 
HAL_NSK21_GetGenIn(void)3004 MS_U32 HAL_NSK21_GetGenIn(void)
3005 {
3006     return NI_REG(REG_NI_NSK2_REG_GENIN);
3007 }
3008 
HAL_NSK21_ReadNIReg(MS_U32 offset)3009 MS_U32 HAL_NSK21_ReadNIReg(MS_U32 offset)
3010 {
3011     return NI_REG(offset);
3012 }
3013 
HAL_NSK21_WriteNIReg(MS_U32 offset,MS_U32 Value)3014 void HAL_NSK21_WriteNIReg(MS_U32 offset, MS_U32 Value)
3015 {
3016     NI_REG(offset) = Value;
3017 }
3018 
HAL_NSK21_WriteJTagKey(MS_U32 OverrideOid,MS_U32 Select)3019 MS_U32 HAL_NSK21_WriteJTagKey(MS_U32 OverrideOid, MS_U32 Select)
3020 {
3021     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s OverrideOid = %x, Select = %x\n",__FUNCTION__,OverrideOid,Select);
3022 
3023     MS_U32 functionality = 0, OID = 0;
3024 
3025     StatusCheck(HAL_NSK2_CheckBusy());
3026 
3027     if(OverrideOid>1) //invalid OverrideOid
3028     {
3029         HALNSK2_DBG(NSK2_DBGLV_ERR,"invalid OverrideOid\n");
3030         return FALSE;
3031     }
3032 
3033     OID = (OverrideOid << NI_KIW_OID_SHIFT) & NI_KIW_OID_MASK ;
3034 #if 0
3035     functionality =  (Select << NI_WriteTKey_PidNo_Shift) & NI_WriteTransportKey_PidNo ;
3036 #else
3037     functionality = 0; //N21_EJTAG_PWD
3038 #endif
3039 
3040     HALNSK2_DBG(NSK2_DBGLV_INFO,"functionality = %x\n",functionality);
3041     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteJTAGKey | functionality | OID);
3042 
3043     MsOS_DelayTaskUs(1);
3044 
3045     return HAL_NSK2_ReadKTEResp();
3046 }
3047 
HAL_NSK21_IncrementNvCounter(void)3048 MS_U32 HAL_NSK21_IncrementNvCounter(void)
3049 {
3050     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n",__FUNCTION__);
3051 
3052     StatusCheck(HAL_NSK2_CheckBusy());
3053 
3054     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_IncrementNvCounter);
3055     MsOS_DelayTaskUs(1);
3056 
3057     return HAL_NSK2_ReadKTEResp();
3058 }
3059 
HAL_NSK2_WriteOtpKey(void)3060 MS_U32 HAL_NSK2_WriteOtpKey(void)
3061 {
3062     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n",__FUNCTION__);
3063     StatusCheck(HAL_NSK2_CheckBusy());
3064 
3065     NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteOTPKey);
3066     MsOS_DelayTask(10);
3067 
3068     return HAL_NSK2_ReadKTEResp();
3069 }
3070 
HAL_NSK2_ReadPWD_Status(void)3071 MS_U32 HAL_NSK2_ReadPWD_Status(void)
3072 {
3073     HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n",__FUNCTION__);
3074     StatusCheck(HAL_NSK2_CheckBusy());
3075     MS_U32 RegPwd;
3076 
3077     RegPwd = NI_REG(REG_NI_NSK2_PWD_ON);
3078 
3079     if(RegPwd & N2_JTAGPWD0_ON)
3080     {
3081         HALNSK2_DBG(NSK2_DBGLV_INFO,"JTAG PWD0 ON \n");
3082     }
3083 
3084     if(RegPwd & N2_JTAGPWD1_ON)
3085     {
3086         HALNSK2_DBG(NSK2_DBGLV_INFO,"JTAG PWD1 ON \n");
3087     }
3088 
3089     if(RegPwd & N2_SCANPWD_ON)
3090     {
3091         HALNSK2_DBG(NSK2_DBGLV_INFO,"SCAN PWD ON \n");
3092     }
3093 
3094     if(RegPwd & N2_MBISTPWD_ON)
3095     {
3096         HALNSK2_DBG(NSK2_DBGLV_INFO,"MBIST PWD ON \n");
3097     }
3098 
3099     if(RegPwd & N2_M2MKEY_ON)
3100     {
3101         HALNSK2_DBG(NSK2_DBGLV_INFO,"M2M KEY ON \n");
3102     }
3103 
3104     if(RegPwd & N2_SCPUKEY0_ON)
3105     {
3106         HALNSK2_DBG(NSK2_DBGLV_INFO,"SCPU KEY0 ON \n");
3107     }
3108 
3109     if(RegPwd & N2_SCPUKEY1_ON)
3110     {
3111         HALNSK2_DBG(NSK2_DBGLV_INFO,"SCPU KEY1 ON \n");
3112     }
3113 
3114     if(RegPwd & N2_RNGVALUE0_ON)
3115     {
3116         HALNSK2_DBG(NSK2_DBGLV_INFO,"RNG VALUE0 ON \n");
3117     }
3118 
3119     if(RegPwd & N2_RNGVALUE1_ON)
3120     {
3121         HALNSK2_DBG(NSK2_DBGLV_INFO,"RNG VALUE1 ON \n");
3122     }
3123 
3124     return TRUE;
3125 }
3126 
HAL_NSK2_CtrlClk(MS_BOOL Enable)3127 MS_U32 HAL_NSK2_CtrlClk(MS_BOOL Enable)
3128 {
3129     if(Enable)
3130     {
3131         HALNSK2_DBG(NSK2_DBGLV_INFO,"enable NSK clock\n");
3132         NI_REG(REG_NI_NSK2_CTRL) |= (NI_NSK2_CLK_ENABLE | NI_NSK2_RESET_DISABLE);
3133         HAL_NSK2_ColdReset();
3134     }
3135     else
3136     {
3137         HALNSK2_DBG(NSK2_DBGLV_INFO,"disable NSK clock\n");
3138         NI_REG(REG_NI_NSK2_CTRL) &= ~(NI_NSK2_CLK_ENABLE | NI_NSK2_RESET_DISABLE);
3139         _gReset = FALSE;
3140         _gCheckBusyFlag = FALSE;
3141     }
3142 
3143     MsOS_DelayTask(1);
3144 
3145 
3146     HALNSK2_DBG(NSK2_DBGLV_INFO,"REG_NI_NSK2_CTRL = %x\n",NI_REG(REG_NI_NSK2_CTRL));
3147     return TRUE;
3148 }
3149 
HAL_NSK2_ReadClkStatus(void)3150 MS_U32 HAL_NSK2_ReadClkStatus(void)
3151 {
3152     MS_U32 status;
3153     MS_U32 rom[4];
3154 
3155     status = (NI_REG(REG_NI_KTE_STATUS)& NI_SLOW_CLOCK_DETECT);
3156 
3157     HALNSK2_DBG(NSK2_DBGLV_INFO,"REG_NI_KTE_STATUS = %x\n",NI_REG(REG_NI_KTE_STATUS));
3158     HALNSK2_DBG(NSK2_DBGLV_INFO,"NI_SLOW_CLOCK_DETECT = %x\n", (status>>5));
3159 
3160     if(status)
3161     {
3162         HALNSK2_DBG(NSK2_DBGLV_INFO,"no clock for NSK\n");
3163     }
3164     else
3165     {
3166         HALNSK2_DBG(NSK2_DBGLV_INFO,"clock is alive for NSK\n");
3167     }
3168 
3169     HALNSK2_DBG(NSK2_DBGLV_INFO,"read NSK ROM value\n");
3170     HAL_NSK2_ReadData32(0x8000, 4, &rom[0]);
3171 
3172     HALNSK2_DBG(NSK2_DBGLV_INFO,"%x, %x, %x, %x,\n",rom[0],rom[1],rom[2],rom[3]);
3173 
3174     return TRUE;
3175 }
3176 
3177