xref: /openbmc/u-boot/cmd/otp.c (revision b0a2e3f1)
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 	otp_write(0x3000, 0x4061); // Write MRA
466 	otp_write(0x5000, 0x302f); // Write MRB
467 	otp_write(0x1000, 0x4020); // Write MR
468 	for (i = 0; i < dw_count; i++) {
469 		prog_address = 0x800;
470 		prog_address |= ((i + otp_addr) / 8) * 0x200;
471 		prog_address |= ((i + otp_addr) % 8) * 0x2;
472 		for (j = 0; j < 32; j++) {
473 			bit_value = (buf[i] >> j) & 0x1;
474 			if (bit_value)
475 				prog_bit = ~(0x1 << j);
476 			else
477 				continue;
478 			pass = 0;
479 			soak = 0;
480 			for (k = 0; k < RETRY; k++) {
481 				if (!soak) {
482 					writel(0x04190760, 0x1e602008); //normal program
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 				}
490 				writel(0x041930d4, 0x1e602008); //soak program
491 				otp_prog(prog_address, prog_bit);
492 				if (prog_verify(prog_address, j, bit_value) == 0) {
493 					pass = 1;
494 					break;
495 				}
496 			}
497 			if (!pass)
498 				return -1;
499 		}
500 	}
501 	return 0;
502 }
503 
504 static void otp_strp_status(struct otpstrap *otpstrap)
505 {
506 	uint32_t OTPSTRAP_RAW[2];
507 	int i, j;
508 
509 	for (j = 0; j < 64; j++) {
510 		otpstrap[j].value = 0;
511 		otpstrap[j].remain_times = 7;
512 		otpstrap[j].writeable_option = -1;
513 		otpstrap[j].protected = 0;
514 	}
515 
516 	for (i = 16; i < 30; i += 2) {
517 		int option = (i - 16) / 2;
518 		otp_read_config(i, &OTPSTRAP_RAW[0]);
519 		otp_read_config(i + 1, &OTPSTRAP_RAW[1]);
520 		for (j = 0; j < 32; j++) {
521 			char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1);
522 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
523 				otpstrap[j].writeable_option = option;
524 			}
525 			if (bit_value == 1)
526 				otpstrap[j].remain_times --;
527 			otpstrap[j].value ^= bit_value;
528 			otpstrap[j].option_array[option] = bit_value;
529 		}
530 		for (j = 32; j < 64; j++) {
531 			char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1);
532 			if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) {
533 				otpstrap[j].writeable_option = option;
534 			}
535 			if (bit_value == 1)
536 				otpstrap[j].remain_times --;
537 			otpstrap[j].value ^= bit_value;
538 			otpstrap[j].option_array[option] = bit_value;
539 		}
540 	}
541 	otp_read_config(30, &OTPSTRAP_RAW[0]);
542 	otp_read_config(31, &OTPSTRAP_RAW[1]);
543 	for (j = 0; j < 32; j++) {
544 		if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1)
545 			otpstrap[j].protected = 1;
546 	}
547 	for (j = 32; j < 64; j++) {
548 		if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1)
549 			otpstrap[j].protected = 1;
550 	}
551 }
552 
553 static int otp_strap_parse(uint32_t *buf)
554 {
555 	int i;
556 	uint32_t *strap_keep = buf + 2;
557 	uint32_t *strap_protect = buf + 4;
558 	int bit, pbit, kbit;
559 	int fail = 0;
560 	struct otpstrap otpstrap[64];
561 
562 	otp_strp_status(otpstrap);
563 	for (i = 0; i < 64; i++) {
564 		if (i < 32) {
565 			bit = (buf[0] >> i) & 0x1;
566 			kbit = (strap_keep[0] >> i) & 0x1;
567 			pbit = (strap_protect[0] >> i) & 0x1;
568 		} else {
569 			bit = (buf[1] >> (i - 32)) & 0x1;
570 			kbit = (strap_keep[1] >> (i - 32)) & 0x1;
571 			pbit = (strap_protect[1] >> (i - 32)) & 0x1;
572 		}
573 
574 		if (kbit == 1) {
575 			continue;
576 		} else {
577 			printf("OTPSTRAP[%d]:\n", i);
578 		}
579 		if (bit == otpstrap[i].value) {
580 			printf("    The value is same as before, skip it.\n");
581 			continue;
582 		}
583 		if (otpstrap[i].protected == 1) {
584 			printf("    This bit is protected and is not writable\n");
585 			fail = 1;
586 			continue;
587 		}
588 		if (otpstrap[i].remain_times == 0) {
589 			printf("    This bit is no remaining number of times to write.\n");
590 			fail = 1;
591 			continue;
592 		}
593 		if (pbit == 1) {
594 			printf("    This bit will be protected and become non-writable.\n");
595 		}
596 		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);
597 	}
598 	if (fail == 1)
599 		return -1;
600 	else
601 		return 0;
602 }
603 
604 static void otp_print_strap(void)
605 {
606 	int i, j;
607 	struct otpstrap otpstrap[64];
608 
609 	otp_strp_status(otpstrap);
610 
611 	for (i = 0; i < 64; i++) {
612 		printf("OTPSTRAP[%d]:\n", i);
613 		printf("  OTP Option value: ");
614 		for (j = 1; j <= 7; j++)
615 			printf("[%d]:%d ", j, otpstrap[i].option_array[j - 1]);
616 		printf("\n");
617 		printf("  OTP Value: %d\n", otpstrap[i].value);
618 		printf("  Status:\n");
619 		if (otpstrap[i].protected == 1) {
620 			printf("    OTPSTRAP[%d] is protected and is not writable\n", i);
621 		} else {
622 			printf("    OTPSTRAP[%d] is not protected ", i);
623 			if (otpstrap[i].remain_times == 0) {
624 				printf("and no remaining number of times to write.\n");
625 			} else {
626 				printf("and still can write %d number of times\n", otpstrap[i].remain_times);
627 			}
628 		}
629 	}
630 }
631 
632 static int otp_prog_strap(uint32_t *buf)
633 {
634 	int i, j;
635 	uint32_t *strap_keep = buf + 2;
636 	uint32_t *strap_protect = buf + 4;
637 	uint32_t prog_bit, prog_address;
638 	int bit, pbit, kbit, offset;
639 	int fail = 0;
640 	int pass, soak;
641 	struct otpstrap otpstrap[64];
642 
643 	otp_strp_status(otpstrap);
644 
645 	otp_write(0x3000, 0x4061); // Write MRA
646 	otp_write(0x5000, 0x302f); // Write MRB
647 	otp_write(0x1000, 0x4020); // Write MR
648 	for (i = 0; i < 64; i++) {
649 		prog_address = 0x800;
650 		if (i < 32) {
651 			offset = i;
652 			bit = (buf[0] >> offset) & 0x1;
653 			kbit = (strap_keep[0] >> offset) & 0x1;
654 			pbit = (strap_protect[0] >> offset) & 0x1;
655 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200;
656 			prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2;
657 
658 		} else {
659 			offset = (i - 32);
660 			bit = (buf[1] >> offset) & 0x1;
661 			kbit = (strap_keep[1] >> offset) & 0x1;
662 			pbit = (strap_protect[1] >> offset) & 0x1;
663 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200;
664 			prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2;
665 		}
666 		prog_bit = ~(0x1 << offset);
667 
668 		if (kbit == 1) {
669 			continue;
670 		}
671 		if (bit == otpstrap[i].value) {
672 			continue;
673 		}
674 		if (otpstrap[i].protected == 1) {
675 			fail = 1;
676 			continue;
677 		}
678 		if (otpstrap[i].remain_times == 0) {
679 			fail = 1;
680 			continue;
681 		}
682 		pass = 0;
683 		soak = 0;
684 		for (j = 0; j < RETRY; j++) {
685 			if (!soak) {
686 				writel(0x04190760, 0x1e602008); //normal program
687 				otp_prog(prog_address, prog_bit);
688 				if (prog_verify(prog_address, offset, 1) == 0) {
689 					pass = 1;
690 					break;
691 				}
692 				soak = 1;
693 			}
694 			writel(0x041930d4, 0x1e602008); //soak program
695 			otp_prog(prog_address, prog_bit);
696 			if (prog_verify(prog_address, offset, 1) == 0) {
697 				pass = 1;
698 				break;
699 			}
700 		}
701 		if (!pass)
702 			return -1;
703 
704 		if (pbit == 0)
705 			continue;
706 		prog_address = 0x800;
707 		if (i < 32)
708 			prog_address |= 0x60c;
709 		else
710 			prog_address |= 0x60e;
711 
712 		for (j = 0; j < RETRY; j++) {
713 			if (!soak) {
714 				writel(0x04190760, 0x1e602008); //normal program
715 				otp_prog(prog_address, prog_bit);
716 				if (prog_verify(prog_address, offset, 1) == 0) {
717 					pass = 1;
718 					break;
719 				}
720 				soak = 1;
721 			}
722 			writel(0x041930d4, 0x1e602008); //soak program
723 			otp_prog(prog_address, prog_bit);
724 			if (prog_verify(prog_address, offset, 1) == 0) {
725 				pass = 1;
726 				break;
727 			}
728 		}
729 		if (!pass)
730 			return -1;
731 
732 	}
733 	if (fail == 1)
734 		return -1;
735 	else
736 		return 0;
737 
738 }
739 
740 static int otp_prog_data(uint32_t *buf, int otp_addr, int dw_count)
741 {
742 	int i, j, k, bit_value;
743 	int pass, soak;
744 	uint32_t prog_bit, prog_address;
745 
746 	otp_write(0x3000, 0x4061); // Write MRA
747 	otp_write(0x5000, 0x302f); // Write MRB
748 	otp_write(0x1000, 0x4020); // Write MR
749 	for (i = 0; i < dw_count; i++) {
750 		prog_address = i + otp_addr;
751 		for (j = 0; j < 32; j++) {
752 			bit_value = (buf[i] >> j) & 0x1;
753 			if (prog_address % 2 == 0) {
754 				prog_address |= 1 << 15;
755 				if (bit_value)
756 					prog_bit = ~(0x1 << j);
757 				else
758 					continue;
759 			} else {
760 				prog_address |= 1 << 15;
761 				// printf("bit_value = %x\n", bit_value);
762 				if (bit_value)
763 					continue;
764 				else
765 					prog_bit = 0x1 << j;
766 			}
767 			pass = 0;
768 			soak = 0;
769 			for (k = 0; k < RETRY; k++) {
770 				if (!soak) {
771 					writel(0x04190760, 0x1e602008); //normal program
772 					// printf("prog_address = %x\n", prog_address);
773 					// printf("prog_bit = %x\n", prog_bit);
774 					otp_prog(prog_address, prog_bit);
775 					if (prog_verify(prog_address, j, bit_value) == 0) {
776 						pass = 1;
777 						break;
778 					}
779 					soak = 1;
780 				}
781 				writel(0x041930d4, 0x1e602008); //soak program
782 				otp_prog(prog_address, prog_bit);
783 				if (prog_verify(prog_address, j, bit_value) == 0) {
784 					pass = 1;
785 					break;
786 				}
787 			}
788 			if (!pass)
789 				return -1;
790 		}
791 	}
792 	return 0;
793 }
794 
795 static int do_otp_prog(int mode, int addr, int otp_addr, int dw_count, int nconfirm)
796 {
797 	int ret;
798 	uint32_t *buf;
799 
800 	buf = map_physmem(addr, dw_count * 4, MAP_WRBACK);
801 	if (!buf) {
802 		puts("Failed to map physical memory\n");
803 		return 1;
804 	}
805 	if (!nconfirm) {
806 		if (mode == MODE_CONF) {
807 			if (otp_conf_parse(buf) < 0) {
808 				printf("OTP config error, please check.\n");
809 				return -1;
810 			}
811 		} else if (mode == MODE_DATA) {
812 			if (otp_data_parse(buf, dw_count) < 0) {
813 				printf("OTP data error, please check.\n");
814 				return -1;
815 			}
816 		} else if (mode == MODE_STRAP) {
817 			if (otp_strap_parse(buf) < 0) {
818 				printf("OTP strap error, please check.\n");
819 				return -1;
820 			}
821 		} else if (mode == MODE_ALL) {
822 			if (otp_conf_parse(buf) < 0) {
823 				printf("OTP config error, please check.\n");
824 				return -1;
825 			}
826 			if (otp_strap_parse(&buf[12]) < 0) {
827 				printf("OTP strap error, please check.\n");
828 				return -1;
829 			}
830 			if (otp_data_parse(&buf[18], dw_count - 18) < 0) {
831 				printf("OTP data error, please check.\n");
832 				return -1;
833 			}
834 		}
835 		printf("type \"YES\" (no quotes) to continue:\n");
836 		if (!confirm_yesno()) {
837 			printf(" Aborting\n");
838 			return 1;
839 		}
840 	}
841 	if (mode == MODE_CONF) {
842 		return otp_prog_conf(buf, otp_addr, dw_count);
843 	} else if (mode == MODE_STRAP) {
844 		return otp_prog_strap(buf);
845 	} else if (mode == MODE_DATA) {
846 		return otp_prog_data(buf, otp_addr, dw_count);
847 	} else if (mode == MODE_ALL) {
848 		printf("programing data region ... ");
849 		ret = otp_prog_data(&buf[16], 0, dw_count - 18);
850 		if (ret < 0) {
851 			printf("Error\n");
852 			return ret;
853 		} else {
854 			printf("Done\n");
855 		}
856 		printf("programing strap region ... ");
857 		ret = otp_prog_strap(&buf[12]);
858 		if (ret < 0) {
859 			printf("Error\n");
860 			return ret;
861 		} else {
862 			printf("Done\n");
863 		}
864 		printf("programing configuration region ... ");
865 		ret = otp_prog_conf(buf, 0, 12);
866 		if (ret < 0) {
867 			printf("Error\n");
868 			return ret;
869 		}
870 		printf("Done\n");
871 		return ret;
872 	}
873 	return 0;
874 }
875 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc,
876 		      char *const argv[])
877 {
878 	char *cmd;
879 	int mode = 0;
880 	int nconfirm = 0;
881 	uint32_t addr, dw_count, otp_addr;
882 
883 
884 
885 	if (argc < 2) {
886 usage:
887 		return CMD_RET_USAGE;
888 	}
889 
890 	cmd = argv[1];
891 	if (!strcmp(cmd, "read")) {
892 		if (!strcmp(argv[2], "conf"))
893 			mode = MODE_CONF;
894 		else if (!strcmp(argv[2], "data"))
895 			mode = MODE_DATA;
896 		else if (!strcmp(argv[2], "strap"))
897 			mode = MODE_STRAP;
898 		else
899 			goto usage;
900 
901 		writel(OTP_PASSWD, 0x1e6f2000); //password
902 		otp_addr = simple_strtoul(argv[3], NULL, 16);
903 		dw_count = simple_strtoul(argv[4], NULL, 16);
904 		if (mode == MODE_CONF) {
905 			otp_print_config(otp_addr, dw_count);
906 		} else if (mode == MODE_DATA) {
907 			otp_print_data(otp_addr, dw_count);
908 		} else if (mode == MODE_STRAP) {
909 			otp_print_strap();
910 		}
911 	} else if (!strcmp(cmd, "prog")) {
912 		if (!strcmp(argv[2], "conf"))
913 			mode = MODE_CONF;
914 		else if (!strcmp(argv[2], "strap"))
915 			mode = MODE_STRAP;
916 		else if (!strcmp(argv[2], "data"))
917 			mode = MODE_DATA;
918 		else if (!strcmp(argv[2], "all"))
919 			mode = MODE_ALL;
920 		else
921 			goto usage;
922 
923 		if (!strcmp(argv[3], "f"))
924 			nconfirm = 1;
925 		writel(OTP_PASSWD, 0x1e6f2000); //password
926 		addr = simple_strtoul(argv[3 + nconfirm], NULL, 16);
927 		otp_addr = simple_strtoul(argv[4 + nconfirm], NULL, 16);
928 		dw_count = simple_strtoul(argv[5 + nconfirm], NULL, 16);
929 		return do_otp_prog(mode, addr, otp_addr, dw_count, nconfirm);
930 	} else if (!strcmp(cmd, "comp")) {
931 		writel(OTP_PASSWD, 0x1e6f2000); //password
932 		addr = simple_strtoul(argv[2], NULL, 16);
933 		otp_addr = simple_strtoul(argv[3], NULL, 16);
934 		if (otp_compare(otp_addr, addr) >= 0) {
935 			printf("Compare pass\n");
936 		} else {
937 			printf("Compare fail\n");
938 		}
939 	} else {
940 		goto usage;
941 	}
942 
943 
944 	return 0;
945 }
946 
947 
948 U_BOOT_CMD(
949 	otp, 7, 0,  do_ast_otp,
950 	"ASPEED One-Time-Programmable sub-system",
951 	"read conf|strap|data <otp_addr> <dw_count>\n"
952 	"otp prog conf|strap|data|all [f] <addr> <otp_addr> <dw_count>\n"
953 	"otp comp <addr> <otp_addr>"
954 );
955