]> git.sur5r.net Git - u-boot/blob - drivers/mtd/spi/atmel.c
drivers/mtd/spi/eeprom_m95xxx.c: add missing error checking
[u-boot] / drivers / mtd / spi / atmel.c
1 /*
2  * Atmel SPI DataFlash support
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Licensed under the GPL-2 or later.
6  */
7
8 #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
33 struct 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
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;
47 };
48
49 static inline struct atmel_spi_flash *
50 to_atmel_spi_flash(struct spi_flash *flash)
51 {
52         return container_of(flash, struct atmel_spi_flash, flash);
53 }
54
55 static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
56         {
57                 .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         {
105                 .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         },
112 };
113
114 static int at45_wait_ready(struct spi_flash *flash, unsigned long timeout)
115 {
116         struct spi_slave *spi = flash->spi;
117         unsigned long timebase;
118         int ret;
119         u8 cmd = CMD_AT45_READ_STATUS;
120         u8 status;
121
122         timebase = get_timer(0);
123
124         ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
125         if (ret)
126                 return -1;
127
128         do {
129                 ret = spi_xfer(spi, 8, NULL, &status, 0);
130                 if (ret)
131                         return -1;
132
133                 if (status & AT45_STATUS_READY)
134                         break;
135         } while (get_timer(timebase) < timeout);
136
137         /* Deactivate CS */
138         spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
139
140         if (status & AT45_STATUS_READY)
141                 return 0;
142
143         /* Timed out */
144         return -1;
145 }
146
147 /*
148  * Assemble the address part of a command for AT45 devices in
149  * non-power-of-two page size mode.
150  */
151 static void at45_build_address(struct atmel_spi_flash *asf, u8 *cmd, u32 offset)
152 {
153         unsigned long page_addr;
154         unsigned long byte_addr;
155         unsigned long page_size;
156         unsigned int page_shift;
157
158         /*
159          * The "extra" space per page is the power-of-two page size
160          * divided by 32.
161          */
162         page_shift = asf->params->l2_page_size;
163         page_size = (1 << page_shift) + (1 << (page_shift - 5));
164         page_shift++;
165         page_addr = offset / page_size;
166         byte_addr = offset % page_size;
167
168         cmd[0] = page_addr >> (16 - page_shift);
169         cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
170         cmd[2] = byte_addr;
171 }
172
173 static int dataflash_read_fast_p2(struct spi_flash *flash,
174                 u32 offset, size_t len, void *buf)
175 {
176         u8 cmd[5];
177
178         cmd[0] = CMD_READ_ARRAY_FAST;
179         cmd[1] = offset >> 16;
180         cmd[2] = offset >> 8;
181         cmd[3] = offset;
182         cmd[4] = 0x00;
183
184         return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
185 }
186
187 static int dataflash_read_fast_at45(struct spi_flash *flash,
188                 u32 offset, size_t len, void *buf)
189 {
190         struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
191         u8 cmd[5];
192
193         cmd[0] = CMD_READ_ARRAY_FAST;
194         at45_build_address(asf, cmd + 1, offset);
195         cmd[4] = 0x00;
196
197         return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
198 }
199
200 /*
201  * TODO: the two write funcs (_p2/_at45) should get unified ...
202  */
203 static int dataflash_write_p2(struct spi_flash *flash,
204                 u32 offset, size_t len, const void *buf)
205 {
206         struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
207         unsigned long page_size;
208         u32 addr = offset;
209         size_t chunk_len;
210         size_t actual;
211         int ret;
212         u8 cmd[4];
213
214         /*
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.
218          */
219
220         page_size = (1 << asf->params->l2_page_size);
221
222         ret = spi_claim_bus(flash->spi);
223         if (ret) {
224                 debug("SF: Unable to claim SPI bus\n");
225                 return ret;
226         }
227
228         for (actual = 0; actual < len; actual += chunk_len) {
229                 chunk_len = min(len - actual, page_size - (addr % page_size));
230
231                 /* Use the same address bits for both commands */
232                 cmd[0] = CMD_AT45_LOAD_BUF1;
233                 cmd[1] = addr >> 16;
234                 cmd[2] = addr >> 8;
235                 cmd[3] = addr;
236
237                 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
238                                 buf + actual, chunk_len);
239                 if (ret < 0) {
240                         debug("SF: Loading AT45 buffer failed\n");
241                         goto out;
242                 }
243
244                 cmd[0] = CMD_AT45_PROG_BUF1;
245                 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
246                 if (ret < 0) {
247                         debug("SF: AT45 page programming failed\n");
248                         goto out;
249                 }
250
251                 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
252                 if (ret < 0) {
253                         debug("SF: AT45 page programming timed out\n");
254                         goto out;
255                 }
256
257                 addr += chunk_len;
258         }
259
260         debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
261                         len, offset);
262         ret = 0;
263
264 out:
265         spi_release_bus(flash->spi);
266         return ret;
267 }
268
269 static int dataflash_write_at45(struct spi_flash *flash,
270                 u32 offset, size_t len, const void *buf)
271 {
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;
277         size_t chunk_len;
278         size_t actual;
279         int ret;
280         u8 cmd[4];
281
282         /*
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.
286          */
287
288         page_shift = asf->params->l2_page_size;
289         page_size = (1 << page_shift) + (1 << (page_shift - 5));
290         page_shift++;
291         page_addr = offset / page_size;
292         byte_addr = offset % page_size;
293
294         ret = spi_claim_bus(flash->spi);
295         if (ret) {
296                 debug("SF: Unable to claim SPI bus\n");
297                 return ret;
298         }
299
300         for (actual = 0; actual < len; actual += chunk_len) {
301                 chunk_len = min(len - actual, page_size - byte_addr);
302
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);
307                 cmd[3] = byte_addr;
308
309                 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
310                                 buf + actual, chunk_len);
311                 if (ret < 0) {
312                         debug("SF: Loading AT45 buffer failed\n");
313                         goto out;
314                 }
315
316                 cmd[0] = CMD_AT45_PROG_BUF1;
317                 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
318                 if (ret < 0) {
319                         debug("SF: AT45 page programming failed\n");
320                         goto out;
321                 }
322
323                 ret = at45_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
324                 if (ret < 0) {
325                         debug("SF: AT45 page programming timed out\n");
326                         goto out;
327                 }
328
329                 page_addr++;
330                 byte_addr = 0;
331         }
332
333         debug("SF: AT45: Successfully programmed %zu bytes @ 0x%x\n",
334                         len, offset);
335         ret = 0;
336
337 out:
338         spi_release_bus(flash->spi);
339         return ret;
340 }
341
342 /*
343  * TODO: the two erase funcs (_p2/_at45) should get unified ...
344  */
345 int dataflash_erase_p2(struct spi_flash *flash, u32 offset, size_t len)
346 {
347         struct atmel_spi_flash *asf = to_atmel_spi_flash(flash);
348         unsigned long page_size;
349
350         size_t actual;
351         int ret;
352         u8 cmd[4];
353
354         /*
355          * TODO: This function currently uses page erase only. We can
356          * probably speed things up by using block and/or sector erase
357          * when possible.
358          */
359
360         page_size = (1 << asf->params->l2_page_size);
361
362         if (offset % page_size || len % page_size) {
363                 debug("SF: Erase offset/length not multiple of page size\n");
364                 return -1;
365         }
366
367         cmd[0] = CMD_AT45_ERASE_PAGE;
368         cmd[3] = 0x00;
369
370         ret = spi_claim_bus(flash->spi);
371         if (ret) {
372                 debug("SF: Unable to claim SPI bus\n");
373                 return ret;
374         }
375
376         for (actual = 0; actual < len; actual += page_size) {
377                 cmd[1] = offset >> 16;
378                 cmd[2] = offset >> 8;
379
380                 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
381                 if (ret < 0) {
382                         debug("SF: AT45 page erase failed\n");
383                         goto out;
384                 }
385
386                 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
387                 if (ret < 0) {
388                         debug("SF: AT45 page erase timed out\n");
389                         goto out;
390                 }
391
392                 offset += page_size;
393         }
394
395         debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
396                         len, offset);
397         ret = 0;
398
399 out:
400         spi_release_bus(flash->spi);
401         return ret;
402 }
403
404 int dataflash_erase_at45(struct spi_flash *flash, u32 offset, size_t len)
405 {
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;
410         size_t actual;
411         int ret;
412         u8 cmd[4];
413
414         /*
415          * TODO: This function currently uses page erase only. We can
416          * probably speed things up by using block and/or sector erase
417          * when possible.
418          */
419
420         page_shift = asf->params->l2_page_size;
421         page_size = (1 << page_shift) + (1 << (page_shift - 5));
422         page_shift++;
423         page_addr = offset / page_size;
424
425         if (offset % page_size || len % page_size) {
426                 debug("SF: Erase offset/length not multiple of page size\n");
427                 return -1;
428         }
429
430         cmd[0] = CMD_AT45_ERASE_PAGE;
431         cmd[3] = 0x00;
432
433         ret = spi_claim_bus(flash->spi);
434         if (ret) {
435                 debug("SF: Unable to claim SPI bus\n");
436                 return ret;
437         }
438
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);
442
443                 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
444                 if (ret < 0) {
445                         debug("SF: AT45 page erase failed\n");
446                         goto out;
447                 }
448
449                 ret = at45_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
450                 if (ret < 0) {
451                         debug("SF: AT45 page erase timed out\n");
452                         goto out;
453                 }
454
455                 page_addr++;
456         }
457
458         debug("SF: AT45: Successfully erased %zu bytes @ 0x%x\n",
459                         len, offset);
460         ret = 0;
461
462 out:
463         spi_release_bus(flash->spi);
464         return ret;
465 }
466
467 struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
468 {
469         const struct atmel_spi_flash_params *params;
470         unsigned long page_size;
471         unsigned int family;
472         struct atmel_spi_flash *asf;
473         unsigned int i;
474         int ret;
475         u8 status;
476
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])
480                         break;
481         }
482
483         if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
484                 debug("SF: Unsupported DataFlash ID %02x\n",
485                                 idcode[1]);
486                 return NULL;
487         }
488
489         asf = malloc(sizeof(struct atmel_spi_flash));
490         if (!asf) {
491                 debug("SF: Failed to allocate memory\n");
492                 return NULL;
493         }
494
495         asf->params = params;
496         asf->flash.spi = spi;
497         asf->flash.name = params->name;
498
499         /* Assuming power-of-two page size initially. */
500         page_size = 1 << params->l2_page_size;
501
502         family = idcode[1] >> 5;
503
504         switch (family) {
505         case DF_FAMILY_AT45:
506                 /*
507                  * AT45 chips have configurable page size. The status
508                  * register indicates which configuration is active.
509                  */
510                 ret = spi_flash_cmd(spi, CMD_AT45_READ_STATUS, &status, 1);
511                 if (ret)
512                         goto err;
513
514                 debug("SF: AT45 status register: %02x\n", status);
515
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);
521                 } else {
522                         asf->flash.read = dataflash_read_fast_p2;
523                         asf->flash.write = dataflash_write_p2;
524                         asf->flash.erase = dataflash_erase_p2;
525                 }
526
527                 break;
528
529         case DF_FAMILY_AT26F:
530         case DF_FAMILY_AT26DF:
531                 asf->flash.read = dataflash_read_fast_p2;
532                 break;
533
534         default:
535                 debug("SF: Unsupported DataFlash family %u\n", family);
536                 goto err;
537         }
538
539         asf->flash.size = page_size * params->pages_per_block
540                                 * params->blocks_per_sector
541                                 * params->nr_sectors;
542
543         debug("SF: Detected %s with page size %lu, total %u bytes\n",
544                         params->name, page_size, asf->flash.size);
545
546         return &asf->flash;
547
548 err:
549         free(asf);
550         return NULL;
551 }