wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000, 2001 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 8 | /* |
| 9 | * Support for read and write access to EEPROM like memory devices. This |
| 10 | * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM). |
| 11 | * FRAM devices read and write data at bus speed. In particular, there is no |
Peter Meerwald | e506a00 | 2012-02-08 05:31:53 +0000 | [diff] [blame] | 12 | * write delay. Also, there is no limit imposed on the number of bytes that can |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 13 | * be transferred with a single read or write. |
Wolfgang Denk | 6617aae | 2005-08-19 00:46:54 +0200 | [diff] [blame] | 14 | * |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 15 | * Use the following configuration options to ensure no unneeded performance |
| 16 | * degradation (typical for EEPROM) is incured for FRAM memory: |
Wolfgang Denk | 6617aae | 2005-08-19 00:46:54 +0200 | [diff] [blame] | 17 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 18 | * #define CONFIG_SYS_I2C_FRAM |
| 19 | * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 23 | #include <common.h> |
| 24 | #include <config.h> |
| 25 | #include <command.h> |
| 26 | #include <i2c.h> |
| 27 | |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 28 | #ifndef CONFIG_SYS_I2C_SPEED |
| 29 | #define CONFIG_SYS_I2C_SPEED 50000 |
Stefan Roese | 98f4a3d | 2005-09-22 09:04:17 +0200 | [diff] [blame] | 30 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 31 | |
Marek Vasut | d738746 | 2015-11-10 20:53:26 +0100 | [diff] [blame] | 32 | #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS |
| 33 | #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0 |
| 34 | #endif |
| 35 | |
Marek Vasut | 6717e3c | 2015-11-10 20:53:28 +0100 | [diff] [blame] | 36 | #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS |
| 37 | #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8 |
| 38 | #endif |
| 39 | |
| 40 | #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS) |
| 41 | #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1)) |
| 42 | |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 43 | /* |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 44 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 45 | * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM. |
| 46 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 47 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 48 | * 0x00000nxx for EEPROM address selectors and page number at n. |
| 49 | */ |
Heiko Schocher | 548738b | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 50 | #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 51 | #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \ |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 52 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \ |
| 53 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 54 | #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 55 | #endif |
| 56 | #endif |
| 57 | |
Marek Vasut | 52cd47c | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 58 | __weak int eeprom_write_enable(unsigned dev_addr, int state) |
| 59 | { |
| 60 | return 0; |
| 61 | } |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 62 | |
Marek Vasut | 354e3ed | 2015-11-10 20:53:31 +0100 | [diff] [blame] | 63 | void eeprom_init(int bus) |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 64 | { |
| 65 | /* SPI EEPROM */ |
| 66 | #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
Marek Vasut | 8eee40a | 2015-11-10 20:53:27 +0100 | [diff] [blame] | 67 | spi_init_f(); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | /* I2C EEPROM */ |
| 71 | #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT) |
Marek Vasut | 354e3ed | 2015-11-10 20:53:31 +0100 | [diff] [blame] | 72 | #if defined(CONFIG_SYS_I2C) |
| 73 | if (bus >= 0) |
| 74 | i2c_set_bus_num(bus); |
| 75 | #endif |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 76 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 77 | #endif |
| 78 | } |
| 79 | |
Marek Vasut | 02c321c | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 80 | static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr) |
| 81 | { |
| 82 | unsigned blk_off; |
| 83 | int alen; |
| 84 | |
| 85 | blk_off = offset & 0xff; /* block offset */ |
| 86 | #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 |
| 87 | addr[0] = offset >> 8; /* block number */ |
| 88 | addr[1] = blk_off; /* block offset */ |
| 89 | alen = 2; |
| 90 | #else |
| 91 | addr[0] = offset >> 16; /* block number */ |
| 92 | addr[1] = offset >> 8; /* upper address octet */ |
| 93 | addr[2] = blk_off; /* lower address octet */ |
| 94 | alen = 3; |
| 95 | #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */ |
| 96 | |
| 97 | addr[0] |= dev_addr; /* insert device address */ |
| 98 | |
| 99 | return alen; |
| 100 | } |
| 101 | |
Marek Vasut | 39b6f98 | 2015-11-10 20:53:29 +0100 | [diff] [blame] | 102 | static int eeprom_len(unsigned offset, unsigned end) |
| 103 | { |
| 104 | unsigned len = end - offset; |
| 105 | |
| 106 | /* |
| 107 | * For a FRAM device there is no limit on the number of the |
| 108 | * bytes that can be ccessed with the single read or write |
| 109 | * operation. |
| 110 | */ |
| 111 | #if !defined(CONFIG_SYS_I2C_FRAM) |
| 112 | unsigned blk_off = offset & 0xff; |
| 113 | unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off); |
| 114 | |
| 115 | if (maxlen > I2C_RXTX_LEN) |
| 116 | maxlen = I2C_RXTX_LEN; |
| 117 | |
| 118 | if (len > maxlen) |
| 119 | len = maxlen; |
| 120 | #endif |
| 121 | |
| 122 | return len; |
| 123 | } |
| 124 | |
Marek Vasut | 9132088 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 125 | static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen, |
| 126 | uchar *buffer, unsigned len, bool read) |
| 127 | { |
| 128 | int ret = 0; |
| 129 | |
| 130 | /* SPI */ |
| 131 | #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
| 132 | if (read) |
| 133 | spi_read(addr, alen, buffer, len); |
| 134 | else |
| 135 | spi_write(addr, alen, buffer, len); |
| 136 | #else /* I2C */ |
| 137 | |
| 138 | #if defined(CONFIG_SYS_I2C_EEPROM_BUS) |
| 139 | i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS); |
| 140 | #endif |
| 141 | |
| 142 | if (read) |
| 143 | ret = i2c_read(addr[0], offset, alen - 1, buffer, len); |
| 144 | else |
| 145 | ret = i2c_write(addr[0], offset, alen - 1, buffer, len); |
| 146 | |
| 147 | if (ret) |
| 148 | ret = 1; |
| 149 | #endif |
| 150 | return ret; |
| 151 | } |
| 152 | |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 153 | static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer, |
| 154 | unsigned cnt, bool read) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 155 | { |
| 156 | unsigned end = offset + cnt; |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 157 | unsigned alen, len; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 158 | int rcode = 0; |
Marek Vasut | 02c321c | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 159 | uchar addr[3]; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 160 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 161 | while (offset < end) { |
Marek Vasut | 02c321c | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 162 | alen = eeprom_addr(dev_addr, offset, addr); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 163 | |
Marek Vasut | 39b6f98 | 2015-11-10 20:53:29 +0100 | [diff] [blame] | 164 | len = eeprom_len(offset, end); |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 165 | |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 166 | rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read); |
Marek Vasut | 9132088 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 167 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 168 | buffer += len; |
| 169 | offset += len; |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 170 | |
| 171 | if (!read) |
| 172 | udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 173 | } |
| d4f5c72 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 174 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 175 | return rcode; |
| 176 | } |
| 177 | |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 178 | int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 179 | { |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 180 | /* |
| 181 | * Read data until done or would cross a page boundary. |
| 182 | * We must write the address again when changing pages |
| 183 | * because the next page may be in a different device. |
| 184 | */ |
| 185 | return eeprom_rw(dev_addr, offset, buffer, cnt, 1); |
| 186 | } |
| 187 | |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 188 | int eeprom_write(unsigned dev_addr, unsigned offset, |
| 189 | uchar *buffer, unsigned cnt) |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 190 | { |
| 191 | int ret; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 192 | |
Marek Vasut | 52cd47c | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 193 | eeprom_write_enable(dev_addr, 1); |
| 194 | |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 195 | /* |
| 196 | * Write data until done or would cross a write page boundary. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 197 | * We must write the address again when changing pages |
| 198 | * because the address counter only increments within a page. |
| 199 | */ |
Alexey Brodkin | 52bc7c7 | 2015-12-14 18:45:34 +0300 | [diff] [blame] | 200 | ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0); |
Marek Vasut | 52cd47c | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 201 | |
| 202 | eeprom_write_enable(dev_addr, 0); |
Marek Vasut | 1a37889 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 203 | return ret; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 206 | static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 207 | { |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 208 | const char *const fmt = |
| 209 | "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... "; |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 210 | char * const *args = &argv[2]; |
| 211 | int rcode; |
| 212 | ulong dev_addr, addr, off, cnt; |
Marek Vasut | d4fec4e | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 213 | int bus_addr; |
Heiko Schocher | 548738b | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 214 | |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 215 | switch (argc) { |
| 216 | #ifdef CONFIG_SYS_DEF_EEPROM_ADDR |
| 217 | case 5: |
Marek Vasut | d4fec4e | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 218 | bus_addr = -1; |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 219 | dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR; |
| 220 | break; |
| 221 | #endif |
| 222 | case 6: |
Marek Vasut | d4fec4e | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 223 | bus_addr = -1; |
| 224 | dev_addr = simple_strtoul(*args++, NULL, 16); |
| 225 | break; |
| 226 | case 7: |
| 227 | bus_addr = simple_strtoul(*args++, NULL, 16); |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 228 | dev_addr = simple_strtoul(*args++, NULL, 16); |
| 229 | break; |
| 230 | default: |
| 231 | return CMD_RET_USAGE; |
| 232 | } |
| 233 | |
| 234 | addr = simple_strtoul(*args++, NULL, 16); |
| 235 | off = simple_strtoul(*args++, NULL, 16); |
| 236 | cnt = simple_strtoul(*args++, NULL, 16); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 237 | |
Marek Vasut | d4fec4e | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 238 | eeprom_init(bus_addr); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 239 | |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 240 | if (strcmp(argv[1], "read") == 0) { |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 241 | printf(fmt, dev_addr, argv[1], addr, off, cnt); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 242 | |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 243 | rcode = eeprom_read(dev_addr, off, (uchar *)addr, cnt); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 244 | |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 245 | puts("done\n"); |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 246 | return rcode; |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 247 | } else if (strcmp(argv[1], "write") == 0) { |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 248 | printf(fmt, dev_addr, argv[1], addr, off, cnt); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 249 | |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 250 | rcode = eeprom_write(dev_addr, off, (uchar *)addr, cnt); |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 251 | |
Marek Vasut | d4e69e6 | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 252 | puts("done\n"); |
Marek Vasut | e4f65d0 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 253 | return rcode; |
Marek Vasut | 4f296d0 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 257 | } |
Heiko Schocher | 548738b | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 258 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 259 | U_BOOT_CMD( |
Marek Vasut | d4fec4e | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 260 | eeprom, 7, 1, do_eeprom, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 261 | "EEPROM sub-system", |
Marek Vasut | d4fec4e | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 262 | "read <bus> <devaddr> addr off cnt\n" |
| 263 | "eeprom write <bus> <devaddr> addr off cnt\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 264 | " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'" |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 265 | ) |