]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - board/bf533-ezkit/flash.c
rename CFG_ macros to CONFIG_SYS
[people/ms/u-boot.git] / board / bf533-ezkit / flash.c
index 067a2609065a3865b14be06e15cb44ab5bd7ab4b..a861e16a3df40790d25009840bc239b9f3d888b6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * U-boot - flash.c Flash driver for PSD4256GV
  *
- * Copyright (c) 2005 blackfin.uclinux.org
+ * Copyright (c) 2005-2007 Analog Devices Inc.
  * This file is based on BF533EzFlash.c originally written by Analog Devices, Inc.
  *
  * (C) Copyright 2000-2004
@@ -22,8 +22,8 @@
  *
  * 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
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
  */
 
 #include <asm/io.h>
@@ -82,7 +82,7 @@ unsigned long flash_init(void)
 
        size_b0 = size_b1 = size_b2 = 0;
 #ifdef DEBUG
-       printf("Flash Memory Start 0x%x\n", CFG_FLASH_BASE);
+       printf("Flash Memory Start 0x%x\n", CONFIG_SYS_FLASH_BASE);
        printf("Memory Map for the Flash\n");
        printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n");
        printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n");
@@ -90,20 +90,20 @@ unsigned long flash_init(void)
        printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n");
        printf("Please type command flinfo for information on Sectors \n");
 #endif
-       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;
        }
 
-       size_b0 = flash_get_size(CFG_FLASH0_BASE, &flash_info[0], 0);
-       size_b1 = flash_get_size(CFG_FLASH0_BASE, &flash_info[1], 1);
-       size_b2 = flash_get_size(CFG_FLASH0_BASE, &flash_info[2], 2);
+       size_b0 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[0], 0);
+       size_b1 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[1], 1);
+       size_b2 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[2], 2);
 
        if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
                printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
                       size_b0, size_b0 >> 20);
        }
 
-       (void)flash_protect(FLAG_PROTECT_SET, CFG_FLASH0_BASE,
+       (void)flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_FLASH0_BASE,
                            (flash_info[0].start[2] - 1), &flash_info[0]);
 
        return (size_b0 + size_b1 + size_b2);
@@ -178,63 +178,66 @@ int flash_erase(flash_info_t * info, int s_first, int s_last)
 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
 {
        int ret;
-
-       ret = write_data(addr, cnt, 1, (int *)src);
+       int d;
+       if (addr % 2) {
+               read_flash(addr - 1 - CONFIG_SYS_FLASH_BASE, &d);
+               d = (int)((d & 0x00FF) | (*src++ << 8));
+               ret = write_data(addr - 1, 2, (uchar *) & d);
+               if (ret == FLASH_FAIL)
+                       return ERR_NOT_ERASED;
+               ret = write_data(addr + 1, cnt - 1, src);
+       } else
+               ret = write_data(addr, cnt, src);
        if (ret == FLASH_FAIL)
                return ERR_NOT_ERASED;
        return FLASH_SUCCESS;
 }
 
