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;
80 page_addr = offset / page_size;
81 byte_addr = offset % page_size;
83 ret = spi_claim_bus(flash->spi);
85 debug("SF: unable to claim SPI bus\n");
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);
93 if (flash->spi->max_write_size)
94 chunk_len = min(chunk_len, flash->spi->max_write_size);
96 cmd[1] = page_addr >> 8;
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);
103 ret = spi_flash_cmd_write_enable(flash);
105 debug("SF: enabling write failed\n");
109 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
110 buf + actual, chunk_len);
112 debug("SF: write failed\n");
116 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
120 byte_addr += chunk_len;
121 if (byte_addr == page_size) {
127 spi_release_bus(flash->spi);
131 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
132 size_t cmd_len, void *data, size_t data_len)
134 struct spi_slave *spi = flash->spi;
138 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
139 spi_release_bus(spi);
144 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
145 size_t len, void *data)
149 /* Handle memory-mapped SPI */
150 if (flash->memory_map) {
151 memcpy(data, flash->memory_map + offset, len);
155 cmd[0] = CMD_READ_ARRAY_FAST;
156 spi_flash_addr(offset, cmd);
159 return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
162 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
165 struct spi_slave *spi = flash->spi;
166 unsigned long timebase;
170 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
172 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
176 timebase = get_timer(0);
180 ret = spi_xfer(spi, 8, NULL, &status, 0);
184 if ((status & poll_bit) == 0)
187 } while (get_timer(timebase) < timeout);
189 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
191 if ((status & poll_bit) == 0)
195 debug("SF: time out!\n");
199 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
201 return spi_flash_cmd_poll_bit(flash, timeout,
202 CMD_READ_STATUS, STATUS_WIP);
205 int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
211 erase_size = flash->sector_size;
212 if (offset % erase_size || len % erase_size) {
213 debug("SF: Erase offset/length not multiple of erase size\n");
217 ret = spi_claim_bus(flash->spi);
219 debug("SF: Unable to claim SPI bus\n");
223 if (erase_size == 4096)
224 cmd[0] = CMD_ERASE_4K;
226 cmd[0] = CMD_ERASE_64K;
229 while (offset < end) {
230 spi_flash_addr(offset, cmd);
231 offset += erase_size;
233 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
234 cmd[2], cmd[3], offset);
236 ret = spi_flash_cmd_write_enable(flash);
240 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
244 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
250 spi_release_bus(flash->spi);
254 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
259 ret = spi_flash_cmd_write_enable(flash);
261 debug("SF: enabling write failed\n");
265 cmd = CMD_WRITE_STATUS;
266 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
268 debug("SF: fail to write status register\n");
272 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
274 debug("SF: write status register timed out\n");
281 #ifdef CONFIG_OF_CONTROL
282 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
288 /* If there is no node, do nothing */
289 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
293 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
294 if (addr == FDT_ADDR_T_NONE) {
295 debug("%s: Cannot decode address\n", __func__);
299 if (flash->size != size) {
300 debug("%s: Memory map must cover entire device\n", __func__);
303 flash->memory_map = (void *)addr;
307 #endif /* CONFIG_OF_CONTROL */
310 * The following table holds all device probe functions
312 * shift: number of continuation bytes before the ID
313 * idcode: the expected IDCODE or 0xff for non JEDEC devices
314 * probe: the function to call
316 * Non JEDEC devices should be ordered in the table such that
317 * the probe functions with best detection algorithms come first.
319 * Several matching entries are permitted, they will be tried
320 * in sequence until a probe function returns non NULL.
322 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
323 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
324 * changed. This is the max number of bytes probe functions may
325 * examine when looking up part-specific identification info.
327 * Probe functions will be given the idcode buffer starting at their
328 * manu id byte (the "idcode" in the table below). In other words,
329 * all of the continuation bytes will be skipped (the "shift" below).
331 #define IDCODE_CONT_LEN 0
332 #define IDCODE_PART_LEN 5
333 static const struct {
336 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
338 /* Keep it sorted by define name */
339 #ifdef CONFIG_SPI_FLASH_ATMEL
340 { 0, 0x1f, spi_flash_probe_atmel, },
342 #ifdef CONFIG_SPI_FLASH_EON
343 { 0, 0x1c, spi_flash_probe_eon, },
345 #ifdef CONFIG_SPI_FLASH_MACRONIX
346 { 0, 0xc2, spi_flash_probe_macronix, },
348 #ifdef CONFIG_SPI_FLASH_SPANSION
349 { 0, 0x01, spi_flash_probe_spansion, },
351 #ifdef CONFIG_SPI_FLASH_SST
352 { 0, 0xbf, spi_flash_probe_sst, },
354 #ifdef CONFIG_SPI_FLASH_STMICRO
355 { 0, 0x20, spi_flash_probe_stmicro, },
357 #ifdef CONFIG_SPI_FLASH_WINBOND
358 { 0, 0xef, spi_flash_probe_winbond, },
360 #ifdef CONFIG_SPI_FRAM_RAMTRON
361 { 6, 0xc2, spi_fram_probe_ramtron, },
362 # undef IDCODE_CONT_LEN
363 # define IDCODE_CONT_LEN 6
365 /* Keep it sorted by best detection */
366 #ifdef CONFIG_SPI_FLASH_STMICRO
367 { 0, 0xff, spi_flash_probe_stmicro, },
369 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
370 { 0, 0xff, spi_fram_probe_ramtron, },
373 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
375 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
376 unsigned int max_hz, unsigned int spi_mode)
378 struct spi_slave *spi;
379 struct spi_flash *flash = NULL;
381 u8 idcode[IDCODE_LEN], *idp;
383 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
385 printf("SF: Failed to set up slave\n");
389 ret = spi_claim_bus(spi);
391 debug("SF: Failed to claim SPI bus: %d\n", ret);
395 /* Read the ID codes */
396 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
401 printf("SF: Got idcodes\n");
402 print_buffer(0, idcode, 1, sizeof(idcode), 0);
405 /* count the number of continuation bytes */
406 for (shift = 0, idp = idcode;
407 shift < IDCODE_CONT_LEN && *idp == 0x7f;
411 /* search the table for matches in shift and id */
412 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
413 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
414 /* we have a match, call probe */
415 flash = flashes[i].probe(spi, idp);
421 printf("SF: Unsupported manufacturer %02x\n", *idp);
422 goto err_manufacturer_probe;
425 #ifdef CONFIG_OF_CONTROL
426 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
427 debug("SF: FDT decode error\n");
428 goto err_manufacturer_probe;
431 printf("SF: Detected %s with page size ", flash->name);
432 print_size(flash->sector_size, ", total ");
433 print_size(flash->size, "");
434 if (flash->memory_map)
435 printf(", mapped at %p", flash->memory_map);
438 spi_release_bus(spi);
442 err_manufacturer_probe:
444 spi_release_bus(spi);
450 void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
453 struct spi_flash *flash;
458 debug("SF: Failed to allocate memory\n");
461 memset(ptr, '\0', size);
462 flash = (struct spi_flash *)(ptr + offset);
464 /* Set up some basic fields - caller will sort out sizes */
468 flash->read = spi_flash_cmd_read_fast;
469 flash->write = spi_flash_cmd_write_multi;
470 flash->erase = spi_flash_cmd_erase;
475 void spi_flash_free(struct spi_flash *flash)
477 spi_free_slave(flash->spi);