xref: /rk3399_rockchip-uboot/drivers/mtd/cfi_flash.c (revision 2e97394a6d07a36dfc139b7b98b12e452b5bd8dc)
1 /*
2  * (C) Copyright 2002-2004
3  * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
4  *
5  * Copyright (C) 2003 Arabella Software Ltd.
6  * Yuli Barcohen <yuli@arabellasw.com>
7  *
8  * Copyright (C) 2004
9  * Ed Okerson
10  *
11  * Copyright (C) 2006
12  * Tolunay Orkun <listmember@orkun.us>
13  *
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  *
32  */
33 
34 /* The DEBUG define must be before common to enable debugging */
35 /* #define DEBUG	*/
36 
37 #include <common.h>
38 #include <asm/processor.h>
39 #include <asm/io.h>
40 #include <asm/byteorder.h>
41 #include <environment.h>
42 #include <mtd/cfi_flash.h>
43 
44 /*
45  * This file implements a Common Flash Interface (CFI) driver for
46  * U-Boot.
47  *
48  * The width of the port and the width of the chips are determined at
49  * initialization.  These widths are used to calculate the address for
50  * access CFI data structures.
51  *
52  * References
53  * JEDEC Standard JESD68 - Common Flash Interface (CFI)
54  * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
55  * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
56  * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
57  * AMD CFI Specification, Release 2.0 December 1, 2001
58  * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
59  *   Device IDs, Publication Number 25538 Revision A, November 8, 2001
60  *
61  * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
62  * reading and writing ... (yes there is such a Hardware).
63  */
64 
65 #ifndef CONFIG_SYS_FLASH_BANKS_LIST
66 #define CONFIG_SYS_FLASH_BANKS_LIST { CONFIG_SYS_FLASH_BASE }
67 #endif
68 
69 static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
70 static uint flash_verbose = 1;
71 
72 /* use CONFIG_SYS_MAX_FLASH_BANKS_DETECT if defined */
73 #ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
74 # define CFI_MAX_FLASH_BANKS	CONFIG_SYS_MAX_FLASH_BANKS_DETECT
75 #else
76 # define CFI_MAX_FLASH_BANKS	CONFIG_SYS_MAX_FLASH_BANKS
77 #endif
78 
79 flash_info_t flash_info[CFI_MAX_FLASH_BANKS];	/* FLASH chips info */
80 
81 /*
82  * Check if chip width is defined. If not, start detecting with 8bit.
83  */
84 #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
85 #define CONFIG_SYS_FLASH_CFI_WIDTH	FLASH_CFI_8BIT
86 #endif
87 
88 static void __flash_write8(u8 value, void *addr)
89 {
90 	__raw_writeb(value, addr);
91 }
92 
93 static void __flash_write16(u16 value, void *addr)
94 {
95 	__raw_writew(value, addr);
96 }
97 
98 static void __flash_write32(u32 value, void *addr)
99 {
100 	__raw_writel(value, addr);
101 }
102 
103 static void __flash_write64(u64 value, void *addr)
104 {
105 	/* No architectures currently implement __raw_writeq() */
106 	*(volatile u64 *)addr = value;
107 }
108 
109 static u8 __flash_read8(void *addr)
110 {
111 	return __raw_readb(addr);
112 }
113 
114 static u16 __flash_read16(void *addr)
115 {
116 	return __raw_readw(addr);
117 }
118 
119 static u32 __flash_read32(void *addr)
120 {
121 	return __raw_readl(addr);
122 }
123 
124 static u64 __flash_read64(void *addr)
125 {
126 	/* No architectures currently implement __raw_readq() */
127 	return *(volatile u64 *)addr;
128 }
129 
130 #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
131 void flash_write8(u8 value, void *addr)__attribute__((weak, alias("__flash_write8")));
132 void flash_write16(u16 value, void *addr)__attribute__((weak, alias("__flash_write16")));
133 void flash_write32(u32 value, void *addr)__attribute__((weak, alias("__flash_write32")));
134 void flash_write64(u64 value, void *addr)__attribute__((weak, alias("__flash_write64")));
135 u8 flash_read8(void *addr)__attribute__((weak, alias("__flash_read8")));
136 u16 flash_read16(void *addr)__attribute__((weak, alias("__flash_read16")));
137 u32 flash_read32(void *addr)__attribute__((weak, alias("__flash_read32")));
138 u64 flash_read64(void *addr)__attribute__((weak, alias("__flash_read64")));
139 #else
140 #define flash_write8	__flash_write8
141 #define flash_write16	__flash_write16
142 #define flash_write32	__flash_write32
143 #define flash_write64	__flash_write64
144 #define flash_read8	__flash_read8
145 #define flash_read16	__flash_read16
146 #define flash_read32	__flash_read32
147 #define flash_read64	__flash_read64
148 #endif
149 
150 /*-----------------------------------------------------------------------
151  */
152 #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
153 flash_info_t *flash_get_info(ulong base)
154 {
155 	int i;
156 	flash_info_t * info = 0;
157 
158 	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
159 		info = & flash_info[i];
160 		if (info->size && info->start[0] <= base &&
161 		    base <= info->start[0] + info->size - 1)
162 			break;
163 	}
164 
165 	return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
166 }
167 #endif
168 
169 unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
170 {
171 	if (sect != (info->sector_count - 1))
172 		return info->start[sect + 1] - info->start[sect];
173 	else
174 		return info->start[0] + info->size - info->start[sect];
175 }
176 
177 /*-----------------------------------------------------------------------
178  * create an address based on the offset and the port width
179  */
180 static inline void *
181 flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
182 {
183 	unsigned int byte_offset = offset * info->portwidth;
184 
185 	return (void *)(info->start[sect] + byte_offset);
186 }
187 
188 static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
189 		unsigned int offset, void *addr)
190 {
191 }
192 
193 /*-----------------------------------------------------------------------
194  * make a proper sized command based on the port and chip widths
195  */
196 static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
197 {
198 	int i;
199 	int cword_offset;
200 	int cp_offset;
201 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
202 	u32 cmd_le = cpu_to_le32(cmd);
203 #endif
204 	uchar val;
205 	uchar *cp = (uchar *) cmdbuf;
206 
207 	for (i = info->portwidth; i > 0; i--){
208 		cword_offset = (info->portwidth-i)%info->chipwidth;
209 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
210 		cp_offset = info->portwidth - i;
211 		val = *((uchar*)&cmd_le + cword_offset);
212 #else
213 		cp_offset = i - 1;
214 		val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
215 #endif
216 		cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
217 	}
218 }
219 
220 #ifdef DEBUG
221 /*-----------------------------------------------------------------------
222  * Debug support
223  */
224 static void print_longlong (char *str, unsigned long long data)
225 {
226 	int i;
227 	char *cp;
228 
229 	cp = (char *) &data;
230 	for (i = 0; i < 8; i++)
231 		sprintf (&str[i * 2], "%2.2x", *cp++);
232 }
233 
234 static void flash_printqry (struct cfi_qry *qry)
235 {
236 	u8 *p = (u8 *)qry;
237 	int x, y;
238 
239 	for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
240 		debug("%02x : ", x);
241 		for (y = 0; y < 16; y++)
242 			debug("%2.2x ", p[x + y]);
243 		debug(" ");
244 		for (y = 0; y < 16; y++) {
245 			unsigned char c = p[x + y];
246 			if (c >= 0x20 && c <= 0x7e)
247 				debug("%c", c);
248 			else
249 				debug(".");
250 		}
251 		debug("\n");
252 	}
253 }
254 #endif
255 
256 
257 /*-----------------------------------------------------------------------
258  * read a character at a port width address
259  */
260 static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
261 {
262 	uchar *cp;
263 	uchar retval;
264 
265 	cp = flash_map (info, 0, offset);
266 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
267 	retval = flash_read8(cp);
268 #else
269 	retval = flash_read8(cp + info->portwidth - 1);
270 #endif
271 	flash_unmap (info, 0, offset, cp);
272 	return retval;
273 }
274 
275 /*-----------------------------------------------------------------------
276  * read a word at a port width address, assume 16bit bus
277  */
278 static inline ushort flash_read_word (flash_info_t * info, uint offset)
279 {
280 	ushort *addr, retval;
281 
282 	addr = flash_map (info, 0, offset);
283 	retval = flash_read16 (addr);
284 	flash_unmap (info, 0, offset, addr);
285 	return retval;
286 }
287 
288 
289 /*-----------------------------------------------------------------------
290  * read a long word by picking the least significant byte of each maximum
291  * port size word. Swap for ppc format.
292  */
293 static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
294 			      uint offset)
295 {
296 	uchar *addr;
297 	ulong retval;
298 
299 #ifdef DEBUG
300 	int x;
301 #endif
302 	addr = flash_map (info, sect, offset);
303 
304 #ifdef DEBUG
305 	debug ("long addr is at %p info->portwidth = %d\n", addr,
306 	       info->portwidth);
307 	for (x = 0; x < 4 * info->portwidth; x++) {
308 		debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
309 	}
310 #endif
311 #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
312 	retval = ((flash_read8(addr) << 16) |
313 		  (flash_read8(addr + info->portwidth) << 24) |
314 		  (flash_read8(addr + 2 * info->portwidth)) |
315 		  (flash_read8(addr + 3 * info->portwidth) << 8));
316 #else
317 	retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
318 		  (flash_read8(addr + info->portwidth - 1) << 16) |
319 		  (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
320 		  (flash_read8(addr + 3 * info->portwidth - 1)));
321 #endif
322 	flash_unmap(info, sect, offset, addr);
323 
324 	return retval;
325 }
326 
327 /*
328  * Write a proper sized command to the correct address
329  */
330 void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
331 		      uint offset, u32 cmd)
332 {
333 
334 	void *addr;
335 	cfiword_t cword;
336 
337 	addr = flash_map (info, sect, offset);
338 	flash_make_cmd (info, cmd, &cword);
339 	switch (info->portwidth) {
340 	case FLASH_CFI_8BIT:
341 		debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
342 		       cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
343 		flash_write8(cword.c, addr);
344 		break;
345 	case FLASH_CFI_16BIT:
346 		debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
347 		       cmd, cword.w,
348 		       info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
349 		flash_write16(cword.w, addr);
350 		break;
351 	case FLASH_CFI_32BIT:
352 		debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr,
353 		       cmd, cword.l,
354 		       info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
355 		flash_write32(cword.l, addr);
356 		break;
357 	case FLASH_CFI_64BIT:
358 #ifdef DEBUG
359 		{
360 			char str[20];
361 
362 			print_longlong (str, cword.ll);
363 
364 			debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
365 			       addr, cmd, str,
366 			       info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
367 		}
368 #endif
369 		flash_write64(cword.ll, addr);
370 		break;
371 	}
372 
373 	/* Ensure all the instructions are fully finished */
374 	sync();
375 
376 	flash_unmap(info, sect, offset, addr);
377 }
378 
379 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
380 {
381 	flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
382 	flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
383 }
384 
385 /*-----------------------------------------------------------------------
386  */
387 static int flash_isequal (flash_info_t * info, flash_sect_t sect,
388 			  uint offset, uchar cmd)
389 {
390 	void *addr;
391 	cfiword_t cword;
392 	int retval;
393 
394 	addr = flash_map (info, sect, offset);
395 	flash_make_cmd (info, cmd, &cword);
396 
397 	debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
398 	switch (info->portwidth) {
399 	case FLASH_CFI_8BIT:
400 		debug ("is= %x %x\n", flash_read8(addr), cword.c);
401 		retval = (flash_read8(addr) == cword.c);
402 		break;
403 	case FLASH_CFI_16BIT:
404 		debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w);
405 		retval = (flash_read16(addr) == cword.w);
406 		break;
407 	case FLASH_CFI_32BIT:
408 		debug ("is= %8.8x %8.8lx\n", flash_read32(addr), cword.l);
409 		retval = (flash_read32(addr) == cword.l);
410 		break;
411 	case FLASH_CFI_64BIT:
412 #ifdef DEBUG
413 		{
414 			char str1[20];
415 			char str2[20];
416 
417 			print_longlong (str1, flash_read64(addr));
418 			print_longlong (str2, cword.ll);
419 			debug ("is= %s %s\n", str1, str2);
420 		}
421 #endif
422 		retval = (flash_read64(addr) == cword.ll);
423 		break;
424 	default:
425 		retval = 0;
426 		break;
427 	}
428 	flash_unmap(info, sect, offset, addr);
429 
430 	return retval;
431 }
432 
433 /*-----------------------------------------------------------------------
434  */
435 static int flash_isset (flash_info_t * info, flash_sect_t sect,
436 			uint offset, uchar cmd)
437 {
438 	void *addr;
439 	cfiword_t cword;
440 	int retval;
441 
442 	addr = flash_map (info, sect, offset);
443 	flash_make_cmd (info, cmd, &cword);
444 	switch (info->portwidth) {
445 	case FLASH_CFI_8BIT:
446 		retval = ((flash_read8(addr) & cword.c) == cword.c);
447 		break;
448 	case FLASH_CFI_16BIT:
449 		retval = ((flash_read16(addr) & cword.w) == cword.w);
450 		break;
451 	case FLASH_CFI_32BIT:
452 		retval = ((flash_read32(addr) & cword.l) == cword.l);
453 		break;
454 	case FLASH_CFI_64BIT:
455 		retval = ((flash_read64(addr) & cword.ll) == cword.ll);
456 		break;
457 	default:
458 		retval = 0;
459 		break;
460 	}
461 	flash_unmap(info, sect, offset, addr);
462 
463 	return retval;
464 }
465 
466 /*-----------------------------------------------------------------------
467  */
468 static int flash_toggle (flash_info_t * info, flash_sect_t sect,
469 			 uint offset, uchar cmd)
470 {
471 	void *addr;
472 	cfiword_t cword;
473 	int retval;
474 
475 	addr = flash_map (info, sect, offset);
476 	flash_make_cmd (info, cmd, &cword);
477 	switch (info->portwidth) {
478 	case FLASH_CFI_8BIT:
479 		retval = flash_read8(addr) != flash_read8(addr);
480 		break;
481 	case FLASH_CFI_16BIT:
482 		retval = flash_read16(addr) != flash_read16(addr);
483 		break;
484 	case FLASH_CFI_32BIT:
485 		retval = flash_read32(addr) != flash_read32(addr);
486 		break;
487 	case FLASH_CFI_64BIT:
488 		retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
489 			   (flash_read32(addr+4) != flash_read32(addr+4)) );
490 		break;
491 	default:
492 		retval = 0;
493 		break;
494 	}
495 	flash_unmap(info, sect, offset, addr);
496 
497 	return retval;
498 }
499 
500 /*
501  * flash_is_busy - check to see if the flash is busy
502  *
503  * This routine checks the status of the chip and returns true if the
504  * chip is busy.
505  */
506 static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
507 {
508 	int retval;
509 
510 	switch (info->vendor) {
511 	case CFI_CMDSET_INTEL_PROG_REGIONS:
512 	case CFI_CMDSET_INTEL_STANDARD:
513 	case CFI_CMDSET_INTEL_EXTENDED:
514 		retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
515 		break;
516 	case CFI_CMDSET_AMD_STANDARD:
517 	case CFI_CMDSET_AMD_EXTENDED:
518 #ifdef CONFIG_FLASH_CFI_LEGACY
519 	case CFI_CMDSET_AMD_LEGACY:
520 #endif
521 		retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
522 		break;
523 	default:
524 		retval = 0;
525 	}
526 	debug ("flash_is_busy: %d\n", retval);
527 	return retval;
528 }
529 
530 /*-----------------------------------------------------------------------
531  *  wait for XSR.7 to be set. Time out with an error if it does not.
532  *  This routine does not set the flash to read-array mode.
533  */
534 static int flash_status_check (flash_info_t * info, flash_sect_t sector,
535 			       ulong tout, char *prompt)
536 {
537 	ulong start;
538 
539 #if CONFIG_SYS_HZ != 1000
540 	if ((ulong)CONFIG_SYS_HZ > 100000)
541 		tout *= (ulong)CONFIG_SYS_HZ / 1000;  /* for a big HZ, avoid overflow */
542 	else
543 		tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
544 #endif
545 
546 	/* Wait for command completion */
547 	reset_timer();
548 	start = get_timer (0);
549 	while (flash_is_busy (info, sector)) {
550 		if (get_timer (start) > tout) {
551 			printf ("Flash %s timeout at address %lx data %lx\n",
552 				prompt, info->start[sector],
553 				flash_read_long (info, sector, 0));
554 			flash_write_cmd (info, sector, 0, info->cmd_reset);
555 			return ERR_TIMOUT;
556 		}
557 		udelay (1);		/* also triggers watchdog */
558 	}
559 	return ERR_OK;
560 }
561 
562 /*-----------------------------------------------------------------------
563  * Wait for XSR.7 to be set, if it times out print an error, otherwise
564  * do a full status check.
565  *
566  * This routine sets the flash to read-array mode.
567  */
568 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
569 				    ulong tout, char *prompt)
570 {
571 	int retcode;
572 
573 	retcode = flash_status_check (info, sector, tout, prompt);
574 	switch (info->vendor) {
575 	case CFI_CMDSET_INTEL_PROG_REGIONS:
576 	case CFI_CMDSET_INTEL_EXTENDED:
577 	case CFI_CMDSET_INTEL_STANDARD:
578 		if ((retcode != ERR_OK)
579 		    && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
580 			retcode = ERR_INVAL;
581 			printf ("Flash %s error at address %lx\n", prompt,
582 				info->start[sector]);
583 			if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
584 					 FLASH_STATUS_PSLBS)) {
585 				puts ("Command Sequence Error.\n");
586 			} else if (flash_isset (info, sector, 0,
587 						FLASH_STATUS_ECLBS)) {
588 				puts ("Block Erase Error.\n");
589 				retcode = ERR_NOT_ERASED;
590 			} else if (flash_isset (info, sector, 0,
591 						FLASH_STATUS_PSLBS)) {
592 				puts ("Locking Error\n");
593 			}
594 			if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
595 				puts ("Block locked.\n");
596 				retcode = ERR_PROTECTED;
597 			}
598 			if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
599 				puts ("Vpp Low Error.\n");
600 		}
601 		flash_write_cmd (info, sector, 0, info->cmd_reset);
602 		break;
603 	default:
604 		break;
605 	}
606 	return retcode;
607 }
608 
609 static int use_flash_status_poll(flash_info_t *info)
610 {
611 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
612 	if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
613 	    info->vendor == CFI_CMDSET_AMD_STANDARD)
614 		return 1;
615 #endif
616 	return 0;
617 }
618 
619 static int flash_status_poll(flash_info_t *info, void *src, void *dst,
620 			     ulong tout, char *prompt)
621 {
622 #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
623 	ulong start;
624 	int ready;
625 
626 #if CONFIG_SYS_HZ != 1000
627 	if ((ulong)CONFIG_SYS_HZ > 100000)
628 		tout *= (ulong)CONFIG_SYS_HZ / 1000;  /* for a big HZ, avoid overflow */
629 	else
630 		tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
631 #endif
632 
633 	/* Wait for command completion */
634 	reset_timer();
635 	start = get_timer(0);
636 	while (1) {
637 		switch (info->portwidth) {
638 		case FLASH_CFI_8BIT:
639 			ready = flash_read8(dst) == flash_read8(src);
640 			break;
641 		case FLASH_CFI_16BIT:
642 			ready = flash_read16(dst) == flash_read16(src);
643 			break;
644 		case FLASH_CFI_32BIT:
645 			ready = flash_read32(dst) == flash_read32(src);
646 			break;
647 		case FLASH_CFI_64BIT:
648 			ready = flash_read64(dst) == flash_read64(src);
649 			break;
650 		default:
651 			ready = 0;
652 			break;
653 		}
654 		if (ready)
655 			break;
656 		if (get_timer(start) > tout) {
657 			printf("Flash %s timeout at address %lx data %lx\n",
658 			       prompt, (ulong)dst, (ulong)flash_read8(dst));
659 			return ERR_TIMOUT;
660 		}
661 		udelay(1);		/* also triggers watchdog */
662 	}
663 #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
664 	return ERR_OK;
665 }
666 
667 /*-----------------------------------------------------------------------
668  */
669 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
670 {
671 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
672 	unsigned short	w;
673 	unsigned int	l;
674 	unsigned long long ll;
675 #endif
676 
677 	switch (info->portwidth) {
678 	case FLASH_CFI_8BIT:
679 		cword->c = c;
680 		break;
681 	case FLASH_CFI_16BIT:
682 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
683 		w = c;
684 		w <<= 8;
685 		cword->w = (cword->w >> 8) | w;
686 #else
687 		cword->w = (cword->w << 8) | c;
688 #endif
689 		break;
690 	case FLASH_CFI_32BIT:
691 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
692 		l = c;
693 		l <<= 24;
694 		cword->l = (cword->l >> 8) | l;
695 #else
696 		cword->l = (cword->l << 8) | c;
697 #endif
698 		break;
699 	case FLASH_CFI_64BIT:
700 #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
701 		ll = c;
702 		ll <<= 56;
703 		cword->ll = (cword->ll >> 8) | ll;
704 #else
705 		cword->ll = (cword->ll << 8) | c;
706 #endif
707 		break;
708 	}
709 }
710 
711 /*
712  * Loop through the sector table starting from the previously found sector.
713  * Searches forwards or backwards, dependent on the passed address.
714  */
715 static flash_sect_t find_sector (flash_info_t * info, ulong addr)
716 {
717 	static flash_sect_t saved_sector = 0; /* previously found sector */
718 	flash_sect_t sector = saved_sector;
719 
720 	while ((info->start[sector] < addr)
721 			&& (sector < info->sector_count - 1))
722 		sector++;
723 	while ((info->start[sector] > addr) && (sector > 0))
724 		/*
725 		 * also decrements the sector in case of an overshot
726 		 * in the first loop
727 		 */
728 		sector--;
729 
730 	saved_sector = sector;
731 	return sector;
732 }
733 
734 /*-----------------------------------------------------------------------
735  */
736 static int flash_write_cfiword (flash_info_t * info, ulong dest,
737 				cfiword_t cword)
738 {
739 	void *dstaddr = (void *)dest;
740 	int flag;
741 	flash_sect_t sect = 0;
742 	char sect_found = 0;
743 
744 	/* Check if Flash is (sufficiently) erased */
745 	switch (info->portwidth) {
746 	case FLASH_CFI_8BIT:
747 		flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
748 		break;
749 	case FLASH_CFI_16BIT:
750 		flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
751 		break;
752 	case FLASH_CFI_32BIT:
753 		flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
754 		break;
755 	case FLASH_CFI_64BIT:
756 		flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
757 		break;
758 	default:
759 		flag = 0;
760 		break;
761 	}
762 	if (!flag)
763 		return ERR_NOT_ERASED;
764 
765 	/* Disable interrupts which might cause a timeout here */
766 	flag = disable_interrupts ();
767 
768 	switch (info->vendor) {
769 	case CFI_CMDSET_INTEL_PROG_REGIONS:
770 	case CFI_CMDSET_INTEL_EXTENDED:
771 	case CFI_CMDSET_INTEL_STANDARD:
772 		flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
773 		flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
774 		break;
775 	case CFI_CMDSET_AMD_EXTENDED:
776 	case CFI_CMDSET_AMD_STANDARD:
777 		sect = find_sector(info, dest);
778 		flash_unlock_seq (info, sect);
779 		flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
780 		sect_found = 1;
781 		break;
782 #ifdef CONFIG_FLASH_CFI_LEGACY
783 	case CFI_CMDSET_AMD_LEGACY:
784 		sect = find_sector(info, dest);
785 		flash_unlock_seq (info, 0);
786 		flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
787 		sect_found = 1;
788 		break;
789 #endif
790 	}
791 
792 	switch (info->portwidth) {
793 	case FLASH_CFI_8BIT:
794 		flash_write8(cword.c, dstaddr);
795 		break;
796 	case FLASH_CFI_16BIT:
797 		flash_write16(cword.w, dstaddr);
798 		break;
799 	case FLASH_CFI_32BIT:
800 		flash_write32(cword.l, dstaddr);
801 		break;
802 	case FLASH_CFI_64BIT:
803 		flash_write64(cword.ll, dstaddr);
804 		break;
805 	}
806 
807 	/* re-enable interrupts if necessary */
808 	if (flag)
809 		enable_interrupts ();
810 
811 	if (!sect_found)
812 		sect = find_sector (info, dest);
813 
814 	if (use_flash_status_poll(info))
815 		return flash_status_poll(info, &cword, dstaddr,
816 					 info->write_tout, "write");
817 	else
818 		return flash_full_status_check(info, sect,
819 					       info->write_tout, "write");
820 }
821 
822 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
823 
824 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
825 				  int len)
826 {
827 	flash_sect_t sector;
828 	int cnt;
829 	int retcode;
830 	void *src = cp;
831 	void *dst = (void *)dest;
832 	void *dst2 = dst;
833 	int flag = 0;
834 	uint offset = 0;
835 	unsigned int shift;
836 	uchar write_cmd;
837 
838 	switch (info->portwidth) {
839 	case FLASH_CFI_8BIT:
840 		shift = 0;
841 		break;
842 	case FLASH_CFI_16BIT:
843 		shift = 1;
844 		break;
845 	case FLASH_CFI_32BIT:
846 		shift = 2;
847 		break;
848 	case FLASH_CFI_64BIT:
849 		shift = 3;
850 		break;
851 	default:
852 		retcode = ERR_INVAL;
853 		goto out_unmap;
854 	}
855 
856 	cnt = len >> shift;
857 
858 	while ((cnt-- > 0) && (flag == 0)) {
859 		switch (info->portwidth) {
860 		case FLASH_CFI_8BIT:
861 			flag = ((flash_read8(dst2) & flash_read8(src)) ==
862 				flash_read8(src));
863 			src += 1, dst2 += 1;
864 			break;
865 		case FLASH_CFI_16BIT:
866 			flag = ((flash_read16(dst2) & flash_read16(src)) ==
867 				flash_read16(src));
868 			src += 2, dst2 += 2;
869 			break;
870 		case FLASH_CFI_32BIT:
871 			flag = ((flash_read32(dst2) & flash_read32(src)) ==
872 				flash_read32(src));
873 			src += 4, dst2 += 4;
874 			break;
875 		case FLASH_CFI_64BIT:
876 			flag = ((flash_read64(dst2) & flash_read64(src)) ==
877 				flash_read64(src));
878 			src += 8, dst2 += 8;
879 			break;
880 		}
881 	}
882 	if (!flag) {
883 		retcode = ERR_NOT_ERASED;
884 		goto out_unmap;
885 	}
886 
887 	src = cp;
888 	sector = find_sector (info, dest);
889 
890 	switch (info->vendor) {
891 	case CFI_CMDSET_INTEL_PROG_REGIONS:
892 	case CFI_CMDSET_INTEL_STANDARD:
893 	case CFI_CMDSET_INTEL_EXTENDED:
894 		write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
895 					FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
896 		flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
897 		flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
898 		flash_write_cmd (info, sector, 0, write_cmd);
899 		retcode = flash_status_check (info, sector,
900 					      info->buffer_write_tout,
901 					      "write to buffer");
902 		if (retcode == ERR_OK) {
903 			/* reduce the number of loops by the width of
904 			 * the port */
905 			cnt = len >> shift;
906 			flash_write_cmd (info, sector, 0, cnt - 1);
907 			while (cnt-- > 0) {
908 				switch (info->portwidth) {
909 				case FLASH_CFI_8BIT:
910 					flash_write8(flash_read8(src), dst);
911 					src += 1, dst += 1;
912 					break;
913 				case FLASH_CFI_16BIT:
914 					flash_write16(flash_read16(src), dst);
915 					src += 2, dst += 2;
916 					break;
917 				case FLASH_CFI_32BIT:
918 					flash_write32(flash_read32(src), dst);
919 					src += 4, dst += 4;
920 					break;
921 				case FLASH_CFI_64BIT:
922 					flash_write64(flash_read64(src), dst);
923 					src += 8, dst += 8;
924 					break;
925 				default:
926 					retcode = ERR_INVAL;
927 					goto out_unmap;
928 				}
929 			}
930 			flash_write_cmd (info, sector, 0,
931 					 FLASH_CMD_WRITE_BUFFER_CONFIRM);
932 			retcode = flash_full_status_check (
933 				info, sector, info->buffer_write_tout,
934 				"buffer write");
935 		}
936 
937 		break;
938 
939 	case CFI_CMDSET_AMD_STANDARD:
940 	case CFI_CMDSET_AMD_EXTENDED:
941 		flash_unlock_seq(info,0);
942 
943 #ifdef CONFIG_FLASH_SPANSION_S29WS_N
944 		offset = ((unsigned long)dst - info->start[sector]) >> shift;
945 #endif
946 		flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
947 		cnt = len >> shift;
948 		flash_write_cmd(info, sector, offset, cnt - 1);
949 
950 		switch (info->portwidth) {
951 		case FLASH_CFI_8BIT:
952 			while (cnt-- > 0) {
953 				flash_write8(flash_read8(src), dst);
954 				src += 1, dst += 1;
955 			}
956 			break;
957 		case FLASH_CFI_16BIT:
958 			while (cnt-- > 0) {
959 				flash_write16(flash_read16(src), dst);
960 				src += 2, dst += 2;
961 			}
962 			break;
963 		case FLASH_CFI_32BIT:
964 			while (cnt-- > 0) {
965 				flash_write32(flash_read32(src), dst);
966 				src += 4, dst += 4;
967 			}
968 			break;
969 		case FLASH_CFI_64BIT:
970 			while (cnt-- > 0) {
971 				flash_write64(flash_read64(src), dst);
972 				src += 8, dst += 8;
973 			}
974 			break;
975 		default:
976 			retcode = ERR_INVAL;
977 			goto out_unmap;
978 		}
979 
980 		flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
981 		if (use_flash_status_poll(info))
982 			retcode = flash_status_poll(info, src - (1 << shift),
983 						    dst - (1 << shift),
984 						    info->buffer_write_tout,
985 						    "buffer write");
986 		else
987 			retcode = flash_full_status_check(info, sector,
988 							  info->buffer_write_tout,
989 							  "buffer write");
990 		break;
991 
992 	default:
993 		debug ("Unknown Command Set\n");
994 		retcode = ERR_INVAL;
995 		break;
996 	}
997 
998 out_unmap:
999 	return retcode;
1000 }
1001 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1002 
1003 
1004 /*-----------------------------------------------------------------------
1005  */
1006 int flash_erase (flash_info_t * info, int s_first, int s_last)
1007 {
1008 	int rcode = 0;
1009 	int prot;
1010 	flash_sect_t sect;
1011 	int st;
1012 
1013 	if (info->flash_id != FLASH_MAN_CFI) {
1014 		puts ("Can't erase unknown flash type - aborted\n");
1015 		return 1;
1016 	}
1017 	if ((s_first < 0) || (s_first > s_last)) {
1018 		puts ("- no sectors to erase\n");
1019 		return 1;
1020 	}
1021 
1022 	prot = 0;
1023 	for (sect = s_first; sect <= s_last; ++sect) {
1024 		if (info->protect[sect]) {
1025 			prot++;
1026 		}
1027 	}
1028 	if (prot) {
1029 		printf ("- Warning: %d protected sectors will not be erased!\n",
1030 			prot);
1031 	} else if (flash_verbose) {
1032 		putc ('\n');
1033 	}
1034 
1035 
1036 	for (sect = s_first; sect <= s_last; sect++) {
1037 		if (info->protect[sect] == 0) { /* not protected */
1038 			switch (info->vendor) {
1039 			case CFI_CMDSET_INTEL_PROG_REGIONS:
1040 			case CFI_CMDSET_INTEL_STANDARD:
1041 			case CFI_CMDSET_INTEL_EXTENDED:
1042 				flash_write_cmd (info, sect, 0,
1043 						 FLASH_CMD_CLEAR_STATUS);
1044 				flash_write_cmd (info, sect, 0,
1045 						 FLASH_CMD_BLOCK_ERASE);
1046 				flash_write_cmd (info, sect, 0,
1047 						 FLASH_CMD_ERASE_CONFIRM);
1048 				break;
1049 			case CFI_CMDSET_AMD_STANDARD:
1050 			case CFI_CMDSET_AMD_EXTENDED:
1051 				flash_unlock_seq (info, sect);
1052 				flash_write_cmd (info, sect,
1053 						info->addr_unlock1,
1054 						AMD_CMD_ERASE_START);
1055 				flash_unlock_seq (info, sect);
1056 				flash_write_cmd (info, sect, 0,
1057 						 AMD_CMD_ERASE_SECTOR);
1058 				break;
1059 #ifdef CONFIG_FLASH_CFI_LEGACY
1060 			case CFI_CMDSET_AMD_LEGACY:
1061 				flash_unlock_seq (info, 0);
1062 				flash_write_cmd (info, 0, info->addr_unlock1,
1063 						AMD_CMD_ERASE_START);
1064 				flash_unlock_seq (info, 0);
1065 				flash_write_cmd (info, sect, 0,
1066 						AMD_CMD_ERASE_SECTOR);
1067 				break;
1068 #endif
1069 			default:
1070 				debug ("Unkown flash vendor %d\n",
1071 				       info->vendor);
1072 				break;
1073 			}
1074 
1075 			if (use_flash_status_poll(info)) {
1076 				cfiword_t cword = (cfiword_t)0xffffffffffffffffULL;
1077 				void *dest;
1078 				dest = flash_map(info, sect, 0);
1079 				st = flash_status_poll(info, &cword, dest,
1080 						       info->erase_blk_tout, "erase");
1081 				flash_unmap(info, sect, 0, dest);
1082 			} else
1083 				st = flash_full_status_check(info, sect,
1084 							     info->erase_blk_tout,
1085 							     "erase");
1086 			if (st)
1087 				rcode = 1;
1088 			else if (flash_verbose)
1089 				putc ('.');
1090 		}
1091 	}
1092 
1093 	if (flash_verbose)
1094 		puts (" done\n");
1095 
1096 	return rcode;
1097 }
1098 
1099 /*-----------------------------------------------------------------------
1100  */
1101 void flash_print_info (flash_info_t * info)
1102 {
1103 	int i;
1104 
1105 	if (info->flash_id != FLASH_MAN_CFI) {
1106 		puts ("missing or unknown FLASH type\n");
1107 		return;
1108 	}
1109 
1110 	printf ("%s FLASH (%d x %d)",
1111 		info->name,
1112 		(info->portwidth << 3), (info->chipwidth << 3));
1113 	if (info->size < 1024*1024)
1114 		printf ("  Size: %ld kB in %d Sectors\n",
1115 			info->size >> 10, info->sector_count);
1116 	else
1117 		printf ("  Size: %ld MB in %d Sectors\n",
1118 			info->size >> 20, info->sector_count);
1119 	printf ("  ");
1120 	switch (info->vendor) {
1121 		case CFI_CMDSET_INTEL_PROG_REGIONS:
1122 			printf ("Intel Prog Regions");
1123 			break;
1124 		case CFI_CMDSET_INTEL_STANDARD:
1125 			printf ("Intel Standard");
1126 			break;
1127 		case CFI_CMDSET_INTEL_EXTENDED:
1128 			printf ("Intel Extended");
1129 			break;
1130 		case CFI_CMDSET_AMD_STANDARD:
1131 			printf ("AMD Standard");
1132 			break;
1133 		case CFI_CMDSET_AMD_EXTENDED:
1134 			printf ("AMD Extended");
1135 			break;
1136 #ifdef CONFIG_FLASH_CFI_LEGACY
1137 		case CFI_CMDSET_AMD_LEGACY:
1138 			printf ("AMD Legacy");
1139 			break;
1140 #endif
1141 		default:
1142 			printf ("Unknown (%d)", info->vendor);
1143 			break;
1144 	}
1145 	printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
1146 		info->manufacturer_id, info->device_id);
1147 	if (info->device_id == 0x7E) {
1148 		printf("%04X", info->device_id2);
1149 	}
1150 	printf ("\n  Erase timeout: %ld ms, write timeout: %ld ms\n",
1151 		info->erase_blk_tout,
1152 		info->write_tout);
1153 	if (info->buffer_size > 1) {
1154 		printf ("  Buffer write timeout: %ld ms, "
1155 			"buffer size: %d bytes\n",
1156 		info->buffer_write_tout,
1157 		info->buffer_size);
1158 	}
1159 
1160 	puts ("\n  Sector Start Addresses:");
1161 	for (i = 0; i < info->sector_count; ++i) {
1162 		if (ctrlc())
1163 			return;
1164 		if ((i % 5) == 0)
1165 			printf ("\n");
1166 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
1167 		int k;
1168 		int size;
1169 		int erased;
1170 		volatile unsigned long *flash;
1171 
1172 		/*
1173 		 * Check if whole sector is erased
1174 		 */
1175 		size = flash_sector_size(info, i);
1176 		erased = 1;
1177 		flash = (volatile unsigned long *) info->start[i];
1178 		size = size >> 2;	/* divide by 4 for longword access */
1179 		for (k = 0; k < size; k++) {
1180 			if (*flash++ != 0xffffffff) {
1181 				erased = 0;
1182 				break;
1183 			}
1184 		}
1185 
1186 		/* print empty and read-only info */
1187 		printf ("  %08lX %c %s ",
1188 			info->start[i],
1189 			erased ? 'E' : ' ',
1190 			info->protect[i] ? "RO" : "  ");
1191 #else	/* ! CONFIG_SYS_FLASH_EMPTY_INFO */
1192 		printf ("  %08lX   %s ",
1193 			info->start[i],
1194 			info->protect[i] ? "RO" : "  ");
1195 #endif
1196 	}
1197 	putc ('\n');
1198 	return;
1199 }
1200 
1201 /*-----------------------------------------------------------------------
1202  * This is used in a few places in write_buf() to show programming
1203  * progress.  Making it a function is nasty because it needs to do side
1204  * effect updates to digit and dots.  Repeated code is nasty too, so
1205  * we define it once here.
1206  */
1207 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1208 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
1209 	if (flash_verbose) { \
1210 		dots -= dots_sub; \
1211 		if ((scale > 0) && (dots <= 0)) { \
1212 			if ((digit % 5) == 0) \
1213 				printf ("%d", digit / 5); \
1214 			else \
1215 				putc ('.'); \
1216 			digit--; \
1217 			dots += scale; \
1218 		} \
1219 	}
1220 #else
1221 #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
1222 #endif
1223 
1224 /*-----------------------------------------------------------------------
1225  * Copy memory to flash, returns:
1226  * 0 - OK
1227  * 1 - write timeout
1228  * 2 - Flash not erased
1229  */
1230 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
1231 {
1232 	ulong wp;
1233 	uchar *p;
1234 	int aln;
1235 	cfiword_t cword;
1236 	int i, rc;
1237 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1238 	int buffered_size;
1239 #endif
1240 #ifdef CONFIG_FLASH_SHOW_PROGRESS
1241 	int digit = CONFIG_FLASH_SHOW_PROGRESS;
1242 	int scale = 0;
1243 	int dots  = 0;
1244 
1245 	/*
1246 	 * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
1247 	 */
1248 	if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
1249 		scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
1250 			CONFIG_FLASH_SHOW_PROGRESS);
1251 	}
1252 #endif
1253 
1254 	/* get lower aligned address */
1255 	wp = (addr & ~(info->portwidth - 1));
1256 
1257 	/* handle unaligned start */
1258 	if ((aln = addr - wp) != 0) {
1259 		cword.l = 0;
1260 		p = (uchar *)wp;
1261 		for (i = 0; i < aln; ++i)
1262 			flash_add_byte (info, &cword, flash_read8(p + i));
1263 
1264 		for (; (i < info->portwidth) && (cnt > 0); i++) {
1265 			flash_add_byte (info, &cword, *src++);
1266 			cnt--;
1267 		}
1268 		for (; (cnt == 0) && (i < info->portwidth); ++i)
1269 			flash_add_byte (info, &cword, flash_read8(p + i));
1270 
1271 		rc = flash_write_cfiword (info, wp, cword);
1272 		if (rc != 0)
1273 			return rc;
1274 
1275 		wp += i;
1276 		FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1277 	}
1278 
1279 	/* handle the aligned part */
1280 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
1281 	buffered_size = (info->portwidth / info->chipwidth);
1282 	buffered_size *= info->buffer_size;
1283 	while (cnt >= info->portwidth) {
1284 		/* prohibit buffer write when buffer_size is 1 */
1285 		if (info->buffer_size == 1) {
1286 			cword.l = 0;
1287 			for (i = 0; i < info->portwidth; i++)
1288 				flash_add_byte (info, &cword, *src++);
1289 			if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1290 				return rc;
1291 			wp += info->portwidth;
1292 			cnt -= info->portwidth;
1293 			continue;
1294 		}
1295 
1296 		/* write buffer until next buffered_size aligned boundary */
1297 		i = buffered_size - (wp % buffered_size);
1298 		if (i > cnt)
1299 			i = cnt;
1300 		if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
1301 			return rc;
1302 		i -= i & (info->portwidth - 1);
1303 		wp += i;
1304 		src += i;
1305 		cnt -= i;
1306 		FLASH_SHOW_PROGRESS(scale, dots, digit, i);
1307 	}
1308 #else
1309 	while (cnt >= info->portwidth) {
1310 		cword.l = 0;
1311 		for (i = 0; i < info->portwidth; i++) {
1312 			flash_add_byte (info, &cword, *src++);
1313 		}
1314 		if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
1315 			return rc;
1316 		wp += info->portwidth;
1317 		cnt -= info->portwidth;
1318 		FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
1319 	}
1320 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
1321 
1322 	if (cnt == 0) {
1323 		return (0);
1324 	}
1325 
1326 	/*
1327 	 * handle unaligned tail bytes
1328 	 */
1329 	cword.l = 0;
1330 	p = (uchar *)wp;
1331 	for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
1332 		flash_add_byte (info, &cword, *src++);
1333 		--cnt;
1334 	}
1335 	for (; i < info->portwidth; ++i)
1336 		flash_add_byte (info, &cword, flash_read8(p + i));
1337 
1338 	return flash_write_cfiword (info, wp, cword);
1339 }
1340 
1341 /*-----------------------------------------------------------------------
1342  */
1343 #ifdef CONFIG_SYS_FLASH_PROTECTION
1344 
1345 int flash_real_protect (flash_info_t * info, long sector, int prot)
1346 {
1347 	int retcode = 0;
1348 
1349 	switch (info->vendor) {
1350 		case CFI_CMDSET_INTEL_PROG_REGIONS:
1351 		case CFI_CMDSET_INTEL_STANDARD:
1352 		case CFI_CMDSET_INTEL_EXTENDED:
1353 			flash_write_cmd (info, sector, 0,
1354 					 FLASH_CMD_CLEAR_STATUS);
1355 			flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1356 			if (prot)
1357 				flash_write_cmd (info, sector, 0,
1358 					FLASH_CMD_PROTECT_SET);
1359 			else
1360 				flash_write_cmd (info, sector, 0,
1361 					FLASH_CMD_PROTECT_CLEAR);
1362 			break;
1363 		case CFI_CMDSET_AMD_EXTENDED:
1364 		case CFI_CMDSET_AMD_STANDARD:
1365 			/* U-Boot only checks the first byte */
1366 			if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
1367 				if (prot) {
1368 					flash_unlock_seq (info, 0);
1369 					flash_write_cmd (info, 0,
1370 							info->addr_unlock1,
1371 							ATM_CMD_SOFTLOCK_START);
1372 					flash_unlock_seq (info, 0);
1373 					flash_write_cmd (info, sector, 0,
1374 							ATM_CMD_LOCK_SECT);
1375 				} else {
1376 					flash_write_cmd (info, 0,
1377 							info->addr_unlock1,
1378 							AMD_CMD_UNLOCK_START);
1379 					if (info->device_id == ATM_ID_BV6416)
1380 						flash_write_cmd (info, sector,
1381 							0, ATM_CMD_UNLOCK_SECT);
1382 				}
1383 			}
1384 			break;
1385 #ifdef CONFIG_FLASH_CFI_LEGACY
1386 		case CFI_CMDSET_AMD_LEGACY:
1387 			flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1388 			flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
1389 			if (prot)
1390 				flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
1391 			else
1392 				flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
1393 #endif
1394 	};
1395 
1396 	if ((retcode =
1397 	     flash_full_status_check (info, sector, info->erase_blk_tout,
1398 				      prot ? "protect" : "unprotect")) == 0) {
1399 
1400 		info->protect[sector] = prot;
1401 
1402 		/*
1403 		 * On some of Intel's flash chips (marked via legacy_unlock)
1404 		 * unprotect unprotects all locking.
1405 		 */
1406 		if ((prot == 0) && (info->legacy_unlock)) {
1407 			flash_sect_t i;
1408 
1409 			for (i = 0; i < info->sector_count; i++) {
1410 				if (info->protect[i])
1411 					flash_real_protect (info, i, 1);
1412 			}
1413 		}
1414 	}
1415 	return retcode;
1416 }
1417 
1418 /*-----------------------------------------------------------------------
1419  * flash_read_user_serial - read the OneTimeProgramming cells
1420  */
1421 void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
1422 			     int len)
1423 {
1424 	uchar *src;
1425 	uchar *dst;
1426 
1427 	dst = buffer;
1428 	src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
1429 	flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1430 	memcpy (dst, src + offset, len);
1431 	flash_write_cmd (info, 0, 0, info->cmd_reset);
1432 	flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
1433 }
1434 
1435 /*
1436  * flash_read_factory_serial - read the device Id from the protection area
1437  */
1438 void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
1439 				int len)
1440 {
1441 	uchar *src;
1442 
1443 	src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
1444 	flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
1445 	memcpy (buffer, src + offset, len);
1446 	flash_write_cmd (info, 0, 0, info->cmd_reset);
1447 	flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
1448 }
1449 
1450 #endif /* CONFIG_SYS_FLASH_PROTECTION */
1451 
1452 /*-----------------------------------------------------------------------
1453  * Reverse the order of the erase regions in the CFI QRY structure.
1454  * This is needed for chips that are either a) correctly detected as
1455  * top-boot, or b) buggy.
1456  */
1457 static void cfi_reverse_geometry(struct cfi_qry *qry)
1458 {
1459 	unsigned int i, j;
1460 	u32 tmp;
1461 
1462 	for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
1463 		tmp = qry->erase_region_info[i];
1464 		qry->erase_region_info[i] = qry->erase_region_info[j];
1465 		qry->erase_region_info[j] = tmp;
1466 	}
1467 }
1468 
1469 /*-----------------------------------------------------------------------
1470  * read jedec ids from device and set corresponding fields in info struct
1471  *
1472  * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
1473  *
1474  */
1475 static void cmdset_intel_read_jedec_ids(flash_info_t *info)
1476 {
1477 	flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1478 	flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
1479 	udelay(1000); /* some flash are slow to respond */
1480 	info->manufacturer_id = flash_read_uchar (info,
1481 					FLASH_OFFSET_MANUFACTURER_ID);
1482 	info->device_id = flash_read_uchar (info,
1483 					FLASH_OFFSET_DEVICE_ID);
1484 	flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1485 }
1486 
1487 static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
1488 {
1489 	info->cmd_reset = FLASH_CMD_RESET;
1490 
1491 	cmdset_intel_read_jedec_ids(info);
1492 	flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1493 
1494 #ifdef CONFIG_SYS_FLASH_PROTECTION
1495 	/* read legacy lock/unlock bit from intel flash */
1496 	if (info->ext_addr) {
1497 		info->legacy_unlock = flash_read_uchar (info,
1498 				info->ext_addr + 5) & 0x08;
1499 	}
1500 #endif
1501 
1502 	return 0;
1503 }
1504 
1505 static void cmdset_amd_read_jedec_ids(flash_info_t *info)
1506 {
1507 	ushort bankId = 0;
1508 	uchar  manuId;
1509 
1510 	flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1511 	flash_unlock_seq(info, 0);
1512 	flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
1513 	udelay(1000); /* some flash are slow to respond */
1514 
1515 	manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
1516 	/* JEDEC JEP106Z specifies ID codes up to bank 7 */
1517 	while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
1518 		bankId += 0x100;
1519 		manuId = flash_read_uchar (info,
1520 			bankId | FLASH_OFFSET_MANUFACTURER_ID);
1521 	}
1522 	info->manufacturer_id = manuId;
1523 
1524 	switch (info->chipwidth){
1525 	case FLASH_CFI_8BIT:
1526 		info->device_id = flash_read_uchar (info,
1527 						FLASH_OFFSET_DEVICE_ID);
1528 		if (info->device_id == 0x7E) {
1529 			/* AMD 3-byte (expanded) device ids */
1530 			info->device_id2 = flash_read_uchar (info,
1531 						FLASH_OFFSET_DEVICE_ID2);
1532 			info->device_id2 <<= 8;
1533 			info->device_id2 |= flash_read_uchar (info,
1534 						FLASH_OFFSET_DEVICE_ID3);
1535 		}
1536 		break;
1537 	case FLASH_CFI_16BIT:
1538 		info->device_id = flash_read_word (info,
1539 						FLASH_OFFSET_DEVICE_ID);
1540 		break;
1541 	default:
1542 		break;
1543 	}
1544 	flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1545 }
1546 
1547 static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
1548 {
1549 	info->cmd_reset = AMD_CMD_RESET;
1550 
1551 	cmdset_amd_read_jedec_ids(info);
1552 	flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
1553 
1554 	return 0;
1555 }
1556 
1557 #ifdef CONFIG_FLASH_CFI_LEGACY
1558 static void flash_read_jedec_ids (flash_info_t * info)
1559 {
1560 	info->manufacturer_id = 0;
1561 	info->device_id       = 0;
1562 	info->device_id2      = 0;
1563 
1564 	switch (info->vendor) {
1565 	case CFI_CMDSET_INTEL_PROG_REGIONS:
1566 	case CFI_CMDSET_INTEL_STANDARD:
1567 	case CFI_CMDSET_INTEL_EXTENDED:
1568 		cmdset_intel_read_jedec_ids(info);
1569 		break;
1570 	case CFI_CMDSET_AMD_STANDARD:
1571 	case CFI_CMDSET_AMD_EXTENDED:
1572 		cmdset_amd_read_jedec_ids(info);
1573 		break;
1574 	default:
1575 		break;
1576 	}
1577 }
1578 
1579 /*-----------------------------------------------------------------------
1580  * Call board code to request info about non-CFI flash.
1581  * board_flash_get_legacy needs to fill in at least:
1582  * info->portwidth, info->chipwidth and info->interface for Jedec probing.
1583  */
1584 static int flash_detect_legacy(phys_addr_t base, int banknum)
1585 {
1586 	flash_info_t *info = &flash_info[banknum];
1587 
1588 	if (board_flash_get_legacy(base, banknum, info)) {
1589 		/* board code may have filled info completely. If not, we
1590 		   use JEDEC ID probing. */
1591 		if (!info->vendor) {
1592 			int modes[] = {
1593 				CFI_CMDSET_AMD_STANDARD,
1594 				CFI_CMDSET_INTEL_STANDARD
1595 			};
1596 			int i;
1597 
1598 			for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
1599 				info->vendor = modes[i];
1600 				info->start[0] =
1601 					(ulong)map_physmem(base,
1602 							   info->portwidth,
1603 							   MAP_NOCACHE);
1604 				if (info->portwidth == FLASH_CFI_8BIT
1605 					&& info->interface == FLASH_CFI_X8X16) {
1606 					info->addr_unlock1 = 0x2AAA;
1607 					info->addr_unlock2 = 0x5555;
1608 				} else {
1609 					info->addr_unlock1 = 0x5555;
1610 					info->addr_unlock2 = 0x2AAA;
1611 				}
1612 				flash_read_jedec_ids(info);
1613 				debug("JEDEC PROBE: ID %x %x %x\n",
1614 						info->manufacturer_id,
1615 						info->device_id,
1616 						info->device_id2);
1617 				if (jedec_flash_match(info, info->start[0]))
1618 					break;
1619 				else
1620 					unmap_physmem((void *)info->start[0],
1621 						      MAP_NOCACHE);
1622 			}
1623 		}
1624 
1625 		switch(info->vendor) {
1626 		case CFI_CMDSET_INTEL_PROG_REGIONS:
1627 		case CFI_CMDSET_INTEL_STANDARD:
1628 		case CFI_CMDSET_INTEL_EXTENDED:
1629 			info->cmd_reset = FLASH_CMD_RESET;
1630 			break;
1631 		case CFI_CMDSET_AMD_STANDARD:
1632 		case CFI_CMDSET_AMD_EXTENDED:
1633 		case CFI_CMDSET_AMD_LEGACY:
1634 			info->cmd_reset = AMD_CMD_RESET;
1635 			break;
1636 		}
1637 		info->flash_id = FLASH_MAN_CFI;
1638 		return 1;
1639 	}
1640 	return 0; /* use CFI */
1641 }
1642 #else
1643 static inline int flash_detect_legacy(phys_addr_t base, int banknum)
1644 {
1645 	return 0; /* use CFI */
1646 }
1647 #endif
1648 
1649 /*-----------------------------------------------------------------------
1650  * detect if flash is compatible with the Common Flash Interface (CFI)
1651  * http://www.jedec.org/download/search/jesd68.pdf
1652  */
1653 static void flash_read_cfi (flash_info_t *info, void *buf,
1654 		unsigned int start, size_t len)
1655 {
1656 	u8 *p = buf;
1657 	unsigned int i;
1658 
1659 	for (i = 0; i < len; i++)
1660 		p[i] = flash_read_uchar(info, start + i);
1661 }
1662 
1663 void __flash_cmd_reset(flash_info_t *info)
1664 {
1665 	/*
1666 	 * We do not yet know what kind of commandset to use, so we issue
1667 	 * the reset command in both Intel and AMD variants, in the hope
1668 	 * that AMD flash roms ignore the Intel command.
1669 	 */
1670 	flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
1671 	flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1672 }
1673 void flash_cmd_reset(flash_info_t *info)
1674 	__attribute__((weak,alias("__flash_cmd_reset")));
1675 
1676 static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1677 {
1678 	int cfi_offset;
1679 
1680 	/* Issue FLASH reset command */
1681 	flash_cmd_reset(info);
1682 
1683 	for (cfi_offset=0;
1684 	     cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
1685 	     cfi_offset++) {
1686 		flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
1687 				 FLASH_CMD_CFI);
1688 		if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
1689 		    && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
1690 		    && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
1691 			flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
1692 					sizeof(struct cfi_qry));
1693 			info->interface	= le16_to_cpu(qry->interface_desc);
1694 
1695 			info->cfi_offset = flash_offset_cfi[cfi_offset];
1696 			debug ("device interface is %d\n",
1697 			       info->interface);
1698 			debug ("found port %d chip %d ",
1699 			       info->portwidth, info->chipwidth);
1700 			debug ("port %d bits chip %d bits\n",
1701 			       info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1702 			       info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1703 
1704 			/* calculate command offsets as in the Linux driver */
1705 			info->addr_unlock1 = 0x555;
1706 			info->addr_unlock2 = 0x2aa;
1707 
1708 			/*
1709 			 * modify the unlock address if we are
1710 			 * in compatibility mode
1711 			 */
1712 			if (	/* x8/x16 in x8 mode */
1713 				((info->chipwidth == FLASH_CFI_BY8) &&
1714 					(info->interface == FLASH_CFI_X8X16)) ||
1715 				/* x16/x32 in x16 mode */
1716 				((info->chipwidth == FLASH_CFI_BY16) &&
1717 					(info->interface == FLASH_CFI_X16X32)))
1718 			{
1719 				info->addr_unlock1 = 0xaaa;
1720 				info->addr_unlock2 = 0x555;
1721 			}
1722 
1723 			info->name = "CFI conformant";
1724 			return 1;
1725 		}
1726 	}
1727 
1728 	return 0;
1729 }
1730 
1731 static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
1732 {
1733 	debug ("flash detect cfi\n");
1734 
1735 	for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
1736 	     info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
1737 		for (info->chipwidth = FLASH_CFI_BY8;
1738 		     info->chipwidth <= info->portwidth;
1739 		     info->chipwidth <<= 1)
1740 			if (__flash_detect_cfi(info, qry))
1741 				return 1;
1742 	}
1743 	debug ("not found\n");
1744 	return 0;
1745 }
1746 
1747 /*
1748  * Manufacturer-specific quirks. Add workarounds for geometry
1749  * reversal, etc. here.
1750  */
1751 static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
1752 {
1753 	/* check if flash geometry needs reversal */
1754 	if (qry->num_erase_regions > 1) {
1755 		/* reverse geometry if top boot part */
1756 		if (info->cfi_version < 0x3131) {
1757 			/* CFI < 1.1, try to guess from device id */
1758 			if ((info->device_id & 0x80) != 0)
1759 				cfi_reverse_geometry(qry);
1760 		} else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
1761 			/* CFI >= 1.1, deduct from top/bottom flag */
1762 			/* note: ext_addr is valid since cfi_version > 0 */
1763 			cfi_reverse_geometry(qry);
1764 		}
1765 	}
1766 }
1767 
1768 static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
1769 {
1770 	int reverse_geometry = 0;
1771 
1772 	/* Check the "top boot" bit in the PRI */
1773 	if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
1774 		reverse_geometry = 1;
1775 
1776 	/* AT49BV6416(T) list the erase regions in the wrong order.
1777 	 * However, the device ID is identical with the non-broken
1778 	 * AT49BV642D they differ in the high byte.
1779 	 */
1780 	if (info->device_id == 0xd6 || info->device_id == 0xd2)
1781 		reverse_geometry = !reverse_geometry;
1782 
1783 	if (reverse_geometry)
1784 		cfi_reverse_geometry(qry);
1785 }
1786 
1787 static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
1788 {
1789 	/* check if flash geometry needs reversal */
1790 	if (qry->num_erase_regions > 1) {
1791 		/* reverse geometry if top boot part */
1792 		if (info->cfi_version < 0x3131) {
1793 			/* CFI < 1.1, guess by device id (M29W320{DT,ET} only) */
1794 			if (info->device_id == 0x22CA ||
1795 			    info->device_id == 0x2256) {
1796 				cfi_reverse_geometry(qry);
1797 			}
1798 		}
1799 	}
1800 }
1801 
1802 /*
1803  * The following code cannot be run from FLASH!
1804  *
1805  */
1806 ulong flash_get_size (phys_addr_t base, int banknum)
1807 {
1808 	flash_info_t *info = &flash_info[banknum];
1809 	int i, j;
1810 	flash_sect_t sect_cnt;
1811 	phys_addr_t sector;
1812 	unsigned long tmp;
1813 	int size_ratio;
1814 	uchar num_erase_regions;
1815 	int erase_region_size;
1816 	int erase_region_count;
1817 	struct cfi_qry qry;
1818 
1819 	memset(&qry, 0, sizeof(qry));
1820 
1821 	info->ext_addr = 0;
1822 	info->cfi_version = 0;
1823 #ifdef CONFIG_SYS_FLASH_PROTECTION
1824 	info->legacy_unlock = 0;
1825 #endif
1826 
1827 	info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
1828 
1829 	if (flash_detect_cfi (info, &qry)) {
1830 		info->vendor = le16_to_cpu(qry.p_id);
1831 		info->ext_addr = le16_to_cpu(qry.p_adr);
1832 		num_erase_regions = qry.num_erase_regions;
1833 
1834 		if (info->ext_addr) {
1835 			info->cfi_version = (ushort) flash_read_uchar (info,
1836 						info->ext_addr + 3) << 8;
1837 			info->cfi_version |= (ushort) flash_read_uchar (info,
1838 						info->ext_addr + 4);
1839 		}
1840 
1841 #ifdef DEBUG
1842 		flash_printqry (&qry);
1843 #endif
1844 
1845 		switch (info->vendor) {
1846 		case CFI_CMDSET_INTEL_PROG_REGIONS:
1847 		case CFI_CMDSET_INTEL_STANDARD:
1848 		case CFI_CMDSET_INTEL_EXTENDED:
1849 			cmdset_intel_init(info, &qry);
1850 			break;
1851 		case CFI_CMDSET_AMD_STANDARD:
1852 		case CFI_CMDSET_AMD_EXTENDED:
1853 			cmdset_amd_init(info, &qry);
1854 			break;
1855 		default:
1856 			printf("CFI: Unknown command set 0x%x\n",
1857 					info->vendor);
1858 			/*
1859 			 * Unfortunately, this means we don't know how
1860 			 * to get the chip back to Read mode. Might
1861 			 * as well try an Intel-style reset...
1862 			 */
1863 			flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
1864 			return 0;
1865 		}
1866 
1867 		/* Do manufacturer-specific fixups */
1868 		switch (info->manufacturer_id) {
1869 		case 0x0001:
1870 			flash_fixup_amd(info, &qry);
1871 			break;
1872 		case 0x001f:
1873 			flash_fixup_atmel(info, &qry);
1874 			break;
1875 		case 0x0020:
1876 			flash_fixup_stm(info, &qry);
1877 			break;
1878 		}
1879 
1880 		debug ("manufacturer is %d\n", info->vendor);
1881 		debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
1882 		debug ("device id is 0x%x\n", info->device_id);
1883 		debug ("device id2 is 0x%x\n", info->device_id2);
1884 		debug ("cfi version is 0x%04x\n", info->cfi_version);
1885 
1886 		size_ratio = info->portwidth / info->chipwidth;
1887 		/* if the chip is x8/x16 reduce the ratio by half */
1888 		if ((info->interface == FLASH_CFI_X8X16)
1889 		    && (info->chipwidth == FLASH_CFI_BY8)) {
1890 			size_ratio >>= 1;
1891 		}
1892 		debug ("size_ratio %d port %d bits chip %d bits\n",
1893 		       size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1894 		       info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1895 		debug ("found %d erase regions\n", num_erase_regions);
1896 		sect_cnt = 0;
1897 		sector = base;
1898 		for (i = 0; i < num_erase_regions; i++) {
1899 			if (i > NUM_ERASE_REGIONS) {
1900 				printf ("%d erase regions found, only %d used\n",
1901 					num_erase_regions, NUM_ERASE_REGIONS);
1902 				break;
1903 			}
1904 
1905 			tmp = le32_to_cpu(qry.erase_region_info[i]);
1906 			debug("erase region %u: 0x%08lx\n", i, tmp);
1907 
1908 			erase_region_count = (tmp & 0xffff) + 1;
1909 			tmp >>= 16;
1910 			erase_region_size =
1911 				(tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
1912 			debug ("erase_region_count = %d erase_region_size = %d\n",
1913 				erase_region_count, erase_region_size);
1914 			for (j = 0; j < erase_region_count; j++) {
1915 				if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
1916 					printf("ERROR: too many flash sectors\n");
1917 					break;
1918 				}
1919 				info->start[sect_cnt] =
1920 					(ulong)map_physmem(sector,
1921 							   info->portwidth,
1922 							   MAP_NOCACHE);
1923 				sector += (erase_region_size * size_ratio);
1924 
1925 				/*
1926 				 * Only read protection status from
1927 				 * supported devices (intel...)
1928 				 */
1929 				switch (info->vendor) {
1930 				case CFI_CMDSET_INTEL_PROG_REGIONS:
1931 				case CFI_CMDSET_INTEL_EXTENDED:
1932 				case CFI_CMDSET_INTEL_STANDARD:
1933 					info->protect[sect_cnt] =
1934 						flash_isset (info, sect_cnt,
1935 							     FLASH_OFFSET_PROTECT,
1936 							     FLASH_STATUS_PROTECT);
1937 					break;
1938 				default:
1939 					/* default: not protected */
1940 					info->protect[sect_cnt] = 0;
1941 				}
1942 
1943 				sect_cnt++;
1944 			}
1945 		}
1946 
1947 		info->sector_count = sect_cnt;
1948 		info->size = 1 << qry.dev_size;
1949 		/* multiply the size by the number of chips */
1950 		info->size *= size_ratio;
1951 		info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
1952 		tmp = 1 << qry.block_erase_timeout_typ;
1953 		info->erase_blk_tout = tmp *
1954 			(1 << qry.block_erase_timeout_max);
1955 		tmp = (1 << qry.buf_write_timeout_typ) *
1956 			(1 << qry.buf_write_timeout_max);
1957 
1958 		/* round up when converting to ms */
1959 		info->buffer_write_tout = (tmp + 999) / 1000;
1960 		tmp = (1 << qry.word_write_timeout_typ) *
1961 			(1 << qry.word_write_timeout_max);
1962 		/* round up when converting to ms */
1963 		info->write_tout = (tmp + 999) / 1000;
1964 		info->flash_id = FLASH_MAN_CFI;
1965 		if ((info->interface == FLASH_CFI_X8X16) &&
1966 		    (info->chipwidth == FLASH_CFI_BY8)) {
1967 			/* XXX - Need to test on x8/x16 in parallel. */
1968 			info->portwidth >>= 1;
1969 		}
1970 
1971 		flash_write_cmd (info, 0, 0, info->cmd_reset);
1972 	}
1973 
1974 	return (info->size);
1975 }
1976 
1977 void flash_set_verbose(uint v)
1978 {
1979 	flash_verbose = v;
1980 }
1981 
1982 /*-----------------------------------------------------------------------
1983  */
1984 unsigned long flash_init (void)
1985 {
1986 	unsigned long size = 0;
1987 	int i;
1988 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
1989 	struct apl_s {
1990 		ulong start;
1991 		ulong size;
1992 	} apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
1993 #endif
1994 
1995 #ifdef CONFIG_SYS_FLASH_PROTECTION
1996 	/* read environment from EEPROM */
1997 	char s[64];
1998 	getenv_f("unlock", s, sizeof(s));
1999 #endif
2000 
2001 #define BANK_BASE(i)	(((phys_addr_t [CFI_MAX_FLASH_BANKS])CONFIG_SYS_FLASH_BANKS_LIST)[i])
2002 
2003 	/* Init: no FLASHes known */
2004 	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
2005 		flash_info[i].flash_id = FLASH_UNKNOWN;
2006 
2007 		if (!flash_detect_legacy (BANK_BASE(i), i))
2008 			flash_get_size (BANK_BASE(i), i);
2009 		size += flash_info[i].size;
2010 		if (flash_info[i].flash_id == FLASH_UNKNOWN) {
2011 #ifndef CONFIG_SYS_FLASH_QUIET_TEST
2012 			printf ("## Unknown FLASH on Bank %d "
2013 				"- Size = 0x%08lx = %ld MB\n",
2014 				i+1, flash_info[i].size,
2015 				flash_info[i].size << 20);
2016 #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
2017 		}
2018 #ifdef CONFIG_SYS_FLASH_PROTECTION
2019 		else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
2020 			/*
2021 			 * Only the U-Boot image and it's environment
2022 			 * is protected, all other sectors are
2023 			 * unprotected (unlocked) if flash hardware
2024 			 * protection is used (CONFIG_SYS_FLASH_PROTECTION)
2025 			 * and the environment variable "unlock" is
2026 			 * set to "yes".
2027 			 */
2028 			if (flash_info[i].legacy_unlock) {
2029 				int k;
2030 
2031 				/*
2032 				 * Disable legacy_unlock temporarily,
2033 				 * since flash_real_protect would
2034 				 * relock all other sectors again
2035 				 * otherwise.
2036 				 */
2037 				flash_info[i].legacy_unlock = 0;
2038 
2039 				/*
2040 				 * Legacy unlocking (e.g. Intel J3) ->
2041 				 * unlock only one sector. This will
2042 				 * unlock all sectors.
2043 				 */
2044 				flash_real_protect (&flash_info[i], 0, 0);
2045 
2046 				flash_info[i].legacy_unlock = 1;
2047 
2048 				/*
2049 				 * Manually mark other sectors as
2050 				 * unlocked (unprotected)
2051 				 */
2052 				for (k = 1; k < flash_info[i].sector_count; k++)
2053 					flash_info[i].protect[k] = 0;
2054 			} else {
2055 				/*
2056 				 * No legancy unlocking -> unlock all sectors
2057 				 */
2058 				flash_protect (FLAG_PROTECT_CLEAR,
2059 					       flash_info[i].start[0],
2060 					       flash_info[i].start[0]
2061 					       + flash_info[i].size - 1,
2062 					       &flash_info[i]);
2063 			}
2064 		}
2065 #endif /* CONFIG_SYS_FLASH_PROTECTION */
2066 	}
2067 
2068 	/* Monitor protection ON by default */
2069 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
2070 	(!defined(CONFIG_MONITOR_IS_IN_RAM))
2071 	flash_protect (FLAG_PROTECT_SET,
2072 		       CONFIG_SYS_MONITOR_BASE,
2073 		       CONFIG_SYS_MONITOR_BASE + monitor_flash_len  - 1,
2074 		       flash_get_info(CONFIG_SYS_MONITOR_BASE));
2075 #endif
2076 
2077 	/* Environment protection ON by default */
2078 #ifdef CONFIG_ENV_IS_IN_FLASH
2079 	flash_protect (FLAG_PROTECT_SET,
2080 		       CONFIG_ENV_ADDR,
2081 		       CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
2082 		       flash_get_info(CONFIG_ENV_ADDR));
2083 #endif
2084 
2085 	/* Redundant environment protection ON by default */
2086 #ifdef CONFIG_ENV_ADDR_REDUND
2087 	flash_protect (FLAG_PROTECT_SET,
2088 		       CONFIG_ENV_ADDR_REDUND,
2089 		       CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
2090 		       flash_get_info(CONFIG_ENV_ADDR_REDUND));
2091 #endif
2092 
2093 #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
2094 	for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
2095 		debug("autoprotecting from %08x to %08x\n",
2096 		      apl[i].start, apl[i].start + apl[i].size - 1);
2097 		flash_protect (FLAG_PROTECT_SET,
2098 			       apl[i].start,
2099 			       apl[i].start + apl[i].size - 1,
2100 			       flash_get_info(apl[i].start));
2101 	}
2102 #endif
2103 
2104 #ifdef CONFIG_FLASH_CFI_MTD
2105 	cfi_mtd_init();
2106 #endif
2107 
2108 	return (size);
2109 }
2110