3 * Wolfgang Denk, DENX Software Engineering, wd@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 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 #if defined(CFG_ENV_IS_IN_FLASH)
31 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
34 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
36 # ifndef CFG_ENV_SECT_SIZE
37 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
41 #define QUAD_ID(id) ((((ulong)(id) & 0xFF) << 24) | \
42 (((ulong)(id) & 0xFF) << 16) | \
43 (((ulong)(id) & 0xFF) << 8) | \
44 (((ulong)(id) & 0xFF) << 0) \
47 /*-----------------------------------------------------------------------
50 static ulong flash_get_size (vu_long * addr, flash_info_t * info);
51 static int write_word (flash_info_t * info, ulong dest, ulong data);
53 /*-----------------------------------------------------------------------
55 unsigned long flash_init (void)
57 volatile immap_t *immap = (immap_t *) CFG_IMMR;
58 volatile memctl8xx_t *memctl = &immap->im_memctl;
59 vu_long *bcsr = (vu_long *)BCSR_ADDR;
60 unsigned long pd_size, total_size, bsize, or_am;
63 /* Init: no FLASHes known */
64 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
65 flash_info[i].flash_id = FLASH_UNKNOWN;
66 flash_info[i].size = 0;
67 flash_info[i].sector_count = 0;
68 flash_info[i].start[0] = 0xFFFFFFFF; /* For TFTP */
71 switch ((bcsr[2] & BCSR2_FLASH_PD_MASK) >> BCSR2_FLASH_PD_SHIFT) {
93 printf("## Unsupported flash detected by BCSR: 0x%08X\n", bcsr[2]);
97 for (i = 0; i < CFG_MAX_FLASH_BANKS && total_size < pd_size; ++i) {
98 bsize = flash_get_size((vu_long *)(CFG_FLASH_BASE + total_size),
101 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
102 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
103 i, bsize, bsize >> 20);
109 if (total_size != pd_size) {
110 printf("## Detected flash size %lu conflicts with PD data %lu\n",
111 total_size, pd_size);
114 /* Remap FLASH according to real size */
115 memctl->memc_or0 = or_am | CFG_OR_TIMING_FLASH;
117 for (i = 0; i < CFG_MAX_FLASH_BANKS && flash_info[i].size != 0; ++i) {
118 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
119 /* monitor protection ON by default */
120 if (CFG_MONITOR_BASE >= flash_info[i].start[0])
121 flash_protect (FLAG_PROTECT_SET,
123 CFG_MONITOR_BASE + monitor_flash_len - 1,
127 #ifdef CFG_ENV_IS_IN_FLASH
128 /* ENV protection ON by default */
129 if (CFG_ENV_ADDR >= flash_info[i].start[0])
130 flash_protect (FLAG_PROTECT_SET,
132 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
140 /*-----------------------------------------------------------------------
142 void flash_print_info (flash_info_t * info)
146 if (info->flash_id == FLASH_UNKNOWN) {
147 printf ("missing or unknown FLASH type\n");
151 switch (info->flash_id & FLASH_VENDMASK) {
159 printf ("BRIGHT MICRO ");
162 printf ("Unknown Vendor ");
166 switch (info->flash_id & FLASH_TYPEMASK) {
168 printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
171 printf ("29F080 or 29LV080 (8 Mbit, uniform sectors)\n");
174 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
177 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
180 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
183 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
186 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
189 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
192 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
195 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
198 printf ("Unknown Chip Type\n");
202 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20,
205 printf (" Sector Start Addresses:");
207 for (i = 0; i < info->sector_count; ++i) {
213 info->start[i], info->protect[i] ? " (RO)" : " ");
219 /*-----------------------------------------------------------------------
220 * The following code can not run from flash!
222 static ulong flash_get_size (vu_long * addr, flash_info_t * info)
226 /* Write auto select command: read Manufacturer ID */
227 addr[0x0555] = 0xAAAAAAAA;
228 addr[0x02AA] = 0x55555555;
229 addr[0x0555] = 0x90909090;
232 case QUAD_ID(AMD_MANUFACT):
233 info->flash_id = FLASH_MAN_AMD;
236 case QUAD_ID(FUJ_MANUFACT):
237 info->flash_id = FLASH_MAN_FUJ;
241 info->flash_id = FLASH_UNKNOWN;
242 info->sector_count = 0;
247 switch (addr[1]) { /* device ID */
248 case QUAD_ID(AMD_ID_F040B):
249 case QUAD_ID(AMD_ID_LV040B):
250 info->flash_id += FLASH_AM040;
251 info->sector_count = 8;
252 info->size = 0x00200000;
255 case QUAD_ID(AMD_ID_F080B):
256 info->flash_id += FLASH_AM080;
257 info->sector_count = 16;
258 info->size = 0x00400000;
262 info->flash_id += FLASH_AM400T;
263 info->sector_count = 11;
264 info->size = 0x00100000;
268 info->flash_id += FLASH_AM400B;
269 info->sector_count = 11;
270 info->size = 0x00100000;
274 info->flash_id += FLASH_AM800T;
275 info->sector_count = 19;
276 info->size = 0x00200000;
280 info->flash_id += FLASH_AM800B;
281 info->sector_count = 19;
282 info->size = 0x00200000;
286 info->flash_id += FLASH_AM160T;
287 info->sector_count = 35;
288 info->size = 0x00400000;
292 info->flash_id += FLASH_AM160B;
293 info->sector_count = 35;
294 info->size = 0x00400000;
298 info->flash_id += FLASH_AM320T;
299 info->sector_count = 67;
300 info->size = 0x00800000;
304 info->flash_id += FLASH_AM320B;
305 info->sector_count = 67;
306 info->size = 0x00800000;
310 info->flash_id = FLASH_UNKNOWN;
311 return (0); /* => no or unknown flash */
315 /* set up sector start address table */
316 if (info->flash_id & FLASH_BTYPE) {
317 /* set sector offsets for bottom boot block type */
318 info->start[0] = base + 0x00000000;
319 info->start[1] = base + 0x00008000;
320 info->start[2] = base + 0x0000C000;
321 info->start[3] = base + 0x00010000;
322 for (i = 4; i < info->sector_count; i++) {
323 info->start[i] = base + (i * 0x00020000) - 0x00060000;
326 /* set sector offsets for top boot block type */
327 i = info->sector_count - 1;
328 info->start[i--] = base + info->size - 0x00008000;
329 info->start[i--] = base + info->size - 0x0000C000;
330 info->start[i--] = base + info->size - 0x00010000;
331 for (; i >= 0; i--) {
332 info->start[i] = base + i * 0x00020000;
336 /* set sector offsets for uniform sector type */
337 for (i = 0; i < info->sector_count; i++)
338 info->start[i] = (ulong)addr + (i * 0x00040000);
341 /* check for protected sectors */
342 for (i = 0; i < info->sector_count; i++) {
343 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
344 /* D0 = 1 if protected */
345 addr = (volatile unsigned long *) (info->start[i]);
346 info->protect[i] = addr[2] & 1;
349 if (info->flash_id != FLASH_UNKNOWN) {
350 addr = (volatile unsigned long *) info->start[0];
351 *addr = 0xF0F0F0F0; /* reset bank */
357 /*-----------------------------------------------------------------------
359 int flash_erase (flash_info_t * info, int s_first, int s_last)
361 vu_long *addr = (vu_long *) (info->start[0]);
362 int flag, prot, sect, l_sect;
363 ulong start, now, last;
365 if ((s_first < 0) || (s_first > s_last)) {
366 if (info->flash_id == FLASH_UNKNOWN) {
367 printf ("- missing\n");
369 printf ("- no sectors to erase\n");
374 if ((info->flash_id == FLASH_UNKNOWN) ||
375 (info->flash_id > FLASH_AMD_COMP)) {
376 printf ("Can't erase unknown flash type - aborted\n");
377 return ERR_UNKNOWN_FLASH_TYPE;
381 for (sect = s_first; sect <= s_last; ++sect) {
382 if (info->protect[sect]) {
388 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
395 /* Disable interrupts which might cause a timeout here */
396 flag = disable_interrupts ();
398 addr[0x0555] = 0xAAAAAAAA;
399 addr[0x02AA] = 0x55555555;
400 addr[0x0555] = 0x80808080;
401 addr[0x0555] = 0xAAAAAAAA;
402 addr[0x02AA] = 0x55555555;
404 /* Start erase on unprotected sectors */
405 for (sect = s_first; sect <= s_last; sect++) {
406 if (info->protect[sect] == 0) { /* not protected */
407 addr = (vu_long *) (info->start[sect]);
408 addr[0] = 0x30303030;
413 /* re-enable interrupts if necessary */
415 enable_interrupts ();
417 /* wait at least 80us - let's wait 1 ms */
421 * We wait for the last triggered sector
426 start = get_timer (0);
428 addr = (vu_long *) (info->start[l_sect]);
429 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
431 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
432 printf ("Timeout\n");
435 /* show that we're waiting */
436 if ((now - last) > 1000) { /* every second */
443 /* reset to read mode */
444 addr = (volatile unsigned long *) info->start[0];
445 addr[0] = 0xF0F0F0F0; /* reset bank */
452 /*-----------------------------------------------------------------------
453 * Copy memory to flash, returns:
456 * 2 - Flash not erased
458 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
463 wp = (addr & ~3); /* get lower word aligned address */
466 * handle unaligned start bytes
468 if ((l = addr - wp) != 0) {
470 for (i = 0, cp = wp; i < l; ++i, ++cp) {
471 data = (data << 8) | (*(uchar *) cp);
473 for (; i < 4 && cnt > 0; ++i) {
474 data = (data << 8) | *src++;
478 for (; cnt == 0 && i < 4; ++i, ++cp) {
479 data = (data << 8) | (*(uchar *) cp);
482 if ((rc = write_word (info, wp, data)) != 0) {
489 * handle word aligned part
493 for (i = 0; i < 4; ++i) {
494 data = (data << 8) | *src++;
496 if ((rc = write_word (info, wp, data)) != 0) {
508 * handle unaligned tail bytes
511 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
512 data = (data << 8) | *src++;
515 for (; i < 4; ++i, ++cp) {
516 data = (data << 8) | (*(uchar *) cp);
519 return (write_word (info, wp, data));
522 /*-----------------------------------------------------------------------
523 * Write a word to Flash, returns:
526 * 2 - Flash not erased
528 static int write_word (flash_info_t * info, ulong dest, ulong data)
530 vu_long *addr = (vu_long *) (info->start[0]);
534 /* Check if Flash is (sufficiently) erased */
535 if ((*((vu_long *) dest) & data) != data) {
536 return ERR_NOT_ERASED;
538 /* Disable interrupts which might cause a timeout here */
539 flag = disable_interrupts ();
541 addr[0x0555] = 0xAAAAAAAA;
542 addr[0x02AA] = 0x55555555;
543 addr[0x0555] = 0xA0A0A0A0;
545 *((vu_long *) dest) = data;
547 /* re-enable interrupts if necessary */
549 enable_interrupts ();
551 /* data polling for D7 */
552 start = get_timer (0);
553 while ((*((vu_long *) dest) & 0x80808080) != (data & 0x80808080))
555 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {