4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
7 * Licensed under the GPL-2 or later.
13 #include <spi_flash.h>
16 #include "spi_flash_internal.h"
18 static void spi_flash_addr(u32 addr, u8 *cmd)
20 /* cmd[0] is actual command */
26 static 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,
31 unsigned long flags = SPI_XFER_BEGIN;
35 flags |= SPI_XFER_END;
37 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
39 debug("SF: Failed to send command (%zu bytes): %d\n",
41 } else if (data_len != 0) {
42 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
44 debug("SF: Failed to transfer %zu bytes of data: %d\n",
51 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
53 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
56 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
57 size_t cmd_len, void *data, size_t data_len)
59 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
62 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
63 const void *data, size_t data_len)
65 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
68 int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
69 size_t len, const void *buf)
71 unsigned long page_addr, byte_addr, page_size;
72 size_t chunk_len, actual;
76 page_size = flash->page_size;
77 page_addr = offset / page_size;
78 byte_addr = offset % page_size;
80 ret = spi_claim_bus(flash->spi);
82 debug("SF: unable to claim SPI bus\n");
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);
90 if (flash->spi->max_write_size)
91 chunk_len = min(chunk_len, flash->spi->max_write_size);
93 cmd[1] = page_addr >> 8;
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);
100 ret = spi_flash_cmd_write_enable(flash);
102 debug("SF: enabling write failed\n");
106 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
107 buf + actual, chunk_len);
109 debug("SF: write failed\n");
113 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
117 byte_addr += chunk_len;
118 if (byte_addr == page_size) {
124 debug("SF: program %s %zu bytes @ %#x\n",
125 ret ? "failure" : "success", len, offset);
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 cmd[0] = CMD_READ_ARRAY_FAST;
150 spi_flash_addr(offset, cmd);
153 return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
156 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
159 struct spi_slave *spi = flash->spi;
160 unsigned long timebase;
164 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
166 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
170 timebase = get_timer(0);
174 ret = spi_xfer(spi, 8, NULL, &status, 0);
178 if ((status & poll_bit) == 0)
181 } while (get_timer(timebase) < timeout);
183 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
185 if ((status & poll_bit) == 0)
189 debug("SF: time out!\n");
193 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
195 return spi_flash_cmd_poll_bit(flash, timeout,
196 CMD_READ_STATUS, STATUS_WIP);
199 int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
201 u32 start, end, erase_size;
205 erase_size = flash->sector_size;
206 if (offset % erase_size || len % erase_size) {
207 debug("SF: Erase offset/length not multiple of erase size\n");
211 ret = spi_claim_bus(flash->spi);
213 debug("SF: Unable to claim SPI bus\n");
217 if (erase_size == 4096)
218 cmd[0] = CMD_ERASE_4K;
220 cmd[0] = CMD_ERASE_64K;
224 while (offset < end) {
225 spi_flash_addr(offset, cmd);
226 offset += erase_size;
228 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
229 cmd[2], cmd[3], offset);
231 ret = spi_flash_cmd_write_enable(flash);
235 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
239 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
244 debug("SF: Successfully erased %zu bytes @ %#x\n", len, start);
247 spi_release_bus(flash->spi);
251 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
256 ret = spi_flash_cmd_write_enable(flash);
258 debug("SF: enabling write failed\n");
262 cmd = CMD_WRITE_STATUS;
263 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
265 debug("SF: fail to write status register\n");
269 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
271 debug("SF: write status register timed out\n");
279 * The following table holds all device probe functions
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
285 * Non JEDEC devices should be ordered in the table such that
286 * the probe functions with best detection algorithms come first.
288 * Several matching entries are permitted, they will be tried
289 * in sequence until a probe function returns non NULL.
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.
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).
300 #define IDCODE_CONT_LEN 0
301 #define IDCODE_PART_LEN 5
302 static const struct {
305 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
307 /* Keep it sorted by define name */
308 #ifdef CONFIG_SPI_FLASH_ATMEL
309 { 0, 0x1f, spi_flash_probe_atmel, },
311 #ifdef CONFIG_SPI_FLASH_EON
312 { 0, 0x1c, spi_flash_probe_eon, },
314 #ifdef CONFIG_SPI_FLASH_MACRONIX
315 { 0, 0xc2, spi_flash_probe_macronix, },
317 #ifdef CONFIG_SPI_FLASH_SPANSION
318 { 0, 0x01, spi_flash_probe_spansion, },
320 #ifdef CONFIG_SPI_FLASH_SST
321 { 0, 0xbf, spi_flash_probe_sst, },
323 #ifdef CONFIG_SPI_FLASH_STMICRO
324 { 0, 0x20, spi_flash_probe_stmicro, },
326 #ifdef CONFIG_SPI_FLASH_WINBOND
327 { 0, 0xef, spi_flash_probe_winbond, },
329 #ifdef CONFIG_SPI_FRAM_RAMTRON
330 { 6, 0xc2, spi_fram_probe_ramtron, },
331 # undef IDCODE_CONT_LEN
332 # define IDCODE_CONT_LEN 6
334 /* Keep it sorted by best detection */
335 #ifdef CONFIG_SPI_FLASH_STMICRO
336 { 0, 0xff, spi_flash_probe_stmicro, },
338 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
339 { 0, 0xff, spi_fram_probe_ramtron, },
342 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
344 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
345 unsigned int max_hz, unsigned int spi_mode)
347 struct spi_slave *spi;
348 struct spi_flash *flash = NULL;
350 u8 idcode[IDCODE_LEN], *idp;
352 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
354 printf("SF: Failed to set up slave\n");
358 ret = spi_claim_bus(spi);
360 debug("SF: Failed to claim SPI bus: %d\n", ret);
364 /* Read the ID codes */
365 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
370 printf("SF: Got idcodes\n");
371 print_buffer(0, idcode, 1, sizeof(idcode), 0);
374 /* count the number of continuation bytes */
375 for (shift = 0, idp = idcode;
376 shift < IDCODE_CONT_LEN && *idp == 0x7f;
380 /* 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);
390 printf("SF: Unsupported manufacturer %02x\n", *idp);
391 goto err_manufacturer_probe;
394 printf("SF: Detected %s with page size ", flash->name);
395 print_size(flash->sector_size, ", total ");
396 print_size(flash->size, "\n");
398 spi_release_bus(spi);
402 err_manufacturer_probe:
404 spi_release_bus(spi);
410 void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
413 struct spi_flash *flash;
418 debug("SF: Failed to allocate memory\n");
421 memset(ptr, '\0', size);
422 flash = (struct spi_flash *)(ptr + offset);
424 /* Set up some basic fields - caller will sort out sizes */
428 flash->read = spi_flash_cmd_read_fast;
429 flash->write = spi_flash_cmd_write_multi;
430 flash->erase = spi_flash_cmd_erase;
435 void spi_flash_free(struct spi_flash *flash)
437 spi_free_slave(flash->spi);