]> git.sur5r.net Git - u-boot/blobdiff - board/lwmon/flash.c
Merge branch 'next' of git://git.denx.de/u-boot-mpc83xx
[u-boot] / board / lwmon / flash.c
index 4004865c3157e04d7eeb798a10a388c3b341d79b..cb60c418a1df6114f7d39cd41a6737258d243129 100644 (file)
@@ -2,23 +2,7 @@
  * (C) Copyright 2001
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /* #define DEBUG */
 #include <common.h>
 #include <mpc8xx.h>
 
-#if defined(CFG_ENV_IS_IN_FLASH)
-# ifndef  CFG_ENV_ADDR
-#  define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
+#if defined(CONFIG_ENV_IS_IN_FLASH)
+# ifndef  CONFIG_ENV_ADDR
+#  define CONFIG_ENV_ADDR      (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
 # endif
-# ifndef  CFG_ENV_SIZE
-#  define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
+# ifndef  CONFIG_ENV_SIZE
+#  define CONFIG_ENV_SIZE      CONFIG_ENV_SECT_SIZE
 # endif
-# ifndef  CFG_ENV_SECT_SIZE
-#  define CFG_ENV_SECT_SIZE  CFG_ENV_SIZE
+# ifndef  CONFIG_ENV_SECT_SIZE
+#  define CONFIG_ENV_SECT_SIZE  CONFIG_ENV_SIZE
 # endif
 #endif
 
 /*---------------------------------------------------------------------*/
 
-flash_info_t   flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips        */
+flash_info_t   flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
 
 /*-----------------------------------------------------------------------
  * Functions
  */
 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
 static int write_data (flash_info_t *info, ulong dest, ulong data);
-#ifdef CFG_FLASH_USE_BUFFER_WRITE
+#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
 static int write_data_buf (flash_info_t * info, ulong dest, uchar * cp, int len);
 #endif
 static void flash_get_offsets (ulong base, flash_info_t *info);
@@ -57,13 +41,13 @@ static void flash_get_offsets (ulong base, flash_info_t *info);
 
 unsigned long flash_init (void)
 {
-       volatile immap_t     *immap  = (immap_t *)CFG_IMMR;
+       volatile immap_t     *immap  = (immap_t *)CONFIG_SYS_IMMR;
        volatile memctl8xx_t *memctl = &immap->im_memctl;
        unsigned long size_b0, size_b1;
        int i;
 
        /* Init: no FLASHes known */
-       for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
+       for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
                flash_info[i].flash_id = FLASH_UNKNOWN;
        }
 
@@ -108,66 +92,66 @@ unsigned long flash_init (void)
                memctl->memc_br1, memctl->memc_or1);
 
        /* Remap FLASH according to real size */
-       memctl->memc_or0 = (-size_b0 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
+       memctl->memc_or0 = (-size_b0 & 0xFFFF8000) | CONFIG_SYS_OR_TIMING_FLASH |
                                OR_CSNT_SAM | OR_ACS_DIV1;
-       memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_32 | BR_V;
+       memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_32 | BR_V;
 
        debug ("## BR0: 0x%08x    OR0: 0x%08x\n",
                memctl->memc_br0, memctl->memc_or0);
 
        /* Re-do sizing to get full correct info */
-       size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
+       size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
 
-       flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
+       flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
 
        flash_info[0].size = size_b0;
 
-#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
+#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
        /* monitor protection ON by default */
        flash_protect(FLAG_PROTECT_SET,
-                     CFG_MONITOR_BASE,
-                     CFG_MONITOR_BASE+monitor_flash_len-1,
+                     CONFIG_SYS_MONITOR_BASE,
+                     CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
                      &flash_info[0]);
 #endif
 
-#ifdef CFG_ENV_IS_IN_FLASH
+#ifdef CONFIG_ENV_IS_IN_FLASH
        /* ENV protection ON by default */
        flash_protect(FLAG_PROTECT_SET,
-                     CFG_ENV_ADDR,
-                     CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
+                     CONFIG_ENV_ADDR,
+                     CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
                      &flash_info[0]);
 #endif
 
        if (size_b1) {
-               memctl->memc_or1 = (-size_b1 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
+               memctl->memc_or1 = (-size_b1 & 0xFFFF8000) | CONFIG_SYS_OR_TIMING_FLASH |
                                        OR_CSNT_SAM | OR_ACS_DIV1;
-               memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
+               memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
                                        BR_PS_32 | BR_V;
 
                debug ("## BR1: 0x%08x    OR1: 0x%08x\n",
                        memctl->memc_br1, memctl->memc_or1);
 
                /* Re-do sizing to get full correct info */
-               size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
+               size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE + size_b0),
                                          &flash_info[1]);
 
                flash_info[1].size = size_b1;
 
-               flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
+               flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
 
-#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
+#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
                /* monitor protection ON by default */
                flash_protect(FLAG_PROTECT_SET,
-                             CFG_MONITOR_BASE,
-                             CFG_MONITOR_BASE+monitor_flash_len-1,
+                             CONFIG_SYS_MONITOR_BASE,
+                             CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
                              &flash_info[1]);
 #endif
 
-#ifdef CFG_ENV_IS_IN_FLASH
+#ifdef CONFIG_ENV_IS_IN_FLASH
                /* ENV protection ON by default */
                flash_protect(FLAG_PROTECT_SET,
-                             CFG_ENV_ADDR,
-                             CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
+                             CONFIG_ENV_ADDR,
+                             CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
                              &flash_info[1]);
 #endif
        } else {
@@ -342,10 +326,10 @@ static ulong flash_get_size (vu_long *addr, flash_info_t *info)
 
        }
 