-int write_data(long lStart, long lCount, long lStride, int *pnData)
+int write_data(long lStart, long lCount, uchar * pnData)
 {
        long i = 0;
-       int j = 0;
-       unsigned long ulOffset = lStart - CFG_FLASH_BASE;
+       unsigned long ulOffset = lStart - CONFIG_SYS_FLASH_BASE;
        int d;
-       int iShift = 0;
-       int iNumWords = 2;
-       int nLeftover = lCount % 4;
        int nSector = 0;
+       int flag = 0;
 
-       for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
-               for (iShift = 0, j = 0; (j < iNumWords);
-                    j++, ulOffset += (lStride * 2)) {
-                       if ((ulOffset >= INVALIDLOCNSTART)
-                           && (ulOffset < INVALIDLOCNEND)) {
-                               printf
-                                   ("Invalid locations, Try writing to another location \n");
-                               return FLASH_FAIL;
-                       }
-                       get_sector_number(ulOffset, &nSector);
-                       read_flash(ulOffset, &d);
-                       if (d != 0xffff) {
-                               printf
-                                   ("Flash not erased at offset 0x%x Please erase to reprogram \n",
-                                    ulOffset);
-                               return FLASH_FAIL;
-                       }
-                       unlock_flash(ulOffset);
-                       if (write_flash(ulOffset, (pnData[i] >> iShift)) < 0) {
-                               printf("Error programming the flash \n");
-                               return FLASH_FAIL;
-                       }
-                       iShift += 16;
-               }
+       if (lCount % 2) {
+               flag = 1;
+               lCount = lCount - 1;
        }
-       if (nLeftover > 0) {
-               if ((ulOffset >= INVALIDLOCNSTART)
-                   && (ulOffset < INVALIDLOCNEND))
+
+       for (i = 0; i < lCount - 1; i += 2, ulOffset += 2) {
+               get_sector_number(ulOffset, &nSector);
+               read_flash(ulOffset, &d);
+               if (d != 0xffff) {
+                       printf
+                           ("Flash not erased at offset 0x%x Please erase to reprogram \n",
+                            ulOffset);
                        return FLASH_FAIL;
+               }
+               unlock_flash(ulOffset);
+               d = (int)(pnData[i] | pnData[i + 1] << 8);
+               write_flash(ulOffset, d);
+               if (poll_toggle_bit(ulOffset) < 0) {
+                       printf("Error programming the flash \n");
+                       return FLASH_FAIL;
+               }
+               if ((i > 0) && (!(i % AFP_SectorSize2)))
+                       printf(".");
+       }
+       if (flag) {
                get_sector_number(ulOffset, &nSector);
                read_flash(ulOffset, &d);
                if (d != 0xffff) {
                        printf
-                           ("Flash already programmed. Please erase to reprogram \n");
-                       printf("uloffset = 0x%x \t d = 0x%x\n", ulOffset, d);
+                           ("Flash not erased at offset 0x%x Please erase to reprogram \n",
+                            ulOffset);
                        return FLASH_FAIL;
                }
                unlock_flash(ulOffset);
-               if (write_flash(ulOffset, pnData[i]) < 0) {
+               d = (int)(pnData[i] | (d & 0xFF00));
+               write_flash(ulOffset, d);
+               if (poll_toggle_bit(ulOffset) < 0) {
                        printf("Error programming the flash \n");
                        return FLASH_FAIL;
                }
@@ -282,10 +285,10 @@ int write_flash(long nOffset, int nValue)
 {
        long addr;
 
-       addr = (CFG_FLASH_BASE + nOffset);
-       sync();
+       addr = (CONFIG_SYS_FLASH_BASE + nOffset);
+       SSYNC();
        *(unsigned volatile short *)addr = nValue;
-       sync();
+       SSYNC();
        if (poll_toggle_bit(nOffset) < 0)
                return FLASH_FAIL;
        return FLASH_SUCCESS;
@@ -294,13 +297,13 @@ int write_flash(long nOffset, int nValue)
 int read_flash(long nOffset, int *pnValue)
 {
        int nValue = 0x0;
-       long addr = (CFG_FLASH_BASE + nOffset);
+       long addr = (CONFIG_SYS_FLASH_BASE + nOffset);
 
        if (nOffset != 0x2)
                reset_flash();
-       sync();
+       SSYNC();
        nValue = *(volatile unsigned short *)addr;
-       sync();
+       SSYNC();
        *pnValue = nValue;
        return TRUE;
 }
@@ -393,7 +396,7 @@ int erase_block_flash(int nBlock, unsigned long address)
        if ((nBlock < 0) || (nBlock > AFP_NumSectors))
                return FALSE;
 
-       ulSectorOff = (address - CFG_FLASH_BASE);
+       ulSectorOff = (address - CONFIG_SYS_FLASH_BASE);
 
        write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
        write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);