eeprom: Zap CONFIG_SYS_EEPROM_X40430
[platform/kernel/u-boot.git] / common / cmd_eeprom.c
1 /*
2  * (C) Copyright 2000, 2001
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
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
12  * write delay. Also, there is no limit imposed on the number of bytes that can
13  * be transferred with a single read or write.
14  *
15  * Use the following configuration options to ensure no unneeded performance
16  * degradation (typical for EEPROM) is incured for FRAM memory:
17  *
18  * #define CONFIG_SYS_I2C_FRAM
19  * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
20  *
21  */
22
23 #include <common.h>
24 #include <config.h>
25 #include <command.h>
26 #include <i2c.h>
27
28 #ifndef CONFIG_SYS_I2C_SPEED
29 #define CONFIG_SYS_I2C_SPEED    50000
30 #endif
31
32 /*
33  * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
34  *   0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
35  *
36  * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
37  *   0x00000nxx for EEPROM address selectors and page number at n.
38  */
39 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
40 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
41     (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
42     (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
43 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
44 #endif
45 #endif
46
47 #if defined(CONFIG_SYS_EEPROM_WREN)
48 extern int eeprom_write_enable (unsigned dev_addr, int state);
49 #endif
50
51 void eeprom_init(void)
52 {
53         /* SPI EEPROM */
54 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
55         spi_init_f ();
56 #endif
57
58         /* I2C EEPROM */
59 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
60         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
61 #endif
62 }
63
64 int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
65 {
66         unsigned end = offset + cnt;
67         unsigned blk_off;
68         int rcode = 0;
69
70         /*
71          * Read data until done or would cross a page boundary.
72          * We must write the address again when changing pages
73          * because the next page may be in a different device.
74          */
75         while (offset < end) {
76                 unsigned alen, len;
77 #if !defined(CONFIG_SYS_I2C_FRAM)
78                 unsigned maxlen;
79 #endif
80
81 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
82                 uchar addr[2];
83
84                 blk_off = offset & 0xFF;        /* block offset */
85
86                 addr[0] = offset >> 8;          /* block number */
87                 addr[1] = blk_off;              /* block offset */
88                 alen    = 2;
89 #else
90                 uchar addr[3];
91
92                 blk_off = offset & 0xFF;        /* block offset */
93
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, CONFIG_SPI_X */
99
100                 addr[0] |= dev_addr;            /* insert device address */
101
102                 len = end - offset;
103
104                 /*
105                  * For a FRAM device there is no limit on the number of the
106                  * bytes that can be ccessed with the single read or write
107                  * operation.
108                  */
109 #if !defined(CONFIG_SYS_I2C_FRAM)
110                 maxlen = 0x100 - blk_off;
111                 if (maxlen > I2C_RXTX_LEN)
112                         maxlen = I2C_RXTX_LEN;
113                 if (len > maxlen)
114                         len = maxlen;
115 #endif
116
117 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
118                 spi_read (addr, alen, buffer, len);
119 #else
120 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
121                 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
122 #endif
123                 if (i2c_read(addr[0], offset, alen - 1, buffer, len))
124                         rcode = 1;
125 #endif
126                 buffer += len;
127                 offset += len;
128         }
129
130         return rcode;
131 }
132
133 int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
134 {
135         unsigned end = offset + cnt;
136         unsigned blk_off;
137         int rcode = 0;
138
139 #if defined(CONFIG_SYS_EEPROM_WREN)
140         eeprom_write_enable (dev_addr,1);
141 #endif
142         /*
143          * Write data until done or would cross a write page boundary.
144          * We must write the address again when changing pages
145          * because the address counter only increments within a page.
146          */
147
148         while (offset < end) {
149                 unsigned alen, len;
150 #if !defined(CONFIG_SYS_I2C_FRAM)
151                 unsigned maxlen;
152 #endif
153
154 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
155                 uchar addr[2];
156
157                 blk_off = offset & 0xFF;        /* block offset */
158
159                 addr[0] = offset >> 8;          /* block number */
160                 addr[1] = blk_off;              /* block offset */
161                 alen    = 2;
162 #else
163                 uchar addr[3];
164
165                 blk_off = offset & 0xFF;        /* block offset */
166
167                 addr[0] = offset >> 16;         /* block number */
168                 addr[1] = offset >>  8;         /* upper address octet */
169                 addr[2] = blk_off;              /* lower address octet */
170                 alen    = 3;
171 #endif  /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
172
173                 addr[0] |= dev_addr;            /* insert device address */
174
175                 len = end - offset;
176
177                 /*
178                  * For a FRAM device there is no limit on the number of the
179                  * bytes that can be accessed with the single read or write
180                  * operation.
181                  */
182 #if !defined(CONFIG_SYS_I2C_FRAM)
183
184 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
185
186 #define EEPROM_PAGE_SIZE        (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
187 #define EEPROM_PAGE_OFFSET(x)   ((x) & (EEPROM_PAGE_SIZE - 1))
188
189                 maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
190 #else
191                 maxlen = 0x100 - blk_off;
192 #endif
193                 if (maxlen > I2C_RXTX_LEN)
194                         maxlen = I2C_RXTX_LEN;
195
196                 if (len > maxlen)
197                         len = maxlen;
198 #endif
199
200 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
201                 spi_write (addr, alen, buffer, len);
202 #else
203
204 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
205                 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
206 #endif
207                 if (i2c_write(addr[0], offset, alen - 1, buffer, len))
208                         rcode = 1;
209
210 #endif
211                 buffer += len;
212                 offset += len;
213
214 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
215                 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
216 #endif
217         }
218 #if defined(CONFIG_SYS_EEPROM_WREN)
219         eeprom_write_enable (dev_addr,0);
220 #endif
221         return rcode;
222 }
223
224 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
225 int eeprom_probe(unsigned dev_addr, unsigned offset)
226 {
227         unsigned char chip;
228
229         /* Probe the chip address
230          */
231 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
232         chip = offset >> 8;             /* block number */
233 #else
234         chip = offset >> 16;            /* block number */
235 #endif  /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
236
237         chip |= dev_addr;               /* insert device address */
238
239         return (i2c_probe (chip));
240 }
241 #endif
242
243 static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
244 {
245         const char *const fmt =
246                 "\nEEPROM @0x%lX %s: addr %08lx  off %04lx  count %ld ... ";
247         char * const *args = &argv[2];
248         int rcode;
249         ulong dev_addr, addr, off, cnt;
250
251         switch (argc) {
252 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
253         case 5:
254                 dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
255                 break;
256 #endif
257         case 6:
258                 dev_addr = simple_strtoul(*args++, NULL, 16);
259                 break;
260         default:
261                 return CMD_RET_USAGE;
262         }
263
264         addr = simple_strtoul(*args++, NULL, 16);
265         off = simple_strtoul(*args++, NULL, 16);
266         cnt = simple_strtoul(*args++, NULL, 16);
267
268 # if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
269         eeprom_init ();
270 # endif /* !CONFIG_SPI */
271
272         if (strcmp (argv[1], "read") == 0) {
273                 printf(fmt, dev_addr, argv[1], addr, off, cnt);
274
275                 rcode = eeprom_read(dev_addr, off, (uchar *) addr, cnt);
276
277                 puts ("done\n");
278                 return rcode;
279         } else if (strcmp (argv[1], "write") == 0) {
280                 printf(fmt, dev_addr, argv[1], addr, off, cnt);
281
282                 rcode = eeprom_write(dev_addr, off, (uchar *) addr, cnt);
283
284                 puts ("done\n");
285                 return rcode;
286         }
287
288         return CMD_RET_USAGE;
289 }
290
291 U_BOOT_CMD(
292         eeprom, 6,      1,      do_eeprom,
293         "EEPROM sub-system",
294         "read  devaddr addr off cnt\n"
295         "eeprom write devaddr addr off cnt\n"
296         "       - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
297 )