1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _TEST_PROJ_OTP_H__
8 #define _TEST_PROJ_OTP_H__
9 
10 #include <stdint.h>
11 #include "cc_otp_defs.h"
12 #include "test_proj.h"
13 
14 /* HW KEYS */
15 #define TEST_HUK_KEY            0
16 #define TEST_RTL_KEY        1
17 #define TEST_KCP_KEY            2
18 #define TEST_KCE_KEY            3
19 #define TEST_KPICV_KEY          4
20 #define TEST_KCEICV_KEY         5
21 
22 #define TEST_HBK0_KEY           16
23 #define TEST_HBK1_KEY           17
24 #define TEST_HBK_FULL_KEY       18
25 
26 /* OTP memory mapping */
27 #define ENV_OTP_START_OFFSET        0x2000UL
28 #define TEST_OTP_SIZE_IN_WORDS      0x2C
29 #define MAX_OTP_SIZE_IN_WORDS       0x7FF
30 #define TEST_OTP_LAST_WORD_IN_MASK 0x20
31 
32 typedef enum {
33     TEST_CHIP_STATE_NOT_INITIALIZED = 0,
34     TEST_CHIP_STATE_TEST = 1,
35     TEST_CHIP_STATE_PRODUCTION = 2,
36     TEST_CHIP_STATE_ERROR = 3,
37 } OtpChipState_t;
38 
39 typedef enum otpHbkTypes_t {
40     TEST_OTP_HBK0_TYPE = 1,    //HBK0
41     TEST_OTP_HBK1_TYPE = 2,    //HBK1
42     TEST_OTP_HBK_256_TYPE = 4,    //HBK
43 } OtpHbkTypes_t;
44 
45 #define TEST_CALC_BUFF_ZEROS(wordBuf, buffWordSize, zeros) {\
46     uint32_t i = 0;\
47     uint32_t j = 0;\
48     uint32_t mask = 0;\
49     zeros = 0;\
50     for (i = 0; i< buffWordSize; i++) {\
51         for (j = 0; j< BITS_IN_32BIT_WORD; j++) {\
52             mask = 0x1;\
53             if (!(*(wordBuf+i) & (mask << j))) {\
54                 zeros++;\
55             }\
56         }\
57     }\
58 }
59 
60 #define TEST_WRITE_OTP_BY_REG(offset, val)  \
61         TEST_WRITE_TEE_CC_REG(CC_OTP_BASE_ADDR +(offset*sizeof(uint32_t)), val)
62 
63 #define TEST_READ_OTP_BY_REG(offset)   \
64         TEST_READ_TEE_CC_REG(CC_OTP_BASE_ADDR+ (offset*sizeof(uint32_t)))
65 
66 #define TEST_WRITE_OTP_BY_ENV(wordOffset, val) \
67         TEST_WRITE_TEE_ENV_REG(ENV_OTP_START_OFFSET + ((wordOffset)*sizeof(uint32_t)), val)
68 
69 #define TEST_READ_OTP_BY_ENV(wordOffset) \
70         TEST_READ_TEE_ENV_REG(ENV_OTP_START_OFFSET + ((wordOffset)*sizeof(uint32_t)))
71 
72 #define SET_OTP_DCU_LOCK(otpBuff, val) {\
73     uint32_t ii = 0; \
74     for (ii = 0; ii < CC_OTP_DCU_SIZE_IN_WORDS; ii++) { \
75         otpBuff[CC_OTP_DCU_OFFSET+ii] = val; \
76     } \
77 }
78 
79 /* calc OTP memory length:
80  read RTL OTP address width. The supported sizes are 6 (for 2 Kbits),7,8,9,10,11 (for 64 Kbits).
81  convert value parameter to addresses of 32b words */
82 #define GET_OTP_LENGTH(otpLength)                           \
83     do {                                                \
84         otpLength = TEST_READ_TEE_CC_REG(CC_REG_OFFSET(HOST_RGF, OTP_ADDR_WIDTH_DEF));  \
85         otpLength = CC_REG_FLD_GET(0, OTP_ADDR_WIDTH_DEF, VALUE, otpLength);            \
86         otpLength = (1 << otpLength);                               \
87     }while(0)
88 
89 typedef struct TestOtpMask_t {
90     uint32_t isPCI;
91     uint32_t* mask;
92 } TestOtpMask_t;
93 
94 extern uint32_t OTP_CM_VALUES[];
95 extern uint32_t OTP_DM_HBK_VALUES[];
96 extern uint32_t gTestOtpMaskV1[];
97 extern uint32_t gTestOtpMaskV2[];
98 
99 extern TestOtpMask_t gOtpStatus;
100 
101 /******************************/
102 /*   function declaration     */
103 /*****************************/
104 
105 /*
106  * @brief This function sets teh chip indication status in a buffer
107  *
108  * @param[in]
109  *
110  * @param[out]
111  *
112  * @return void -
113 
114  */
115 void Test_ProjSetChipIndication(uint32_t *otpValues, OtpChipState_t chipIndication);
116 
117 /*
118  * @brief This function writes to OTP using environment register including RTL mask
119  *
120  * @param[in]
121  *
122  * @param[out]
123  *
124  * @return void -
125  */
126 void Test_ProjWriteOtpWord(uint32_t offsetInWords, uint32_t value);
127 
128 /*
129  * @brief This function reads to OTP using environment register including RTL mask
130  *
131  * @param[in]
132  *
133  * @param[out]
134  *
135  * @return void -
136  */
137 uint32_t Test_ProjReadOtpWord(uint32_t offsetInWords);
138 
139 /*
140  * @brief This function sets the key in OTP buffer including number of zeroes and RTL mask
141  *
142  * @param[in]
143  *
144  * @param[out]
145  *
146  * @return void -
147  */
148 uint32_t Test_ProjSetKeyOtpBuff(uint32_t *otpBuf, uint32_t *keyBuff, uint32_t keyType);
149 
150 /*
151  * @brief This function burns OTP buffer with the required chip indication flag
152  *
153  * @param[in]
154  *
155  * @param[out]
156  *
157  * @return void -
158  */
159 uint32_t Test_ProjBurnCmOtp(OtpChipState_t chipIndication);
160 
161 /*
162  * @brief This function burns OTP buffer with the required chip indication flag
163  *
164  * @param[in]
165  *
166  * @param[out]
167  *
168  * @return void -
169  */
170 uint32_t Test_ProjBurnDmOtp(uint32_t *otpBuf, OtpChipState_t chipIndication);
171 
172 /*
173  * @brief This function burns OTP buffer (as is) with the required chip indication flag
174  *
175  * @param[in]
176  *
177  * @param[out]
178  *
179  * @return void -
180  */
181 uint32_t Test_ProjBurnOtpPlain(uint32_t *otpBuf, uint32_t nextLcs, OtpChipState_t chipIndication);
182 
183 /*
184  * @brief This function burns OTP buffer with the required chip indication flag (including key integrity setting)
185  *
186  * @param[in]
187  *
188  * @param[out]
189  *
190  * @return void -
191 
192  */
193 uint32_t Test_ProjBurnOtp(uint32_t *otpBuf, uint32_t nextLcs, OtpChipState_t chipIndication);
194 
195 /*
196  * @brief This function sets the chip indication status in global variable and defines the right mask to use
197  *
198  * @param[in]
199  *
200  * @param[out]
201  *
202  * @return void -
203  */
204 void Test_ProjSetOtpBufState(uint8_t isPCIMode);
205 
206 /*
207  * @brief This function sets the zero count in OTP buffer
208  *
209  * @param[in]
210  *
211  * @param[out]
212  *
213  * @return void -
214  */
215 uint32_t Test_ProjSetZeroBitsOtpBuff(uint32_t *otpBuf, uint32_t keyType, uint32_t value);
216 
217 /*
218  * @brief This function sets HUK buffer in OTP buffer
219  *
220  * @param[in]
221  *
222  * @param[out]
223  *
224  * @return void -
225  */
226 uint32_t Test_ProjSetKdrInOtpBuff(uint32_t *otp, uint8_t *kdrBuff);
227 
228 /*
229  * @brief This function sets HBK buffer in OTP buffer
230  *
231  * @param[in]
232  *
233  * @param[out]
234  *
235  * @return void -
236  */
237 uint32_t Test_ProjSetHbkInOtpBuff(uint32_t *otp,
238                                   uint8_t *hbkBuff,
239                                   OtpHbkTypes_t type,
240                                   uint8_t isFullHbk);
241 
242 /*
243  * @brief This function sets KPICV buffer in OTP buffer
244  *
245  * @param[in]
246  *
247  * @param[out]
248  *
249  * @return void -
250  */
251 uint32_t Test_ProjSetKpicvInOtpBuff(uint32_t *otpBuf, uint8_t *kpicvBuff);
252 
253 /*
254  * @brief This function sets KCEICV buffer in OTP buffer
255  *
256  * @param[in]
257  *
258  * @param[out]
259  *
260  * @return void -
261  */
262 uint32_t Test_ProjSetKceicvOtpBuff(uint32_t *otpBuf, uint8_t *kceicvBuff);
263 
264 /*
265  * @brief This function sets KCP buffer in OTP buffer
266  *
267  * @param[in]
268  *
269  * @param[out]
270  *
271  * @return void -
272  */
273 uint32_t Test_ProjSetKcpOtpBuff(uint32_t *otpBuf, uint8_t *kcpBuff);
274 
275 /*
276  * @brief This function sets KCE buffer in OTP buffer
277  *
278  * @param[in]
279  *
280  * @param[out]
281  *
282  * @return void -
283  */
284 uint32_t Test_ProjSetKceOtpBuff(uint32_t *otpBuf, uint8_t *kceBuff);
285 
286 /*
287  * @brief This function sets default OTP used for llhw tests
288  *
289  * @param[in]
290  *
291  * @param[out]
292  *
293  * @return void -
294  */
295 uint32_t Test_ProjSetNotInUseOtpBuff(uint32_t *otpBuf, uint32_t keyType, uint32_t value);
296 
297 /*
298  * @brief This function sets default OTP used for llhw tests
299  *
300  * @param[in]
301  *
302  * @param[out]
303  *
304  * @return void -
305  */
306 uint32_t Test_ProjGetKeySizeInWordsOtp(uint32_t keyType, uint32_t* keySizeInWords);
307 
308 /*
309  * @brief This function sets default OTP used for llhw tests
310  *
311  * @param[in]
312  *
313  * @param[out]
314  *
315  * @return void -
316  */
317 uint32_t Test_ProjGetKeyOtpBuff(uint32_t *otpBuf,
318                                 uint32_t keyType,
319                                 uint32_t *keySizeInWords,
320                                 uint32_t *keyBuff);
321 
322 /*
323  * @brief This function sets default OTP used for llhw tests
324  *
325  * @param[in]
326  *
327  * @param[out]
328  *
329  * @return void -
330  */
331 uint32_t Test_ProjSetSwVerInOtpBuff(uint32_t *otp, uint32_t offset, uint32_t max_size);
332 
333 /*
334  * @brief This function sets default OTP used for llhw tests
335  *
336  * @param[in]
337  *
338  * @param[out]
339  *
340  * @return void -
341  */
342 
343 #endif //_TEST_PROJ_OTP_H__
344