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
43 #define MEM_FLASH_ADDR1 (*(volatile u32 *)(CFG_FLASH_BASE + (0x00000555 << 2)))
44 #define MEM_FLASH_ADDR2 (*(volatile u32 *)(CFG_FLASH_BASE + (0x000002AA << 2)))
46 #define BIT_ERASE_DONE 0x00800080
47 #define BIT_RDY_MASK 0x00800080
48 #define BIT_PROGRAM_ERROR 0x00200020
49 #define BIT_TIMEOUT 0x80000000 /* our flag */
55 /*-----------------------------------------------------------------------
58 ulong flash_init (void)
63 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
65 flash_info_t *info = &flash_info[i];
67 /* Init: no FLASHes known */
68 info->flash_id = FLASH_UNKNOWN;
70 size += flash_get_size (CFG_FLASH_BASE, info);
73 flashbase = CFG_FLASH_BASE;
75 panic ("configured too many flash banks!\n");
76 for (j = 0; j < info->sector_count; j++) {
79 info->start[j] = flashbase;
81 switch (info->flash_id & FLASH_TYPEMASK) {
82 case (FLASH_AM320B & FLASH_TYPEMASK):
83 /* Boot sector type: 8 x 8 + N x 128 kB */
84 flashbase += (j < 8) ? 0x4000 : 0x20000;
86 case (FLASH_AM640U & FLASH_TYPEMASK):
87 /* Uniform sector type: 128 kB */
91 printf ("## Bad flash chip type 0x%04lX\n",
92 info->flash_id & FLASH_TYPEMASK);
98 * Protect monitor and environment sectors
100 flash_protect ( FLAG_PROTECT_SET,
102 CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
105 flash_protect ( FLAG_PROTECT_SET,
107 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
109 #ifdef CFG_ENV_ADDR_REDUND
110 flash_protect ( FLAG_PROTECT_SET,
112 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
119 /*-----------------------------------------------------------------------
121 void flash_print_info (flash_info_t * info)
125 switch (info->flash_id & FLASH_VENDMASK) {
126 case (FLASH_MAN_AMD & FLASH_VENDMASK):
127 printf ("AMD "); break;
128 case (FLASH_MAN_FUJ & FLASH_VENDMASK):
129 printf ("FUJITSU "); break;
130 default: printf ("Unknown Vendor "); break;
133 switch (info->flash_id & FLASH_TYPEMASK) {
134 case (FLASH_AM320B & FLASH_TYPEMASK):
135 printf ("2x Am29LV320DB (32Mbit)\n");
137 case (FLASH_AM640U & FLASH_TYPEMASK):
138 printf ("2x Am29LV640D (64Mbit)\n");
141 printf ("Unknown Chip Type\n");
146 printf (" Size: %ld MB in %d Sectors\n",
147 info->size >> 20, info->sector_count);
149 printf (" Sector Start Addresses:");
150 for (i = 0; i < info->sector_count; i++) {
156 info->protect[i] ? " (RO)" : " ");
163 /*-----------------------------------------------------------------------
166 int flash_erase (flash_info_t * info, int s_first, int s_last)
173 int iflag, prot, sect;
177 debug ("flash_erase: s_first %d s_last %d\n", s_first, s_last);
179 /* first look for protection bits */
181 if (info->flash_id == FLASH_UNKNOWN)
182 return ERR_UNKNOWN_FLASH_TYPE;
184 if ((s_first < 0) || (s_first > s_last)) {
188 if ((info->flash_id & FLASH_VENDMASK) !=
189 (FLASH_MAN_AMD & FLASH_VENDMASK)) {
190 return ERR_UNKNOWN_FLASH_VENDOR;
194 for (sect = s_first; sect <= s_last; ++sect) {
195 if (info->protect[sect]) {
201 printf ("- Warning: %d protected sectors will not be erased!\n",
208 * Disable interrupts which might cause a timeout
209 * here. Remember that our exception vectors are
210 * at address 0 in the flash, and we don't want a
211 * (ticker) exception to happen while the flash
212 * chip is in programming mode.
215 cflag = icache_status ();
218 iflag = disable_interrupts ();
220 /* Start erase on unprotected sectors */
221 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
223 debug ("Erasing sector %2d @ %08lX... ",
224 sect, info->start[sect]);
226 /* arm simple, non interrupt dependent timer */
227 reset_timer_masked ();
229 if (info->protect[sect] == 0) { /* not protected */
230 vu_long *addr = (vu_long *) (info->start[sect]);
232 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
233 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
234 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
236 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
237 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
238 *addr = CMD_ERASE_CONFIRM;
240 /* wait until flash is ready */
247 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
248 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
253 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
256 if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
259 if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
262 if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
265 } while (!chip1 || !chip2);
267 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
269 if (chip1 == ERR || chip2 == ERR) {
281 /* allow flash to settle - wait 10 ms */
282 udelay_masked (10000);
285 enable_interrupts ();
294 /*-----------------------------------------------------------------------
295 * Copy memory to flash
298 volatile static int write_word (flash_info_t * info, ulong dest,
301 vu_long *addr = (vu_long *) dest;
312 * Check if Flash is (sufficiently) erased
315 if ((result & data) != data)
316 return ERR_NOT_ERASED;
319 * Disable interrupts which might cause a timeout
320 * here. Remember that our exception vectors are
321 * at address 0 in the flash, and we don't want a
322 * (ticker) exception to happen while the flash
323 * chip is in programming mode.
326 cflag = icache_status ();
329 iflag = disable_interrupts ();
331 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
332 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
333 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
337 /* arm simple, non interrupt dependent timer */
338 reset_timer_masked ();
340 /* wait until flash is ready */
346 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
350 if (!chip1 && ((result & 0x80) == (data & 0x80)))
353 if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
356 if ((result & 0x80) == (data & 0x80))
362 if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
365 if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR)) {
368 if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
374 } while (!chip1 || !chip2);
376 *addr = CMD_READ_ARRAY;
378 if (chip1 == ERR || chip2 == ERR || *addr != data)
382 enable_interrupts ();
392 /*-----------------------------------------------------------------------
393 * Copy memory to flash.
396 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
402 wp = (addr & ~3); /* get lower word aligned address */
405 * handle unaligned start bytes
407 if ((l = addr - wp) != 0) {
409 for (i = 0, cp = wp; i < l; ++i, ++cp) {
410 data = (data >> 8) | (*(uchar *) cp << 24);
412 for (; i < 4 && cnt > 0; ++i) {
413 data = (data >> 8) | (*src++ << 24);
417 for (; cnt == 0 && i < 4; ++i, ++cp) {
418 data = (data >> 8) | (*(uchar *) cp << 24);
421 if ((rc = write_word (info, wp, data)) != 0) {
428 * handle word aligned part
431 data = *((vu_long *) src);
432 if ((rc = write_word (info, wp, data)) != 0) {
445 * handle unaligned tail bytes
448 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
449 data = (data >> 8) | (*src++ << 24);
452 for (; i < 4; ++i, ++cp) {
453 data = (data >> 8) | (*(uchar *) cp << 24);
456 return write_word (info, wp, data);
459 /*-----------------------------------------------------------------------
462 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
466 /* Write auto select command sequence and read Manufacturer ID */
467 addr[0x0555] = CMD_UNLOCK1;
468 addr[0x02AA] = CMD_UNLOCK2;
469 addr[0x0555] = CMD_READ_MANF_ID;
473 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
477 info->flash_id = FLASH_MAN_AMD;
480 info->flash_id = FLASH_MAN_FUJ;
483 info->flash_id = FLASH_UNKNOWN;
484 info->sector_count = 0;
486 addr[0] = 0x00FF00FF; /* restore read mode */
487 debug ("## flash_init: unknown manufacturer\n");
488 return (0); /* no or unknown flash */
491 value = addr[1]; /* device ID */
493 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
497 info->flash_id += FLASH_AM320B;
498 info->sector_count = 71;
499 info->size = 0x00800000;
501 addr[0] = 0x00FF00FF; /* restore read mode */
505 info->flash_id += FLASH_AM640U;
506 info->sector_count = 128;
507 info->size = 0x01000000;
509 addr[0] = 0x00F000F0; /* restore read mode */
510 break; /* => 16 MB */
513 debug ("## flash_init: unknown flash chip\n");
514 info->flash_id = FLASH_UNKNOWN;
515 addr[0] = 0x00FF00FF; /* restore read mode */
516 return (0); /* => no or unknown flash */
520 if (info->sector_count > CFG_MAX_FLASH_SECT) {
521 printf ("** ERROR: sector count %d > max (%d) **\n",
522 info->sector_count, CFG_MAX_FLASH_SECT);
523 info->sector_count = CFG_MAX_FLASH_SECT;