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