]> git.sur5r.net Git - u-boot/blob - drivers/mtd/spi/spi_flash.c
f05f8f4d9faae7e18c83c35bb40ec4e798092e50
[u-boot] / drivers / mtd / spi / spi_flash.c
1 /*
2  * SPI flash interface
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6  *
7  * Licensed under the GPL-2 or later.
8  */
9
10 #include <common.h>
11 #include <fdtdec.h>
12 #include <malloc.h>
13 #include <spi.h>
14 #include <spi_flash.h>
15 #include <watchdog.h>
16
17 #include "spi_flash_internal.h"
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 static void spi_flash_addr(u32 addr, u8 *cmd)
22 {
23         /* cmd[0] is actual command */
24         cmd[1] = addr >> 16;
25         cmd[2] = addr >> 8;
26         cmd[3] = addr >> 0;
27 }
28
29 static int spi_flash_read_write(struct spi_slave *spi,
30                                 const u8 *cmd, size_t cmd_len,
31                                 const u8 *data_out, u8 *data_in,
32                                 size_t data_len)
33 {
34         unsigned long flags = SPI_XFER_BEGIN;
35         int ret;
36
37         if (data_len == 0)
38                 flags |= SPI_XFER_END;
39
40         ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
41         if (ret) {
42                 debug("SF: Failed to send command (%zu bytes): %d\n",
43                                 cmd_len, ret);
44         } else if (data_len != 0) {
45                 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
46                 if (ret)
47                         debug("SF: Failed to transfer %zu bytes of data: %d\n",
48                                         data_len, ret);
49         }
50
51         return ret;
52 }
53
54 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
55 {
56         return spi_flash_cmd_read(spi, &cmd, 1, response, len);
57 }
58
59 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
60                 size_t cmd_len, void *data, size_t data_len)
61 {
62         return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
63 }
64
65 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
66                 const void *data, size_t data_len)
67 {
68         return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
69 }
70
71 int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset,
72                 size_t len, const void *buf)
73 {
74         unsigned long page_addr, byte_addr, page_size;
75         size_t chunk_len, actual;
76         int ret;
77         u8 cmd[4];
78
79         page_size = flash->page_size;
80
81         ret = spi_claim_bus(flash->spi);
82         if (ret) {
83                 debug("SF: unable to claim SPI bus\n");
84                 return ret;
85         }
86
87         cmd[0] = CMD_PAGE_PROGRAM;
88         for (actual = 0; actual < len; actual += chunk_len) {
89 #ifdef CONFIG_SPI_FLASH_BAR
90                 u8 bank_sel;
91
92                 bank_sel = offset / SPI_FLASH_16MB_BOUN;
93
94                 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
95                 if (ret) {
96                         debug("SF: fail to set bank%d\n", bank_sel);
97                         return ret;
98                 }
99 #endif
100                 page_addr = offset / page_size;
101                 byte_addr = offset % page_size;
102                 chunk_len = min(len - actual, page_size - byte_addr);
103
104                 if (flash->spi->max_write_size)
105                         chunk_len = min(chunk_len, flash->spi->max_write_size);
106
107                 cmd[1] = page_addr >> 8;
108                 cmd[2] = page_addr;
109                 cmd[3] = byte_addr;
110
111                 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
112                       buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
113
114                 ret = spi_flash_cmd_write_enable(flash);
115                 if (ret < 0) {
116                         debug("SF: enabling write failed\n");
117                         break;
118                 }
119
120                 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
121                                           buf + actual, chunk_len);
122                 if (ret < 0) {
123                         debug("SF: write failed\n");
124                         break;
125                 }
126
127                 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
128                 if (ret)
129                         break;
130
131                 offset += chunk_len;
132         }
133
134         spi_release_bus(flash->spi);
135         return ret;
136 }
137
138 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
139                 size_t cmd_len, void *data, size_t data_len)
140 {
141         struct spi_slave *spi = flash->spi;
142         int ret;
143
144         spi_claim_bus(spi);
145         ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
146         spi_release_bus(spi);
147
148         return ret;
149 }
150
151 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
152                 size_t len, void *data)
153 {
154         u8 cmd[5], bank_sel = 0;
155         u32 remain_len, read_len;
156         int ret = -1;
157
158         /* Handle memory-mapped SPI */
159         if (flash->memory_map) {
160                 memcpy(data, flash->memory_map + offset, len);
161                 return 0;
162         }
163
164         cmd[0] = CMD_READ_ARRAY_FAST;
165         cmd[4] = 0x00;
166
167         while (len) {
168 #ifdef CONFIG_SPI_FLASH_BAR
169                 bank_sel = offset / SPI_FLASH_16MB_BOUN;
170
171                 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
172                 if (ret) {
173                         debug("SF: fail to set bank%d\n", bank_sel);
174                         return ret;
175                 }
176 #endif
177                 remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1) - offset);
178                 if (len < remain_len)
179                         read_len = len;
180                 else
181                         read_len = remain_len;
182
183                 spi_flash_addr(offset, cmd);
184
185                 ret = spi_flash_read_common(flash, cmd, sizeof(cmd),
186                                                         data, read_len);
187                 if (ret < 0) {
188                         debug("SF: read failed\n");
189                         break;
190                 }
191
192                 offset += read_len;
193                 len -= read_len;
194                 data += read_len;
195         }
196
197         return ret;
198 }
199
200 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
201                            u8 cmd, u8 poll_bit)
202 {
203         struct spi_slave *spi = flash->spi;
204         unsigned long timebase;
205         int ret;
206         u8 status;
207
208         ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
209         if (ret) {
210                 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
211                 return ret;
212         }
213
214         timebase = get_timer(0);
215         do {
216                 WATCHDOG_RESET();
217
218                 ret = spi_xfer(spi, 8, NULL, &status, 0);
219                 if (ret)
220                         return -1;
221
222                 if ((status & poll_bit) == 0)
223                         break;
224
225         } while (get_timer(timebase) < timeout);
226
227         spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
228
229         if ((status & poll_bit) == 0)
230                 return 0;
231
232         /* Timed out */
233         debug("SF: time out!\n");
234         return -1;
235 }
236
237 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
238 {
239         return spi_flash_cmd_poll_bit(flash, timeout,
240                 CMD_READ_STATUS, STATUS_WIP);
241 }
242
243 int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
244 {
245         u32 erase_size;
246         int ret;
247         u8 cmd[4];
248
249         erase_size = flash->sector_size;
250         if (offset % erase_size || len % erase_size) {
251                 debug("SF: Erase offset/length not multiple of erase size\n");
252                 return -1;
253         }
254
255         ret = spi_claim_bus(flash->spi);
256         if (ret) {
257                 debug("SF: Unable to claim SPI bus\n");
258                 return ret;
259         }
260
261         if (erase_size == 4096)
262                 cmd[0] = CMD_ERASE_4K;
263         else
264                 cmd[0] = CMD_ERASE_64K;
265
266         while (len) {
267 #ifdef CONFIG_SPI_FLASH_BAR
268                 u8 bank_sel;
269
270                 bank_sel = offset / SPI_FLASH_16MB_BOUN;
271
272                 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
273                 if (ret) {
274                         debug("SF: fail to set bank%d\n", bank_sel);
275                         return ret;
276                 }
277 #endif
278                 spi_flash_addr(offset, cmd);
279
280                 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
281                       cmd[2], cmd[3], offset);
282
283                 ret = spi_flash_cmd_write_enable(flash);
284                 if (ret)
285                         goto out;
286
287                 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
288                 if (ret)
289                         goto out;
290
291                 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
292                 if (ret)
293                         goto out;
294
295                 offset += erase_size;
296                 len -= erase_size;
297         }
298
299  out:
300         spi_release_bus(flash->spi);
301         return ret;
302 }
303
304 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
305 {
306         u8 cmd;
307         int ret;
308
309         ret = spi_flash_cmd_write_enable(flash);
310         if (ret < 0) {
311                 debug("SF: enabling write failed\n");
312                 return ret;
313         }
314
315         cmd = CMD_WRITE_STATUS;
316         ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
317         if (ret) {
318                 debug("SF: fail to write status register\n");
319                 return ret;
320         }
321
322         ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
323         if (ret < 0) {
324                 debug("SF: write status register timed out\n");
325                 return ret;
326         }
327
328         return 0;
329 }
330
331 #ifdef CONFIG_SPI_FLASH_BAR
332 int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
333 {
334         u8 cmd;
335         int ret;
336
337         if (flash->bank_curr == bank_sel) {
338                 debug("SF: not require to enable bank%d\n", bank_sel);
339                 return 0;
340         }
341
342         cmd = flash->bank_write_cmd;
343         ret = spi_flash_cmd_write_enable(flash);
344         if (ret < 0) {
345                 debug("SF: enabling write failed\n");
346                 return ret;
347         }
348
349         ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &bank_sel, 1);
350         if (ret) {
351                 debug("SF: fail to write bank addr register\n");
352                 return ret;
353         }
354         flash->bank_curr = bank_sel;
355
356         ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
357         if (ret < 0) {
358                 debug("SF: write bank addr register timed out\n");
359                 return ret;
360         }
361
362         return 0;
363 }
364
365 int spi_flash_bank_config(struct spi_flash *flash, u8 idcode0)
366 {
367         u8 cmd;
368         u8 curr_bank = 0;
369
370         /* discover bank cmds */
371         switch (idcode0) {
372         case SPI_FLASH_SPANSION_IDCODE0:
373                 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
374                 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
375                 break;
376         case SPI_FLASH_STMICRO_IDCODE0:
377         case SPI_FLASH_WINBOND_IDCODE0:
378                 flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
379                 flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
380                 break;
381         default:
382                 printf("SF: Unsupported bank commands %02x\n", idcode0);
383                 return -1;
384         }
385
386         /* read the bank reg - on which bank the flash is in currently */
387         cmd = flash->bank_read_cmd;
388         if (flash->size > SPI_FLASH_16MB_BOUN) {
389                 if (spi_flash_read_common(flash, &cmd, 1, &curr_bank, 1)) {
390                         debug("SF: fail to read bank addr register\n");
391                         return -1;
392                 }
393                 flash->bank_curr = curr_bank;
394         } else {
395                 flash->bank_curr = curr_bank;
396         }
397
398         return 0;
399 }
400 #endif
401
402 #ifdef CONFIG_OF_CONTROL
403 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
404 {
405         fdt_addr_t addr;
406         fdt_size_t size;
407         int node;
408
409         /* If there is no node, do nothing */
410         node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
411         if (node < 0)
412                 return 0;
413
414         addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
415         if (addr == FDT_ADDR_T_NONE) {
416                 debug("%s: Cannot decode address\n", __func__);
417                 return 0;
418         }
419
420         if (flash->size != size) {
421                 debug("%s: Memory map must cover entire device\n", __func__);
422                 return -1;
423         }
424         flash->memory_map = (void *)addr;
425
426         return 0;
427 }
428 #endif /* CONFIG_OF_CONTROL */
429
430 /*
431  * The following table holds all device probe functions
432  *
433  * shift:  number of continuation bytes before the ID
434  * idcode: the expected IDCODE or 0xff for non JEDEC devices
435  * probe:  the function to call
436  *
437  * Non JEDEC devices should be ordered in the table such that
438  * the probe functions with best detection algorithms come first.
439  *
440  * Several matching entries are permitted, they will be tried
441  * in sequence until a probe function returns non NULL.
442  *
443  * IDCODE_CONT_LEN may be redefined if a device needs to declare a
444  * larger "shift" value.  IDCODE_PART_LEN generally shouldn't be
445  * changed.  This is the max number of bytes probe functions may
446  * examine when looking up part-specific identification info.
447  *
448  * Probe functions will be given the idcode buffer starting at their
449  * manu id byte (the "idcode" in the table below).  In other words,
450  * all of the continuation bytes will be skipped (the "shift" below).
451  */
452 #define IDCODE_CONT_LEN 0
453 #define IDCODE_PART_LEN 5
454 static const struct {
455         const u8 shift;
456         const u8 idcode;
457         struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
458 } flashes[] = {
459         /* Keep it sorted by define name */
460 #ifdef CONFIG_SPI_FLASH_ATMEL
461         { 0, 0x1f, spi_flash_probe_atmel, },
462 #endif
463 #ifdef CONFIG_SPI_FLASH_EON
464         { 0, 0x1c, spi_flash_probe_eon, },
465 #endif
466 #ifdef CONFIG_SPI_FLASH_MACRONIX
467         { 0, 0xc2, spi_flash_probe_macronix, },
468 #endif
469 #ifdef CONFIG_SPI_FLASH_SPANSION
470         { 0, 0x01, spi_flash_probe_spansion, },
471 #endif
472 #ifdef CONFIG_SPI_FLASH_SST
473         { 0, 0xbf, spi_flash_probe_sst, },
474 #endif
475 #ifdef CONFIG_SPI_FLASH_STMICRO
476         { 0, 0x20, spi_flash_probe_stmicro, },
477 #endif
478 #ifdef CONFIG_SPI_FLASH_WINBOND
479         { 0, 0xef, spi_flash_probe_winbond, },
480 #endif
481 #ifdef CONFIG_SPI_FRAM_RAMTRON
482         { 6, 0xc2, spi_fram_probe_ramtron, },
483 # undef IDCODE_CONT_LEN
484 # define IDCODE_CONT_LEN 6
485 #endif
486         /* Keep it sorted by best detection */
487 #ifdef CONFIG_SPI_FLASH_STMICRO
488         { 0, 0xff, spi_flash_probe_stmicro, },
489 #endif
490 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
491         { 0, 0xff, spi_fram_probe_ramtron, },
492 #endif
493 };
494 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
495
496 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
497                 unsigned int max_hz, unsigned int spi_mode)
498 {
499         struct spi_slave *spi;
500         struct spi_flash *flash = NULL;
501         int ret, i, shift;
502         u8 idcode[IDCODE_LEN], *idp;
503
504         spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
505         if (!spi) {
506                 printf("SF: Failed to set up slave\n");
507                 return NULL;
508         }
509
510         ret = spi_claim_bus(spi);
511         if (ret) {
512                 debug("SF: Failed to claim SPI bus: %d\n", ret);
513                 goto err_claim_bus;
514         }
515
516         /* Read the ID codes */
517         ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
518         if (ret)
519                 goto err_read_id;
520
521 #ifdef DEBUG
522         printf("SF: Got idcodes\n");
523         print_buffer(0, idcode, 1, sizeof(idcode), 0);
524 #endif
525
526         /* count the number of continuation bytes */
527         for (shift = 0, idp = idcode;
528              shift < IDCODE_CONT_LEN && *idp == 0x7f;
529              ++shift, ++idp)
530                 continue;
531
532         /* search the table for matches in shift and id */
533         for (i = 0; i < ARRAY_SIZE(flashes); ++i)
534                 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
535                         /* we have a match, call probe */
536                         flash = flashes[i].probe(spi, idp);
537                         if (flash)
538                                 break;
539                 }
540
541         if (!flash) {
542                 printf("SF: Unsupported manufacturer %02x\n", *idp);
543                 goto err_manufacturer_probe;
544         }
545
546 #ifdef CONFIG_SPI_FLASH_BAR
547         /* Configure the BAR - disover bank cmds and read current bank  */
548         ret = spi_flash_bank_config(flash, *idp);
549         if (ret < 0)
550                 goto err_manufacturer_probe;
551 #endif
552
553 #ifdef CONFIG_OF_CONTROL
554         if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
555                 debug("SF: FDT decode error\n");
556                 goto err_manufacturer_probe;
557         }
558 #endif
559         printf("SF: Detected %s with page size ", flash->name);
560         print_size(flash->sector_size, ", total ");
561         print_size(flash->size, "");
562         if (flash->memory_map)
563                 printf(", mapped at %p", flash->memory_map);
564         puts("\n");
565
566         spi_release_bus(spi);
567
568         return flash;
569
570 err_manufacturer_probe:
571 err_read_id:
572         spi_release_bus(spi);
573 err_claim_bus:
574         spi_free_slave(spi);
575         return NULL;
576 }
577
578 void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
579                          const char *name)
580 {
581         struct spi_flash *flash;
582         void *ptr;
583
584         ptr = malloc(size);
585         if (!ptr) {
586                 debug("SF: Failed to allocate memory\n");
587                 return NULL;
588         }
589         memset(ptr, '\0', size);
590         flash = (struct spi_flash *)(ptr + offset);
591
592         /* Set up some basic fields - caller will sort out sizes */
593         flash->spi = spi;
594         flash->name = name;
595
596         flash->read = spi_flash_cmd_read_fast;
597         flash->write = spi_flash_cmd_write_multi;
598         flash->erase = spi_flash_cmd_erase;
599
600         return flash;
601 }
602
603 void spi_flash_free(struct spi_flash *flash)
604 {
605         spi_free_slave(flash->spi);
606         free(flash);
607 }