4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
7 * Licensed under the GPL-2 or later.
14 #include <spi_flash.h>
17 #include "spi_flash_internal.h"
19 DECLARE_GLOBAL_DATA_PTR;
21 static void spi_flash_addr(u32 addr, u8 *cmd)
23 /* cmd[0] is actual command */
29 static 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,
34 unsigned long flags = SPI_XFER_BEGIN;
38 flags |= SPI_XFER_END;
40 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
42 debug("SF: Failed to send command (%zu bytes): %d\n",
44 } else if (data_len != 0) {
45 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
47 debug("SF: Failed to transfer %zu bytes of data: %d\n",
54 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
56 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
59 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
60 size_t cmd_len, void *data, size_t data_len)
62 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
65 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
66 const void *data, size_t data_len)
68 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
71 int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
72 size_t len, const void *buf)
74 unsigned long page_addr, byte_addr, page_size;
75 size_t chunk_len, actual;
79 page_size = flash->page_size;
81 ret = spi_claim_bus(flash->spi);
83 debug("SF: unable to claim SPI bus\n");
87 cmd[0] = CMD_PAGE_PROGRAM;
88 for (actual = 0; actual < len; actual += chunk_len) {
89 #ifdef CONFIG_SPI_FLASH_BAR
92 bank_sel = offset / SPI_FLASH_16MB_BOUN;
94 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
96 debug("SF: fail to set bank%d\n", bank_sel);
100 page_addr = offset / page_size;
101 byte_addr = offset % page_size;
102 chunk_len = min(len - actual, page_size - byte_addr);
104 if (flash->spi->max_write_size)
105 chunk_len = min(chunk_len, flash->spi->max_write_size);
107 cmd[1] = page_addr >> 8;
111 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
112 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
114 ret = spi_flash_cmd_write_enable(flash);
116 debug("SF: enabling write failed\n");
120 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
121 buf + actual, chunk_len);
123 debug("SF: write failed\n");
127 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
134 spi_release_bus(flash->spi);
138 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
139 size_t cmd_len, void *data, size_t data_len)
141 struct spi_slave *spi = flash->spi;
145 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
146 spi_release_bus(spi);
151 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
152 size_t len, void *data)
154 u8 cmd[5], bank_sel = 0;
155 u32 remain_len, read_len;
158 /* Handle memory-mapped SPI */
159 if (flash->memory_map) {
160 memcpy(data, flash->memory_map + offset, len);
164 cmd[0] = CMD_READ_ARRAY_FAST;
168 #ifdef CONFIG_SPI_FLASH_BAR
169 bank_sel = offset / SPI_FLASH_16MB_BOUN;
171 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
173 debug("SF: fail to set bank%d\n", bank_sel);
177 remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1) - offset);
178 if (len < remain_len)
181 read_len = remain_len;
183 spi_flash_addr(offset, cmd);
185 ret = spi_flash_read_common(flash, cmd, sizeof(cmd),
188 debug("SF: read failed\n");
200 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
203 struct spi_slave *spi = flash->spi;
204 unsigned long timebase;
208 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
210 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
214 timebase = get_timer(0);
218 ret = spi_xfer(spi, 8, NULL, &status, 0);
222 if ((status & poll_bit) == 0)
225 } while (get_timer(timebase) < timeout);
227 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
229 if ((status & poll_bit) == 0)
233 debug("SF: time out!\n");
237 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
239 return spi_flash_cmd_poll_bit(flash, timeout,
240 CMD_READ_STATUS, STATUS_WIP);
243 int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
249 erase_size = flash->sector_size;
250 if (offset % erase_size || len % erase_size) {
251 debug("SF: Erase offset/length not multiple of erase size\n");
255 ret = spi_claim_bus(flash->spi);
257 debug("SF: Unable to claim SPI bus\n");
261 if (erase_size == 4096)
262 cmd[0] = CMD_ERASE_4K;
264 cmd[0] = CMD_ERASE_64K;
267 #ifdef CONFIG_SPI_FLASH_BAR
270 bank_sel = offset / SPI_FLASH_16MB_BOUN;
272 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
274 debug("SF: fail to set bank%d\n", bank_sel);
278 spi_flash_addr(offset, cmd);
280 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
281 cmd[2], cmd[3], offset);
283 ret = spi_flash_cmd_write_enable(flash);
287 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
291 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
295 offset += erase_size;
300 spi_release_bus(flash->spi);
304 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
309 ret = spi_flash_cmd_write_enable(flash);
311 debug("SF: enabling write failed\n");
315 cmd = CMD_WRITE_STATUS;
316 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
318 debug("SF: fail to write status register\n");
322 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
324 debug("SF: write status register timed out\n");
331 #ifdef CONFIG_SPI_FLASH_BAR
332 int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
337 if (flash->bank_curr == bank_sel) {
338 debug("SF: not require to enable bank%d\n", bank_sel);
342 cmd = flash->bank_write_cmd;
343 ret = spi_flash_cmd_write_enable(flash);
345 debug("SF: enabling write failed\n");
349 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &bank_sel, 1);
351 debug("SF: fail to write bank addr register\n");
354 flash->bank_curr = bank_sel;
356 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
358 debug("SF: write bank addr register timed out\n");
365 int spi_flash_bank_config(struct spi_flash *flash, u8 idcode0)
370 /* discover bank cmds */
372 case SPI_FLASH_SPANSION_IDCODE0:
373 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
374 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
376 case SPI_FLASH_STMICRO_IDCODE0:
377 case SPI_FLASH_WINBOND_IDCODE0:
378 flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
379 flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
382 printf("SF: Unsupported bank commands %02x\n", idcode0);
386 /* read the bank reg - on which bank the flash is in currently */
387 cmd = flash->bank_read_cmd;
388 if (flash->size > SPI_FLASH_16MB_BOUN) {
389 if (spi_flash_read_common(flash, &cmd, 1, &curr_bank, 1)) {
390 debug("SF: fail to read bank addr register\n");
393 flash->bank_curr = curr_bank;
395 flash->bank_curr = curr_bank;
402 #ifdef CONFIG_OF_CONTROL
403 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
409 /* If there is no node, do nothing */
410 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
414 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
415 if (addr == FDT_ADDR_T_NONE) {
416 debug("%s: Cannot decode address\n", __func__);
420 if (flash->size != size) {
421 debug("%s: Memory map must cover entire device\n", __func__);
424 flash->memory_map = (void *)addr;
428 #endif /* CONFIG_OF_CONTROL */
431 * The following table holds all device probe functions
433 * shift: number of continuation bytes before the ID
434 * idcode: the expected IDCODE or 0xff for non JEDEC devices
435 * probe: the function to call
437 * Non JEDEC devices should be ordered in the table such that
438 * the probe functions with best detection algorithms come first.
440 * Several matching entries are permitted, they will be tried
441 * in sequence until a probe function returns non NULL.
443 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
444 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
445 * changed. This is the max number of bytes probe functions may
446 * examine when looking up part-specific identification info.
448 * Probe functions will be given the idcode buffer starting at their
449 * manu id byte (the "idcode" in the table below). In other words,
450 * all of the continuation bytes will be skipped (the "shift" below).
452 #define IDCODE_CONT_LEN 0
453 #define IDCODE_PART_LEN 5
454 static const struct {
457 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
459 /* Keep it sorted by define name */
460 #ifdef CONFIG_SPI_FLASH_ATMEL
461 { 0, 0x1f, spi_flash_probe_atmel, },
463 #ifdef CONFIG_SPI_FLASH_EON
464 { 0, 0x1c, spi_flash_probe_eon, },
466 #ifdef CONFIG_SPI_FLASH_MACRONIX
467 { 0, 0xc2, spi_flash_probe_macronix, },
469 #ifdef CONFIG_SPI_FLASH_SPANSION
470 { 0, 0x01, spi_flash_probe_spansion, },
472 #ifdef CONFIG_SPI_FLASH_SST
473 { 0, 0xbf, spi_flash_probe_sst, },
475 #ifdef CONFIG_SPI_FLASH_STMICRO
476 { 0, 0x20, spi_flash_probe_stmicro, },
478 #ifdef CONFIG_SPI_FLASH_WINBOND
479 { 0, 0xef, spi_flash_probe_winbond, },
481 #ifdef CONFIG_SPI_FRAM_RAMTRON
482 { 6, 0xc2, spi_fram_probe_ramtron, },
483 # undef IDCODE_CONT_LEN
484 # define IDCODE_CONT_LEN 6
486 /* Keep it sorted by best detection */
487 #ifdef CONFIG_SPI_FLASH_STMICRO
488 { 0, 0xff, spi_flash_probe_stmicro, },
490 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
491 { 0, 0xff, spi_fram_probe_ramtron, },
494 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
496 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
497 unsigned int max_hz, unsigned int spi_mode)
499 struct spi_slave *spi;
500 struct spi_flash *flash = NULL;
502 u8 idcode[IDCODE_LEN], *idp;
504 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
506 printf("SF: Failed to set up slave\n");
510 ret = spi_claim_bus(spi);
512 debug("SF: Failed to claim SPI bus: %d\n", ret);
516 /* Read the ID codes */
517 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
522 printf("SF: Got idcodes\n");
523 print_buffer(0, idcode, 1, sizeof(idcode), 0);
526 /* count the number of continuation bytes */
527 for (shift = 0, idp = idcode;
528 shift < IDCODE_CONT_LEN && *idp == 0x7f;
532 /* search the table for matches in shift and id */
533 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
534 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
535 /* we have a match, call probe */
536 flash = flashes[i].probe(spi, idp);
542 printf("SF: Unsupported manufacturer %02x\n", *idp);
543 goto err_manufacturer_probe;
546 #ifdef CONFIG_SPI_FLASH_BAR
547 /* Configure the BAR - disover bank cmds and read current bank */
548 ret = spi_flash_bank_config(flash, *idp);
550 goto err_manufacturer_probe;
553 #ifdef CONFIG_OF_CONTROL
554 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
555 debug("SF: FDT decode error\n");
556 goto err_manufacturer_probe;
559 printf("SF: Detected %s with page size ", flash->name);
560 print_size(flash->sector_size, ", total ");
561 print_size(flash->size, "");
562 if (flash->memory_map)
563 printf(", mapped at %p", flash->memory_map);
566 spi_release_bus(spi);
570 err_manufacturer_probe:
572 spi_release_bus(spi);
578 void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
581 struct spi_flash *flash;
586 debug("SF: Failed to allocate memory\n");
589 memset(ptr, '\0', size);
590 flash = (struct spi_flash *)(ptr + offset);
592 /* Set up some basic fields - caller will sort out sizes */
596 flash->read = spi_flash_cmd_read_fast;
597 flash->write = spi_flash_cmd_write_multi;
598 flash->erase = spi_flash_cmd_erase;
603 void spi_flash_free(struct spi_flash *flash)
605 spi_free_slave(flash->spi);