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