3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
12 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
14 /*-----------------------------------------------------------------------
17 ulong flash_recognize (vu_long *base);
18 int write_word (flash_info_t *info, ulong dest, ulong data);
19 void flash_get_geometry (vu_long *base, flash_info_t *info);
20 void flash_unprotect(flash_info_t *info);
21 int _flash_real_protect(flash_info_t *info, long idx, int on);
24 unsigned long flash_init (void)
26 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
27 volatile memctl8xx_t *memctl = &immap->im_memctl;
31 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
32 flash_info[i].flash_id = FLASH_UNKNOWN;
35 *((vu_short*)CONFIG_SYS_FLASH_BASE) = 0xffff;
37 flash_get_geometry ((vu_long*)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
39 /* Remap FLASH according to real size */
40 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-flash_info[0].size & 0xFFFF8000);
41 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
42 (memctl->memc_br0 & ~(BR_BA_MSK));
44 rec = flash_recognize((vu_long*)CONFIG_SYS_FLASH_BASE);
46 if (rec == FLASH_UNKNOWN) {
47 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
48 flash_info[0].size, flash_info[0].size<<20);
51 #if CONFIG_SYS_FLASH_PROTECTION
52 /*Unprotect all the flash memory*/
53 flash_unprotect(&flash_info[0]);
56 *((vu_short*)CONFIG_SYS_FLASH_BASE) = 0xffff;
58 return (flash_info[0].size);
60 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
61 /* monitor protection ON by default */
62 flash_protect(FLAG_PROTECT_SET,
63 CONFIG_SYS_MONITOR_BASE,
64 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
68 #ifdef CONFIG_ENV_IS_IN_FLASH
69 /* ENV protection ON by default */
70 flash_protect(FLAG_PROTECT_SET,
72 CONFIG_ENV_OFFSET+CONFIG_ENV_SIZE-1,
75 return (flash_info[0].size);
79 int flash_get_protect_status(flash_info_t * info, long idx)
85 printf("\n Attempting to set protection info with %d sectors\n", info->sector_count);
89 base = (vu_short*)info->start[idx];
93 *(base + 0x55) = 0x0098;
106 void flash_get_geometry (vu_long *base, flash_info_t *info)
110 vu_short * sb = (vu_short*)base;
111 ulong offset = (ulong)base;
113 /* Read Device geometry */
119 info->flash_id = ((ulong)base[0x0]);
121 printf("Id is %x\n", (uint)(ulong)info->flash_id);
128 info->size = 1 << (sb[0x27]); /* Read flash size */
131 printf("Size is %x\n", (uint)(ulong)info->size);
136 *(sb + 0x55) = 0x0098;
137 ner = sb[0x2c] ; /*Number of erase regions*/
140 printf("Number of erase regions %x\n", (uint)ner);
143 info->sector_count = 0;
145 for(i = 0; i < ner; i++)
153 *(sb + 0x55) = 0x0098;
160 count = ((t1 & 0x00ff) | (((t2 & 0x00ff) << 8) & 0xff00) )+ 1; /*sector count*/
161 s = ((t3 & 0x00ff) | (((t4 & 0x00ff) << 8) & 0xff00)) * 256;; /*Sector size*/
164 printf("count and size %x, %x\n", count, s);
165 printf("sector count for erase region %d is %d\n", i, count);
167 for(j = 0; j < count; j++)
170 printf("%x, ", (uint)offset);
172 info->start[ info->sector_count + j] = offset;
175 info->sector_count += count;
178 if ((offset - (ulong)base) != info->size)
179 printf("WARNING reported size %x does not match to calculted size %x.\n"
180 , (uint)info->size, (uint)(offset - (ulong)base) );
182 /* Next check if there are any sectors protected.*/
184 for(i = 0; i < info->sector_count; i++)
185 info->protect[i] = flash_get_protect_status(info, i);
190 /*-----------------------------------------------------------------------
192 void flash_print_info (flash_info_t *info)
196 if (info->flash_id == FLASH_UNKNOWN) {
197 printf ("missing or unknown FLASH type\n");
201 switch (info->flash_id & FLASH_VENDMASK) {
202 case INTEL_MANUFACT & FLASH_VENDMASK:
206 printf ("Unknown Vendor ");
210 switch (info->flash_id & FLASH_TYPEMASK) {
211 case INTEL_ID_28F320C3B & FLASH_TYPEMASK:
212 printf ("28F320RC3(4 MB)\n");
214 case INTEL_ID_28F320J3A:
215 printf("28F320J3A (4 MB)\n");
218 printf ("Unknown Chip Type\n");
222 printf (" Size: %ld MB in %d Sectors\n",
223 info->size >> 20, info->sector_count);
225 printf (" Sector Start Addresses:");
226 for (i=0; i<info->sector_count; ++i) {
229 printf (" %02d %08lX%s",
231 info->protect[i]!=0 ? " (RO)" : " "
238 ulong flash_recognize (vu_long *base)
241 ulong res = FLASH_UNKNOWN;
242 vu_short * sb = (vu_short*)base;
249 switch (id & 0x00FF0000)
251 case (MT_MANUFACT & 0x00FF0000): /* MT or => Intel */
252 case (INTEL_ALT_MANU & 0x00FF0000):
253 res = FLASH_MAN_INTEL;
264 /*-----------------------------------------------------------------------*/
265 #define INTEL_FLASH_STATUS_BLS 0x02
266 #define INTEL_FLASH_STATUS_PSS 0x04
267 #define INTEL_FLASH_STATUS_VPPS 0x08
268 #define INTEL_FLASH_STATUS_PS 0x10
269 #define INTEL_FLASH_STATUS_ES 0x20
270 #define INTEL_FLASH_STATUS_ESS 0x40
271 #define INTEL_FLASH_STATUS_WSMS 0x80
273 int flash_decode_status_bits(char status)
277 if(!(status & INTEL_FLASH_STATUS_WSMS)) {
282 if(status & INTEL_FLASH_STATUS_ESS) {
283 printf("Erase suspended\n");
287 if(status & INTEL_FLASH_STATUS_ES) {
288 printf("Error in block erase\n");
292 if(status & INTEL_FLASH_STATUS_PS) {
293 printf("Error in programming\n");
297 if(status & INTEL_FLASH_STATUS_VPPS) {
298 printf("Vpp low, operation aborted\n");
302 if(status & INTEL_FLASH_STATUS_PSS) {
303 printf("Program is suspended\n");
307 if(status & INTEL_FLASH_STATUS_BLS) {
308 printf("Attempting to program/erase a locked sector\n");
312 if((status & INTEL_FLASH_STATUS_PS) &&
313 (status & INTEL_FLASH_STATUS_ES) &&
314 (status & INTEL_FLASH_STATUS_ESS)) {
315 printf("A command sequence error\n");
322 /*-----------------------------------------------------------------------
325 int flash_erase (flash_info_t *info, int s_first, int s_last)
328 int flag, prot, sect;
332 if ((s_first < 0) || (s_first > s_last)) {
333 if (info->flash_id == FLASH_UNKNOWN) {
334 printf ("- missing\n");
336 printf ("- no sectors to erase\n");
341 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK)) {
342 printf ("Can't erase unknown flash type %08lx - aborted\n",
348 for (sect=s_first; sect<=s_last; ++sect) {
349 if (info->protect[sect]) {
355 printf ("- Warning: %d protected sectors will not be erased!\n",
361 start = get_timer (0);
363 /* Start erase on unprotected sectors */
364 for (sect = s_first; sect<=s_last; sect++) {
367 if (info->protect[sect] == 0) { /* not protected */
368 addr = (vu_short *)(info->start[sect]);
370 /* Disable interrupts which might cause a timeout here */
371 flag = disable_interrupts();
373 /* Single Block Erase Command */
377 /* Resume Command, as per errata update */
380 /* re-enable interrupts if necessary */
384 *addr = 0x70; /*Read status register command*/
385 tmp = (short)*addr & 0x00FF; /* Read the status */
386 while (!(tmp & INTEL_FLASH_STATUS_WSMS)) {
387 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
388 *addr = 0x0050; /* Reset the status register */
390 printf ("Timeout\n");
393 /* show that we're waiting */
394 if ((now - start) > 1000) { /* every second */
397 udelay(100000); /* 100 ms */
398 *addr = 0x0070; /*Read status register command*/
399 tmp = (short)*addr & 0x00FF; /* Read status */
400 start = get_timer(0);
402 if( tmp & INTEL_FLASH_STATUS_ES )
403 flash_decode_status_bits(tmp);
405 *addr = 0x0050; /* Reset the status register */
406 *addr = 0xffff; /* Reset to read mode */
415 void flash_unprotect (flash_info_t *info)
417 /*We can only unprotect the whole flash at once*/
418 /*Therefore we must prevent the _flash_real_protect()*/
419 /*from re-protecting sectors, that ware protected before */
420 /*we called flash_real_protect();*/
424 for(i = 0; i < info->sector_count; i++)
425 info->protect[i] = 0;
427 #ifdef CONFIG_SYS_FLASH_PROTECTION
428 _flash_real_protect(info, 0, 0);
432 /*-----------------------------------------------------------------------
433 * Copy memory to flash, returns:
436 * 2 - Flash not erased
439 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
444 wp = (addr & ~3); /* get lower word aligned address */
447 * handle unaligned start bytes
449 if ((l = addr - wp) != 0) {
451 for (i=0, cp=wp; i<l; ++i, ++cp) {
452 data = (data << 8) | (*(uchar *)cp);
454 for (; i<4 && cnt>0; ++i) {
455 data = (data << 8) | *src++;
459 for (; cnt==0 && i<4; ++i, ++cp) {
460 data = (data << 8) | (*(uchar *)cp);
463 if ((rc = write_word(info, wp, data)) != 0) {
470 * handle word aligned part
474 for (i=0; i<4; ++i) {
475 data = (data << 8) | *src++;
477 if ((rc = write_word(info, wp, data)) != 0) {
489 * handle unaligned tail bytes
492 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
493 data = (data << 8) | *src++;
496 for (; i<4; ++i, ++cp) {
497 data = (data << 8) | (*(uchar *)cp);
500 return (write_word(info, wp, data));
503 /*-----------------------------------------------------------------------
504 * Write a word to Flash, returns:
507 * 2 - Flash not erased
509 int write_word (flash_info_t *info, ulong dest, ulong da)
511 vu_short *addr = (vu_short *)dest;
523 /* Check if Flash is (sufficiently) erased */
524 if (((*addr & data.data16[0]) != data.data16[0]) ||
525 ((*(addr+1) & data.data16[1]) != data.data16[1])) {
528 /* Disable interrupts which might cause a timeout here */
529 flag = disable_interrupts();
531 for(i = 0; i < 2; i++)
537 *addr = data.data16[i];
539 /* re-enable interrupts if necessary */
543 /* data polling for D7 */
544 start = get_timer (0);
546 *addr = 0x0070; /*Read statusregister command */
547 while (((csr = *addr) & INTEL_FLASH_STATUS_WSMS)!=INTEL_FLASH_STATUS_WSMS) {
548 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
552 *addr = 0x0070; /*Read statusregister command */
554 if (csr & INTEL_FLASH_STATUS_PSS) {
555 printf ("CSR indicates write error (%0x) at %08lx\n",
560 /* Clear Status Registers Command */
562 /* Reset to read array mode */
570 int flash_real_protect(flash_info_t *info, long offset, int prot)
574 for(idx = 0; idx < info->sector_count; idx++)
575 if(info->start[idx] == offset)
578 if(idx==info->sector_count)
582 /* Unprotect one sector, which means unprotect all flash
583 * and reprotect the other protected sectors.
585 _flash_real_protect(info, 0, 0); /* Unprotects the whole flash*/
586 info->protect[idx] = 0;
588 for(i = 0; i < info->sector_count; i++)
590 _flash_real_protect(info, i, 1);
593 /* We can protect individual sectors */
594 _flash_real_protect(info, idx, 1);
597 for( i = 0; i < info->sector_count; i++)
598 info->protect[i] = flash_get_protect_status(info, i);
603 int _flash_real_protect(flash_info_t *info, long idx, int prot)
611 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK)) {
612 printf ("Can't change protection for unknown flash type %08lx - aborted\n",
618 /*Unlock the sector*/
626 addr = (vu_short *)(info->start[idx]);
628 /* If chip is busy, wait for it */
629 start = get_timer(0);
630 *addr = 0x0070; /*Read status register command*/
631 tmp = ((ushort)(*addr))&0x00ff; /*Read the status*/
632 while(!(tmp & INTEL_FLASH_STATUS_WSMS)) {
633 /*Write State Machine Busy*/
634 /*Wait untill done or timeout.*/
635 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_WRITE_TOUT) {
636 *addr = 0x0050; /* Reset the status register */
637 *addr = 0xffff; /* Reset the chip */
638 printf ("TTimeout\n");
642 tmp = ((ushort)(*addr))&0x00ff; /*Read the status*/
643 start = get_timer(0);
646 /* Disable interrupts which might cause a timeout here */
647 flag = disable_interrupts();
654 /* re-enable interrupts if necessary */
658 start = get_timer(0);
659 *addr = 0x0070; /*Read status register command*/
660 tmp = ((ushort)(*addr)) & 0x00FF; /* Read the status */
661 while (!(tmp & INTEL_FLASH_STATUS_WSMS)) {
662 /* Write State Machine Busy */
663 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_WRITE_TOUT) {
664 *addr = 0x0050; /* Reset the status register */
666 printf ("Timeout\n");
669 /* show that we're waiting */
670 if ((now - start) > 1000) { /* every second */
673 udelay(100000); /* 100 ms */
674 *addr = 0x70; /*Read status register command*/
675 tmp = (short)*addr & 0x00FF; /* Read status */
676 start = get_timer(0);
678 if( tmp & INTEL_FLASH_STATUS_PS )
679 flash_decode_status_bits(tmp);
681 *addr =0x0050; /*Clear status register*/
683 /* reset to read mode */