3 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 #include <environment.h>
29 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
31 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
34 #define CMD_READ_ARRAY 0x00F000F0
35 #define CMD_UNLOCK1 0x00AA00AA
36 #define CMD_UNLOCK2 0x00550055
37 #define CMD_ERASE_SETUP 0x00800080
38 #define CMD_ERASE_CONFIRM 0x00300030
39 #define CMD_PROGRAM 0x00A000A0
40 #define CMD_UNLOCK_BYPASS 0x00200020
41 #define CMD_READ_MANF_ID 0x00900090
42 #define CMD_UNLOCK_BYPASS_RES1 0x00900090
43 #define CMD_UNLOCK_BYPASS_RES2 0x00000000
45 #define MEM_FLASH_ADDR (*(volatile u32 *)CFG_FLASH_BASE)
46 #define MEM_FLASH_ADDR1 (*(volatile u32 *)(CFG_FLASH_BASE + (0x00000555 << 2)))
47 #define MEM_FLASH_ADDR2 (*(volatile u32 *)(CFG_FLASH_BASE + (0x000002AA << 2)))
49 #define BIT_ERASE_DONE 0x00800080
50 #define BIT_RDY_MASK 0x00800080
51 #define BIT_PROGRAM_ERROR 0x00200020
52 #define BIT_TIMEOUT 0x80000000 /* our flag */
58 /*-----------------------------------------------------------------------
61 ulong flash_init (void)
66 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
68 flash_info_t *info = &flash_info[i];
70 /* Init: no FLASHes known */
71 info->flash_id = FLASH_UNKNOWN;
73 size += flash_get_size (CFG_FLASH_BASE, info);
76 flashbase = CFG_FLASH_BASE;
78 panic ("configured too many flash banks!\n");
79 for (j = 0; j < info->sector_count; j++) {
82 info->start[j] = flashbase;
84 switch (info->flash_id & FLASH_TYPEMASK) {
85 case (FLASH_AM320B & FLASH_TYPEMASK):
86 case (FLASH_MXLV320B & FLASH_TYPEMASK):
87 /* Boot sector type: 8 x 8 + N x 128 kB */
88 flashbase += (j < 8) ? 0x4000 : 0x20000;
90 case (FLASH_AM640U & FLASH_TYPEMASK):
91 /* Uniform sector type: 128 kB */
95 printf ("## Bad flash chip type 0x%04lX\n",
96 info->flash_id & FLASH_TYPEMASK);
102 * Protect monitor and environment sectors
104 flash_protect ( FLAG_PROTECT_SET,
106 CFG_FLASH_BASE + monitor_flash_len - 1,
109 flash_protect ( FLAG_PROTECT_SET,
111 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
113 #ifdef CFG_ENV_ADDR_REDUND
114 flash_protect ( FLAG_PROTECT_SET,
116 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
123 /*-----------------------------------------------------------------------
125 void flash_print_info (flash_info_t * info)
129 switch (info->flash_id & FLASH_VENDMASK) {
130 case (FLASH_MAN_AMD & FLASH_VENDMASK):
131 printf ("AMD "); break;
132 case (FLASH_MAN_FUJ & FLASH_VENDMASK):
133 printf ("FUJITSU "); break;
134 case (FLASH_MAN_MX & FLASH_VENDMASK):
135 printf ("MACRONIX "); break;
136 default: printf ("Unknown Vendor "); break;
139 switch (info->flash_id & FLASH_TYPEMASK) {
140 case (FLASH_AM320B & FLASH_TYPEMASK):
141 printf ("2x Am29LV320DB (32Mbit)\n");
143 case (FLASH_MXLV320B & FLASH_TYPEMASK):
144 printf ("2x MX29LV320DB (32Mbit)\n");
146 case (FLASH_AM640U & FLASH_TYPEMASK):
147 printf ("2x Am29LV640D (64Mbit)\n");
150 printf ("Unknown Chip Type\n");
155 printf (" Size: %ld MB in %d Sectors\n",
156 info->size >> 20, info->sector_count);
158 printf (" Sector Start Addresses:");
159 for (i = 0; i < info->sector_count; i++) {
165 info->protect[i] ? " (RO)" : " ");
172 /*-----------------------------------------------------------------------
175 int flash_erase (flash_info_t * info, int s_first, int s_last)
182 int iflag, prot, sect;
186 debug ("flash_erase: s_first %d s_last %d\n", s_first, s_last);
188 /* first look for protection bits */
190 if (info->flash_id == FLASH_UNKNOWN)
191 return ERR_UNKNOWN_FLASH_TYPE;
193 if ((s_first < 0) || (s_first > s_last)) {
197 switch (info->flash_id & FLASH_VENDMASK) {
198 case (FLASH_MAN_AMD & FLASH_VENDMASK): break; /* OK */
199 case (FLASH_MAN_FUJ & FLASH_VENDMASK): break; /* OK */
200 case (FLASH_MAN_MX & FLASH_VENDMASK): break; /* OK */
202 debug ("## flash_erase: unknown manufacturer\n");
203 return (ERR_UNKNOWN_FLASH_VENDOR);
207 for (sect = s_first; sect <= s_last; ++sect) {
208 if (info->protect[sect]) {
214 printf ("- Warning: %d protected sectors will not be erased!\n",
221 * Disable interrupts which might cause a timeout
222 * here. Remember that our exception vectors are
223 * at address 0 in the flash, and we don't want a
224 * (ticker) exception to happen while the flash
225 * chip is in programming mode.
228 cflag = icache_status ();
231 iflag = disable_interrupts ();
233 /* Start erase on unprotected sectors */
234 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
236 debug ("Erasing sector %2d @ %08lX... ",
237 sect, info->start[sect]);
239 /* arm simple, non interrupt dependent timer */
240 reset_timer_masked ();
242 if (info->protect[sect] == 0) { /* not protected */
243 vu_long *addr = (vu_long *) (info->start[sect]);
245 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
246 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
247 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
249 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
250 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
251 *addr = CMD_ERASE_CONFIRM;
253 /* wait until flash is ready */
260 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
261 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
266 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
269 if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
272 if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
275 if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
278 } while (!chip1 || !chip2);
280 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
282 if (chip1 == ERR || chip2 == ERR) {
294 /* allow flash to settle - wait 10 ms */
295 udelay_masked (10000);
298 enable_interrupts ();
307 /*-----------------------------------------------------------------------
308 * Copy memory to flash
311 static int write_word (flash_info_t * info, ulong dest, ulong data)
313 vu_long *addr = (vu_long *) dest;
324 * Check if Flash is (sufficiently) erased
327 if ((result & data) != data)
328 return ERR_NOT_ERASED;
331 * Disable interrupts which might cause a timeout
332 * here. Remember that our exception vectors are
333 * at address 0 in the flash, and we don't want a
334 * (ticker) exception to happen while the flash
335 * chip is in programming mode.
338 cflag = icache_status ();
341 iflag = disable_interrupts ();
346 /* arm simple, non interrupt dependent timer */
347 reset_timer_masked ();
349 /* wait until flash is ready */
355 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
359 if (!chip1 && ((result & 0x80) == (data & 0x80)))
362 if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
365 if ((result & 0x80) == (data & 0x80))
371 if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
374 if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR)) {
377 if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
383 } while (!chip1 || !chip2);
385 *addr = CMD_READ_ARRAY;
387 if (chip1 == ERR || chip2 == ERR || *addr != data)
391 enable_interrupts ();
401 /*-----------------------------------------------------------------------
402 * Copy memory to flash.
405 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
411 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
412 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
413 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
415 wp = (addr & ~3); /* get lower word aligned address */
418 * handle unaligned start bytes
420 if ((l = addr - wp) != 0) {
422 for (i = 0, cp = wp; i < l; ++i, ++cp) {
423 data = (data >> 8) | (*(uchar *) cp << 24);
425 for (; i < 4 && cnt > 0; ++i) {
426 data = (data >> 8) | (*src++ << 24);
430 for (; cnt == 0 && i < 4; ++i, ++cp) {
431 data = (data >> 8) | (*(uchar *) cp << 24);
434 if ((rc = write_word (info, wp, data)) != 0) {
441 * handle word aligned part
444 if (((ulong)src) & 0x3) {
445 for (i = 0; i < 4; i++) {
446 ((char *)&data)[i] = ((vu_char *)src)[i];
450 data = *((vu_long *) src);
453 if ((rc = write_word (info, wp, data)) != 0) {
467 * handle unaligned tail bytes
470 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
471 data = (data >> 8) | (*src++ << 24);
474 for (; i < 4; ++i, ++cp) {
475 data = (data >> 8) | (*(uchar *) cp << 24);
478 rc = write_word (info, wp, data);
482 MEM_FLASH_ADDR = CMD_UNLOCK_BYPASS_RES1;
483 MEM_FLASH_ADDR = CMD_UNLOCK_BYPASS_RES2;
488 /*-----------------------------------------------------------------------
491 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
495 /* Write auto select command sequence and read Manufacturer ID */
496 addr[0x0555] = CMD_UNLOCK1;
497 addr[0x02AA] = CMD_UNLOCK2;
498 addr[0x0555] = CMD_READ_MANF_ID;
502 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
506 info->flash_id = FLASH_MAN_AMD;
509 info->flash_id = FLASH_MAN_FUJ;
512 info->flash_id = FLASH_MAN_MX;
515 info->flash_id = FLASH_UNKNOWN;
516 info->sector_count = 0;
518 addr[0] = 0x00FF00FF; /* restore read mode */
519 debug ("## flash_init: unknown manufacturer\n");
520 return (0); /* no or unknown flash */
523 value = addr[1]; /* device ID */
525 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
529 info->flash_id += FLASH_AM320B;
530 info->sector_count = 71;
531 info->size = 0x00800000;
533 addr[0] = 0x00FF00FF; /* restore read mode */
537 info->flash_id += FLASH_AM640U;
538 info->sector_count = 128;
539 info->size = 0x01000000;
541 addr[0] = 0x00F000F0; /* restore read mode */
542 break; /* => 16 MB */
545 info->flash_id += FLASH_MXLV320B;
546 info->sector_count = 71;
547 info->size = 0x00800000;
549 addr[0] = 0x00FF00FF; /* restore read mode */
553 debug ("## flash_init: unknown flash chip\n");
554 info->flash_id = FLASH_UNKNOWN;
555 addr[0] = 0x00FF00FF; /* restore read mode */
556 return (0); /* => no or unknown flash */
560 if (info->sector_count > CFG_MAX_FLASH_SECT) {
561 printf ("** ERROR: sector count %d > max (%d) **\n",
562 info->sector_count, CFG_MAX_FLASH_SECT);
563 info->sector_count = CFG_MAX_FLASH_SECT;