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,
28 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
30 /*-----------------------------------------------------------------------
33 ulong flash_recognize (vu_long *base);
34 int write_word (flash_info_t *info, ulong dest, ulong data);
35 void flash_get_geometry (vu_long *base, flash_info_t *info);
36 void flash_unprotect(flash_info_t *info);
37 int _flash_real_protect(flash_info_t *info, long idx, int on);
40 unsigned long flash_init (void)
42 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
43 volatile memctl8xx_t *memctl = &immap->im_memctl;
47 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
51 *((vu_short*)CONFIG_SYS_FLASH_BASE) = 0xffff;
53 flash_get_geometry ((vu_long*)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
55 /* Remap FLASH according to real size */
56 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-flash_info[0].size & 0xFFFF8000);
57 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
58 (memctl->memc_br0 & ~(BR_BA_MSK));
60 rec = flash_recognize((vu_long*)CONFIG_SYS_FLASH_BASE);
62 if (rec == FLASH_UNKNOWN) {
63 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
64 flash_info[0].size, flash_info[0].size<<20);
67 #if CONFIG_SYS_FLASH_PROTECTION
68 /*Unprotect all the flash memory*/
69 flash_unprotect(&flash_info[0]);
72 *((vu_short*)CONFIG_SYS_FLASH_BASE) = 0xffff;
74 return (flash_info[0].size);
76 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
77 /* monitor protection ON by default */
78 flash_protect(FLAG_PROTECT_SET,
79 CONFIG_SYS_MONITOR_BASE,
80 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
84 #ifdef CONFIG_ENV_IS_IN_FLASH
85 /* ENV protection ON by default */
86 flash_protect(FLAG_PROTECT_SET,
88 CONFIG_ENV_OFFSET+CONFIG_ENV_SIZE-1,
91 return (flash_info[0].size);
95 int flash_get_protect_status(flash_info_t * info, long idx)
101 printf("\n Attempting to set protection info with %d sectors\n", info->sector_count);
105 base = (vu_short*)info->start[idx];
109 *(base + 0x55) = 0x0098;
122 void flash_get_geometry (vu_long *base, flash_info_t *info)
126 vu_short * sb = (vu_short*)base;
127 ulong offset = (ulong)base;
129 /* Read Device geometry */
135 info->flash_id = ((ulong)base[0x0]);
137 printf("Id is %x\n", (uint)(ulong)info->flash_id);
144 info->size = 1 << (sb[0x27]); /* Read flash size */
147 printf("Size is %x\n", (uint)(ulong)info->size);
152 *(sb + 0x55) = 0x0098;
153 ner = sb[0x2c] ; /*Number of erase regions*/
156 printf("Number of erase regions %x\n", (uint)ner);
159 info->sector_count = 0;
161 for(i = 0; i < ner; i++)
169 *(sb + 0x55) = 0x0098;
176 count = ((t1 & 0x00ff) | (((t2 & 0x00ff) << 8) & 0xff00) )+ 1; /*sector count*/
177 s = ((t3 & 0x00ff) | (((t4 & 0x00ff) << 8) & 0xff00)) * 256;; /*Sector size*/
180 printf("count and size %x, %x\n", count, s);
181 printf("sector count for erase region %d is %d\n", i, count);
183 for(j = 0; j < count; j++)
186 printf("%x, ", (uint)offset);
188 info->start[ info->sector_count + j] = offset;
191 info->sector_count += count;
194 if ((offset - (ulong)base) != info->size)
195 printf("WARNING reported size %x does not match to calculted size %x.\n"
196 , (uint)info->size, (uint)(offset - (ulong)base) );
198 /* Next check if there are any sectors protected.*/
200 for(i = 0; i < info->sector_count; i++)
201 info->protect[i] = flash_get_protect_status(info, i);
206 /*-----------------------------------------------------------------------
208 void flash_print_info (flash_info_t *info)
212 if (info->flash_id == FLASH_UNKNOWN) {
213 printf ("missing or unknown FLASH type\n");
217 switch (info->flash_id & FLASH_VENDMASK) {
218 case INTEL_MANUFACT & FLASH_VENDMASK:
222 printf ("Unknown Vendor ");
226 switch (info->flash_id & FLASH_TYPEMASK) {
227 case INTEL_ID_28F320C3B & FLASH_TYPEMASK:
228 printf ("28F320RC3(4 MB)\n");
230 case INTEL_ID_28F320J3A:
231 printf("28F320J3A (4 MB)\n");
234 printf ("Unknown Chip Type\n");
238 printf (" Size: %ld MB in %d Sectors\n",
239 info->size >> 20, info->sector_count);
241 printf (" Sector Start Addresses:");
242 for (i=0; i<info->sector_count; ++i) {
245 printf (" %02d %08lX%s",
247 info->protect[i]!=0 ? " (RO)" : " "
254 ulong flash_recognize (vu_long *base)
257 ulong res = FLASH_UNKNOWN;
258 vu_short * sb = (vu_short*)base;
265 switch (id & 0x00FF0000)
267 case (MT_MANUFACT & 0x00FF0000): /* MT or => Intel */
268 case (INTEL_ALT_MANU & 0x00FF0000):
269 res = FLASH_MAN_INTEL;
280 /*-----------------------------------------------------------------------*/
281 #define INTEL_FLASH_STATUS_BLS 0x02
282 #define INTEL_FLASH_STATUS_PSS 0x04
283 #define INTEL_FLASH_STATUS_VPPS 0x08
284 #define INTEL_FLASH_STATUS_PS 0x10
285 #define INTEL_FLASH_STATUS_ES 0x20
286 #define INTEL_FLASH_STATUS_ESS 0x40
287 #define INTEL_FLASH_STATUS_WSMS 0x80
289 int flash_decode_status_bits(char status)
293 if(!(status & INTEL_FLASH_STATUS_WSMS)) {
298 if(status & INTEL_FLASH_STATUS_ESS) {
299 printf("Erase suspended\n");
303 if(status & INTEL_FLASH_STATUS_ES) {
304 printf("Error in block erase\n");
308 if(status & INTEL_FLASH_STATUS_PS) {
309 printf("Error in programming\n");
313 if(status & INTEL_FLASH_STATUS_VPPS) {
314 printf("Vpp low, operation aborted\n");
318 if(status & INTEL_FLASH_STATUS_PSS) {
319 printf("Program is suspended\n");
323 if(status & INTEL_FLASH_STATUS_BLS) {
324 printf("Attempting to program/erase a locked sector\n");
328 if((status & INTEL_FLASH_STATUS_PS) &&
329 (status & INTEL_FLASH_STATUS_ES) &&
330 (status & INTEL_FLASH_STATUS_ESS)) {
331 printf("A command sequence error\n");
338 /*-----------------------------------------------------------------------
341 int flash_erase (flash_info_t *info, int s_first, int s_last)
344 int flag, prot, sect;
348 if ((s_first < 0) || (s_first > s_last)) {
349 if (info->flash_id == FLASH_UNKNOWN) {
350 printf ("- missing\n");
352 printf ("- no sectors to erase\n");
357 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK)) {
358 printf ("Can't erase unknown flash type %08lx - aborted\n",
364 for (sect=s_first; sect<=s_last; ++sect) {
365 if (info->protect[sect]) {
371 printf ("- Warning: %d protected sectors will not be erased!\n",
377 start = get_timer (0);
379 /* Start erase on unprotected sectors */
380 for (sect = s_first; sect<=s_last; sect++) {
383 if (info->protect[sect] == 0) { /* not protected */
384 addr = (vu_short *)(info->start[sect]);
386 /* Disable interrupts which might cause a timeout here */
387 flag = disable_interrupts();
389 /* Single Block Erase Command */
393 /* Resume Command, as per errata update */
396 /* re-enable interrupts if necessary */
400 *addr = 0x70; /*Read status register command*/
401 tmp = (short)*addr & 0x00FF; /* Read the status */
402 while (!(tmp & INTEL_FLASH_STATUS_WSMS)) {
403 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
404 *addr = 0x0050; /* Reset the status register */
406 printf ("Timeout\n");
409 /* show that we're waiting */
410 if ((now - start) > 1000) { /* every second */
413 udelay(100000); /* 100 ms */
414 *addr = 0x0070; /*Read status register command*/
415 tmp = (short)*addr & 0x00FF; /* Read status */
416 start = get_timer(0);
418 if( tmp & INTEL_FLASH_STATUS_ES )
419 flash_decode_status_bits(tmp);
421 *addr = 0x0050; /* Reset the status register */
422 *addr = 0xffff; /* Reset to read mode */
431 void flash_unprotect (flash_info_t *info)
433 /*We can only unprotect the whole flash at once*/
434 /*Therefore we must prevent the _flash_real_protect()*/
435 /*from re-protecting sectors, that ware protected before */
436 /*we called flash_real_protect();*/
440 for(i = 0; i < info->sector_count; i++)
441 info->protect[i] = 0;
443 #ifdef CONFIG_SYS_FLASH_PROTECTION
444 _flash_real_protect(info, 0, 0);
448 /*-----------------------------------------------------------------------
449 * Copy memory to flash, returns:
452 * 2 - Flash not erased
455 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
460 wp = (addr & ~3); /* get lower word aligned address */
463 * handle unaligned start bytes
465 if ((l = addr - wp) != 0) {
467 for (i=0, cp=wp; i<l; ++i, ++cp) {
468 data = (data << 8) | (*(uchar *)cp);
470 for (; i<4 && cnt>0; ++i) {
471 data = (data << 8) | *src++;
475 for (; cnt==0 && i<4; ++i, ++cp) {
476 data = (data << 8) | (*(uchar *)cp);
479 if ((rc = write_word(info, wp, data)) != 0) {
486 * handle word aligned part
490 for (i=0; i<4; ++i) {
491 data = (data << 8) | *src++;
493 if ((rc = write_word(info, wp, data)) != 0) {
505 * handle unaligned tail bytes
508 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
509 data = (data << 8) | *src++;
512 for (; i<4; ++i, ++cp) {
513 data = (data << 8) | (*(uchar *)cp);
516 return (write_word(info, wp, data));
519 /*-----------------------------------------------------------------------
520 * Write a word to Flash, returns:
523 * 2 - Flash not erased
525 int write_word (flash_info_t *info, ulong dest, ulong da)
527 vu_short *addr = (vu_short *)dest;
539 /* Check if Flash is (sufficiently) erased */
540 if (((*addr & data.data16[0]) != data.data16[0]) ||
541 ((*(addr+1) & data.data16[1]) != data.data16[1])) {
544 /* Disable interrupts which might cause a timeout here */
545 flag = disable_interrupts();
547 for(i = 0; i < 2; i++)
553 *addr = data.data16[i];
555 /* re-enable interrupts if necessary */
559 /* data polling for D7 */
560 start = get_timer (0);
562 *addr = 0x0070; /*Read statusregister command */
563 while (((csr = *addr) & INTEL_FLASH_STATUS_WSMS)!=INTEL_FLASH_STATUS_WSMS) {
564 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
568 *addr = 0x0070; /*Read statusregister command */
570 if (csr & INTEL_FLASH_STATUS_PSS) {
571 printf ("CSR indicates write error (%0x) at %08lx\n",
576 /* Clear Status Registers Command */
578 /* Reset to read array mode */
586 int flash_real_protect(flash_info_t *info, long offset, int prot)
590 for(idx = 0; idx < info->sector_count; idx++)
591 if(info->start[idx] == offset)
594 if(idx==info->sector_count)
598 /* Unprotect one sector, which means unprotect all flash
599 * and reprotect the other protected sectors.
601 _flash_real_protect(info, 0, 0); /* Unprotects the whole flash*/
602 info->protect[idx] = 0;
604 for(i = 0; i < info->sector_count; i++)
606 _flash_real_protect(info, i, 1);
609 /* We can protect individual sectors */
610 _flash_real_protect(info, idx, 1);
613 for( i = 0; i < info->sector_count; i++)
614 info->protect[i] = flash_get_protect_status(info, i);
619 int _flash_real_protect(flash_info_t *info, long idx, int prot)
627 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK)) {
628 printf ("Can't change protection for unknown flash type %08lx - aborted\n",
634 /*Unlock the sector*/
642 addr = (vu_short *)(info->start[idx]);
644 /* If chip is busy, wait for it */
645 start = get_timer(0);
646 *addr = 0x0070; /*Read status register command*/
647 tmp = ((ushort)(*addr))&0x00ff; /*Read the status*/
648 while(!(tmp & INTEL_FLASH_STATUS_WSMS)) {
649 /*Write State Machine Busy*/
650 /*Wait untill done or timeout.*/
651 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_WRITE_TOUT) {
652 *addr = 0x0050; /* Reset the status register */
653 *addr = 0xffff; /* Reset the chip */
654 printf ("TTimeout\n");
658 tmp = ((ushort)(*addr))&0x00ff; /*Read the status*/
659 start = get_timer(0);
662 /* Disable interrupts which might cause a timeout here */
663 flag = disable_interrupts();
670 /* re-enable interrupts if necessary */
674 start = get_timer(0);
675 *addr = 0x0070; /*Read status register command*/
676 tmp = ((ushort)(*addr)) & 0x00FF; /* Read the status */
677 while (!(tmp & INTEL_FLASH_STATUS_WSMS)) {
678 /* Write State Machine Busy */
679 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_WRITE_TOUT) {
680 *addr = 0x0050; /* Reset the status register */
682 printf ("Timeout\n");
685 /* show that we're waiting */
686 if ((now - start) > 1000) { /* every second */
689 udelay(100000); /* 100 ms */
690 *addr = 0x70; /*Read status register command*/
691 tmp = (short)*addr & 0x00FF; /* Read status */
692 start = get_timer(0);
694 if( tmp & INTEL_FLASH_STATUS_PS )
695 flash_decode_status_bits(tmp);
697 *addr =0x0050; /*Clear status register*/
699 /* reset to read mode */