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