xref: /openbmc/u-boot/cmd/otp.c (revision 21426358)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2021 Aspeed Technology Inc.
4  */
5 
6 #include <stdlib.h>
7 #include <common.h>
8 #include <console.h>
9 #include <bootretry.h>
10 #include <cli.h>
11 #include <command.h>
12 #include <console.h>
13 #include <malloc.h>
14 #include <inttypes.h>
15 #include <mapmem.h>
16 #include <asm/io.h>
17 #include <linux/compiler.h>
18 #include <u-boot/sha256.h>
19 #include "otp_info.h"
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 #define OTP_VER				"1.1.0"
24 
25 #define OTP_PASSWD			0x349fe38a
26 #define RETRY				20
27 #define OTP_REGION_STRAP		BIT(0)
28 #define OTP_REGION_CONF			BIT(1)
29 #define OTP_REGION_DATA			BIT(2)
30 
31 #define OTP_USAGE			-1
32 #define OTP_FAILURE			-2
33 #define OTP_SUCCESS			0
34 
35 #define OTP_PROG_SKIP			1
36 
37 #define OTP_KEY_TYPE_RSA_PUB		1
38 #define OTP_KEY_TYPE_RSA_PRIV		2
39 #define OTP_KEY_TYPE_AES		3
40 #define OTP_KEY_TYPE_VAULT		4
41 #define OTP_KEY_TYPE_HMAC		5
42 
43 #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
44 #define PBWIDTH 60
45 
46 #define OTP_BASE		0x1e6f2000
47 #define OTP_PROTECT_KEY		OTP_BASE
48 #define OTP_COMMAND		OTP_BASE + 0x4
49 #define OTP_TIMING		OTP_BASE + 0x8
50 #define OTP_ADDR		OTP_BASE + 0x10
51 #define OTP_STATUS		OTP_BASE + 0x14
52 #define OTP_COMPARE_1		OTP_BASE + 0x20
53 #define OTP_COMPARE_2		OTP_BASE + 0x24
54 #define OTP_COMPARE_3		OTP_BASE + 0x28
55 #define OTP_COMPARE_4		OTP_BASE + 0x2c
56 #define SW_REV_ID0		OTP_BASE + 0x68
57 #define SW_REV_ID1		OTP_BASE + 0x6c
58 
59 #define OTP_MAGIC		"SOCOTP"
60 #define CHECKSUM_LEN		32
61 #define OTP_INC_DATA		BIT(31)
62 #define OTP_INC_CONFIG		BIT(30)
63 #define OTP_INC_STRAP		BIT(29)
64 #define OTP_ECC_EN		BIT(28)
65 #define OTP_REGION_SIZE(info)	((info >> 16) & 0xffff)
66 #define OTP_REGION_OFFSET(info)	(info & 0xffff)
67 #define OTP_IMAGE_SIZE(info)	(info & 0xffff)
68 
69 #define OTP_A0		0
70 #define OTP_A1		1
71 #define OTP_A2		2
72 #define OTP_A3		3
73 
74 #define ID0_AST2600A0	0x05000303
75 #define ID1_AST2600A0	0x05000303
76 #define ID0_AST2600A1	0x05010303
77 #define ID1_AST2600A1	0x05010203
78 #define ID0_AST2600A2	0x05010303
79 #define ID1_AST2600A2	0x05020303
80 #define ID0_AST2600A3	0x05030303
81 #define ID1_AST2600A3	0x05030303
82 #define ID0_AST2620A1	0x05010203
83 #define ID1_AST2620A1	0x05010203
84 #define ID0_AST2620A2	0x05010203
85 #define ID1_AST2620A2	0x05020203
86 #define ID0_AST2620A3	0x05030203
87 #define ID1_AST2620A3	0x05030203
88 #define ID0_AST2620A3	0x05030203
89 #define ID1_AST2620A3	0x05030203
90 #define ID0_AST2605A2	0x05010103
91 #define ID1_AST2605A2	0x05020103
92 #define ID0_AST2605A3	0x05030103
93 #define ID1_AST2605A3	0x05030103
94 #define ID0_AST2625A3	0x05030403
95 #define ID1_AST2625A3	0x05030403
96 
97 struct otp_header {
98 	u8	otp_magic[8];
99 	u8	otp_version[8];
100 	u32	image_info;
101 	u32	data_info;
102 	u32	config_info;
103 	u32	strap_info;
104 	u32	checksum_offset;
105 } __packed;
106 
107 struct otpstrap_status {
108 	int value;
109 	int option_array[7];
110 	int remain_times;
111 	int writeable_option;
112 	int reg_protected;
113 	int protected;
114 };
115 
116 struct otpconf_parse {
117 	int dw_offset;
118 	int bit;
119 	int length;
120 	int value;
121 	int ignore;
122 	char status[80];
123 };
124 
125 struct otpkey_type {
126 	int value;
127 	int key_type;
128 	int need_id;
129 	char information[110];
130 };
131 
132 struct otp_info_cb {
133 	int version;
134 	char ver_name[3];
135 	const struct otpstrap_info *strap_info;
136 	int strap_info_len;
137 	const struct otpconf_info *conf_info;
138 	int conf_info_len;
139 	const struct otpkey_type *key_info;
140 	int key_info_len;
141 };
142 
143 struct otp_image_layout {
144 	int data_length;
145 	int conf_length;
146 	int strap_length;
147 	u8 *data;
148 	u8 *data_ignore;
149 	u8 *conf;
150 	u8 *conf_ignore;
151 	u8 *strap;
152 	u8 *strap_reg_pro;
153 	u8 *strap_pro;
154 	u8 *strap_ignore;
155 };
156 
157 static struct otp_info_cb info_cb;
158 
159 static const struct otpkey_type a0_key_type[] = {
160 	{0, OTP_KEY_TYPE_AES,   0, "AES-256 as OEM platform key for image encryption/decryption"},
161 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
162 	{4, OTP_KEY_TYPE_HMAC,  1, "HMAC as encrypted OEM HMAC keys in Mode 1"},
163 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
164 	{9, OTP_KEY_TYPE_RSA_PUB,   0, "RSA-public as SOC public key"},
165 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
166 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as SOC private key"},
167 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
168 };
169 
170 static const struct otpkey_type a1_key_type[] = {
171 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
172 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
173 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
174 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
175 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
176 };
177 
178 static const struct otpkey_type a2_key_type[] = {
179 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
180 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
181 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
182 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
183 	{14, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
184 };
185 
186 static const struct otpkey_type a3_key_type[] = {
187 	{1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
188 	{2, OTP_KEY_TYPE_AES,   1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"},
189 	{8, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2"},
190 	{9, OTP_KEY_TYPE_RSA_PUB,   1, "RSA-public as OEM DSS public keys in Mode 2(big endian)"},
191 	{10, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key"},
192 	{11, OTP_KEY_TYPE_RSA_PUB,  0, "RSA-public as AES key decryption key(big endian)"},
193 	{12, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key"},
194 	{13, OTP_KEY_TYPE_RSA_PRIV,  0, "RSA-private as AES key decryption key(big endian)"},
195 };
196 
197 static void buf_print(u8 *buf, int len)
198 {
199 	int i;
200 
201 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
202 	for (i = 0; i < len; i++) {
203 		if (i % 16 == 0)
204 			printf("%04X: ", i);
205 		printf("%02X ", buf[i]);
206 		if ((i + 1) % 16 == 0)
207 			printf("\n");
208 	}
209 }
210 
211 static int get_dw_bit(u32 *rid, int offset)
212 {
213 	int bit_offset;
214 	int i;
215 
216 	if (offset < 32) {
217 		i = 0;
218 		bit_offset = offset;
219 	} else {
220 		i = 1;
221 		bit_offset = offset - 32;
222 	}
223 	if ((rid[i] >> bit_offset) & 0x1)
224 		return 1;
225 	else
226 		return 0;
227 }
228 
229 static int get_rid_num(u32 *rid)
230 {
231 	int i;
232 	int fz = 0;
233 	int rid_num = 0;
234 	int ret = 0;
235 
236 	for (i = 0; i < 64; i++) {
237 		if (get_dw_bit(rid, i) == 0) {
238 			if (!fz)
239 				fz = 1;
240 
241 		} else {
242 			rid_num++;
243 			if (fz)
244 				ret = OTP_FAILURE;
245 		}
246 	}
247 	if (ret)
248 		return ret;
249 
250 	return rid_num;
251 }
252 
253 static u32 chip_version(void)
254 {
255 	u32 revid0, revid1;
256 
257 	revid0 = readl(ASPEED_REVISION_ID0);
258 	revid1 = readl(ASPEED_REVISION_ID1);
259 
260 	if (revid0 == ID0_AST2600A0 && revid1 == ID1_AST2600A0) {
261 		/* AST2600-A0 */
262 		return OTP_A0;
263 	} else if (revid0 == ID0_AST2600A1 && revid1 == ID1_AST2600A1) {
264 		/* AST2600-A1 */
265 		return OTP_A1;
266 	} else if (revid0 == ID0_AST2600A2 && revid1 == ID1_AST2600A2) {
267 		/* AST2600-A2 */
268 		return OTP_A2;
269 	} else if (revid0 == ID0_AST2600A3 && revid1 == ID1_AST2600A3) {
270 		/* AST2600-A3 */
271 		return OTP_A3;
272 	} else if (revid0 == ID0_AST2620A1 && revid1 == ID1_AST2620A1) {
273 		/* AST2620-A1 */
274 		return OTP_A1;
275 	} else if (revid0 == ID0_AST2620A2 && revid1 == ID1_AST2620A2) {
276 		/* AST2620-A2 */
277 		return OTP_A2;
278 	} else if (revid0 == ID0_AST2620A3 && revid1 == ID1_AST2620A3) {
279 		/* AST2620-A3 */
280 		return OTP_A3;
281 	} else if (revid0 == ID0_AST2605A2 && revid1 == ID1_AST2605A2) {
282 		/* AST2605-A2 */
283 		return OTP_A2;
284 	} else if (revid0 == ID0_AST2605A3 && revid1 == ID1_AST2605A3) {
285 		/* AST2605-A3 */
286 		return OTP_A3;
287 	} else if (revid0 == ID0_AST2625A3 && revid1 == ID1_AST2625A3) {
288 		/* AST2605-A3 */
289 		return OTP_A3;
290 	}
291 	return OTP_FAILURE;
292 }
293 
294 static void wait_complete(void)
295 {
296 	int reg;
297 
298 	do {
299 		reg = readl(OTP_STATUS);
300 	} while ((reg & 0x6) != 0x6);
301 }
302 
303 static void otp_write(u32 otp_addr, u32 data)
304 {
305 	writel(otp_addr, OTP_ADDR); //write address
306 	writel(data, OTP_COMPARE_1); //write data
307 	writel(0x23b1e362, OTP_COMMAND); //write command
308 	wait_complete();
309 }
310 
311 static void otp_soak(int soak)
312 {
313 	if (info_cb.version == OTP_A2 || info_cb.version == OTP_A3) {
314 		switch (soak) {
315 		case 0: //default
316 			otp_write(0x3000, 0x0210); // Write MRA
317 			otp_write(0x5000, 0x2000); // Write MRB
318 			otp_write(0x1000, 0x0); // Write MR
319 			break;
320 		case 1: //normal program
321 			otp_write(0x3000, 0x1200); // Write MRA
322 			otp_write(0x5000, 0x107F); // Write MRB
323 			otp_write(0x1000, 0x1024); // Write MR
324 			writel(0x04191388, OTP_TIMING); // 200us
325 			break;
326 		case 2: //soak program
327 			otp_write(0x3000, 0x1220); // Write MRA
328 			otp_write(0x5000, 0x2074); // Write MRB
329 			otp_write(0x1000, 0x08a4); // Write MR
330 			writel(0x04193a98, OTP_TIMING); // 600us
331 			break;
332 		}
333 	} else {
334 		switch (soak) {
335 		case 0: //default
336 			otp_write(0x3000, 0x0); // Write MRA
337 			otp_write(0x5000, 0x0); // Write MRB
338 			otp_write(0x1000, 0x0); // Write MR
339 			break;
340 		case 1: //normal program
341 			otp_write(0x3000, 0x4021); // Write MRA
342 			otp_write(0x5000, 0x302f); // Write MRB
343 			otp_write(0x1000, 0x4020); // Write MR
344 			writel(0x04190760, OTP_TIMING); // 75us
345 			break;
346 		case 2: //soak program
347 			otp_write(0x3000, 0x4021); // Write MRA
348 			otp_write(0x5000, 0x1027); // Write MRB
349 			otp_write(0x1000, 0x4820); // Write MR
350 			writel(0x041930d4, OTP_TIMING); // 500us
351 			break;
352 		}
353 	}
354 
355 	wait_complete();
356 }
357 
358 static void otp_read_data(u32 offset, u32 *data)
359 {
360 	writel(offset, OTP_ADDR); //Read address
361 	writel(0x23b1e361, OTP_COMMAND); //trigger read
362 	wait_complete();
363 	data[0] = readl(OTP_COMPARE_1);
364 	data[1] = readl(OTP_COMPARE_2);
365 }
366 
367 static void otp_read_conf(u32 offset, u32 *data)
368 {
369 	int config_offset;
370 
371 	config_offset = 0x800;
372 	config_offset |= (offset / 8) * 0x200;
373 	config_offset |= (offset % 8) * 0x2;
374 
375 	writel(config_offset, OTP_ADDR);  //Read address
376 	writel(0x23b1e361, OTP_COMMAND); //trigger read
377 	wait_complete();
378 	data[0] = readl(OTP_COMPARE_1);
379 }
380 
381 static int otp_compare(u32 otp_addr, u32 addr)
382 {
383 	u32 ret;
384 	u32 *buf;
385 
386 	buf = map_physmem(addr, 16, MAP_WRBACK);
387 	printf("%08X\n", buf[0]);
388 	printf("%08X\n", buf[1]);
389 	printf("%08X\n", buf[2]);
390 	printf("%08X\n", buf[3]);
391 	writel(otp_addr, OTP_ADDR); //Compare address
392 	writel(buf[0], OTP_COMPARE_1); //Compare data 1
393 	writel(buf[1], OTP_COMPARE_2); //Compare data 2
394 	writel(buf[2], OTP_COMPARE_3); //Compare data 3
395 	writel(buf[3], OTP_COMPARE_4); //Compare data 4
396 	writel(0x23b1e363, OTP_COMMAND); //Compare command
397 	wait_complete();
398 	ret = readl(OTP_STATUS); //Compare command
399 	if (ret & 0x1)
400 		return OTP_SUCCESS;
401 	else
402 		return OTP_FAILURE;
403 }
404 
405 static int verify_bit(u32 otp_addr, int bit_offset, int value)
406 {
407 	u32 ret[2];
408 
409 	if (otp_addr % 2 == 0)
410 		writel(otp_addr, OTP_ADDR); //Read address
411 	else
412 		writel(otp_addr - 1, OTP_ADDR); //Read address
413 
414 	writel(0x23b1e361, OTP_COMMAND); //trigger read
415 	wait_complete();
416 	ret[0] = readl(OTP_COMPARE_1);
417 	ret[1] = readl(OTP_COMPARE_2);
418 
419 	if (otp_addr % 2 == 0) {
420 		if (((ret[0] >> bit_offset) & 1) == value)
421 			return OTP_SUCCESS;
422 		else
423 			return OTP_FAILURE;
424 	} else {
425 		if (((ret[1] >> bit_offset) & 1) == value)
426 			return OTP_SUCCESS;
427 		else
428 			return OTP_FAILURE;
429 	}
430 }
431 
432 static u32 verify_dw(u32 otp_addr, u32 *value, u32 *ignore, u32 *compare, int size)
433 {
434 	u32 ret[2];
435 
436 	otp_addr &= ~(1 << 15);
437 
438 	if (otp_addr % 2 == 0)
439 		writel(otp_addr, OTP_ADDR); //Read address
440 	else
441 		writel(otp_addr - 1, OTP_ADDR); //Read address
442 	writel(0x23b1e361, OTP_COMMAND); //trigger read
443 	wait_complete();
444 	ret[0] = readl(OTP_COMPARE_1);
445 	ret[1] = readl(OTP_COMPARE_2);
446 	if (size == 1) {
447 		if (otp_addr % 2 == 0) {
448 			// printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]);
449 			if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) {
450 				compare[0] = 0;
451 				return OTP_SUCCESS;
452 			}
453 			compare[0] = value[0] ^ ret[0];
454 			return OTP_FAILURE;
455 
456 		} else {
457 			// printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]);
458 			if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) {
459 				compare[0] = ~0;
460 				return OTP_SUCCESS;
461 			}
462 			compare[0] = ~(value[0] ^ ret[1]);
463 			return OTP_FAILURE;
464 		}
465 	} else if (size == 2) {
466 		// otp_addr should be even
467 		if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) {
468 			// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
469 			// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
470 			compare[0] = 0;
471 			compare[1] = ~0;
472 			return OTP_SUCCESS;
473 		}
474 		// printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]);
475 		// printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]);
476 		compare[0] = value[0] ^ ret[0];
477 		compare[1] = ~(value[1] ^ ret[1]);
478 		return OTP_FAILURE;
479 	} else {
480 		return OTP_FAILURE;
481 	}
482 }
483 
484 static void otp_prog(u32 otp_addr, u32 prog_bit)
485 {
486 	otp_write(0x0, prog_bit);
487 	writel(otp_addr, OTP_ADDR); //write address
488 	writel(prog_bit, OTP_COMPARE_1); //write data
489 	writel(0x23b1e364, OTP_COMMAND); //write command
490 	wait_complete();
491 }
492 
493 static void _otp_prog_bit(u32 value, u32 prog_address, u32 bit_offset)
494 {
495 	int prog_bit;
496 
497 	if (prog_address % 2 == 0) {
498 		if (value)
499 			prog_bit = ~(0x1 << bit_offset);
500 		else
501 			return;
502 	} else {
503 		if (info_cb.version != OTP_A3)
504 			prog_address |= 1 << 15;
505 		if (!value)
506 			prog_bit = 0x1 << bit_offset;
507 		else
508 			return;
509 	}
510 	otp_prog(prog_address, prog_bit);
511 }
512 
513 static int otp_prog_dc_b(u32 value, u32 prog_address, u32 bit_offset)
514 {
515 	int pass;
516 	int i;
517 
518 	otp_soak(1);
519 	_otp_prog_bit(value, prog_address, bit_offset);
520 	pass = 0;
521 
522 	for (i = 0; i < RETRY; i++) {
523 		if (verify_bit(prog_address, bit_offset, value) != 0) {
524 			otp_soak(2);
525 			_otp_prog_bit(value, prog_address, bit_offset);
526 			if (verify_bit(prog_address, bit_offset, value) != 0) {
527 				otp_soak(1);
528 			} else {
529 				pass = 1;
530 				break;
531 			}
532 		} else {
533 			pass = 1;
534 			break;
535 		}
536 	}
537 	if (pass)
538 		return OTP_SUCCESS;
539 
540 	return OTP_FAILURE;
541 }
542 
543 static void otp_prog_dw(u32 value, u32 ignore, u32 prog_address)
544 {
545 	int j, bit_value, prog_bit;
546 
547 	for (j = 0; j < 32; j++) {
548 		if ((ignore >> j) & 0x1)
549 			continue;
550 		bit_value = (value >> j) & 0x1;
551 		if (prog_address % 2 == 0) {
552 			if (bit_value)
553 				prog_bit = ~(0x1 << j);
554 			else
555 				continue;
556 		} else {
557 			if (info_cb.version != OTP_A3)
558 				prog_address |= 1 << 15;
559 			if (bit_value)
560 				continue;
561 			else
562 				prog_bit = 0x1 << j;
563 		}
564 		otp_prog(prog_address, prog_bit);
565 	}
566 }
567 
568 static int otp_prog_verify_2dw(u32 *data, u32 *buf, u32 *ignore_mask, u32 prog_address)
569 {
570 	int pass;
571 	int i;
572 	u32 data0_masked;
573 	u32 data1_masked;
574 	u32 buf0_masked;
575 	u32 buf1_masked;
576 	u32 compare[2];
577 
578 	data0_masked = data[0]  & ~ignore_mask[0];
579 	buf0_masked  = buf[0] & ~ignore_mask[0];
580 	data1_masked = data[1]  & ~ignore_mask[1];
581 	buf1_masked  = buf[1] & ~ignore_mask[1];
582 	if (data0_masked == buf0_masked && data1_masked == buf1_masked)
583 		return OTP_SUCCESS;
584 
585 	otp_soak(1);
586 	if (data0_masked != buf0_masked)
587 		otp_prog_dw(buf[0], ignore_mask[0], prog_address);
588 	if (data1_masked != buf1_masked)
589 		otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1);
590 
591 	pass = 0;
592 	for (i = 0; i < RETRY; i++) {
593 		if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
594 			otp_soak(2);
595 			if (compare[0] != 0)
596 				otp_prog_dw(compare[0], ignore_mask[0], prog_address);
597 			if (compare[1] != ~0)
598 				otp_prog_dw(compare[1], ignore_mask[1], prog_address + 1);
599 			if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) {
600 				otp_soak(1);
601 			} else {
602 				pass = 1;
603 				break;
604 			}
605 		} else {
606 			pass = 1;
607 			break;
608 		}
609 	}
610 
611 	if (!pass) {
612 		otp_soak(0);
613 		return OTP_FAILURE;
614 	}
615 	return OTP_SUCCESS;
616 }
617 
618 static void otp_strap_status(struct otpstrap_status *otpstrap)
619 {
620 	u32 OTPSTRAP_RAW[2];
621 	int strap_end;
622 	int i, j;
623 
624 	if (info_cb.version == OTP_A0) {
625 		for (j = 0; j < 64; j++) {
626 			otpstrap[j].value = 0;
627 			otpstrap[j].remain_times = 7;
628 			otpstrap[j].writeable_option = -1;
629 			otpstrap[j].protected = 0;
630 		}
631 		strap_end = 30;
632 	} else {
633 		for (j = 0; j < 64; j++) {
634 			otpstrap[j].value = 0;
635 			otpstrap[j].remain_times = 6;
636 			otpstrap[j].writeable_option = -1;
637 			otpstrap[j].reg_protected = 0;
638 			otpstrap[j].protected = 0;
639 		}
640 		strap_end = 28;
641 	}
642 
643 	otp_soak(0);
644 	for (i = 16; i < strap_end; i += 2) {
645 		int option = (i - 16) / 2;
646 
647 		otp_read_conf(i, &OTPSTRAP_RAW[0]);
648 		otp_read_conf(i + 1, &OTPSTRAP_RAW[1]);
649 		for (j = 0; j < 32; j++) {
650 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
651 
652 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
653 				otpstrap[j].writeable_option = option;
654 			if (bit_value == 1)
655 				otpstrap[j].remain_times--;
656 			otpstrap[j].value ^= bit_value;
657 			otpstrap[j].option_array[option] = bit_value;
658 		}
659 		for (j = 32; j < 64; j++) {
660 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
661 
662 			if (bit_value == 0 && otpstrap[j].writeable_option == -1)
663 				otpstrap[j].writeable_option = option;
664 			if (bit_value == 1)
665 				otpstrap[j].remain_times--;
666 			otpstrap[j].value ^= bit_value;
667 			otpstrap[j].option_array[option] = bit_value;
668 		}
669 	}
670 
671 	if (info_cb.version != OTP_A0) {
672 		otp_read_conf(28, &OTPSTRAP_RAW[0]);
673 		otp_read_conf(29, &OTPSTRAP_RAW[1]);
674 		for (j = 0; j < 32; j++) {
675 			if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
676 				otpstrap[j].reg_protected = 1;
677 		}
678 		for (j = 32; j < 64; j++) {
679 			if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
680 				otpstrap[j].reg_protected = 1;
681 		}
682 	}
683 
684 	otp_read_conf(30, &OTPSTRAP_RAW[0]);
685 	otp_read_conf(31, &OTPSTRAP_RAW[1]);
686 	for (j = 0; j < 32; j++) {
687 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
688 			otpstrap[j].protected = 1;
689 	}
690 	for (j = 32; j < 64; j++) {
691 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
692 			otpstrap[j].protected = 1;
693 	}
694 }
695 
696 static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit)
697 {
698 	int prog_flag = 0;
699 
700 	// ignore this bit
701 	if (ibit == 1)
702 		return OTP_SUCCESS;
703 	printf("OTPSTRAP[%X]:\n", offset);
704 
705 	if (bit == otpstrap->value) {
706 		if (!pbit && !rpbit) {
707 			printf("    The value is same as before, skip it.\n");
708 			return OTP_PROG_SKIP;
709 		}
710 		printf("    The value is same as before.\n");
711 	} else {
712 		prog_flag = 1;
713 	}
714 	if (otpstrap->protected == 1 && prog_flag) {
715 		printf("    This bit is protected and is not writable\n");
716 		return OTP_FAILURE;
717 	}
718 	if (otpstrap->remain_times == 0 && prog_flag) {
719 		printf("    This bit is no remaining times to write.\n");
720 		return OTP_FAILURE;
721 	}
722 	if (pbit == 1)
723 		printf("    This bit will be protected and become non-writable.\n");
724 	if (rpbit == 1 && info_cb.version != OTP_A0)
725 		printf("    The relative register will be protected.\n");
726 	if (prog_flag)
727 		printf("    Write 1 to OTPSTRAP[%X] OPTION[%X], that value becomes from %d to %d.\n", offset, otpstrap->writeable_option + 1, otpstrap->value, otpstrap->value ^ 1);
728 
729 	return OTP_SUCCESS;
730 }
731 
732 static int otp_prog_strap_b(int bit_offset, int value)
733 {
734 	struct otpstrap_status otpstrap[64];
735 	u32 prog_address;
736 	int offset;
737 	int ret;
738 
739 	otp_strap_status(otpstrap);
740 
741 	ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
742 
743 	if (ret != OTP_SUCCESS)
744 		return ret;
745 
746 	prog_address = 0x800;
747 	if (bit_offset < 32) {
748 		offset = bit_offset;
749 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200;
750 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2;
751 
752 	} else {
753 		offset = (bit_offset - 32);
754 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200;
755 		prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2;
756 	}
757 
758 	return otp_prog_dc_b(1, prog_address, offset);
759 }
760 
761 static int otp_print_conf(u32 offset, int dw_count)
762 {
763 	int i;
764 	u32 ret[1];
765 
766 	if (offset + dw_count > 32)
767 		return OTP_USAGE;
768 	otp_soak(0);
769 	for (i = offset; i < offset + dw_count; i++) {
770 		otp_read_conf(i, ret);
771 		printf("OTPCFG%X: %08X\n", i, ret[0]);
772 	}
773 	printf("\n");
774 	return OTP_SUCCESS;
775 }
776 
777 static int otp_print_data(u32 offset, int dw_count)
778 {
779 	int i;
780 	u32 ret[2];
781 
782 	if (offset + dw_count > 2048 || offset % 4 != 0)
783 		return OTP_USAGE;
784 	otp_soak(0);
785 	for (i = offset; i < offset + dw_count; i += 2) {
786 		otp_read_data(i, ret);
787 		if (i % 4 == 0)
788 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
789 		else
790 			printf("%08X %08X\n", ret[0], ret[1]);
791 	}
792 	printf("\n");
793 	return OTP_SUCCESS;
794 }
795 
796 static int otp_print_strap(int start, int count)
797 {
798 	int i, j;
799 	int remains;
800 	struct otpstrap_status otpstrap[64];
801 
802 	if (start < 0 || start > 64)
803 		return OTP_USAGE;
804 
805 	if ((start + count) < 0 || (start + count) > 64)
806 		return OTP_USAGE;
807 
808 	otp_strap_status(otpstrap);
809 
810 	if (info_cb.version == OTP_A0) {
811 		remains = 7;
812 		printf("BIT(hex)  Value  Option           Status\n");
813 	} else {
814 		remains = 6;
815 		printf("BIT(hex)  Value  Option         Reg_Protect Status\n");
816 	}
817 	printf("______________________________________________________________________________\n");
818 
819 	for (i = start; i < start + count; i++) {
820 		printf("0x%-8X", i);
821 		printf("%-7d", otpstrap[i].value);
822 		for (j = 0; j < remains; j++)
823 			printf("%d ", otpstrap[i].option_array[j]);
824 		printf("   ");
825 		if (info_cb.version != OTP_A0)
826 			printf("%d           ", otpstrap[i].reg_protected);
827 		if (otpstrap[i].protected == 1) {
828 			printf("protected and not writable");
829 		} else {
830 			printf("not protected ");
831 			if (otpstrap[i].remain_times == 0)
832 				printf("and no remaining times to write.");
833 			else
834 				printf("and still can write %d times", otpstrap[i].remain_times);
835 		}
836 		printf("\n");
837 	}
838 
839 	return OTP_SUCCESS;
840 }
841 
842 static void otp_print_revid(u32 *rid)
843 {
844 	int bit_offset;
845 	int i, j;
846 
847 	printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
848 	printf("___________________________________________________\n");
849 	for (i = 0; i < 64; i++) {
850 		if (i < 32) {
851 			j = 0;
852 			bit_offset = i;
853 		} else {
854 			j = 1;
855 			bit_offset = i - 32;
856 		}
857 		if (i % 16 == 0)
858 			printf("%2x | ", i);
859 		printf("%d  ", (rid[j] >> bit_offset) & 0x1);
860 		if ((i + 1) % 16 == 0)
861 			printf("\n");
862 	}
863 }
864 
865 static int otp_print_conf_image(struct otp_image_layout *image_layout)
866 {
867 	const struct otpconf_info *conf_info = info_cb.conf_info;
868 	u32 *OTPCFG = (u32 *)image_layout->conf;
869 	u32 *OTPCFG_IGNORE = (u32 *)image_layout->conf_ignore;
870 	u32 mask;
871 	u32 dw_offset;
872 	u32 bit_offset;
873 	u32 otp_value;
874 	u32 otp_ignore;
875 	int fail = 0;
876 	int rid_num = 0;
877 	char valid_bit[20];
878 	int fz;
879 	int i;
880 	int j;
881 
882 	printf("DW    BIT        Value       Description\n");
883 	printf("__________________________________________________________________________\n");
884 	for (i = 0; i < info_cb.conf_info_len; i++) {
885 		dw_offset = conf_info[i].dw_offset;
886 		bit_offset = conf_info[i].bit_offset;
887 		mask = BIT(conf_info[i].length) - 1;
888 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
889 		otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask;
890 
891 		if (otp_ignore == mask)
892 			continue;
893 		else if (otp_ignore != 0)
894 			fail = 1;
895 
896 		if (otp_value != conf_info[i].value &&
897 		    conf_info[i].value != OTP_REG_RESERVED &&
898 		    conf_info[i].value != OTP_REG_VALUE &&
899 		    conf_info[i].value != OTP_REG_VALID_BIT)
900 			continue;
901 		printf("0x%-4X", dw_offset);
902 
903 		if (conf_info[i].length == 1) {
904 			printf("0x%-9X", conf_info[i].bit_offset);
905 		} else {
906 			printf("0x%-2X:0x%-4X",
907 			       conf_info[i].bit_offset + conf_info[i].length - 1,
908 			       conf_info[i].bit_offset);
909 		}
910 		printf("0x%-10x", otp_value);
911 
912 		if (fail) {
913 			printf("Ignore mask error\n");
914 			continue;
915 		}
916 		if (conf_info[i].value == OTP_REG_RESERVED) {
917 			printf("Reserved\n");
918 		} else if (conf_info[i].value == OTP_REG_VALUE) {
919 			printf(conf_info[i].information, otp_value);
920 			printf("\n");
921 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
922 			if (otp_value != 0) {
923 				for (j = 0; j < 7; j++) {
924 					if (otp_value == (1 << j))
925 						valid_bit[j * 2] = '1';
926 					else
927 						valid_bit[j * 2] = '0';
928 					valid_bit[j * 2 + 1] = ' ';
929 				}
930 				valid_bit[15] = 0;
931 			} else {
932 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
933 			}
934 			printf(conf_info[i].information, valid_bit);
935 			printf("\n");
936 		} else {
937 			printf("%s\n", conf_info[i].information);
938 		}
939 	}
940 
941 	if (OTPCFG[0xa] != 0 || OTPCFG[0xb] != 0) {
942 		if (OTPCFG_IGNORE[0xa] != 0 && OTPCFG_IGNORE[0xb] != 0) {
943 			printf("OTP revision ID is invalid.\n");
944 			fail = 1;
945 		} else {
946 			fz = 0;
947 			for (i = 0; i < 64; i++) {
948 				if (get_dw_bit(&OTPCFG[0xa], i) == 0) {
949 					if (!fz)
950 						fz = 1;
951 				} else {
952 					rid_num++;
953 					if (fz) {
954 						printf("OTP revision ID is invalid.\n");
955 						fail = 1;
956 						break;
957 					}
958 				}
959 			}
960 		}
961 		if (fail)
962 			printf("OTP revision ID\n");
963 		else
964 			printf("OTP revision ID: 0x%x\n", rid_num);
965 		otp_print_revid(&OTPCFG[0xa]);
966 	}
967 
968 	if (fail)
969 		return OTP_FAILURE;
970 
971 	return OTP_SUCCESS;
972 }
973 
974 static int otp_print_conf_info(int input_offset)
975 {
976 	const struct otpconf_info *conf_info = info_cb.conf_info;
977 	u32 OTPCFG[16];
978 	u32 mask;
979 	u32 dw_offset;
980 	u32 bit_offset;
981 	u32 otp_value;
982 	char valid_bit[20];
983 	int i;
984 	int j;
985 
986 	otp_soak(0);
987 	for (i = 0; i < 16; i++)
988 		otp_read_conf(i, &OTPCFG[i]);
989 
990 	printf("DW    BIT        Value       Description\n");
991 	printf("__________________________________________________________________________\n");
992 	for (i = 0; i < info_cb.conf_info_len; i++) {
993 		if (input_offset != -1 && input_offset != conf_info[i].dw_offset)
994 			continue;
995 		dw_offset = conf_info[i].dw_offset;
996 		bit_offset = conf_info[i].bit_offset;
997 		mask = BIT(conf_info[i].length) - 1;
998 		otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask;
999 
1000 		if (otp_value != conf_info[i].value &&
1001 		    conf_info[i].value != OTP_REG_RESERVED &&
1002 		    conf_info[i].value != OTP_REG_VALUE &&
1003 		    conf_info[i].value != OTP_REG_VALID_BIT)
1004 			continue;
1005 		printf("0x%-4X", dw_offset);
1006 
1007 		if (conf_info[i].length == 1) {
1008 			printf("0x%-9X", conf_info[i].bit_offset);
1009 		} else {
1010 			printf("0x%-2X:0x%-4X",
1011 			       conf_info[i].bit_offset + conf_info[i].length - 1,
1012 			       conf_info[i].bit_offset);
1013 		}
1014 		printf("0x%-10x", otp_value);
1015 
1016 		if (conf_info[i].value == OTP_REG_RESERVED) {
1017 			printf("Reserved\n");
1018 		} else if (conf_info[i].value == OTP_REG_VALUE) {
1019 			printf(conf_info[i].information, otp_value);
1020 			printf("\n");
1021 		} else if (conf_info[i].value == OTP_REG_VALID_BIT) {
1022 			if (otp_value != 0) {
1023 				for (j = 0; j < 7; j++) {
1024 					if (otp_value == (1 << j))
1025 						valid_bit[j * 2] = '1';
1026 					else
1027 						valid_bit[j * 2] = '0';
1028 					valid_bit[j * 2 + 1] = ' ';
1029 				}
1030 				valid_bit[15] = 0;
1031 			} else {
1032 				strcpy(valid_bit, "0 0 0 0 0 0 0 0\0");
1033 			}
1034 			printf(conf_info[i].information, valid_bit);
1035 			printf("\n");
1036 		} else {
1037 			printf("%s\n", conf_info[i].information);
1038 		}
1039 	}
1040 	return OTP_SUCCESS;
1041 }
1042 
1043 static int otp_print_strap_image(struct otp_image_layout *image_layout)
1044 {
1045 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1046 	u32 *OTPSTRAP;
1047 	u32 *OTPSTRAP_REG_PRO;
1048 	u32 *OTPSTRAP_PRO;
1049 	u32 *OTPSTRAP_IGNORE;
1050 	int i;
1051 	int fail = 0;
1052 	u32 bit_offset;
1053 	u32 dw_offset;
1054 	u32 mask;
1055 	u32 otp_value;
1056 	u32 otp_reg_protect;
1057 	u32 otp_protect;
1058 	u32 otp_ignore;
1059 
1060 	OTPSTRAP = (u32 *)image_layout->strap;
1061 	OTPSTRAP_PRO = (u32 *)image_layout->strap_pro;
1062 	OTPSTRAP_IGNORE = (u32 *)image_layout->strap_ignore;
1063 	if (info_cb.version == OTP_A0) {
1064 		OTPSTRAP_REG_PRO = NULL;
1065 		printf("BIT(hex)   Value       Protect     Description\n");
1066 	} else {
1067 		OTPSTRAP_REG_PRO = (u32 *)image_layout->strap_reg_pro;
1068 		printf("BIT(hex)   Value       Reg_Protect Protect     Description\n");
1069 	}
1070 	printf("__________________________________________________________________________________________\n");
1071 
1072 	for (i = 0; i < info_cb.strap_info_len; i++) {
1073 		if (strap_info[i].bit_offset > 31) {
1074 			dw_offset = 1;
1075 			bit_offset = strap_info[i].bit_offset - 32;
1076 		} else {
1077 			dw_offset = 0;
1078 			bit_offset = strap_info[i].bit_offset;
1079 		}
1080 
1081 		mask = BIT(strap_info[i].length) - 1;
1082 		otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask;
1083 		otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask;
1084 		otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask;
1085 
1086 		if (info_cb.version != OTP_A0)
1087 			otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask;
1088 		else
1089 			otp_reg_protect = 0;
1090 
1091 		if (otp_ignore == mask)
1092 			continue;
1093 		else if (otp_ignore != 0)
1094 			fail = 1;
1095 
1096 		if (otp_value != strap_info[i].value &&
1097 		    strap_info[i].value != OTP_REG_RESERVED)
1098 			continue;
1099 
1100 		if (strap_info[i].length == 1) {
1101 			printf("0x%-9X", strap_info[i].bit_offset);
1102 		} else {
1103 			printf("0x%-2X:0x%-4X",
1104 			       strap_info[i].bit_offset + strap_info[i].length - 1,
1105 			       strap_info[i].bit_offset);
1106 		}
1107 		printf("0x%-10x", otp_value);
1108 		if (info_cb.version != OTP_A0)
1109 			printf("0x%-10x", otp_reg_protect);
1110 		printf("0x%-10x", otp_protect);
1111 
1112 		if (fail) {
1113 			printf("Ignore mask error\n");
1114 		} else {
1115 			if (strap_info[i].value != OTP_REG_RESERVED)
1116 				printf("%s\n", strap_info[i].information);
1117 			else
1118 				printf("Reserved\n");
1119 		}
1120 	}
1121 
1122 	if (fail)
1123 		return OTP_FAILURE;
1124 
1125 	return OTP_SUCCESS;
1126 }
1127 
1128 static int otp_print_strap_info(int view)
1129 {
1130 	const struct otpstrap_info *strap_info = info_cb.strap_info;
1131 	struct otpstrap_status strap_status[64];
1132 	int i, j;
1133 	int fail = 0;
1134 	u32 bit_offset;
1135 	u32 length;
1136 	u32 otp_value;
1137 	u32 otp_protect;
1138 
1139 	otp_strap_status(strap_status);
1140 
1141 	if (view) {
1142 		if (info_cb.version == OTP_A0)
1143 			printf("BIT(hex) Value  Remains  Protect   Description\n");
1144 		else
1145 			printf("BIT(hex) Value  Remains  Reg_Protect Protect   Description\n");
1146 		printf("___________________________________________________________________________________________________\n");
1147 	} else {
1148 		printf("BIT(hex)   Value       Description\n");
1149 		printf("________________________________________________________________________________\n");
1150 	}
1151 	for (i = 0; i < info_cb.strap_info_len; i++) {
1152 		otp_value = 0;
1153 		bit_offset = strap_info[i].bit_offset;
1154 		length = strap_info[i].length;
1155 		for (j = 0; j < length; j++) {
1156 			otp_value |= strap_status[bit_offset + j].value << j;
1157 			otp_protect |= strap_status[bit_offset + j].protected << j;
1158 		}
1159 		if (otp_value != strap_info[i].value &&
1160 		    strap_info[i].value != OTP_REG_RESERVED)
1161 			continue;
1162 		if (view) {
1163 			for (j = 0; j < length; j++) {
1164 				printf("0x%-7X", strap_info[i].bit_offset + j);
1165 				printf("0x%-5X", strap_status[bit_offset + j].value);
1166 				printf("%-9d", strap_status[bit_offset + j].remain_times);
1167 				if (info_cb.version != OTP_A0)
1168 					printf("0x%-10X", strap_status[bit_offset + j].reg_protected);
1169 				printf("0x%-7X", strap_status[bit_offset + j].protected);
1170 				if (strap_info[i].value == OTP_REG_RESERVED) {
1171 					printf(" Reserved\n");
1172 					continue;
1173 				}
1174 				if (length == 1) {
1175 					printf(" %s\n", strap_info[i].information);
1176 					continue;
1177 				}
1178 
1179 				if (j == 0)
1180 					printf("/%s\n", strap_info[i].information);
1181 				else if (j == length - 1)
1182 					printf("\\ \"\n");
1183 				else
1184 					printf("| \"\n");
1185 			}
1186 		} else {
1187 			if (length == 1) {
1188 				printf("0x%-9X", strap_info[i].bit_offset);
1189 			} else {
1190 				printf("0x%-2X:0x%-4X",
1191 				       bit_offset + length - 1, bit_offset);
1192 			}
1193 
1194 			printf("0x%-10X", otp_value);
1195 
1196 			if (strap_info[i].value != OTP_REG_RESERVED)
1197 				printf("%s\n", strap_info[i].information);
1198 			else
1199 				printf("Reserved\n");
1200 		}
1201 	}
1202 
1203 	if (fail)
1204 		return OTP_FAILURE;
1205 
1206 	return OTP_SUCCESS;
1207 }
1208 
1209 static int otp_print_data_image(struct otp_image_layout *image_layout)
1210 {
1211 	int key_id, key_offset, last, key_type, key_length, exp_length;
1212 	const struct otpkey_type *key_info_array = info_cb.key_info;
1213 	struct otpkey_type key_info;
1214 	u32 *buf;
1215 	u8 *byte_buf;
1216 	char empty = 1;
1217 	int i = 0, len = 0;
1218 	int j;
1219 
1220 	byte_buf = image_layout->data;
1221 	buf = (u32 *)byte_buf;
1222 
1223 	for (i = 0; i < 16; i++) {
1224 		if (buf[i] != 0)
1225 			empty = 0;
1226 	}
1227 	if (empty)
1228 		return OTP_SUCCESS;
1229 
1230 	i = 0;
1231 	while (1) {
1232 		key_id = buf[i] & 0x7;
1233 		key_offset = buf[i] & 0x1ff8;
1234 		last = (buf[i] >> 13) & 1;
1235 		key_type = (buf[i] >> 14) & 0xf;
1236 		key_length = (buf[i] >> 18) & 0x3;
1237 		exp_length = (buf[i] >> 20) & 0xfff;
1238 
1239 		for (j = 0; j < info_cb.key_info_len; j++) {
1240 			if (key_type == key_info_array[j].value) {
1241 				key_info = key_info_array[j];
1242 				break;
1243 			}
1244 		}
1245 
1246 		printf("\nKey[%d]:\n", i);
1247 		printf("Key Type: ");
1248 		printf("%s\n", key_info.information);
1249 
1250 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
1251 			printf("HMAC SHA Type: ");
1252 			switch (key_length) {
1253 			case 0:
1254 				printf("HMAC(SHA224)\n");
1255 				break;
1256 			case 1:
1257 				printf("HMAC(SHA256)\n");
1258 				break;
1259 			case 2:
1260 				printf("HMAC(SHA384)\n");
1261 				break;
1262 			case 3:
1263 				printf("HMAC(SHA512)\n");
1264 				break;
1265 			}
1266 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV ||
1267 			   key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1268 			printf("RSA SHA Type: ");
1269 			switch (key_length) {
1270 			case 0:
1271 				printf("RSA1024\n");
1272 				len = 0x100;
1273 				break;
1274 			case 1:
1275 				printf("RSA2048\n");
1276 				len = 0x200;
1277 				break;
1278 			case 2:
1279 				printf("RSA3072\n");
1280 				len = 0x300;
1281 				break;
1282 			case 3:
1283 				printf("RSA4096\n");
1284 				len = 0x400;
1285 				break;
1286 			}
1287 			printf("RSA exponent bit length: %d\n", exp_length);
1288 		}
1289 		if (key_info.need_id)
1290 			printf("Key Number ID: %d\n", key_id);
1291 		printf("Key Value:\n");
1292 		if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
1293 			buf_print(&byte_buf[key_offset], 0x40);
1294 		} else if (key_info.key_type == OTP_KEY_TYPE_AES) {
1295 			printf("AES Key:\n");
1296 			buf_print(&byte_buf[key_offset], 0x20);
1297 			if (info_cb.version == OTP_A0) {
1298 				printf("AES IV:\n");
1299 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
1300 			}
1301 
1302 		} else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
1303 			if (info_cb.version == OTP_A0) {
1304 				printf("AES Key:\n");
1305 				buf_print(&byte_buf[key_offset], 0x20);
1306 				printf("AES IV:\n");
1307 				buf_print(&byte_buf[key_offset + 0x20], 0x10);
1308 			} else {
1309 				printf("AES Key 1:\n");
1310 				buf_print(&byte_buf[key_offset], 0x20);
1311 				printf("AES Key 2:\n");
1312 				buf_print(&byte_buf[key_offset + 0x20], 0x20);
1313 			}
1314 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV) {
1315 			printf("RSA mod:\n");
1316 			buf_print(&byte_buf[key_offset], len / 2);
1317 			printf("RSA exp:\n");
1318 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
1319 		} else if (key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1320 			printf("RSA mod:\n");
1321 			buf_print(&byte_buf[key_offset], len / 2);
1322 			printf("RSA exp:\n");
1323 			buf_print((u8 *)"\x01\x00\x01", 3);
1324 		}
1325 		if (last)
1326 			break;
1327 		i++;
1328 	}
1329 	return OTP_SUCCESS;
1330 }
1331 
1332 static int otp_strap_image_confirm(struct otp_image_layout *image_layout)
1333 {
1334 	int i;
1335 	u32 *strap;
1336 	u32 *strap_ignore;
1337 	u32 *strap_reg_protect;
1338 	u32 *strap_pro;
1339 	int bit, pbit, ibit, rpbit;
1340 	int fail = 0;
1341 	int ret;
1342 	struct otpstrap_status otpstrap[64];
1343 
1344 	strap = (u32 *)image_layout->strap;
1345 	strap_pro = (u32 *)image_layout->strap_pro;
1346 	strap_ignore = (u32 *)image_layout->strap_ignore;
1347 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1348 
1349 	otp_strap_status(otpstrap);
1350 	for (i = 0; i < 64; i++) {
1351 		if (i < 32) {
1352 			bit = (strap[0] >> i) & 0x1;
1353 			ibit = (strap_ignore[0] >> i) & 0x1;
1354 			pbit = (strap_pro[0] >> i) & 0x1;
1355 		} else {
1356 			bit = (strap[1] >> (i - 32)) & 0x1;
1357 			ibit = (strap_ignore[1] >> (i - 32)) & 0x1;
1358 			pbit = (strap_pro[1] >> (i - 32)) & 0x1;
1359 		}
1360 
1361 		if (info_cb.version != OTP_A0) {
1362 			if (i < 32)
1363 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1364 			else
1365 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1366 		} else {
1367 			rpbit = 0;
1368 		}
1369 		ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit);
1370 
1371 		if (ret == OTP_FAILURE)
1372 			fail = 1;
1373 	}
1374 	if (fail == 1)
1375 		return OTP_FAILURE;
1376 	else
1377 		return OTP_SUCCESS;
1378 }
1379 
1380 static int otp_prog_data(struct otp_image_layout *image_layout)
1381 {
1382 	int i;
1383 	int ret;
1384 	int data_dw;
1385 	u32 data[2048];
1386 	u32 *buf;
1387 	u32 *buf_ignore;
1388 	u32 data_masked;
1389 	u32 buf_masked;
1390 
1391 	buf = (u32 *)image_layout->data;
1392 	buf_ignore = (u32 *)image_layout->data_ignore;
1393 
1394 	data_dw = image_layout->data_length / 4;
1395 
1396 	printf("Read OTP Data:\n");
1397 
1398 	for (i = 0; i < data_dw - 2 ; i += 2)
1399 		otp_read_data(i, &data[i]);
1400 
1401 	printf("Check writable...\n");
1402 	// ignore last two dw, the last two dw is used for slt otp write check.
1403 	for (i = 0; i < data_dw - 2; i++) {
1404 		data_masked = data[i]  & ~buf_ignore[i];
1405 		buf_masked  = buf[i] & ~buf_ignore[i];
1406 		if (data_masked == buf_masked)
1407 			continue;
1408 		if (i % 2 == 0) {
1409 			if ((data_masked | buf_masked) == buf_masked) {
1410 				continue;
1411 			} else {
1412 				printf("Input image can't program into OTP, please check.\n");
1413 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1414 				printf("Input   [%x] = %x\n", i, buf[i]);
1415 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1416 				return OTP_FAILURE;
1417 			}
1418 		} else {
1419 			if ((data_masked & buf_masked) == buf_masked) {
1420 				continue;
1421 			} else {
1422 				printf("Input image can't program into OTP, please check.\n");
1423 				printf("OTP_ADDR[%x] = %x\n", i, data[i]);
1424 				printf("Input   [%x] = %x\n", i, buf[i]);
1425 				printf("Mask    [%x] = %x\n", i, ~buf_ignore[i]);
1426 				return OTP_FAILURE;
1427 			}
1428 		}
1429 	}
1430 
1431 	printf("Start Programing...\n");
1432 
1433 	// programing ecc region first
1434 	for (i = 1792; i < 2046; i += 2) {
1435 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1436 		if (ret != OTP_SUCCESS) {
1437 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1438 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1439 			return ret;
1440 		}
1441 	}
1442 
1443 	for (i = 0; i < 1792; i += 2) {
1444 		ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i);
1445 		if (ret != OTP_SUCCESS) {
1446 			printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n",
1447 			       i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]);
1448 			return ret;
1449 		}
1450 	}
1451 	otp_soak(0);
1452 	return OTP_SUCCESS;
1453 }
1454 
1455 static int otp_prog_strap(struct otp_image_layout *image_layout)
1456 {
1457 	u32 *strap;
1458 	u32 *strap_ignore;
1459 	u32 *strap_pro;
1460 	u32 *strap_reg_protect;
1461 	u32 prog_address;
1462 	int i;
1463 	int bit, pbit, ibit, offset, rpbit;
1464 	int fail = 0;
1465 	int ret;
1466 	int prog_flag = 0;
1467 	struct otpstrap_status otpstrap[64];
1468 
1469 	strap = (u32 *)image_layout->strap;
1470 	strap_pro = (u32 *)image_layout->strap_pro;
1471 	strap_ignore = (u32 *)image_layout->strap_ignore;
1472 	strap_reg_protect = (u32 *)image_layout->strap_reg_pro;
1473 
1474 	printf("Read OTP Strap Region:\n");
1475 	otp_strap_status(otpstrap);
1476 
1477 	printf("Check writable...\n");
1478 	if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) {
1479 		printf("Input image can't program into OTP, please check.\n");
1480 		return OTP_FAILURE;
1481 	}
1482 
1483 	for (i = 0; i < 64; i++) {
1484 		prog_address = 0x800;
1485 		if (i < 32) {
1486 			offset = i;
1487 			bit = (strap[0] >> offset) & 0x1;
1488 			ibit = (strap_ignore[0] >> offset) & 0x1;
1489 			pbit = (strap_pro[0] >> offset) & 0x1;
1490 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
1491 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
1492 
1493 		} else {
1494 			offset = (i - 32);
1495 			bit = (strap[1] >> offset) & 0x1;
1496 			ibit = (strap_ignore[1] >> offset) & 0x1;
1497 			pbit = (strap_pro[1] >> offset) & 0x1;
1498 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
1499 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
1500 		}
1501 		if (info_cb.version != OTP_A0) {
1502 			if (i < 32)
1503 				rpbit = (strap_reg_protect[0] >> i) & 0x1;
1504 			else
1505 				rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1;
1506 		} else {
1507 			rpbit = 0;
1508 		}
1509 
1510 		if (ibit == 1)
1511 			continue;
1512 		if (bit == otpstrap[i].value)
1513 			prog_flag = 0;
1514 		else
1515 			prog_flag = 1;
1516 
1517 		if (otpstrap[i].protected == 1 && prog_flag) {
1518 			fail = 1;
1519 			continue;
1520 		}
1521 		if (otpstrap[i].remain_times == 0 && prog_flag) {
1522 			fail = 1;
1523 			continue;
1524 		}
1525 
1526 		if (prog_flag) {
1527 			ret = otp_prog_dc_b(1, prog_address, offset);
1528 			if (ret)
1529 				return OTP_FAILURE;
1530 		}
1531 
1532 		if (rpbit == 1 && info_cb.version != OTP_A0) {
1533 			prog_address = 0x800;
1534 			if (i < 32)
1535 				prog_address |= 0x608;
1536 			else
1537 				prog_address |= 0x60a;
1538 
1539 			ret = otp_prog_dc_b(1, prog_address, offset);
1540 			if (ret)
1541 				return OTP_FAILURE;
1542 		}
1543 
1544 		if (pbit != 0) {
1545 			prog_address = 0x800;
1546 			if (i < 32)
1547 				prog_address |= 0x60c;
1548 			else
1549 				prog_address |= 0x60e;
1550 
1551 			ret = otp_prog_dc_b(1, prog_address, offset);
1552 			if (ret)
1553 				return OTP_FAILURE;
1554 		}
1555 	}
1556 	otp_soak(0);
1557 	if (fail == 1)
1558 		return OTP_FAILURE;
1559 	return OTP_SUCCESS;
1560 }
1561 
1562 static int otp_prog_conf(struct otp_image_layout *image_layout)
1563 {
1564 	int i, k;
1565 	int pass = 0;
1566 	u32 prog_address;
1567 	u32 data[16];
1568 	u32 compare[2];
1569 	u32 *conf = (u32 *)image_layout->conf;
1570 	u32 *conf_ignore = (u32 *)image_layout->conf_ignore;
1571 	u32 data_masked;
1572 	u32 buf_masked;
1573 
1574 	printf("Read OTP Config Region:\n");
1575 
1576 	for (i = 0; i < 16 ; i++) {
1577 		prog_address = 0x800;
1578 		prog_address |= (i / 8) * 0x200;
1579 		prog_address |= (i % 8) * 0x2;
1580 		otp_read_data(prog_address, &data[i]);
1581 	}
1582 
1583 	printf("Check writable...\n");
1584 	for (i = 0; i < 16; i++) {
1585 		data_masked = data[i]  & ~conf_ignore[i];
1586 		buf_masked  = conf[i] & ~conf_ignore[i];
1587 		if (data_masked == buf_masked)
1588 			continue;
1589 		if ((data_masked | buf_masked) == buf_masked) {
1590 			continue;
1591 		} else {
1592 			printf("Input image can't program into OTP, please check.\n");
1593 			printf("OTPCFG[%X] = %x\n", i, data[i]);
1594 			printf("Input [%X] = %x\n", i, conf[i]);
1595 			printf("Mask  [%X] = %x\n", i, ~conf_ignore[i]);
1596 			return OTP_FAILURE;
1597 		}
1598 	}
1599 
1600 	printf("Start Programing...\n");
1601 	otp_soak(0);
1602 	for (i = 0; i < 16; i++) {
1603 		data_masked = data[i]  & ~conf_ignore[i];
1604 		buf_masked  = conf[i] & ~conf_ignore[i];
1605 		prog_address = 0x800;
1606 		prog_address |= (i / 8) * 0x200;
1607 		prog_address |= (i % 8) * 0x2;
1608 		if (data_masked == buf_masked) {
1609 			pass = 1;
1610 			continue;
1611 		}
1612 
1613 		otp_soak(1);
1614 		otp_prog_dw(conf[i], conf_ignore[i], prog_address);
1615 
1616 		pass = 0;
1617 		for (k = 0; k < RETRY; k++) {
1618 			if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1619 				otp_soak(2);
1620 				otp_prog_dw(compare[0], conf_ignore[i], prog_address);
1621 				if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) {
1622 					otp_soak(1);
1623 				} else {
1624 					pass = 1;
1625 					break;
1626 				}
1627 			} else {
1628 				pass = 1;
1629 				break;
1630 			}
1631 		}
1632 		if (pass == 0) {
1633 			printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n",
1634 			       i, data[i], conf[i], conf_ignore[i]);
1635 			break;
1636 		}
1637 	}
1638 
1639 	otp_soak(0);
1640 	if (!pass)
1641 		return OTP_FAILURE;
1642 
1643 	return OTP_SUCCESS;
1644 }
1645 
1646 static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf)
1647 {
1648 	sha256_context ctx;
1649 	u8 digest_ret[CHECKSUM_LEN];
1650 
1651 	sha256_starts(&ctx);
1652 	sha256_update(&ctx, src_buf, length);
1653 	sha256_finish(&ctx, digest_ret);
1654 
1655 	if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN))
1656 		return OTP_SUCCESS;
1657 	return OTP_FAILURE;
1658 }
1659 
1660 static int otp_prog_image(int addr, int nconfirm)
1661 {
1662 	int ret;
1663 	int image_version = 0;
1664 	struct otp_header *otp_header;
1665 	struct otp_image_layout image_layout;
1666 	int image_size;
1667 	u8 *buf;
1668 	u8 *checksum;
1669 
1670 	otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1671 	if (!otp_header) {
1672 		puts("Failed to map physical memory\n");
1673 		return OTP_FAILURE;
1674 	}
1675 
1676 	image_size = OTP_IMAGE_SIZE(otp_header->image_info);
1677 	unmap_physmem(otp_header, MAP_WRBACK);
1678 
1679 	buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK);
1680 
1681 	if (!buf) {
1682 		puts("Failed to map physical memory\n");
1683 		return OTP_FAILURE;
1684 	}
1685 	otp_header = (struct otp_header *)buf;
1686 	checksum = buf + otp_header->checksum_offset;
1687 
1688 	if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) {
1689 		puts("Image is invalid\n");
1690 		return OTP_FAILURE;
1691 	}
1692 
1693 	image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2);
1694 	image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info);
1695 	image_layout.data_ignore = image_layout.data + image_layout.data_length;
1696 
1697 	image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2);
1698 	image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
1699 	image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1700 
1701 	image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1702 
1703 	if (!strcmp("A0", (char *)otp_header->otp_version)) {
1704 		image_version = OTP_A0;
1705 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
1706 		image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
1707 		image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1708 	} else if (!strcmp("A1", (char *)otp_header->otp_version)) {
1709 		image_version = OTP_A1;
1710 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
1711 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
1712 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
1713 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1714 	} else if (!strcmp("A2", (char *)otp_header->otp_version)) {
1715 		image_version = OTP_A2;
1716 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
1717 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
1718 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
1719 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1720 	} else if (!strcmp("A3", (char *)otp_header->otp_version)) {
1721 		image_version = OTP_A3;
1722 		image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4);
1723 		image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length;
1724 		image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length;
1725 		image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length;
1726 	} else {
1727 		puts("Version is not supported\n");
1728 		return OTP_FAILURE;
1729 	}
1730 
1731 	if (image_version != info_cb.version) {
1732 		puts("Version is not match\n");
1733 		return OTP_FAILURE;
1734 	}
1735 
1736 	if (otp_verify_image(buf, image_size, checksum)) {
1737 		puts("checksum is invalid\n");
1738 		return OTP_FAILURE;
1739 	}
1740 
1741 	if (!nconfirm) {
1742 		if (otp_header->image_info & OTP_INC_DATA) {
1743 			printf("\nOTP data region :\n");
1744 			if (otp_print_data_image(&image_layout) < 0) {
1745 				printf("OTP data error, please check.\n");
1746 				return OTP_FAILURE;
1747 			}
1748 		}
1749 		if (otp_header->image_info & OTP_INC_STRAP) {
1750 			printf("\nOTP strap region :\n");
1751 			if (otp_print_strap_image(&image_layout) < 0) {
1752 				printf("OTP strap error, please check.\n");
1753 				return OTP_FAILURE;
1754 			}
1755 		}
1756 		if (otp_header->image_info & OTP_INC_CONFIG) {
1757 			printf("\nOTP configuration region :\n");
1758 			if (otp_print_conf_image(&image_layout) < 0) {
1759 				printf("OTP config error, please check.\n");
1760 				return OTP_FAILURE;
1761 			}
1762 		}
1763 
1764 		printf("type \"YES\" (no quotes) to continue:\n");
1765 		if (!confirm_yesno()) {
1766 			printf(" Aborting\n");
1767 			return OTP_FAILURE;
1768 		}
1769 	}
1770 
1771 	if (otp_header->image_info & OTP_INC_DATA) {
1772 		printf("programing data region ...\n");
1773 		ret = otp_prog_data(&image_layout);
1774 		if (ret != 0) {
1775 			printf("Error\n");
1776 			return ret;
1777 		}
1778 		printf("Done\n");
1779 	}
1780 	if (otp_header->image_info & OTP_INC_STRAP) {
1781 		printf("programing strap region ...\n");
1782 		ret = otp_prog_strap(&image_layout);
1783 		if (ret != 0) {
1784 			printf("Error\n");
1785 			return ret;
1786 		}
1787 		printf("Done\n");
1788 	}
1789 	if (otp_header->image_info & OTP_INC_CONFIG) {
1790 		printf("programing configuration region ...\n");
1791 		ret = otp_prog_conf(&image_layout);
1792 		if (ret != 0) {
1793 			printf("Error\n");
1794 			return ret;
1795 		}
1796 		printf("Done\n");
1797 	}
1798 
1799 	return OTP_SUCCESS;
1800 }
1801 
1802 static int otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm)
1803 {
1804 	u32 read[2];
1805 	u32 prog_address = 0;
1806 	struct otpstrap_status otpstrap[64];
1807 	int otp_bit;
1808 	int ret = 0;
1809 
1810 	otp_soak(0);
1811 	switch (mode) {
1812 	case OTP_REGION_CONF:
1813 		otp_read_conf(otp_dw_offset, read);
1814 		prog_address = 0x800;
1815 		prog_address |= (otp_dw_offset / 8) * 0x200;
1816 		prog_address |= (otp_dw_offset % 8) * 0x2;
1817 		otp_bit = (read[0] >> bit_offset) & 0x1;
1818 		if (otp_bit == value) {
1819 			printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1820 			printf("No need to program\n");
1821 			return OTP_SUCCESS;
1822 		}
1823 		if (otp_bit == 1 && value == 0) {
1824 			printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset);
1825 			printf("OTP is programed, which can't be clean\n");
1826 			return OTP_FAILURE;
1827 		}
1828 		printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset);
1829 		break;
1830 	case OTP_REGION_DATA:
1831 		prog_address = otp_dw_offset;
1832 
1833 		if (otp_dw_offset % 2 == 0) {
1834 			otp_read_data(otp_dw_offset, read);
1835 			otp_bit = (read[0] >> bit_offset) & 0x1;
1836 
1837 			if (otp_bit == 1 && value == 0) {
1838 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1839 				printf("OTP is programed, which can't be cleaned\n");
1840 				return OTP_FAILURE;
1841 			}
1842 		} else {
1843 			otp_read_data(otp_dw_offset - 1, read);
1844 			otp_bit = (read[1] >> bit_offset) & 0x1;
1845 
1846 			if (otp_bit == 0 && value == 1) {
1847 				printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset);
1848 				printf("OTP is programed, which can't be writen\n");
1849 				return OTP_FAILURE;
1850 			}
1851 		}
1852 		if (otp_bit == value) {
1853 			printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value);
1854 			printf("No need to program\n");
1855 			return OTP_SUCCESS;
1856 		}
1857 
1858 		printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset);
1859 		break;
1860 	case OTP_REGION_STRAP:
1861 		otp_strap_status(otpstrap);
1862 		otp_print_strap(bit_offset, 1);
1863 		ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0);
1864 		if (ret == OTP_FAILURE)
1865 			return OTP_FAILURE;
1866 		else if (ret == OTP_PROG_SKIP)
1867 			return OTP_SUCCESS;
1868 
1869 		break;
1870 	}
1871 
1872 	if (!nconfirm) {
1873 		printf("type \"YES\" (no quotes) to continue:\n");
1874 		if (!confirm_yesno()) {
1875 			printf(" Aborting\n");
1876 			return OTP_FAILURE;
1877 		}
1878 	}
1879 
1880 	switch (mode) {
1881 	case OTP_REGION_STRAP:
1882 		ret =  otp_prog_strap_b(bit_offset, value);
1883 		break;
1884 	case OTP_REGION_CONF:
1885 	case OTP_REGION_DATA:
1886 		ret = otp_prog_dc_b(value, prog_address, bit_offset);
1887 		break;
1888 	}
1889 	otp_soak(0);
1890 	if (ret) {
1891 		printf("OTP cannot be programed\n");
1892 		printf("FAILURE\n");
1893 		return OTP_FAILURE;
1894 	}
1895 
1896 	printf("SUCCESS\n");
1897 	return OTP_SUCCESS;
1898 }
1899 
1900 static int otp_update_rid(u32 update_num, int force)
1901 {
1902 	u32 otp_rid[2];
1903 	u32 sw_rid[2];
1904 	int rid_num = 0;
1905 	int sw_rid_num = 0;
1906 	int bit_offset;
1907 	int dw_offset;
1908 	int i;
1909 	int ret;
1910 
1911 	otp_read_conf(10, &otp_rid[0]);
1912 	otp_read_conf(11, &otp_rid[1]);
1913 
1914 	sw_rid[0] = readl(SW_REV_ID0);
1915 	sw_rid[1] = readl(SW_REV_ID1);
1916 
1917 	rid_num = get_rid_num(otp_rid);
1918 	sw_rid_num = get_rid_num(sw_rid);
1919 
1920 	if (sw_rid_num < 0) {
1921 		printf("SW revision id is invalid, please check.\n");
1922 		return OTP_FAILURE;
1923 	}
1924 
1925 	if (update_num > sw_rid_num) {
1926 		printf("current SW revision ID: 0x%x\n", sw_rid_num);
1927 		printf("update number could not bigger than current SW revision id\n");
1928 		return OTP_FAILURE;
1929 	}
1930 
1931 	if (rid_num < 0) {
1932 		printf("Currennt OTP revision ID cannot handle by this command,\n"
1933 		       "plase use 'otp pb' command to update it manually\n");
1934 		otp_print_revid(otp_rid);
1935 		return OTP_FAILURE;
1936 	}
1937 
1938 	printf("current OTP revision ID: 0x%x\n", rid_num);
1939 	otp_print_revid(otp_rid);
1940 	printf("input update number: 0x%x\n", update_num);
1941 
1942 	if (rid_num > update_num) {
1943 		printf("OTP rev_id is bigger than 0x%X\n", update_num);
1944 		printf("Skip\n");
1945 		return OTP_FAILURE;
1946 	} else if (rid_num == update_num) {
1947 		printf("OTP rev_id is same as input\n");
1948 		printf("Skip\n");
1949 		return OTP_FAILURE;
1950 	}
1951 
1952 	for (i = rid_num; i < update_num; i++) {
1953 		if (i < 32) {
1954 			dw_offset = 0xa;
1955 			bit_offset = i;
1956 		} else {
1957 			dw_offset = 0xb;
1958 			bit_offset = i - 32;
1959 		}
1960 		printf("OTPCFG%X[%d]", dw_offset, bit_offset);
1961 		if (i + 1 != update_num)
1962 			printf(", ");
1963 	}
1964 
1965 	printf(" will be programmed\n");
1966 	if (force == 0) {
1967 		printf("type \"YES\" (no quotes) to continue:\n");
1968 		if (!confirm_yesno()) {
1969 			printf(" Aborting\n");
1970 			return OTP_FAILURE;
1971 		}
1972 	}
1973 
1974 	ret = 0;
1975 	for (i = rid_num; i < update_num; i++) {
1976 		if (i < 32) {
1977 			dw_offset = 0xa04;
1978 			bit_offset = i;
1979 		} else {
1980 			dw_offset = 0xa06;
1981 			bit_offset = i - 32;
1982 		}
1983 		if (otp_prog_dc_b(1, dw_offset, bit_offset)) {
1984 			printf("OTPCFG%X[%d] programming failed\n", dw_offset, bit_offset);
1985 			ret = OTP_FAILURE;
1986 			break;
1987 		}
1988 	}
1989 
1990 	otp_read_conf(10, &otp_rid[0]);
1991 	otp_read_conf(11, &otp_rid[1]);
1992 	rid_num = get_rid_num(otp_rid);
1993 	if (rid_num >= 0)
1994 		printf("OTP revision ID: 0x%x\n", rid_num);
1995 	else
1996 		printf("OTP revision ID\n");
1997 	otp_print_revid(otp_rid);
1998 	if (!ret)
1999 		printf("SUCCESS\n");
2000 	else
2001 		printf("FAILED\n");
2002 	return ret;
2003 }
2004 
2005 static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2006 {
2007 	u32 offset, count;
2008 	int ret;
2009 
2010 	if (argc == 4) {
2011 		offset = simple_strtoul(argv[2], NULL, 16);
2012 		count = simple_strtoul(argv[3], NULL, 16);
2013 	} else if (argc == 3) {
2014 		offset = simple_strtoul(argv[2], NULL, 16);
2015 		count = 1;
2016 	} else {
2017 		return CMD_RET_USAGE;
2018 	}
2019 
2020 	if (!strcmp(argv[1], "conf")) {
2021 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2022 		ret = otp_print_conf(offset, count);
2023 	} else if (!strcmp(argv[1], "data")) {
2024 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2025 		ret = otp_print_data(offset, count);
2026 	} else if (!strcmp(argv[1], "strap")) {
2027 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2028 		ret = otp_print_strap(offset, count);
2029 	} else {
2030 		return CMD_RET_USAGE;
2031 	}
2032 
2033 	if (ret == OTP_SUCCESS)
2034 		return CMD_RET_SUCCESS;
2035 	return CMD_RET_USAGE;
2036 }
2037 
2038 static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2039 {
2040 	phys_addr_t addr;
2041 	int ret;
2042 
2043 	if (argc == 3) {
2044 		if (strcmp(argv[1], "o"))
2045 			return CMD_RET_USAGE;
2046 		addr = simple_strtoul(argv[2], NULL, 16);
2047 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2048 		ret = otp_prog_image(addr, 1);
2049 	} else if (argc == 2) {
2050 		addr = simple_strtoul(argv[1], NULL, 16);
2051 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2052 		ret = otp_prog_image(addr, 0);
2053 	} else {
2054 		return CMD_RET_USAGE;
2055 	}
2056 
2057 	if (ret == OTP_SUCCESS)
2058 		return CMD_RET_SUCCESS;
2059 	else if (ret == OTP_FAILURE)
2060 		return CMD_RET_FAILURE;
2061 	else
2062 		return CMD_RET_USAGE;
2063 }
2064 
2065 static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2066 {
2067 	int mode = 0;
2068 	int nconfirm = 0;
2069 	int otp_addr = 0;
2070 	int bit_offset;
2071 	int value;
2072 	int ret;
2073 
2074 	if (argc != 4 && argc != 5 && argc != 6)
2075 		return CMD_RET_USAGE;
2076 
2077 	/* Drop the pb cmd */
2078 	argc--;
2079 	argv++;
2080 
2081 	if (!strcmp(argv[0], "conf"))
2082 		mode = OTP_REGION_CONF;
2083 	else if (!strcmp(argv[0], "strap"))
2084 		mode = OTP_REGION_STRAP;
2085 	else if (!strcmp(argv[0], "data"))
2086 		mode = OTP_REGION_DATA;
2087 	else
2088 		return CMD_RET_USAGE;
2089 
2090 	/* Drop the region cmd */
2091 	argc--;
2092 	argv++;
2093 
2094 	if (!strcmp(argv[0], "o")) {
2095 		nconfirm = 1;
2096 		/* Drop the force option */
2097 		argc--;
2098 		argv++;
2099 	}
2100 
2101 	if (mode == OTP_REGION_STRAP) {
2102 		bit_offset = simple_strtoul(argv[0], NULL, 16);
2103 		value = simple_strtoul(argv[1], NULL, 16);
2104 		if (bit_offset >= 64 || (value != 0 && value != 1))
2105 			return CMD_RET_USAGE;
2106 	} else {
2107 		otp_addr = simple_strtoul(argv[0], NULL, 16);
2108 		bit_offset = simple_strtoul(argv[1], NULL, 16);
2109 		value = simple_strtoul(argv[2], NULL, 16);
2110 		if (bit_offset >= 32 || (value != 0 && value != 1))
2111 			return CMD_RET_USAGE;
2112 		if (mode == OTP_REGION_DATA) {
2113 			if (otp_addr >= 0x800)
2114 				return CMD_RET_USAGE;
2115 		} else {
2116 			if (otp_addr >= 0x20)
2117 				return CMD_RET_USAGE;
2118 		}
2119 	}
2120 	if (value != 0 && value != 1)
2121 		return CMD_RET_USAGE;
2122 
2123 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2124 	ret = otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm);
2125 
2126 	if (ret == OTP_SUCCESS)
2127 		return CMD_RET_SUCCESS;
2128 	else if (ret == OTP_FAILURE)
2129 		return CMD_RET_FAILURE;
2130 	else
2131 		return CMD_RET_USAGE;
2132 }
2133 
2134 static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2135 {
2136 	phys_addr_t addr;
2137 	int otp_addr = 0;
2138 
2139 	if (argc != 3)
2140 		return CMD_RET_USAGE;
2141 
2142 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2143 	addr = simple_strtoul(argv[1], NULL, 16);
2144 	otp_addr = simple_strtoul(argv[2], NULL, 16);
2145 	if (otp_compare(otp_addr, addr) == 0) {
2146 		printf("Compare pass\n");
2147 		return CMD_RET_SUCCESS;
2148 	}
2149 	printf("Compare fail\n");
2150 	return CMD_RET_FAILURE;
2151 }
2152 
2153 static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2154 {
2155 	int view = 0;
2156 	int input;
2157 
2158 	if (argc != 2 && argc != 3)
2159 		return CMD_RET_USAGE;
2160 
2161 	if (!strcmp(argv[1], "conf")) {
2162 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2163 		if (argc == 3) {
2164 			input = simple_strtoul(argv[2], NULL, 16);
2165 			otp_print_conf_info(input);
2166 		} else {
2167 			otp_print_conf_info(-1);
2168 		}
2169 	} else if (!strcmp(argv[1], "strap")) {
2170 		if (!strcmp(argv[2], "v")) {
2171 			view = 1;
2172 			/* Drop the view option */
2173 			argc--;
2174 			argv++;
2175 		}
2176 		writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2177 		otp_print_strap_info(view);
2178 	} else {
2179 		return CMD_RET_USAGE;
2180 	}
2181 
2182 	return CMD_RET_SUCCESS;
2183 }
2184 
2185 static int _do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[], int preg)
2186 {
2187 	int input;
2188 	int bit_offset;
2189 	u32 prog_address;
2190 	int ret;
2191 	char info[10];
2192 
2193 	if (preg) {
2194 		sprintf(info, "register ");
2195 		prog_address = 0xe08;
2196 	} else {
2197 		info[0] = 0;
2198 		prog_address = 0xe0c;
2199 	}
2200 
2201 	if (argc != 3 && argc != 2)
2202 		return CMD_RET_USAGE;
2203 
2204 	if (!strcmp(argv[1], "o")) {
2205 		input = simple_strtoul(argv[2], NULL, 16);
2206 	} else {
2207 		input = simple_strtoul(argv[1], NULL, 16);
2208 		printf("OTPSTRAP[%d] %swill be protected\n", input, info);
2209 		printf("type \"YES\" (no quotes) to continue:\n");
2210 		if (!confirm_yesno()) {
2211 			printf(" Aborting\n");
2212 			return CMD_RET_FAILURE;
2213 		}
2214 	}
2215 
2216 	if (input < 32) {
2217 		bit_offset = input;
2218 	} else if (input < 64) {
2219 		bit_offset = input - 32;
2220 		prog_address += 2;
2221 	} else {
2222 		return CMD_RET_USAGE;
2223 	}
2224 
2225 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2226 	if (verify_bit(prog_address, bit_offset, 1) == 0) {
2227 		printf("OTPSTRAP[%d] %salready protected\n", input, info);
2228 		return CMD_RET_SUCCESS;
2229 	}
2230 
2231 	ret = otp_prog_dc_b(1, prog_address, bit_offset);
2232 	otp_soak(0);
2233 
2234 	if (ret) {
2235 		printf("Protect OTPSTRAP[%d] %sfail\n", input, info);
2236 		return CMD_RET_FAILURE;
2237 	}
2238 
2239 	printf("OTPSTRAP[%d] %sis protected\n", input, info);
2240 	return CMD_RET_SUCCESS;
2241 }
2242 
2243 static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2244 {
2245 	return _do_otpprotect(cmdtp, flag, argc, argv, 0);
2246 }
2247 
2248 static int do_otprprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2249 {
2250 	return _do_otpprotect(cmdtp, flag, argc, argv, 1);
2251 }
2252 
2253 static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2254 {
2255 	printf("SOC OTP version: %s\n", info_cb.ver_name);
2256 	printf("OTP tool version: %s\n", OTP_VER);
2257 	printf("OTP info version: %s\n", OTP_INFO_VER);
2258 
2259 	return CMD_RET_SUCCESS;
2260 }
2261 
2262 static int do_otpupdate(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2263 {
2264 	u32 update_num;
2265 	int force = 0;
2266 	int ret;
2267 
2268 	if (argc == 3) {
2269 		if (strcmp(argv[1], "o"))
2270 			return CMD_RET_USAGE;
2271 		force = 1;
2272 		update_num = simple_strtoul(argv[2], NULL, 16);
2273 	} else if (argc == 2) {
2274 		update_num = simple_strtoul(argv[1], NULL, 16);
2275 	} else {
2276 		return CMD_RET_USAGE;
2277 	}
2278 
2279 	if (update_num > 64)
2280 		return CMD_RET_USAGE;
2281 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2282 	ret = otp_update_rid(update_num, force);
2283 	if (ret)
2284 		return CMD_RET_FAILURE;
2285 	return CMD_RET_SUCCESS;
2286 }
2287 
2288 static int do_otprid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2289 {
2290 	u32 otp_rid[2];
2291 	u32 sw_rid[2];
2292 	int rid_num = 0;
2293 	int sw_rid_num = 0;
2294 	int ret;
2295 
2296 	if (argc != 1)
2297 		return CMD_RET_USAGE;
2298 
2299 	writel(OTP_PASSWD, OTP_PROTECT_KEY); //password
2300 	otp_read_conf(10, &otp_rid[0]);
2301 	otp_read_conf(11, &otp_rid[1]);
2302 
2303 	sw_rid[0] = readl(SW_REV_ID0);
2304 	sw_rid[1] = readl(SW_REV_ID1);
2305 
2306 	rid_num = get_rid_num(otp_rid);
2307 	sw_rid_num = get_rid_num(sw_rid);
2308 
2309 	printf("current SW revision ID: 0x%x\n", sw_rid_num);
2310 	if (rid_num >= 0) {
2311 		printf("current OTP revision ID: 0x%x\n", rid_num);
2312 		ret = CMD_RET_SUCCESS;
2313 	} else {
2314 		printf("Currennt OTP revision ID cannot handle by 'otp update',\n"
2315 		       "plase use 'otp pb' command to update it manually\n"
2316 		       "current OTP revision ID\n");
2317 		ret = CMD_RET_FAILURE;
2318 	}
2319 	otp_print_revid(otp_rid);
2320 
2321 	return ret;
2322 }
2323 
2324 static cmd_tbl_t cmd_otp[] = {
2325 	U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""),
2326 	U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
2327 	U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
2328 	U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
2329 	U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
2330 	U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
2331 	U_BOOT_CMD_MKENT(rprotect, 3, 0, do_otprprotect, "", ""),
2332 	U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
2333 	U_BOOT_CMD_MKENT(update, 3, 0, do_otpupdate, "", ""),
2334 	U_BOOT_CMD_MKENT(rid, 1, 0, do_otprid, "", ""),
2335 };
2336 
2337 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2338 {
2339 	cmd_tbl_t *cp;
2340 	u32 ver;
2341 	int ret;
2342 
2343 	cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp));
2344 
2345 	/* Drop the otp command */
2346 	argc--;
2347 	argv++;
2348 
2349 	if (!cp || argc > cp->maxargs)
2350 		return CMD_RET_USAGE;
2351 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
2352 		return CMD_RET_SUCCESS;
2353 
2354 	ver = chip_version();
2355 	switch (ver) {
2356 	case OTP_A0:
2357 		info_cb.version = OTP_A0;
2358 		info_cb.conf_info = a0_conf_info;
2359 		info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info);
2360 		info_cb.strap_info = a0_strap_info;
2361 		info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info);
2362 		info_cb.key_info = a0_key_type;
2363 		info_cb.key_info_len = ARRAY_SIZE(a0_key_type);
2364 		sprintf(info_cb.ver_name, "A0");
2365 		break;
2366 	case OTP_A1:
2367 		info_cb.version = OTP_A1;
2368 		info_cb.conf_info = a1_conf_info;
2369 		info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info);
2370 		info_cb.strap_info = a1_strap_info;
2371 		info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info);
2372 		info_cb.key_info = a1_key_type;
2373 		info_cb.key_info_len = ARRAY_SIZE(a1_key_type);
2374 		sprintf(info_cb.ver_name, "A1");
2375 		break;
2376 	case OTP_A2:
2377 		info_cb.version = OTP_A2;
2378 		info_cb.conf_info = a2_conf_info;
2379 		info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info);
2380 		info_cb.strap_info = a2_strap_info;
2381 		info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info);
2382 		info_cb.key_info = a2_key_type;
2383 		info_cb.key_info_len = ARRAY_SIZE(a2_key_type);
2384 		sprintf(info_cb.ver_name, "A2");
2385 		break;
2386 	case OTP_A3:
2387 		info_cb.version = OTP_A3;
2388 		info_cb.conf_info = a2_conf_info;
2389 		info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info);
2390 		info_cb.strap_info = a2_strap_info;
2391 		info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info);
2392 		info_cb.key_info = a3_key_type;
2393 		info_cb.key_info_len = ARRAY_SIZE(a3_key_type);
2394 		sprintf(info_cb.ver_name, "A3");
2395 		break;
2396 	default:
2397 		printf("SOC is not supported\n");
2398 		return CMD_RET_FAILURE;
2399 	}
2400 
2401 	ret = cp->cmd(cmdtp, flag, argc, argv);
2402 	writel(1, OTP_PROTECT_KEY); //password
2403 
2404 	return ret;
2405 }
2406 
2407 U_BOOT_CMD(otp, 7, 0,  do_ast_otp,
2408 	   "ASPEED One-Time-Programmable sub-system",
2409 	   "version\n"
2410 	   "otp read conf|data <otp_dw_offset> <dw_count>\n"
2411 	   "otp read strap <strap_bit_offset> <bit_count>\n"
2412 	   "otp info strap [v]\n"
2413 	   "otp info conf [otp_dw_offset]\n"
2414 	   "otp prog [o] <addr>\n"
2415 	   "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
2416 	   "otp pb strap [o] <bit_offset> <value>\n"
2417 	   "otp protect [o] <bit_offset>\n"
2418 	   "otp rprotect [o] <bit_offset>\n"
2419 	   "otp update [o] <revision_id>\n"
2420 	   "otp rid\n"
2421 	  );
2422