xref: /openbmc/u-boot/cmd/otp.c (revision ea68d6ca)
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 
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 
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 MODE_CONF	1
29 #define MODE_STRAP	2
30 #define MODE_DATA	3
31 #define MODE_ALL	4
32 
33 struct otpstrap {
34 	int value;
35 	int option_array[7];
36 	int remain_times;
37 	int writeable_option;
38 	int protected;
39 };
40 
41 static int otp_read_data(uint32_t offset, uint32_t *data)
42 {
43 	writel(offset, 0x1e6f2010); //Read address
44 	writel(0x23b1e361, 0x1e6f2004); //trigger read
45 	udelay(2);
46 	data[0] = readl(0x1e6f2020);
47 	data[1] = readl(0x1e6f2024);
48 	return 1;
49 }
50 
51 static int otp_read_config(uint32_t offset, uint32_t *data)
52 {
53 	int config_offset;
54 
55 	config_offset = 0x800;
56 	config_offset |= (offset / 8) * 0x200;
57 	config_offset |= (offset % 8) * 0x2;
58 
59 	writel(config_offset, 0x1e6f2010);  //Read address
60 	writel(0x23b1e361, 0x1e6f2004); //trigger read
61 	udelay(2);
62 	data[0] = readl(0x1e6f2020);
63 
64 	return 1;
65 }
66 
67 static int otp_print_config(uint32_t offset, int dw_count)
68 {
69 	int i;
70 	uint32_t ret[1];
71 
72 	if (offset + dw_count > 32)
73 		return -1;
74 	for (i = offset; i < offset + dw_count; i ++) {
75 		otp_read_config(i, ret);
76 		printf("OTPCFG%d: %08X\n", i, ret[0]);
77 	}
78 	printf("\n");
79 	return 1;
80 }
81 
82 static int otp_print_data(uint32_t offset, int dw_count)
83 {
84 	int i;
85 	uint32_t ret[2];
86 
87 	if (offset + dw_count > 2048 || offset % 4 != 0)
88 		return -1;
89 	for (i = offset; i < offset + dw_count; i += 2) {
90 		otp_read_data(i, ret);
91 		if (i % 4 == 0)
92 			printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]);
93 		else
94 			printf("%08X %08X\n", ret[0], ret[1]);
95 
96 	}
97 	printf("\n");
98 	return 1;
99 }
100 
101 static int otp_compare(uint32_t otp_addr, uint32_t addr)
102 {
103 	uint32_t ret;
104 	uint32_t *buf;
105 
106 	buf = map_physmem(addr, 16, MAP_WRBACK);
107 	printf("%08X\n", buf[0]);
108 	printf("%08X\n", buf[1]);
109 	printf("%08X\n", buf[2]);
110 	printf("%08X\n", buf[3]);
111 	writel(otp_addr, 0x1e6f2010); //Compare address
112 	writel(buf[0], 0x1e6f2020); //Compare data 1
113 	writel(buf[1], 0x1e6f2024); //Compare data 2
114 	writel(buf[2], 0x1e6f2028); //Compare data 3
115 	writel(buf[3], 0x1e6f202c); //Compare data 4
116 	writel(0x23b1e363, 0x1e6f2004); //Compare command
117 	udelay(10);
118 	ret = readl(0x1e6f2014); //Compare command
119 	if (ret & 0x1)
120 		return 0;
121 	else
122 		return -1;
123 }
124 
125 static void otp_write(uint32_t otp_addr, uint32_t data)
126 {
127 	writel(otp_addr, 0x1e6f2010); //write address
128 	writel(data, 0x1e6f2020); //write data
129 	writel(0x23b1e362, 0x1e6f2004); //write command
130 	udelay(100);
131 }
132 
133 static void otp_prog(uint32_t otp_addr, uint32_t prog_bit)
134 {
135 	writel(otp_addr, 0x1e6f2010); //write address
136 	writel(prog_bit, 0x1e6f2020); //write data
137 	writel(0x23b1e364, 0x1e6f2004); //write command
138 	udelay(85);
139 }
140 
141 static int prog_verify(uint32_t otp_addr, int bit_offset, int value)
142 {
143 	int ret;
144 
145 	writel(otp_addr, 0x1e6f2010); //Read address
146 	writel(0x23b1e361, 0x1e6f2004); //trigger read
147 	udelay(2);
148 	ret = readl(0x1e6f2020);
149 	// printf("prog_verify = %x\n", ret);
150 	if (((ret >> bit_offset) & 1) == value)
151 		return 0;
152 	else
153 		return -1;
154 }
155 
156 static int otp_conf_parse(uint32_t *OTPCFG)
157 {
158 	int tmp, i, pass;
159 
160 	printf("OTPCFG0-D[0]\n");
161 	if (OTPCFG[0] & (1))
162 		printf("  Disable Secure Region programming\n");
163 	else
164 		printf("  Enable Secure Region programming\n");
165 	printf("OTPCFG0-D[1]\n");
166 	if (OTPCFG[0] & (1 << 1))
167 		printf("  Enable Secure Boot\n");
168 	else
169 		printf("  Disable Secure Boot\n");
170 	printf("OTPCFG0-D[3]\n");
171 	if (OTPCFG[0] & (1 << 3))
172 		printf("  User region ECC enable\n");
173 	else
174 		printf("  User region ECC disable\n");
175 	printf("OTPCFG0-D[4]\n");
176 	if (OTPCFG[0] & (1 << 4))
177 		printf("  Secure Region ECC enable\n");
178 	else
179 		printf("  Secure Region ECC disable\n");
180 	printf("OTPCFG0-D[5]\n");
181 	if (OTPCFG[0] & (1 << 5))
182 		printf("  Disable low security key\n");
183 	else
184 		printf("  Enable low security key\n");
185 	printf("OTPCFG0-D[6]\n");
186 	if (OTPCFG[0] & (1 << 6))
187 		printf("  Ignore Secure Boot hardware strap\n");
188 	else
189 		printf("  Do not ignore Secure Boot hardware strap\n");
190 	printf("OTPCFG0-D[7]\n");
191 	printf("  Secure Boot Mode: %d\n", (OTPCFG[0] >> 7) & 1);
192 	printf("OTPCFG0-D[9:8]\n");
193 	printf("  OTP bit cell mode : ");
194 	tmp = ((OTPCFG[0] >> 8) & 3);
195 	if (tmp == 0) {
196 		printf("Single cell mode (recommended)\n");
197 	} else if (tmp == 1) {
198 		printf("Differnetial mode\n");
199 	} else if (tmp == 2) {
200 		printf("Differential-redundant mode\n");
201 	} else {
202 		printf("Value error\n");
203 		return -1;
204 	}
205 	printf("OTPCFG0-D[11:10]\n");
206 	printf("  RSA mode : ");
207 	tmp = ((OTPCFG[0] >> 10) & 3);
208 	if (tmp == 0) {
209 		printf("RSA1024\n");
210 	} else if (tmp == 1) {
211 		printf("RSA2048\n");
212 	} else if (tmp == 2) {
213 		printf("RSA3072\n");
214 	} else {
215 		printf("RSA4096\n");
216 	}
217 	printf("OTPCFG0-D[13:12]\n");
218 	printf("  SHA mode : ");
219 	tmp = ((OTPCFG[0] >> 12) & 3);
220 	if (tmp == 0) {
221 		printf("SHA224\n");
222 	} else if (tmp == 1) {
223 		printf("SHA256\n");
224 	} else if (tmp == 2) {
225 		printf("SHA384\n");
226 	} else {
227 		printf("SHA512\n");
228 	}
229 
230 	printf("OTPCFG0-D[21:16]\n");
231 	tmp = ((OTPCFG[0] >> 16) & 0x3F);
232 	printf("  Secure Region size (DW): %x\n", tmp);
233 
234 	printf("OTPCFG0-D[22]\n");
235 	if (OTPCFG[0] & (1 << 22))
236 		printf("  Secure Region : Write Protect\n");
237 	else
238 		printf("  Secure Region : Writable\n");
239 	printf("OTPCFG0-D[23]\n");
240 	if (OTPCFG[0] & (1 << 23))
241 		printf("  User Region : Write Protect\n");
242 	else
243 		printf("  User Region : Writable\n");
244 	printf("OTPCFG0-D[24]\n");
245 	if (OTPCFG[0] & (1 << 24))
246 		printf("  Configure Region : Write Protect\n");
247 	else
248 		printf("  Configure Region : Writable\n");
249 	printf("OTPCFG0-D[25]\n");
250 	if (OTPCFG[0] & (1 << 7))
251 		printf("  OTP strap Region : Write Protect\n");
252 	else
253 		printf("  OTP strap Region : Writable\n");
254 	printf("OTPCFG0-D[26]\n");
255 	if (OTPCFG[0] & (1 << 26))
256 		printf("  Copy Boot Image to Internal SRAM\n");
257 	else
258 		printf("  Disable Copy Boot Image to Internal SRAM\n");
259 	printf("OTPCFG0-D[27]\n");
260 	if (OTPCFG[0] & (1 << 27))
261 		printf("  Enable image encryption\n");
262 	else
263 		printf("  Disable image encryption\n");
264 	printf("OTPCFG0-D[29]\n");
265 	if (OTPCFG[0] & (1 << 29))
266 		printf("  OTP key retire Region : Write Protect\n");
267 	else
268 		printf("  OTP key retire Region : Writable\n");
269 	printf("OTPCFG0-D[30]\n");
270 	if (OTPCFG[0] & (1 << 30))
271 		printf("  SIPROM RED_EN redundancy repair enable\n");
272 	else
273 		printf("  SIPROM RED_EN redundancy repair disable\n");
274 	printf("OTPCFG0-D[31]\n");
275 	if (OTPCFG[0] & (1 << 31))
276 		printf("  SIPROM Mlock memory lock enable\n");
277 	else
278 		printf("  SIPROM Mlock memory lock disable\n");
279 
280 	printf("OTPCFG2-D[15:0]\n");
281 	tmp = (OTPCFG[2] & 0xFFFF);
282 	printf("  Vender ID : %x\n", tmp);
283 
284 	printf("OTPCFG2-D[31:16]\n");
285 	tmp = ((OTPCFG[2] >> 16) & 0xFFFF);
286 	printf("  Key Revision : %x\n", tmp);
287 
288 	printf("OTPCFG3-D[15:0]\n");
289 	tmp = (OTPCFG[3] & 0xFFFF);
290 	printf("  Secure boot header offset : %x\n", tmp);
291 
292 	printf("OTPCFG4-D[7:0]\n");
293 	tmp = (OTPCFG[4] & 0xff);
294 	pass = -1;
295 	for (i = 0; i < 7; i++) {
296 		if (tmp == (1 << i)) {
297 			if (pass != -1) {
298 				printf("  Keys valid ID value error : %x\n", tmp);
299 				return -1;
300 			}
301 			pass = i;
302 		}
303 	}
304 	printf("  Keys valid ID : %d\n", pass);
305 
306 	printf("OTPCFG4-D[23:16]\n");
307 	tmp = ((OTPCFG[4] >> 16) & 0xff);
308 	pass = -1;
309 	for (i = 0; i < 7; i++) {
310 		if (tmp == (1 << i)) {
311 			if (pass != -1) {
312 				printf("  Keys Retire ID value error : %x\n", tmp);
313 				return -1;
314 			}
315 			pass = i;
316 		}
317 	}
318 	printf("  Keys Retire ID : %d\n", pass);
319 
320 	printf("OTPCFG5-D[31:0]\n");
321 	printf("  User define data, random number low : %x\n", OTPCFG[5]);
322 
323 	printf("OTPCFG6-D[31:0]\n");
324 	printf("  User define data, random number high : %x\n", OTPCFG[6]);
325 
326 	printf("OTPCFG8-D[31:0]\n");
327 	printf("  Redundancy Repair : %x\n", OTPCFG[8]);
328 
329 	printf("OTPCFG10-D[31:0]\n");
330 	printf("  Manifest ID low : %x\n", OTPCFG[10]);
331 
332 	printf("OTPCFG11-D[31:0]\n");
333 	printf("  Manifest ID high : %x\n", OTPCFG[11]);
334 	return 0;
335 
336 }
337 
338 static void buf_print(char *buf, int len)
339 {
340 	int i;
341 	printf("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
342 	for (i = 0; i < len; i++) {
343 		if (i % 16 == 0) {
344 			printf("%04X: ", i);
345 		}
346 		printf("%02X ", buf[i]);
347 		if ((i + 1) % 16 == 0) {
348 			printf("\n");
349 		}
350 	}
351 }
352 
353 static int otp_data_parse(uint32_t *buf, int dw_count)
354 {
355 	int key_id, key_offset, last, key_type, key_length, exp_length;
356 	char *byte_buf;
357 	int i = 0, len = 0;
358 	byte_buf = (char *)buf;
359 	while (1) {
360 		key_id = buf[i] & 0x7;
361 		key_offset = buf[i] & 0x1ff8;
362 		last = (buf[i] >> 13) & 1;
363 		key_type = (buf[i] >> 14) & 0xf;
364 		key_length = (buf[i] >> 18) & 0x3;
365 		exp_length = (buf[i] >> 20) & 0xfff;
366 		printf("Key[%d]:\n", i);
367 		printf("Key Type: ");
368 		switch (key_type) {
369 		case 0:
370 			printf("AES-256 as OEM platform key for image encryption/decryption\n");
371 			break;
372 		case 1:
373 			printf("AES-256 as secret vault key\n");
374 			break;
375 		case 4:
376 			printf("HMAC as encrypted OEM HMAC keys in Mode 1\n");
377 			break;
378 		case 8:
379 			printf("RSA-public as OEM DSS public keys in Mode 2\n");
380 			break;
381 		case 9:
382 			printf("RSA-public as SOC public key\n");
383 			break;
384 		case 10:
385 			printf("RSA-public as AES key decryption key\n");
386 			break;
387 		case 13:
388 			printf("RSA-private as SOC private key\n");
389 			break;
390 		case 14:
391 			printf("RSA-private as AES key decryption key\n");
392 			break;
393 		default:
394 			printf("key_type error: %x\n", key_type);
395 			return -1;
396 		}
397 		if (key_type == 4) {
398 			printf("HMAC SHA Type: ");
399 			switch (key_length) {
400 			case 0:
401 				printf("HMAC(SHA224)\n");
402 				break;
403 			case 1:
404 				printf("HMAC(SHA256)\n");
405 				break;
406 			case 2:
407 				printf("HMAC(SHA384)\n");
408 				break;
409 			case 3:
410 				printf("HMAC(SHA512)\n");
411 				break;
412 			}
413 		} else if (key_type != 0 || key_type != 1) {
414 			printf("RSA SHA Type: ");
415 			switch (key_length) {
416 			case 0:
417 				printf("RSA1024\n");
418 				len = 0x100;
419 				break;
420 			case 1:
421 				printf("RSA2048\n");
422 				len = 0x200;
423 				break;
424 			case 2:
425 				printf("RSA3072\n");
426 				len = 0x300;
427 				break;
428 			case 3:
429 				printf("RSA4096\n");
430 				len = 0x400;
431 				break;
432 			}
433 			printf("RSA exponent bit length: %d\n", exp_length);
434 		}
435 		if (key_type == 4 || key_type == 8)
436 			printf("Key Number ID: %d\n", key_id);
437 		printf("Key Value:\n");
438 		if (key_type == 4) {
439 			buf_print(&byte_buf[key_offset], 0x40);
440 		} else if (key_type == 0 || key_type == 1) {
441 			printf("AES Key:\n");
442 			buf_print(&byte_buf[key_offset], 0x20);
443 			printf("AES IV:\n");
444 			buf_print(&byte_buf[key_offset + 0x20], 0x10);
445 
446 		} else {
447 			printf("RSA mod:\n");
448 			buf_print(&byte_buf[key_offset], len / 2);
449 			printf("RSA exp:\n");
450 			buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
451 		}
452 		if (last)
453 			break;
454 		i++;
455 	}
456 	return 0;
457 }
458 
459 static int otp_prog_conf(uint32_t *buf, int otp_addr, int dw_count)
460 {
461 	int i, j, k, bit_value;
462 	int pass, soak;
463 	uint32_t prog_bit, prog_address;
464 
465 	for (i = 0; i < dw_count; i++) {
466 		prog_address = 0x800;
467 		prog_address |= ((i + otp_addr) / 8) * 0x200;
468 		prog_address |= ((i + otp_addr) % 8) * 0x2;
469 		for (j = 0; j < 32; j++) {
470 			bit_value = (buf[i] >> j) & 0x1;
471 			if (bit_value)
472 				prog_bit = ~(0x1 << j);
473 			else
474 				continue;
475 			pass = 0;
476 			soak = 0;
477 			otp_write(0x3000, 0x4061); // Write MRA
478 			otp_write(0x5000, 0x302f); // Write MRB
479 			otp_write(0x1000, 0x4020); // Write MR
480 			writel(0x04190760, 0x1e602008); //normal program
481 			for (k = 0; k < RETRY; k++) {
482 				if (!soak) {
483 					otp_prog(prog_address, prog_bit);
484 					if (prog_verify(prog_address, j, bit_value) == 0) {
485 						pass = 1;
486 						break;
487 					}
488 					soak = 1;
489 					otp_write(0x3000, 0x4021); // Write MRA
490 					otp_write(0x5000, 0x1027); // Write MRB
491 					otp_write(0x1000, 0x4820); // Write MR
492 					writel(0x041930d4, 0x1e602008); //soak program
493 				}
494 				otp_prog(prog_address, prog_bit);
495 				if (prog_verify(prog_address, j, bit_value) == 0) {
496 					pass = 1;
497 					break;
498 				}
499 			}
500 			if (!pass)
501 				return -1;
502 		}
503 	}
504 	return 0;
505 }
506 
507 static void otp_strp_status(struct otpstrap *otpstrap)
508 {
509 	uint32_t OTPSTRAP_RAW[2];
510 	int i, j;
511 
512 	for (j = 0; j < 64; j++) {
513 		otpstrap[j].value = 0;
514 		otpstrap[j].remain_times = 7;
515 		otpstrap[j].writeable_option = -1;
516 		otpstrap[j].protected = 0;
517 	}
518 
519 	for (i = 16; i < 30; i += 2) {
520 		int option = (i - 16) / 2;
521 		otp_read_config(i, &OTPSTRAP_RAW[0]);
522 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
523 		for (j = 0; j < 32; j++) {
524 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
525 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
526 				otpstrap[j].writeable_option = option;
527 			}
528 			if (bit_value == 1)
529 				otpstrap[j].remain_times --;
530 			otpstrap[j].value ^= bit_value;
531 			otpstrap[j].option_array[option] = bit_value;
532 		}
533 		for (j = 32; j < 64; j++) {
534 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
535 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
536 				otpstrap[j].writeable_option = option;
537 			}
538 			if (bit_value == 1)
539 				otpstrap[j].remain_times --;
540 			otpstrap[j].value ^= bit_value;
541 			otpstrap[j].option_array[option] = bit_value;
542 		}
543 	}
544 	otp_read_config(30, &OTPSTRAP_RAW[0]);
545 	otp_read_config(31, &OTPSTRAP_RAW[1]);
546 	for (j = 0; j < 32; j++) {
547 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
548 			otpstrap[j].protected = 1;
549 	}
550 	for (j = 32; j < 64; j++) {
551 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
552 			otpstrap[j].protected = 1;
553 	}
554 }
555 
556 static int otp_strap_parse(uint32_t *buf)
557 {
558 	int i;
559 	uint32_t *strap_keep = buf + 2;
560 	uint32_t *strap_protect = buf + 4;
561 	int bit, pbit, kbit;
562 	int fail = 0;
563 	struct otpstrap otpstrap[64];
564 
565 	otp_strp_status(otpstrap);
566 	for (i = 0; i < 64; i++) {
567 		if (i < 32) {
568 			bit = (buf[0] >> i) & 0x1;
569 			kbit = (strap_keep[0] >> i) & 0x1;
570 			pbit = (strap_protect[0] >> i) & 0x1;
571 		} else {
572 			bit = (buf[1] >> (i - 32)) & 0x1;
573 			kbit = (strap_keep[1] >> (i - 32)) & 0x1;
574 			pbit = (strap_protect[1] >> (i - 32)) & 0x1;
575 		}
576 
577 		if (kbit == 1) {
578 			continue;
579 		} else {
580 			printf("OTPSTRAP[%d]:\n", i);
581 		}
582 		if (bit == otpstrap[i].value) {
583 			printf("    The value is same as before, skip it.\n");
584 			continue;
585 		}
586 		if (otpstrap[i].protected == 1) {
587 			printf("    This bit is protected and is not writable\n");
588 			fail = 1;
589 			continue;
590 		}
591 		if (otpstrap[i].remain_times == 0) {
592 			printf("    This bit is no remaining number of times to write.\n");
593 			fail = 1;
594 			continue;
595 		}
596 		if (pbit == 1) {
597 			printf("    This bit will be protected and become non-writable.\n");
598 		}
599 		printf("    Write 1 to OTPSTRAP[%d] OPTION[%d], that value becomes frome %d to %d.\n", i, otpstrap[i].writeable_option + 1, otpstrap[i].value, otpstrap[i].value ^ 1);
600 	}
601 	if (fail == 1)
602 		return -1;
603 	else
604 		return 0;
605 }
606 
607 static void otp_print_strap(void)
608 {
609 	int i, j;
610 	struct otpstrap otpstrap[64];
611 
612 	otp_strp_status(otpstrap);
613 
614 	for (i = 0; i < 64; i++) {
615 		printf("OTPSTRAP[%d]:\n", i);
616 		printf("  OTP Option value: ");
617 		for (j = 1; j <= 7; j++)
618 			printf("[%d]:%d ", j, otpstrap[i].option_array[j - 1]);
619 		printf("\n");
620 		printf("  OTP Value: %d\n", otpstrap[i].value);
621 		printf("  Status:\n");
622 		if (otpstrap[i].protected == 1) {
623 			printf("    OTPSTRAP[%d] is protected and is not writable\n", i);
624 		} else {
625 			printf("    OTPSTRAP[%d] is not protected ", i);
626 			if (otpstrap[i].remain_times == 0) {
627 				printf("and no remaining number of times to write.\n");
628 			} else {
629 				printf("and still can write %d number of times\n", otpstrap[i].remain_times);
630 			}
631 		}
632 	}
633 }
634 
635 static int otp_prog_strap(uint32_t *buf)
636 {
637 	int i, j;
638 	uint32_t *strap_keep = buf + 2;
639 	uint32_t *strap_protect = buf + 4;
640 	uint32_t prog_bit, prog_address;
641 	int bit, pbit, kbit, offset;
642 	int fail = 0;
643 	int pass, soak;
644 	struct otpstrap otpstrap[64];
645 
646 	otp_strp_status(otpstrap);
647 
648 	otp_write(0x3000, 0x4061); // Write MRA
649 	otp_write(0x5000, 0x302f); // Write MRB
650 	otp_write(0x1000, 0x4020); // Write MR
651 	for (i = 0; i < 64; i++) {
652 		prog_address = 0x800;
653 		if (i < 32) {
654 			offset = i;
655 			bit = (buf[0] >> offset) & 0x1;
656 			kbit = (strap_keep[0] >> offset) & 0x1;
657 			pbit = (strap_protect[0] >> offset) & 0x1;
658 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
659 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
660 
661 		} else {
662 			offset = (i - 32);
663 			bit = (buf[1] >> offset) & 0x1;
664 			kbit = (strap_keep[1] >> offset) & 0x1;
665 			pbit = (strap_protect[1] >> offset) & 0x1;
666 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
667 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
668 		}
669 		prog_bit = ~(0x1 << offset);
670 
671 		if (kbit == 1) {
672 			continue;
673 		}
674 		if (bit == otpstrap[i].value) {
675 			continue;
676 		}
677 		if (otpstrap[i].protected == 1) {
678 			fail = 1;
679 			continue;
680 		}
681 		if (otpstrap[i].remain_times == 0) {
682 			fail = 1;
683 			continue;
684 		}
685 		pass = 0;
686 		soak = 0;
687 		otp_write(0x3000, 0x4061); // Write MRA
688 		otp_write(0x5000, 0x302f); // Write MRB
689 		otp_write(0x1000, 0x4020); // Write MR
690 		writel(0x04190760, 0x1e602008); //normal program
691 		for (j = 0; j < RETRY; j++) {
692 			if (!soak) {
693 				otp_prog(prog_address, prog_bit);
694 				if (prog_verify(prog_address, offset, 1) == 0) {
695 					pass = 1;
696 					break;
697 				}
698 				soak = 1;
699 				otp_write(0x3000, 0x4021); // Write MRA
700 				otp_write(0x5000, 0x1027); // Write MRB
701 				otp_write(0x1000, 0x4820); // Write MR
702 				writel(0x041930d4, 0x1e602008); //soak program
703 			}
704 			otp_prog(prog_address, prog_bit);
705 			if (prog_verify(prog_address, offset, 1) == 0) {
706 				pass = 1;
707 				break;
708 			}
709 		}
710 		if (!pass)
711 			return -1;
712 
713 		if (pbit == 0)
714 			continue;
715 		prog_address = 0x800;
716 		if (i < 32)
717 			prog_address |= 0x60c;
718 		else
719 			prog_address |= 0x60e;
720 
721 		for (j = 0; j < RETRY; j++) {
722 			if (!soak) {
723 				writel(0x04190760, 0x1e602008); //normal program
724 				otp_prog(prog_address, prog_bit);
725 				if (prog_verify(prog_address, offset, 1) == 0) {
726 					pass = 1;
727 					break;
728 				}
729 				soak = 1;
730 			}
731 			writel(0x041930d4, 0x1e602008); //soak program
732 			otp_prog(prog_address, prog_bit);
733 			if (prog_verify(prog_address, offset, 1) == 0) {
734 				pass = 1;
735 				break;
736 			}
737 		}
738 		if (!pass)
739 			return -1;
740 
741 	}
742 	if (fail == 1)
743 		return -1;
744 	else
745 		return 0;
746 
747 }
748 
749 static int otp_prog_data(uint32_t *buf, int otp_addr, int dw_count)
750 {
751 	int i, j, k, bit_value;
752 	int pass, soak;
753 	uint32_t prog_bit, prog_address;
754 
755 	for (i = 0; i < dw_count; i++) {
756 		prog_address = i + otp_addr;
757 		for (j = 0; j < 32; j++) {
758 			bit_value = (buf[i] >> j) & 0x1;
759 			if (prog_address % 2 == 0) {
760 				prog_address |= 1 << 15;
761 				if (bit_value)
762 					prog_bit = ~(0x1 << j);
763 				else
764 					continue;
765 			} else {
766 				prog_address |= 1 << 15;
767 				// printf("bit_value = %x\n", bit_value);
768 				if (bit_value)
769 					continue;
770 				else
771 					prog_bit = 0x1 << j;
772 			}
773 			pass = 0;
774 			soak = 0;
775 			otp_write(0x3000, 0x4061); // Write MRA
776 			otp_write(0x5000, 0x302f); // Write MRB
777 			otp_write(0x1000, 0x4020); // Write MR
778 			writel(0x04190760, 0x1e602008); //normal program
779 			for (k = 0; k < RETRY; k++) {
780 				if (!soak) {
781 					// printf("prog_address = %x\n", prog_address);
782 					// printf("prog_bit = %x\n", prog_bit);
783 					otp_prog(prog_address, prog_bit);
784 					if (prog_verify(prog_address, j, bit_value) == 0) {
785 						pass = 1;
786 						break;
787 					}
788 					soak = 1;
789 					otp_write(0x3000, 0x4021); // Write MRA
790 					otp_write(0x5000, 0x1027); // Write MRB
791 					otp_write(0x1000, 0x4820); // Write MR
792 					writel(0x041930d4, 0x1e602008); //soak program
793 				}
794 				otp_prog(prog_address, prog_bit);
795 				if (prog_verify(prog_address, j, bit_value) == 0) {
796 					pass = 1;
797 					break;
798 				}
799 			}
800 			if (!pass)
801 				return -1;
802 		}
803 	}
804 	return 0;
805 }
806 
807 static int do_otp_prog(int mode, int addr, int otp_addr, int dw_count, int nconfirm)
808 {
809 	int ret;
810 	uint32_t *buf;
811 
812 	buf = map_physmem(addr, dw_count * 4, MAP_WRBACK);
813 	if (!buf) {
814 		puts("Failed to map physical memory\n");
815 		return 1;
816 	}
817 	if (!nconfirm) {
818 		if (mode == MODE_CONF) {
819 			if (otp_conf_parse(buf) < 0) {
820 				printf("OTP config error, please check.\n");
821 				return -1;
822 			}
823 		} else if (mode == MODE_DATA) {
824 			if (otp_data_parse(buf, dw_count) < 0) {
825 				printf("OTP data error, please check.\n");
826 				return -1;
827 			}
828 		} else if (mode == MODE_STRAP) {
829 			if (otp_strap_parse(buf) < 0) {
830 				printf("OTP strap error, please check.\n");
831 				return -1;
832 			}
833 		} else if (mode == MODE_ALL) {
834 			if (otp_conf_parse(buf) < 0) {
835 				printf("OTP config error, please check.\n");
836 				return -1;
837 			}
838 			if (otp_strap_parse(&buf[12]) < 0) {
839 				printf("OTP strap error, please check.\n");
840 				return -1;
841 			}
842 			if (otp_data_parse(&buf[18], dw_count - 18) < 0) {
843 				printf("OTP data error, please check.\n");
844 				return -1;
845 			}
846 		}
847 		printf("type \"YES\" (no quotes) to continue:\n");
848 		if (!confirm_yesno()) {
849 			printf(" Aborting\n");
850 			return 1;
851 		}
852 	}
853 	if (mode == MODE_CONF) {
854 		return otp_prog_conf(buf, otp_addr, dw_count);
855 	} else if (mode == MODE_STRAP) {
856 		return otp_prog_strap(buf);
857 	} else if (mode == MODE_DATA) {
858 		return otp_prog_data(buf, otp_addr, dw_count);
859 	} else if (mode == MODE_ALL) {
860 		printf("programing data region ... ");
861 		ret = otp_prog_data(&buf[16], 0, dw_count - 18);
862 		if (ret < 0) {
863 			printf("Error\n");
864 			return ret;
865 		} else {
866 			printf("Done\n");
867 		}
868 		printf("programing strap region ... ");
869 		ret = otp_prog_strap(&buf[12]);
870 		if (ret < 0) {
871 			printf("Error\n");
872 			return ret;
873 		} else {
874 			printf("Done\n");
875 		}
876 		printf("programing configuration region ... ");
877 		ret = otp_prog_conf(buf, 0, 12);
878 		if (ret < 0) {
879 			printf("Error\n");
880 			return ret;
881 		}
882 		printf("Done\n");
883 		return ret;
884 	}
885 	return 0;
886 }
887 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc,
888 		      char *const argv[])
889 {
890 	char *cmd;
891 	int mode = 0;
892 	int nconfirm = 0;
893 	uint32_t addr, dw_count, otp_addr;
894 
895 
896 
897 	if (argc < 2) {
898 usage:
899 		return CMD_RET_USAGE;
900 	}
901 
902 	cmd = argv[1];
903 	if (!strcmp(cmd, "read")) {
904 		if (!strcmp(argv[2], "conf"))
905 			mode = MODE_CONF;
906 		else if (!strcmp(argv[2], "data"))
907 			mode = MODE_DATA;
908 		else if (!strcmp(argv[2], "strap"))
909 			mode = MODE_STRAP;
910 		else
911 			goto usage;
912 
913 		writel(OTP_PASSWD, 0x1e6f2000); //password
914 		otp_addr = simple_strtoul(argv[3], NULL, 16);
915 		dw_count = simple_strtoul(argv[4], NULL, 16);
916 		if (mode == MODE_CONF) {
917 			otp_print_config(otp_addr, dw_count);
918 		} else if (mode == MODE_DATA) {
919 			otp_print_data(otp_addr, dw_count);
920 		} else if (mode == MODE_STRAP) {
921 			otp_print_strap();
922 		}
923 	} else if (!strcmp(cmd, "prog")) {
924 		if (!strcmp(argv[2], "conf"))
925 			mode = MODE_CONF;
926 		else if (!strcmp(argv[2], "strap"))
927 			mode = MODE_STRAP;
928 		else if (!strcmp(argv[2], "data"))
929 			mode = MODE_DATA;
930 		else if (!strcmp(argv[2], "all"))
931 			mode = MODE_ALL;
932 		else
933 			goto usage;
934 
935 		if (!strcmp(argv[3], "f"))
936 			nconfirm = 1;
937 		writel(OTP_PASSWD, 0x1e6f2000); //password
938 		addr = simple_strtoul(argv[3 + nconfirm], NULL, 16);
939 		otp_addr = simple_strtoul(argv[4 + nconfirm], NULL, 16);
940 		dw_count = simple_strtoul(argv[5 + nconfirm], NULL, 16);
941 		return do_otp_prog(mode, addr, otp_addr, dw_count, nconfirm);
942 	} else if (!strcmp(cmd, "comp")) {
943 		writel(OTP_PASSWD, 0x1e6f2000); //password
944 		addr = simple_strtoul(argv[2], NULL, 16);
945 		otp_addr = simple_strtoul(argv[3], NULL, 16);
946 		if (otp_compare(otp_addr, addr) >= 0) {
947 			printf("Compare pass\n");
948 		} else {
949 			printf("Compare fail\n");
950 		}
951 	} else {
952 		goto usage;
953 	}
954 
955 
956 	return 0;
957 }
958 
959 
960 U_BOOT_CMD(
961 	otp, 7, 0,  do_ast_otp,
962 	"ASPEED One-Time-Programmable sub-system",
963 	"read conf|strap|data <otp_addr> <dw_count>\n"
964 	"otp prog conf|strap|data|all [f] <addr> <otp_addr> <dw_count>\n"
965 	"otp comp <addr> <otp_addr>"
966 );
967