2 * Atmel SPI DataFlash support
4 * Copyright (C) 2008 Atmel Corporation
5 * Licensed under the GPL-2 or later.
10 #include <spi_flash.h>
12 #include "spi_flash_internal.h"
14 /* AT45-specific commands */
15 #define CMD_AT45_READ_STATUS 0xd7
16 #define CMD_AT45_ERASE_PAGE 0x81
17 #define CMD_AT45_LOAD_PROG_BUF1 0x82
18 #define CMD_AT45_LOAD_BUF1 0x84
19 #define CMD_AT45_LOAD_PROG_BUF2 0x85
20 #define CMD_AT45_LOAD_BUF2 0x87
21 #define CMD_AT45_PROG_BUF1 0x88
22 #define CMD_AT45_PROG_BUF2 0x89
24 /* AT45 status register bits */
25 #define AT45_STATUS_P2_PAGE_SIZE (1 << 0)
26 #define AT45_STATUS_READY (1 << 7)
28 /* DataFlash family IDs, as obtained from the second idcode byte */
29 #define DF_FAMILY_AT26F 0
30 #define DF_FAMILY_AT45 1
31 #define DF_FAMILY_AT26DF 2 /* AT25DF and AT26DF */
33 struct atmel_spi_flash_params {
35 /* Log2 of page size in power-of-two mode */
43 /* spi_flash needs to be first so upper layers can free() it */
44 struct atmel_spi_flash {
45 struct spi_flash flash;
46 const struct atmel_spi_flash_params *params;
49 static inline struct atmel_spi_flash *
50 to_atmel_spi_flash(struct spi_flash *flash)
52 return container_of(flash, struct atmel_spi_flash, flash);
55 static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
60 .blocks_per_sector = 16,
68 .blocks_per_sector = 16,
76 .blocks_per_sector = 32,
84 .blocks_per_sector = 32,
92 .blocks_per_sector = 32,
100 .blocks_per_sector = 64,
102 .name = "AT45DB321D",
107 .pages_per_block = 8,
108 .blocks_per_sector = 32,
110 .name = "AT45DB642D",
114 static int at45_wait_ready(struct spi_flash *flash, unsigned long timeout)
116 struct spi_slave *spi = flash->spi;
117 unsigned long timebase;
119 u8 cmd = CMD_AT45_READ_STATUS;
122 timebase = get_timer(0);
124 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
129 ret = spi_xfer(spi, 8, NULL, &status, 0);
133 if (status & AT45_STATUS_READY)
135 } while (get_timer(timebase) < timeout);
138 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
140 if (status & AT45_STATUS_READY)
148 * Assemble the address part of a command for AT45 devices in
149 * non-power-of-two page size mode.
151 static void at45_build_address(struct atmel_spi_flash *asf, u8 *cmd, u32 offset)
153 unsigned long page_addr;
154 unsigned long byte_addr;
155 unsigned long page_size;
156 unsigned int page_shift;
159 * The "extra" space per page is the power-of-two page size
162 page_shift = asf->params->l2_page_size;
163 page_size = (1 << page_shift) + (1 << (page_shift - 5));
165 page_addr = offset / page_size;
166 byte_addr = offset % page_size;
168 cmd[0] = page_addr >> (16 - page_shift);
169 cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
173 static int dataflash_read_fast_p2(struct spi_flash *flash,
174 u32 offset, size_t len, void *buf)
178 cmd[0] = CMD_READ_ARRAY_FAST;
179 cmd[1] = offset >> 16;
180 cmd[2] = offset >> 8;
184 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
187 static int dataflash_read_fast_at45(struct spi_flash *flash,
188 u32 offset, size_t len, void *buf)
190 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
193 cmd[0] = CMD_READ_ARRAY_FAST;
194 at45_build_address(asf, cmd + 1, offset);
197 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
201 * TODO: the two write funcs (_p2/_at45) should get unified ...
203 static int dataflash_write_p2(struct spi_flash *flash,
204 u32 offset, size_t len, const void *buf)
206 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
207 unsigned long page_size;
215 * TODO: This function currently uses only page buffer #1. We can
216 * speed this up by using both buffers and loading one buffer while
217 * the other is being programmed into main memory.
220 page_size = (1 << asf->params->l2_page_size);
222 ret = spi_claim_bus(flash->spi);
224 debug("SF: Unable to claim SPI bus\n");
228 for (actual = 0; actual < len; actual += chunk_len) {
229 chunk_len = min(len - actual, page_size - (addr % page_size));
231 /* Use the same address bits for both commands */
232 cmd[0] = CMD_AT45_LOAD_BUF1;
237 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
238 buf + actual, chunk_len);
240 debug("SF: Loading AT45 buffer failed\n");
244 cmd[0] = CMD_AT45_PROG_BUF1;
245 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
247 debug("SF: AT45 page programming failed\n");
251 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
253 debug("SF: AT45 page programming timed out\n");
260 debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
265 spi_release_bus(flash->spi);
269 static int dataflash_write_at45(struct spi_flash *flash,
270 u32 offset, size_t len, const void *buf)
272 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
273 unsigned long page_addr;
274 unsigned long byte_addr;
275 unsigned long page_size;
276 unsigned int page_shift;
283 * TODO: This function currently uses only page buffer #1. We can
284 * speed this up by using both buffers and loading one buffer while
285 * the other is being programmed into main memory.
288 page_shift = asf->params->l2_page_size;
289 page_size = (1 << page_shift) + (1 << (page_shift - 5));
291 page_addr = offset / page_size;
292 byte_addr = offset % page_size;
294 ret = spi_claim_bus(flash->spi);
296 debug("SF: Unable to claim SPI bus\n");
300 for (actual = 0; actual < len; actual += chunk_len) {
301 chunk_len = min(len - actual, page_size - byte_addr);
303 /* Use the same address bits for both commands */
304 cmd[0] = CMD_AT45_LOAD_BUF1;
305 cmd[1] = page_addr >> (16 - page_shift);
306 cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
309 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
310 buf + actual, chunk_len);
312 debug("SF: Loading AT45 buffer failed\n");
316 cmd[0] = CMD_AT45_PROG_BUF1;
317 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
319 debug("SF: AT45 page programming failed\n");
323 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
325 debug("SF: AT45 page programming timed out\n");
333 debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
338 spi_release_bus(flash->spi);
343 * TODO: the two erase funcs (_p2/_at45) should get unified ...
345 int dataflash_erase_p2(struct spi_flash *flash, u32 offset, size_t len)
347 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
348 unsigned long page_size;
355 * TODO: This function currently uses page erase only. We can
356 * probably speed things up by using block and/or sector erase
360 page_size = (1 << asf->params->l2_page_size);
362 if (offset % page_size || len % page_size) {
363 debug("SF: Erase offset/length not multiple of page size\n");
367 cmd[0] = CMD_AT45_ERASE_PAGE;
370 ret = spi_claim_bus(flash->spi);
372 debug("SF: Unable to claim SPI bus\n");
376 for (actual = 0; actual < len; actual += page_size) {
377 cmd[1] = offset >> 16;
378 cmd[2] = offset >> 8;
380 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
382 debug("SF: AT45 page erase failed\n");
386 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
388 debug("SF: AT45 page erase timed out\n");
395 debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
400 spi_release_bus(flash->spi);
404 int dataflash_erase_at45(struct spi_flash *flash, u32 offset, size_t len)
406 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
407 unsigned long page_addr;
408 unsigned long page_size;
409 unsigned int page_shift;
415 * TODO: This function currently uses page erase only. We can
416 * probably speed things up by using block and/or sector erase
420 page_shift = asf->params->l2_page_size;
421 page_size = (1 << page_shift) + (1 << (page_shift - 5));
423 page_addr = offset / page_size;
425 if (offset % page_size || len % page_size) {
426 debug("SF: Erase offset/length not multiple of page size\n");
430 cmd[0] = CMD_AT45_ERASE_PAGE;
433 ret = spi_claim_bus(flash->spi);
435 debug("SF: Unable to claim SPI bus\n");
439 for (actual = 0; actual < len; actual += page_size) {
440 cmd[1] = page_addr >> (16 - page_shift);
441 cmd[2] = page_addr << (page_shift - 8);
443 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
445 debug("SF: AT45 page erase failed\n");
449 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
451 debug("SF: AT45 page erase timed out\n");
458 debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
463 spi_release_bus(flash->spi);
467 struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
469 const struct atmel_spi_flash_params *params;
470 unsigned long page_size;
472 struct atmel_spi_flash *asf;
477 for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) {
478 params = &atmel_spi_flash_table[i];
479 if (params->idcode1 == idcode[1])
483 if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
484 debug("SF: Unsupported DataFlash ID %02x\n",
489 asf = malloc(sizeof(struct atmel_spi_flash));
491 debug("SF: Failed to allocate memory\n");
495 asf->params = params;
496 asf->flash.spi = spi;
497 asf->flash.name = params->name;
499 /* Assuming power-of-two page size initially. */
500 page_size = 1 << params->l2_page_size;
502 family = idcode[1] >> 5;
507 * AT45 chips have configurable page size. The status
508 * register indicates which configuration is active.
510 ret = spi_flash_cmd(spi, CMD_AT45_READ_STATUS, &status, 1);
514 debug("SF: AT45 status register: %02x\n", status);
516 if (!(status & AT45_STATUS_P2_PAGE_SIZE)) {
517 asf->flash.read = dataflash_read_fast_at45;
518 asf->flash.write = dataflash_write_at45;
519 asf->flash.erase = dataflash_erase_at45;
520 page_size += 1 << (params->l2_page_size - 5);
522 asf->flash.read = dataflash_read_fast_p2;
523 asf->flash.write = dataflash_write_p2;
524 asf->flash.erase = dataflash_erase_p2;
529 case DF_FAMILY_AT26F:
530 case DF_FAMILY_AT26DF:
531 asf->flash.read = dataflash_read_fast_p2;
535 debug("SF: Unsupported DataFlash family %u\n", family);
539 asf->flash.size = page_size * params->pages_per_block
540 * params->blocks_per_sector
541 * params->nr_sectors;
543 debug("SF: Detected %s with page size %lu, total %u bytes\n",
544 params->name, page_size, asf->flash.size);