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