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