PMC405 board update
[pandora-u-boot.git] / board / esd / pmc405 / strataflash.c
1 /*
2  * (C) Copyright 2002
3  * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <asm/processor.h>
26
27 #undef  DEBUG_FLASH 
28 /*
29  * This file implements a Common Flash Interface (CFI) driver for ppcboot.
30  * The width of the port and the width of the chips are determined at initialization.
31  * These widths are used to calculate the address for access CFI data structures.
32  * It has been tested on an Intel Strataflash implementation.
33  *
34  * References
35  * JEDEC Standard JESD68 - Common Flash Interface (CFI)
36  * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
37  * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
38  * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
39  *
40  * TODO
41  * Use Primary Extended Query table (PRI) and Alternate Algorithm Query Table (ALT) to determine if protection is available
42  * Add support for other command sets Use the PRI and ALT to determine command set
43  * Verify erase and program timeouts.
44  */
45
46 #define FLASH_CMD_CFI                   0x98
47 #define FLASH_CMD_READ_ID               0x90
48 #define FLASH_CMD_RESET                 0xff
49 #define FLASH_CMD_BLOCK_ERASE           0x20
50 #define FLASH_CMD_ERASE_CONFIRM         0xD0
51 #define FLASH_CMD_WRITE                 0x40
52 #define FLASH_CMD_PROTECT               0x60
53 #define FLASH_CMD_PROTECT_SET           0x01
54 #define FLASH_CMD_PROTECT_CLEAR         0xD0
55 #define FLASH_CMD_CLEAR_STATUS          0x50
56 #define FLASH_CMD_WRITE_TO_BUFFER       0xE8
57 #define FLASH_CMD_WRITE_BUFFER_CONFIRM  0xD0
58
59 #define FLASH_STATUS_DONE               0x80
60 #define FLASH_STATUS_ESS                0x40
61 #define FLASH_STATUS_ECLBS              0x20
62 #define FLASH_STATUS_PSLBS              0x10
63 #define FLASH_STATUS_VPENS              0x08
64 #define FLASH_STATUS_PSS                0x04
65 #define FLASH_STATUS_DPS                0x02
66 #define FLASH_STATUS_R                  0x01
67 #define FLASH_STATUS_PROTECT            0x01
68
69 #define FLASH_OFFSET_CFI                0x55
70 #define FLASH_OFFSET_CFI_RESP           0x10
71 #define FLASH_OFFSET_WTOUT              0x1F
72 #define FLASH_OFFSET_WBTOUT             0x20
73 #define FLASH_OFFSET_ETOUT              0x21
74 #define FLASH_OFFSET_CETOUT             0x22
75 #define FLASH_OFFSET_WMAX_TOUT          0x23
76 #define FLASH_OFFSET_WBMAX_TOUT         0x24
77 #define FLASH_OFFSET_EMAX_TOUT          0x25
78 #define FLASH_OFFSET_CEMAX_TOUT         0x26
79 #define FLASH_OFFSET_SIZE               0x27
80 #define FLASH_OFFSET_INTERFACE          0x28
81 #define FLASH_OFFSET_BUFFER_SIZE        0x2A
82 #define FLASH_OFFSET_NUM_ERASE_REGIONS  0x2C
83 #define FLASH_OFFSET_ERASE_REGIONS      0x2D
84 #define FLASH_OFFSET_PROTECT            0x02
85 #define FLASH_OFFSET_USER_PROTECTION    0x85
86 #define FLASH_OFFSET_INTEL_PROTECTION   0x81
87
88
89 #define FLASH_MAN_CFI                   0x01000000
90
91
92
93
94 typedef union {
95         unsigned char c;
96         unsigned short w;
97         unsigned long l;
98 } cfiword_t;
99
100 typedef union {
101         unsigned char * cp;
102         unsigned short *wp;
103         unsigned long *lp;
104 } cfiptr_t;
105
106 #define NUM_ERASE_REGIONS 4
107
108 flash_info_t    flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips        */
109
110
111 /*-----------------------------------------------------------------------
112  * Functions
113  */
114
115
116
117 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
118 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
119 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd);
120 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd);
121 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd);
122 static int flash_detect_cfi(flash_info_t * info);
123 static ulong flash_get_size (ulong base, int banknum);
124 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
125 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt);
126 #ifdef CFG_FLASH_USE_BUFFER_WRITE
127 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
128 #endif
129 /*-----------------------------------------------------------------------
130  * create an address based on the offset and the port width
131  */
132 inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
133 {
134         return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
135 }
136 /*-----------------------------------------------------------------------
137  * read a character at a port width address
138  */
139 inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
140 {
141         uchar *cp;
142         cp = flash_make_addr(info, 0, offset);
143         return (cp[info->portwidth - 1]);
144 }
145
146 /*-----------------------------------------------------------------------
147  * read a short word by swapping for ppc format.
148  */
149 ushort flash_read_ushort(flash_info_t * info, int sect,  uchar offset)
150 {
151     uchar * addr;
152
153     addr = flash_make_addr(info, sect, offset);
154     return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
155
156 }
157
158 /*-----------------------------------------------------------------------
159  * read a long word by picking the least significant byte of each maiximum
160  * port size word. Swap for ppc format.
161  */
162 ulong flash_read_long(flash_info_t * info, int sect,  uchar offset)
163 {
164     uchar * addr;
165
166     addr = flash_make_addr(info, sect, offset);
167     return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) |
168             (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]);
169
170 }
171
172 /*-----------------------------------------------------------------------
173  */
174 unsigned long flash_init (void)
175 {
176         unsigned long size;
177         int i;
178         unsigned long  address;
179
180
181         /* The flash is positioned back to back, with the demultiplexing of the chip
182          * based on the A24 address line.
183          *
184          */
185
186         address = CFG_FLASH_BASE;
187         size = 0;
188
189         /* Init: no FLASHes known */
190         for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
191                 flash_info[i].flash_id = FLASH_UNKNOWN;
192                 size += flash_info[i].size = flash_get_size(address, i);
193                 address += CFG_FLASH_INCREMENT;
194                 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
195                         printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i,
196                                 flash_info[0].size, flash_info[i].size<<20);
197                 }
198         }
199
200 #if 0 /* test-only */
201         /* Monitor protection ON by default */
202 #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
203         for(i=0; flash_info[0].start[i] < CFG_MONITOR_BASE+CFG_MONITOR_LEN-1; i++)
204                 (void)flash_real_protect(&flash_info[0], i, 1);
205 #endif
206 #else
207         /* monitor protection ON by default */
208         flash_protect (FLAG_PROTECT_SET,
209                        - CFG_MONITOR_LEN,
210                        - 1, &flash_info[1]);
211 #endif
212
213         return (size);
214 }
215
216 /*-----------------------------------------------------------------------
217  */
218 int flash_erase (flash_info_t *info, int s_first, int s_last)
219 {
220         int rcode = 0;
221         int prot;
222         int sect;
223
224         if( info->flash_id != FLASH_MAN_CFI) {
225                 printf ("Can't erase unknown flash type - aborted\n");
226                 return 1;
227         }
228         if ((s_first < 0) || (s_first > s_last)) {
229                 printf ("- no sectors to erase\n");
230                 return 1;
231         }
232
233         prot = 0;
234         for (sect=s_first; sect<=s_last; ++sect) {
235                 if (info->protect[sect]) {
236                         prot++;
237                 }
238         }
239         if (prot) {
240                 printf ("- Warning: %d protected sectors will not be erased!\n",
241                         prot);
242         } else {
243                 printf ("\n");
244         }
245
246
247         for (sect = s_first; sect<=s_last; sect++) {
248                 if (info->protect[sect] == 0) { /* not protected */
249                         flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
250                         flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
251                         flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
252                         
253                         if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
254                                 rcode = 1;
255                         } else
256                                 printf(".");
257                 }
258         }
259         printf (" done\n");
260         return rcode;
261 }
262
263 /*-----------------------------------------------------------------------
264  */
265 void flash_print_info  (flash_info_t *info)
266 {
267         int i;
268
269         if (info->flash_id != FLASH_MAN_CFI) {
270                 printf ("missing or unknown FLASH type\n");
271                 return;
272         }
273
274         printf("CFI conformant FLASH (%d x %d)",
275                (info->portwidth  << 3 ), (info->chipwidth  << 3 ));
276         printf ("  Size: %ld MB in %d Sectors\n",
277                 info->size >> 20, info->sector_count);
278         printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
279                info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
280       
281         printf ("  Sector Start Addresses:");
282         for (i=0; i<info->sector_count; ++i) {
283 #ifdef CFG_FLASH_EMPTY_INFO
284                 int k;
285                 int size;
286                 int erased;
287                 volatile unsigned long *flash;
288
289                 /*
290                  * Check if whole sector is erased
291                  */
292                 if (i != (info->sector_count-1))
293                   size = info->start[i+1] - info->start[i];
294                 else
295                   size = info->start[0] + info->size - info->start[i];
296                 erased = 1;
297                 flash = (volatile unsigned long *)info->start[i];
298                 size = size >> 2;        /* divide by 4 for longword access */
299                 for (k=0; k<size; k++)
300                   {
301                     if (*flash++ != 0xffffffff)
302                       {
303                         erased = 0;
304                         break;
305                       }
306                   }
307
308                 if ((i % 5) == 0)
309                         printf ("\n   ");
310                 /* print empty and read-only info */
311                 printf (" %08lX%s%s",
312                         info->start[i],
313                         erased ? " E" : "  ",
314                         info->protect[i] ? "RO " : "   ");
315 #else
316                 if ((i % 5) == 0)
317                         printf ("\n   ");
318                 printf (" %08lX%s",
319                         info->start[i],
320                         info->protect[i] ? " (RO)" : "     ");
321 #endif
322         }
323         printf ("\n");
324         return;
325 }
326
327 /*-----------------------------------------------------------------------
328  * Copy memory to flash, returns:
329  * 0 - OK
330  * 1 - write timeout
331  * 2 - Flash not erased
332  */
333 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
334 {
335         ulong wp;
336         ulong cp;
337         int aln;
338         cfiword_t cword;
339         int i, rc;
340
341         /* get lower aligned address */
342         wp = (addr & ~(info->portwidth - 1));
343
344         /* handle unaligned start */
345         if((aln = addr - wp) != 0) {
346                 cword.l = 0;
347                 cp = wp;
348                 for(i=0;i<aln; ++i, ++cp)
349                         flash_add_byte(info, &cword, (*(uchar *)cp));
350
351                 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
352                         flash_add_byte(info, &cword, *src++);
353                         cnt--;
354                         cp++;
355                 }
356                 for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
357                         flash_add_byte(info, &cword, (*(uchar *)cp));
358                 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
359                         return rc;
360                 wp = cp;
361         }
362
363 #ifdef CFG_FLASH_USE_BUFFER_WRITE
364         while(cnt >= info->portwidth) {
365                 i = info->buffer_size > cnt? cnt: info->buffer_size;
366                 if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
367                         return rc;
368                 wp += i;
369                 src += i;
370                 cnt -=i;
371         }
372 #else
373         /* handle the aligned part */
374         while(cnt >= info->portwidth) {
375                 cword.l = 0;
376                 for(i = 0; i < info->portwidth; i++) {
377                         flash_add_byte(info, &cword, *src++);
378                 }
379                 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
380                         return rc;
381                 wp += info->portwidth;
382                 cnt -= info->portwidth;
383         }
384 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
385         if (cnt == 0) {
386                 return (0);
387         }
388
389         /*
390          * handle unaligned tail bytes
391          */
392         cword.l = 0;
393         for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
394                 flash_add_byte(info, &cword, *src++);
395                 --cnt;
396         }
397         for (; i<info->portwidth; ++i, ++cp) {
398                 flash_add_byte(info, & cword, (*(uchar *)cp));
399         }
400
401         return flash_write_cfiword(info, wp, cword);
402 }
403
404 /*-----------------------------------------------------------------------
405  */
406 int flash_real_protect(flash_info_t *info, long sector, int prot)
407 {
408         int retcode = 0;
409
410         flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
411         flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
412         if(prot)
413                 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
414         else
415                 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
416
417         if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout, 
418                                          prot?"protect":"unprotect")) == 0) {
419
420                 info->protect[sector] = prot;
421                 /* Intel's unprotect unprotects all locking */
422                 if(prot == 0) {
423                         int i;
424                         for(i = 0 ; i<info->sector_count; i++) {
425                                 if(info->protect[i])
426                                         flash_real_protect(info, i, 1);
427                         }
428                 }
429         }
430
431         return retcode;
432 }
433 /*-----------------------------------------------------------------------
434  *  wait for XSR.7 to be set. Time out with an error if it does not.
435  *  This routine does not set the flash to read-array mode.
436  */
437 static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
438 {
439         ulong start;
440
441         /* Wait for command completion */
442         start = get_timer (0);
443         while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
444                 if (get_timer(start) > info->erase_blk_tout) {
445                         printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
446                         flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
447                         return ERR_TIMOUT;
448                 }
449         }
450         return ERR_OK;
451 }
452 /*-----------------------------------------------------------------------
453  * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
454  * This routine sets the flash to read-array mode.
455  */
456 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
457 {
458         int retcode;
459         retcode = flash_status_check(info, sector, tout, prompt);
460         if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
461                 retcode = ERR_INVAL;
462                 printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
463                 if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
464                         printf("Command Sequence Error.\n");
465                 } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
466                         printf("Block Erase Error.\n");
467                         retcode = ERR_NOT_ERASED;
468                 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
469                         printf("Locking Error\n");
470                 }
471                 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
472                         printf("Block locked.\n");
473                         retcode = ERR_PROTECTED;
474                 }
475                 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
476                         printf("Vpp Low Error.\n");
477         }
478         flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
479         return retcode;
480 }
481 /*-----------------------------------------------------------------------
482  */
483 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
484 {
485         switch(info->portwidth) {
486         case FLASH_CFI_8BIT:
487                 cword->c = c;
488                 break;
489         case FLASH_CFI_16BIT:
490                 cword->w = (cword->w << 8) | c;
491                 break;
492         case FLASH_CFI_32BIT:
493                 cword->l = (cword->l << 8) | c;
494         }
495 }
496
497
498 /*-----------------------------------------------------------------------
499  * make a proper sized command based on the port and chip widths
500  */
501 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
502 {
503         int i;
504         uchar *cp = (uchar *)cmdbuf;
505         for(i=0; i< info->portwidth; i++)
506                 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
507 }
508
509 /*
510  * Write a proper sized command to the correct address
511  */
512 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
513 {
514
515         volatile cfiptr_t addr;
516         cfiword_t cword;
517         addr.cp = flash_make_addr(info, sect, offset);
518         flash_make_cmd(info, cmd, &cword);
519         switch(info->portwidth) {
520         case FLASH_CFI_8BIT:
521                 *addr.cp = cword.c;
522                 break;
523         case FLASH_CFI_16BIT:
524                 *addr.wp = cword.w;
525                 break;
526         case FLASH_CFI_32BIT:
527                 *addr.lp = cword.l;
528                 break;
529         }
530 }
531
532 /*-----------------------------------------------------------------------
533  */
534 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
535 {
536         cfiptr_t cptr;
537         cfiword_t cword;
538         int retval;
539         cptr.cp = flash_make_addr(info, sect, offset);
540         flash_make_cmd(info, cmd, &cword);
541         switch(info->portwidth) {
542         case FLASH_CFI_8BIT:
543                 retval = (cptr.cp[0] == cword.c);
544                 break;
545         case FLASH_CFI_16BIT:
546                 retval = (cptr.wp[0] == cword.w);
547                 break;
548         case FLASH_CFI_32BIT:
549                 retval = (cptr.lp[0] == cword.l);
550                 break;
551         default:
552                 retval = 0;
553                 break;
554         }
555         return retval;
556 }
557 /*-----------------------------------------------------------------------
558  */
559 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
560 {
561         cfiptr_t cptr;
562         cfiword_t cword;
563         int retval;
564         cptr.cp = flash_make_addr(info, sect, offset);
565         flash_make_cmd(info, cmd, &cword);
566         switch(info->portwidth) {
567         case FLASH_CFI_8BIT:
568                 retval = ((cptr.cp[0] & cword.c) == cword.c);
569                 break;
570         case FLASH_CFI_16BIT:
571                 retval = ((cptr.wp[0] & cword.w) == cword.w);
572                 break;
573         case FLASH_CFI_32BIT:
574                 retval = ((cptr.lp[0] & cword.l) == cword.l);
575                 break;
576         default:
577                 retval = 0;
578                 break;
579         }
580         return retval;
581 }
582
583 /*-----------------------------------------------------------------------
584  * detect if flash is compatible with the Common Flash Interface (CFI)
585  * http://www.jedec.org/download/search/jesd68.pdf
586  *
587 */
588 static int flash_detect_cfi(flash_info_t * info)
589 {
590
591         for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
592             info->portwidth <<= 1) {
593                 for(info->chipwidth =FLASH_CFI_BY8;
594                     info->chipwidth <= info->portwidth;
595                     info->chipwidth <<= 1) {
596                         flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
597                         flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
598                         if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
599                            flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
600                            flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
601                                 return 1;
602                 }
603         }
604         return 0;
605 }
606 /*
607  * The following code cannot be run from FLASH!
608  *
609  */
610 static ulong flash_get_size (ulong base, int banknum)
611 {
612         flash_info_t * info = &flash_info[banknum];
613         int i, j;
614         int sect_cnt;
615         unsigned long sector;
616         unsigned long tmp;
617         int size_ratio;
618         uchar num_erase_regions;
619         int  erase_region_size;
620         int  erase_region_count;
621
622         info->start[0] = base;
623
624         if(flash_detect_cfi(info)){
625 #ifdef DEBUG_FLASH
626                 printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
627 #endif
628                 size_ratio = info->portwidth / info->chipwidth;
629                 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
630 #ifdef DEBUG_FLASH
631                 printf("found %d erase regions\n", num_erase_regions);
632 #endif
633                 sect_cnt = 0;
634                 sector = base;
635                 for(i = 0 ; i < num_erase_regions; i++) {
636                         if(i > NUM_ERASE_REGIONS) {
637                                 printf("%d erase regions found, only %d used\n",
638                                        num_erase_regions, NUM_ERASE_REGIONS);
639                                 break;
640                         }
641                         tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
642                         erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
643                         tmp >>= 16;
644                         erase_region_count = (tmp & 0xffff) +1;
645                         for(j = 0; j< erase_region_count; j++) {
646                                 info->start[sect_cnt] = sector;
647                                 sector += (erase_region_size * size_ratio);
648                                 info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
649                                 sect_cnt++;
650                         }
651                 }
652
653                 info->sector_count = sect_cnt;
654                 /* multiply the size by the number of chips */
655                 info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
656                 info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
657                 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
658                 info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
659                 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
660                 info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
661                 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
662                 info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
663                 info->flash_id = FLASH_MAN_CFI;
664         }
665
666         flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
667         return(info->size);
668 }
669
670
671 /*-----------------------------------------------------------------------
672  */
673 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
674 {
675
676         cfiptr_t ctladdr;
677         cfiptr_t cptr;
678         int flag;
679
680         ctladdr.cp = flash_make_addr(info, 0, 0);
681         cptr.cp = (uchar *)dest;
682
683
684         /* Check if Flash is (sufficiently) erased */
685         switch(info->portwidth) {
686         case FLASH_CFI_8BIT:
687                 flag = ((cptr.cp[0] & cword.c) == cword.c);
688                 break;
689         case FLASH_CFI_16BIT:
690                 flag = ((cptr.wp[0] & cword.w) == cword.w);
691                 break;
692         case FLASH_CFI_32BIT:
693                 flag = ((cptr.lp[0] & cword.l)  == cword.l);
694                 break;
695         default:
696                 return 2;
697         }
698         if(!flag)
699                 return 2;
700
701         /* Disable interrupts which might cause a timeout here */
702         flag = disable_interrupts();
703
704         flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
705         flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
706
707         switch(info->portwidth) {
708         case FLASH_CFI_8BIT:
709                 cptr.cp[0] = cword.c;
710                 break;
711         case FLASH_CFI_16BIT:
712                 cptr.wp[0] = cword.w;
713                 break;
714         case FLASH_CFI_32BIT:
715                 cptr.lp[0] = cword.l;
716                 break;
717         }
718
719         /* re-enable interrupts if necessary */
720         if(flag)
721                 enable_interrupts();
722
723         return flash_full_status_check(info, 0, info->write_tout, "write");
724 }
725
726 #ifdef CFG_FLASH_USE_BUFFER_WRITE
727
728 /* loop through the sectors from the highest address
729  * when the passed address is greater or equal to the sector address
730  * we have a match
731  */
732 static int find_sector(flash_info_t *info, ulong addr)
733 {
734         int sector;
735         for(sector = info->sector_count - 1; sector >= 0; sector--) {
736                 if(addr >= info->start[sector]) 
737                         break;
738         }
739         return sector;
740 }
741
742 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
743 {
744         
745         int sector;
746         int cnt;
747         int retcode;
748         volatile cfiptr_t src;
749         volatile cfiptr_t dst;
750
751         src.cp = cp;
752         dst.cp = (uchar *)dest;
753         sector = find_sector(info, dest);
754         flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
755         flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
756         if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
757                                          "write to buffer")) == ERR_OK) {
758                 switch(info->portwidth) {
759                 case FLASH_CFI_8BIT:
760                         cnt = len;
761                         break;
762                 case FLASH_CFI_16BIT:
763                         cnt = len >> 1;
764                         break;
765                 case FLASH_CFI_32BIT:
766                         cnt = len >> 2;
767                         break;
768                 default:
769                         return ERR_INVAL;
770                         break;
771                 }
772                 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
773                 while(cnt-- > 0) {
774                         switch(info->portwidth) {
775                         case FLASH_CFI_8BIT:
776                                 *dst.cp++ = *src.cp++;
777                                 break;
778                         case FLASH_CFI_16BIT:
779                                 *dst.wp++ = *src.wp++;
780                                 break;
781                         case FLASH_CFI_32BIT:
782                                 *dst.lp++ = *src.lp++;
783                                 break;
784                         default:
785                                 return ERR_INVAL;
786                                 break;
787                         }
788                 }
789                 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
790                 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
791                                              "buffer write");
792         } 
793         flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
794         return retcode;
795 }       
796 #endif /* CFG_USE_FLASH_BUFFER_WRITE */