blob: 6fa932cbd9c3de70300a136e13136932e7ad49c1 [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>
Simon Glassbb8215f2013-03-11 06:08:08 +000011#include <fdtdec.h>
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020012#include <malloc.h>
13#include <spi.h>
14#include <spi_flash.h>
Patrick Sestierbd0d19c2011-04-15 14:25:25 +000015#include <watchdog.h>
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020016
17#include "spi_flash_internal.h"
18
Simon Glassbb8215f2013-03-11 06:08:08 +000019DECLARE_GLOBAL_DATA_PTR;
20
Mike Frysingere7b44ed2011-01-10 02:20:13 -050021static void spi_flash_addr(u32 addr, u8 *cmd)
22{
23 /* cmd[0] is actual command */
24 cmd[1] = addr >> 16;
25 cmd[2] = addr >> 8;
26 cmd[3] = addr >> 0;
27}
28
Mike Frysinger000044d2011-01-10 02:20:11 -050029static int spi_flash_read_write(struct spi_slave *spi,
30 const u8 *cmd, size_t cmd_len,
31 const u8 *data_out, u8 *data_in,
32 size_t data_len)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020033{
34 unsigned long flags = SPI_XFER_BEGIN;
35 int ret;
36
Mike Frysinger000044d2011-01-10 02:20:11 -050037 if (data_len == 0)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020038 flags |= SPI_XFER_END;
39
Mike Frysinger000044d2011-01-10 02:20:11 -050040 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020041 if (ret) {
Mike Frysinger000044d2011-01-10 02:20:11 -050042 debug("SF: Failed to send command (%zu bytes): %d\n",
43 cmd_len, ret);
44 } else if (data_len != 0) {
45 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020046 if (ret)
Mike Frysinger000044d2011-01-10 02:20:11 -050047 debug("SF: Failed to transfer %zu bytes of data: %d\n",
48 data_len, ret);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020049 }
50
51 return ret;
52}
53
Mike Frysinger000044d2011-01-10 02:20:11 -050054int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
55{
56 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
57}
58
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020059int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
60 size_t cmd_len, void *data, size_t data_len)
61{
Mike Frysinger000044d2011-01-10 02:20:11 -050062 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020063}
64
65int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
66 const void *data, size_t data_len)
67{
Mike Frysinger000044d2011-01-10 02:20:11 -050068 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020069}
70
Mike Frysingerd4aa5002011-04-25 06:58:29 +000071int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
72 size_t len, const void *buf)
73{
74 unsigned long page_addr, byte_addr, page_size;
75 size_t chunk_len, actual;
76 int ret;
77 u8 cmd[4];
78
79 page_size = flash->page_size;
80 page_addr = offset / page_size;
81 byte_addr = offset % page_size;
82
83 ret = spi_claim_bus(flash->spi);
84 if (ret) {
85 debug("SF: unable to claim SPI bus\n");
86 return ret;
87 }
88
89 cmd[0] = CMD_PAGE_PROGRAM;
90 for (actual = 0; actual < len; actual += chunk_len) {
91 chunk_len = min(len - actual, page_size - byte_addr);
92
Simon Glass1e566bc2013-03-11 06:08:06 +000093 if (flash->spi->max_write_size)
94 chunk_len = min(chunk_len, flash->spi->max_write_size);
95
Mike Frysingerd4aa5002011-04-25 06:58:29 +000096 cmd[1] = page_addr >> 8;
97 cmd[2] = page_addr;
98 cmd[3] = byte_addr;
99
100 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
101 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
102
103 ret = spi_flash_cmd_write_enable(flash);
104 if (ret < 0) {
105 debug("SF: enabling write failed\n");
106 break;
107 }
108
109 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
110 buf + actual, chunk_len);
111 if (ret < 0) {
112 debug("SF: write failed\n");
113 break;
114 }
115
116 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
117 if (ret)
118 break;
119
Simon Glass1e566bc2013-03-11 06:08:06 +0000120 byte_addr += chunk_len;
121 if (byte_addr == page_size) {
122 page_addr++;
123 byte_addr = 0;
124 }
Mike Frysingerd4aa5002011-04-25 06:58:29 +0000125 }
126
127 debug("SF: program %s %zu bytes @ %#x\n",
128 ret ? "failure" : "success", len, offset);
129
130 spi_release_bus(flash->spi);
131 return ret;
132}
133
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200134int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
135 size_t cmd_len, void *data, size_t data_len)
136{
137 struct spi_slave *spi = flash->spi;
138 int ret;
139
140 spi_claim_bus(spi);
141 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
142 spi_release_bus(spi);
143
144 return ret;
145}
146
Mike Frysingera4c3b402011-01-10 02:20:14 -0500147int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
148 size_t len, void *data)
149{
150 u8 cmd[5];
151
Simon Glassbb8215f2013-03-11 06:08:08 +0000152 /* Handle memory-mapped SPI */
Jagannadha Sutradharudu Teki0d3b5962013-05-27 10:14:14 +0000153 if (flash->memory_map) {
Simon Glassbb8215f2013-03-11 06:08:08 +0000154 memcpy(data, flash->memory_map + offset, len);
Jagannadha Sutradharudu Teki0d3b5962013-05-27 10:14:14 +0000155 return 0;
156 }
Simon Glassbb8215f2013-03-11 06:08:08 +0000157
Mike Frysingera4c3b402011-01-10 02:20:14 -0500158 cmd[0] = CMD_READ_ARRAY_FAST;
159 spi_flash_addr(offset, cmd);
160 cmd[4] = 0x00;
161
162 return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
163}
164
Mike Frysinger61630452011-01-10 02:20:12 -0500165int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
166 u8 cmd, u8 poll_bit)
167{
168 struct spi_slave *spi = flash->spi;
169 unsigned long timebase;
170 int ret;
171 u8 status;
172
173 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
174 if (ret) {
175 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
176 return ret;
177 }
178
179 timebase = get_timer(0);
180 do {
Patrick Sestierbd0d19c2011-04-15 14:25:25 +0000181 WATCHDOG_RESET();
182
Mike Frysinger61630452011-01-10 02:20:12 -0500183 ret = spi_xfer(spi, 8, NULL, &status, 0);
184 if (ret)
185 return -1;
186
187 if ((status & poll_bit) == 0)
188 break;
189
190 } while (get_timer(timebase) < timeout);
191
192 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
193
194 if ((status & poll_bit) == 0)
195 return 0;
196
197 /* Timed out */
198 debug("SF: time out!\n");
199 return -1;
200}
201
202int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
203{
204 return spi_flash_cmd_poll_bit(flash, timeout,
205 CMD_READ_STATUS, STATUS_WIP);
206}
207
Mike Frysingerc4e932c2012-03-04 22:35:50 -0500208int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500209{
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500210 u32 start, end, erase_size;
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500211 int ret;
212 u8 cmd[4];
213
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500214 erase_size = flash->sector_size;
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500215 if (offset % erase_size || len % erase_size) {
216 debug("SF: Erase offset/length not multiple of erase size\n");
217 return -1;
218 }
219
220 ret = spi_claim_bus(flash->spi);
221 if (ret) {
222 debug("SF: Unable to claim SPI bus\n");
223 return ret;
224 }
225
Mike Frysingerc4e932c2012-03-04 22:35:50 -0500226 if (erase_size == 4096)
227 cmd[0] = CMD_ERASE_4K;
228 else
229 cmd[0] = CMD_ERASE_64K;
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500230 start = offset;
231 end = start + len;
232
233 while (offset < end) {
234 spi_flash_addr(offset, cmd);
235 offset += erase_size;
236
237 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
238 cmd[2], cmd[3], offset);
239
Mike Frysinger2744a4e2011-04-23 23:05:55 +0000240 ret = spi_flash_cmd_write_enable(flash);
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500241 if (ret)
242 goto out;
243
244 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
245 if (ret)
246 goto out;
247
248 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
249 if (ret)
250 goto out;
251 }
252
Vadim Bendebury1f6734c2011-10-12 11:28:38 +0000253 debug("SF: Successfully erased %zu bytes @ %#x\n", len, start);
Mike Frysingere7b44ed2011-01-10 02:20:13 -0500254
255 out:
256 spi_release_bus(flash->spi);
257 return ret;
258}
259
Mike Frysinger41e17132012-03-04 23:18:17 -0500260int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
261{
262 u8 cmd;
263 int ret;
264
265 ret = spi_flash_cmd_write_enable(flash);
266 if (ret < 0) {
267 debug("SF: enabling write failed\n");
268 return ret;
269 }
270
271 cmd = CMD_WRITE_STATUS;
272 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
273 if (ret) {
274 debug("SF: fail to write status register\n");
275 return ret;
276 }
277
278 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
279 if (ret < 0) {
280 debug("SF: write status register timed out\n");
281 return ret;
282 }
283
284 return 0;
285}
286
Simon Glassbb8215f2013-03-11 06:08:08 +0000287#ifdef CONFIG_OF_CONTROL
288int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
289{
290 fdt_addr_t addr;
291 fdt_size_t size;
292 int node;
293
294 /* If there is no node, do nothing */
295 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
296 if (node < 0)
297 return 0;
298
299 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
300 if (addr == FDT_ADDR_T_NONE) {
301 debug("%s: Cannot decode address\n", __func__);
302 return 0;
303 }
304
305 if (flash->size != size) {
306 debug("%s: Memory map must cover entire device\n", __func__);
307 return -1;
308 }
309 flash->memory_map = (void *)addr;
310
311 return 0;
312}
313#endif /* CONFIG_OF_CONTROL */
314
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200315/*
316 * The following table holds all device probe functions
317 *
318 * shift: number of continuation bytes before the ID
319 * idcode: the expected IDCODE or 0xff for non JEDEC devices
320 * probe: the function to call
321 *
322 * Non JEDEC devices should be ordered in the table such that
323 * the probe functions with best detection algorithms come first.
324 *
325 * Several matching entries are permitted, they will be tried
326 * in sequence until a probe function returns non NULL.
327 *
328 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
329 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
330 * changed. This is the max number of bytes probe functions may
331 * examine when looking up part-specific identification info.
332 *
333 * Probe functions will be given the idcode buffer starting at their
334 * manu id byte (the "idcode" in the table below). In other words,
335 * all of the continuation bytes will be skipped (the "shift" below).
336 */
337#define IDCODE_CONT_LEN 0
338#define IDCODE_PART_LEN 5
339static const struct {
340 const u8 shift;
341 const u8 idcode;
342 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
343} flashes[] = {
344 /* Keep it sorted by define name */
345#ifdef CONFIG_SPI_FLASH_ATMEL
346 { 0, 0x1f, spi_flash_probe_atmel, },
347#endif
Chong Huangd1d906562010-11-30 03:33:25 -0500348#ifdef CONFIG_SPI_FLASH_EON
349 { 0, 0x1c, spi_flash_probe_eon, },
350#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200351#ifdef CONFIG_SPI_FLASH_MACRONIX
352 { 0, 0xc2, spi_flash_probe_macronix, },
353#endif
354#ifdef CONFIG_SPI_FLASH_SPANSION
355 { 0, 0x01, spi_flash_probe_spansion, },
356#endif
357#ifdef CONFIG_SPI_FLASH_SST
358 { 0, 0xbf, spi_flash_probe_sst, },
359#endif
360#ifdef CONFIG_SPI_FLASH_STMICRO
361 { 0, 0x20, spi_flash_probe_stmicro, },
362#endif
363#ifdef CONFIG_SPI_FLASH_WINBOND
364 { 0, 0xef, spi_flash_probe_winbond, },
365#endif
Reinhard Meyere0987e22010-10-05 16:56:40 +0200366#ifdef CONFIG_SPI_FRAM_RAMTRON
367 { 6, 0xc2, spi_fram_probe_ramtron, },
368# undef IDCODE_CONT_LEN
369# define IDCODE_CONT_LEN 6
370#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200371 /* Keep it sorted by best detection */
372#ifdef CONFIG_SPI_FLASH_STMICRO
373 { 0, 0xff, spi_flash_probe_stmicro, },
374#endif
Reinhard Meyere0987e22010-10-05 16:56:40 +0200375#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
376 { 0, 0xff, spi_fram_probe_ramtron, },
377#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200378};
379#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
380
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200381struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
382 unsigned int max_hz, unsigned int spi_mode)
383{
384 struct spi_slave *spi;
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200385 struct spi_flash *flash = NULL;
386 int ret, i, shift;
387 u8 idcode[IDCODE_LEN], *idp;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200388
389 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
390 if (!spi) {
Mike Frysingerb376bbb2010-04-29 00:35:12 -0400391 printf("SF: Failed to set up slave\n");
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200392 return NULL;
393 }
394
395 ret = spi_claim_bus(spi);
396 if (ret) {
397 debug("SF: Failed to claim SPI bus: %d\n", ret);
398 goto err_claim_bus;
399 }
400
401 /* Read the ID codes */
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200402 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200403 if (ret)
404 goto err_read_id;
405
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200406#ifdef DEBUG
407 printf("SF: Got idcodes\n");
408 print_buffer(0, idcode, 1, sizeof(idcode), 0);
409#endif
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200410
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200411 /* count the number of continuation bytes */
412 for (shift = 0, idp = idcode;
413 shift < IDCODE_CONT_LEN && *idp == 0x7f;
414 ++shift, ++idp)
415 continue;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200416
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200417 /* search the table for matches in shift and id */
418 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
419 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
420 /* we have a match, call probe */
421 flash = flashes[i].probe(spi, idp);
422 if (flash)
423 break;
424 }
425
426 if (!flash) {
427 printf("SF: Unsupported manufacturer %02x\n", *idp);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200428 goto err_manufacturer_probe;
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200429 }
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200430
Simon Glassbb8215f2013-03-11 06:08:08 +0000431#ifdef CONFIG_OF_CONTROL
432 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
433 debug("SF: FDT decode error\n");
434 goto err_manufacturer_probe;
435 }
436#endif
Mike Frysinger493c3602011-04-12 02:09:28 -0400437 printf("SF: Detected %s with page size ", flash->name);
438 print_size(flash->sector_size, ", total ");
Simon Glassbb8215f2013-03-11 06:08:08 +0000439 print_size(flash->size, "");
440 if (flash->memory_map)
441 printf(", mapped at %p", flash->memory_map);
442 puts("\n");
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500443
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200444 spi_release_bus(spi);
445
446 return flash;
447
448err_manufacturer_probe:
449err_read_id:
450 spi_release_bus(spi);
451err_claim_bus:
452 spi_free_slave(spi);
453 return NULL;
454}
455
Simon Glassb5aec142013-03-11 06:08:02 +0000456void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
457 const char *name)
458{
459 struct spi_flash *flash;
460 void *ptr;
461
462 ptr = malloc(size);
463 if (!ptr) {
464 debug("SF: Failed to allocate memory\n");
465 return NULL;
466 }
467 memset(ptr, '\0', size);
468 flash = (struct spi_flash *)(ptr + offset);
469
470 /* Set up some basic fields - caller will sort out sizes */
471 flash->spi = spi;
472 flash->name = name;
473
474 flash->read = spi_flash_cmd_read_fast;
475 flash->write = spi_flash_cmd_write_multi;
476 flash->erase = spi_flash_cmd_erase;
477
478 return flash;
479}
480
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200481void spi_flash_free(struct spi_flash *flash)
482{
483 spi_free_slave(flash->spi);
484 free(flash);
485}