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