otp.c (2e192b245ed36a63bab0ef576999a95e23f60ecd) | otp.c (69d5fd8f8e263a5a3e1f26fc51078298f42a5525) |
---|---|
1/* | 1/* |
2 * cmd_otp.c - interface to Blackfin on-chip One-Time-Programmable memory | 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. |
3 * | 6 * |
4 * Copyright (c) 2007-2008 Analog Devices Inc. 5 * 6 * Licensed under the GPL-2 or later. | 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 |
7 */ 8 | 10 */ 11 |
9/* There are 512 128-bit "pages" (0x000 through 0x1FF). 10 * The pages are accessable as 64-bit "halfpages" (an upper and lower half). 11 * The pages are not part of the memory map. There is an OTP controller which 12 * handles scanning in/out of bits. While access is done through OTP MMRs, 13 * the bootrom provides C-callable helper functions to handle the interaction. 14 */ 15 16#include <config.h> | |
17#include <common.h> | 12#include <common.h> |
13#include <console.h> 14#include <bootretry.h> 15#include <cli.h> |
|
18#include <command.h> 19#include <console.h> 20 | 16#include <command.h> 17#include <console.h> 18 |
21#include <asm/blackfin.h> 22#include <asm/clock.h> 23#include <asm/mach-common/bits/otp.h> | 19#include <inttypes.h> 20#include <mapmem.h> 21#include <asm/io.h> 22#include <linux/compiler.h> |
24 | 23 |
25static const char *otp_strerror(uint32_t err) | 24DECLARE_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 33struct otpstrap { 34 int value; 35 int option_array[7]; 36 int remain_times; 37 int writeable_option; 38 int protected; 39}; 40 41static int otp_read_data(uint32_t offset, uint32_t *data) |
26{ | 42{ |
27 switch (err) { 28 case 0: return "no error"; 29 case OTP_WRITE_ERROR: return "OTP fuse write error"; 30 case OTP_READ_ERROR: return "OTP fuse read error"; 31 case OTP_ACC_VIO_ERROR: return "invalid OTP address"; 32 case OTP_DATA_MULT_ERROR: return "multiple bad bits detected"; 33 case OTP_ECC_MULT_ERROR: return "error in ECC bits"; 34 case OTP_PREV_WR_ERROR: return "space already written"; 35 case OTP_DATA_SB_WARN: return "single bad bit in half page"; 36 case OTP_ECC_SB_WARN: return "single bad bit in ECC"; 37 default: return "unknown error"; | 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 51static 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 67static 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]); |
38 } | 77 } |
78 printf("\n"); 79 return 1; |
|
39} 40 | 80} 81 |
41#define lowup(x) ((x) % 2 ? "upper" : "lower") | 82static int otp_print_data(uint32_t offset, int dw_count) 83{ 84 int i; 85 uint32_t ret[2]; |
42 | 86 |
43static int check_voltage(void) | 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 101static int otp_compare(uint32_t otp_addr, uint32_t addr) |
44{ | 102{ |
45 /* Make sure voltage limits are within datasheet spec */ 46 uint16_t vr_ctl = bfin_read_VR_CTL(); | 103 uint32_t ret; 104 uint32_t *buf; |
47 | 105 |
48#ifdef __ADSPBF54x__ 49 /* 0.9V <= VDDINT <= 1.1V */ 50 if ((vr_ctl & 0xc) && (vr_ctl & 0xc0) == 0xc0) 51 return 1; 52#else 53 /* for the parts w/out qualification yet */ 54 (void)vr_ctl; 55#endif | 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} |
56 | 124 |
125static 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 133static 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 141static 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 156static 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]); |
|
57 return 0; | 334 return 0; |
335 |
|
58} 59 | 336} 337 |
60static void set_otp_timing(bool write) | 338static void buf_print(char *buf, int len) |
61{ | 339{ |
62 static uint32_t timing; 63 if (!timing) { 64 uint32_t tp1, tp2, tp3; 65 /* OTP_TP1 = 1000 / sclk_period (in nanoseconds) 66 * OTP_TP1 = 1000 / (1 / get_sclk() * 10^9) 67 * OTP_TP1 = (1000 * get_sclk()) / 10^9 68 * OTP_TP1 = get_sclk() / 10^6 69 */ 70 tp1 = get_sclk() / 1000000; 71 /* OTP_TP2 = 400 / (2 * sclk_period) 72 * OTP_TP2 = 400 / (2 * 1 / get_sclk() * 10^9) 73 * OTP_TP2 = (400 * get_sclk()) / (2 * 10^9) 74 * OTP_TP2 = (2 * get_sclk()) / 10^7 75 */ 76 tp2 = (2 * get_sclk() / 10000000) << 8; 77 /* OTP_TP3 = magic constant */ 78 tp3 = (0x1401) << 15; 79 timing = tp1 | tp2 | tp3; | 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 } |
80 } | 350 } |
351} |
|
81 | 352 |
82 bfrom_OtpCommand(OTP_INIT, write ? timing : timing & ~(-1 << 15)); | 353static 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; |
83} 84 | 457} 458 |
85int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | 459static int otp_prog_conf(uint32_t *buf, int otp_addr, int dw_count) |
86{ | 460{ |
87 char *cmd; 88 uint32_t ret, base_flags; 89 bool prompt_user, force_read; 90 uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content); | 461 int i, j, k, bit_value; 462 int pass, soak; 463 uint32_t prog_bit, prog_address; |
91 | 464 |
92 if (argc < 4) { 93 usage: 94 return CMD_RET_USAGE; | 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 } |
95 } | 500 } |
501 return 0; 502} |
|
96 | 503 |
97 prompt_user = false; 98 base_flags = 0; 99 cmd = argv[1]; 100 if (!strcmp(cmd, "read")) 101 otp_func = bfrom_OtpRead; 102 else if (!strcmp(cmd, "dump")) { 103 otp_func = bfrom_OtpRead; 104 force_read = true; 105 } else if (!strcmp(cmd, "write")) { 106 otp_func = bfrom_OtpWrite; 107 base_flags = OTP_CHECK_FOR_PREV_WRITE; 108 if (!strcmp(argv[2], "--force")) { 109 argv++; 110 --argc; 111 } else 112 prompt_user = false; 113 } else if (!strcmp(cmd, "lock")) { 114 if (argc != 4) 115 goto usage; 116 otp_func = bfrom_OtpWrite; 117 base_flags = OTP_LOCK; 118 } else 119 goto usage; | 504static void otp_strp_status(struct otpstrap *otpstrap) 505{ 506 uint32_t OTPSTRAP_RAW[2]; 507 int i, j; |
120 | 508 |
121 uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16); 122 uint32_t page = simple_strtoul(argv[3], NULL, 16); 123 uint32_t flags; 124 size_t i, count; 125 ulong half; | 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 } |
126 | 515 |
127 if (argc > 4) 128 count = simple_strtoul(argv[4], NULL, 16); | 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 553static 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; |
129 else | 600 else |
130 count = 2; | 601 return 0; 602} |
131 | 603 |
132 if (argc > 5) { 133 half = simple_strtoul(argv[5], NULL, 16); 134 if (half != 0 && half != 1) { 135 puts("Error: 'half' can only be '0' or '1'\n"); 136 goto usage; | 604static 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 } |
137 } | 628 } |
138 } else 139 half = 0; | 629 } 630} |
140 | 631 |
141 /* "otp lock" has slightly different semantics */ 142 if (base_flags & OTP_LOCK) { 143 count = page; 144 page = (uint32_t)addr; 145 addr = NULL; | 632static 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 |
146 } | 732 } |
733 if (fail == 1) 734 return -1; 735 else 736 return 0; |
|
147 | 737 |
148 /* do to the nature of OTP, make sure users are sure */ 149 if (prompt_user) { 150 printf( 151 "Writing one time programmable memory\n" 152 "Make sure your operating voltages and temperature are within spec\n" 153 " source address: 0x%p\n" 154 " OTP destination: %s page 0x%03X - %s page 0x%03lX\n" 155 " number to write: %lu halfpages\n" 156 " type \"YES\" (no quotes) to confirm: ", 157 addr, 158 lowup(half), page, 159 lowup(half + count - 1), page + (half + count - 1) / 2, 160 half + count 161 ); | 738} 739 740static 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 795static 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"); |
162 if (!confirm_yesno()) { 163 printf(" Aborting\n"); 164 return 1; 165 } 166 } | 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} 875static 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; |
|
167 | 882 |
168 printf("OTP memory %s: addr 0x%p page 0x%03X count %zu ... ", 169 cmd, addr, page, count); | |
170 | 883 |
171 set_otp_timing(otp_func == bfrom_OtpWrite); 172 if (otp_func == bfrom_OtpWrite && check_voltage()) { 173 puts("ERROR: VDDINT voltage is out of spec for writing\n"); 174 return -1; | 884 885 if (argc < 2) { 886usage: 887 return CMD_RET_USAGE; |
175 } 176 | 888 } 889 |
177 /* Do the actual reading/writing stuff */ 178 ret = 0; 179 for (i = half; i < count + half; ++i) { 180 flags = base_flags | (i % 2 ? OTP_UPPER_HALF : OTP_LOWER_HALF); 181 try_again: 182 ret = otp_func(page, flags, addr); 183 if (ret & OTP_MASTER_ERROR) { 184 if (force_read) { 185 if (flags & OTP_NO_ECC) 186 break; 187 else 188 flags |= OTP_NO_ECC; 189 puts("E"); 190 goto try_again; 191 } else 192 break; 193 } else if (ret) 194 puts("W"); | 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; |
195 else | 898 else |
196 puts("."); 197 if (!(base_flags & OTP_LOCK)) { 198 ++addr; 199 if (i % 2) 200 ++page; 201 } else 202 ++page; | 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; |
203 } | 941 } |
204 if (ret & 0x1) 205 printf("\nERROR at page 0x%03X (%s-halfpage): 0x%03X: %s\n", 206 page, lowup(i), ret, otp_strerror(ret)); 207 else 208 puts(" done\n"); | |
209 | 942 |
210 /* Make sure we disable writing */ 211 set_otp_timing(false); 212 bfrom_OtpCommand(OTP_CLOSE, 0); | |
213 | 943 |
214 return ret; | 944 return 0; |
215} 216 | 945} 946 |
947 |
|
217U_BOOT_CMD( | 948U_BOOT_CMD( |
218 otp, 7, 0, do_otp, 219 "One-Time-Programmable sub-system", 220 "read <addr> <page> [count] [half]\n" 221 " - read 'count' half-pages starting at 'page' (offset 'half') to 'addr'\n" 222 "otp dump <addr> <page> [count] [half]\n" 223 " - like 'otp read', but skip read errors\n" 224 "otp write [--force] <addr> <page> [count] [half]\n" 225 " - write 'count' half-pages starting at 'page' (offset 'half') from 'addr'\n" 226 "otp lock <page> <count>\n" 227 " - lock 'count' pages starting at 'page'" | 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>" |
228); | 954); |