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