2 * Atmel SPI DataFlash support
4 * Copyright (C) 2008 Atmel Corporation
11 #include "spi_flash_internal.h"
13 /* AT45-specific commands */
14 #define CMD_AT45_READ_STATUS 0xd7
15 #define CMD_AT45_ERASE_PAGE 0x81
16 #define CMD_AT45_LOAD_PROG_BUF1 0x82
17 #define CMD_AT45_LOAD_BUF1 0x84
18 #define CMD_AT45_LOAD_PROG_BUF2 0x85
19 #define CMD_AT45_LOAD_BUF2 0x87
20 #define CMD_AT45_PROG_BUF1 0x88
21 #define CMD_AT45_PROG_BUF2 0x89
23 /* AT45 status register bits */
24 #define AT45_STATUS_P2_PAGE_SIZE (1 << 0)
25 #define AT45_STATUS_READY (1 << 7)
27 /* DataFlash family IDs, as obtained from the second idcode byte */
28 #define DF_FAMILY_AT26F 0
29 #define DF_FAMILY_AT45 1
30 #define DF_FAMILY_AT26DF 2 /* AT25DF and AT26DF */
32 struct atmel_spi_flash_params {
34 /* Log2 of page size in power-of-two mode */
42 /* spi_flash needs to be first so upper layers can free() it */
43 struct atmel_spi_flash {
44 struct spi_flash flash;
45 const struct atmel_spi_flash_params *params;
48 static inline struct atmel_spi_flash *
49 to_atmel_spi_flash(struct spi_flash *flash)
51 return container_of(flash, struct atmel_spi_flash, flash);
54 static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
59 .blocks_per_sector = 16,
67 .blocks_per_sector = 16,
75 .blocks_per_sector = 32,
83 .blocks_per_sector = 32,
91 .blocks_per_sector = 32,
99 .blocks_per_sector = 64,
101 .name = "AT45DB321D",
106 .pages_per_block = 8,
107 .blocks_per_sector = 32,
109 .name = "AT45DB642D",
113 static int at45_wait_ready(struct spi_flash *flash, unsigned long timeout)
115 struct spi_slave *spi = flash->spi;
116 unsigned long timebase;
118 u8 cmd = CMD_AT45_READ_STATUS;
121 timebase = get_timer(0);
123 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
128 ret = spi_xfer(spi, 8, NULL, &status, 0);
132 if (status & AT45_STATUS_READY)
134 } while (get_timer(timebase) < timeout);
137 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
139 if (status & AT45_STATUS_READY)
147 * Assemble the address part of a command for AT45 devices in
148 * non-power-of-two page size mode.
150 static void at45_build_address(struct atmel_spi_flash *asf, u8 *cmd, u32 offset)
152 unsigned long page_addr;
153 unsigned long byte_addr;
154 unsigned long page_size;
155 unsigned int page_shift;
158 * The "extra" space per page is the power-of-two page size
161 page_shift = asf->params->l2_page_size;
162 page_size = (1 << page_shift) + (1 << (page_shift - 5));
164 page_addr = offset / page_size;
165 byte_addr = offset % page_size;
167 cmd[0] = page_addr >> (16 - page_shift);
168 cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
172 static int dataflash_read_fast_p2(struct spi_flash *flash,
173 u32 offset, size_t len, void *buf)
177 cmd[0] = CMD_READ_ARRAY_FAST;
178 cmd[1] = offset >> 16;
179 cmd[2] = offset >> 8;
183 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
186 static int dataflash_read_fast_at45(struct spi_flash *flash,
187 u32 offset, size_t len, void *buf)
189 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
192 cmd[0] = CMD_READ_ARRAY_FAST;
193 at45_build_address(asf, cmd + 1, offset);
196 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
199 static int dataflash_write_at45(struct spi_flash *flash,
200 u32 offset, size_t len, const void *buf)
202 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
203 unsigned long page_addr;
204 unsigned long byte_addr;
205 unsigned long page_size;
206 unsigned int page_shift;
212 page_shift = asf->params->l2_page_size;
213 page_size = (1 << page_shift) + (1 << (page_shift - 5));
215 page_addr = offset / page_size;
216 byte_addr = offset % page_size;
218 ret = spi_claim_bus(flash->spi);
220 debug("SF: Unable to claim SPI bus\n");
224 for (actual = 0; actual < len; actual += chunk_len) {
225 chunk_len = min(len - actual, page_size - byte_addr);
227 /* Use the same address bits for both commands */
228 cmd[0] = CMD_AT45_LOAD_BUF1;
229 cmd[1] = page_addr >> (16 - page_shift);
230 cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
233 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
234 buf + actual, chunk_len);
236 debug("SF: Loading AT45 buffer failed\n");
240 cmd[0] = CMD_AT45_PROG_BUF1;
241 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
243 debug("SF: AT45 page programming failed\n");
247 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
249 debug("SF: AT45 page programming timed out\n");
257 debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
262 spi_release_bus(flash->spi);
266 int dataflash_erase_at45(struct spi_flash *flash, u32 offset, size_t len)
268 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
269 unsigned long page_addr;
270 unsigned long page_size;
271 unsigned int page_shift;
277 * TODO: This function currently uses page erase only. We can
278 * probably speed things up by using block and/or sector erase
282 page_shift = asf->params->l2_page_size;
283 page_size = (1 << page_shift) + (1 << (page_shift - 5));
285 page_addr = offset / page_size;
287 if (offset % page_size || len % page_size) {
288 debug("SF: Erase offset/length not multiple of page size\n");
292 cmd[0] = CMD_AT45_ERASE_PAGE;
295 ret = spi_claim_bus(flash->spi);
297 debug("SF: Unable to claim SPI bus\n");
301 for (actual = 0; actual < len; actual += page_size) {
302 cmd[1] = page_addr >> (16 - page_shift);
303 cmd[2] = page_addr << (page_shift - 8);
305 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
307 debug("SF: AT45 page erase failed\n");
311 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
313 debug("SF: AT45 page erase timed out\n");
320 debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
325 spi_release_bus(flash->spi);
329 struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
331 const struct atmel_spi_flash_params *params;
332 unsigned long page_size;
334 struct atmel_spi_flash *asf;
339 for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) {
340 params = &atmel_spi_flash_table[i];
341 if (params->idcode1 == idcode[1])
345 if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
346 debug("SF: Unsupported DataFlash ID %02x\n",
351 asf = malloc(sizeof(struct atmel_spi_flash));
353 debug("SF: Failed to allocate memory\n");
357 asf->params = params;
358 asf->flash.spi = spi;
359 asf->flash.name = params->name;
361 /* Assuming power-of-two page size initially. */
362 page_size = 1 << params->l2_page_size;
364 family = idcode[1] >> 5;
369 * AT45 chips have configurable page size. The status
370 * register indicates which configuration is active.
372 ret = spi_flash_cmd(spi, CMD_AT45_READ_STATUS, &status, 1);
376 debug("SF: AT45 status register: %02x\n", status);
378 if (!(status & AT45_STATUS_P2_PAGE_SIZE)) {
379 asf->flash.read = dataflash_read_fast_at45;
380 asf->flash.write = dataflash_write_at45;
381 asf->flash.erase = dataflash_erase_at45;
382 page_size += 1 << (params->l2_page_size - 5);
384 asf->flash.read = dataflash_read_fast_p2;
389 case DF_FAMILY_AT26F:
390 case DF_FAMILY_AT26DF:
391 asf->flash.read = dataflash_read_fast_p2;
395 debug("SF: Unsupported DataFlash family %u\n", family);
399 asf->flash.size = page_size * params->pages_per_block
400 * params->blocks_per_sector
401 * params->nr_sectors;
403 debug("SF: Detected %s with page size %lu, total %u bytes\n",
404 params->name, page_size, asf->flash.size);