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