1 /* 2 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor 3 * York Sun (yorksun@freescale.com) 4 * Haiying Wang (haiying.wang@freescale.com) 5 * Timur Tabi (timur@freescale.com) 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 10 #include <common.h> 11 #include <command.h> 12 #include <i2c.h> 13 #include <linux/ctype.h> 14 15 #ifdef CONFIG_SYS_I2C_EEPROM_CCID 16 #include "../common/eeprom.h" 17 #define MAX_NUM_PORTS 8 18 #endif 19 20 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 21 #define MAX_NUM_PORTS 23 22 #define NXID_VERSION 1 23 #endif 24 25 /** 26 * static eeprom: EEPROM layout for CCID or NXID formats 27 * 28 * See application note AN3638 for details. 29 */ 30 static struct __attribute__ ((__packed__)) eeprom { 31 #ifdef CONFIG_SYS_I2C_EEPROM_CCID 32 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ 33 u8 major; /* 0x04 Board revision, major */ 34 u8 minor; /* 0x05 Board revision, minor */ 35 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/ 36 u8 errata[2]; /* 0x10 - 0x11 Errata Level */ 37 u8 date[6]; /* 0x12 - 0x17 Build Date */ 38 u8 res_0[40]; /* 0x18 - 0x3f Reserved */ 39 u8 mac_count; /* 0x40 Number of MAC addresses */ 40 u8 mac_flag; /* 0x41 MAC table flags */ 41 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */ 42 u32 crc; /* 0x72 CRC32 checksum */ 43 #endif 44 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 45 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */ 46 u8 sn[12]; /* 0x04 - 0x0F Serial Number */ 47 u8 errata[5]; /* 0x10 - 0x14 Errata Level */ 48 u8 date[6]; /* 0x15 - 0x1a Build Date */ 49 u8 res_0; /* 0x1b Reserved */ 50 u32 version; /* 0x1c - 0x1f NXID Version */ 51 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */ 52 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */ 53 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */ 54 u8 res_1[21]; /* 0x2b - 0x3f Reserved */ 55 u8 mac_count; /* 0x40 Number of MAC addresses */ 56 u8 mac_flag; /* 0x41 MAC table flags */ 57 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */ 58 u32 crc; /* x+1 CRC32 checksum */ 59 #endif 60 } e; 61 62 /* Set to 1 if we've read EEPROM into memory */ 63 static int has_been_read = 0; 64 65 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 66 /* Is this a valid NXID EEPROM? */ 67 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \ 68 (e.id[2] == 'I') || (e.id[3] == 'D')) 69 #endif 70 71 #ifdef CONFIG_SYS_I2C_EEPROM_CCID 72 /* Is this a valid CCID EEPROM? */ 73 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \ 74 (e.id[2] == 'I') || (e.id[3] == 'D')) 75 #endif 76 77 /** 78 * show_eeprom - display the contents of the EEPROM 79 */ 80 static void show_eeprom(void) 81 { 82 int i; 83 unsigned int crc; 84 85 /* EEPROM tag ID, either CCID or NXID */ 86 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 87 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], 88 be32_to_cpu(e.version)); 89 #else 90 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); 91 #endif 92 93 /* Serial number */ 94 printf("SN: %s\n", e.sn); 95 96 /* Errata level. */ 97 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 98 printf("Errata: %s\n", e.errata); 99 #else 100 printf("Errata: %c%c\n", 101 e.errata[0] ? e.errata[0] : '.', 102 e.errata[1] ? e.errata[1] : '.'); 103 #endif 104 105 /* Build date, BCD date values, as YYMMDDhhmmss */ 106 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n", 107 e.date[0], e.date[1], e.date[2], 108 e.date[3] & 0x7F, e.date[4], e.date[5], 109 e.date[3] & 0x80 ? "PM" : ""); 110 111 /* Show MAC addresses */ 112 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) { 113 114 u8 *p = e.mac[i]; 115 116 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i, 117 p[0], p[1], p[2], p[3], p[4], p[5]); 118 } 119 120 crc = crc32(0, (void *)&e, sizeof(e) - 4); 121 122 if (crc == be32_to_cpu(e.crc)) 123 printf("CRC: %08x\n", be32_to_cpu(e.crc)); 124 else 125 printf("CRC: %08x (should be %08x)\n", 126 be32_to_cpu(e.crc), crc); 127 128 #ifdef DEBUG 129 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e)); 130 for (i = 0; i < sizeof(e); i++) { 131 if ((i % 16) == 0) 132 printf("%02X: ", i); 133 printf("%02X ", ((u8 *)&e)[i]); 134 if (((i % 16) == 15) || (i == sizeof(e) - 1)) 135 printf("\n"); 136 } 137 #endif 138 } 139 140 /** 141 * read_eeprom - read the EEPROM into memory 142 */ 143 static int read_eeprom(void) 144 { 145 int ret; 146 #ifdef CONFIG_SYS_EEPROM_BUS_NUM 147 unsigned int bus; 148 #endif 149 150 if (has_been_read) 151 return 0; 152 153 #ifdef CONFIG_SYS_EEPROM_BUS_NUM 154 bus = i2c_get_bus_num(); 155 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); 156 #endif 157 158 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, 159 (void *)&e, sizeof(e)); 160 161 #ifdef CONFIG_SYS_EEPROM_BUS_NUM 162 i2c_set_bus_num(bus); 163 #endif 164 165 #ifdef DEBUG 166 show_eeprom(); 167 #endif 168 169 has_been_read = (ret == 0) ? 1 : 0; 170 171 return ret; 172 } 173 174 /** 175 * update_crc - update the CRC 176 * 177 * This function should be called after each update to the EEPROM structure, 178 * to make sure the CRC is always correct. 179 */ 180 static void update_crc(void) 181 { 182 u32 crc; 183 184 crc = crc32(0, (void *)&e, sizeof(e) - 4); 185 e.crc = cpu_to_be32(crc); 186 } 187 188 /** 189 * prog_eeprom - write the EEPROM from memory 190 */ 191 static int prog_eeprom(void) 192 { 193 int ret = 0; 194 int i; 195 void *p; 196 #ifdef CONFIG_SYS_EEPROM_BUS_NUM 197 unsigned int bus; 198 #endif 199 200 /* Set the reserved values to 0xFF */ 201 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 202 e.res_0 = 0xFF; 203 memset(e.res_1, 0xFF, sizeof(e.res_1)); 204 #else 205 memset(e.res_0, 0xFF, sizeof(e.res_0)); 206 #endif 207 update_crc(); 208 209 #ifdef CONFIG_SYS_EEPROM_BUS_NUM 210 bus = i2c_get_bus_num(); 211 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); 212 #endif 213 214 /* 215 * The AT24C02 datasheet says that data can only be written in page 216 * mode, which means 8 bytes at a time, and it takes up to 5ms to 217 * complete a given write. 218 */ 219 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) { 220 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, 221 p, min((sizeof(e) - i), 8)); 222 if (ret) 223 break; 224 udelay(5000); /* 5ms write cycle timing */ 225 } 226 227 if (!ret) { 228 /* Verify the write by reading back the EEPROM and comparing */ 229 struct eeprom e2; 230 231 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 232 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2)); 233 if (!ret && memcmp(&e, &e2, sizeof(e))) 234 ret = -1; 235 } 236 237 #ifdef CONFIG_SYS_EEPROM_BUS_NUM 238 i2c_set_bus_num(bus); 239 #endif 240 241 if (ret) { 242 printf("Programming failed.\n"); 243 has_been_read = 0; 244 return -1; 245 } 246 247 printf("Programming passed.\n"); 248 return 0; 249 } 250 251 /** 252 * h2i - converts hex character into a number 253 * 254 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns 255 * the integer equivalent. 256 */ 257 static inline u8 h2i(char p) 258 { 259 if ((p >= '0') && (p <= '9')) 260 return p - '0'; 261 262 if ((p >= 'A') && (p <= 'F')) 263 return (p - 'A') + 10; 264 265 if ((p >= 'a') && (p <= 'f')) 266 return (p - 'a') + 10; 267 268 return 0; 269 } 270 271 /** 272 * set_date - stores the build date into the EEPROM 273 * 274 * This function takes a pointer to a string in the format "YYMMDDhhmmss" 275 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string, 276 * and stores it in the build date field of the EEPROM local copy. 277 */ 278 static void set_date(const char *string) 279 { 280 unsigned int i; 281 282 if (strlen(string) != 12) { 283 printf("Usage: mac date YYMMDDhhmmss\n"); 284 return; 285 } 286 287 for (i = 0; i < 6; i++) 288 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]); 289 290 update_crc(); 291 } 292 293 /** 294 * set_mac_address - stores a MAC address into the EEPROM 295 * 296 * This function takes a pointer to MAC address string 297 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and 298 * stores it in one of the MAC address fields of the EEPROM local copy. 299 */ 300 static void set_mac_address(unsigned int index, const char *string) 301 { 302 char *p = (char *) string; 303 unsigned int i; 304 305 if ((index >= MAX_NUM_PORTS) || !string) { 306 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n"); 307 return; 308 } 309 310 for (i = 0; *p && (i < 6); i++) { 311 e.mac[index][i] = simple_strtoul(p, &p, 16); 312 if (*p == ':') 313 p++; 314 } 315 316 update_crc(); 317 } 318 319 int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 320 { 321 char cmd; 322 323 if (argc == 1) { 324 show_eeprom(); 325 return 0; 326 } 327 328 cmd = argv[1][0]; 329 330 if (cmd == 'r') { 331 read_eeprom(); 332 return 0; 333 } 334 335 if (cmd == 'i') { 336 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 337 memcpy(e.id, "NXID", sizeof(e.id)); 338 e.version = NXID_VERSION; 339 #else 340 memcpy(e.id, "CCID", sizeof(e.id)); 341 #endif 342 update_crc(); 343 return 0; 344 } 345 346 if (!is_valid) { 347 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n"); 348 return 0; 349 } 350 351 if (argc == 2) { 352 switch (cmd) { 353 case 's': /* save */ 354 prog_eeprom(); 355 break; 356 default: 357 return cmd_usage(cmdtp); 358 } 359 360 return 0; 361 } 362 363 /* We know we have at least one parameter */ 364 365 switch (cmd) { 366 case 'n': /* serial number */ 367 memset(e.sn, 0, sizeof(e.sn)); 368 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); 369 update_crc(); 370 break; 371 case 'e': /* errata */ 372 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 373 memset(e.errata, 0, 5); 374 strncpy((char *)e.errata, argv[2], 4); 375 #else 376 e.errata[0] = argv[2][0]; 377 e.errata[1] = argv[2][1]; 378 #endif 379 update_crc(); 380 break; 381 case 'd': /* date BCD format YYMMDDhhmmss */ 382 set_date(argv[2]); 383 break; 384 case 'p': /* MAC table size */ 385 e.mac_count = simple_strtoul(argv[2], NULL, 16); 386 update_crc(); 387 break; 388 case '0' ... '9': /* "mac 0" through "mac 22" */ 389 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]); 390 break; 391 case 'h': /* help */ 392 default: 393 return cmd_usage(cmdtp); 394 } 395 396 return 0; 397 } 398 399 /** 400 * mac_read_from_eeprom - read the MAC addresses from EEPROM 401 * 402 * This function reads the MAC addresses from EEPROM and sets the 403 * appropriate environment variables for each one read. 404 * 405 * The environment variables are only set if they haven't been set already. 406 * This ensures that any user-saved variables are never overwritten. 407 * 408 * This function must be called after relocation. 409 * 410 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0 411 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is 412 * located at a different offset. 413 */ 414 int mac_read_from_eeprom(void) 415 { 416 unsigned int i; 417 u32 crc, crc_offset = offsetof(struct eeprom, crc); 418 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */ 419 420 puts("EEPROM: "); 421 422 if (read_eeprom()) { 423 printf("Read failed.\n"); 424 return -1; 425 } 426 427 if (!is_valid) { 428 printf("Invalid ID (%02x %02x %02x %02x)\n", 429 e.id[0], e.id[1], e.id[2], e.id[3]); 430 return -1; 431 } 432 433 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 434 /* 435 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset 436 * to where it is in v0. 437 */ 438 if (e.version == 0) 439 crc_offset = 0x72; 440 #endif 441 442 crc = crc32(0, (void *)&e, crc_offset); 443 crcp = (void *)&e + crc_offset; 444 if (crc != be32_to_cpu(*crcp)) { 445 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc)); 446 return -1; 447 } 448 449 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 450 /* 451 * MAC address #9 in v1 occupies the same position as the CRC in v0. 452 * Erase it so that it's not mistaken for a MAC address. We'll 453 * update the CRC later. 454 */ 455 if (e.version == 0) 456 memset(e.mac[8], 0xff, 6); 457 #endif 458 459 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) { 460 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) && 461 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { 462 char ethaddr[18]; 463 char enetvar[9]; 464 465 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", 466 e.mac[i][0], 467 e.mac[i][1], 468 e.mac[i][2], 469 e.mac[i][3], 470 e.mac[i][4], 471 e.mac[i][5]); 472 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); 473 /* Only initialize environment variables that are blank 474 * (i.e. have not yet been set) 475 */ 476 if (!getenv(enetvar)) 477 setenv(enetvar, ethaddr); 478 } 479 } 480 481 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 482 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], 483 be32_to_cpu(e.version)); 484 #else 485 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); 486 #endif 487 488 #ifdef CONFIG_SYS_I2C_EEPROM_NXID 489 /* 490 * Now we need to upconvert the data into v1 format. We do this last so 491 * that at boot time, U-Boot will still say "NXID v0". 492 */ 493 if (e.version == 0) { 494 e.version = NXID_VERSION; 495 update_crc(); 496 } 497 #endif 498 499 return 0; 500 } 501 502 #ifdef CONFIG_SYS_I2C_EEPROM_CCID 503 504 /** 505 * get_cpu_board_revision - get the CPU board revision on 85xx boards 506 * 507 * Read the EEPROM to determine the board revision. 508 * 509 * This function is called before relocation, so we need to read a private 510 * copy of the EEPROM into a local variable on the stack. 511 * 512 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global 513 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM, 514 * so that the SPD code will work. This means that all pre-relocation I2C 515 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if 516 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when 517 * this function is called. Oh well. 518 */ 519 unsigned int get_cpu_board_revision(void) 520 { 521 struct board_eeprom { 522 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ 523 u8 major; /* 0x04 Board revision, major */ 524 u8 minor; /* 0x05 Board revision, minor */ 525 } be; 526 527 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, 528 (void *)&be, sizeof(be)); 529 530 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D')) 531 return MPC85XX_CPU_BOARD_REV(0, 0); 532 533 if ((be.major == 0xff) && (be.minor == 0xff)) 534 return MPC85XX_CPU_BOARD_REV(0, 0); 535 536 return MPC85XX_CPU_BOARD_REV(be.major, be.minor); 537 } 538 #endif 539