blob: de57318e26cb647e6536d79c8ac93c11dcd8f19a [file] [log] [blame]
wdenk37bd3212002-11-03 11:21:28 +00001/*
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 U-Boot.
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
94typedef union {
95 unsigned char c;
96 unsigned short w;
97 unsigned long l;
98} cfiword_t;
99
100typedef union {
101 unsigned char * cp;
102 unsigned short *wp;
103 unsigned long *lp;
104} cfiptr_t;
105
106#define NUM_ERASE_REGIONS 4
107
108flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
109
110
111/*-----------------------------------------------------------------------
112 * Functions
113 */
114
115
116
117static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
118static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
119static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd);
120static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd);
121static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd);
122static int flash_detect_cfi(flash_info_t * info);
123static ulong flash_get_size (ulong base, int banknum);
124static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
125static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt);
126#ifdef CFG_FLASH_USE_BUFFER_WRITE
127static 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 */
132inline 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 */
139inline 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 */
149ushort 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 */
162ulong 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 */
174unsigned 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)
wdenk3b57fe02003-05-30 12:48:29 +0000203 for(i=0; flash_info[0].start[i] < CFG_MONITOR_BASE+monitor_flash_len-1; i++)
wdenk37bd3212002-11-03 11:21:28 +0000204 (void)flash_real_protect(&flash_info[0], i, 1);
205#endif
206#endif
207
208 return (size);
209}
210
211/*-----------------------------------------------------------------------
212 */
213int flash_erase (flash_info_t *info, int s_first, int s_last)
214{
215 int rcode = 0;
216 int prot;
217 int sect;
218
219 if( info->flash_id != FLASH_MAN_CFI) {
220 printf ("Can't erase unknown flash type - aborted\n");
221 return 1;
222 }
223 if ((s_first < 0) || (s_first > s_last)) {
224 printf ("- no sectors to erase\n");
225 return 1;
226 }
227
228 prot = 0;
229 for (sect=s_first; sect<=s_last; ++sect) {
230 if (info->protect[sect]) {
231 prot++;
232 }
233 }
234 if (prot) {
235 printf ("- Warning: %d protected sectors will not be erased!\n",
236 prot);
237 } else {
238 printf ("\n");
239 }
240
241
242 for (sect = s_first; sect<=s_last; sect++) {
243 if (info->protect[sect] == 0) { /* not protected */
244 flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
245 flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
246 flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
247
248 if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
249 rcode = 1;
250 } else
251 printf(".");
252 }
253 }
254 printf (" done\n");
255 return rcode;
256}
257
258/*-----------------------------------------------------------------------
259 */
260void flash_print_info (flash_info_t *info)
261{
262 int i;
263
264 if (info->flash_id != FLASH_MAN_CFI) {
265 printf ("missing or unknown FLASH type\n");
266 return;
267 }
268
269 printf("CFI conformant FLASH (%d x %d)",
270 (info->portwidth << 3 ), (info->chipwidth << 3 ));
271 printf (" Size: %ld MB in %d Sectors\n",
272 info->size >> 20, info->sector_count);
273 printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
274 info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
275
276 printf (" Sector Start Addresses:");
277 for (i=0; i<info->sector_count; ++i) {
278 if ((i % 5) == 0)
279 printf ("\n");
280 printf (" %08lX%5s",
281 info->start[i],
282 info->protect[i] ? " (RO)" : " "
283 );
284 }
285 printf ("\n");
286 return;
287}
288
289/*-----------------------------------------------------------------------
290 * Copy memory to flash, returns:
291 * 0 - OK
292 * 1 - write timeout
293 * 2 - Flash not erased
294 */
295int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
296{
297 ulong wp;
298 ulong cp;
299 int aln;
300 cfiword_t cword;
301 int i, rc;
302
303 /* get lower aligned address */
304 wp = (addr & ~(info->portwidth - 1));
305
306 /* handle unaligned start */
307 if((aln = addr - wp) != 0) {
308 cword.l = 0;
309 cp = wp;
310 for(i=0;i<aln; ++i, ++cp)
311 flash_add_byte(info, &cword, (*(uchar *)cp));
312
313 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
314 flash_add_byte(info, &cword, *src++);
315 cnt--;
316 cp++;
317 }
318 for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
319 flash_add_byte(info, &cword, (*(uchar *)cp));
320 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
321 return rc;
322 wp = cp;
323 }
324
325#ifdef CFG_FLASH_USE_BUFFER_WRITE
326 while(cnt >= info->portwidth) {
327 i = info->buffer_size > cnt? cnt: info->buffer_size;
328 if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
329 return rc;
330 wp += i;
331 src += i;
332 cnt -=i;
333 }
334#else
335 /* handle the aligned part */
336 while(cnt >= info->portwidth) {
337 cword.l = 0;
338 for(i = 0; i < info->portwidth; i++) {
339 flash_add_byte(info, &cword, *src++);
340 }
341 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
342 return rc;
343 wp += info->portwidth;
344 cnt -= info->portwidth;
345 }
346#endif /* CFG_FLASH_USE_BUFFER_WRITE */
347 if (cnt == 0) {
348 return (0);
349 }
350
351 /*
352 * handle unaligned tail bytes
353 */
354 cword.l = 0;
355 for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
356 flash_add_byte(info, &cword, *src++);
357 --cnt;
358 }
359 for (; i<info->portwidth; ++i, ++cp) {
360 flash_add_byte(info, & cword, (*(uchar *)cp));
361 }
362
363 return flash_write_cfiword(info, wp, cword);
364}
365
366/*-----------------------------------------------------------------------
367 */
368int flash_real_protect(flash_info_t *info, long sector, int prot)
369{
370 int retcode = 0;
371
372 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
373 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
374 if(prot)
375 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
376 else
377 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
378
379 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
380 prot?"protect":"unprotect")) == 0) {
381
382 info->protect[sector] = prot;
383 /* Intel's unprotect unprotects all locking */
384 if(prot == 0) {
385 int i;
386 for(i = 0 ; i<info->sector_count; i++) {
387 if(info->protect[i])
388 flash_real_protect(info, i, 1);
389 }
390 }
391 }
392
393 return retcode;
394}
395/*-----------------------------------------------------------------------
396 * wait for XSR.7 to be set. Time out with an error if it does not.
397 * This routine does not set the flash to read-array mode.
398 */
399static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
400{
401 ulong start;
402
403 /* Wait for command completion */
404 start = get_timer (0);
405 while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
406 if (get_timer(start) > info->erase_blk_tout) {
407 printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
408 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
409 return ERR_TIMOUT;
410 }
411 }
412 return ERR_OK;
413}
414/*-----------------------------------------------------------------------
415 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
416 * This routine sets the flash to read-array mode.
417 */
418static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
419{
420 int retcode;
421 retcode = flash_status_check(info, sector, tout, prompt);
422 if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
423 retcode = ERR_INVAL;
424 printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
425 if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
426 printf("Command Sequence Error.\n");
427 } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
428 printf("Block Erase Error.\n");
429 retcode = ERR_NOT_ERASED;
430 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
431 printf("Locking Error\n");
432 }
433 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
434 printf("Block locked.\n");
435 retcode = ERR_PROTECTED;
436 }
437 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
438 printf("Vpp Low Error.\n");
439 }
440 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
441 return retcode;
442}
443/*-----------------------------------------------------------------------
444 */
445static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
446{
447 switch(info->portwidth) {
448 case FLASH_CFI_8BIT:
449 cword->c = c;
450 break;
451 case FLASH_CFI_16BIT:
452 cword->w = (cword->w << 8) | c;
453 break;
454 case FLASH_CFI_32BIT:
455 cword->l = (cword->l << 8) | c;
456 }
457}
458
459
460/*-----------------------------------------------------------------------
461 * make a proper sized command based on the port and chip widths
462 */
463static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
464{
465 int i;
466 uchar *cp = (uchar *)cmdbuf;
467 for(i=0; i< info->portwidth; i++)
468 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
469}
470
471/*
472 * Write a proper sized command to the correct address
473 */
474static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
475{
476
477 volatile cfiptr_t addr;
478 cfiword_t cword;
479 addr.cp = flash_make_addr(info, sect, offset);
480 flash_make_cmd(info, cmd, &cword);
481 switch(info->portwidth) {
482 case FLASH_CFI_8BIT:
483 *addr.cp = cword.c;
484 break;
485 case FLASH_CFI_16BIT:
486 *addr.wp = cword.w;
487 break;
488 case FLASH_CFI_32BIT:
489 *addr.lp = cword.l;
490 break;
491 }
492}
493
494/*-----------------------------------------------------------------------
495 */
496static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
497{
498 cfiptr_t cptr;
499 cfiword_t cword;
500 int retval;
501 cptr.cp = flash_make_addr(info, sect, offset);
502 flash_make_cmd(info, cmd, &cword);
503 switch(info->portwidth) {
504 case FLASH_CFI_8BIT:
505 retval = (cptr.cp[0] == cword.c);
506 break;
507 case FLASH_CFI_16BIT:
508 retval = (cptr.wp[0] == cword.w);
509 break;
510 case FLASH_CFI_32BIT:
511 retval = (cptr.lp[0] == cword.l);
512 break;
513 default:
514 retval = 0;
515 break;
516 }
517 return retval;
518}
519/*-----------------------------------------------------------------------
520 */
521static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
522{
523 cfiptr_t cptr;
524 cfiword_t cword;
525 int retval;
526 cptr.cp = flash_make_addr(info, sect, offset);
527 flash_make_cmd(info, cmd, &cword);
528 switch(info->portwidth) {
529 case FLASH_CFI_8BIT:
530 retval = ((cptr.cp[0] & cword.c) == cword.c);
531 break;
532 case FLASH_CFI_16BIT:
533 retval = ((cptr.wp[0] & cword.w) == cword.w);
534 break;
535 case FLASH_CFI_32BIT:
536 retval = ((cptr.lp[0] & cword.l) == cword.l);
537 break;
538 default:
539 retval = 0;
540 break;
541 }
542 return retval;
543}
544
545/*-----------------------------------------------------------------------
546 * detect if flash is compatible with the Common Flash Interface (CFI)
547 * http://www.jedec.org/download/search/jesd68.pdf
548 *
549 */
550static int flash_detect_cfi(flash_info_t * info)
551{
552
553 for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
554 info->portwidth <<= 1) {
555 for(info->chipwidth =FLASH_CFI_BY8;
556 info->chipwidth <= info->portwidth;
557 info->chipwidth <<= 1) {
558 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
559 flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
560 if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
561 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
562 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
563 return 1;
564 }
565 }
566 return 0;
567}
568/*
569 * The following code cannot be run from FLASH!
570 *
571 */
572static ulong flash_get_size (ulong base, int banknum)
573{
574 flash_info_t * info = &flash_info[banknum];
575 int i, j;
576 int sect_cnt;
577 unsigned long sector;
578 unsigned long tmp;
579 int size_ratio;
580 uchar num_erase_regions;
581 int erase_region_size;
582 int erase_region_count;
583
584 info->start[0] = base;
585
586 if(flash_detect_cfi(info)){
587#ifdef DEBUG_FLASH
588 printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
589#endif
590 size_ratio = info->portwidth / info->chipwidth;
591 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
592#ifdef DEBUG_FLASH
593 printf("found %d erase regions\n", num_erase_regions);
594#endif
595 sect_cnt = 0;
596 sector = base;
597 for(i = 0 ; i < num_erase_regions; i++) {
598 if(i > NUM_ERASE_REGIONS) {
599 printf("%d erase regions found, only %d used\n",
600 num_erase_regions, NUM_ERASE_REGIONS);
601 break;
602 }
603 tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
604 erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
605 tmp >>= 16;
606 erase_region_count = (tmp & 0xffff) +1;
607 for(j = 0; j< erase_region_count; j++) {
608 info->start[sect_cnt] = sector;
609 sector += (erase_region_size * size_ratio);
610 info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
611 sect_cnt++;
612 }
613 }
614
615 info->sector_count = sect_cnt;
616 /* multiply the size by the number of chips */
617 info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
618 info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
619 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
620 info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
621 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
622 info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
623 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
624 info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
625 info->flash_id = FLASH_MAN_CFI;
626 }
627
628 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
629 return(info->size);
630}
631
632
633/*-----------------------------------------------------------------------
634 */
635static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
636{
637
638 cfiptr_t ctladdr;
639 cfiptr_t cptr;
640 int flag;
641
642 ctladdr.cp = flash_make_addr(info, 0, 0);
643 cptr.cp = (uchar *)dest;
644
645
646 /* Check if Flash is (sufficiently) erased */
647 switch(info->portwidth) {
648 case FLASH_CFI_8BIT:
649 flag = ((cptr.cp[0] & cword.c) == cword.c);
650 break;
651 case FLASH_CFI_16BIT:
652 flag = ((cptr.wp[0] & cword.w) == cword.w);
653 break;
654 case FLASH_CFI_32BIT:
655 flag = ((cptr.lp[0] & cword.l) == cword.l);
656 break;
657 default:
658 return 2;
659 }
660 if(!flag)
661 return 2;
662
663 /* Disable interrupts which might cause a timeout here */
664 flag = disable_interrupts();
665
666 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
667 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
668
669 switch(info->portwidth) {
670 case FLASH_CFI_8BIT:
671 cptr.cp[0] = cword.c;
672 break;
673 case FLASH_CFI_16BIT:
674 cptr.wp[0] = cword.w;
675 break;
676 case FLASH_CFI_32BIT:
677 cptr.lp[0] = cword.l;
678 break;
679 }
680
681 /* re-enable interrupts if necessary */
682 if(flag)
683 enable_interrupts();
684
685 return flash_full_status_check(info, 0, info->write_tout, "write");
686}
687
688#ifdef CFG_FLASH_USE_BUFFER_WRITE
689
690/* loop through the sectors from the highest address
691 * when the passed address is greater or equal to the sector address
692 * we have a match
693 */
694static int find_sector(flash_info_t *info, ulong addr)
695{
696 int sector;
697 for(sector = info->sector_count - 1; sector >= 0; sector--) {
698 if(addr >= info->start[sector])
699 break;
700 }
701 return sector;
702}
703
704static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
705{
706
707 int sector;
708 int cnt;
709 int retcode;
710 volatile cfiptr_t src;
711 volatile cfiptr_t dst;
712
713 src.cp = cp;
714 dst.cp = (uchar *)dest;
715 sector = find_sector(info, dest);
716 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
717 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
718 if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
719 "write to buffer")) == ERR_OK) {
720 switch(info->portwidth) {
721 case FLASH_CFI_8BIT:
722 cnt = len;
723 break;
724 case FLASH_CFI_16BIT:
725 cnt = len >> 1;
726 break;
727 case FLASH_CFI_32BIT:
728 cnt = len >> 2;
729 break;
730 default:
731 return ERR_INVAL;
732 break;
733 }
734 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
735 while(cnt-- > 0) {
736 switch(info->portwidth) {
737 case FLASH_CFI_8BIT:
738 *dst.cp++ = *src.cp++;
739 break;
740 case FLASH_CFI_16BIT:
741 *dst.wp++ = *src.wp++;
742 break;
743 case FLASH_CFI_32BIT:
744 *dst.lp++ = *src.lp++;
745 break;
746 default:
747 return ERR_INVAL;
748 break;
749 }
750 }
751 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
752 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
753 "buffer write");
754 }
755 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
756 return retcode;
757}
758#endif /* CFG_USE_FLASH_BUFFER_WRITE */