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