blob: 006f6d5d04fcb2258b3014fa41a32bdbadd8fb56 [file] [log] [blame]
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02001/*
2 * Atmel SPI DataFlash support
3 *
4 * Copyright (C) 2008 Atmel Corporation
Mike Frysinger4166ee52009-10-09 17:12:44 -04005 * Licensed under the GPL-2 or later.
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02006 */
Mike Frysingerf773a1b2009-03-23 23:03:58 -04007
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02008#include <common.h>
9#include <malloc.h>
10#include <spi_flash.h>
11
12#include "spi_flash_internal.h"
13
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
23
24/* AT45 status register bits */
25#define AT45_STATUS_P2_PAGE_SIZE (1 << 0)
26#define AT45_STATUS_READY (1 << 7)
27
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 */
32
33struct atmel_spi_flash_params {
34 u8 idcode1;
35 /* Log2 of page size in power-of-two mode */
36 u8 l2_page_size;
37 u8 pages_per_block;
38 u8 blocks_per_sector;
39 u8 nr_sectors;
40 const char *name;
41};
42
Brad Bozarth68f87182009-01-01 22:45:47 -050043/* spi_flash needs to be first so upper layers can free() it */
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020044struct atmel_spi_flash {
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020045 struct spi_flash flash;
Brad Bozarth68f87182009-01-01 22:45:47 -050046 const struct atmel_spi_flash_params *params;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020047};
48
49static inline struct atmel_spi_flash *
50to_atmel_spi_flash(struct spi_flash *flash)
51{
52 return container_of(flash, struct atmel_spi_flash, flash);
53}
54
55static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
56 {
Jean-Christophe PLAGNIOL-VILLARD6b850a92009-01-04 07:44:07 +010057 .idcode1 = 0x22,
58 .l2_page_size = 8,
59 .pages_per_block = 8,
60 .blocks_per_sector = 16,
61 .nr_sectors = 4,
62 .name = "AT45DB011D",
63 },
64 {
65 .idcode1 = 0x23,
66 .l2_page_size = 8,
67 .pages_per_block = 8,
68 .blocks_per_sector = 16,
69 .nr_sectors = 8,
70 .name = "AT45DB021D",
71 },
72 {
73 .idcode1 = 0x24,
74 .l2_page_size = 8,
75 .pages_per_block = 8,
76 .blocks_per_sector = 32,
77 .nr_sectors = 8,
78 .name = "AT45DB041D",
79 },
80 {
81 .idcode1 = 0x25,
82 .l2_page_size = 8,
83 .pages_per_block = 8,
84 .blocks_per_sector = 32,
85 .nr_sectors = 16,
86 .name = "AT45DB081D",
87 },
88 {
89 .idcode1 = 0x26,
90 .l2_page_size = 9,
91 .pages_per_block = 8,
92 .blocks_per_sector = 32,
93 .nr_sectors = 16,
94 .name = "AT45DB161D",
95 },
96 {
97 .idcode1 = 0x27,
98 .l2_page_size = 9,
99 .pages_per_block = 8,
100 .blocks_per_sector = 64,
101 .nr_sectors = 64,
102 .name = "AT45DB321D",
103 },
104 {
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200105 .idcode1 = 0x28,
106 .l2_page_size = 10,
107 .pages_per_block = 8,
108 .blocks_per_sector = 32,
109 .nr_sectors = 32,
110 .name = "AT45DB642D",
111 },
Bo Shen053a4d12012-08-15 18:44:25 +0000112 {
113 .idcode1 = 0x47,
114 .l2_page_size = 8,
115 .pages_per_block = 16,
116 .blocks_per_sector = 16,
117 .nr_sectors = 64,
118 .name = "AT25DF321",
119 },
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200120};
121
122static int at45_wait_ready(struct spi_flash *flash, unsigned long timeout)
123{
Mike Frysingercdb6a002011-04-11 23:39:28 -0400124 struct spi_slave *spi = flash->spi;
125 unsigned long timebase;
126 int ret;
127 u8 cmd = CMD_AT45_READ_STATUS;
128 u8 status;
129
130 timebase = get_timer(0);
131
132 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
133 if (ret)
134 return -1;
135
136 do {
137 ret = spi_xfer(spi, 8, NULL, &status, 0);
138 if (ret)
139 return -1;
140
141 if (status & AT45_STATUS_READY)
142 break;
143 } while (get_timer(timebase) < timeout);
144
145 /* Deactivate CS */
146 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
147
148 if (status & AT45_STATUS_READY)
149 return 0;
150
151 /* Timed out */
152 return -1;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200153}
154
155/*
156 * Assemble the address part of a command for AT45 devices in
157 * non-power-of-two page size mode.
158 */
159static void at45_build_address(struct atmel_spi_flash *asf, u8 *cmd, u32 offset)
160{
161 unsigned long page_addr;
162 unsigned long byte_addr;
163 unsigned long page_size;
164 unsigned int page_shift;
165
166 /*
167 * The "extra" space per page is the power-of-two page size
168 * divided by 32.
169 */
170 page_shift = asf->params->l2_page_size;
171 page_size = (1 << page_shift) + (1 << (page_shift - 5));
172 page_shift++;
173 page_addr = offset / page_size;
174 byte_addr = offset % page_size;
175
176 cmd[0] = page_addr >> (16 - page_shift);
177 cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
178 cmd[2] = byte_addr;
179}
180
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200181static int dataflash_read_fast_at45(struct spi_flash *flash,
182 u32 offset, size_t len, void *buf)
183{
184 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
185 u8 cmd[5];
186
187 cmd[0] = CMD_READ_ARRAY_FAST;
188 at45_build_address(asf, cmd + 1, offset);
189 cmd[4] = 0x00;
190
191 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
192}
193
Todor I Mollov2a6cc582009-04-04 07:14:44 -0400194/*
195 * TODO: the two write funcs (_p2/_at45) should get unified ...
196 */
197static int dataflash_write_p2(struct spi_flash *flash,
198 u32 offset, size_t len, const void *buf)
199{
200 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
201 unsigned long page_size;
202 u32 addr = offset;
203 size_t chunk_len;
204 size_t actual;
205 int ret;
206 u8 cmd[4];
207
208 /*
209 * TODO: This function currently uses only page buffer #1. We can
210 * speed this up by using both buffers and loading one buffer while
211 * the other is being programmed into main memory.
212 */
213
214 page_size = (1 << asf->params->l2_page_size);
215
216 ret = spi_claim_bus(flash->spi);
217 if (ret) {
218 debug("SF: Unable to claim SPI bus\n");
219 return ret;
220 }
221
222 for (actual = 0; actual < len; actual += chunk_len) {
223 chunk_len = min(len - actual, page_size - (addr % page_size));
224
225 /* Use the same address bits for both commands */
226 cmd[0] = CMD_AT45_LOAD_BUF1;
227 cmd[1] = addr >> 16;
228 cmd[2] = addr >> 8;
229 cmd[3] = addr;
230
231 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
232 buf + actual, chunk_len);
233 if (ret < 0) {
234 debug("SF: Loading AT45 buffer failed\n");
235 goto out;
236 }
237
238 cmd[0] = CMD_AT45_PROG_BUF1;
239 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
240 if (ret < 0) {
241 debug("SF: AT45 page programming failed\n");
242 goto out;
243 }
244
245 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
246 if (ret < 0) {
247 debug("SF: AT45 page programming timed out\n");
248 goto out;
249 }
250
251 addr += chunk_len;
252 }
253
254 debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
255 len, offset);
256 ret = 0;
257
258out:
259 spi_release_bus(flash->spi);
260 return ret;
261}
262
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200263static int dataflash_write_at45(struct spi_flash *flash,
264 u32 offset, size_t len, const void *buf)
265{
266 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
267 unsigned long page_addr;
268 unsigned long byte_addr;
269 unsigned long page_size;
270 unsigned int page_shift;
271 size_t chunk_len;
272 size_t actual;
273 int ret;
274 u8 cmd[4];
275
Todor I Mollov2a6cc582009-04-04 07:14:44 -0400276 /*
277 * TODO: This function currently uses only page buffer #1. We can
278 * speed this up by using both buffers and loading one buffer while
279 * the other is being programmed into main memory.
280 */
281
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200282 page_shift = asf->params->l2_page_size;
283 page_size = (1 << page_shift) + (1 << (page_shift - 5));
284 page_shift++;
285 page_addr = offset / page_size;
286 byte_addr = offset % page_size;
287
288 ret = spi_claim_bus(flash->spi);
289 if (ret) {
290 debug("SF: Unable to claim SPI bus\n");
291 return ret;
292 }
293
294 for (actual = 0; actual < len; actual += chunk_len) {
295 chunk_len = min(len - actual, page_size - byte_addr);
296
297 /* Use the same address bits for both commands */
298 cmd[0] = CMD_AT45_LOAD_BUF1;
299 cmd[1] = page_addr >> (16 - page_shift);
300 cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
301 cmd[3] = byte_addr;
302
303 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
304 buf + actual, chunk_len);
305 if (ret < 0) {
306 debug("SF: Loading AT45 buffer failed\n");
307 goto out;
308 }
309
310 cmd[0] = CMD_AT45_PROG_BUF1;
311 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
312 if (ret < 0) {
313 debug("SF: AT45 page programming failed\n");
314 goto out;
315 }
316
317 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
318 if (ret < 0) {
319 debug("SF: AT45 page programming timed out\n");
320 goto out;
321 }
322
323 page_addr++;
324 byte_addr = 0;
325 }
326
Stefan Roesef2302d42008-08-06 14:05:38 +0200327 debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200328 len, offset);
329 ret = 0;
330
331out:
332 spi_release_bus(flash->spi);
333 return ret;
334}
335
Todor I Mollov2a6cc582009-04-04 07:14:44 -0400336/*
337 * TODO: the two erase funcs (_p2/_at45) should get unified ...
338 */
Mike Frysingerf8f07572011-04-12 01:51:29 -0400339static int dataflash_erase_p2(struct spi_flash *flash, u32 offset, size_t len)
Todor I Mollov2a6cc582009-04-04 07:14:44 -0400340{
341 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
342 unsigned long page_size;
343
344 size_t actual;
345 int ret;
346 u8 cmd[4];
347
348 /*
349 * TODO: This function currently uses page erase only. We can
350 * probably speed things up by using block and/or sector erase
351 * when possible.
352 */
353
354 page_size = (1 << asf->params->l2_page_size);
355
356 if (offset % page_size || len % page_size) {
357 debug("SF: Erase offset/length not multiple of page size\n");
358 return -1;
359 }
360
361 cmd[0] = CMD_AT45_ERASE_PAGE;
362 cmd[3] = 0x00;
363
364 ret = spi_claim_bus(flash->spi);
365 if (ret) {
366 debug("SF: Unable to claim SPI bus\n");
367 return ret;
368 }
369
370 for (actual = 0; actual < len; actual += page_size) {
371 cmd[1] = offset >> 16;
372 cmd[2] = offset >> 8;
373
374 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
375 if (ret < 0) {
376 debug("SF: AT45 page erase failed\n");
377 goto out;
378 }
379
380 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
381 if (ret < 0) {
382 debug("SF: AT45 page erase timed out\n");
383 goto out;
384 }
385
386 offset += page_size;
387 }
388
389 debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
390 len, offset);
391 ret = 0;
392
393out:
394 spi_release_bus(flash->spi);
395 return ret;
396}
397
Mike Frysingerf8f07572011-04-12 01:51:29 -0400398static int dataflash_erase_at45(struct spi_flash *flash, u32 offset, size_t len)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200399{
400 struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
401 unsigned long page_addr;
402 unsigned long page_size;
403 unsigned int page_shift;
404 size_t actual;
405 int ret;
406 u8 cmd[4];
407
408 /*
409 * TODO: This function currently uses page erase only. We can
410 * probably speed things up by using block and/or sector erase
411 * when possible.
412 */
413
414 page_shift = asf->params->l2_page_size;
415 page_size = (1 << page_shift) + (1 << (page_shift - 5));
416 page_shift++;
417 page_addr = offset / page_size;
418
419 if (offset % page_size || len % page_size) {
420 debug("SF: Erase offset/length not multiple of page size\n");
421 return -1;
422 }
423
424 cmd[0] = CMD_AT45_ERASE_PAGE;
425 cmd[3] = 0x00;
426
427 ret = spi_claim_bus(flash->spi);
428 if (ret) {
429 debug("SF: Unable to claim SPI bus\n");
430 return ret;
431 }
432
433 for (actual = 0; actual < len; actual += page_size) {
434 cmd[1] = page_addr >> (16 - page_shift);
435 cmd[2] = page_addr << (page_shift - 8);
436
437 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
438 if (ret < 0) {
439 debug("SF: AT45 page erase failed\n");
440 goto out;
441 }
442
443 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
444 if (ret < 0) {
445 debug("SF: AT45 page erase timed out\n");
446 goto out;
447 }
448
449 page_addr++;
450 }
451
Stefan Roesef2302d42008-08-06 14:05:38 +0200452 debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200453 len, offset);
454 ret = 0;
455
456out:
457 spi_release_bus(flash->spi);
458 return ret;
459}
460
461struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
462{
463 const struct atmel_spi_flash_params *params;
Mike Frysingerb376bbb2010-04-29 00:35:12 -0400464 unsigned page_size;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200465 unsigned int family;
466 struct atmel_spi_flash *asf;
467 unsigned int i;
468 int ret;
469 u8 status;
470
471 for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) {
472 params = &atmel_spi_flash_table[i];
473 if (params->idcode1 == idcode[1])
474 break;
475 }
476
477 if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
478 debug("SF: Unsupported DataFlash ID %02x\n",
479 idcode[1]);
480 return NULL;
481 }
482
483 asf = malloc(sizeof(struct atmel_spi_flash));
484 if (!asf) {
485 debug("SF: Failed to allocate memory\n");
486 return NULL;
487 }
488
489 asf->params = params;
490 asf->flash.spi = spi;
491 asf->flash.name = params->name;
492
493 /* Assuming power-of-two page size initially. */
494 page_size = 1 << params->l2_page_size;
495
496 family = idcode[1] >> 5;
497
498 switch (family) {
499 case DF_FAMILY_AT45:
500 /*
501 * AT45 chips have configurable page size. The status
502 * register indicates which configuration is active.
503 */
504 ret = spi_flash_cmd(spi, CMD_AT45_READ_STATUS, &status, 1);
505 if (ret)
506 goto err;
507
508 debug("SF: AT45 status register: %02x\n", status);
509
510 if (!(status & AT45_STATUS_P2_PAGE_SIZE)) {
511 asf->flash.read = dataflash_read_fast_at45;
512 asf->flash.write = dataflash_write_at45;
513 asf->flash.erase = dataflash_erase_at45;
514 page_size += 1 << (params->l2_page_size - 5);
515 } else {
Mike Frysingera4c3b402011-01-10 02:20:14 -0500516 asf->flash.read = spi_flash_cmd_read_fast;
Todor I Mollov2a6cc582009-04-04 07:14:44 -0400517 asf->flash.write = dataflash_write_p2;
518 asf->flash.erase = dataflash_erase_p2;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200519 }
520
Bo Shen83fd09a2012-08-19 20:32:21 +0000521 asf->flash.page_size = page_size;
522 asf->flash.sector_size = page_size;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200523 break;
524
525 case DF_FAMILY_AT26F:
526 case DF_FAMILY_AT26DF:
Mike Frysingera4c3b402011-01-10 02:20:14 -0500527 asf->flash.read = spi_flash_cmd_read_fast;
Bo Shen83fd09a2012-08-19 20:32:21 +0000528 asf->flash.write = spi_flash_cmd_write_multi;
529 asf->flash.erase = spi_flash_cmd_erase;
530 asf->flash.page_size = page_size;
531 asf->flash.sector_size = 4096;
532 /* clear SPRL# bit for locked flash */
533 spi_flash_cmd_write_status(&asf->flash, 0);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200534 break;
535
536 default:
537 debug("SF: Unsupported DataFlash family %u\n", family);
538 goto err;
539 }
540
541 asf->flash.size = page_size * params->pages_per_block
542 * params->blocks_per_sector
543 * params->nr_sectors;
544
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200545 return &asf->flash;
546
547err:
548 free(asf);
549 return NULL;
550}