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 struct atmel_spi_flash {
43 const struct atmel_spi_flash_params *params;
44 struct spi_flash flash;
47 static inline struct atmel_spi_flash *
48 to_atmel_spi_flash(struct spi_flash *flash)
50 return container_of(flash, struct atmel_spi_flash, flash);
53 static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
58 .blocks_per_sector = 32,
64 static int at45_wait_ready(struct spi_flash *flash, unsigned long timeout)
66 struct spi_slave *spi = flash->spi;
67 unsigned long timebase;
69 u8 cmd = CMD_AT45_READ_STATUS;
72 timebase = get_timer(0);
74 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
79 ret = spi_xfer(spi, 8, NULL, &status, 0);
83 if (status & AT45_STATUS_READY)
85 } while (get_timer(timebase) < timeout);
88 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
90 if (status & AT45_STATUS_READY)
98 * Assemble the address part of a command for AT45 devices in
99 * non-power-of-two page size mode.
101 static void at45_build_address(struct atmel_spi_flash *asf, u8 *cmd, u32 offset)
103 unsigned long page_addr;
104 unsigned long byte_addr;
105 unsigned long page_size;
106 unsigned int page_shift;
109 * The "extra" space per page is the power-of-two page size
112 page_shift = asf->params->l2_page_size;
113 page_size = (1 << page_shift) + (1 << (page_shift - 5));
115 page_addr = offset / page_size;
116 byte_addr = offset % page_size;
118 cmd[0] = page_addr >> (16 - page_shift);
119 cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
123 static int dataflash_read_fast_p2(struct spi_flash *flash,
124 u32 offset, size_t len, void *buf)
128 cmd[0] = CMD_READ_ARRAY_FAST;
129 cmd[1] = offset >> 16;
130 cmd[2] = offset >> 8;
134 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
137 static int dataflash_read_fast_at45(struct spi_flash *flash,
138 u32 offset, size_t len, void *buf)
140 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
143 cmd[0] = CMD_READ_ARRAY_FAST;
144 at45_build_address(asf, cmd + 1, offset);
147 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
150 static int dataflash_write_at45(struct spi_flash *flash,
151 u32 offset, size_t len, const void *buf)
153 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
154 unsigned long page_addr;
155 unsigned long byte_addr;
156 unsigned long page_size;
157 unsigned int page_shift;
163 page_shift = asf->params->l2_page_size;
164 page_size = (1 << page_shift) + (1 << (page_shift - 5));
166 page_addr = offset / page_size;
167 byte_addr = offset % page_size;
169 ret = spi_claim_bus(flash->spi);
171 debug("SF: Unable to claim SPI bus\n");
175 for (actual = 0; actual < len; actual += chunk_len) {
176 chunk_len = min(len - actual, page_size - byte_addr);
178 /* Use the same address bits for both commands */
179 cmd[0] = CMD_AT45_LOAD_BUF1;
180 cmd[1] = page_addr >> (16 - page_shift);
181 cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
184 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
185 buf + actual, chunk_len);
187 debug("SF: Loading AT45 buffer failed\n");
191 cmd[0] = CMD_AT45_PROG_BUF1;
192 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
194 debug("SF: AT45 page programming failed\n");
198 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
200 debug("SF: AT45 page programming timed out\n");
208 debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
213 spi_release_bus(flash->spi);
217 int dataflash_erase_at45(struct spi_flash *flash, u32 offset, size_t len)
219 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
220 unsigned long page_addr;
221 unsigned long page_size;
222 unsigned int page_shift;
228 * TODO: This function currently uses page erase only. We can
229 * probably speed things up by using block and/or sector erase
233 page_shift = asf->params->l2_page_size;
234 page_size = (1 << page_shift) + (1 << (page_shift - 5));
236 page_addr = offset / page_size;
238 if (offset % page_size || len % page_size) {
239 debug("SF: Erase offset/length not multiple of page size\n");
243 cmd[0] = CMD_AT45_ERASE_PAGE;
246 ret = spi_claim_bus(flash->spi);
248 debug("SF: Unable to claim SPI bus\n");
252 for (actual = 0; actual < len; actual += page_size) {
253 cmd[1] = page_addr >> (16 - page_shift);
254 cmd[2] = page_addr << (page_shift - 8);
256 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
258 debug("SF: AT45 page erase failed\n");
262 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
264 debug("SF: AT45 page erase timed out\n");
271 debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
276 spi_release_bus(flash->spi);
280 struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
282 const struct atmel_spi_flash_params *params;
283 unsigned long page_size;
285 struct atmel_spi_flash *asf;
290 for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) {
291 params = &atmel_spi_flash_table[i];
292 if (params->idcode1 == idcode[1])
296 if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
297 debug("SF: Unsupported DataFlash ID %02x\n",
302 asf = malloc(sizeof(struct atmel_spi_flash));
304 debug("SF: Failed to allocate memory\n");
308 asf->params = params;
309 asf->flash.spi = spi;
310 asf->flash.name = params->name;
312 /* Assuming power-of-two page size initially. */
313 page_size = 1 << params->l2_page_size;
315 family = idcode[1] >> 5;
320 * AT45 chips have configurable page size. The status
321 * register indicates which configuration is active.
323 ret = spi_flash_cmd(spi, CMD_AT45_READ_STATUS, &status, 1);
327 debug("SF: AT45 status register: %02x\n", status);
329 if (!(status & AT45_STATUS_P2_PAGE_SIZE)) {
330 asf->flash.read = dataflash_read_fast_at45;
331 asf->flash.write = dataflash_write_at45;
332 asf->flash.erase = dataflash_erase_at45;
333 page_size += 1 << (params->l2_page_size - 5);
335 asf->flash.read = dataflash_read_fast_p2;
340 case DF_FAMILY_AT26F:
341 case DF_FAMILY_AT26DF:
342 asf->flash.read = dataflash_read_fast_p2;
346 debug("SF: Unsupported DataFlash family %u\n", family);
350 asf->flash.size = page_size * params->pages_per_block
351 * params->blocks_per_sector
352 * params->nr_sectors;
354 debug("SF: Detected %s with page size %lu, total %u bytes\n",
355 params->name, page_size, asf->flash.size);