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 DISABLE_SECREG_PROG BIT(0) 34 #define ENABLE_SEC_BOOT BIT(1) 35 #define INIT_PROG_DONE BIT(2) 36 #define ENABLE_USERREG_ECC BIT(3) 37 #define ENABLE_SECREG_ECC BIT(4) 38 #define DISABLE_LOW_SEC_KEY BIT(5) 39 #define IGNORE_SEC_BOOT_HWSTRAP BIT(6) 40 #define SEC_BOOT_MDOES(x) (x >> 7) 41 #define SEC_MODE1 0x0 42 #define SEC_MODE2 0x1 43 #define OTP_BIT_CELL_MODES(x) ((x >> 8) & 0x3) 44 #define SINGLE_CELL_MODE 0x0 45 #define DIFFERENTIAL_MODE 0x1 46 #define DIFFERENTIAL_REDUDANT_MODE 0x2 47 #define CRYPTO_MODES(x) ((x >> 10) & 0x3) 48 #define CRYPTO_RSA1024 0x0 49 #define CRYPTO_RSA2048 0x1 50 #define CRYPTO_RSA3072 0x2 51 #define CRYPTO_RSA4096 0x3 52 #define HASH_MODES(x) ((x >> 12) & 0x3) 53 #define HASH_SAH224 0x0 54 #define HASH_SAH256 0x1 55 #define HASH_SAH384 0x2 56 #define HASH_SAH512 0x3 57 #define SECREG_SIZE(x) ((x >> 16) & 0x3f) 58 #define WRITE_PROTECT_SECREG BIT(22) 59 #define WRITE_PROTECT_USERREG BIT(23) 60 #define WRITE_PROTECT_CONFREG BIT(24) 61 #define WRITE_PROTECT_STRAPREG BIT(25) 62 #define ENABLE_COPY_TO_SRAM BIT(26) 63 #define ENABLE_IMAGE_ENC BIT(27) 64 #define WRITE_PROTECT_KEY_RETIRE BIT(29) 65 #define ENABLE_SIPROM_RED BIT(30) 66 #define ENABLE_SIPROM_MLOCK BIT(31) 67 68 #define VENDER_ID(x) (x & 0xFFFF) 69 #define KEY_REVISION(x) ((x >> 16) & 0xFFFF) 70 71 #define SEC_BOOT_HEADER_OFFSET(x) (x & 0xFFFF) 72 73 #define KEYS_VALID_BITS(x) (x & 0xff) 74 #define KEYS_RETIRE_BITS(x) ((x >> 16) & 0xff) 75 76 #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||" 77 #define PBWIDTH 60 78 79 void printProgress(int numerator, int denominator, char *format, ...) 80 { 81 int val = numerator * 100 / denominator; 82 int lpad = numerator * PBWIDTH / denominator; 83 int rpad = PBWIDTH - lpad; 84 char buffer[256]; 85 va_list aptr; 86 87 va_start(aptr, format); 88 vsprintf(buffer, format, aptr); 89 va_end(aptr); 90 91 printf("\r%3d%% [%.*s%*s] %s", val, lpad, PBSTR, rpad, "", buffer); 92 if (numerator == denominator) 93 printf("\n"); 94 } 95 96 struct otpstrap { 97 int value; 98 int option_array[7]; 99 int remain_times; 100 int writeable_option; 101 int protected; 102 }; 103 104 static int otp_read_data(uint32_t offset, uint32_t *data) 105 { 106 writel(offset, 0x1e6f2010); //Read address 107 writel(0x23b1e361, 0x1e6f2004); //trigger read 108 udelay(2); 109 data[0] = readl(0x1e6f2020); 110 data[1] = readl(0x1e6f2024); 111 return 1; 112 } 113 114 static int otp_read_config(uint32_t offset, uint32_t *data) 115 { 116 int config_offset; 117 118 config_offset = 0x800; 119 config_offset |= (offset / 8) * 0x200; 120 config_offset |= (offset % 8) * 0x2; 121 122 writel(config_offset, 0x1e6f2010); //Read address 123 writel(0x23b1e361, 0x1e6f2004); //trigger read 124 udelay(2); 125 data[0] = readl(0x1e6f2020); 126 127 return 1; 128 } 129 130 static int otp_print_config(uint32_t offset, int dw_count) 131 { 132 int i; 133 uint32_t ret[1]; 134 135 if (offset + dw_count > 32) 136 return -1; 137 for (i = offset; i < offset + dw_count; i ++) { 138 otp_read_config(i, ret); 139 printf("OTPCFG%d: %08X\n", i, ret[0]); 140 } 141 printf("\n"); 142 return 1; 143 } 144 145 static int otp_print_data(uint32_t offset, int dw_count) 146 { 147 int i; 148 uint32_t ret[2]; 149 150 if (offset + dw_count > 2048 || offset % 4 != 0) 151 return -1; 152 for (i = offset; i < offset + dw_count; i += 2) { 153 otp_read_data(i, ret); 154 if (i % 4 == 0) 155 printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]); 156 else 157 printf("%08X %08X\n", ret[0], ret[1]); 158 159 } 160 printf("\n"); 161 return 1; 162 } 163 164 static int otp_compare(uint32_t otp_addr, uint32_t addr) 165 { 166 uint32_t ret; 167 uint32_t *buf; 168 169 buf = map_physmem(addr, 16, MAP_WRBACK); 170 printf("%08X\n", buf[0]); 171 printf("%08X\n", buf[1]); 172 printf("%08X\n", buf[2]); 173 printf("%08X\n", buf[3]); 174 writel(otp_addr, 0x1e6f2010); //Compare address 175 writel(buf[0], 0x1e6f2020); //Compare data 1 176 writel(buf[1], 0x1e6f2024); //Compare data 2 177 writel(buf[2], 0x1e6f2028); //Compare data 3 178 writel(buf[3], 0x1e6f202c); //Compare data 4 179 writel(0x23b1e363, 0x1e6f2004); //Compare command 180 udelay(10); 181 ret = readl(0x1e6f2014); //Compare command 182 if (ret & 0x1) 183 return 0; 184 else 185 return -1; 186 } 187 188 static void otp_write(uint32_t otp_addr, uint32_t data) 189 { 190 writel(otp_addr, 0x1e6f2010); //write address 191 writel(data, 0x1e6f2020); //write data 192 writel(0x23b1e362, 0x1e6f2004); //write command 193 udelay(100); 194 } 195 196 static void otp_prog(uint32_t otp_addr, uint32_t prog_bit) 197 { 198 writel(otp_addr, 0x1e6f2010); //write address 199 writel(prog_bit, 0x1e6f2020); //write data 200 writel(0x23b1e364, 0x1e6f2004); //write command 201 udelay(85); 202 } 203 204 static int verify_bit(uint32_t otp_addr, int bit_offset, int value) 205 { 206 int ret; 207 208 writel(otp_addr, 0x1e6f2010); //Read address 209 writel(0x23b1e361, 0x1e6f2004); //trigger read 210 udelay(2); 211 ret = readl(0x1e6f2020); 212 // printf("verify_bit = %x\n", ret); 213 if (((ret >> bit_offset) & 1) == value) 214 return 0; 215 else 216 return -1; 217 } 218 219 static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *keep, uint32_t *compare, int size) 220 { 221 uint32_t ret[2]; 222 223 otp_addr &= ~(1 << 15); 224 225 if (otp_addr % 2 == 0) 226 writel(otp_addr, 0x1e6f2010); //Read address 227 else 228 writel(otp_addr - 1, 0x1e6f2010); //Read address 229 writel(0x23b1e361, 0x1e6f2004); //trigger read 230 udelay(2); 231 ret[0] = readl(0x1e6f2020); 232 ret[1] = readl(0x1e6f2024); 233 if (size == 1) { 234 if (otp_addr % 2 == 0) { 235 // printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]); 236 if ((value[0] & ~keep[0]) == (ret[0] & ~keep[0])) { 237 compare[0] = 0; 238 return 0; 239 } else { 240 compare[0] = value[0] ^ ret[0]; 241 return -1; 242 } 243 244 } else { 245 // printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]); 246 if ((value[0] & ~keep[0]) == (ret[1] & ~keep[0])) { 247 compare[0] = ~0; 248 return 0; 249 } else { 250 compare[0] = ~(value[0] ^ ret[1]); 251 return -1; 252 } 253 } 254 } else if (size == 2) { 255 // otp_addr should be even 256 if ((value[0] & ~keep[0]) == (ret[0] & ~keep[0]) && (value[1] & ~keep[1]) == (ret[1] & ~keep[1])) { 257 // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 258 // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 259 compare[0] = 0; 260 compare[1] = ~0; 261 return 0; 262 } else { 263 // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 264 // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 265 compare[0] = value[0] ^ ret[0]; 266 compare[1] = ~(value[1] ^ ret[1]); 267 return -1; 268 } 269 } else { 270 return -1; 271 } 272 } 273 274 void otp_soak(int soak) 275 { 276 if (soak) { 277 otp_write(0x3000, 0x4021); // Write MRA 278 otp_write(0x5000, 0x1027); // Write MRB 279 otp_write(0x1000, 0x4820); // Write MR 280 writel(0x041930d4, 0x1e602008); //soak program 281 } else { 282 otp_write(0x3000, 0x4061); // Write MRA 283 otp_write(0x5000, 0x302f); // Write MRB 284 otp_write(0x1000, 0x4020); // Write MR 285 writel(0x04190760, 0x1e602008); //normal program 286 } 287 } 288 289 static void otp_prog_dw(uint32_t value, uint32_t keep, uint32_t prog_address) 290 { 291 int j, bit_value, prog_bit; 292 293 for (j = 0; j < 32; j++) { 294 if ((keep >> j) & 0x1) 295 continue; 296 bit_value = (value >> j) & 0x1; 297 if (prog_address % 2 == 0) { 298 if (bit_value) 299 prog_bit = ~(0x1 << j); 300 else 301 continue; 302 } else { 303 prog_address |= 1 << 15; 304 if (bit_value) 305 continue; 306 else 307 prog_bit = 0x1 << j; 308 } 309 otp_prog(prog_address, prog_bit); 310 } 311 } 312 313 static int otp_conf_parse(uint32_t *OTPCFG) 314 { 315 int tmp, i; 316 int pass = 0; 317 uint32_t *OTPCFG_KEEP = &OTPCFG[12]; 318 319 if (OTPCFG_KEEP[0] & DISABLE_SECREG_PROG) { 320 printf("OTPCFG0-D[0]\n"); 321 printf(" Skip\n"); 322 } else { 323 printf("OTPCFG0-D[0]\n"); 324 if (OTPCFG[0] & DISABLE_SECREG_PROG) 325 printf(" Disable Secure Region programming\n"); 326 else 327 printf(" Enable Secure Region programming\n"); 328 } 329 330 if (OTPCFG_KEEP[0] & ENABLE_SEC_BOOT) { 331 printf("OTPCFG0-D[1]\n"); 332 printf(" Skip\n"); 333 } else { 334 335 printf("OTPCFG0-D[1]\n"); 336 if (OTPCFG[0] & ENABLE_SEC_BOOT) 337 printf(" Enable Secure Boot\n"); 338 else 339 printf(" Disable Secure Boot\n"); 340 } 341 342 if (OTPCFG_KEEP[0] & ENABLE_USERREG_ECC) { 343 printf("OTPCFG0-D[3]\n"); 344 printf(" Skip\n"); 345 } else { 346 printf("OTPCFG0-D[3]\n"); 347 if (OTPCFG[0] & ENABLE_USERREG_ECC) 348 printf(" User region ECC enable\n"); 349 else 350 printf(" User region ECC disable\n"); 351 } 352 353 if (OTPCFG_KEEP[0] & ENABLE_SECREG_ECC) { 354 printf("OTPCFG0-D[4]\n"); 355 printf(" Skip\n"); 356 } else { 357 printf("OTPCFG0-D[4]\n"); 358 if (OTPCFG[0] & ENABLE_SECREG_ECC) 359 printf(" Secure Region ECC enable\n"); 360 else 361 printf(" Secure Region ECC disable\n"); 362 } 363 364 if (OTPCFG_KEEP[0] & DISABLE_LOW_SEC_KEY) { 365 printf("OTPCFG0-D[5]\n"); 366 printf(" Skip\n"); 367 } else { 368 printf("OTPCFG0-D[5]\n"); 369 if (OTPCFG[0] & DISABLE_LOW_SEC_KEY) 370 printf(" Disable low security key\n"); 371 else 372 printf(" Enable low security key\n"); 373 } 374 375 if (OTPCFG_KEEP[0] & IGNORE_SEC_BOOT_HWSTRAP) { 376 printf("OTPCFG0-D[6]\n"); 377 printf(" Skip\n"); 378 } else { 379 printf("OTPCFG0-D[6]\n"); 380 if (OTPCFG[0] & IGNORE_SEC_BOOT_HWSTRAP) 381 printf(" Ignore Secure Boot hardware strap\n"); 382 else 383 printf(" Do not ignore Secure Boot hardware strap\n"); 384 } 385 386 if (SEC_BOOT_MDOES(OTPCFG_KEEP[0]) == 0x1) { 387 printf("OTPCFG0-D[7]\n"); 388 printf(" Skip\n"); 389 } else { 390 printf("OTPCFG0-D[7]\n"); 391 if (SEC_BOOT_MDOES(OTPCFG[0]) == SEC_MODE1) 392 printf(" Secure Boot Mode: 1\n"); 393 else 394 printf(" Secure Boot Mode: 2\n"); 395 } 396 397 if (OTP_BIT_CELL_MODES(OTPCFG_KEEP[0]) == 0x3) { 398 printf("OTPCFG0-D[9:8]\n"); 399 printf(" Skip\n"); 400 } else { 401 printf("OTPCFG0-D[9:8]\n"); 402 printf(" OTP bit cell mode : "); 403 tmp = OTP_BIT_CELL_MODES(OTPCFG[0]); 404 if (tmp == SINGLE_CELL_MODE) { 405 printf("Single cell mode (recommended)\n"); 406 } else if (tmp == DIFFERENTIAL_MODE) { 407 printf("Differnetial mode\n"); 408 } else if (tmp == DIFFERENTIAL_REDUDANT_MODE) { 409 printf("Differential-redundant mode\n"); 410 } else { 411 printf("Value error\n"); 412 return -1; 413 } 414 } 415 if (CRYPTO_MODES(OTPCFG_KEEP[0]) == 0x3) { 416 printf("OTPCFG0-D[11:10]\n"); 417 printf(" Skip\n"); 418 } else { 419 printf("OTPCFG0-D[11:10]\n"); 420 printf(" RSA mode : "); 421 tmp = CRYPTO_MODES(OTPCFG[0]); 422 if (tmp == CRYPTO_RSA1024) { 423 printf("RSA1024\n"); 424 } else if (tmp == CRYPTO_RSA2048) { 425 printf("RSA2048\n"); 426 } else if (tmp == CRYPTO_RSA3072) { 427 printf("RSA3072\n"); 428 } else { 429 printf("RSA4096\n"); 430 } 431 } 432 if (HASH_MODES(OTPCFG_KEEP[0]) == 0x3) { 433 printf("OTPCFG0-D[13:12]\n"); 434 printf(" Skip\n"); 435 } else { 436 printf("OTPCFG0-D[13:12]\n"); 437 printf(" SHA mode : "); 438 tmp = HASH_MODES(OTPCFG[0]); 439 if (tmp == HASH_SAH224) { 440 printf("SHA224\n"); 441 } else if (tmp == HASH_SAH256) { 442 printf("SHA256\n"); 443 } else if (tmp == HASH_SAH384) { 444 printf("SHA384\n"); 445 } else { 446 printf("SHA512\n"); 447 } 448 } 449 450 if (SECREG_SIZE(OTPCFG_KEEP[0]) == 0x3f) { 451 printf("OTPCFG0-D[21:16]\n"); 452 printf(" Skip\n"); 453 } else { 454 printf("OTPCFG0-D[21:16]\n"); 455 printf(" Secure Region size (DW): %x\n", SECREG_SIZE(OTPCFG[0])); 456 } 457 458 if (OTPCFG_KEEP[0] & WRITE_PROTECT_SECREG) { 459 printf("OTPCFG0-D[22]\n"); 460 printf(" Skip\n"); 461 } else { 462 printf("OTPCFG0-D[22]\n"); 463 if (OTPCFG[0] & WRITE_PROTECT_SECREG) 464 printf(" Secure Region : Write Protect\n"); 465 else 466 printf(" Secure Region : Writable\n"); 467 } 468 469 if (OTPCFG_KEEP[0] & WRITE_PROTECT_USERREG) { 470 printf("OTPCFG0-D[23]\n"); 471 printf(" Skip\n"); 472 } else { 473 printf("OTPCFG0-D[23]\n"); 474 if (OTPCFG[0] & WRITE_PROTECT_USERREG) 475 printf(" User Region : Write Protect\n"); 476 else 477 printf(" User Region : Writable\n"); 478 } 479 480 if (OTPCFG_KEEP[0] & WRITE_PROTECT_CONFREG) { 481 printf("OTPCFG0-D[24]\n"); 482 printf(" Skip\n"); 483 } else { 484 printf("OTPCFG0-D[24]\n"); 485 if (OTPCFG[0] & WRITE_PROTECT_CONFREG) 486 printf(" Configure Region : Write Protect\n"); 487 else 488 printf(" Configure Region : Writable\n"); 489 } 490 491 if (OTPCFG_KEEP[0] & WRITE_PROTECT_STRAPREG) { 492 printf("OTPCFG0-D[25]\n"); 493 printf(" Skip\n"); 494 } else { 495 printf("OTPCFG0-D[25]\n"); 496 if (OTPCFG[0] & WRITE_PROTECT_STRAPREG) 497 printf(" OTP strap Region : Write Protect\n"); 498 else 499 printf(" OTP strap Region : Writable\n"); 500 } 501 502 if (OTPCFG_KEEP[0] & ENABLE_COPY_TO_SRAM) { 503 printf("OTPCFG0-D[25]\n"); 504 printf(" Skip\n"); 505 } else { 506 printf("OTPCFG0-D[26]\n"); 507 if (OTPCFG[0] & ENABLE_COPY_TO_SRAM) 508 printf(" Copy Boot Image to Internal SRAM\n"); 509 else 510 printf(" Disable Copy Boot Image to Internal SRAM\n"); 511 } 512 if (OTPCFG_KEEP[0] & ENABLE_IMAGE_ENC) { 513 printf("OTPCFG0-D[27]\n"); 514 printf(" Skip\n"); 515 } else { 516 printf("OTPCFG0-D[27]\n"); 517 if (OTPCFG[0] & ENABLE_IMAGE_ENC) 518 printf(" Enable image encryption\n"); 519 else 520 printf(" Disable image encryption\n"); 521 } 522 523 if (OTPCFG_KEEP[0] & WRITE_PROTECT_KEY_RETIRE) { 524 printf("OTPCFG0-D[29]\n"); 525 printf(" Skip\n"); 526 } else { 527 printf("OTPCFG0-D[29]\n"); 528 if (OTPCFG[0] & WRITE_PROTECT_KEY_RETIRE) 529 printf(" OTP key retire Region : Write Protect\n"); 530 else 531 printf(" OTP key retire Region : Writable\n"); 532 } 533 534 if (OTPCFG_KEEP[0] & ENABLE_SIPROM_RED) { 535 printf("OTPCFG0-D[30]\n"); 536 printf(" Skip\n"); 537 } else { 538 printf("OTPCFG0-D[30]\n"); 539 if (OTPCFG[0] & ENABLE_SIPROM_RED) 540 printf(" SIPROM RED_EN redundancy repair enable\n"); 541 else 542 printf(" SIPROM RED_EN redundancy repair disable\n"); 543 } 544 545 if (OTPCFG_KEEP[0] & ENABLE_SIPROM_MLOCK) { 546 printf("OTPCFG0-D[31]\n"); 547 printf(" Skip\n"); 548 } else { 549 printf("OTPCFG0-D[31]\n"); 550 if (OTPCFG[0] & ENABLE_SIPROM_MLOCK) 551 printf(" SIPROM Mlock memory lock enable\n"); 552 else 553 printf(" SIPROM Mlock memory lock disable\n"); 554 } 555 if (SECREG_SIZE(OTPCFG_KEEP[2]) == 0xFFFF) { 556 printf("OTPCFG2-D[15:0]\n"); 557 printf(" Skip\n"); 558 } else { 559 printf("OTPCFG2-D[15:0]\n"); 560 printf(" Vender ID : %x\n", VENDER_ID(OTPCFG[2])); 561 } 562 563 if (SECREG_SIZE(OTPCFG_KEEP[2]) == 0xFFFF) { 564 printf("OTPCFG2-D[31:16]\n"); 565 printf(" Skip\n"); 566 } else { 567 printf("OTPCFG2-D[31:16]\n"); 568 printf(" Key Revision : %x\n", KEY_REVISION(OTPCFG[2])); 569 } 570 571 if (SEC_BOOT_HEADER_OFFSET(OTPCFG_KEEP[3]) == 0xFFFF) { 572 printf("OTPCFG3-D[15:0]\n"); 573 printf(" Skip\n"); 574 } else { 575 printf("OTPCFG3-D[15:0]\n"); 576 printf(" Secure boot header offset : %x\n", 577 SEC_BOOT_HEADER_OFFSET(OTPCFG[3])); 578 } 579 580 if (KEYS_VALID_BITS(OTPCFG_KEEP[4]) == 0xFF) { 581 printf("OTPCFG4-D[7:0]\n"); 582 printf(" Skip\n"); 583 } else { 584 printf("OTPCFG4-D[7:0]\n"); 585 tmp = KEYS_VALID_BITS(OTPCFG[4]); 586 if (tmp != 0) { 587 for (i = 0; i < 7; i++) { 588 if (tmp == (1 << i)) { 589 pass = i + 1; 590 } 591 } 592 } else { 593 pass = 0; 594 } 595 printf(" Keys valid : %d\n", pass); 596 } 597 if (KEYS_RETIRE_BITS(OTPCFG_KEEP[4]) == 0xFF) { 598 printf("OTPCFG4-D[23:16]\n"); 599 printf(" Skip\n"); 600 } else { 601 printf("OTPCFG4-D[23:16]\n"); 602 tmp = KEYS_RETIRE_BITS(OTPCFG[4]); 603 if (tmp != 0) { 604 for (i = 0; i < 7; i++) { 605 if (tmp == (1 << i)) { 606 pass = i + 1; 607 } 608 } 609 } else { 610 pass = 0; 611 } 612 printf(" Keys Retire ID : %d\n", pass); 613 } 614 if (OTPCFG_KEEP[5] == 0xFFFFFFFF) { 615 printf("OTPCFG5-D[31:0]\n"); 616 printf(" Skip\n"); 617 } else { 618 printf("OTPCFG5-D[31:0]\n"); 619 printf(" User define data, random number low : %x\n", OTPCFG[5]); 620 } 621 622 if (OTPCFG_KEEP[6] == 0xFFFFFFFF) { 623 printf("OTPCFG6-D[31:0]\n"); 624 printf(" Skip\n"); 625 } else { 626 printf("OTPCFG6-D[31:0]\n"); 627 printf(" User define data, random number high : %x\n", OTPCFG[6]); 628 } 629 630 if (OTPCFG_KEEP[8] == 0xFFFFFFFF) { 631 printf("OTPCFG8-D[31:0]\n"); 632 printf(" Skip\n"); 633 } else { 634 printf("OTPCFG8-D[31:0]\n"); 635 printf(" Redundancy Repair : %x\n", OTPCFG[8]); 636 } 637 638 if (OTPCFG_KEEP[10] == 0xFFFFFFFF) { 639 printf("OTPCFG10-D[31:0]\n"); 640 printf(" Skip\n"); 641 } else { 642 printf("OTPCFG10-D[31:0]\n"); 643 printf(" Manifest ID low : %x\n", OTPCFG[10]); 644 } 645 646 if (OTPCFG_KEEP[11] == 0xFFFFFFFF) { 647 printf("OTPCFG11-D[31:0]\n"); 648 printf(" Skip\n"); 649 } else { 650 printf("OTPCFG11-D[31:0]\n"); 651 printf(" Manifest ID high : %x\n", OTPCFG[11]); 652 } 653 654 return 0; 655 656 } 657 658 static void buf_print(char *buf, int len) 659 { 660 int i; 661 printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); 662 for (i = 0; i < len; i++) { 663 if (i % 16 == 0) { 664 printf("%04X: ", i); 665 } 666 printf("%02X ", buf[i]); 667 if ((i + 1) % 16 == 0) { 668 printf("\n"); 669 } 670 } 671 } 672 673 static int otp_data_parse(uint32_t *buf) 674 { 675 int key_id, key_offset, last, key_type, key_length, exp_length; 676 char *byte_buf; 677 int i = 0, len = 0; 678 byte_buf = (char *)buf; 679 while (1) { 680 key_id = buf[i] & 0x7; 681 key_offset = buf[i] & 0x1ff8; 682 last = (buf[i] >> 13) & 1; 683 key_type = (buf[i] >> 14) & 0xf; 684 key_length = (buf[i] >> 18) & 0x3; 685 exp_length = (buf[i] >> 20) & 0xfff; 686 printf("Key[%d]:\n", i); 687 printf("Key Type: "); 688 switch (key_type) { 689 case 0: 690 printf("AES-256 as OEM platform key for image encryption/decryption\n"); 691 break; 692 case 1: 693 printf("AES-256 as secret vault key\n"); 694 break; 695 case 4: 696 printf("HMAC as encrypted OEM HMAC keys in Mode 1\n"); 697 break; 698 case 8: 699 printf("RSA-public as OEM DSS public keys in Mode 2\n"); 700 break; 701 case 9: 702 printf("RSA-public as SOC public key\n"); 703 break; 704 case 10: 705 printf("RSA-public as AES key decryption key\n"); 706 break; 707 case 13: 708 printf("RSA-private as SOC private key\n"); 709 break; 710 case 14: 711 printf("RSA-private as AES key decryption key\n"); 712 break; 713 default: 714 printf("key_type error: %x\n", key_type); 715 return -1; 716 } 717 if (key_type == 4) { 718 printf("HMAC SHA Type: "); 719 switch (key_length) { 720 case 0: 721 printf("HMAC(SHA224)\n"); 722 break; 723 case 1: 724 printf("HMAC(SHA256)\n"); 725 break; 726 case 2: 727 printf("HMAC(SHA384)\n"); 728 break; 729 case 3: 730 printf("HMAC(SHA512)\n"); 731 break; 732 } 733 } else if (key_type != 0 && key_type != 1) { 734 printf("RSA SHA Type: "); 735 switch (key_length) { 736 case 0: 737 printf("RSA1024\n"); 738 len = 0x100; 739 break; 740 case 1: 741 printf("RSA2048\n"); 742 len = 0x200; 743 break; 744 case 2: 745 printf("RSA3072\n"); 746 len = 0x300; 747 break; 748 case 3: 749 printf("RSA4096\n"); 750 len = 0x400; 751 break; 752 } 753 printf("RSA exponent bit length: %d\n", exp_length); 754 } 755 if (key_type == 4 || key_type == 8) 756 printf("Key Number ID: %d\n", key_id); 757 printf("Key Value:\n"); 758 if (key_type == 4) { 759 buf_print(&byte_buf[key_offset], 0x40); 760 } else if (key_type == 0 || key_type == 1) { 761 printf("AES Key:\n"); 762 buf_print(&byte_buf[key_offset], 0x20); 763 printf("AES IV:\n"); 764 buf_print(&byte_buf[key_offset + 0x20], 0x10); 765 766 } else { 767 printf("RSA mod:\n"); 768 buf_print(&byte_buf[key_offset], len / 2); 769 printf("RSA exp:\n"); 770 buf_print(&byte_buf[key_offset + (len / 2)], len / 2); 771 } 772 if (last) 773 break; 774 i++; 775 } 776 return 0; 777 } 778 779 static int otp_prog_conf(uint32_t *buf) 780 { 781 int i, k; 782 int pass = 0; 783 int soak = 0; 784 uint32_t prog_address; 785 uint32_t data[12]; 786 uint32_t compare[2]; 787 uint32_t *buf_keep = &buf[12]; 788 uint32_t data_masked; 789 uint32_t buf_masked; 790 791 printf("Read OTP Config Region:\n"); 792 793 printProgress(0, 12, ""); 794 for (i = 0; i < 12 ; i ++) { 795 printProgress(i + 1, 12, ""); 796 prog_address = 0x800; 797 prog_address |= (i / 8) * 0x200; 798 prog_address |= (i % 8) * 0x2; 799 otp_read_data(prog_address, &data[i]); 800 } 801 802 printf("Check writable...\n"); 803 for (i = 0; i < 12; i++) { 804 data_masked = data[i] & ~buf_keep[i]; 805 buf_masked = buf[i] & ~buf_keep[i]; 806 if (data_masked == buf_masked) 807 continue; 808 if ((data_masked | buf_masked) == buf_masked) { 809 continue; 810 } else { 811 printf("Input image can't program into OTP, please check.\n"); 812 printf("OTPCFG[%d] = %x\n", i, data[i]); 813 printf("Input [%d] = %x\n", i, buf[i]); 814 printf("Mask [%x] = %x\n", i, ~buf_keep[i]); 815 return -1; 816 } 817 } 818 819 printf("Start Programing...\n"); 820 printProgress(0, 12, ""); 821 otp_soak(0); 822 for (i = 0; i < 12; i++) { 823 data_masked = data[i] & ~buf_keep[i]; 824 buf_masked = buf[i] & ~buf_keep[i]; 825 prog_address = 0x800; 826 prog_address |= (i / 8) * 0x200; 827 prog_address |= (i % 8) * 0x2; 828 if (data_masked == buf_masked) { 829 printProgress(i + 1, 12, "[%03X]=%08X HIT", prog_address, buf[i]); 830 continue; 831 } 832 if (soak) { 833 soak = 0; 834 otp_soak(0); 835 } 836 printProgress(i + 1, 12, "[%03X]=%08X ", prog_address, buf[i]); 837 838 otp_prog_dw(buf[i], buf_keep[i], prog_address); 839 840 pass = 0; 841 for (k = 0; k < RETRY; k++) { 842 if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 1) != 0) { 843 if (soak == 0) { 844 soak = 1; 845 otp_soak(1); 846 } 847 otp_prog_dw(compare[0], prog_address, 1); 848 } else { 849 pass = 1; 850 break; 851 } 852 } 853 } 854 855 if (!pass) 856 return -1; 857 858 return 0; 859 860 } 861 862 static void otp_strp_status(struct otpstrap *otpstrap) 863 { 864 uint32_t OTPSTRAP_RAW[2]; 865 int i, j; 866 867 for (j = 0; j < 64; j++) { 868 otpstrap[j].value = 0; 869 otpstrap[j].remain_times = 7; 870 otpstrap[j].writeable_option = -1; 871 otpstrap[j].protected = 0; 872 } 873 874 for (i = 16; i < 30; i += 2) { 875 int option = (i - 16) / 2; 876 otp_read_config(i, &OTPSTRAP_RAW[0]); 877 otp_read_config(i + 1, &OTPSTRAP_RAW[1]); 878 for (j = 0; j < 32; j++) { 879 char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1); 880 if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) { 881 otpstrap[j].writeable_option = option; 882 } 883 if (bit_value == 1) 884 otpstrap[j].remain_times --; 885 otpstrap[j].value ^= bit_value; 886 otpstrap[j].option_array[option] = bit_value; 887 } 888 for (j = 32; j < 64; j++) { 889 char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1); 890 if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) { 891 otpstrap[j].writeable_option = option; 892 } 893 if (bit_value == 1) 894 otpstrap[j].remain_times --; 895 otpstrap[j].value ^= bit_value; 896 otpstrap[j].option_array[option] = bit_value; 897 } 898 } 899 otp_read_config(30, &OTPSTRAP_RAW[0]); 900 otp_read_config(31, &OTPSTRAP_RAW[1]); 901 for (j = 0; j < 32; j++) { 902 if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1) 903 otpstrap[j].protected = 1; 904 } 905 for (j = 32; j < 64; j++) { 906 if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1) 907 otpstrap[j].protected = 1; 908 } 909 } 910 911 static int otp_strap_parse(uint32_t *buf) 912 { 913 int i; 914 uint32_t *strap_keep = buf + 2; 915 uint32_t *strap_protect = buf + 4; 916 int bit, pbit, kbit; 917 int fail = 0; 918 struct otpstrap otpstrap[64]; 919 920 otp_strp_status(otpstrap); 921 for (i = 0; i < 64; i++) { 922 if (i < 32) { 923 bit = (buf[0] >> i) & 0x1; 924 kbit = (strap_keep[0] >> i) & 0x1; 925 pbit = (strap_protect[0] >> i) & 0x1; 926 } else { 927 bit = (buf[1] >> (i - 32)) & 0x1; 928 kbit = (strap_keep[1] >> (i - 32)) & 0x1; 929 pbit = (strap_protect[1] >> (i - 32)) & 0x1; 930 } 931 932 if (kbit == 1) { 933 continue; 934 } else { 935 printf("OTPSTRAP[%d]:\n", i); 936 } 937 if (bit == otpstrap[i].value) { 938 printf(" The value is same as before, skip it.\n"); 939 continue; 940 } 941 if (otpstrap[i].protected == 1) { 942 printf(" This bit is protected and is not writable\n"); 943 fail = 1; 944 continue; 945 } 946 if (otpstrap[i].remain_times == 0) { 947 printf(" This bit is no remaining number of times to write.\n"); 948 fail = 1; 949 continue; 950 } 951 if (pbit == 1) { 952 printf(" This bit will be protected and become non-writable.\n"); 953 } 954 printf(" Write 1 to OTPSTRAP[%d] OPTION[%d], that value becomes from %d to %d.\n", i, otpstrap[i].writeable_option + 1, otpstrap[i].value, otpstrap[i].value ^ 1); 955 } 956 if (fail == 1) 957 return -1; 958 else 959 return 0; 960 } 961 962 static void otp_print_strap(int start, int count) 963 { 964 int i, j; 965 struct otpstrap otpstrap[64]; 966 967 otp_strp_status(otpstrap); 968 969 for (i = start; i < start + count; i++) { 970 printf("OTPSTRAP[%d]:\n", i); 971 printf(" OTP Option value: "); 972 for (j = 1; j <= 7; j++) 973 printf("[%d]:%d ", j, otpstrap[i].option_array[j - 1]); 974 printf("\n"); 975 printf(" OTP Value: %d\n", otpstrap[i].value); 976 printf(" Status:\n"); 977 if (otpstrap[i].protected == 1) { 978 printf(" OTPSTRAP[%d] is protected and is not writable\n", i); 979 } else { 980 printf(" OTPSTRAP[%d] is not protected ", i); 981 if (otpstrap[i].remain_times == 0) { 982 printf("and no remaining number of times to write.\n"); 983 } else { 984 printf("and still can write %d number of times\n", otpstrap[i].remain_times); 985 } 986 } 987 } 988 } 989 990 static int otp_prog_strap(uint32_t *buf) 991 { 992 int i, j; 993 uint32_t *strap_keep = buf + 2; 994 uint32_t *strap_protect = buf + 4; 995 uint32_t prog_bit, prog_address; 996 int bit, pbit, kbit, offset; 997 int fail = 0; 998 int pass, soak; 999 struct otpstrap otpstrap[64]; 1000 1001 otp_strp_status(otpstrap); 1002 1003 otp_write(0x3000, 0x4061); // Write MRA 1004 otp_write(0x5000, 0x302f); // Write MRB 1005 otp_write(0x1000, 0x4020); // Write MR 1006 for (i = 0; i < 64; i++) { 1007 printProgress(i + 1, 64, ""); 1008 prog_address = 0x800; 1009 if (i < 32) { 1010 offset = i; 1011 bit = (buf[0] >> offset) & 0x1; 1012 kbit = (strap_keep[0] >> offset) & 0x1; 1013 pbit = (strap_protect[0] >> offset) & 0x1; 1014 prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200; 1015 prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2; 1016 1017 } else { 1018 offset = (i - 32); 1019 bit = (buf[1] >> offset) & 0x1; 1020 kbit = (strap_keep[1] >> offset) & 0x1; 1021 pbit = (strap_protect[1] >> offset) & 0x1; 1022 prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200; 1023 prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2; 1024 } 1025 prog_bit = ~(0x1 << offset); 1026 1027 if (kbit == 1) { 1028 continue; 1029 } 1030 if (bit == otpstrap[i].value) { 1031 continue; 1032 } 1033 if (otpstrap[i].protected == 1) { 1034 fail = 1; 1035 continue; 1036 } 1037 if (otpstrap[i].remain_times == 0) { 1038 fail = 1; 1039 continue; 1040 } 1041 pass = 0; 1042 soak = 0; 1043 otp_write(0x3000, 0x4061); // Write MRA 1044 otp_write(0x5000, 0x302f); // Write MRB 1045 otp_write(0x1000, 0x4020); // Write MR 1046 writel(0x04190760, 0x1e602008); //normal program 1047 for (j = 0; j < RETRY; j++) { 1048 if (!soak) { 1049 otp_prog(prog_address, prog_bit); 1050 if (verify_bit(prog_address, offset, 1) == 0) { 1051 pass = 1; 1052 break; 1053 } 1054 soak = 1; 1055 otp_write(0x3000, 0x4021); // Write MRA 1056 otp_write(0x5000, 0x1027); // Write MRB 1057 otp_write(0x1000, 0x4820); // Write MR 1058 writel(0x041930d4, 0x1e602008); //soak program 1059 } 1060 otp_prog(prog_address, prog_bit); 1061 if (verify_bit(prog_address, offset, 1) == 0) { 1062 pass = 1; 1063 break; 1064 } 1065 } 1066 if (!pass) 1067 return -1; 1068 1069 if (pbit == 0) 1070 continue; 1071 prog_address = 0x800; 1072 if (i < 32) 1073 prog_address |= 0x60c; 1074 else 1075 prog_address |= 0x60e; 1076 1077 for (j = 0; j < RETRY; j++) { 1078 if (!soak) { 1079 writel(0x04190760, 0x1e602008); //normal program 1080 otp_prog(prog_address, prog_bit); 1081 if (verify_bit(prog_address, offset, 1) == 0) { 1082 pass = 1; 1083 break; 1084 } 1085 soak = 1; 1086 } 1087 writel(0x041930d4, 0x1e602008); //soak program 1088 otp_prog(prog_address, prog_bit); 1089 if (verify_bit(prog_address, offset, 1) == 0) { 1090 pass = 1; 1091 break; 1092 } 1093 } 1094 if (!pass) 1095 return -1; 1096 1097 } 1098 if (fail == 1) 1099 return -1; 1100 else 1101 return 0; 1102 1103 } 1104 1105 static void otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset, int soak) 1106 { 1107 int prog_bit; 1108 1109 if (soak) { 1110 otp_write(0x3000, 0x4021); // Write MRA 1111 otp_write(0x5000, 0x1027); // Write MRB 1112 otp_write(0x1000, 0x4820); // Write MR 1113 writel(0x041930d4, 0x1e602008); //soak program 1114 } else { 1115 otp_write(0x3000, 0x4061); // Write MRA 1116 otp_write(0x5000, 0x302f); // Write MRB 1117 otp_write(0x1000, 0x4020); // Write MR 1118 writel(0x04190760, 0x1e602008); //normal program 1119 } 1120 if (prog_address % 2 == 0) { 1121 if (value) 1122 prog_bit = ~(0x1 << bit_offset); 1123 else 1124 return; 1125 } else { 1126 prog_address |= 1 << 15; 1127 if (!value) 1128 prog_bit = 0x1 << bit_offset; 1129 else 1130 return; 1131 } 1132 otp_prog(prog_address, prog_bit); 1133 } 1134 1135 static int otp_prog_data(uint32_t *buf) 1136 { 1137 int i, k; 1138 int pass; 1139 int soak = 0; 1140 uint32_t prog_address; 1141 uint32_t data[2048]; 1142 uint32_t compare[2]; 1143 uint32_t *buf_keep = &buf[2048]; 1144 1145 uint32_t data0_masked; 1146 uint32_t data1_masked; 1147 uint32_t buf0_masked; 1148 uint32_t buf1_masked; 1149 1150 printf("Read OTP Data:\n"); 1151 1152 printProgress(0, 2048, ""); 1153 for (i = 0; i < 2048 ; i += 2) { 1154 printProgress(i + 2, 2048, ""); 1155 otp_read_data(i, &data[i]); 1156 } 1157 1158 1159 printf("Check writable...\n"); 1160 for (i = 0; i < 2048; i++) { 1161 data0_masked = data[i] & ~buf_keep[i]; 1162 buf0_masked = buf[i] & ~buf_keep[i]; 1163 if (data0_masked == buf0_masked) 1164 continue; 1165 if (i % 2 == 0) { 1166 if ((data0_masked | buf0_masked) == buf0_masked) { 1167 continue; 1168 } else { 1169 printf("Input image can't program into OTP, please check.\n"); 1170 printf("OTP_ADDR[%x] = %x\n", i, data[i]); 1171 printf("Input [%x] = %x\n", i, buf[i]); 1172 printf("Mask [%x] = %x\n", i, ~buf_keep[i]); 1173 return -1; 1174 } 1175 } else { 1176 if ((data0_masked & buf0_masked) == buf0_masked) { 1177 continue; 1178 } else { 1179 printf("Input image can't program into OTP, please check.\n"); 1180 printf("OTP_ADDR[%x] = %x\n", i, data[i]); 1181 printf("Input [%x] = %x\n", i, buf[i]); 1182 printf("Mask [%x] = %x\n", i, ~buf_keep[i]); 1183 return -1; 1184 } 1185 } 1186 } 1187 1188 printf("Start Programing...\n"); 1189 printProgress(0, 2048, ""); 1190 1191 for (i = 0; i < 2048; i += 2) { 1192 prog_address = i; 1193 data0_masked = data[i] & ~buf_keep[i]; 1194 buf0_masked = buf[i] & ~buf_keep[i]; 1195 data1_masked = data[i + 1] & ~buf_keep[i + 1]; 1196 buf1_masked = buf[i + 1] & ~buf_keep[i + 1]; 1197 if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked)) { 1198 printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]); 1199 continue; 1200 } 1201 if (soak) { 1202 soak = 0; 1203 otp_soak(0); 1204 } 1205 if (data1_masked == buf1_masked) { 1206 printProgress(i + 2, 2048, "[%03X]=%08X ;[%03X]=%08X HIT", prog_address, buf[i], prog_address + 1, buf[i + 1]); 1207 otp_prog_dw(buf[i], buf_keep[i], prog_address); 1208 } else if (data0_masked == buf0_masked) { 1209 printProgress(i + 2, 2048, "[%03X]=%08X HIT;[%03X]=%08X ", prog_address, buf[i], prog_address + 1, buf[i + 1]); 1210 otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1); 1211 } else { 1212 printProgress(i + 2, 2048, "[%03X]=%08X ;[%03X]=%08X ", prog_address, buf[i], prog_address + 1, buf[i + 1]); 1213 otp_prog_dw(buf[i], buf_keep[i], prog_address); 1214 otp_prog_dw(buf[i + 1], buf_keep[i + 1], prog_address + 1); 1215 } 1216 1217 pass = 0; 1218 for (k = 0; k < RETRY; k++) { 1219 if (verify_dw(prog_address, &buf[i], &buf_keep[i], compare, 2) != 0) { 1220 if (soak == 0) { 1221 soak = 1; 1222 otp_soak(1); 1223 } 1224 if (compare[0] != 0) { 1225 otp_prog_dw(compare[0], buf_keep[i], prog_address); 1226 } 1227 if (compare[1] != ~0) { 1228 otp_prog_dw(compare[1], buf_keep[i], prog_address + 1); 1229 } 1230 } else { 1231 pass = 1; 1232 break; 1233 } 1234 } 1235 1236 if (!pass) 1237 return -1; 1238 } 1239 return 0; 1240 1241 } 1242 1243 static int do_otp_prog(int addr, int byte_size, int nconfirm) 1244 { 1245 int ret; 1246 int mode; 1247 uint32_t *buf; 1248 uint32_t *data_region = NULL; 1249 uint32_t *conf_region = NULL; 1250 uint32_t *strap_region = NULL; 1251 1252 buf = map_physmem(addr, byte_size, MAP_WRBACK); 1253 if (!buf) { 1254 puts("Failed to map physical memory\n"); 1255 return 1; 1256 } 1257 1258 if (((buf[0] >> 29) & 0x7) == 0x7) { 1259 mode = OTP_REGION_ALL; 1260 conf_region = &buf[1]; 1261 strap_region = &buf[25]; 1262 data_region = &buf[31]; 1263 } else { 1264 if (buf[0] & BIT(29)) { 1265 mode = OTP_REGION_DATA; 1266 data_region = &buf[31]; 1267 } 1268 if (buf[0] & BIT(30)) { 1269 mode = OTP_REGION_CONF; 1270 strap_region = &buf[25]; 1271 } 1272 if (buf[0] & BIT(31)) { 1273 mode = OTP_REGION_STRAP; 1274 conf_region = &buf[1]; 1275 } 1276 } 1277 1278 if (!nconfirm) { 1279 if (mode == OTP_REGION_CONF) { 1280 if (otp_conf_parse(conf_region) < 0) { 1281 printf("OTP config error, please check.\n"); 1282 return -1; 1283 } 1284 } else if (mode == OTP_REGION_DATA) { 1285 if (otp_data_parse(data_region) < 0) { 1286 printf("OTP data error, please check.\n"); 1287 return -1; 1288 } 1289 } else if (mode == OTP_REGION_STRAP) { 1290 if (otp_strap_parse(strap_region) < 0) { 1291 printf("OTP strap error, please check.\n"); 1292 return -1; 1293 } 1294 } else if (mode == OTP_REGION_ALL) { 1295 if (otp_conf_parse(conf_region) < 0) { 1296 printf("OTP config error, please check.\n"); 1297 return -1; 1298 } 1299 if (otp_strap_parse(strap_region) < 0) { 1300 printf("OTP strap error, please check.\n"); 1301 return -1; 1302 } 1303 if (otp_data_parse(data_region) < 0) { 1304 printf("OTP data error, please check.\n"); 1305 return -1; 1306 } 1307 } 1308 printf("type \"YES\" (no quotes) to continue:\n"); 1309 if (!confirm_yesno()) { 1310 printf(" Aborting\n"); 1311 return 1; 1312 } 1313 } 1314 if (mode == OTP_REGION_CONF) { 1315 return otp_prog_conf(conf_region); 1316 } else if (mode == OTP_REGION_STRAP) { 1317 return otp_prog_strap(strap_region); 1318 } else if (mode == OTP_REGION_DATA) { 1319 return otp_prog_data(data_region); 1320 } else if (mode == OTP_REGION_ALL) { 1321 printf("programing data region ... "); 1322 ret = otp_prog_data(data_region); 1323 if (ret < 0) { 1324 printf("Error\n"); 1325 return ret; 1326 } else { 1327 printf("Done\n"); 1328 } 1329 printf("programing strap region ... "); 1330 ret = otp_prog_strap(strap_region); 1331 if (ret < 0) { 1332 printf("Error\n"); 1333 return ret; 1334 } else { 1335 printf("Done\n"); 1336 } 1337 printf("programing configuration region ... "); 1338 ret = otp_prog_conf(conf_region); 1339 if (ret < 0) { 1340 printf("Error\n"); 1341 return ret; 1342 } 1343 printf("Done\n"); 1344 return ret; 1345 } 1346 return 0; 1347 } 1348 1349 static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int protect, int nconfirm) 1350 { 1351 uint32_t ret[2]; 1352 uint32_t strap_buf[6]; 1353 uint32_t prog_address = 0; 1354 struct otpstrap otpstrap[64]; 1355 int otp_bit; 1356 int i; 1357 int pass; 1358 1359 switch (mode) { 1360 case OTP_REGION_CONF: 1361 otp_read_config(otp_dw_offset, ret); 1362 prog_address = 0x800; 1363 prog_address |= (otp_dw_offset / 8) * 0x200; 1364 prog_address |= (otp_dw_offset % 8) * 0x2; 1365 otp_bit = (ret[0] >> bit_offset) & 0x1; 1366 if (otp_bit == value) { 1367 printf("OTPCFG%X[%d] = %d\n", otp_dw_offset, bit_offset, value); 1368 printf("No need to program\n"); 1369 return 0; 1370 } 1371 if (otp_bit == 1 && value == 0) { 1372 printf("OTPCFG%X[%d] = 1\n", otp_dw_offset, bit_offset); 1373 printf("OTP is programed, which can't be clean\n"); 1374 return -1; 1375 } 1376 printf("Program OTPCFG%X[%d] to 1\n", otp_dw_offset, bit_offset); 1377 break; 1378 case OTP_REGION_DATA: 1379 prog_address = otp_dw_offset; 1380 1381 if (otp_dw_offset % 2 == 0) { 1382 otp_read_data(otp_dw_offset, ret); 1383 otp_bit = (ret[0] >> bit_offset) & 0x1; 1384 } else { 1385 otp_read_data(otp_dw_offset - 1, ret); 1386 otp_bit = (ret[1] >> bit_offset) & 0x1; 1387 } 1388 if (otp_bit == value) { 1389 printf("OTPDATA%X[%d] = %d\n", otp_dw_offset, bit_offset, value); 1390 printf("No need to program\n"); 1391 return 0; 1392 } 1393 if (otp_bit == 1 && value == 0) { 1394 printf("OTPDATA%X[%d] = 1\n", otp_dw_offset, bit_offset); 1395 printf("OTP is programed, which can't be clean\n"); 1396 return -1; 1397 } 1398 printf("Program OTPDATA%X[%d] to 1\n", otp_dw_offset, bit_offset); 1399 break; 1400 case OTP_REGION_STRAP: 1401 otp_strp_status(otpstrap); 1402 otp_print_strap(bit_offset, 1); 1403 if (bit_offset < 32) { 1404 strap_buf[0] = value << bit_offset; 1405 strap_buf[2] = ~BIT(bit_offset); 1406 strap_buf[3] = ~0; 1407 strap_buf[5] = 0; 1408 if (protect) 1409 strap_buf[4] = BIT(bit_offset); 1410 else 1411 strap_buf[4] = 0; 1412 } else { 1413 strap_buf[1] = value << (bit_offset - 32); 1414 strap_buf[2] = ~0; 1415 strap_buf[3] = ~BIT(bit_offset - 32); 1416 strap_buf[4] = 0; 1417 if (protect) 1418 strap_buf[5] = BIT(bit_offset - 32); 1419 else 1420 strap_buf[5] = 0; 1421 } 1422 if (otp_strap_parse(strap_buf) < 0) 1423 return -1; 1424 break; 1425 } 1426 1427 if (!nconfirm) { 1428 printf("type \"YES\" (no quotes) to continue:\n"); 1429 if (!confirm_yesno()) { 1430 printf(" Aborting\n"); 1431 return 1; 1432 } 1433 } 1434 1435 switch (mode) { 1436 case OTP_REGION_STRAP: 1437 return otp_prog_strap(strap_buf); 1438 case OTP_REGION_CONF: 1439 case OTP_REGION_DATA: 1440 otp_prog_bit(value, prog_address, bit_offset, 0); 1441 pass = -1; 1442 for (i = 0; i < RETRY; i++) { 1443 if (verify_bit(prog_address, bit_offset, value) != 0) { 1444 otp_prog_bit(value, prog_address, bit_offset, 1); 1445 } else { 1446 pass = 0; 1447 break; 1448 } 1449 } 1450 return pass; 1451 } 1452 1453 return -1; 1454 } 1455 1456 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, 1457 char *const argv[]) 1458 { 1459 char *cmd; 1460 int mode = 0; 1461 int nconfirm = 0; 1462 uint32_t addr = 0; 1463 uint32_t otp_addr = 0; 1464 int dw_count = 0; 1465 int byte_size = 0; 1466 int bit_offset = 0; 1467 int value = 0; 1468 int protect = 0; 1469 1470 1471 1472 if (argc < 2) { 1473 usage: 1474 return CMD_RET_USAGE; 1475 } 1476 1477 cmd = argv[1]; 1478 if (!strcmp(cmd, "read")) { 1479 if (!strcmp(argv[2], "conf")) 1480 mode = OTP_REGION_CONF; 1481 else if (!strcmp(argv[2], "data")) 1482 mode = OTP_REGION_DATA; 1483 else if (!strcmp(argv[2], "strap")) 1484 mode = OTP_REGION_STRAP; 1485 else 1486 goto usage; 1487 1488 writel(OTP_PASSWD, 0x1e6f2000); //password 1489 otp_addr = simple_strtoul(argv[3], NULL, 16); 1490 dw_count = simple_strtoul(argv[4], NULL, 16); 1491 if (mode == OTP_REGION_CONF) { 1492 otp_print_config(otp_addr, dw_count); 1493 } else if (mode == OTP_REGION_DATA) { 1494 otp_print_data(otp_addr, dw_count); 1495 } else if (mode == OTP_REGION_STRAP) { 1496 otp_print_strap(0, 64); 1497 } 1498 } else if (!strcmp(cmd, "prog")) { 1499 // if (!strcmp(argv[2], "conf")) 1500 // mode = OTP_REGION_CONF; 1501 // else if (!strcmp(argv[2], "strap")) 1502 // mode = OTP_REGION_STRAP; 1503 // else if (!strcmp(argv[2], "data")) 1504 // mode = OTP_REGION_DATA; 1505 // else if (!strcmp(argv[2], "all")) 1506 // mode = OTP_REGION_ALL; 1507 // else 1508 // goto usage; 1509 1510 if (!strcmp(argv[2], "f")) 1511 nconfirm = 1; 1512 writel(OTP_PASSWD, 0x1e6f2000); //password 1513 addr = simple_strtoul(argv[2 + nconfirm], NULL, 16); 1514 byte_size = simple_strtoul(argv[3 + nconfirm], NULL, 16); 1515 return do_otp_prog(addr, byte_size, nconfirm); 1516 } else if (!strcmp(cmd, "pb")) { 1517 if (!strcmp(argv[2], "conf")) 1518 mode = OTP_REGION_CONF; 1519 else if (!strcmp(argv[2], "strap")) 1520 mode = OTP_REGION_STRAP; 1521 else if (!strcmp(argv[2], "data")) 1522 mode = OTP_REGION_DATA; 1523 else 1524 goto usage; 1525 if (!strcmp(argv[3], "f")) 1526 nconfirm = 1; 1527 1528 if (mode == OTP_REGION_STRAP) { 1529 bit_offset = simple_strtoul(argv[3 + nconfirm], NULL, 16); 1530 value = simple_strtoul(argv[4 + nconfirm], NULL, 16); 1531 protect = simple_strtoul(argv[5 + nconfirm], NULL, 16); 1532 if (bit_offset >= 64) 1533 return -1; 1534 } else { 1535 otp_addr = simple_strtoul(argv[3 + nconfirm], NULL, 16); 1536 bit_offset = simple_strtoul(argv[4 + nconfirm], NULL, 16); 1537 value = simple_strtoul(argv[5 + nconfirm], NULL, 16); 1538 if (bit_offset >= 32) 1539 return -1; 1540 } 1541 if (value != 0 && value != 1) 1542 return -1; 1543 1544 writel(OTP_PASSWD, 0x1e6f2000); //password 1545 return do_otp_prog_bit(mode, otp_addr, bit_offset, value, protect, nconfirm); 1546 } else if (!strcmp(cmd, "comp")) { 1547 writel(OTP_PASSWD, 0x1e6f2000); //password 1548 addr = simple_strtoul(argv[2], NULL, 16); 1549 otp_addr = simple_strtoul(argv[3], NULL, 16); 1550 if (otp_compare(otp_addr, addr) >= 0) { 1551 printf("Compare pass\n"); 1552 } else { 1553 printf("Compare fail\n"); 1554 } 1555 } else { 1556 goto usage; 1557 } 1558 1559 1560 return 0; 1561 } 1562 1563 1564 U_BOOT_CMD( 1565 otp, 7, 0, do_ast_otp, 1566 "ASPEED One-Time-Programmable sub-system", 1567 "read conf|strap|data <otp_dw_offset> <dw_count>\n" 1568 "otp prog [f] <addr> <byte_size>\n" 1569 "otp pb conf|data [f] <otp_dw_offset> <bit_offset> <value>\n" 1570 "otp pb strap [f] <bit_offset> <value> <protect>\n" 1571 "otp comp <addr> <otp_dw_offset>\n" 1572 ); 1573