-       if (info->sector_count > CFG_MAX_FLASH_SECT) {
+       if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
                printf ("** ERROR: sector count %d > max (%d) **\n",
-                       info->sector_count, CFG_MAX_FLASH_SECT);
-               info->sector_count = CFG_MAX_FLASH_SECT;
+                       info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
+               info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
        }
 
        addr[0] = 0x00FF00FF;           /* restore read mode */
@@ -403,6 +387,26 @@ int        flash_erase (flash_info_t *info, int s_first, int s_last)
                        /* Disable interrupts which might cause a timeout here */
                        flag = disable_interrupts();
 
+                       *addr = 0x00600060;     /* clear lock bit setup */
+                       *addr = 0x00D000D0;     /* clear lock bit confirm */
+
+                       udelay (1000);
+                       /* This takes awfully long - up to 50 ms and more */
+                       while (((status = *addr) & 0x00800080) != 0x00800080) {
+                               if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
+                                       printf ("Timeout\n");
+                                       *addr = 0x00FF00FF; /* reset to read mode */
+                                       return 1;
+                               }
+
+                               /* show that we're waiting */
+                               if ((now - last) > 1000) {      /* every second */
+                                       putc ('.');
+                                       last = now;
+                               }
+                               udelay (1000);  /* to trigger the watchdog */
+                       }
+
                        *addr = 0x00500050;     /* clear status register */
                        *addr = 0x00200020;     /* erase setup */
                        *addr = 0x00D000D0;     /* erase confirm */
@@ -415,7 +419,7 @@ int flash_erase (flash_info_t *info, int s_first, int s_last)
                        udelay (1000);
 
                        while (((status = *addr) & 0x00800080) != 0x00800080) {
-                               if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
+                               if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
                                        printf ("Timeout\n");
                                        *addr = 0x00B000B0; /* suspend erase      */
                                        *addr = 0x00FF00FF; /* reset to read mode */
@@ -427,6 +431,7 @@ int flash_erase (flash_info_t *info, int s_first, int s_last)
                                        putc ('.');
                                        last = now;
                                }
+                               udelay (1000);  /* to trigger the watchdog */
                        }
 
                        *addr = 0x00FF00FF;     /* reset to read mode */
@@ -483,10 +488,10 @@ int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
        /*
         * handle FLASH_WIDTH aligned part
         */
-#ifdef CFG_FLASH_USE_BUFFER_WRITE
+#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
        while(cnt >= FLASH_WIDTH) {
-               i = CFG_FLASH_BUFFER_SIZE > cnt ?
-                   (cnt & ~(FLASH_WIDTH - 1)) : CFG_FLASH_BUFFER_SIZE;
+               i = CONFIG_SYS_FLASH_BUFFER_SIZE > cnt ?
+                   (cnt & ~(FLASH_WIDTH - 1)) : CONFIG_SYS_FLASH_BUFFER_SIZE;
                if((rc = write_data_buf(info, wp, src,i)) != 0)
                        return rc;
                wp += i;
@@ -505,7 +510,7 @@ int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
                wp  += FLASH_WIDTH;
                cnt -= FLASH_WIDTH;
        }
-#endif /* CFG_FLASH_USE_BUFFER_WRITE */
+#endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
 
        if (cnt == 0) {
                return (0);
@@ -573,7 +578,7 @@ static int write_data (flash_info_t *info, ulong dest, ulong data)
        if (flag)
                enable_interrupts();
 
-       if (flash_status_check(addr, CFG_FLASH_WRITE_TOUT, "write") != 0) {
+       if (flash_status_check(addr, CONFIG_SYS_FLASH_WRITE_TOUT, "write") != 0) {
                return (1);
        }
 
@@ -582,7 +587,7 @@ static int write_data (flash_info_t *info, ulong dest, ulong data)
        return (0);
 }
 
-#ifdef CFG_FLASH_USE_BUFFER_WRITE
+#ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
 /*-----------------------------------------------------------------------
  * Write a buffer to Flash, returns:
  * 0 - OK
@@ -606,7 +611,7 @@ static int write_data_buf(flash_info_t * info, ulong dest, uchar * cp, int len)
        *addr = 0x00500050;             /* clear status */
        *addr = 0x00e800e8;             /* write buffer */
 
-       if((retcode = flash_status_check(addr, CFG_FLASH_BUFFER_WRITE_TOUT,
+       if((retcode = flash_status_check(addr, CONFIG_SYS_FLASH_BUFFER_WRITE_TOUT,
                                         "write to buffer")) == 0) {
                cnt = len / FLASH_WIDTH;
                *addr = (cnt-1) | ((cnt-1) << 16);
@@ -614,14 +619,14 @@ static int write_data_buf(flash_info_t * info, ulong dest, uchar * cp, int len)
                        *dst++ = *src++;
                }
                *addr = 0x00d000d0;             /* write buffer confirm */
-               retcode = flash_status_check(addr, CFG_FLASH_BUFFER_WRITE_TOUT,
+               retcode = flash_status_check(addr, CONFIG_SYS_FLASH_BUFFER_WRITE_TOUT,
                                                 "buffer write");
        }
        *addr = 0x00FF00FF;     /* restore read mode */
        *addr = 0x00500050;     /* clear status */
        return retcode;
 }
-#endif /* CFG_USE_FLASH_BUFFER_WRITE */
+#endif /* CONFIG_SYS_USE_FLASH_BUFFER_WRITE */
 
 /*-----------------------------------------------------------------------
  */