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