]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/g2000/strataflash.c
fd72f47310e8ced567c7acd76df608bb212a5d41
[people/ms/u-boot.git] / board / g2000 / 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 #define FLASH_MAN_CFI 0x01000000
89
90 typedef union {
91 unsigned char c;
92 unsigned short w;
93 unsigned long l;
94 } cfiword_t;
95
96 typedef union {
97 unsigned char * cp;
98 unsigned short *wp;
99 unsigned long *lp;
100 } cfiptr_t;
101
102 #define NUM_ERASE_REGIONS 4
103
104 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
105
106 /*-----------------------------------------------------------------------
107 * Functions
108 */
109
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 CONFIG_SYS_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
124 */
125 inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
126 {
127 return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
128 }
129 /*-----------------------------------------------------------------------
130 * read a character at a port width address
131 */
132 inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
133 {
134 uchar *cp;
135 cp = flash_make_addr(info, 0, offset);
136 return (cp[info->portwidth - 1]);
137 }
138
139 /*-----------------------------------------------------------------------
140 * read a short word by swapping for ppc format.
141 */
142 ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset)
143 {
144 uchar * addr;
145
146 addr = flash_make_addr(info, sect, offset);
147 return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
148
149 }
150
151 /*-----------------------------------------------------------------------
152 * read a long word by picking the least significant byte of each maiximum
153 * port size word. Swap for ppc format.
154 */
155 ulong flash_read_long(flash_info_t * info, int sect, uchar offset)
156 {
157 uchar * addr;
158
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]);
162
163 }
164
165 /*-----------------------------------------------------------------------
166 */
167 unsigned long flash_init (void)
168 {
169 unsigned long size;
170 int i;
171 unsigned long address;
172
173
174 /* The flash is positioned back to back, with the demultiplexing of the chip
175 * based on the A24 address line.
176 *
177 */
178
179 address = CONFIG_SYS_FLASH_BASE;
180 size = 0;
181
182 /* Init: no FLASHes known */
183 for (i=0; i<CONFIG_SYS_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 += CONFIG_SYS_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);
190 }
191 }
192
193 #if 0 /* test-only */
194 /* Monitor protection ON by default */
195 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
196 for(i=0; flash_info[0].start[i] < CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_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 - CONFIG_SYS_MONITOR_LEN,
203 - 1, &flash_info[1]);
204 #endif
205
206 return (size);
207 }
208
209 /*-----------------------------------------------------------------------
210 */
211 int 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 */
258 void 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 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
277 int k;
278 int size;
279 int erased;
280 volatile unsigned long *flash;
281
282 /*
283 * Check if whole sector is erased
284 */
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++)
293 {
294 if (*flash++ != 0xffffffff)
295 {
296 erased = 0;
297 break;
298 }
299 }
300
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
315 }
316 printf ("\n");
317 return;
318 }
319
320 /*-----------------------------------------------------------------------
321 * Copy memory to flash, returns:
322 * 0 - OK
323 * 1 - write timeout
324 * 2 - Flash not erased
325 */
326 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
327 {
328 ulong wp;
329 ulong cp;
330 int aln;
331 cfiword_t cword;
332 int i, rc;
333
334 /* get lower aligned address */
335 wp = (addr & ~(info->portwidth - 1));
336
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));
343
344 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
345 flash_add_byte(info, &cword, *src++);
346 cnt--;
347 cp++;
348 }
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;
354 }
355
356 #ifdef CONFIG_SYS_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;
364 }
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++);
371 }
372 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
373 return rc;
374 wp += info->portwidth;
375 cnt -= info->portwidth;
376 }
377 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
378 if (cnt == 0) {
379 return (0);
380 }
381
382 /*
383 * handle unaligned tail bytes
384 */
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;
389 }
390 for (; i<info->portwidth; ++i, ++cp) {
391 flash_add_byte(info, & cword, (*(uchar *)cp));
392 }
393
394 return flash_write_cfiword(info, wp, cword);
395 }
396
397 /*-----------------------------------------------------------------------
398 */
399 int flash_real_protect(flash_info_t *info, long sector, int prot)
400 {
401 int retcode = 0;
402
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);
409
410 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
411 prot?"protect":"unprotect")) == 0) {
412
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);
420 }
421 }
422 }
423
424 return retcode;
425 }
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.
429 */
430 static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
431 {
432 ulong start;
433
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;
441 }
442 }
443 return ERR_OK;
444 }
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.
448 */
449 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
450 {
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");
463 }
464 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
465 printf("Block locked.\n");
466 retcode = ERR_PROTECTED;
467 }
468 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
469 printf("Vpp Low Error.\n");
470 }
471 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
472 return retcode;
473 }
474 /*-----------------------------------------------------------------------
475 */
476 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
477 {
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;
487 }
488 }
489
490
491 /*-----------------------------------------------------------------------
492 * make a proper sized command based on the port and chip widths
493 */
494 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
495 {
496 int i;
497 uchar *cp = (uchar *)cmdbuf;
498 for(i=0; i< info->portwidth; i++)
499 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
500 }
501
502 /*
503 * Write a proper sized command to the correct address
504 */
505 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
506 {
507
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;
522 }
523 }
524
525 /*-----------------------------------------------------------------------
526 */
527 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
528 {
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;
547 }
548 return retval;
549 }
550 /*-----------------------------------------------------------------------
551 */
552 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
553 {
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;
572 }
573 return retval;
574 }
575
576 /*-----------------------------------------------------------------------
577 * detect if flash is compatible with the Common Flash Interface (CFI)
578 * http://www.jedec.org/download/search/jesd68.pdf
579 *
580 */
581 static int flash_detect_cfi(flash_info_t * info)
582 {
583
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;
595 }
596 }
597 return 0;
598 }
599 /*
600 * The following code cannot be run from FLASH!
601 *
602 */
603 static ulong flash_get_size (ulong base, int banknum)
604 {
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;
614
615 info->start[0] = base;
616
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;
633 }
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++;
643 }
644 }
645
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;
657 }
658
659 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
660 return(info->size);
661 }
662
663
664 /*-----------------------------------------------------------------------
665 */
666 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
667 {
668
669 cfiptr_t cptr;
670 int flag;
671
672 cptr.cp = (uchar *)dest;
673
674 /* Check if Flash is (sufficiently) erased */
675 switch(info->portwidth) {
676 case FLASH_CFI_8BIT:
677 flag = ((cptr.cp[0] & cword.c) == cword.c);
678 break;
679 case FLASH_CFI_16BIT:
680 flag = ((cptr.wp[0] & cword.w) == cword.w);
681 break;
682 case FLASH_CFI_32BIT:
683 flag = ((cptr.lp[0] & cword.l) == cword.l);
684 break;
685 default:
686 return 2;
687 }
688 if(!flag)
689 return 2;
690
691 /* Disable interrupts which might cause a timeout here */
692 flag = disable_interrupts();
693
694 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
695 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
696
697 switch(info->portwidth) {
698 case FLASH_CFI_8BIT:
699 cptr.cp[0] = cword.c;
700 break;
701 case FLASH_CFI_16BIT:
702 cptr.wp[0] = cword.w;
703 break;
704 case FLASH_CFI_32BIT:
705 cptr.lp[0] = cword.l;
706 break;
707 }
708
709 /* re-enable interrupts if necessary */
710 if(flag)
711 enable_interrupts();
712
713 return flash_full_status_check(info, 0, info->write_tout, "write");
714 }
715
716 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
717
718 /* loop through the sectors from the highest address
719 * when the passed address is greater or equal to the sector address
720 * we have a match
721 */
722 static int find_sector(flash_info_t *info, ulong addr)
723 {
724 int sector;
725 for(sector = info->sector_count - 1; sector >= 0; sector--) {
726 if(addr >= info->start[sector])
727 break;
728 }
729 return sector;
730 }
731
732 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
733 {
734
735 int sector;
736 int cnt;
737 int retcode;
738 volatile cfiptr_t src;
739 volatile cfiptr_t dst;
740
741 src.cp = cp;
742 dst.cp = (uchar *)dest;
743 sector = find_sector(info, dest);
744 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
745 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
746 if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
747 "write to buffer")) == ERR_OK) {
748 switch(info->portwidth) {
749 case FLASH_CFI_8BIT:
750 cnt = len;
751 break;
752 case FLASH_CFI_16BIT:
753 cnt = len >> 1;
754 if (len & 0x1) { /* test-only: unaligned size */
755 puts("\nUnalgined size!!!\n"); /* test-only */
756 cnt++;
757 }
758 break;
759 case FLASH_CFI_32BIT:
760 cnt = len >> 2;
761 break;
762 default:
763 return ERR_INVAL;
764 break;
765 }
766 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
767 while(cnt-- > 0) {
768 switch(info->portwidth) {
769 case FLASH_CFI_8BIT:
770 *dst.cp++ = *src.cp++;
771 break;
772 case FLASH_CFI_16BIT:
773 *dst.wp++ = *src.wp++;
774 break;
775 case FLASH_CFI_32BIT:
776 *dst.lp++ = *src.lp++;
777 break;
778 default:
779 return ERR_INVAL;
780 break;
781 }
782 }
783 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
784 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
785 "buffer write");
786 }
787 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
788 return retcode;
789 }
790 #endif /* CONFIG_SYS_USE_FLASH_BUFFER_WRITE */