deadbat-alternate.patch
[u-boot-openmoko/mini2440.git] / board / esd / apc405 / strataflash.c
blobad7a71dc4db819d342da587d740a362337867897
1 /*
2 * (C) Copyright 2002
3 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
24 #include <common.h>
25 #include <asm/processor.h>
27 #undef DEBUG_FLASH
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.
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
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.
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
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
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
88 #define FLASH_MAN_CFI 0x01000000
90 typedef union {
91 unsigned char c;
92 unsigned short w;
93 unsigned long l;
94 } cfiword_t;
96 typedef union {
97 unsigned char * cp;
98 unsigned short *wp;
99 unsigned long *lp;
100 } cfiptr_t;
102 #define NUM_ERASE_REGIONS 4
104 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
106 /*-----------------------------------------------------------------------
107 * Functions
110 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
111 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
112 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd);
113 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd);
114 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd);
115 static int flash_detect_cfi(flash_info_t * info);
116 static ulong flash_get_size (ulong base, int banknum);
117 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
118 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt);
119 #ifdef CFG_FLASH_USE_BUFFER_WRITE
120 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
121 #endif
122 /*-----------------------------------------------------------------------
123 * create an address based on the offset and the port width
125 inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
127 return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
129 /*-----------------------------------------------------------------------
130 * read a character at a port width address
132 inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
134 uchar *cp;
135 cp = flash_make_addr(info, 0, offset);
136 return (cp[info->portwidth - 1]);
139 /*-----------------------------------------------------------------------
140 * read a short word by swapping for ppc format.
142 ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset)
144 uchar * addr;
146 addr = flash_make_addr(info, sect, offset);
147 return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
151 /*-----------------------------------------------------------------------
152 * read a long word by picking the least significant byte of each maiximum
153 * port size word. Swap for ppc format.
155 ulong flash_read_long(flash_info_t * info, int sect, uchar offset)
157 uchar * addr;
159 addr = flash_make_addr(info, sect, offset);
160 return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) |
161 (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]);
165 /*-----------------------------------------------------------------------
167 unsigned long flash_init (void)
169 unsigned long size;
170 int i;
171 unsigned long address;
174 /* The flash is positioned back to back, with the demultiplexing of the chip
175 * based on the A24 address line.
179 address = CFG_FLASH_BASE;
180 size = 0;
182 /* Init: no FLASHes known */
183 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
184 flash_info[i].flash_id = FLASH_UNKNOWN;
185 size += flash_info[i].size = flash_get_size(address, i);
186 address += CFG_FLASH_INCREMENT;
187 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
188 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i,
189 flash_info[0].size, flash_info[i].size<<20);
193 #if 0 /* test-only */
194 /* Monitor protection ON by default */
195 #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
196 for(i=0; flash_info[0].start[i] < CFG_MONITOR_BASE+CFG_MONITOR_LEN-1; i++)
197 (void)flash_real_protect(&flash_info[0], i, 1);
198 #endif
199 #else
200 /* monitor protection ON by default */
201 flash_protect (FLAG_PROTECT_SET,
202 - CFG_MONITOR_LEN,
203 - 1, &flash_info[1]);
204 #endif
206 return (size);
209 /*-----------------------------------------------------------------------
211 int flash_erase (flash_info_t *info, int s_first, int s_last)
213 int rcode = 0;
214 int prot;
215 int sect;
217 if( info->flash_id != FLASH_MAN_CFI) {
218 printf ("Can't erase unknown flash type - aborted\n");
219 return 1;
221 if ((s_first < 0) || (s_first > s_last)) {
222 printf ("- no sectors to erase\n");
223 return 1;
226 prot = 0;
227 for (sect=s_first; sect<=s_last; ++sect) {
228 if (info->protect[sect]) {
229 prot++;
232 if (prot) {
233 printf ("- Warning: %d protected sectors will not be erased!\n",
234 prot);
235 } else {
236 printf ("\n");
240 for (sect = s_first; sect<=s_last; sect++) {
241 if (info->protect[sect] == 0) { /* not protected */
242 flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
243 flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
244 flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
246 if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
247 rcode = 1;
248 } else
249 printf(".");
252 printf (" done\n");
253 return rcode;
256 /*-----------------------------------------------------------------------
258 void flash_print_info (flash_info_t *info)
260 int i;
262 if (info->flash_id != FLASH_MAN_CFI) {
263 printf ("missing or unknown FLASH type\n");
264 return;
267 printf("CFI conformant FLASH (%d x %d)",
268 (info->portwidth << 3 ), (info->chipwidth << 3 ));
269 printf (" Size: %ld MB in %d Sectors\n",
270 info->size >> 20, info->sector_count);
271 printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
272 info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
274 printf (" Sector Start Addresses:");
275 for (i=0; i<info->sector_count; ++i) {
276 #ifdef CFG_FLASH_EMPTY_INFO
277 int k;
278 int size;
279 int erased;
280 volatile unsigned long *flash;
283 * Check if whole sector is erased
285 if (i != (info->sector_count-1))
286 size = info->start[i+1] - info->start[i];
287 else
288 size = info->start[0] + info->size - info->start[i];
289 erased = 1;
290 flash = (volatile unsigned long *)info->start[i];
291 size = size >> 2; /* divide by 4 for longword access */
292 for (k=0; k<size; k++)
294 if (*flash++ != 0xffffffff)
296 erased = 0;
297 break;
301 if ((i % 5) == 0)
302 printf ("\n ");
303 /* print empty and read-only info */
304 printf (" %08lX%s%s",
305 info->start[i],
306 erased ? " E" : " ",
307 info->protect[i] ? "RO " : " ");
308 #else
309 if ((i % 5) == 0)
310 printf ("\n ");
311 printf (" %08lX%s",
312 info->start[i],
313 info->protect[i] ? " (RO)" : " ");
314 #endif
316 printf ("\n");
317 return;
320 /*-----------------------------------------------------------------------
321 * Copy memory to flash, returns:
322 * 0 - OK
323 * 1 - write timeout
324 * 2 - Flash not erased
326 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
328 ulong wp;
329 ulong cp;
330 int aln;
331 cfiword_t cword;
332 int i, rc;
334 /* get lower aligned address */
335 wp = (addr & ~(info->portwidth - 1));
337 /* handle unaligned start */
338 if((aln = addr - wp) != 0) {
339 cword.l = 0;
340 cp = wp;
341 for(i=0;i<aln; ++i, ++cp)
342 flash_add_byte(info, &cword, (*(uchar *)cp));
344 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
345 flash_add_byte(info, &cword, *src++);
346 cnt--;
347 cp++;
349 for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
350 flash_add_byte(info, &cword, (*(uchar *)cp));
351 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
352 return rc;
353 wp = cp;
356 #ifdef CFG_FLASH_USE_BUFFER_WRITE
357 while(cnt >= info->portwidth) {
358 i = info->buffer_size > cnt? cnt: info->buffer_size;
359 if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
360 return rc;
361 wp += i;
362 src += i;
363 cnt -=i;
365 #else
366 /* handle the aligned part */
367 while(cnt >= info->portwidth) {
368 cword.l = 0;
369 for(i = 0; i < info->portwidth; i++) {
370 flash_add_byte(info, &cword, *src++);
372 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
373 return rc;
374 wp += info->portwidth;
375 cnt -= info->portwidth;
377 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
378 if (cnt == 0) {
379 return (0);
383 * handle unaligned tail bytes
385 cword.l = 0;
386 for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
387 flash_add_byte(info, &cword, *src++);
388 --cnt;
390 for (; i<info->portwidth; ++i, ++cp) {
391 flash_add_byte(info, & cword, (*(uchar *)cp));
394 return flash_write_cfiword(info, wp, cword);
397 /*-----------------------------------------------------------------------
399 int flash_real_protect(flash_info_t *info, long sector, int prot)
401 int retcode = 0;
403 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
404 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
405 if(prot)
406 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
407 else
408 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
410 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
411 prot?"protect":"unprotect")) == 0) {
413 info->protect[sector] = prot;
414 /* Intel's unprotect unprotects all locking */
415 if(prot == 0) {
416 int i;
417 for(i = 0 ; i<info->sector_count; i++) {
418 if(info->protect[i])
419 flash_real_protect(info, i, 1);
424 return retcode;
426 /*-----------------------------------------------------------------------
427 * wait for XSR.7 to be set. Time out with an error if it does not.
428 * This routine does not set the flash to read-array mode.
430 static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
432 ulong start;
434 /* Wait for command completion */
435 start = get_timer (0);
436 while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
437 if (get_timer(start) > info->erase_blk_tout) {
438 printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
439 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
440 return ERR_TIMOUT;
443 return ERR_OK;
445 /*-----------------------------------------------------------------------
446 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
447 * This routine sets the flash to read-array mode.
449 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
451 int retcode;
452 retcode = flash_status_check(info, sector, tout, prompt);
453 if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
454 retcode = ERR_INVAL;
455 printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
456 if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
457 printf("Command Sequence Error.\n");
458 } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
459 printf("Block Erase Error.\n");
460 retcode = ERR_NOT_ERASED;
461 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
462 printf("Locking Error\n");
464 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
465 printf("Block locked.\n");
466 retcode = ERR_PROTECTED;
468 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
469 printf("Vpp Low Error.\n");
471 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
472 return retcode;
474 /*-----------------------------------------------------------------------
476 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
478 switch(info->portwidth) {
479 case FLASH_CFI_8BIT:
480 cword->c = c;
481 break;
482 case FLASH_CFI_16BIT:
483 cword->w = (cword->w << 8) | c;
484 break;
485 case FLASH_CFI_32BIT:
486 cword->l = (cword->l << 8) | c;
491 /*-----------------------------------------------------------------------
492 * make a proper sized command based on the port and chip widths
494 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
496 int i;
497 uchar *cp = (uchar *)cmdbuf;
498 for(i=0; i< info->portwidth; i++)
499 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
503 * Write a proper sized command to the correct address
505 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
508 volatile cfiptr_t addr;
509 cfiword_t cword;
510 addr.cp = flash_make_addr(info, sect, offset);
511 flash_make_cmd(info, cmd, &cword);
512 switch(info->portwidth) {
513 case FLASH_CFI_8BIT:
514 *addr.cp = cword.c;
515 break;
516 case FLASH_CFI_16BIT:
517 *addr.wp = cword.w;
518 break;
519 case FLASH_CFI_32BIT:
520 *addr.lp = cword.l;
521 break;
525 /*-----------------------------------------------------------------------
527 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
529 cfiptr_t cptr;
530 cfiword_t cword;
531 int retval;
532 cptr.cp = flash_make_addr(info, sect, offset);
533 flash_make_cmd(info, cmd, &cword);
534 switch(info->portwidth) {
535 case FLASH_CFI_8BIT:
536 retval = (cptr.cp[0] == cword.c);
537 break;
538 case FLASH_CFI_16BIT:
539 retval = (cptr.wp[0] == cword.w);
540 break;
541 case FLASH_CFI_32BIT:
542 retval = (cptr.lp[0] == cword.l);
543 break;
544 default:
545 retval = 0;
546 break;
548 return retval;
550 /*-----------------------------------------------------------------------
552 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
554 cfiptr_t cptr;
555 cfiword_t cword;
556 int retval;
557 cptr.cp = flash_make_addr(info, sect, offset);
558 flash_make_cmd(info, cmd, &cword);
559 switch(info->portwidth) {
560 case FLASH_CFI_8BIT:
561 retval = ((cptr.cp[0] & cword.c) == cword.c);
562 break;
563 case FLASH_CFI_16BIT:
564 retval = ((cptr.wp[0] & cword.w) == cword.w);
565 break;
566 case FLASH_CFI_32BIT:
567 retval = ((cptr.lp[0] & cword.l) == cword.l);
568 break;
569 default:
570 retval = 0;
571 break;
573 return retval;
576 /*-----------------------------------------------------------------------
577 * detect if flash is compatible with the Common Flash Interface (CFI)
578 * http://www.jedec.org/download/search/jesd68.pdf
581 static int flash_detect_cfi(flash_info_t * info)
584 for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
585 info->portwidth <<= 1) {
586 for(info->chipwidth =FLASH_CFI_BY8;
587 info->chipwidth <= info->portwidth;
588 info->chipwidth <<= 1) {
589 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
590 flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
591 if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
592 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
593 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
594 return 1;
597 return 0;
600 * The following code cannot be run from FLASH!
603 static ulong flash_get_size (ulong base, int banknum)
605 flash_info_t * info = &flash_info[banknum];
606 int i, j;
607 int sect_cnt;
608 unsigned long sector;
609 unsigned long tmp;
610 int size_ratio;
611 uchar num_erase_regions;
612 int erase_region_size;
613 int erase_region_count;
615 info->start[0] = base;
617 if(flash_detect_cfi(info)){
618 #ifdef DEBUG_FLASH
619 printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
620 #endif
621 size_ratio = info->portwidth / info->chipwidth;
622 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
623 #ifdef DEBUG_FLASH
624 printf("found %d erase regions\n", num_erase_regions);
625 #endif
626 sect_cnt = 0;
627 sector = base;
628 for(i = 0 ; i < num_erase_regions; i++) {
629 if(i > NUM_ERASE_REGIONS) {
630 printf("%d erase regions found, only %d used\n",
631 num_erase_regions, NUM_ERASE_REGIONS);
632 break;
634 tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
635 erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
636 tmp >>= 16;
637 erase_region_count = (tmp & 0xffff) +1;
638 for(j = 0; j< erase_region_count; j++) {
639 info->start[sect_cnt] = sector;
640 sector += (erase_region_size * size_ratio);
641 info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
642 sect_cnt++;
646 info->sector_count = sect_cnt;
647 /* multiply the size by the number of chips */
648 info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
649 info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
650 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
651 info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
652 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
653 info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
654 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
655 info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
656 info->flash_id = FLASH_MAN_CFI;
659 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
660 return(info->size);
664 /*-----------------------------------------------------------------------
666 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
669 cfiptr_t ctladdr;
670 cfiptr_t cptr;
671 int flag;
673 ctladdr.cp = flash_make_addr(info, 0, 0);
674 cptr.cp = (uchar *)dest;
677 /* Check if Flash is (sufficiently) erased */
678 switch(info->portwidth) {
679 case FLASH_CFI_8BIT:
680 flag = ((cptr.cp[0] & cword.c) == cword.c);
681 break;
682 case FLASH_CFI_16BIT:
683 flag = ((cptr.wp[0] & cword.w) == cword.w);
684 break;
685 case FLASH_CFI_32BIT:
686 flag = ((cptr.lp[0] & cword.l) == cword.l);
687 break;
688 default:
689 return 2;
691 if(!flag)
692 return 2;
694 /* Disable interrupts which might cause a timeout here */
695 flag = disable_interrupts();
697 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
698 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
700 switch(info->portwidth) {
701 case FLASH_CFI_8BIT:
702 cptr.cp[0] = cword.c;
703 break;
704 case FLASH_CFI_16BIT:
705 cptr.wp[0] = cword.w;
706 break;
707 case FLASH_CFI_32BIT:
708 cptr.lp[0] = cword.l;
709 break;
712 /* re-enable interrupts if necessary */
713 if(flag)
714 enable_interrupts();
716 return flash_full_status_check(info, 0, info->write_tout, "write");
719 #ifdef CFG_FLASH_USE_BUFFER_WRITE
721 /* loop through the sectors from the highest address
722 * when the passed address is greater or equal to the sector address
723 * we have a match
725 static int find_sector(flash_info_t *info, ulong addr)
727 int sector;
728 for(sector = info->sector_count - 1; sector >= 0; sector--) {
729 if(addr >= info->start[sector])
730 break;
732 return sector;
735 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
738 int sector;
739 int cnt;
740 int retcode;
741 volatile cfiptr_t src;
742 volatile cfiptr_t dst;
744 src.cp = cp;
745 dst.cp = (uchar *)dest;
746 sector = find_sector(info, dest);
747 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
748 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
749 if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
750 "write to buffer")) == ERR_OK) {
751 switch(info->portwidth) {
752 case FLASH_CFI_8BIT:
753 cnt = len;
754 break;
755 case FLASH_CFI_16BIT:
756 cnt = len >> 1;
757 break;
758 case FLASH_CFI_32BIT:
759 cnt = len >> 2;
760 break;
761 default:
762 return ERR_INVAL;
763 break;
765 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
766 while(cnt-- > 0) {
767 switch(info->portwidth) {
768 case FLASH_CFI_8BIT:
769 *dst.cp++ = *src.cp++;
770 break;
771 case FLASH_CFI_16BIT:
772 *dst.wp++ = *src.wp++;
773 break;
774 case FLASH_CFI_32BIT:
775 *dst.lp++ = *src.lp++;
776 break;
777 default:
778 return ERR_INVAL;
779 break;
782 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
783 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
784 "buffer write");
786 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
787 return retcode;
789 #endif /* CFG_USE_FLASH_BUFFER_WRITE */