]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/cfi_flash.c
* Patch by Stephen Williams, 15 July 2004
[karo-tx-uboot.git] / drivers / cfi_flash.c
1 /*
2  * (C) Copyright 2002-2004
3  * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4  *
5  * Copyright (C) 2003 Arabella Software Ltd.
6  * Yuli Barcohen <yuli@arabellasw.com>
7  * Modified to work with AMD flashes
8  *
9  * Copyright (C) 2004
10  * Ed Okerson
11  * Modified to work with little-endian systems.
12  *
13  * See file CREDITS for list of people who contributed to this
14  * project.
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation; either version 2 of
19  * the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29  * MA 02111-1307 USA
30  *
31  * History
32  * 01/20/2004 - combined variants of original driver.
33  * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay)
34  * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud)
35  * 01/27/2004 - Little endian support Ed Okerson
36  *
37  * Tested Architectures
38  * Port Width  Chip Width    # of banks    Flash Chip  Board
39  * 32          16            1             28F128J3    seranoa/eagle
40  * 64          16            1             28F128J3    seranoa/falcon
41  *
42  */
43
44 /* The DEBUG define must be before common to enable debugging */
45 /* #define DEBUG        */
46
47 #include <common.h>
48 #include <asm/processor.h>
49 #include <asm/byteorder.h>
50 #include <linux/byteorder/swab.h>
51 #ifdef  CFG_FLASH_CFI_DRIVER
52
53 /*
54  * This file implements a Common Flash Interface (CFI) driver for U-Boot.
55  * The width of the port and the width of the chips are determined at initialization.
56  * These widths are used to calculate the address for access CFI data structures.
57  * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
58  *
59  * References
60  * JEDEC Standard JESD68 - Common Flash Interface (CFI)
61  * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
62  * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
63  * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
64  *
65  * TODO
66  *
67  * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
68  * Table (ALT) to determine if protection is available
69  *
70  * Add support for other command sets Use the PRI and ALT to determine command set
71  * Verify erase and program timeouts.
72  */
73
74 #ifndef CFG_FLASH_BANKS_LIST
75 #define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
76 #endif
77
78 #define FLASH_CMD_CFI                   0x98
79 #define FLASH_CMD_READ_ID               0x90
80 #define FLASH_CMD_RESET                 0xff
81 #define FLASH_CMD_BLOCK_ERASE           0x20
82 #define FLASH_CMD_ERASE_CONFIRM         0xD0
83 #define FLASH_CMD_WRITE                 0x40
84 #define FLASH_CMD_PROTECT               0x60
85 #define FLASH_CMD_PROTECT_SET           0x01
86 #define FLASH_CMD_PROTECT_CLEAR         0xD0
87 #define FLASH_CMD_CLEAR_STATUS          0x50
88 #define FLASH_CMD_WRITE_TO_BUFFER       0xE8
89 #define FLASH_CMD_WRITE_BUFFER_CONFIRM  0xD0
90
91 #define FLASH_STATUS_DONE               0x80
92 #define FLASH_STATUS_ESS                0x40
93 #define FLASH_STATUS_ECLBS              0x20
94 #define FLASH_STATUS_PSLBS              0x10
95 #define FLASH_STATUS_VPENS              0x08
96 #define FLASH_STATUS_PSS                0x04
97 #define FLASH_STATUS_DPS                0x02
98 #define FLASH_STATUS_R                  0x01
99 #define FLASH_STATUS_PROTECT            0x01
100
101 #define AMD_CMD_RESET                   0xF0
102 #define AMD_CMD_WRITE                   0xA0
103 #define AMD_CMD_ERASE_START             0x80
104 #define AMD_CMD_ERASE_SECTOR            0x30
105 #define AMD_CMD_UNLOCK_START            0xAA
106 #define AMD_CMD_UNLOCK_ACK              0x55
107
108 #define AMD_STATUS_TOGGLE               0x40
109 #define AMD_STATUS_ERROR                0x20
110 #define AMD_ADDR_ERASE_START            0x555
111 #define AMD_ADDR_START                  0x555
112 #define AMD_ADDR_ACK                    0x2AA
113
114 #define FLASH_OFFSET_CFI                0x55
115 #define FLASH_OFFSET_CFI_RESP           0x10
116 #define FLASH_OFFSET_PRIMARY_VENDOR     0x13
117 #define FLASH_OFFSET_WTOUT              0x1F
118 #define FLASH_OFFSET_WBTOUT             0x20
119 #define FLASH_OFFSET_ETOUT              0x21
120 #define FLASH_OFFSET_CETOUT             0x22
121 #define FLASH_OFFSET_WMAX_TOUT          0x23
122 #define FLASH_OFFSET_WBMAX_TOUT         0x24
123 #define FLASH_OFFSET_EMAX_TOUT          0x25
124 #define FLASH_OFFSET_CEMAX_TOUT         0x26
125 #define FLASH_OFFSET_SIZE               0x27
126 #define FLASH_OFFSET_INTERFACE          0x28
127 #define FLASH_OFFSET_BUFFER_SIZE        0x2A
128 #define FLASH_OFFSET_NUM_ERASE_REGIONS  0x2C
129 #define FLASH_OFFSET_ERASE_REGIONS      0x2D
130 #define FLASH_OFFSET_PROTECT            0x02
131 #define FLASH_OFFSET_USER_PROTECTION    0x85
132 #define FLASH_OFFSET_INTEL_PROTECTION   0x81
133
134
135 #define FLASH_MAN_CFI                   0x01000000
136
137 #define CFI_CMDSET_NONE             0
138 #define CFI_CMDSET_INTEL_EXTENDED   1
139 #define CFI_CMDSET_AMD_STANDARD     2
140 #define CFI_CMDSET_INTEL_STANDARD   3
141 #define CFI_CMDSET_AMD_EXTENDED     4
142 #define CFI_CMDSET_MITSU_STANDARD   256
143 #define CFI_CMDSET_MITSU_EXTENDED   257
144 #define CFI_CMDSET_SST              258
145
146
147 typedef union {
148         unsigned char c;
149         unsigned short w;
150         unsigned long l;
151         unsigned long long ll;
152 } cfiword_t;
153
154 typedef union {
155         volatile unsigned char *cp;
156         volatile unsigned short *wp;
157         volatile unsigned long *lp;
158         volatile unsigned long long *llp;
159 } cfiptr_t;
160
161 #define NUM_ERASE_REGIONS 4
162
163 static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
164
165 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];   /* info for FLASH chips   */
166
167 /*-----------------------------------------------------------------------
168  * Functions
169  */
170
171 typedef unsigned long flash_sect_t;
172
173 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
174 static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
175 static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
176 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
177 static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
178 static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
179 static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
180 static int flash_detect_cfi (flash_info_t * info);
181 static ulong flash_get_size (ulong base, int banknum);
182 static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
183 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
184                                     ulong tout, char *prompt);
185 #ifdef CFG_FLASH_USE_BUFFER_WRITE
186 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
187 #endif
188
189 /*-----------------------------------------------------------------------
190  * create an address based on the offset and the port width
191  */
192 inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
193 {
194         return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
195 }
196
197 #ifdef DEBUG
198 /*-----------------------------------------------------------------------
199  * Debug support
200  */
201 void print_longlong (char *str, unsigned long long data)
202 {
203         int i;
204         char *cp;
205
206         cp = (unsigned char *) &data;
207         for (i = 0; i < 8; i++)
208                 sprintf (&str[i * 2], "%2.2x", *cp++);
209 }
210 static void flash_printqry (flash_info_t * info, flash_sect_t sect)
211 {
212         cfiptr_t cptr;
213         int x, y;
214
215         for (x = 0; x < 0x40; x += 16 / info->portwidth) {
216                 cptr.cp =
217                         flash_make_addr (info, sect,
218                                          x + FLASH_OFFSET_CFI_RESP);
219                 debug ("%p : ", cptr.cp);
220                 for (y = 0; y < 16; y++) {
221                         debug ("%2.2x ", cptr.cp[y]);
222                 }
223                 debug (" ");
224                 for (y = 0; y < 16; y++) {
225                         if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
226                                 debug ("%c", cptr.cp[y]);
227                         } else {
228                                 debug (".");
229                         }
230                 }
231                 debug ("\n");
232         }
233 }
234 #endif
235
236
237 /*-----------------------------------------------------------------------
238  * read a character at a port width address
239  */
240 inline uchar flash_read_uchar (flash_info_t * info, uint offset)
241 {
242         uchar *cp;
243
244         cp = flash_make_addr (info, 0, offset);
245 #if defined(__LITTLE_ENDIAN)
246         return (cp[0]);
247 #else
248         return (cp[info->portwidth - 1]);
249 #endif
250 }
251
252 /*-----------------------------------------------------------------------
253  * read a short word by swapping for ppc format.
254  */
255 ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
256 {
257         uchar *addr;
258         ushort retval;
259
260 #ifdef DEBUG
261         int x;
262 #endif
263         addr = flash_make_addr (info, sect, offset);
264
265 #ifdef DEBUG
266         debug ("ushort addr is at %p info->portwidth = %d\n", addr,
267                info->portwidth);
268         for (x = 0; x < 2 * info->portwidth; x++) {
269                 debug ("addr[%x] = 0x%x\n", x, addr[x]);
270         }
271 #endif
272 #if defined(__LITTLE_ENDIAN)
273         retval = ((addr[(info->portwidth)] << 8) | addr[0]);
274 #else
275         retval = ((addr[(2 * info->portwidth) - 1] << 8) |
276                   addr[info->portwidth - 1]);
277 #endif
278
279         debug ("retval = 0x%x\n", retval);
280         return retval;
281 }
282
283 /*-----------------------------------------------------------------------
284  * read a long word by picking the least significant byte of each maiximum
285  * port size word. Swap for ppc format.
286  */
287 ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
288 {
289         uchar *addr;
290         ulong retval;
291
292 #ifdef DEBUG
293         int x;
294 #endif
295         addr = flash_make_addr (info, sect, offset);
296
297 #ifdef DEBUG
298         debug ("long addr is at %p info->portwidth = %d\n", addr,
299                info->portwidth);
300         for (x = 0; x < 4 * info->portwidth; x++) {
301                 debug ("addr[%x] = 0x%x\n", x, addr[x]);
302         }
303 #endif
304 #if defined(__LITTLE_ENDIAN)
305         retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
306                 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
307 #else
308         retval = (addr[(2 * info->portwidth) - 1] << 24) |
309                 (addr[(info->portwidth) - 1] << 16) |
310                 (addr[(4 * info->portwidth) - 1] << 8) |
311                 addr[(3 * info->portwidth) - 1];
312 #endif
313         return retval;
314 }
315
316 /*-----------------------------------------------------------------------
317  */
318 unsigned long flash_init (void)
319 {
320         unsigned long size = 0;
321         int i;
322
323         /* Init: no FLASHes known */
324         for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
325                 flash_info[i].flash_id = FLASH_UNKNOWN;
326                 size += flash_info[i].size = flash_get_size (bank_base[i], i);
327                 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
328                         printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
329                                 i, flash_info[i].size, flash_info[i].size << 20);
330                 }
331         }
332
333         /* Monitor protection ON by default */
334 #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
335         flash_protect (FLAG_PROTECT_SET,
336                        CFG_MONITOR_BASE,
337                        CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
338                        &flash_info[0]);
339 #endif
340
341         return (size);
342 }
343
344 /*-----------------------------------------------------------------------
345  */
346 int flash_erase (flash_info_t * info, int s_first, int s_last)
347 {
348         int rcode = 0;
349         int prot;
350         flash_sect_t sect;
351
352         if (info->flash_id != FLASH_MAN_CFI) {
353                 puts ("Can't erase unknown flash type - aborted\n");
354                 return 1;
355         }
356         if ((s_first < 0) || (s_first > s_last)) {
357                 puts ("- no sectors to erase\n");
358                 return 1;
359         }
360
361         prot = 0;
362         for (sect = s_first; sect <= s_last; ++sect) {
363                 if (info->protect[sect]) {
364                         prot++;
365                 }
366         }
367         if (prot) {
368                 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
369         } else {
370                 putc ('\n');
371         }
372
373
374         for (sect = s_first; sect <= s_last; sect++) {
375                 if (info->protect[sect] == 0) { /* not protected */
376                         switch (info->vendor) {
377                         case CFI_CMDSET_INTEL_STANDARD:
378                         case CFI_CMDSET_INTEL_EXTENDED:
379                                 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
380                                 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
381                                 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
382                                 break;
383                         case CFI_CMDSET_AMD_STANDARD:
384                         case CFI_CMDSET_AMD_EXTENDED:
385                                 flash_unlock_seq (info, sect);
386                                 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
387                                                         AMD_CMD_ERASE_START);
388                                 flash_unlock_seq (info, sect);
389                                 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
390                                 break;
391                         default:
392                                 debug ("Unkown flash vendor %d\n",
393                                        info->vendor);
394                                 break;
395                         }
396
397                         if (flash_full_status_check
398                             (info, sect, info->erase_blk_tout, "erase")) {
399                                 rcode = 1;
400                         } else
401                                 putc ('.');
402                 }
403         }
404         puts (" done\n");
405         return rcode;
406 }
407
408 /*-----------------------------------------------------------------------
409  */
410 void flash_print_info (flash_info_t * info)
411 {
412         int i;
413
414         if (info->flash_id != FLASH_MAN_CFI) {
415                 puts ("missing or unknown FLASH type\n");
416                 return;
417         }
418
419         printf ("CFI conformant FLASH (%d x %d)",
420                 (info->portwidth << 3), (info->chipwidth << 3));
421         printf ("  Size: %ld MB in %d Sectors\n",
422                 info->size >> 20, info->sector_count);
423         printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
424                 info->erase_blk_tout,
425                 info->write_tout,
426                 info->buffer_write_tout,
427                 info->buffer_size);
428
429         puts ("  Sector Start Addresses:");
430         for (i = 0; i < info->sector_count; ++i) {
431 #ifdef CFG_FLASH_EMPTY_INFO
432                 int k;
433                 int size;
434                 int erased;
435                 volatile unsigned long *flash;
436
437                 /*
438                  * Check if whole sector is erased
439                  */
440                 if (i != (info->sector_count - 1))
441                         size = info->start[i + 1] - info->start[i];
442                 else
443                         size = info->start[0] + info->size - info->start[i];
444                 erased = 1;
445                 flash = (volatile unsigned long *) info->start[i];
446                 size = size >> 2;       /* divide by 4 for longword access */
447                 for (k = 0; k < size; k++) {
448                         if (*flash++ != 0xffffffff) {
449                                 erased = 0;
450                                 break;
451                         }
452                 }
453
454                 if ((i % 5) == 0)
455                         printf ("\n");
456                 /* print empty and read-only info */
457                 printf (" %08lX%s%s",
458                         info->start[i],
459                         erased ? " E" : "  ",
460                         info->protect[i] ? "RO " : "   ");
461 #else
462                 if ((i % 5) == 0)
463                         printf ("\n   ");
464                 printf (" %08lX%s",
465                         info->start[i], info->protect[i] ? " (RO)" : "     ");
466 #endif
467         }
468         putc ('\n');
469         return;
470 }
471
472 /*-----------------------------------------------------------------------
473  * Copy memory to flash, returns:
474  * 0 - OK
475  * 1 - write timeout
476  * 2 - Flash not erased
477  */
478 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
479 {
480         ulong wp;
481         ulong cp;
482         int aln;
483         cfiword_t cword;
484         int i, rc;
485
486 #ifdef CFG_FLASH_USE_BUFFER_WRITE
487         int buffered_size;
488 #endif
489         /* get lower aligned address */
490         /* get lower aligned address */
491         wp = (addr & ~(info->portwidth - 1));
492
493         /* handle unaligned start */
494         if ((aln = addr - wp) != 0) {
495                 cword.l = 0;
496                 cp = wp;
497                 for (i = 0; i < aln; ++i, ++cp)
498                         flash_add_byte (info, &cword, (*(uchar *) cp));
499
500                 for (; (i < info->portwidth) && (cnt > 0); i++) {
501                         flash_add_byte (info, &cword, *src++);
502                         cnt--;
503                         cp++;
504                 }
505                 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
506                         flash_add_byte (info, &cword, (*(uchar *) cp));
507                 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
508                         return rc;
509                 wp = cp;
510         }
511
512         /* handle the aligned part */
513 #ifdef CFG_FLASH_USE_BUFFER_WRITE
514         buffered_size = (info->portwidth / info->chipwidth);
515         buffered_size *= info->buffer_size;
516         while (cnt >= info->portwidth) {
517                 i = buffered_size > cnt ? cnt : buffered_size;
518                 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
519                         return rc;
520                 i -= (i % info->portwidth);
521                 wp += i;
522                 src += i;
523                 cnt -= i;
524         }
525 #else
526         while (cnt >= info->portwidth) {
527                 cword.l = 0;
528                 for (i = 0; i < info->portwidth; i++) {
529                         flash_add_byte (info, &cword, *src++);
530                 }
531                 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
532                         return rc;
533                 wp += info->portwidth;
534                 cnt -= info->portwidth;
535         }
536 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
537         if (cnt == 0) {
538                 return (0);
539         }
540
541         /*
542          * handle unaligned tail bytes
543          */
544         cword.l = 0;
545         for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
546                 flash_add_byte (info, &cword, *src++);
547                 --cnt;
548         }
549         for (; i < info->portwidth; ++i, ++cp) {
550                 flash_add_byte (info, &cword, (*(uchar *) cp));
551         }
552
553         return flash_write_cfiword (info, wp, cword);
554 }
555
556 /*-----------------------------------------------------------------------
557  */
558 #ifdef CFG_FLASH_PROTECTION
559
560 int flash_real_protect (flash_info_t * info, long sector, int prot)
561 {
562         int retcode = 0;
563
564         flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
565         flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
566         if (prot)
567                 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
568         else
569                 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
570
571         if ((retcode =
572              flash_full_status_check (info, sector, info->erase_blk_tout,
573                                       prot ? "protect" : "unprotect")) == 0) {
574
575                 info->protect[sector] = prot;
576                 /* Intel's unprotect unprotects all locking */
577                 if (prot == 0) {
578                         flash_sect_t i;
579
580                         for (i = 0; i < info->sector_count; i++) {
581                                 if (info->protect[i])
582                                         flash_real_protect (info, i, 1);
583                         }
584                 }
585         }
586         return retcode;
587 }
588
589 /*-----------------------------------------------------------------------
590  * flash_read_user_serial - read the OneTimeProgramming cells
591  */
592 void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
593                              int len)
594 {
595         uchar *src;
596         uchar *dst;
597
598         dst = buffer;
599         src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
600         flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
601         memcpy (dst, src + offset, len);
602         flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
603 }
604
605 /*
606  * flash_read_factory_serial - read the device Id from the protection area
607  */
608 void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
609                                 int len)
610 {
611         uchar *src;
612
613         src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
614         flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
615         memcpy (buffer, src + offset, len);
616         flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
617 }
618
619 #endif /* CFG_FLASH_PROTECTION */
620
621 /*
622  * flash_is_busy - check to see if the flash is busy
623  * This routine checks the status of the chip and returns true if the chip is busy
624  */
625 static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
626 {
627         int retval;
628
629         switch (info->vendor) {
630         case CFI_CMDSET_INTEL_STANDARD:
631         case CFI_CMDSET_INTEL_EXTENDED:
632                 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
633                 break;
634         case CFI_CMDSET_AMD_STANDARD:
635         case CFI_CMDSET_AMD_EXTENDED:
636                 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
637                 break;
638         default:
639                 retval = 0;
640         }
641         debug ("flash_is_busy: %d\n", retval);
642         return retval;
643 }
644
645 /*-----------------------------------------------------------------------
646  *  wait for XSR.7 to be set. Time out with an error if it does not.
647  *  This routine does not set the flash to read-array mode.
648  */
649 static int flash_status_check (flash_info_t * info, flash_sect_t sector,
650                                ulong tout, char *prompt)
651 {
652         ulong start;
653
654         /* Wait for command completion */
655         start = get_timer (0);
656         while (flash_is_busy (info, sector)) {
657                 if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
658                         printf ("Flash %s timeout at address %lx data %lx\n",
659                                 prompt, info->start[sector],
660                                 flash_read_long (info, sector, 0));
661                         flash_write_cmd (info, sector, 0, info->cmd_reset);
662                         return ERR_TIMOUT;
663                 }
664         }
665         return ERR_OK;
666 }
667
668 /*-----------------------------------------------------------------------
669  * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
670  * This routine sets the flash to read-array mode.
671  */
672 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
673                                     ulong tout, char *prompt)
674 {
675         int retcode;
676
677         retcode = flash_status_check (info, sector, tout, prompt);
678         switch (info->vendor) {
679         case CFI_CMDSET_INTEL_EXTENDED:
680         case CFI_CMDSET_INTEL_STANDARD:
681                 if ((retcode != ERR_OK)
682                     && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
683                         retcode = ERR_INVAL;
684                         printf ("Flash %s error at address %lx\n", prompt,
685                                 info->start[sector]);
686                         if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
687                                 puts ("Command Sequence Error.\n");
688                         } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
689                                 puts ("Block Erase Error.\n");
690                                 retcode = ERR_NOT_ERASED;
691                         } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
692                                 puts ("Locking Error\n");
693                         }
694                         if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
695                                 puts ("Block locked.\n");
696                                 retcode = ERR_PROTECTED;
697                         }
698                         if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
699                                 puts ("Vpp Low Error.\n");
700                 }
701                 flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
702                 break;
703         default:
704                 break;
705         }
706         return retcode;
707 }
708
709 /*-----------------------------------------------------------------------
710  */
711 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
712 {
713 #if defined(__LITTLE_ENDIAN)
714         unsigned short  w;
715         unsigned int    l;
716         unsigned long long ll;
717 #endif
718
719         switch (info->portwidth) {
720         case FLASH_CFI_8BIT:
721                 cword->c = c;
722                 break;
723         case FLASH_CFI_16BIT:
724 #if defined(__LITTLE_ENDIAN)
725                 w = c;
726                 w <<= 8;
727                 cword->w = (cword->w >> 8) | w;
728 #else
729                 cword->w = (cword->w << 8) | c;
730 #endif
731                 break;
732         case FLASH_CFI_32BIT:
733 #if defined(__LITTLE_ENDIAN)
734                 l = c;
735                 l <<= 24;
736                 cword->l = (cword->l >> 8) | l;
737 #else
738                 cword->l = (cword->l << 8) | c;
739 #endif
740                 break;
741         case FLASH_CFI_64BIT:
742 #if defined(__LITTLE_ENDIAN)
743                 ll = c;
744                 ll <<= 56;
745                 cword->ll = (cword->ll >> 8) | ll;
746 #else
747                 cword->ll = (cword->ll << 8) | c;
748 #endif
749                 break;
750         }
751 }
752
753
754 /*-----------------------------------------------------------------------
755  * make a proper sized command based on the port and chip widths
756  */
757 static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
758 {
759         int i;
760
761 #if defined(__LITTLE_ENDIAN)
762         ushort stmpw;
763         uint   stmpi;
764 #endif
765         uchar *cp = (uchar *) cmdbuf;
766
767         for (i = 0; i < info->portwidth; i++)
768                 *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
769 #if defined(__LITTLE_ENDIAN)
770         switch (info->portwidth) {
771         case FLASH_CFI_8BIT:
772                 break;
773         case FLASH_CFI_16BIT:
774                 stmpw = *(ushort *) cmdbuf;
775                 *(ushort *) cmdbuf = __swab16 (stmpw);
776                 break;
777         case FLASH_CFI_32BIT:
778                 stmpi = *(uint *) cmdbuf;
779                 *(uint *) cmdbuf = __swab32 (stmpi);
780                 break;
781         default:
782                 puts ("WARNING: flash_make_cmd: unsuppported LittleEndian mode\n");
783                 break;
784         }
785 #endif
786 }
787
788 /*
789  * Write a proper sized command to the correct address
790  */
791 static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
792 {
793
794         volatile cfiptr_t addr;
795         cfiword_t cword;
796
797         addr.cp = flash_make_addr (info, sect, offset);
798         flash_make_cmd (info, cmd, &cword);
799         switch (info->portwidth) {
800         case FLASH_CFI_8BIT:
801                 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
802                        cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
803                 *addr.cp = cword.c;
804                 break;
805         case FLASH_CFI_16BIT:
806                 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
807                        cmd, cword.w,
808                        info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
809                 *addr.wp = cword.w;
810                 break;
811         case FLASH_CFI_32BIT:
812                 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
813                        cmd, cword.l,
814                        info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
815                 *addr.lp = cword.l;
816                 break;
817         case FLASH_CFI_64BIT:
818 #ifdef DEBUG
819                 {
820                         char str[20];
821
822                         print_longlong (str, cword.ll);
823
824                         debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
825                                addr.llp, cmd, str,
826                                info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
827                 }
828 #endif
829                 *addr.llp = cword.ll;
830                 break;
831         }
832 }
833
834 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
835 {
836         flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
837         flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
838 }
839
840 /*-----------------------------------------------------------------------
841  */
842 static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
843 {
844         cfiptr_t cptr;
845         cfiword_t cword;
846         int retval;
847
848         cptr.cp = flash_make_addr (info, sect, offset);
849         flash_make_cmd (info, cmd, &cword);
850
851         debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
852         switch (info->portwidth) {
853         case FLASH_CFI_8BIT:
854                 debug ("is= %x %x\n", cptr.cp[0], cword.c);
855                 retval = (cptr.cp[0] == cword.c);
856                 break;
857         case FLASH_CFI_16BIT:
858                 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
859                 retval = (cptr.wp[0] == cword.w);
860                 break;
861         case FLASH_CFI_32BIT:
862                 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
863                 retval = (cptr.lp[0] == cword.l);
864                 break;
865         case FLASH_CFI_64BIT:
866 #ifdef DEBUG
867                 {
868                         char str1[20];
869                         char str2[20];
870
871                         print_longlong (str1, cptr.llp[0]);
872                         print_longlong (str2, cword.ll);
873                         debug ("is= %s %s\n", str1, str2);
874                 }
875 #endif
876                 retval = (cptr.llp[0] == cword.ll);
877                 break;
878         default:
879                 retval = 0;
880                 break;
881         }
882         return retval;
883 }
884
885 /*-----------------------------------------------------------------------
886  */
887 static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
888 {
889         cfiptr_t cptr;
890         cfiword_t cword;
891         int retval;
892
893         cptr.cp = flash_make_addr (info, sect, offset);
894         flash_make_cmd (info, cmd, &cword);
895         switch (info->portwidth) {
896         case FLASH_CFI_8BIT:
897                 retval = ((cptr.cp[0] & cword.c) == cword.c);
898                 break;
899         case FLASH_CFI_16BIT:
900                 retval = ((cptr.wp[0] & cword.w) == cword.w);
901                 break;
902         case FLASH_CFI_32BIT:
903                 retval = ((cptr.lp[0] & cword.l) == cword.l);
904                 break;
905         case FLASH_CFI_64BIT:
906                 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
907                 break;
908         default:
909                 retval = 0;
910                 break;
911         }
912         return retval;
913 }
914
915 /*-----------------------------------------------------------------------
916  */
917 static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
918 {
919         cfiptr_t cptr;
920         cfiword_t cword;
921         int retval;
922
923         cptr.cp = flash_make_addr (info, sect, offset);
924         flash_make_cmd (info, cmd, &cword);
925         switch (info->portwidth) {
926         case FLASH_CFI_8BIT:
927                 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
928                 break;
929         case FLASH_CFI_16BIT:
930                 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
931                 break;
932         case FLASH_CFI_32BIT:
933                 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
934                 break;
935         case FLASH_CFI_64BIT:
936                 retval = ((cptr.llp[0] & cword.ll) !=
937                           (cptr.llp[0] & cword.ll));
938                 break;
939         default:
940                 retval = 0;
941                 break;
942         }
943         return retval;
944 }
945
946 /*-----------------------------------------------------------------------
947  * detect if flash is compatible with the Common Flash Interface (CFI)
948  * http://www.jedec.org/download/search/jesd68.pdf
949  *
950 */
951 static int flash_detect_cfi (flash_info_t * info)
952 {
953         debug ("flash detect cfi\n");
954
955         for (info->portwidth = FLASH_CFI_8BIT;
956              info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
957                 for (info->chipwidth = FLASH_CFI_BY8;
958                      info->chipwidth <= info->portwidth;
959                      info->chipwidth <<= 1) {
960                         flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
961                         flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
962                         if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
963                             && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
964                             && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
965                                 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
966                                 debug ("device interface is %d\n",
967                                        info->interface);
968                                 debug ("found port %d chip %d ",
969                                        info->portwidth, info->chipwidth);
970                                 debug ("port %d bits chip %d bits\n",
971                                        info->portwidth << CFI_FLASH_SHIFT_WIDTH,
972                                        info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
973                                 return 1;
974                         }
975                 }
976         }
977         debug ("not found\n");
978         return 0;
979 }
980
981 /*
982  * The following code cannot be run from FLASH!
983  *
984  */
985 static ulong flash_get_size (ulong base, int banknum)
986 {
987         flash_info_t *info = &flash_info[banknum];
988         int i, j;
989         flash_sect_t sect_cnt;
990         unsigned long sector;
991         unsigned long tmp;
992         int size_ratio;
993         uchar num_erase_regions;
994         int erase_region_size;
995         int erase_region_count;
996
997         info->start[0] = base;
998
999         if (flash_detect_cfi (info)) {
1000                 info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
1001 #ifdef DEBUG
1002                 flash_printqry (info, 0);
1003 #endif
1004                 switch (info->vendor) {
1005                 case CFI_CMDSET_INTEL_STANDARD:
1006                 case CFI_CMDSET_INTEL_EXTENDED:
1007                 default:
1008                         info->cmd_reset = FLASH_CMD_RESET;
1009                         break;
1010                 case CFI_CMDSET_AMD_STANDARD:
1011                 case CFI_CMDSET_AMD_EXTENDED:
1012                         info->cmd_reset = AMD_CMD_RESET;
1013                         break;
1014                 }
1015
1016                 debug ("manufacturer is %d\n", info->vendor);
1017                 size_ratio = info->portwidth / info->chipwidth;
1018                 /* if the chip is x8/x16 reduce the ratio by half */
1019                 if ((info->interface == FLASH_CFI_X8X16)
1020                     && (info->chipwidth == FLASH_CFI_BY8)) {
1021                         size_ratio >>= 1;
1022                 }
1023                 num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
1024                 debug ("size_ratio %d port %d bits chip %d bits\n",
1025                        size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1026                        info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1027                 debug ("found %d erase regions\n", num_erase_regions);
1028                 sect_cnt = 0;
1029                 sector = base;
1030                 for (i = 0; i < num_erase_regions; i++) {
1031                         if (i > NUM_ERASE_REGIONS) {
1032                                 printf ("%d erase regions found, only %d used\n",
1033                                         num_erase_regions, NUM_ERASE_REGIONS);
1034                                 break;
1035                         }
1036                         tmp = flash_read_long (info, 0,
1037                                                FLASH_OFFSET_ERASE_REGIONS +
1038                                                i * 4);
1039                         erase_region_size =
1040                                 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
1041                         tmp >>= 16;
1042                         erase_region_count = (tmp & 0xffff) + 1;
1043                         debug ("erase_region_count = %d erase_region_size = %d\n",
1044                                 erase_region_count, erase_region_size);
1045                         for (j = 0; j < erase_region_count; j++) {
1046                                 info->start[sect_cnt] = sector;
1047                                 sector += (erase_region_size * size_ratio);
1048                                 info->protect[sect_cnt] =
1049                                         flash_isset (info, sect_cnt,
1050                                                      FLASH_OFFSET_PROTECT,
1051                                                      FLASH_STATUS_PROTECT);
1052                                 sect_cnt++;
1053                         }
1054                 }
1055
1056                 info->sector_count = sect_cnt;
1057                 /* multiply the size by the number of chips */
1058                 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1059                 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
1060                 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
1061                 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
1062                 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
1063                 info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
1064                 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
1065                 info->write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT))) / 1000;
1066                 info->flash_id = FLASH_MAN_CFI;
1067                 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1068                         info->portwidth >>= 1;  /* XXX - Need to test on x8/x16 in parallel. */
1069                 }
1070         }
1071
1072         flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
1073         return (info->size);
1074 }
1075
1076
1077 /*-----------------------------------------------------------------------
1078  */
1079 static int flash_write_cfiword (flash_info_t * info, ulong dest,
1080                                 cfiword_t cword)
1081 {
1082
1083         cfiptr_t ctladdr;
1084         cfiptr_t cptr;
1085         int flag;
1086
1087         ctladdr.cp = flash_make_addr (info, 0, 0);
1088         cptr.cp = (uchar *) dest;
1089
1090
1091         /* Check if Flash is (sufficiently) erased */
1092         switch (info->portwidth) {
1093         case FLASH_CFI_8BIT:
1094                 flag = ((cptr.cp[0] & cword.c) == cword.c);
1095                 break;
1096         case FLASH_CFI_16BIT:
1097                 flag = ((cptr.wp[0] & cword.w) == cword.w);
1098                 break;
1099         case FLASH_CFI_32BIT:
1100                 flag = ((cptr.lp[0] & cword.l) == cword.l);
1101                 break;
1102         case FLASH_CFI_64BIT:
1103                 flag = ((cptr.lp[0] & cword.ll) == cword.ll);
1104                 break;
1105         default:
1106                 return 2;
1107         }
1108         if (!flag)
1109                 return 2;
1110
1111         /* Disable interrupts which might cause a timeout here */
1112         flag = disable_interrupts ();
1113
1114         switch (info->vendor) {
1115         case CFI_CMDSET_INTEL_EXTENDED:
1116         case CFI_CMDSET_INTEL_STANDARD:
1117                 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1118                 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
1119                 break;
1120         case CFI_CMDSET_AMD_EXTENDED:
1121         case CFI_CMDSET_AMD_STANDARD:
1122                 flash_unlock_seq (info, 0);
1123                 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
1124                 break;
1125         }
1126
1127         switch (info->portwidth) {
1128         case FLASH_CFI_8BIT:
1129                 cptr.cp[0] = cword.c;
1130                 break;
1131         case FLASH_CFI_16BIT:
1132                 cptr.wp[0] = cword.w;
1133                 break;
1134         case FLASH_CFI_32BIT:
1135                 cptr.lp[0] = cword.l;
1136                 break;
1137         case FLASH_CFI_64BIT:
1138                 cptr.llp[0] = cword.ll;
1139                 break;
1140         }
1141
1142         /* re-enable interrupts if necessary */
1143         if (flag)
1144                 enable_interrupts ();
1145
1146         return flash_full_status_check (info, 0, info->write_tout, "write");
1147 }
1148
1149 #ifdef CFG_FLASH_USE_BUFFER_WRITE
1150
1151 /* loop through the sectors from the highest address
1152  * when the passed address is greater or equal to the sector address
1153  * we have a match
1154  */
1155 static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1156 {
1157         flash_sect_t sector;
1158
1159         for (sector = info->sector_count - 1; sector >= 0; sector--) {
1160                 if (addr >= info->start[sector])
1161                         break;
1162         }
1163         return sector;
1164 }
1165
1166 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1167                                   int len)
1168 {
1169         flash_sect_t sector;
1170         int cnt;
1171         int retcode;
1172         volatile cfiptr_t src;
1173         volatile cfiptr_t dst;
1174         /* buffered writes in the AMD chip set is not supported yet */
1175         if((info->vendor ==  CFI_CMDSET_AMD_STANDARD) ||
1176                 (info->vendor == CFI_CMDSET_AMD_EXTENDED))
1177                 return ERR_INVAL;
1178
1179         src.cp = cp;
1180         dst.cp = (uchar *) dest;
1181         sector = find_sector (info, dest);
1182         flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1183         flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1184         if ((retcode =
1185              flash_status_check (info, sector, info->buffer_write_tout,
1186                                  "write to buffer")) == ERR_OK) {
1187                 /* reduce the number of loops by the width of the port  */
1188                 switch (info->portwidth) {
1189                 case FLASH_CFI_8BIT:
1190                         cnt = len;
1191                         break;
1192                 case FLASH_CFI_16BIT:
1193                         cnt = len >> 1;
1194                         break;
1195                 case FLASH_CFI_32BIT:
1196                         cnt = len >> 2;
1197                         break;
1198                 case FLASH_CFI_64BIT:
1199                         cnt = len >> 3;
1200                         break;
1201                 default:
1202                         return ERR_INVAL;
1203                         break;
1204                 }
1205                 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1206                 while (cnt-- > 0) {
1207                         switch (info->portwidth) {
1208                         case FLASH_CFI_8BIT:
1209                                 *dst.cp++ = *src.cp++;
1210                                 break;
1211                         case FLASH_CFI_16BIT:
1212                                 *dst.wp++ = *src.wp++;
1213                                 break;
1214                         case FLASH_CFI_32BIT:
1215                                 *dst.lp++ = *src.lp++;
1216                                 break;
1217                         case FLASH_CFI_64BIT:
1218                                 *dst.llp++ = *src.llp++;
1219                                 break;
1220                         default:
1221                                 return ERR_INVAL;
1222                                 break;
1223                         }
1224                 }
1225                 flash_write_cmd (info, sector, 0,
1226                                  FLASH_CMD_WRITE_BUFFER_CONFIRM);
1227                 retcode =
1228                         flash_full_status_check (info, sector,
1229                                                  info->buffer_write_tout,
1230                                                  "buffer write");
1231         }
1232         flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1233         return retcode;
1234 }
1235 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
1236 #endif /* CFG_FLASH_CFI */