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