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