blob: b82011d0fd2833301cc4b5df12ce0b1e4edd49e7 [file] [log] [blame]
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02001/*
2 * SPI flash interface
3 *
4 * Copyright (C) 2008 Atmel Corporation
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +02005 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 *
Mike Frysinger4166ee52009-10-09 17:12:44 -04007 * Licensed under the GPL-2 or later.
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02008 */
Mike Frysingerf773a1b2009-03-23 23:03:58 -04009
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020010#include <common.h>
11#include <malloc.h>
12#include <spi.h>
13#include <spi_flash.h>
Patrick Sestierbd0d19c2011-04-15 14:25:25 +000014#include <watchdog.h>
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020015
16#include "spi_flash_internal.h"
17
Mike Frysingere7b44ed2011-01-10 02:20:13 -050018static void spi_flash_addr(u32 addr, u8 *cmd)
19{
20 /* cmd[0] is actual command */
21 cmd[1] = addr >> 16;
22 cmd[2] = addr >> 8;
23 cmd[3] = addr >> 0;
24}
25
Mike Frysinger000044d2011-01-10 02:20:11 -050026static int spi_flash_read_write(struct spi_slave *spi,
27 const u8 *cmd, size_t cmd_len,
28 const u8 *data_out, u8 *data_in,
29 size_t data_len)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020030{
31 unsigned long flags = SPI_XFER_BEGIN;
32 int ret;
33
Mike Frysinger000044d2011-01-10 02:20:11 -050034 if (data_len == 0)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020035 flags |= SPI_XFER_END;
36
Mike Frysinger000044d2011-01-10 02:20:11 -050037 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020038 if (ret) {
Mike Frysinger000044d2011-01-10 02:20:11 -050039 debug("SF: Failed to send command (%zu bytes): %d\n",
40 cmd_len, ret);
41 } else if (data_len != 0) {
42 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020043 if (ret)
Mike Frysinger000044d2011-01-10 02:20:11 -050044 debug("SF: Failed to transfer %zu bytes of data: %d\n",
45 data_len, ret);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020046 }
47
48 return ret;
49}
50
Mike Frysinger000044d2011-01-10 02:20:11 -050051int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
52{
53 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
54}
55
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020056int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
57 size_t cmd_len, void *data, size_t data_len)
58{
Mike Frysinger000044d2011-01-10 02:20:11 -050059 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020060}
61
62int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
63 const void *data, size_t data_len)
64{
Mike Frysinger000044d2011-01-10 02:20:11 -050065 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020066}
67
Mike Frysingerd4aa5002011-04-25 06:58:29 +000068int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
69 size_t len, const void *buf)
70{
71 unsigned long page_addr, byte_addr, page_size;
72 size_t chunk_len, actual;
73 int ret;
74 u8 cmd[4];
75
76 page_size = flash->page_size;
77 page_addr = offset / page_size;
78 byte_addr = offset % page_size;
79
80 ret = spi_claim_bus(flash->spi);
81 if (ret) {
82 debug("SF: unable to claim SPI bus\n");
83 return ret;
84 }
85
86 cmd[0] = CMD_PAGE_PROGRAM;
87 for (actual = 0; actual < len; actual += chunk_len) {
88 chunk_len = min(len - actual, page_size - byte_addr);
89
Simon Glass1e566bc2013-03-11 06:08:06 +000090 if (flash->spi->max_write_size)
91 chunk_len = min(chunk_len, flash->spi->max_write_size);
92
Mike Frysingerd4aa5002011-04-25 06:58:29 +000093 cmd[1] = page_addr >> 8;
94 cmd[2] = page_addr;
95 cmd[3] = byte_addr;
96
97 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
98 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
99
100 ret = spi_flash_cmd_write_enable(flash);
101 if (ret < 0) {
102 debug("SF: enabling write failed\n");
103 break;
104 }
105
106 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
107 buf + actual, chunk_len);
108 if (ret < 0) {
109 debug("SF: write failed\n");
110 break;
111 }
112
113 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
114 if (ret)
115 break;
116
Simon Glass1e566bc2013-03-11 06:08:06 +0000117 byte_addr += chunk_len;
118 if (byte_addr == page_size) {
119 page_addr++;
120 byte_addr = 0;
121 }
Mike Frysingerd4aa5002011-04-25 06:58:29 +0000122 }
123
124 debug("SF: program %s %zu bytes @ %#x\n",
125 ret ? "failure" : "success", len, offset);
126
127 spi_release_bus(flash->spi);
128 return ret;
129}
130
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200131int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
132 size_t cmd_len, void *data, size_t data_len)
133{
134 struct spi_slave *spi = flash->spi;
135 int ret;
136
137 spi_claim_bus(spi);
138 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
139 spi_release_bus(spi);
140
141 return ret;
142}
143
Mike Frysingera4c3b402011-01-10 02:20:14 -0500144int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
145 size_t len, void *data)
146{
147 u8 cmd[5];
148
149 cmd[0] = CMD_READ_ARRAY_FAST;
150 spi_flash_addr(offset, cmd);
151 cmd[4] = 0x00;
152
153 return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
154}
155
Mike Frysinger61630452011-01-10 02:20:12 -0500156int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
157 u8 cmd, u8 poll_bit)
158{
159 struct spi_slave *spi = flash->spi;
160 unsigned long timebase;
161 int ret;
162 u8 status;
163
164 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
165 if (ret) {
166 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
167 return ret;
168 }
169
170 timebase = get_timer(0);
171 do {
Patrick Sestierbd0d19c2011-04-15 14:25:25 +0000172 WATCHDOG_RESET();
173
Mike Frysinger61630452011-01-10 02:20:12 -0500174 ret = spi_xfer(spi, 8, NULL, &status, 0);
175 if (ret)
176 return -1;
177
178 if ((status & poll_bit) == 0)
179 break;
180
181 } while (get_timer(timebase) < timeout);
182
183 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
184
185 if ((status & poll_bit) == 0)
186 return 0;
187
188 /* Timed out */
189 debug("SF: time out!\n");
190 return -1;
191}
192
193int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
194{
195 return spi_flash_cmd_poll_bit(flash, timeout,
196 CMD_READ_STATUS, STATUS_WIP);
197}
198
Mike Frysingerc4e932c2012-03-04 22:35:50 -0500199int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500200{
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500201 u32 start, end, erase_size;
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500202 int ret;
203 u8 cmd[4];
204
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500205 erase_size = flash->sector_size;
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500206 if (offset % erase_size || len % erase_size) {
207 debug("SF: Erase offset/length not multiple of erase size\n");
208 return -1;
209 }
210
211 ret = spi_claim_bus(flash->spi);
212 if (ret) {
213 debug("SF: Unable to claim SPI bus\n");
214 return ret;
215 }
216
Mike Frysingerc4e932c2012-03-04 22:35:50 -0500217 if (erase_size == 4096)
218 cmd[0] = CMD_ERASE_4K;
219 else
220 cmd[0] = CMD_ERASE_64K;
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500221 start = offset;
222 end = start + len;
223
224 while (offset < end) {
225 spi_flash_addr(offset, cmd);
226 offset += erase_size;
227
228 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
229 cmd[2], cmd[3], offset);
230
Mike Frysinger2744a4e2011-04-23 23:05:55 +0000231 ret = spi_flash_cmd_write_enable(flash);
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500232 if (ret)
233 goto out;
234
235 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
236 if (ret)
237 goto out;
238
239 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
240 if (ret)
241 goto out;
242 }
243
Vadim Bendebury1f6734c2011-10-12 11:28:38 +0000244 debug("SF: Successfully erased %zu bytes @ %#x\n", len, start);
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500245
246 out:
247 spi_release_bus(flash->spi);
248 return ret;
249}
250
Mike Frysinger41e17132012-03-04 23:18:17 -0500251int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
252{
253 u8 cmd;
254 int ret;
255
256 ret = spi_flash_cmd_write_enable(flash);
257 if (ret < 0) {
258 debug("SF: enabling write failed\n");
259 return ret;
260 }
261
262 cmd = CMD_WRITE_STATUS;
263 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
264 if (ret) {
265 debug("SF: fail to write status register\n");
266 return ret;
267 }
268
269 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
270 if (ret < 0) {
271 debug("SF: write status register timed out\n");
272 return ret;
273 }
274
275 return 0;
276}
277
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200278/*
279 * The following table holds all device probe functions
280 *
281 * shift: number of continuation bytes before the ID
282 * idcode: the expected IDCODE or 0xff for non JEDEC devices
283 * probe: the function to call
284 *
285 * Non JEDEC devices should be ordered in the table such that
286 * the probe functions with best detection algorithms come first.
287 *
288 * Several matching entries are permitted, they will be tried
289 * in sequence until a probe function returns non NULL.
290 *
291 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
292 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
293 * changed. This is the max number of bytes probe functions may
294 * examine when looking up part-specific identification info.
295 *
296 * Probe functions will be given the idcode buffer starting at their
297 * manu id byte (the "idcode" in the table below). In other words,
298 * all of the continuation bytes will be skipped (the "shift" below).
299 */
300#define IDCODE_CONT_LEN 0
301#define IDCODE_PART_LEN 5
302static const struct {
303 const u8 shift;
304 const u8 idcode;
305 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
306} flashes[] = {
307 /* Keep it sorted by define name */
308#ifdef CONFIG_SPI_FLASH_ATMEL
309 { 0, 0x1f, spi_flash_probe_atmel, },
310#endif
Chong Huangd1d906562010-11-30 03:33:25 -0500311#ifdef CONFIG_SPI_FLASH_EON
312 { 0, 0x1c, spi_flash_probe_eon, },
313#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200314#ifdef CONFIG_SPI_FLASH_MACRONIX
315 { 0, 0xc2, spi_flash_probe_macronix, },
316#endif
317#ifdef CONFIG_SPI_FLASH_SPANSION
318 { 0, 0x01, spi_flash_probe_spansion, },
319#endif
320#ifdef CONFIG_SPI_FLASH_SST
321 { 0, 0xbf, spi_flash_probe_sst, },
322#endif
323#ifdef CONFIG_SPI_FLASH_STMICRO
324 { 0, 0x20, spi_flash_probe_stmicro, },
325#endif
326#ifdef CONFIG_SPI_FLASH_WINBOND
327 { 0, 0xef, spi_flash_probe_winbond, },
328#endif
Reinhard Meyere0987e22010-10-05 16:56:40 +0200329#ifdef CONFIG_SPI_FRAM_RAMTRON
330 { 6, 0xc2, spi_fram_probe_ramtron, },
331# undef IDCODE_CONT_LEN
332# define IDCODE_CONT_LEN 6
333#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200334 /* Keep it sorted by best detection */
335#ifdef CONFIG_SPI_FLASH_STMICRO
336 { 0, 0xff, spi_flash_probe_stmicro, },
337#endif
Reinhard Meyere0987e22010-10-05 16:56:40 +0200338#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
339 { 0, 0xff, spi_fram_probe_ramtron, },
340#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200341};
342#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
343
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200344struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
345 unsigned int max_hz, unsigned int spi_mode)
346{
347 struct spi_slave *spi;
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200348 struct spi_flash *flash = NULL;
349 int ret, i, shift;
350 u8 idcode[IDCODE_LEN], *idp;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200351
352 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
353 if (!spi) {
Mike Frysingerb376bbb2010-04-29 00:35:12 -0400354 printf("SF: Failed to set up slave\n");
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200355 return NULL;
356 }
357
358 ret = spi_claim_bus(spi);
359 if (ret) {
360 debug("SF: Failed to claim SPI bus: %d\n", ret);
361 goto err_claim_bus;
362 }
363
364 /* Read the ID codes */
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200365 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200366 if (ret)
367 goto err_read_id;
368
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200369#ifdef DEBUG
370 printf("SF: Got idcodes\n");
371 print_buffer(0, idcode, 1, sizeof(idcode), 0);
372#endif
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200373
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200374 /* count the number of continuation bytes */
375 for (shift = 0, idp = idcode;
376 shift < IDCODE_CONT_LEN && *idp == 0x7f;
377 ++shift, ++idp)
378 continue;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200379
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200380 /* search the table for matches in shift and id */
381 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
382 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
383 /* we have a match, call probe */
384 flash = flashes[i].probe(spi, idp);
385 if (flash)
386 break;
387 }
388
389 if (!flash) {
390 printf("SF: Unsupported manufacturer %02x\n", *idp);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200391 goto err_manufacturer_probe;
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200392 }
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200393
Mike Frysinger493c3602011-04-12 02:09:28 -0400394 printf("SF: Detected %s with page size ", flash->name);
395 print_size(flash->sector_size, ", total ");
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500396 print_size(flash->size, "\n");
397
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200398 spi_release_bus(spi);
399
400 return flash;
401
402err_manufacturer_probe:
403err_read_id:
404 spi_release_bus(spi);
405err_claim_bus:
406 spi_free_slave(spi);
407 return NULL;
408}
409
Simon Glassb5aec142013-03-11 06:08:02 +0000410void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
411 const char *name)
412{
413 struct spi_flash *flash;
414 void *ptr;
415
416 ptr = malloc(size);
417 if (!ptr) {
418 debug("SF: Failed to allocate memory\n");
419 return NULL;
420 }
421 memset(ptr, '\0', size);
422 flash = (struct spi_flash *)(ptr + offset);
423
424 /* Set up some basic fields - caller will sort out sizes */
425 flash->spi = spi;
426 flash->name = name;
427
428 flash->read = spi_flash_cmd_read_fast;
429 flash->write = spi_flash_cmd_write_multi;
430 flash->erase = spi_flash_cmd_erase;
431
432 return flash;
433}
434
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200435void spi_flash_free(struct spi_flash *flash)
436{
437 spi_free_slave(flash->spi);
438 free(flash);
439}