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 | d79de1d | 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 | |
m8 | a484c60 | 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 | c48ae8c | 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 |
m8 | a484c60 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 13 | * be transferred with a single read or write. |
Wolfgang Denk | e4e5e4e | 2005-08-19 00:46:54 +0200 | [diff] [blame] | 14 | * |
m8 | a484c60 | 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 | e4e5e4e | 2005-08-19 00:46:54 +0200 | [diff] [blame] | 17 | * |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 18 | * #define CONFIG_SYS_I2C_FRAM |
| 19 | * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS |
m8 | a484c60 | 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 | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 28 | #ifndef CONFIG_SYS_I2C_SPEED |
| 29 | #define CONFIG_SYS_I2C_SPEED 50000 |
Stefan Roese | d9d9774 | 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 | d848b08 | 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 | 4208679 | 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 | |
Mario Six | df0a914 | 2016-04-20 10:44:52 +0200 | [diff] [blame] | 40 | #ifndef I2C_RXTX_LEN |
| 41 | #define I2C_RXTX_LEN 128 |
| 42 | #endif |
| 43 | |
Marek Vasut | 4208679 | 2015-11-10 20:53:28 +0100 | [diff] [blame] | 44 | #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS) |
| 45 | #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1)) |
| 46 | |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 47 | /* |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 48 | * 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] | 49 | * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM. |
| 50 | * |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 51 | * 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] | 52 | * 0x00000nxx for EEPROM address selectors and page number at n. |
| 53 | */ |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 54 | #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 55 | #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \ |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 56 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \ |
| 57 | (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 58 | #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2 |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 59 | #endif |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 60 | #endif |
| 61 | |
Marek Vasut | 2a632ffc | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 62 | __weak int eeprom_write_enable(unsigned dev_addr, int state) |
| 63 | { |
| 64 | return 0; |
| 65 | } |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 66 | |
Marek Vasut | f422ae7 | 2015-11-10 20:53:31 +0100 | [diff] [blame] | 67 | void eeprom_init(int bus) |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 68 | { |
| 69 | /* SPI EEPROM */ |
| 70 | #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
Marek Vasut | a1d4aeb | 2015-11-10 20:53:27 +0100 | [diff] [blame] | 71 | spi_init_f(); |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 72 | #endif |
| 73 | |
| 74 | /* I2C EEPROM */ |
| 75 | #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT) |
Marek Vasut | f422ae7 | 2015-11-10 20:53:31 +0100 | [diff] [blame] | 76 | #if defined(CONFIG_SYS_I2C) |
| 77 | if (bus >= 0) |
| 78 | i2c_set_bus_num(bus); |
| 79 | #endif |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 80 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 81 | #endif |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 82 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 83 | |
Marek Vasut | 6ae5957 | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 84 | static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr) |
| 85 | { |
| 86 | unsigned blk_off; |
| 87 | int alen; |
| 88 | |
| 89 | blk_off = offset & 0xff; /* block offset */ |
| 90 | #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 |
| 91 | addr[0] = offset >> 8; /* block number */ |
| 92 | addr[1] = blk_off; /* block offset */ |
| 93 | alen = 2; |
| 94 | #else |
| 95 | addr[0] = offset >> 16; /* block number */ |
| 96 | addr[1] = offset >> 8; /* upper address octet */ |
| 97 | addr[2] = blk_off; /* lower address octet */ |
| 98 | alen = 3; |
| 99 | #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */ |
| 100 | |
| 101 | addr[0] |= dev_addr; /* insert device address */ |
| 102 | |
| 103 | return alen; |
| 104 | } |
| 105 | |
Marek Vasut | d062c48 | 2015-11-10 20:53:29 +0100 | [diff] [blame] | 106 | static int eeprom_len(unsigned offset, unsigned end) |
| 107 | { |
| 108 | unsigned len = end - offset; |
| 109 | |
| 110 | /* |
| 111 | * For a FRAM device there is no limit on the number of the |
| 112 | * bytes that can be ccessed with the single read or write |
| 113 | * operation. |
| 114 | */ |
| 115 | #if !defined(CONFIG_SYS_I2C_FRAM) |
| 116 | unsigned blk_off = offset & 0xff; |
| 117 | unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off); |
| 118 | |
| 119 | if (maxlen > I2C_RXTX_LEN) |
| 120 | maxlen = I2C_RXTX_LEN; |
| 121 | |
| 122 | if (len > maxlen) |
| 123 | len = maxlen; |
| 124 | #endif |
| 125 | |
| 126 | return len; |
| 127 | } |
| 128 | |
Marek Vasut | 78990c7 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 129 | static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen, |
| 130 | uchar *buffer, unsigned len, bool read) |
| 131 | { |
| 132 | int ret = 0; |
| 133 | |
| 134 | /* SPI */ |
| 135 | #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C) |
| 136 | if (read) |
| 137 | spi_read(addr, alen, buffer, len); |
| 138 | else |
| 139 | spi_write(addr, alen, buffer, len); |
| 140 | #else /* I2C */ |
| 141 | |
| 142 | #if defined(CONFIG_SYS_I2C_EEPROM_BUS) |
| 143 | i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS); |
| 144 | #endif |
| 145 | |
| 146 | if (read) |
| 147 | ret = i2c_read(addr[0], offset, alen - 1, buffer, len); |
| 148 | else |
| 149 | ret = i2c_write(addr[0], offset, alen - 1, buffer, len); |
| 150 | |
| 151 | if (ret) |
| 152 | ret = 1; |
| 153 | #endif |
| 154 | return ret; |
| 155 | } |
| 156 | |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 157 | static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer, |
| 158 | unsigned cnt, bool read) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 159 | { |
| 160 | unsigned end = offset + cnt; |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 161 | unsigned alen, len; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 162 | int rcode = 0; |
Marek Vasut | 6ae5957 | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 163 | uchar addr[3]; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 164 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 165 | while (offset < end) { |
Marek Vasut | 6ae5957 | 2015-11-10 20:53:24 +0100 | [diff] [blame] | 166 | alen = eeprom_addr(dev_addr, offset, addr); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 167 | |
Marek Vasut | d062c48 | 2015-11-10 20:53:29 +0100 | [diff] [blame] | 168 | len = eeprom_len(offset, end); |
m8 | a484c60 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 169 | |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 170 | rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read); |
Marek Vasut | 78990c7 | 2015-11-10 20:53:23 +0100 | [diff] [blame] | 171 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 172 | buffer += len; |
| 173 | offset += len; |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 174 | |
| 175 | if (!read) |
| 176 | udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 177 | } |
m8 | a484c60 | 2005-08-12 21:16:13 +0200 | [diff] [blame] | 178 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 179 | return rcode; |
| 180 | } |
| 181 | |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 182 | int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 183 | { |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 184 | /* |
| 185 | * Read data until done or would cross a page boundary. |
| 186 | * We must write the address again when changing pages |
| 187 | * because the next page may be in a different device. |
| 188 | */ |
| 189 | return eeprom_rw(dev_addr, offset, buffer, cnt, 1); |
| 190 | } |
| 191 | |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 192 | int eeprom_write(unsigned dev_addr, unsigned offset, |
| 193 | uchar *buffer, unsigned cnt) |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 194 | { |
| 195 | int ret; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 196 | |
Marek Vasut | 2a632ffc | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 197 | eeprom_write_enable(dev_addr, 1); |
| 198 | |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 199 | /* |
| 200 | * Write data until done or would cross a write page boundary. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 201 | * We must write the address again when changing pages |
| 202 | * because the address counter only increments within a page. |
| 203 | */ |
Alexey Brodkin | 90eed7f | 2015-12-14 18:45:34 +0300 | [diff] [blame] | 204 | ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0); |
Marek Vasut | 2a632ffc | 2015-11-10 20:53:25 +0100 | [diff] [blame] | 205 | |
| 206 | eeprom_write_enable(dev_addr, 0); |
Marek Vasut | 7a23721 | 2015-11-10 20:53:30 +0100 | [diff] [blame] | 207 | return ret; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 210 | 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] | 211 | { |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 212 | const char *const fmt = |
| 213 | "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... "; |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 214 | char * const *args = &argv[2]; |
| 215 | int rcode; |
| 216 | ulong dev_addr, addr, off, cnt; |
Marek Vasut | 4d17f71 | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 217 | int bus_addr; |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 218 | |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 219 | switch (argc) { |
| 220 | #ifdef CONFIG_SYS_DEF_EEPROM_ADDR |
| 221 | case 5: |
Marek Vasut | 4d17f71 | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 222 | bus_addr = -1; |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 223 | dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR; |
| 224 | break; |
| 225 | #endif |
| 226 | case 6: |
Marek Vasut | 4d17f71 | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 227 | bus_addr = -1; |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 228 | dev_addr = simple_strtoul(*args++, NULL, 16); |
| 229 | break; |
Marek Vasut | 4d17f71 | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 230 | case 7: |
| 231 | bus_addr = simple_strtoul(*args++, NULL, 16); |
| 232 | dev_addr = simple_strtoul(*args++, NULL, 16); |
| 233 | break; |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 234 | default: |
| 235 | return CMD_RET_USAGE; |
| 236 | } |
| 237 | |
| 238 | addr = simple_strtoul(*args++, NULL, 16); |
| 239 | off = simple_strtoul(*args++, NULL, 16); |
| 240 | cnt = simple_strtoul(*args++, NULL, 16); |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 241 | |
Marek Vasut | 4d17f71 | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 242 | eeprom_init(bus_addr); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 243 | |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 244 | if (strcmp(argv[1], "read") == 0) { |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 245 | printf(fmt, dev_addr, argv[1], addr, off, cnt); |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 246 | |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 247 | rcode = eeprom_read(dev_addr, off, (uchar *)addr, cnt); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 248 | |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 249 | puts("done\n"); |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 250 | return rcode; |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 251 | } else if (strcmp(argv[1], "write") == 0) { |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 252 | printf(fmt, dev_addr, argv[1], addr, off, cnt); |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 253 | |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 254 | rcode = eeprom_write(dev_addr, off, (uchar *)addr, cnt); |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 255 | |
Marek Vasut | a24ea7a | 2015-11-10 20:53:33 +0100 | [diff] [blame] | 256 | puts("done\n"); |
Marek Vasut | ee54144 | 2015-11-10 20:53:19 +0100 | [diff] [blame] | 257 | return rcode; |
Marek Vasut | 704a096 | 2015-11-10 20:53:18 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return CMD_RET_USAGE; |
| 261 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 262 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 263 | U_BOOT_CMD( |
Marek Vasut | 4d17f71 | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 264 | eeprom, 7, 1, do_eeprom, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 265 | "EEPROM sub-system", |
Marek Vasut | 4d17f71 | 2015-11-10 20:53:32 +0100 | [diff] [blame] | 266 | "read <bus> <devaddr> addr off cnt\n" |
| 267 | "eeprom write <bus> <devaddr> addr off cnt\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 268 | " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'" |
Jeroen Hofstee | f384fbf | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 269 | ) |