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 printf("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 printf("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 printf("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 printf("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 printf("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 printf("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 printf("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
1878 {0x40, 1, 0x3B20, 27, 24}, //U_OTP_allow_illegalNDSFlagChk
1879 {0x41, 1, 0x3B24, 3, 0}, //U_OTP_ena_ACPUUseNSK2
1880 {0x42, 1, 0x3B24, 7, 4}, //U_OTP_ena_DBUSUseNSK2
1881 {0x43, 1, 0x3B24, 11, 8}, //U_OTP_ena_ForceOneMilSec
1882 {0x44, 1, 0x3B24, 15, 12}, //U_OTP_allow_SCCheck
1883 {0x45, 1, 0x3B24, 19, 16}, //U_OTP_ena_TestRCFreq
1884 {0x46, 1, 0x3B24, 23, 20}, //U_OTP_ena_SWRN
1885 {0x47, 1, 0x3B24, 27, 24}, //U_OTP_ena_NSKSeedPRNG
1886 {0x48, 1, 0x3B24, 31, 28}, //U_OTP_OTPWritePWDProtect
1887 {0x49, 1, 0x3B28, 11, 8}, //U_OTP_ena_EMMFilter
1888 {0x4A, 1, 0x3B28, 15, 12}, //U_OTP_ena_TestGenIN
1889 {0x4B, 1, 0x3B28, 7, 6}, //U_OTP_allow_NSK2_PWD_Mode
1890 {0x4C, 1, 0x3B2C, 3, 2}, //U_OTP_allow_NDSSC_ReadFail_BadPkt
1891 {0x4D, 1, 0x3B2C, 5, 4}, //U_OTP_allow_RANDOM_keybus
1892 {0x4E, 1, 0x3B2C, 7, 6}, //U_OTP_allow_RANDOM_byteacc
1893 {0x4F, 1, 0x3B2C, 9, 8}, //U_OTP_allow_NDS_Rd55AA
1894 {0x50, 1, 0x3B2C, 11, 10}, //U_OTP_allow_NDS_Parity_chk
1895 {0x51, 1, 0x3B2C, 15, 14}, //U_OTP_forbid_OTPBuiltInTest
1896 {0x52, 1, 0x3C58, 3, 0}, //U_OTP_ena_ESAAlgo_invalidate
1897 {0x53, 1, 0x3C58, 7, 4}, //U_OTP_ena_LocalAlgo_Invalidate
1898 {0x54, 1, 0x3C58, 11, 8}, //U_OTP_ContentProtEn
1899 {0x55, 1, 0x3C58, 13, 12}, //U_OTP_concurrency_configuration
1900 {0x56, 1, 0x3C58, 15, 14}, //U_OTP_allow_NSK_RNG_ROSC
1901 {0x57, 1, 0x3C58, 16, 16}, //U_OTP_nds_fc_disable
1902 {0x58, 1, 0x3C58, 17, 17}, //U_OTP_NDS_CPNR0_sel
1903 {0x59, 1, 0x3C58, 18, 18}, //U_OTP_NDS_CPNR_off
1904 {0x5A, 1, 0x3C60, 0, 0}, //U_OTP_forbid_CLK_SEED_TEST
1905 {0x5B, 1, 0x3C60, 1, 1}, //U_OTP_MOBF_TOP_use_DES
1906 {0x5C, 1, 0x3C78, 3, 0}, //U_OTP_ena_PVR_secure_protect_0
1907 {0x5D, 1, 0x3C74, 3, 0}, //U_OTP_ena_PVRNS2S
1908 {0x5E, 1, 0x3C80, 17, 16}, //U_OTP_forbid_SW_SPSD_Key
1909 {0x5F, 1, 0x3C80, 19, 18}, //U_OTP_forbid_KL_SPSD_Key
1910 {0x60, 1, 0x3CC0, 1, 0}, //U_OTP_forbid_ACPUWriteOTP
1911 {0x61, 1, 0x3CC0, 3, 2}, //U_OTP_forbid_ACPUReadOTP
1912 {0x62, 1, 0x3CC0, 5, 4}, //U_OTP_forbid_SCPUWriteOTP
1913 {0x63, 1, 0x3CC0, 7, 6}, //U_OTP_forbid_SCPUReadOTP
1914 {0x64, 1, 0x3CC0, 9, 8}, //U_OTP_forbid_DBBUSWriteOTP
1915 {0x65, 1, 0x3CC0, 11, 10}, //U_OTP_forbid_DBBUSReadOTP
1916 {0x66, 1, 0x3CC0, 13, 12}, //U_OTP_allow_RANDOM
1917 {0x67, 1, 0x3CC0, 15, 14}, //U_OTP_allow_NOISE_Rd
1918 {0x68, 1, 0x3CC8, 3, 0}, //U_OTP_SBoot
1919 {0x69, 1, 0x3CC8, 7, 4}, //U_OTP_SecretAreaEnable
1920 {0x6A, 2, 0x3CCC, 8, 0}, //U_OTP_SCAN_MODE
1921 {0x6B, 1, 0x3CCC, 15, 10}, //U_OTP_MBIST_MODE
1922 {0x6C, 1, 0x3CCC, 21, 16}, //U_OTP_I2C_MODE
1923 {0x6D, 1, 0x3CCC, 31, 26}, //U_OTP_EJTAG_MODE
1924 {0x6E, 1, 0x3C68, 3, 0}, //U_OTP_forbid_USBSlaveMode
1925 {0x6F, 1, 0x3CD0, 9, 8}, //U_OTP_allow_DRAM_MOBF
1926 {0x70, 1, 0x3CD4, 13, 8}, //U_OTP_BootMode
1927 {0x71, 1, 0x3CD4, 21, 16}, //U_OTP_SEPBootMode
1928 {0x72, 2, 0x3CD8, 23, 12}, //U_OTP_PostMskAreaRange1
1929 {0x73, 2, 0x3CE0, 23, 16}, //U_OTP_BootDevice
1930 {0x74, 1, 0x3D24, 11, 8}, //U_OTP_ena_NSK2
1931 {0x75, 1, 0x3D24, 15, 14}, //U_OTP_ena_DMA
1932 {0x76, 1, 0x3D24, 19, 16}, //U_OTP_ena_SEP
1933 {0x77, 1, 0x3D40, 21, 18}, //U_OTP_allow_SPSSPDKeyShuffle
1934 {0x78, 1, 0x3D44, 3, 0}, //U_OTP_ena_ACPU2KT
1935 {0x79, 1, 0x3D44, 7, 4}, //U_OTP_ena_NSK2KT
1936 {0x7A, 1, 0x3D44, 27, 24}, //U_OTP_ena_NSKCW2CryptoDMA
1937 {0x7B, 1, 0x3D44, 31, 28}, //U_OTP_ena_ACPU2LSA
1938 {0x7C, 1, 0x3CD0, 31, 26}, //U_OTP_I2C2TEE_MODE
1939 {0x7D, 1, 0x3CC8, 19, 16}, //U_OTP_SecR2_Sboot
1940 {0x7E, 1, 0x3C88, 27, 24}, //U_OTP_NDS_CODE_INTEGRITY_RSA_KEY_SEL
1941 {0x7F, 1, 0x3CC8, 27, 24}, //U_OTP_xCPU_Sboot
1942 };
1943
1944
endian_change(MS_U8 * ptr,MS_U32 bytes)1945 void endian_change(MS_U8* ptr, MS_U32 bytes)
1946 {
1947
1948 MS_U32 u32halve;
1949 MS_U32 u32tmp;
1950 MS_U32 i;
1951
1952 u32halve = bytes/2;
1953 for(i=0; i<u32halve; i++)
1954 {
1955 u32tmp = ptr[i];
1956 ptr[i] = ptr[bytes-i-1];
1957 ptr[bytes-i-1] = u32tmp;
1958 }
1959 }
1960
HAL_NSK2_GetOTPProperties(MS_U32 * desc_size,MS_U8 * desc)1961 MS_U32 HAL_NSK2_GetOTPProperties(MS_U32 *desc_size, MS_U8 *desc)
1962 {
1963 MS_U32 NumOfTag = sizeof(K6liteAsst)/sizeof(ASST_t);
1964 MS_U32 ret_size = 0, i, j;
1965 MS_U8 *pDesc = (MS_U8 *)desc;
1966 MS_U32 OTPValue;
1967 MS_U8 RunLens;
1968 MS_U32 RunOffset;
1969
1970 ret_size = 0;
1971 for( i=0; i<NumOfTag; i++)
1972 {
1973 ret_size += (K6liteAsst[i].u8Length + 2);
1974 }
1975
1976 *desc_size = ret_size;
1977
1978 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"length of OTP Tag = %x\n",ret_size);
1979 if(desc == NULL)
1980 {
1981 return TRUE;
1982 }
1983
1984 for( i=0; i<NumOfTag; i++)
1985 {
1986 *pDesc++ = K6liteAsst[i].u8Tag;
1987 *pDesc++ = K6liteAsst[i].u8Length;
1988
1989 if(0x20 == K6liteAsst[i].u8Tag) //pubOTP...special case....
1990 {
1991 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"begin of Tag 0x20\n");
1992 *pDesc++ = 0;
1993 //4 Chip ID
1994 MS_U8 PubOTP[8];
1995 for(j=0;j<8;j+=4)
1996 {
1997 MS_U32 u32Tmp;
1998 u32Tmp = MDrv_CA_OTP_Read(K6liteAsst[i].Offset + j);
1999 memcpy(&PubOTP[j], &u32Tmp, 4);
2000 }
2001
2002
2003 endian_change(PubOTP, 8);
2004
2005 memcpy(pDesc,PubOTP,8);
2006 pDesc += 8;
2007
2008 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"end of Tag 0x20\n");
2009 }
2010 else if (0x25 == K6liteAsst[i].u8Tag) //NV Counter...special case....
2011 {
2012 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"begin of Tag 0x25\n");
2013 MS_U32 MaxNVCounter = 1024;
2014 *pDesc++ = (MS_U8)((MaxNVCounter>>24)&0xff);
2015 *pDesc++ = (MS_U8)((MaxNVCounter>>16)&0xff);
2016 *pDesc++ = (MS_U8)((MaxNVCounter>>8)&0xff);
2017 *pDesc++ = (MS_U8)(MaxNVCounter&0xff);
2018
2019 *pDesc++ = 0;
2020 *pDesc++ = 0;
2021 *pDesc++ = 0;
2022 *pDesc++ = 0;
2023
2024 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"end of Tag 0x25\n");
2025 }
2026 else
2027 {
2028 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"begin of Tag 0x%x\n",K6liteAsst[i].u8Tag);
2029 RunLens = K6liteAsst[i].u8Length;
2030 RunOffset = K6liteAsst[i].Offset;
2031 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"Offset = %x, length = %x\n",RunOffset, RunLens);
2032 if(K6liteAsst[i].u8Length <= 4)
2033 {
2034 HAL_NSK2_OTP_Get(K6liteAsst[i].Offset, K6liteAsst[i].MSB, K6liteAsst[i].LSB, &OTPValue);
2035 if(1 == RunLens)
2036 *pDesc++ = (MS_U8)(OTPValue&0xff);
2037 else if(2 == RunLens)
2038 {
2039 *pDesc++ = (MS_U8)((OTPValue>>8)&0xff);
2040 *pDesc++ = (MS_U8)(OTPValue&0xff);
2041 }
2042 else if(4 == RunLens)
2043 {
2044 *pDesc++ = (MS_U8)((OTPValue>>24)&0xff);
2045 *pDesc++ = (MS_U8)((OTPValue>>16)&0xff);
2046 *pDesc++ = (MS_U8)((OTPValue>>8)&0xff);
2047 *pDesc++ = (MS_U8)(OTPValue&0xff);
2048 }
2049 }
2050 else //length > 4, tmp solution
2051 {
2052 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"length = %x\n",RunLens);
2053 MS_U8 BulkData[16];
2054 for( j=0; j<RunLens; j+=4 )
2055 {
2056 HAL_NSK2_OTP_Get(RunOffset + j, 31, 0, &OTPValue);
2057 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"run offset = %x, Value = %x\n", (RunOffset + j),OTPValue);
2058 memcpy(&BulkData[j],&OTPValue,4);
2059 }
2060 endian_change(BulkData, 16);
2061 memcpy(pDesc,BulkData,16);
2062 pDesc += 16;
2063 }
2064
2065 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"end of Tag 0x%x\n",K6liteAsst[i].u8Tag);
2066 }
2067 }
2068
2069 #if 0
2070 MS_U32 size = *desc_size;
2071
2072 for(i=0;i<size;i++)
2073 {
2074 printf(" (%d, %x) \n", i , desc[i]);
2075 }
2076 #endif
2077 return TRUE;
2078 }
2079
2080
2081
2082 typedef struct
2083 {
2084 MS_U32 Offset;
2085 MS_U32 MSB;
2086 MS_U32 LSB;
2087 MS_U32 Bits;
2088 } ChipOTPCfg_t;
2089
2090
2091 static ChipOTPCfg_t K6ChipCfg[] =
2092 {
2093 {0x3B20, 7, 0, 8}, //[127:120] U_OTP_forbid_NSK_wr_sck[7:0] 11111111
2094 {0x3B20, 15, 8, 8}, //[119:112] U_OTP_UseCheckSum[7:0] 11111111
2095 {0x3B20, 17, 16, 2}, //[111:110] U_OTP_allow_NDSKey_BlankChk[1:0] 11
2096 {0x3B20, 19, 18, 2}, //[109:108] U_OTP_allow_ProgFail_RuinNDSKey[1:0] 11
2097 {0x3B20, 21, 20, 2}, //[107:106] U_OTP_allow_NDSReadKeyWait200ms[1:0] 11
2098 {0x3B20, 23, 22, 2}, //[105:104] U_OTP_allow_ReadErrorRstOtp[1:0] 11
2099 {0x3B20, 27, 24, 4}, //[103:100] U_OTP_allow_illegalNDSFlagChk[3:0] 1111
2100 {0x3B20, 31, 28, 4}, //[99:96] U_OTP_allow_Rst_NDS_SCFlag_ParityFail[3:0] 1111
2101 {0x3B24, 3, 0, 4}, //[95:92] U_OTP_ena_ACPUUseNSK2[3:0] 0001
2102 {0x3B28, 7, 6, 2}, //[91:90] U_OTP_allow_NSK2_PWD_Mode[1:0] 11
2103 {0x3B2C, 15, 14, 2}, //[[89:88] U_OTP_forbid_OTPBuiltInTest[1:0] 11
2104 {0x3B2C, 17, 16, 2}, //[87:86] U_OTP_forbid_Kilo_ProgRepair[1:0] 11
2105 {0x3C50, 1, 0, 2}, //[85:84] U_OTP_NDS_ESCK_Key1_obfuscation[1:0] 11
2106 {0x3C50, 3, 2, 2}, //[83:82] U_OTP_NDS_Key1_integrity_chk[1:0] 11
2107 {0x3C50, 5, 4, 2}, //[81:80] U_OTP_NDS_ESCK_Key2_obfuscation[1:0] 11
2108 {0x3C50, 7, 6, 2}, //[79:78] U_OTP_NDS_Key2_integrity_chk[1:0] 11
2109 {0x3C50, 9, 8, 2}, //[77:76] U_OTP_NDS_ESCK_Key3_obfuscation[1:0] 11
2110 {0x3C50, 11, 10, 2}, //[75:74] U_OTP_NDS_Key3_integrity_chk[1:0] 11
2111 {0x3C50, 13, 12, 2}, //[73:72] U_OTP_NDS_ESCK_Key4_obfuscation[1:0] 11
2112 {0x3C50, 15, 14, 2}, //[71:70] U_OTP_NDS_Key4_integrity_chk[1:0] 11
2113 {0x3C50, 17, 16, 2}, //[69:68] U_OTP_NDS_ESCK_Key5_obfuscation[1:0] 11
2114 {0x3C50, 19, 18, 2}, //[67:66] U_OTP_NDS_Key5_integrity_chk[1:0] 11
2115 {0x3C50, 21, 20, 2}, //[65:64] U_OTP_NDS_ESCK_Key6_obfuscation[1:0] 11
2116 {0x3C50, 23, 22, 2}, //[63:62] U_OTP_NDS_Key6_integrity_chk[1:0] 11
2117 {0x3C50, 25, 24, 2}, //[61:60] U_OTP_NDS_ESCK_Key7_obfuscation[1:0] 11
2118 {0x3C50, 27, 26, 2}, //[59:58] U_OTP_NDS_Key7_integrity_chk[1:0] 11
2119 {0x3C50, 29, 28, 2}, //[57:56] U_OTP_NDS_ESCK_Key8_obfuscation[1:0] 11
2120 {0x3C50, 31, 30, 2}, //[55:54] U_OTP_NDS_Key8_integrity_chk[1:0] 11
2121 {0x3C58, 11, 8, 4}, //[53:50] U_OTP_ContentProtEn[3:0] 1111
2122 {0x3C58, 13, 12, 2}, //[49:48] U_OTP_concurrency_configuration[1:0] 10
2123 {0x3C58, 15, 14, 2}, //[47:46] U_OTP_allow_NSK_RNG_ROSC[1:0] 11
2124 {0x3C60, 0, 0, 1}, //[45] U_OTP_forbid_CLK_SEED_TEST 1
2125 {0x3C60, 3, 2, 2}, //[44:43] U_OTP_OBFUSCATEVideoStream[1:0] 00
2126 {0x3C78, 3, 0, 4}, //[42:39] U_OTP_ena_PVR_secure_protect_0[3:0] 0001
2127 {0x3C78, 11, 8, 4}, //[38:35] U_OTP_dis_TSO[3:0] 0001
2128 {0x3CC0, 13, 12, 2}, //[34:33] U_OTP_allow_RANDOM[1:0] 11
2129 {0x3CC8, 3, 0, 4}, //[32:29] U_OTP_SBoot[3:0] 0000
2130 {0x3C68, 3, 0, 4}, //[28:25] U_OTP_forbid_USBSlaveMode[3:0] 1111
2131 {0x3CD0, 9, 8, 2}, //[24:23] U_OTP_allow_DRAM_MOBF[1:0] 00
2132 {0x3CD0, 18, 16, 3}, //[22:20] U_OTP_forbid_clk_otp_sel[2:0] 111
2133 {0x3CE0, 31, 30, 2}, //[19:18] U_OTP_forbid_STR[1:0] 00
2134 {0x3D24, 11, 8, 4}, //[17:14] U_OTP_ena_NSK2[3:0] 0001
2135 {0x3D24, 15, 14, 2}, //[13:12] U_OTP_ena_DMA[1:0] 01
2136 {0x3D24, 19, 16, 4}, //[11:8] U_OTP_ena_SCPU[3:0] 0001
2137 {0x3D44, 3, 0, 4}, //[7:4] U_OTP_ena_ACPU2KT[3:0] 0001
2138 {0x3D44, 7, 4, 4}, //[3:0] U_OTP_ena_NSK2KT[3:0] 0001
2139
2140 {0x3D44, 19, 16, 4}, //[127:124] U_OTP_ena_ACPUWrNSKKey2KT[3:0] 0111
2141 {0x3D50, 3, 0, 4}, //[123:120] U_OTP_ena_LSACPCM[3:0] 0001
2142 {0x3D50, 7, 4, 4}, //[119:116] U_OTP_ena_AESBasedCipher[3:0] 0001
2143 {0x3D50, 11, 8, 4}, //[115:112] U_OTP_ena_DESBasedCipher[3:0] 0001
2144 {0x3D50, 15, 12, 4}, //[111:108] U_OTP_ena_3DESBasedCipher[3:0] 0001
2145 {0x3D50, 19, 16, 4}, //[107:104] U_OTP_ena_Multi2BasedCipher[3:0] 0001
2146 {0x3D50, 23, 20, 4}, //[103:100] U_OTP_ena_DVBCSA2ConfCipher[3:0] 0001
2147 {0x3D50, 27, 24, 4}, //[99:96] U_OTP_ena_DVBCSA2Cipher[3:0] 0001
2148 {0x3D50, 31, 28, 4}, //[95:92] U_OTP_ena_DVBCSA3BasedCipher[3:0] 0001
2149 {0x3D54, 31, 0, 32}, //[91:28] U_OTP_Ch_SwitchComb[63:0] 1111111111111111111111111111111111111111111111111111111111111111
2150 {0x3D58, 31, 0, 32}, //[91:28] U_OTP_Ch_SwitchComb[63:0] 1111111111111111111111111111111111111111111111111111111111111111
2151 {0x3D5C, 3, 0, 4}, //[27:24] U_OTP_ena_LSAD_MDI[3:0] 0001
2152 {0x3D5C, 7, 4, 4}, //[23:20] U_OTP_ena_LSAD_MDD[3:0] 0001
2153 {0x3D5C, 11, 8, 4}, //[19:16] U_OTP_ena_LSAD_CIPLUS_AES[3:0] 0001
2154 {0x3D5C, 15, 12, 4}, //[15:12] U_OTP_ena_LSAD_AES_ECB_CLEAR[3:0] 0001
2155 {0x3D5C, 19, 16, 4}, //[11:8] U_OTP_ena_LSAD_SCTE41_SCTE52_DES[3:0] 0001
2156 {0x3D60, 3, 0, 4}, //[7:4] U_OTP_ena_ESA_CIPLUS_AES[3:0] 0001
2157 {0x3D60, 7, 4, 4}, //[3:0] U_OTP_ena_ESA_CIPLUS_DES[3:0] 0001
2158
2159 {0x3D60, 11, 8, 4}, //[127:124] U_OTP_ena_ESA_SCTE52_DES[3:0] 0001
2160 {0x3D60, 15, 12, 4}, //[123:120] U_OTP_ena_ESA_tDES_CBC_CLEAR[3:0] 0001
2161 {0x3D64, 3, 0, 4}, //[119:116] U_OTP_ena_LSAS_MDI[3:0] 0001
2162 {0x3D64, 7, 4, 4}, //[115:112] U_OTP_ena_LSAS_MDD[3:0] 0001
2163 {0x3D64, 11, 8, 4}, //[111:108] U_OTP_ena_LSAS_CIPLUS_AES[3:0] 0001
2164 {0x3D64, 15, 12, 4}, //[107:104] U_OTP_ena_LSAS_AES_ECB_CLEAR[3:0] 0001
2165 {0x3D64, 19, 16, 4}, //[103:100] U_OTP_ena_LSAS_SCTE41_SCTE52_DES[3:0] 0001
2166 {0x3D68, 7, 4, 4}, //[99:96] U_OTP_ena_ReviewFailPkt[3:0] 0001
2167 {0x3D68, 11, 8, 4}, //[95:92] U_OTP_dis_NonSecRangeEncrypt[3:0] 0001
2168 {0x3D6C, 3, 0, 4}, //[91:88] U_OTP_ena_CA_PVR_secure_protect_0[3:0] 0001
2169 {0x3D6C, 19, 16, 4}, //[87:84] U_OTP_ena_LowerPathRec[3:0] 0111
2170 {0x3FA0, 9, 8, 2}, //[83:82] U_OTP_I2C_PWD_obfuscation[1:0] 11
2171 {0x3FA0, 11, 10, 2}, //[81:80] U_OTP_EJTAG_PWD_obfuscation[1:0] 11
2172 {0x3FA0, 13, 12, 2}, //[79:78] U_OTP_SCAN_PWD_obfuscation[1:0] 11
2173 {0x3FA0, 15, 14, 2}, //[77:76] U_OTP_MBIST_PWD_obfuscation[1:0] 11
2174 //[63:0] 0 0000000000000000000000000000000000000000000000000000000000000000
2175
2176 };
2177
2178 #define FullChipConfigSize 0x27
2179
HAL_NSK2_GetFullChipSize(void)2180 MS_U32 HAL_NSK2_GetFullChipSize(void)
2181 {
2182 return FullChipConfigSize;
2183 }
2184
HAL_NSK2_GetFullChipConfig(MS_U32 * desc_size,MS_U8 * desc)2185 MS_U32 HAL_NSK2_GetFullChipConfig(MS_U32 *desc_size, MS_U8 *desc)
2186 {
2187 *desc_size = FullChipConfigSize;
2188 if(desc == NULL)
2189 {
2190 return TRUE;
2191 }
2192
2193 MS_U32 i, j, remain_bits, shift_bits;
2194 MS_U32 NumOfConfigs = sizeof(K6ChipCfg)/sizeof(ChipOTPCfg_t);
2195 MS_U32 TotalOTPValue[100];
2196
2197 memset(desc, 0x0, FullChipConfigSize);
2198 memset(TotalOTPValue, 0x0, 100*sizeof(MS_U32));
2199 //desc[0x40-1] = 0x28;
2200 //desc[0x28] = 0x80;
2201
2202 for(i=0; i<NumOfConfigs; i++)
2203 {
2204 HAL_NSK2_OTP_Get(K6ChipCfg[i].Offset, K6ChipCfg[i].MSB, K6ChipCfg[i].LSB, &TotalOTPValue[i]);
2205 printf("%d, %x\n",i, TotalOTPValue[i]);
2206 }
2207
2208 j = 0;
2209 remain_bits = 8;
2210 for(i=0; i<NumOfConfigs; i++)
2211 {
2212 if(K6ChipCfg[i].Bits>remain_bits)
2213 {
2214 if(K6ChipCfg[i].Bits == 32)
2215 {
2216 if(remain_bits != 8)
2217 {
2218 shift_bits = 8-remain_bits;
2219 desc[j] |= 0xf;
2220 j++;
2221 desc[j] = 0xff;
2222 j++;
2223 desc[j] = 0xff;
2224 j++;
2225 desc[j] = 0xff;
2226 j++;
2227 desc[j] = 0xf0;
2228 remain_bits = 4;
2229 }
2230 else
2231 {
2232 memcpy(&desc[j], &TotalOTPValue[i], 4);
2233 j += 4;
2234 remain_bits = 0;
2235 }
2236 }
2237 else
2238 {
2239
2240 shift_bits = K6ChipCfg[i].Bits-remain_bits;
2241 //printf("not equal case (%d), shift_bits = %x\n",i,shift_bits);
2242 desc[j] |= (TotalOTPValue[i] >> shift_bits);
2243
2244 j++;
2245
2246 remain_bits = (8-shift_bits);
2247 desc[j] |= (( TotalOTPValue[i] << remain_bits ) & 0xff); }
2248
2249 }
2250 else
2251 {
2252 remain_bits -= K6ChipCfg[i].Bits;
2253
2254 desc[j] |= (TotalOTPValue[i] << (remain_bits));
2255
2256 }
2257
2258 if(remain_bits == 0)
2259 {
2260 remain_bits = 8;
2261 j++;
2262 }
2263 }
2264
2265 return TRUE;
2266 }
2267
2268
2269 static NVCounter_Desc_t nvcounter_desc = {
2270 .descriptor_tag = NSK2HDI_OTP_NVCOUNTER_DESC_TAG,
2271 .descriptor_length = 8, //sizeof(NVCounter_Desc_t) - 2,
2272 .max_nvcounter[0] = 0x00,
2273 .max_nvcounter[1] = 0x00,
2274 .max_nvcounter[2] = 0x04,
2275 .max_nvcounter[3] = 0x00,
2276 .left_nvcounter[0] = 0x00,
2277 .left_nvcounter[1] = 0x00,
2278 .left_nvcounter[2] = 0x04,
2279 .left_nvcounter[3] = 0x00,
2280 };
2281
2282 #define TotalNVNumber 1024
2283
2284
2285
HAL_NSK2_GetNVCounterConfig(MS_U32 * desc_size,MS_U8 * desc)2286 MS_U32 HAL_NSK2_GetNVCounterConfig(MS_U32 *desc_size, MS_U8 *desc)
2287 {
2288 MS_U32 u32NvCounter = 0;
2289 MS_U32 u32LeftNvCounter = 0;
2290
2291 if(desc == NULL)
2292 {
2293 *desc_size = sizeof(nvcounter_desc);
2294 HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_NVCounterUpdates return desc_size = %x\n",*desc_size);
2295 return TRUE;
2296 }
2297
2298 *desc_size = sizeof(nvcounter_desc);
2299 // Read NV Counter from register directly
2300 #if 1
2301 u32NvCounter = NI_REG (REG_NI_NSK21_GET_NVCOUNTER);
2302 #else
2303 HAL_NSK2_WriteControl(0x1);
2304 MsOS_DelayTaskUs(10);
2305 HAL_NSK2_WriteCommand(0x73);
2306 MsOS_DelayTaskUs(10);
2307 StatusCheck(HAL_NSK2_CheckBusy());
2308
2309 MS_U32 NVValue, LeftNVCounter;
2310 HAL_NSK2_ReadData32(0x1C, 0x1, &NVValue);
2311 #endif
2312
2313 u32LeftNvCounter = TotalNVNumber - u32NvCounter;
2314
2315 printf("NVValue = %d, LeftNVCounter = %d \n", u32NvCounter, u32LeftNvCounter);
2316
2317 nvcounter_desc.left_nvcounter[0] = (MS_U8)((u32LeftNvCounter>>24)&0xff);
2318 nvcounter_desc.left_nvcounter[1] = (MS_U8)((u32LeftNvCounter>>16)&0xff);
2319 nvcounter_desc.left_nvcounter[2] = (MS_U8)((u32LeftNvCounter>>8)&0xff);
2320 nvcounter_desc.left_nvcounter[3] = (MS_U8)(u32LeftNvCounter&0xff);
2321
2322 memcpy(desc,&nvcounter_desc,sizeof(nvcounter_desc));
2323
2324 return TRUE;
2325 }
2326
HAL_NSK2_SetDbgLevel(MS_U32 u32Level)2327 void HAL_NSK2_SetDbgLevel(MS_U32 u32Level)
2328 {
2329 _g32NSK2HalDbgLv = u32Level;
2330 HALNSK2_DBG(NSK2_DBGLV_INFO, "%s level: %x\n", __FUNCTION__, u32Level);
2331 return;
2332 }
2333
HAL_NSK2_SetPollingCnt(MS_U32 u32Cnt)2334 void HAL_NSK2_SetPollingCnt(MS_U32 u32Cnt)
2335 {
2336 dead_polling_cnt = u32Cnt;
2337 }
2338
2339 #if 0
2340 void HAL_NSK2_BurstLen(MS_U32 u32PVREng, MS_U32 u32BurstMode)
2341 {
2342 HAL_PVR_BurstLen(u32PVREng, u32BurstMode);
2343 }
2344 #endif
2345
HAL_NSK2_ClockTest(MS_U32 testnum)2346 void HAL_NSK2_ClockTest(MS_U32 testnum)
2347 {
2348
2349 MS_U32 u32Data;
2350 switch(testnum)
2351 {
2352 case 0:
2353 RSA_REG(REG_RSA_CLK_ENABLE) |= RSA_PM_NSKCLK_ENABLE;
2354 MsOS_DelayTaskUs(1);
2355
2356 u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2357 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2358
2359
2360 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2361 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2362
2363 //bit 0 set to 1...
2364 NI_REG(REG_NI_NSK2_CTRL) = u32Data | NI_NSK2_RESET_DISABLE | NI_NSK2_CLK_ENABLE;
2365
2366 MsOS_DelayTaskUs(1);
2367 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2368 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2369 break;
2370
2371
2372 case 1:
2373 RSA_REG(REG_RSA_CLK_ENABLE) |= RSA_PM_NSKCLK_ENABLE;
2374 MsOS_DelayTaskUs(1);
2375
2376 u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2377 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2378
2379
2380 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2381 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2382
2383 //disable nsk2 clock
2384 NI_REG(REG_NI_NSK2_CTRL) = u32Data & ~(NI_NSK2_CLK_ENABLE) ;
2385 MsOS_DelayTask(1);
2386 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2387 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2388
2389 break;
2390
2391 case 2:
2392 RSA_REG(REG_RSA_CLK_ENABLE) &= (~RSA_PM_NSKCLK_ENABLE);
2393 MsOS_DelayTaskUs(1);
2394
2395 u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2396 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2397
2398
2399 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2400 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2401
2402 //bit 0 set to 1...
2403 NI_REG(REG_NI_NSK2_CTRL) = u32Data | NI_NSK2_RESET_DISABLE | NI_NSK2_CLK_ENABLE;
2404
2405 MsOS_DelayTaskUs(1);
2406 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2407 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2408 break;
2409
2410 case 3:
2411 RSA_REG(REG_RSA_CLK_ENABLE) &= (~RSA_PM_NSKCLK_ENABLE);
2412 MsOS_DelayTaskUs(1);
2413
2414 u32Data = RSA_REG(REG_RSA_CLK_ENABLE);
2415 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "RSA REG_RSA_CLK_ENABLE = %x\n",u32Data);
2416
2417 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2418 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2419
2420 //disable nsk2 clock
2421 NI_REG(REG_NI_NSK2_CTRL) = u32Data & ~(NI_NSK2_CLK_ENABLE) ;
2422 MsOS_DelayTask(1);
2423 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2424 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "NI REG_NI_NSK2_CTRL = %x\n",u32Data);
2425 break;
2426 }
2427 }
2428
HAL_NSK2_GetRNGThroughPut(void * pRngData,MS_U32 u32DataSize,MS_BOOL bDump)2429 void HAL_NSK2_GetRNGThroughPut(void *pRngData, MS_U32 u32DataSize, MS_BOOL bDump)
2430 {
2431 MS_U32 u32StartTime;
2432 MS_U32 *pRNG_Data = (MS_U32 *)pRngData;
2433 MS_U32 valid = 0;
2434 MS_U32 i = 0;
2435
2436 NI_REG(REG_NI_NSK2_CTRL) = NI_REG(REG_NI_NSK2_CTRL) & (~NI_NSK2_RESET_DISABLE);
2437 NI_REG(REG_NI_NSK2_FREERUN) = NI_REG(REG_NI_NSK2_FREERUN) & (~ (NI_NSK2_RANDOM_FREERUN | NI_NSK2_RANDOM_ONEBYONE) );
2438
2439 u32StartTime = MsOS_GetSystemTime();
2440
2441 for(i=0; i< u32DataSize; i++)
2442 {
2443 do
2444 {
2445 valid = NI_REG(REG_NI_NSK2_TRNG_VALID) & NI_NSK2_TRNG_VALID_MASK; //trng_sw_read_valid_nsk ;
2446 }while(valid != 1);
2447
2448 pRNG_Data[i] = NI_REG(REG_NI_NSK2_TRNG_DATA);//trng_sw_read_data_nsk;
2449 NI_REG(REG_NI_NSK2_FREERUN) = NI_REG(REG_NI_NSK2_FREERUN) | NI_NSK2_RANDOM_ONEBYONE; //lfsr_get_go_nsk = 1 ;
2450 }
2451 MS_U32 u32EndTime = MsOS_GetSystemTime();
2452
2453 HALNSK2_DBG(NSK2_DBGLV_ERR, "1M bit data size, total Time = %1d ms\n", (MS_U32)(u32EndTime - u32StartTime));
2454
2455 if(bDump)
2456 {
2457 for(i = 0 ; i < u32DataSize ; i ++)
2458 {
2459 HALNSK2_DBG(NSK2_DBGLV_ERR, "%08x\n", (unsigned int)(pRNG_Data[i]));
2460 }
2461 }
2462 }
2463
HAL_NSK2_RunFree(MS_BOOL bRunFree)2464 void HAL_NSK2_RunFree(MS_BOOL bRunFree)
2465 {
2466 HALNSK2_DBG(NSK2_DBGLV_DEBUG, "bRunFree = %d\n", bRunFree);
2467
2468 if(bRunFree)
2469 {
2470 _gCheckBusyFlag = FALSE;
2471 }
2472 else
2473 {
2474 _gCheckBusyFlag = TRUE;
2475 }
2476 }
2477
2478
HAL_NSK2_PushSlowClock(MS_BOOL HaltClk,MS_U32 NumOfMs)2479 MS_U32 HAL_NSK2_PushSlowClock ( MS_BOOL HaltClk, MS_U32 NumOfMs)
2480 {
2481 //HALNSK2_DBG(NSK2_DBGLV_DEBUG, "HaltClk = %d NumofTenSecond = %d\n", HaltClk, NumofTenSecond);
2482 _gCheckBusyFlag = TRUE;
2483 StatusCheck(HAL_NSK2_CheckBusy());
2484 MS_U32 u32Data = NI_REG(REG_NI_NSK2_CTRL);
2485
2486 if(HaltClk == TRUE)//close clk
2487 {
2488 NI_REG(REG_NI_NSK2_CTRL) = u32Data & (~NI_NSK2_CLK_ENABLE) ;
2489 _gCheckBusyFlag = FALSE;
2490 }
2491 else //open clk
2492 {
2493 _gCheckBusyFlag = TRUE;
2494 //_gCheckBusyFlag = FALSE;
2495 if( (u32Data & NI_NSK2_CLK_ENABLE) == 0)
2496 {
2497 NI_REG(REG_NI_NSK2_CTRL) = u32Data | NI_NSK2_CLK_ENABLE ;
2498 //MsOS_DelayTaskUs(1);
2499 }
2500 }
2501
2502 //printf("REG_NI_NSK2_CTRL = %x\n",NI_REG(REG_NI_NSK2_CTRL));
2503
2504 //u32Data = 0;
2505 u32Data = NI_REG(REG_NI_NSK2_CLK_CSA);
2506
2507
2508 NI_REG(REG_NI_NSK2_CLK_CSA) = u32Data | NSK2_PUSH_SLOW_CLK;
2509
2510
2511 if(NumOfMs <= 2)
2512 {
2513 MsOS_DelayTaskUs(NumOfMs*500);
2514 }
2515 else
2516 {
2517 MsOS_DelayTask(NumOfMs-1);
2518 }
2519
2520 return TRUE;
2521 }
2522
2523
2524
HAL_NSK2_GetCMProperties(MS_U32 * desc_size,MS_U8 * desc)2525 MS_U32 HAL_NSK2_GetCMProperties(MS_U32 *desc_size, MS_U8 *desc)
2526 {
2527 MS_U32 i;
2528 if(desc == NULL)
2529 {
2530 *desc_size = sizeof(cmchannel_group_capability_descriptor_t) + sizeof(cm_algo) /*sizeof(cmchannel_group_algorithm_record_descriptor_t)*CMChannelDescSize*/ ;
2531 HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetCMProperties return desc_size = %x\n",*desc_size);
2532 return TRUE;
2533 }
2534
2535 for(i=0;i<32;i++)
2536 {
2537 cm_capb.switch_combination_bitmap[i] = 0xFF;
2538 }
2539
2540 *desc_size = sizeof(cmchannel_group_capability_descriptor_t) + sizeof(cm_algo);
2541
2542 memcpy(desc,&cm_capb,sizeof(cmchannel_group_capability_descriptor_t));
2543 memcpy(desc+sizeof(cmchannel_group_capability_descriptor_t), &cm_algo[0] , sizeof(cm_algo) /*sizeof(cmchannel_group_algorithm_record_descriptor_t)*CMChannelDescSize*/ );
2544
2545 HALNSK2_DBG(NSK2_DBGLV_INFO,"\nHAL_NSK2_GetCMProperties return *desc_size = %x,desc = %x\n",*desc_size,(MS_U32)desc);
2546
2547 return TRUE;
2548 }
2549
HAL_NSK2_GetM2MProperties(MS_U32 * desc_size,MS_U8 * desc)2550 MS_U32 HAL_NSK2_GetM2MProperties(MS_U32 *desc_size, MS_U8 *desc)
2551 {
2552 if(desc == NULL)
2553 {
2554 *desc_size = sizeof(m2m_capa_desc) + sizeof(m2m_algo);
2555 HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetM2MProperties #1 return desc_size = %x\n",*desc_size);
2556 return TRUE;
2557 }
2558
2559 *desc_size = sizeof(m2m_capa_desc) + sizeof(m2m_algo);
2560
2561 memcpy(desc, &m2m_capa_desc, sizeof(m2m_capa_desc));
2562 memcpy(desc+sizeof(m2m_capa_desc), &m2m_algo[0], sizeof(m2m_algo));
2563
2564 HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetM2MProperties #2 return desc_size = %x\n",*desc_size);
2565 return TRUE;
2566 }
2567
HAL_NSK2_GetDMAProperties(MS_U32 * desc_size,MS_U8 * desc)2568 MS_U32 HAL_NSK2_GetDMAProperties(MS_U32 *desc_size, MS_U8 *desc)
2569 {
2570 if(desc == NULL)
2571 {
2572 *desc_size = sizeof(dma_capa_desc);
2573 HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_GetDMAProperties return desc_size = %x\n",*desc_size);
2574 return TRUE;
2575 }
2576
2577 *desc_size = sizeof(dma_capa_desc);
2578
2579 memcpy(desc, &dma_capa_desc, *desc_size);
2580
2581 return TRUE;
2582 }
2583
2584 //---------------Debug Information----------------------------//
2585 //------------------------------------------------------------//
2586
HAL_NSK2_ReadTSPInfo(MS_U32 pid_no)2587 void HAL_NSK2_ReadTSPInfo(MS_U32 pid_no)
2588 {
2589 MS_U32 pid_data;
2590 MS_U32 FltPid, TS_SRC, dscmb_key_en, prim, pid_pair;
2591
2592 FltPid = TS_SRC = dscmb_key_en = prim = pid_pair = 0;
2593 REG_PidFlt *pPidFlt = PPIDFLT0(pid_no);
2594
2595 pid_data = TSP32_IdrR(pPidFlt);
2596
2597 HALNSK2_DBG(NSK2_DBGLV_INFO,"pid_data = %x\n",pid_data);
2598
2599 FltPid = (pid_data & 0x1fff);
2600 TS_SRC = ((pid_data>>13) & 0x7);
2601 dscmb_key_en = ((pid_data>>16) & 0x1);
2602 prim = ((pid_data>>17) & 0x1);
2603 pid_pair = ((pid_data>>18) & 0xf);
2604
2605 if(dscmb_key_en)
2606 {
2607 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);
2608 }
2609 }
2610
HAL_NSK2_ReadTSPDstInfo(MS_U32 pid_no)2611 void HAL_NSK2_ReadTSPDstInfo(MS_U32 pid_no)
2612 {
2613 MS_U32 pid_data;
2614 REG_PidFlt *pPidFlt = PPIDFLT1(pid_no);
2615
2616 pid_data = TSP32_IdrR(pPidFlt);
2617
2618 HALNSK2_DBG(NSK2_DBGLV_INFO,"dst_data = %x\n",pid_data);
2619 }
2620
HAL_NSK2_AllTSPPidFilter(void)2621 void HAL_NSK2_AllTSPPidFilter(void)
2622 {
2623 MS_U32 i;
2624 for(i=0;i<0x100;i++)
2625 {
2626 HAL_NSK2_ReadTSPInfo(i);
2627 }
2628
2629 for(i=0;i<0x100;i++)
2630 {
2631 HAL_NSK2_ReadTSPDstInfo(i);
2632 }
2633 }
2634
HAL_NSK2_ChangePidFilter(MS_U32 pid_no,MS_U32 Data)2635 void HAL_NSK2_ChangePidFilter(MS_U32 pid_no, MS_U32 Data)
2636 {
2637 REG_PidFlt *pPidFlt = PPIDFLT0(pid_no);
2638 TSP32_IdrW(pPidFlt,Data);
2639 }
2640
2641
2642
HAL_NSK2_ReadSwitchFromNSK2(void)2643 MS_U32 HAL_NSK2_ReadSwitchFromNSK2(void)
2644 {
2645
2646 #if 0
2647 2. N2_KteValid=1. NI bank offset 13 bit[3].
2648 3. N2_KteDest=0. NI bank offset 13 bit[2:0].
2649 4. Read the NSK bank offset 0xfc40-0xfc48 to read the NSK driven switch value. (see NSK-ICD-253 page21).
2650 In the read 96 bits, you can find:
2651 Bit[55] AU
2652 Bit[54] BU
2653 Bit[53] DU
2654 Bit[52] EU
2655 Bit[51] AL
2656 Bit[50] BL
2657 Bit[49] DL
2658 Bit[48] EL
2659 Bit[47:44] LocalSelectD
2660 Bit[43:40] LocalSelectS
2661 Bit[39:36] ESA select
2662 Bit[35:33] ESA subselect
2663 Please print out these fields and send the log to me.
2664 #endif
2665
2666 #ifdef ReadSwitchInfoNSK2
2667 MS_U32 NI13 = NI_REG(13);
2668 HALNSK2_DBG(0,"NI13 = %x\n",NI13);
2669
2670 MS_U32 NSK2Debug[3];
2671 NSK2Debug[0] = HAL_NSK2_ReadReg(0xFC40);
2672 NSK2Debug[1] = HAL_NSK2_ReadReg(0xFC44);
2673 NSK2Debug[2] = HAL_NSK2_ReadReg(0xFC48);
2674
2675 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"NSK2Debug = (%x,%x,%x)\n",NSK2Debug[0],NSK2Debug[1],NSK2Debug[2]);
2676 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);
2677 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);
2678 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"LocalSelectD = %x, LocalSelectS = %x\n",(NSK2Debug[1]>>12)&0xf,(NSK2Debug[1]>>8)&0xf);
2679 HALNSK2_DBG(NSK2_DBGLV_DEBUG,"ESA select = %x, ESA subselect = %x\n",(NSK2Debug[1]>>4)&0xf,(NSK2Debug[1]>>0)&0xf);
2680 #endif
2681 return TRUE;
2682 }
2683
HAL_NSK2_GetSRAlignBit(void)2684 MS_U32 HAL_NSK2_GetSRAlignBit(void)
2685 {
2686 return 16;
2687 }
2688
2689 /* 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)2690 MS_BOOL HAL_NSK2_RSA_SetSecureRange(MS_U32 u32SecSet, MS_U32 u32SecStart, MS_U32 u32SecEnd, MS_BOOL bEnable)
2691 {
2692 if(u32SecSet > REG_MPROT_SECRANGE_SET || u32SecEnd <= u32SecStart)
2693 return FALSE;
2694
2695 *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_START(u32SecSet)) = (u32SecStart & MPROT_SECRANGE_MASK);
2696
2697 if (TRUE == bEnable)
2698 {
2699 *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_END(u32SecSet)) = REG_MPROT_SECRANGE_ENABLE & ( u32SecEnd & MPROT_SECRANGE_MASK);
2700 }
2701 else
2702 {
2703 *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_END(u32SecSet)) = (~REG_MPROT_SECRANGE_ENABLE) | ( u32SecEnd & MPROT_SECRANGE_MASK);
2704 }
2705
2706 return TRUE;
2707 }
2708 /**
2709 * @brief Configure CA secure range mask
2710 */
HAL_NSK2_RSA_SetSR_Mask(MS_U32 u32DramSize)2711 void HAL_NSK2_RSA_SetSR_Mask(MS_U32 u32DramSize)
2712 {
2713 MS_U32 u32Mask = 0x00000000;
2714
2715 if (0 != ((u32DramSize >> 16) >> 8))
2716 {
2717 u32Mask = (((u32DramSize >> 16) >> 8) - 1) & 0xFF;
2718
2719 *(volatile MS_U32*) (_gBasicAddr + REG_MPROT_SECRANGE_MASK) = u32Mask;
2720 }
2721 }
2722 //=====================================
2723 //====== NSK2.1 new functions ======
2724 //=====================================
2725
2726
HAL_NSK2_ReadAllOTP(void)2727 void HAL_NSK2_ReadAllOTP(void)
2728 {
2729 MS_U32 u32RetValue, addr;
2730
2731 for( addr=0 ; addr<0x4000; addr+=4)
2732 {
2733 HAL_NSK2_OTP_Get(addr, 31, 0, &u32RetValue);
2734 //if( (addr&0xf) == 0)
2735 // printf("\n");
2736
2737 if(u32RetValue != 0)
2738 {
2739 printf("addr = %x, value = %x \n",addr, u32RetValue);
2740 }
2741 }
2742 }
2743
HAL_NSK2_ReadKTEResp(void)2744 MS_U32 HAL_NSK2_ReadKTEResp(void)
2745 {
2746 StatusCheck(HAL_NSK2_CheckBusy());
2747
2748 MS_U32 u32KTEResp;
2749 u32KTEResp = NI_REG(REG_NI_NSK2_KTE_RESP);
2750
2751 HALNSK2_DBG(NSK2_DBGLV_INFO,"KTE_RESP = %x\n",u32KTEResp);
2752
2753 return u32KTEResp;
2754 }
2755
HAL_NSK2_GetPubOTP(MS_U8 * pPubOTP)2756 MS_BOOL HAL_NSK2_GetPubOTP(MS_U8 *pPubOTP)
2757 {
2758 #if 0
2759 Table 7 Public OTP Bits
2760 Bit Name Description
2761 127:64 U_OTP_v_pubOtpUniqueID1 OTP unique ID.
2762 This field is unique chip data that can be shared with other non-NDS modules.
2763 63:48 U_OTP_v_PubOtpGP General purpose OTP field.
2764 47:40 U_OTP_v_PubOtpMinConfVer OTPpublic minimum configuration version.
2765 39:36 U_OTP_v_PubOtpRsaIndex OTP public RSA index
2766 35:32 U_OTP_v_PubOtpBID OTP public BlackBox ID
2767 31:16 U_OTP_v_PubOtpVID OTP public version ID
2768 15:0 U_OTP_v_PubOtpOID OTP public owner ID
2769 #endif
2770
2771 //U_OTP_v_pubOtpUniqueID1 0x3DA8 63 0
2772 //U_OTP_v_PubOtpGP 0x3B08 15 0
2773 //U_OTP_v_PubOtpMinConfVer 0x3B14 7 0
2774 //U_OTP_v_PubOtpRsaIndex 0x3B10 3 0
2775 //U_OTP_v_PubOtpBID 0x3B0C 3 0
2776 //U_OTP_v_PubOtpVID 0x3B04 15 0
2777 //U_OTP_v_PubOtpOID 0x3B00 15 0
2778
2779 MS_U8 *pRunPubOTP = (MS_U8 *)pPubOTP;
2780 MS_U32 ReadValue,ReadValue2;
2781
2782 HAL_NSK2_OTP_Get(0x3B00, 15, 0, &ReadValue);
2783 printf("U_OTP_v_PubOtpOID = %x\n",ReadValue);
2784 memcpy(pRunPubOTP,&ReadValue,2);
2785 pRunPubOTP += 2;
2786
2787 HAL_NSK2_OTP_Get(0x3B04, 15, 0, &ReadValue);
2788 printf("U_OTP_v_PubOtpVID = %x\n",ReadValue);
2789 memcpy(pRunPubOTP,&ReadValue,2);
2790 pRunPubOTP += 2;
2791
2792 HAL_NSK2_OTP_Get(0x3B0C, 3, 0, &ReadValue);
2793 printf("U_OTP_v_PubOtpBID = %x\n",ReadValue);
2794 HAL_NSK2_OTP_Get(0x3B10, 3, 0, &ReadValue2);
2795 printf("U_OTP_v_PubOtpRsaIndex = %x\n",ReadValue2);
2796 *pRunPubOTP = (MS_U8)(ReadValue&0xF) + (MS_U8)((ReadValue2&0xF)<<4);
2797 pRunPubOTP ++;
2798
2799 HAL_NSK2_OTP_Get(0x3B14, 7, 0, &ReadValue);
2800 printf("U_OTP_v_PubOtpMinConfVer = %x\n",ReadValue);
2801 memcpy(pRunPubOTP,&ReadValue,1);
2802 pRunPubOTP ++;
2803
2804 HAL_NSK2_OTP_Get(0x3B08, 15, 0, &ReadValue);
2805 printf("U_OTP_v_PubOtpGP = %x\n",ReadValue);
2806 memcpy(pRunPubOTP,&ReadValue,2);
2807 pRunPubOTP += 2;
2808
2809 HAL_NSK2_OTP_Get(0x3DA8, 31, 0, &ReadValue);
2810 printf("U_OTP_v_pubOtpUniqueID1 = %x\n",ReadValue);
2811 memcpy(pRunPubOTP,&ReadValue,4);
2812 pRunPubOTP += 4;
2813
2814 HAL_NSK2_OTP_Get(0x3DAC, 31, 0, &ReadValue);
2815 memcpy(pRunPubOTP,&ReadValue,4);
2816 pRunPubOTP += 4;
2817
2818 return TRUE;
2819 }
2820
HAL_NSK21_InvalidCmChannel(MS_U16 PidSlot)2821 MS_U32 HAL_NSK21_InvalidCmChannel(MS_U16 PidSlot)
2822 {
2823 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s PidSlot = %x\n", __FUNCTION__,PidSlot);
2824
2825 MS_U32 u32PidSlot = 0;
2826 StatusCheck(HAL_NSK2_CheckBusy());
2827
2828 u32PidSlot = (((MS_U32)PidSlot<<NI_WriteTKey_PidNo_Shift)&NI_WriteTransportKey_PidNo);
2829 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_InvalidateCmChannel | u32PidSlot);
2830
2831 return TRUE;
2832 }
2833
2834 typedef enum
2835 {
2836 NSK21_DVBCSA2 = 0,
2837 NSK21_DVBCSA2_CONF,
2838 NSK21_DVBCSA3,
2839 NSK21_CPCM_LSA_MDI_CBC,
2840 NSK21_CPCM_LSA_MDI_RCBC,
2841 NSK21_CPCM_LSA_MDD_CBC, //5
2842 NSK21_CPCM_LSA_MDD_RCBC,
2843 NSK21_SYNAMEDIA_AES,
2844 NSK21_AES_ECB_CLR,
2845 NSK21_CIPLUS_AES,
2846 NSK21_SCTE41_DES, //10
2847 NSK21_SCTE52_DES,
2848 NSK21_TDES_ECB_CLR,
2849 NSK21_MULTI2_TS,
2850 NSK21_TDES_ECB_CTS,
2851 NSK21_DES_ECB_CTS, //15
2852 NSK21_IDSA_AES,
2853 }NSK21_CMChAlgo_e;
2854
HAL_NSK21_CfgCmChannel(MS_U16 PidSlot,MS_U16 LocalDAlgo,MS_U16 ESAAlgo,MS_U16 LocalSAlgo,MS_U16 SysKeyIndex)2855 MS_U32 HAL_NSK21_CfgCmChannel(MS_U16 PidSlot, MS_U16 LocalDAlgo, MS_U16 ESAAlgo, MS_U16 LocalSAlgo, MS_U16 SysKeyIndex)
2856 {
2857
2858 #define Algoffset 100
2859 HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK2_CfgCmChannel PidSlot = %x, LocalDAlgo = %x, ESAAlgo = %x, LocalSAlgo = %x, SysKeyIndex = %x\n",PidSlot, LocalDAlgo, ESAAlgo, LocalSAlgo, SysKeyIndex);
2860 StatusCheck(HAL_NSK2_CheckBusy());
2861 MS_U32 algorithms = 0, u32PidSlot = 0;
2862
2863 if(LocalDAlgo < Algoffset)
2864 LocalDAlgo = 0;
2865 else
2866 LocalDAlgo -= Algoffset;
2867
2868 if(ESAAlgo < Algoffset)
2869 ESAAlgo = 0;
2870 else
2871 ESAAlgo -= Algoffset;
2872
2873 if(LocalSAlgo < Algoffset)
2874 LocalSAlgo = 0;
2875 else
2876 LocalSAlgo -= Algoffset;
2877
2878 #if 0 //tmp solution...
2879 algorithms = ((LocalDAlgo - 100) & NI_KIW_LSAD_ALGO_MASK) +
2880 (((ESAAlgo - 100) << NI_KIW_ESA_ALGO_SHIFT) & NI_KIW_ESA_ALGO_MASK) +
2881 (((LocalSAlgo - 100) << NI_KIW_LSAS_ALGO_SHIFT) & NI_KIW_LSAS_ALGO_MASK) ;
2882 #else
2883 algorithms = (LocalDAlgo & NI_KIW_LSAD_ALGO_MASK) +
2884 ((ESAAlgo << NI_KIW_ESA_ALGO_SHIFT) & NI_KIW_ESA_ALGO_MASK) +
2885 ((LocalSAlgo << NI_KIW_LSAS_ALGO_SHIFT) & NI_KIW_LSAS_ALGO_MASK) ;
2886 #endif
2887
2888 HALNSK2_DBG(NSK2_DBGLV_INFO,"algorithms = %x\n",algorithms);
2889 NI_REG(REG_NI_DSCMB_ALGO) = algorithms;
2890
2891 u32PidSlot = (((MS_U32)PidSlot<<NI_WriteTKey_PidNo_Shift)&NI_WriteTransportKey_PidNo);
2892 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_ConfigureCmChannel | u32PidSlot);
2893 MsOS_DelayTaskUs(1);
2894 HALNSK2_DBG(NSK2_DBGLV_INFO,"REG_NI_DSCMB_ALGO = %x\n",NI_REG(REG_NI_DSCMB_ALGO));
2895
2896 return TRUE;
2897 }
2898
HAL_NSK21_WriteTransportKey(MS_U8 SCB,MS_U8 ForceSCB,void * pLocalDIV1,void * pLocalDIV2,void * pESAIV1,void * pESAIV2,void * pLocalSIV1,void * pLocalSIV2,MS_U16 PidSlot)2899 MS_U32 HAL_NSK21_WriteTransportKey(MS_U8 SCB, MS_U8 ForceSCB, void *pLocalDIV1, void *pLocalDIV2,
2900 void *pESAIV1, void *pESAIV2, void *pLocalSIV1, void *pLocalSIV2, MS_U16 PidSlot )
2901 {
2902 HALNSK2_DBG(NSK2_DBGLV_INFO,"HAL_NSK21_WriteTransportKey PidSlot = %x, SCB = %x, ForceSCB = %x\n", PidSlot, SCB, ForceSCB);
2903
2904 MS_U32 data,data2;
2905 //check NULL pointer or not....
2906
2907 StatusCheck(HAL_NSK2_CheckBusy());
2908
2909 data = NI_REG(REG_NI_KTE_STATUS);
2910 if( ( data & NI_KTE_DEST_MASK ) == 0 )
2911 {
2912
2913 ForceSCB = HAL_NSK2_SCBTransToHW(ForceSCB);
2914
2915 HALNSK2_DBG(NSK2_DBGLV_INFO,"PidSlot: %x\n", PidSlot);
2916 data2 = (((MS_U32)PidSlot<<NI_WriteTKey_PidNo_Shift)&NI_WriteTransportKey_PidNo) +
2917 (((MS_U32)SCB<<NI_WriteTKey_SCB_Shift) & NI_WriteTKey_SCB_MASK)
2918 + ( ( (MS_U32)ForceSCB<<NI_WriteTKey_FSCB_Shift) & NI_WriteTKey_FSCB_MASK) ;
2919
2920 #if 1
2921 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteTransportKey | data2 | NI_OTP_ACK_NSK2 | NI_ERR_INVALID_SLOT);
2922 #else
2923 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteTransportKey | data2);
2924 #endif
2925 HALNSK2_DBG(NSK2_DBGLV_INFO,"NI 6 = %x\n", NI_REG(REG_NI_COMMAND));
2926
2927 StatusCheck(HAL_NSK2_KIW_BusyPolling());
2928 }
2929 else
2930 {
2931 HALNSK2_DBG(NSK2_DBGLV_ERR,"WriteTransportKey abnormal ignored: KteDest not zero\n");
2932 return HAL_NSK2_ReadKTEResp();
2933 }
2934
2935 return HAL_NSK2_ReadKTEResp();
2936 }
2937
HAL_NSK21_WriteM2MKey(MS_U32 M2MAlgo)2938 MS_U32 HAL_NSK21_WriteM2MKey(MS_U32 M2MAlgo)
2939 {
2940 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s M2MAlgo = %x\n",__FUNCTION__,M2MAlgo);
2941
2942 MS_U32 algorithms;
2943
2944 StatusCheck(HAL_NSK2_CheckBusy());
2945
2946 algorithms = (M2MAlgo & NI_KIW_LSAD_ALGO_MASK) +
2947 ((M2MAlgo << NI_KIW_LSAS_ALGO_SHIFT) & NI_KIW_LSAS_ALGO_MASK) ;
2948
2949 HALNSK2_DBG(NSK2_DBGLV_INFO,"algorithms = %x\n",algorithms);
2950 NI_REG(REG_NI_DSCMB_ALGO) = algorithms;
2951 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteM2MKey);
2952
2953 MsOS_DelayTaskUs(1);
2954
2955 return HAL_NSK2_ReadKTEResp();
2956 }
2957
HAL_NSK21_ModifyGenIn(MS_U32 MaskVal,MS_U32 XorVal)2958 MS_U32 HAL_NSK21_ModifyGenIn(MS_U32 MaskVal,MS_U32 XorVal)
2959 {
2960 MS_U32 GenIn = 0, WriteGenIn = 0;
2961
2962 StatusCheck(HAL_NSK2_CheckBusy());
2963
2964 GenIn = NI_REG(REG_NI_NSK2_REG_GENIN);
2965
2966 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s GenIn = %x\n",__FUNCTION__,GenIn);
2967
2968 GenIn = (GenIn&MaskVal);
2969
2970 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s GenIn = %x, XorVal = %x\n",__FUNCTION__,GenIn,XorVal);
2971
2972 WriteGenIn = GenIn^XorVal;
2973 printf("WriteGenIn = %x\n",WriteGenIn);
2974
2975 #ifdef TestGenIn
2976 NI_REG(REG_NI_NSK21_GENIN) = WriteGenIn;
2977 MsOS_DelayTaskUs(10);
2978 #else
2979
2980 //NI_REG(REG_NI_NSK21_GENIN) = ( (WriteGenIn & BMASK(9:0)) | (WriteGenIn & __BIT(13)) );
2981 NI_REG(REG_NI_NSK21_GENIN) = WriteGenIn;
2982
2983 NI_REG(REG_NI_NSK21_CONCURR_PROT_EN) = ((WriteGenIn>>10) & BMASK(1:0));
2984 NI_REG(REG_NI_NSK21_CONCURR_SET) = ((WriteGenIn>>12) & BMASK(0:0));
2985 NI_REG(REG_NI_NSK21_GEN_SHOT) = ((WriteGenIn>>14) & BMASK(3:0));
2986
2987 MsOS_DelayTaskUs(10);
2988
2989 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));
2990 #endif
2991 GenIn = NI_REG(REG_NI_NSK2_REG_GENIN);
2992
2993 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s GenIn = %x\n",__FUNCTION__,GenIn);
2994
2995 if(GenIn == WriteGenIn)
2996 return TRUE;
2997 else
2998 return FALSE;
2999
3000 }
3001
HAL_NSK21_GetGenIn(void)3002 MS_U32 HAL_NSK21_GetGenIn(void)
3003 {
3004 return NI_REG(REG_NI_NSK2_REG_GENIN);
3005 }
3006
HAL_NSK21_ReadNIReg(MS_U32 offset)3007 MS_U32 HAL_NSK21_ReadNIReg(MS_U32 offset)
3008 {
3009 return NI_REG(offset);
3010 }
3011
HAL_NSK21_WriteNIReg(MS_U32 offset,MS_U32 Value)3012 void HAL_NSK21_WriteNIReg(MS_U32 offset, MS_U32 Value)
3013 {
3014 NI_REG(offset) = Value;
3015 }
3016
HAL_NSK21_WriteJTagKey(MS_U32 OverrideOid,MS_U32 Select)3017 MS_U32 HAL_NSK21_WriteJTagKey(MS_U32 OverrideOid, MS_U32 Select)
3018 {
3019 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s OverrideOid = %x, Select = %x\n",__FUNCTION__,OverrideOid,Select);
3020
3021 MS_U32 functionality = 0, OID = 0;
3022
3023 StatusCheck(HAL_NSK2_CheckBusy());
3024
3025 if(OverrideOid>1) //invalid OverrideOid
3026 {
3027 HALNSK2_DBG(NSK2_DBGLV_ERR,"invalid OverrideOid\n");
3028 return FALSE;
3029 }
3030
3031 OID = (OverrideOid << NI_KIW_OID_SHIFT) & NI_KIW_OID_MASK ;
3032 #if 0
3033 functionality = (Select << NI_WriteTKey_PidNo_Shift) & NI_WriteTransportKey_PidNo ;
3034 #else
3035 functionality = 0; //N21_EJTAG_PWD
3036 #endif
3037
3038 HALNSK2_DBG(NSK2_DBGLV_INFO,"functionality = %x\n",functionality);
3039 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteJTAGKey | functionality | OID);
3040
3041 MsOS_DelayTaskUs(1);
3042
3043 return HAL_NSK2_ReadKTEResp();
3044 }
3045
HAL_NSK21_IncrementNvCounter(void)3046 MS_U32 HAL_NSK21_IncrementNvCounter(void)
3047 {
3048 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n",__FUNCTION__);
3049
3050 StatusCheck(HAL_NSK2_CheckBusy());
3051
3052 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_IncrementNvCounter);
3053 MsOS_DelayTaskUs(1);
3054
3055 return HAL_NSK2_ReadKTEResp();
3056 }
3057
HAL_NSK2_WriteOtpKey(void)3058 MS_U32 HAL_NSK2_WriteOtpKey(void)
3059 {
3060 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n",__FUNCTION__);
3061 StatusCheck(HAL_NSK2_CheckBusy());
3062
3063 NI_REG(REG_NI_COMMAND) = (NI_COMMAND_START | NI_WriteOTPKey);
3064 MsOS_DelayTask(10);
3065
3066 return HAL_NSK2_ReadKTEResp();
3067 }
3068
HAL_NSK2_ReadPWD_Status(void)3069 MS_U32 HAL_NSK2_ReadPWD_Status(void)
3070 {
3071 HALNSK2_DBG(NSK2_DBGLV_INFO,"%s \n",__FUNCTION__);
3072 StatusCheck(HAL_NSK2_CheckBusy());
3073 MS_U32 RegPwd;
3074
3075 RegPwd = NI_REG(REG_NI_NSK2_PWD_ON);
3076
3077 if(RegPwd & N2_JTAGPWD0_ON)
3078 {
3079 printf("JTAG PWD0 ON \n");
3080 }
3081
3082 if(RegPwd & N2_JTAGPWD1_ON)
3083 {
3084 printf("JTAG PWD1 ON \n");
3085 }
3086
3087 if(RegPwd & N2_SCANPWD_ON)
3088 {
3089 printf("SCAN PWD ON \n");
3090 }
3091
3092 if(RegPwd & N2_MBISTPWD_ON)
3093 {
3094 printf("MBIST PWD ON \n");
3095 }
3096
3097 if(RegPwd & N2_M2MKEY_ON)
3098 {
3099 printf("M2M KEY ON \n");
3100 }
3101
3102 if(RegPwd & N2_SCPUKEY0_ON)
3103 {
3104 printf("SCPU KEY0 ON \n");
3105 }
3106
3107 if(RegPwd & N2_SCPUKEY1_ON)
3108 {
3109 printf("SCPU KEY1 ON \n");
3110 }
3111
3112 if(RegPwd & N2_RNGVALUE0_ON)
3113 {
3114 printf("RNG VALUE0 ON \n");
3115 }
3116
3117 if(RegPwd & N2_RNGVALUE1_ON)
3118 {
3119 printf("RNG VALUE1 ON \n");
3120 }
3121
3122 return TRUE;
3123 }
3124
HAL_NSK2_CtrlClk(MS_BOOL Enable)3125 MS_U32 HAL_NSK2_CtrlClk(MS_BOOL Enable)
3126 {
3127 if(Enable)
3128 {
3129 printf("enable NSK clock\n");
3130 NI_REG(REG_NI_NSK2_CTRL) |= (NI_NSK2_CLK_ENABLE | NI_NSK2_RESET_DISABLE);
3131 HAL_NSK2_ColdReset();
3132 }
3133 else
3134 {
3135 printf("disable NSK clock\n");
3136 NI_REG(REG_NI_NSK2_CTRL) &= ~(NI_NSK2_CLK_ENABLE | NI_NSK2_RESET_DISABLE);
3137 _gReset = FALSE;
3138 _gCheckBusyFlag = FALSE;
3139 }
3140
3141 MsOS_DelayTask(1);
3142
3143
3144 printf("REG_NI_NSK2_CTRL = %x\n",NI_REG(REG_NI_NSK2_CTRL));
3145 return TRUE;
3146 }
3147
HAL_NSK2_ReadClkStatus(void)3148 MS_U32 HAL_NSK2_ReadClkStatus(void)
3149 {
3150 MS_U32 status;
3151 MS_U32 rom[4];
3152
3153 status = (NI_REG(REG_NI_KTE_STATUS)& NI_SLOW_CLOCK_DETECT);
3154
3155 printf("REG_NI_KTE_STATUS = %x\n",NI_REG(REG_NI_KTE_STATUS));
3156 printf("NI_SLOW_CLOCK_DETECT = %x\n", (status>>5));
3157
3158 if(status)
3159 {
3160 printf("no clock for NSK\n");
3161 }
3162 else
3163 {
3164 printf("clock is alive for NSK\n");
3165 }
3166
3167 printf("read NSK ROM value\n");
3168 HAL_NSK2_ReadData32(0x8000, 4, &rom[0]);
3169
3170 printf("%x, %x, %x, %x,\n",rom[0],rom[1],rom[2],rom[3]);
3171
3172 return TRUE;
3173 }
3174
3175