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