uboot/cmd/eeprom.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * (C) Copyright 2000, 2001
   4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   5 */
   6
   7/*
   8 * Support for read and write access to EEPROM like memory devices. This
   9 * includes regular EEPROM as well as  FRAM (ferroelectic nonvolaile RAM).
  10 * FRAM devices read and write data at bus speed. In particular, there is no
  11 * write delay. Also, there is no limit imposed on the number of bytes that can
  12 * be transferred with a single read or write.
  13 *
  14 * Use the following configuration options to ensure no unneeded performance
  15 * degradation (typical for EEPROM) is incured for FRAM memory:
  16 *
  17 * #define CONFIG_SYS_I2C_FRAM
  18 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  19 *
  20 */
  21
  22#include <common.h>
  23#include <config.h>
  24#include <command.h>
  25#include <i2c.h>
  26#include <eeprom_layout.h>
  27
  28#ifndef CONFIG_SYS_I2C_SPEED
  29#define CONFIG_SYS_I2C_SPEED    50000
  30#endif
  31
  32#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  33#define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS   0
  34#endif
  35
  36#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
  37#define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS       8
  38#endif
  39
  40#ifndef I2C_RXTX_LEN
  41#define I2C_RXTX_LEN    128
  42#endif
  43
  44#define EEPROM_PAGE_SIZE        (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  45#define EEPROM_PAGE_OFFSET(x)   ((x) & (EEPROM_PAGE_SIZE - 1))
  46
  47/*
  48 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  49 *   0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  50 *
  51 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  52 *   0x00000nxx for EEPROM address selectors and page number at n.
  53 */
  54#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  55#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
  56        (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
  57        (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
  58#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
  59#endif
  60#endif
  61
  62#if defined(CONFIG_DM_I2C)
  63int eeprom_i2c_bus;
  64#endif
  65
  66__weak int eeprom_write_enable(unsigned dev_addr, int state)
  67{
  68        return 0;
  69}
  70
  71void eeprom_init(int bus)
  72{
  73        /* I2C EEPROM */
  74#if defined(CONFIG_DM_I2C)
  75        eeprom_i2c_bus = bus;
  76#elif defined(CONFIG_SYS_I2C)
  77        if (bus >= 0)
  78                i2c_set_bus_num(bus);
  79        i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  80#endif
  81}
  82
  83static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
  84{
  85        unsigned blk_off;
  86        int alen;
  87
  88        blk_off = offset & 0xff;        /* block offset */
  89#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
  90        addr[0] = offset >> 8;          /* block number */
  91        addr[1] = blk_off;              /* block offset */
  92        alen = 2;
  93#else
  94        addr[0] = offset >> 16;         /* block number */
  95        addr[1] = offset >>  8;         /* upper address octet */
  96        addr[2] = blk_off;              /* lower address octet */
  97        alen = 3;
  98#endif  /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
  99
 100        addr[0] |= dev_addr;            /* insert device address */
 101
 102        return alen;
 103}
 104
 105static int eeprom_len(unsigned offset, unsigned end)
 106{
 107        unsigned len = end - offset;
 108
 109        /*
 110         * For a FRAM device there is no limit on the number of the
 111         * bytes that can be ccessed with the single read or write
 112         * operation.
 113         */
 114#if !defined(CONFIG_SYS_I2C_FRAM)
 115        unsigned blk_off = offset & 0xff;
 116        unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
 117
 118        if (maxlen > I2C_RXTX_LEN)
 119                maxlen = I2C_RXTX_LEN;
 120
 121        if (len > maxlen)
 122                len = maxlen;
 123#endif
 124
 125        return len;
 126}
 127
 128static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
 129                           uchar *buffer, unsigned len, bool read)
 130{
 131        int ret = 0;
 132
 133#if defined(CONFIG_DM_I2C)
 134        struct udevice *dev;
 135
 136        ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
 137                                      alen - 1, &dev);
 138        if (ret) {
 139                printf("%s: Cannot find udev for a bus %d\n", __func__,
 140                       eeprom_i2c_bus);
 141                return CMD_RET_FAILURE;
 142        }
 143
 144        if (read)
 145                ret = dm_i2c_read(dev, offset, buffer, len);
 146        else
 147                ret = dm_i2c_write(dev, offset, buffer, len);
 148
 149#else /* Non DM I2C support - will be removed */
 150
 151        if (read)
 152                ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
 153        else
 154                ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
 155#endif /* CONFIG_DM_I2C */
 156        if (ret)
 157                ret = CMD_RET_FAILURE;
 158
 159        return ret;
 160}
 161
 162static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
 163                     unsigned cnt, bool read)
 164{
 165        unsigned end = offset + cnt;
 166        unsigned alen, len;
 167        int rcode = 0;
 168        uchar addr[3];
 169
 170#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
 171        eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
 172#endif
 173
 174        while (offset < end) {
 175                alen = eeprom_addr(dev_addr, offset, addr);
 176
 177                len = eeprom_len(offset, end);
 178
 179                rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
 180
 181                buffer += len;
 182                offset += len;
 183
 184                if (!read)
 185                        udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
 186        }
 187
 188        return rcode;
 189}
 190
 191int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
 192{
 193        /*
 194         * Read data until done or would cross a page boundary.
 195         * We must write the address again when changing pages
 196         * because the next page may be in a different device.
 197         */
 198        return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
 199}
 200
 201int eeprom_write(unsigned dev_addr, unsigned offset,
 202                 uchar *buffer, unsigned cnt)
 203{
 204        int ret;
 205
 206        eeprom_write_enable(dev_addr, 1);
 207
 208        /*
 209         * Write data until done or would cross a write page boundary.
 210         * We must write the address again when changing pages
 211         * because the address counter only increments within a page.
 212         */
 213        ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
 214
 215        eeprom_write_enable(dev_addr, 0);
 216        return ret;
 217}
 218
 219static int parse_numeric_param(char *str)
 220{
 221        char *endptr;
 222        int value = simple_strtol(str, &endptr, 16);
 223
 224        return (*endptr != '\0') ? -1 : value;
 225}
 226
 227/**
 228 * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
 229 *
 230 * @i2c_bus:    address to store the i2c bus
 231 * @i2c_addr:   address to store the device i2c address
 232 * @argc:       count of command line arguments left to parse
 233 * @argv:       command line arguments left to parse
 234 * @argc_no_bus_addr:   argc value we expect to see when bus & addr aren't given
 235 *
 236 * @returns:    number of arguments parsed or CMD_RET_USAGE if error
 237 */
 238static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
 239                              char * const argv[], int argc_no_bus_addr)
 240{
 241        int argc_no_bus = argc_no_bus_addr + 1;
 242        int argc_bus_addr = argc_no_bus_addr + 2;
 243
 244#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
 245        if (argc == argc_no_bus_addr) {
 246                *i2c_bus = -1;
 247                *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
 248
 249                return 0;
 250        }
 251#endif
 252        if (argc == argc_no_bus) {
 253                *i2c_bus = -1;
 254                *i2c_addr = parse_numeric_param(argv[0]);
 255
 256                return 1;
 257        }
 258
 259        if (argc == argc_bus_addr) {
 260                *i2c_bus = parse_numeric_param(argv[0]);
 261                *i2c_addr = parse_numeric_param(argv[1]);
 262
 263                return 2;
 264        }
 265
 266        return CMD_RET_USAGE;
 267}
 268
 269#ifdef CONFIG_CMD_EEPROM_LAYOUT
 270
 271__weak int eeprom_parse_layout_version(char *str)
 272{
 273        return LAYOUT_VERSION_UNRECOGNIZED;
 274}
 275
 276static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
 277
 278#endif
 279
 280enum eeprom_action {
 281        EEPROM_READ,
 282        EEPROM_WRITE,
 283        EEPROM_PRINT,
 284        EEPROM_UPDATE,
 285        EEPROM_ACTION_INVALID,
 286};
 287
 288static enum eeprom_action parse_action(char *cmd)
 289{
 290        if (!strncmp(cmd, "read", 4))
 291                return EEPROM_READ;
 292        if (!strncmp(cmd, "write", 5))
 293                return EEPROM_WRITE;
 294#ifdef CONFIG_CMD_EEPROM_LAYOUT
 295        if (!strncmp(cmd, "print", 5))
 296                return EEPROM_PRINT;
 297        if (!strncmp(cmd, "update", 6))
 298                return EEPROM_UPDATE;
 299#endif
 300
 301        return EEPROM_ACTION_INVALID;
 302}
 303
 304static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
 305                                  ulong i2c_addr, int layout_ver, char *key,
 306                                  char *value, ulong addr, ulong off, ulong cnt)
 307{
 308        int rcode = 0;
 309        const char *const fmt =
 310                "\nEEPROM @0x%lX %s: addr %08lx  off %04lx  count %ld ... ";
 311#ifdef CONFIG_CMD_EEPROM_LAYOUT
 312        struct eeprom_layout layout;
 313#endif
 314
 315        if (action == EEPROM_ACTION_INVALID)
 316                return CMD_RET_USAGE;
 317
 318        eeprom_init(i2c_bus);
 319        if (action == EEPROM_READ) {
 320                printf(fmt, i2c_addr, "read", addr, off, cnt);
 321
 322                rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
 323
 324                puts("done\n");
 325                return rcode;
 326        } else if (action == EEPROM_WRITE) {
 327                printf(fmt, i2c_addr, "write", addr, off, cnt);
 328
 329                rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
 330
 331                puts("done\n");
 332                return rcode;
 333        }
 334
 335#ifdef CONFIG_CMD_EEPROM_LAYOUT
 336        rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
 337        if (rcode < 0)
 338                return rcode;
 339
 340        eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
 341                            layout_ver);
 342
 343        if (action == EEPROM_PRINT) {
 344                layout.print(&layout);
 345                return 0;
 346        }
 347
 348        layout.update(&layout, key, value);
 349
 350        rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
 351#endif
 352
 353        return rcode;
 354}
 355
 356#define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
 357int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 358{
 359        int layout_ver = LAYOUT_VERSION_AUTODETECT;
 360        enum eeprom_action action = EEPROM_ACTION_INVALID;
 361        int i2c_bus = -1, index = 0;
 362        ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
 363        int ret;
 364        char *field_name = "";
 365        char *field_value = "";
 366
 367        if (argc <= 1)
 368                return CMD_RET_USAGE;
 369
 370        NEXT_PARAM(argc, index); /* Skip program name */
 371
 372        action = parse_action(argv[index]);
 373        NEXT_PARAM(argc, index);
 374
 375        if (action == EEPROM_ACTION_INVALID)
 376                return CMD_RET_USAGE;
 377
 378#ifdef CONFIG_CMD_EEPROM_LAYOUT
 379        if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
 380                if (!strcmp(argv[index], "-l")) {
 381                        NEXT_PARAM(argc, index);
 382                        layout_ver = eeprom_parse_layout_version(argv[index]);
 383                        NEXT_PARAM(argc, index);
 384                }
 385        }
 386#endif
 387
 388        switch (action) {
 389        case EEPROM_READ:
 390        case EEPROM_WRITE:
 391                ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
 392                                         argv + index, 3);
 393                break;
 394        case EEPROM_PRINT:
 395                ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
 396                                         argv + index, 0);
 397                break;
 398        case EEPROM_UPDATE:
 399                ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
 400                                         argv + index, 2);
 401                break;
 402        default:
 403                /* Get compiler to stop whining */
 404                return CMD_RET_USAGE;
 405        }
 406
 407        if (ret == CMD_RET_USAGE)
 408                return ret;
 409
 410        while (ret--)
 411                NEXT_PARAM(argc, index);
 412
 413        if (action == EEPROM_READ || action == EEPROM_WRITE) {
 414                addr = parse_numeric_param(argv[index]);
 415                NEXT_PARAM(argc, index);
 416                off = parse_numeric_param(argv[index]);
 417                NEXT_PARAM(argc, index);
 418                cnt = parse_numeric_param(argv[index]);
 419        }
 420
 421#ifdef CONFIG_CMD_EEPROM_LAYOUT
 422        if (action == EEPROM_UPDATE) {
 423                field_name = argv[index];
 424                NEXT_PARAM(argc, index);
 425                field_value = argv[index];
 426                NEXT_PARAM(argc, index);
 427        }
 428#endif
 429
 430        return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
 431                                      field_name, field_value, addr, off, cnt);
 432}
 433
 434U_BOOT_CMD(
 435        eeprom, 8,      1,      do_eeprom,
 436        "EEPROM sub-system",
 437        "read  <bus> <devaddr> addr off cnt\n"
 438        "eeprom write <bus> <devaddr> addr off cnt\n"
 439        "       - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
 440#ifdef CONFIG_CMD_EEPROM_LAYOUT
 441        "\n"
 442        "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
 443        "       - Print layout fields and their data in human readable format\n"
 444        "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
 445        "       - Update a specific eeprom field with new data.\n"
 446        "         The new data must be written in the same human readable format as shown by the print command.\n"
 447        "\n"
 448        "LAYOUT VERSIONS\n"
 449        "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
 450        "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
 451        "The values which can be provided with the -l option are:\n"
 452        CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"
 453#endif
 454)
 455