Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - flash.c Flash driver for PSD4256GV |
| 3 | * |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 4 | * Copyright (c) 2005-2007 Analog Devices Inc. |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 5 | * This file is based on BF533EzFlash.c originally written by Analog Devices, Inc. |
| 6 | * |
| 7 | * (C) Copyright 2000-2004 |
| 8 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 9 | * |
| 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 26 | * MA 02110-1301 USA |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 27 | */ |
| 28 | |
Aubrey Li | 3c56995 | 2007-03-12 00:25:14 +0800 | [diff] [blame] | 29 | #include <asm/io.h> |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 30 | #include "flash-defines.h" |
| 31 | |
Mike Frysinger | f543e6b | 2008-10-11 20:30:28 -0400 | [diff] [blame^] | 32 | int AFP_NumSectors = 40; |
| 33 | long AFP_SectorSize1 = 0x10000; |
| 34 | int AFP_SectorSize2 = 0x4000; |
| 35 | |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 36 | void flash_reset(void) |
| 37 | { |
| 38 | reset_flash(); |
| 39 | } |
| 40 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 41 | unsigned long flash_get_size(ulong baseaddr, flash_info_t * info, int bank_flag) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 42 | { |
| 43 | int id = 0, i = 0; |
| 44 | static int FlagDev = 1; |
| 45 | |
| 46 | id = get_codes(); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 47 | if (FlagDev) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 48 | #ifdef DEBUG |
| 49 | printf("Device ID of the Flash is %x\n", id); |
| 50 | #endif |
| 51 | FlagDev = 0; |
| 52 | } |
| 53 | info->flash_id = id; |
| 54 | |
| 55 | switch (bank_flag) { |
| 56 | case 0: |
| 57 | for (i = PriFlashABegin; i < SecFlashABegin; i++) |
| 58 | info->start[i] = (baseaddr + (i * AFP_SectorSize1)); |
| 59 | info->size = 0x200000; |
| 60 | info->sector_count = 32; |
| 61 | break; |
| 62 | case 1: |
| 63 | info->start[0] = baseaddr + SecFlashASec1Off; |
| 64 | info->start[1] = baseaddr + SecFlashASec2Off; |
| 65 | info->start[2] = baseaddr + SecFlashASec3Off; |
| 66 | info->start[3] = baseaddr + SecFlashASec4Off; |
| 67 | info->size = 0x10000; |
| 68 | info->sector_count = 4; |
| 69 | break; |
| 70 | case 2: |
| 71 | info->start[0] = baseaddr + SecFlashBSec1Off; |
| 72 | info->start[1] = baseaddr + SecFlashBSec2Off; |
| 73 | info->start[2] = baseaddr + SecFlashBSec3Off; |
| 74 | info->start[3] = baseaddr + SecFlashBSec4Off; |
| 75 | info->size = 0x10000; |
| 76 | info->sector_count = 4; |
| 77 | break; |
| 78 | } |
| 79 | return (info->size); |
| 80 | } |
| 81 | |
| 82 | unsigned long flash_init(void) |
| 83 | { |
| 84 | unsigned long size_b0, size_b1, size_b2; |
| 85 | int i; |
| 86 | |
| 87 | size_b0 = size_b1 = size_b2 = 0; |
| 88 | #ifdef DEBUG |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 89 | printf("Flash Memory Start 0x%x\n", CONFIG_SYS_FLASH_BASE); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 90 | printf("Memory Map for the Flash\n"); |
| 91 | printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n"); |
| 92 | printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n"); |
| 93 | printf("0x20200000 - 0x2020FFFF Flash A Secondary (64KB)\n"); |
| 94 | printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n"); |
| 95 | printf("Please type command flinfo for information on Sectors \n"); |
| 96 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 97 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 98 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 99 | } |
| 100 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 101 | size_b0 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[0], 0); |
| 102 | size_b1 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[1], 1); |
| 103 | size_b2 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[2], 2); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 104 | |
| 105 | if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) { |
| 106 | printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 107 | size_b0, size_b0 >> 20); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 110 | (void)flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_FLASH0_BASE, |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 111 | (flash_info[0].start[2] - 1), &flash_info[0]); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 112 | |
| 113 | return (size_b0 + size_b1 + size_b2); |
| 114 | } |
| 115 | |
| 116 | void flash_print_info(flash_info_t * info) |
| 117 | { |
| 118 | int i; |
| 119 | |
| 120 | if (info->flash_id == FLASH_UNKNOWN) { |
| 121 | printf("missing or unknown FLASH type\n"); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | switch (info->flash_id) { |
| 126 | case FLASH_PSD4256GV: |
| 127 | printf("ST Microelectronics "); |
| 128 | break; |
| 129 | default: |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 130 | printf("Unknown Vendor: (0x%08X) ", info->flash_id); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 131 | break; |
| 132 | } |
| 133 | for (i = 0; i < info->sector_count; ++i) { |
| 134 | if ((i % 5) == 0) |
| 135 | printf("\n "); |
| 136 | printf(" %08lX%s", |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 137 | info->start[i], info->protect[i] ? " (RO)" : " "); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 138 | } |
| 139 | printf("\n"); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | int flash_erase(flash_info_t * info, int s_first, int s_last) |
| 144 | { |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 145 | int cnt = 0, i; |
| 146 | int prot, sect; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 147 | |
| 148 | prot = 0; |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 149 | for (sect = s_first; sect <= s_last; ++sect) { |
| 150 | if (info->protect[sect]) |
| 151 | prot++; |
| 152 | } |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 153 | |
| 154 | if (prot) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 155 | printf("- Warning: %d protected sectors will not be erased!\n", |
| 156 | prot); |
Wolfgang Denk | 2bad868 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 157 | else |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 158 | printf("\n"); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 159 | |
| 160 | cnt = s_last - s_first + 1; |
| 161 | |
| 162 | if (cnt == FLASH_TOT_SECT) { |
| 163 | printf("Erasing flash, Please Wait \n"); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 164 | if (erase_flash() < 0) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 165 | printf("Erasing flash failed \n"); |
| 166 | return FLASH_FAIL; |
| 167 | } |
| 168 | } else { |
| 169 | printf("Erasing Flash locations, Please Wait\n"); |
| 170 | for (i = s_first; i <= s_last; i++) { |
| 171 | if (info->protect[i] == 0) { /* not protected */ |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 172 | if (erase_block_flash(i, info->start[i]) < 0) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 173 | printf("Error Sector erasing \n"); |
| 174 | return FLASH_FAIL; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | return FLASH_SUCCESS; |
| 180 | } |
| 181 | |
| 182 | int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
| 183 | { |
| 184 | int ret; |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 185 | int d; |
| 186 | if (addr % 2) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 187 | read_flash(addr - 1 - CONFIG_SYS_FLASH_BASE, &d); |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 188 | d = (int)((d & 0x00FF) | (*src++ << 8)); |
| 189 | ret = write_data(addr - 1, 2, (uchar *) & d); |
| 190 | if (ret == FLASH_FAIL) |
| 191 | return ERR_NOT_ERASED; |
| 192 | ret = write_data(addr + 1, cnt - 1, src); |
| 193 | } else |
| 194 | ret = write_data(addr, cnt, src); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 195 | if (ret == FLASH_FAIL) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 196 | return ERR_NOT_ERASED; |
| 197 | return FLASH_SUCCESS; |
| 198 | } |
| 199 | |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 200 | int write_data(long lStart, long lCount, uchar * pnData) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 201 | { |
| 202 | long i = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 203 | unsigned long ulOffset = lStart - CONFIG_SYS_FLASH_BASE; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 204 | int d; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 205 | int nSector = 0; |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 206 | int flag = 0; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 207 | |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 208 | if (lCount % 2) { |
| 209 | flag = 1; |
| 210 | lCount = lCount - 1; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 211 | } |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 212 | |
| 213 | for (i = 0; i < lCount - 1; i += 2, ulOffset += 2) { |
| 214 | get_sector_number(ulOffset, &nSector); |
| 215 | read_flash(ulOffset, &d); |
| 216 | if (d != 0xffff) { |
| 217 | printf |
| 218 | ("Flash not erased at offset 0x%x Please erase to reprogram \n", |
| 219 | ulOffset); |
| 220 | return FLASH_FAIL; |
| 221 | } |
| 222 | unlock_flash(ulOffset); |
| 223 | d = (int)(pnData[i] | pnData[i + 1] << 8); |
| 224 | write_flash(ulOffset, d); |
| 225 | if (poll_toggle_bit(ulOffset) < 0) { |
| 226 | printf("Error programming the flash \n"); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 227 | return FLASH_FAIL; |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 228 | } |
| 229 | if ((i > 0) && (!(i % AFP_SectorSize2))) |
| 230 | printf("."); |
| 231 | } |
| 232 | if (flag) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 233 | get_sector_number(ulOffset, &nSector); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 234 | read_flash(ulOffset, &d); |
| 235 | if (d != 0xffff) { |
| 236 | printf |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 237 | ("Flash not erased at offset 0x%x Please erase to reprogram \n", |
| 238 | ulOffset); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 239 | return FLASH_FAIL; |
| 240 | } |
| 241 | unlock_flash(ulOffset); |
Aubrey Li | c9e6d06 | 2007-04-05 18:28:34 +0800 | [diff] [blame] | 242 | d = (int)(pnData[i] | (d & 0xFF00)); |
| 243 | write_flash(ulOffset, d); |
| 244 | if (poll_toggle_bit(ulOffset) < 0) { |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 245 | printf("Error programming the flash \n"); |
| 246 | return FLASH_FAIL; |
| 247 | } |
| 248 | } |
| 249 | return FLASH_SUCCESS; |
| 250 | } |
| 251 | |
| 252 | int read_data(long ulStart, long lCount, long lStride, int *pnData) |
| 253 | { |
| 254 | long i = 0; |
| 255 | int j = 0; |
| 256 | long ulOffset = ulStart; |
| 257 | int iShift = 0; |
| 258 | int iNumWords = 2; |
| 259 | int nLeftover = lCount % 4; |
| 260 | int nHi, nLow; |
| 261 | int nSector = 0; |
| 262 | |
| 263 | for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) { |
| 264 | for (iShift = 0, j = 0; j < iNumWords; j += 2) { |
| 265 | if ((ulOffset >= INVALIDLOCNSTART) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 266 | && (ulOffset < INVALIDLOCNEND)) |
| 267 | return FLASH_FAIL; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 268 | |
| 269 | get_sector_number(ulOffset, &nSector); |
| 270 | read_flash(ulOffset, &nLow); |
| 271 | ulOffset += (lStride * 2); |
| 272 | read_flash(ulOffset, &nHi); |
| 273 | ulOffset += (lStride * 2); |
| 274 | pnData[i] = (nHi << 16) | nLow; |
| 275 | } |
| 276 | } |
| 277 | if (nLeftover > 0) { |
| 278 | if ((ulOffset >= INVALIDLOCNSTART) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 279 | && (ulOffset < INVALIDLOCNEND)) |
| 280 | return FLASH_FAIL; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 281 | |
| 282 | get_sector_number(ulOffset, &nSector); |
| 283 | read_flash(ulOffset, &pnData[i]); |
| 284 | } |
| 285 | return FLASH_SUCCESS; |
| 286 | } |
| 287 | |
| 288 | int write_flash(long nOffset, int nValue) |
| 289 | { |
| 290 | long addr; |
| 291 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 292 | addr = (CONFIG_SYS_FLASH_BASE + nOffset); |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame] | 293 | SSYNC(); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 294 | *(unsigned volatile short *)addr = nValue; |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame] | 295 | SSYNC(); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 296 | if (poll_toggle_bit(nOffset) < 0) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 297 | return FLASH_FAIL; |
| 298 | return FLASH_SUCCESS; |
| 299 | } |
| 300 | |
| 301 | int read_flash(long nOffset, int *pnValue) |
| 302 | { |
| 303 | int nValue = 0x0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 304 | long addr = (CONFIG_SYS_FLASH_BASE + nOffset); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 305 | |
| 306 | if (nOffset != 0x2) |
| 307 | reset_flash(); |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame] | 308 | SSYNC(); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 309 | nValue = *(volatile unsigned short *)addr; |
Mike Frysinger | 66c4cf4 | 2008-02-04 19:26:55 -0500 | [diff] [blame] | 310 | SSYNC(); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 311 | *pnValue = nValue; |
| 312 | return TRUE; |
| 313 | } |
| 314 | |
| 315 | int poll_toggle_bit(long lOffset) |
| 316 | { |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 317 | unsigned int u1, u2; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 318 | unsigned long timeout = 0xFFFFFFFF; |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 319 | volatile unsigned long *FB = |
| 320 | (volatile unsigned long *)(0x20000000 + lOffset); |
| 321 | while (1) { |
| 322 | if (timeout < 0) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 323 | break; |
| 324 | u1 = *(volatile unsigned short *)FB; |
| 325 | u2 = *(volatile unsigned short *)FB; |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 326 | if ((u1 & 0x0040) == (u2 & 0x0040)) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 327 | return FLASH_SUCCESS; |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 328 | if ((u2 & 0x0020) == 0x0000) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 329 | continue; |
| 330 | u1 = *(volatile unsigned short *)FB; |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 331 | if ((u2 & 0x0040) == (u1 & 0x0040)) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 332 | return FLASH_SUCCESS; |
| 333 | else { |
| 334 | reset_flash(); |
| 335 | return FLASH_FAIL; |
| 336 | } |
| 337 | timeout--; |
| 338 | } |
| 339 | printf("Time out occured \n"); |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 340 | if (timeout < 0) |
| 341 | return FLASH_FAIL; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void reset_flash(void) |
| 345 | { |
| 346 | write_flash(WRITESEQ1, RESET_VAL); |
| 347 | /* Wait for 10 micro seconds */ |
| 348 | udelay(10); |
| 349 | } |
| 350 | |
| 351 | int erase_flash(void) |
| 352 | { |
| 353 | write_flash(WRITESEQ1, WRITEDATA1); |
| 354 | write_flash(WRITESEQ2, WRITEDATA2); |
| 355 | write_flash(WRITESEQ3, WRITEDATA3); |
| 356 | write_flash(WRITESEQ4, WRITEDATA4); |
| 357 | write_flash(WRITESEQ5, WRITEDATA5); |
| 358 | write_flash(WRITESEQ6, WRITEDATA6); |
| 359 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 360 | if (poll_toggle_bit(0x0000) < 0) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 361 | return FLASH_FAIL; |
| 362 | |
| 363 | write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1); |
| 364 | write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2); |
| 365 | write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3); |
| 366 | write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4); |
| 367 | write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5); |
| 368 | write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6); |
| 369 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 370 | if (poll_toggle_bit(SecFlashASec1Off) < 0) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 371 | return FLASH_FAIL; |
| 372 | |
| 373 | write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1); |
| 374 | write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2); |
| 375 | write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3); |
| 376 | write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4); |
| 377 | write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5); |
| 378 | write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6); |
| 379 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 380 | if (poll_toggle_bit(PriFlashBOff) < 0) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 381 | return FLASH_FAIL; |
| 382 | |
| 383 | write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1); |
| 384 | write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2); |
| 385 | write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3); |
| 386 | write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4); |
| 387 | write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5); |
| 388 | write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6); |
| 389 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 390 | if (poll_toggle_bit(SecFlashBOff) < 0) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 391 | return FLASH_FAIL; |
| 392 | |
| 393 | return FLASH_SUCCESS; |
| 394 | } |
| 395 | |
| 396 | int erase_block_flash(int nBlock, unsigned long address) |
| 397 | { |
| 398 | long ulSectorOff = 0x0; |
| 399 | |
| 400 | if ((nBlock < 0) || (nBlock > AFP_NumSectors)) |
| 401 | return FALSE; |
| 402 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 403 | ulSectorOff = (address - CONFIG_SYS_FLASH_BASE); |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 404 | |
| 405 | write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1); |
| 406 | write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2); |
| 407 | write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3); |
| 408 | write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4); |
| 409 | write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5); |
| 410 | |
| 411 | write_flash(ulSectorOff, BlockEraseVal); |
| 412 | |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 413 | if (poll_toggle_bit(ulSectorOff) < 0) |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 414 | return FLASH_FAIL; |
| 415 | |
| 416 | return FLASH_SUCCESS; |
| 417 | } |
| 418 | |
| 419 | void unlock_flash(long ulOffset) |
| 420 | { |
| 421 | unsigned long ulOffsetAddr = ulOffset; |
| 422 | ulOffsetAddr &= 0xFFFF0000; |
| 423 | |
| 424 | write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1); |
| 425 | write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2); |
| 426 | write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3); |
| 427 | } |
| 428 | |
| 429 | int get_codes() |
| 430 | { |
| 431 | int dev_id = 0; |
| 432 | |
| 433 | write_flash(WRITESEQ1, GETCODEDATA1); |
| 434 | write_flash(WRITESEQ2, GETCODEDATA2); |
| 435 | write_flash(WRITESEQ3, GETCODEDATA3); |
| 436 | |
| 437 | read_flash(0x0002, &dev_id); |
| 438 | dev_id &= 0x00FF; |
| 439 | |
| 440 | reset_flash(); |
| 441 | |
| 442 | return dev_id; |
| 443 | } |
| 444 | |
| 445 | void get_sector_number(long ulOffset, int *pnSector) |
| 446 | { |
| 447 | int nSector = 0; |
| 448 | |
| 449 | if (ulOffset >= SecFlashAOff) { |
| 450 | if ((ulOffset < SecFlashASec1Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 451 | && (ulOffset < SecFlashASec2Off)) { |
| 452 | nSector = SECT32; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 453 | } else if ((ulOffset >= SecFlashASec2Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 454 | && (ulOffset < SecFlashASec3Off)) { |
| 455 | nSector = SECT33; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 456 | } else if ((ulOffset >= SecFlashASec3Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 457 | && (ulOffset < SecFlashASec4Off)) { |
| 458 | nSector = SECT34; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 459 | } else if ((ulOffset >= SecFlashASec4Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 460 | && (ulOffset < SecFlashAEndOff)) { |
| 461 | nSector = SECT35; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 462 | } |
| 463 | } else if (ulOffset >= SecFlashBOff) { |
| 464 | if ((ulOffset < SecFlashBSec1Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 465 | && (ulOffset < SecFlashBSec2Off)) { |
| 466 | nSector = SECT36; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 467 | } |
| 468 | if ((ulOffset < SecFlashBSec2Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 469 | && (ulOffset < SecFlashBSec3Off)) { |
| 470 | nSector = SECT37; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 471 | } |
| 472 | if ((ulOffset < SecFlashBSec3Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 473 | && (ulOffset < SecFlashBSec4Off)) { |
| 474 | nSector = SECT38; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 475 | } |
| 476 | if ((ulOffset < SecFlashBSec4Off) |
Aubrey.Li | 9da597f | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 477 | && (ulOffset < SecFlashBEndOff)) { |
| 478 | nSector = SECT39; |
Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 479 | } |
| 480 | } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) { |
| 481 | nSector = ulOffset & 0xffff0000; |
| 482 | nSector = ulOffset >> 16; |
| 483 | nSector = nSector & 0x000ff; |
| 484 | } |
| 485 | |
| 486 | if ((nSector >= 0) && (nSector < AFP_NumSectors)) { |
| 487 | *pnSector = nSector; |
| 488 | } |
| 489 | } |