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