wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Lineo, Inc. <www.lineo.com> |
| 4 | * Bernhard Kuhn <bkuhn@lineo.com> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Alex Zuepke <azu@sysgo.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 |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | |
| 29 | #include <common.h> |
| 30 | |
| 31 | ulong myflush(void); |
| 32 | |
| 33 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 34 | /* Flash Organization Structure */ |
| 35 | typedef struct OrgDef |
| 36 | { |
| 37 | unsigned int sector_number; |
| 38 | unsigned int sector_size; |
| 39 | } OrgDef; |
| 40 | |
| 41 | |
| 42 | /* Flash Organizations */ |
| 43 | OrgDef OrgAT49BV16x4[] = |
| 44 | { |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 45 | { 8, 8*1024 }, /* 8 * 8 kBytes sectors */ |
| 46 | { 2, 32*1024 }, /* 2 * 32 kBytes sectors */ |
| 47 | { 30, 64*1024 }, /* 30 * 64 kBytes sectors */ |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | OrgDef OrgAT49BV16x4A[] = |
| 51 | { |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 52 | { 8, 8*1024 }, /* 8 * 8 kBytes sectors */ |
| 53 | { 31, 64*1024 }, /* 31 * 64 kBytes sectors */ |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 56 | OrgDef OrgAT49BV6416[] = |
| 57 | { |
| 58 | { 8, 8*1024 }, /* 8 * 8 kBytes sectors */ |
| 59 | { 127, 64*1024 }, /* 127 * 64 kBytes sectors */ |
| 60 | }; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 61 | |
| 62 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; |
| 63 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 64 | /* AT49BV1614A Codes */ |
| 65 | #define FLASH_CODE1 0xAA |
| 66 | #define FLASH_CODE2 0x55 |
| 67 | #define ID_IN_CODE 0x90 |
| 68 | #define ID_OUT_CODE 0xF0 |
| 69 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 70 | |
| 71 | #define CMD_READ_ARRAY 0x00F0 |
| 72 | #define CMD_UNLOCK1 0x00AA |
| 73 | #define CMD_UNLOCK2 0x0055 |
| 74 | #define CMD_ERASE_SETUP 0x0080 |
| 75 | #define CMD_ERASE_CONFIRM 0x0030 |
| 76 | #define CMD_PROGRAM 0x00A0 |
| 77 | #define CMD_UNLOCK_BYPASS 0x0020 |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 78 | #define CMD_SECTOR_UNLOCK 0x0070 |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 79 | |
| 80 | #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00005555<<1))) |
| 81 | #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00002AAA<<1))) |
| 82 | |
| 83 | #define BIT_ERASE_DONE 0x0080 |
| 84 | #define BIT_RDY_MASK 0x0080 |
| 85 | #define BIT_PROGRAM_ERROR 0x0020 |
| 86 | #define BIT_TIMEOUT 0x80000000 /* our flag */ |
| 87 | |
| 88 | #define READY 1 |
| 89 | #define ERR 2 |
| 90 | #define TMO 4 |
| 91 | |
| 92 | /*----------------------------------------------------------------------- |
| 93 | */ |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 94 | void flash_identification (flash_info_t * info) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 95 | { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 96 | volatile u16 manuf_code, device_code, add_device_code; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 97 | |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 98 | MEM_FLASH_ADDR1 = FLASH_CODE1; |
| 99 | MEM_FLASH_ADDR2 = FLASH_CODE2; |
| 100 | MEM_FLASH_ADDR1 = ID_IN_CODE; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 101 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 102 | manuf_code = *(volatile u16 *) CFG_FLASH_BASE; |
| 103 | device_code = *(volatile u16 *) (CFG_FLASH_BASE + 2); |
| 104 | add_device_code = *(volatile u16 *) (CFG_FLASH_BASE + (3 << 1)); |
| 105 | |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 106 | MEM_FLASH_ADDR1 = FLASH_CODE1; |
| 107 | MEM_FLASH_ADDR2 = FLASH_CODE2; |
| 108 | MEM_FLASH_ADDR1 = ID_OUT_CODE; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 109 | |
| 110 | /* Vendor type */ |
| 111 | info->flash_id = ATM_MANUFACT & FLASH_VENDMASK; |
| 112 | printf ("Atmel: "); |
| 113 | |
| 114 | if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV1614 & FLASH_TYPEMASK)) { |
| 115 | |
| 116 | if ((add_device_code & FLASH_TYPEMASK) == |
| 117 | (ATM_ID_BV1614A & FLASH_TYPEMASK)) { |
| 118 | info->flash_id |= ATM_ID_BV1614A & FLASH_TYPEMASK; |
| 119 | printf ("AT49BV1614A (16Mbit)\n"); |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 120 | } else { /* AT49BV1614 Flash */ |
| 121 | info->flash_id |= ATM_ID_BV1614 & FLASH_TYPEMASK; |
| 122 | printf ("AT49BV1614 (16Mbit)\n"); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 123 | } |
| 124 | |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 125 | } else if ((device_code & FLASH_TYPEMASK) == (ATM_ID_BV6416 & FLASH_TYPEMASK)) { |
| 126 | info->flash_id |= ATM_ID_BV6416 & FLASH_TYPEMASK; |
| 127 | printf ("AT49BV6416 (64Mbit)\n"); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 128 | } |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 129 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 130 | |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 131 | ushort flash_number_sector(OrgDef *pOrgDef, unsigned int nb_blocks) |
| 132 | { |
| 133 | int i, nb_sectors = 0; |
| 134 | |
| 135 | for (i=0; i<nb_blocks; i++){ |
| 136 | nb_sectors += pOrgDef[i].sector_number; |
| 137 | } |
| 138 | |
| 139 | return nb_sectors; |
| 140 | } |
| 141 | |
| 142 | void flash_unlock_sector(flash_info_t * info, unsigned int sector) |
| 143 | { |
| 144 | volatile u16 *addr = (volatile u16 *) (info->start[sector]); |
| 145 | |
| 146 | MEM_FLASH_ADDR1 = CMD_UNLOCK1; |
| 147 | *addr = CMD_SECTOR_UNLOCK; |
| 148 | } |
| 149 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 150 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 151 | ulong flash_init (void) |
| 152 | { |
| 153 | int i, j, k; |
| 154 | unsigned int flash_nb_blocks, sector; |
| 155 | unsigned int start_address; |
| 156 | OrgDef *pOrgDef; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 157 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 158 | ulong size = 0; |
| 159 | |
| 160 | for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) { |
| 161 | ulong flashbase = 0; |
| 162 | |
| 163 | flash_identification (&flash_info[i]); |
| 164 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 165 | if ((flash_info[i].flash_id & FLASH_TYPEMASK) == |
| 166 | (ATM_ID_BV1614 & FLASH_TYPEMASK)) { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 167 | |
| 168 | pOrgDef = OrgAT49BV16x4; |
| 169 | flash_nb_blocks = sizeof (OrgAT49BV16x4) / sizeof (OrgDef); |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 170 | } else if ((flash_info[i].flash_id & FLASH_TYPEMASK) == |
| 171 | (ATM_ID_BV1614A & FLASH_TYPEMASK)){ /* AT49BV1614A Flash */ |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 172 | |
| 173 | pOrgDef = OrgAT49BV16x4A; |
| 174 | flash_nb_blocks = sizeof (OrgAT49BV16x4A) / sizeof (OrgDef); |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 175 | } else if ((flash_info[i].flash_id & FLASH_TYPEMASK) == |
| 176 | (ATM_ID_BV6416 & FLASH_TYPEMASK)){ /* AT49BV6416 Flash */ |
| 177 | |
| 178 | pOrgDef = OrgAT49BV6416; |
| 179 | flash_nb_blocks = sizeof (OrgAT49BV6416) / sizeof (OrgDef); |
| 180 | } else { |
| 181 | flash_nb_blocks = 0; |
| 182 | pOrgDef = OrgAT49BV16x4; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 183 | } |
| 184 | |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 185 | flash_info[i].sector_count = flash_number_sector(pOrgDef, flash_nb_blocks); |
| 186 | memset (flash_info[i].protect, 0, flash_info[i].sector_count); |
| 187 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 188 | if (i == 0) |
| 189 | flashbase = PHYS_FLASH_1; |
| 190 | else |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame] | 191 | panic ("configured too many flash banks!\n"); |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 192 | |
| 193 | sector = 0; |
| 194 | start_address = flashbase; |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 195 | flash_info[i].size = 0; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 196 | |
| 197 | for (j = 0; j < flash_nb_blocks; j++) { |
| 198 | for (k = 0; k < pOrgDef[j].sector_number; k++) { |
| 199 | flash_info[i].start[sector++] = start_address; |
| 200 | start_address += pOrgDef[j].sector_size; |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 201 | flash_info[i].size += pOrgDef[j].sector_size; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | size += flash_info[i].size; |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 206 | |
| 207 | if ((flash_info[i].flash_id & FLASH_TYPEMASK) == |
| 208 | (ATM_ID_BV6416 & FLASH_TYPEMASK)){ /* AT49BV6416 Flash */ |
| 209 | |
| 210 | /* Unlock all sectors at reset */ |
| 211 | for (j=0; j<flash_info[i].sector_count; j++){ |
| 212 | flash_unlock_sector(&flash_info[i], j); |
| 213 | } |
| 214 | } |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* Protect binary boot image */ |
| 218 | flash_protect (FLAG_PROTECT_SET, |
| 219 | CFG_FLASH_BASE, |
| 220 | CFG_FLASH_BASE + CFG_BOOT_SIZE - 1, &flash_info[0]); |
| 221 | |
| 222 | /* Protect environment variables */ |
| 223 | flash_protect (FLAG_PROTECT_SET, |
| 224 | CFG_ENV_ADDR, |
| 225 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]); |
| 226 | |
| 227 | /* Protect U-Boot gzipped image */ |
| 228 | flash_protect (FLAG_PROTECT_SET, |
| 229 | CFG_U_BOOT_BASE, |
| 230 | CFG_U_BOOT_BASE + CFG_U_BOOT_SIZE - 1, &flash_info[0]); |
| 231 | |
| 232 | return size; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /*----------------------------------------------------------------------- |
| 236 | */ |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 237 | void flash_print_info (flash_info_t * info) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 238 | { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 239 | int i; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 240 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 241 | switch (info->flash_id & FLASH_VENDMASK) { |
| 242 | case (ATM_MANUFACT & FLASH_VENDMASK): |
| 243 | printf ("Atmel: "); |
| 244 | break; |
| 245 | default: |
| 246 | printf ("Unknown Vendor "); |
| 247 | break; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 248 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 249 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 250 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 251 | case (ATM_ID_BV1614 & FLASH_TYPEMASK): |
| 252 | printf ("AT49BV1614 (16Mbit)\n"); |
| 253 | break; |
| 254 | case (ATM_ID_BV1614A & FLASH_TYPEMASK): |
| 255 | printf ("AT49BV1614A (16Mbit)\n"); |
| 256 | break; |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 257 | case (ATM_ID_BV6416 & FLASH_TYPEMASK): |
| 258 | printf ("AT49BV6416 (64Mbit)\n"); |
| 259 | break; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 260 | default: |
| 261 | printf ("Unknown Chip Type\n"); |
| 262 | goto Done; |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | printf (" Size: %ld MB in %d Sectors\n", |
| 267 | info->size >> 20, info->sector_count); |
| 268 | |
| 269 | printf (" Sector Start Addresses:"); |
| 270 | for (i = 0; i < info->sector_count; i++) { |
| 271 | if ((i % 5) == 0) { |
| 272 | printf ("\n "); |
| 273 | } |
| 274 | printf (" %08lX%s", info->start[i], |
| 275 | info->protect[i] ? " (RO)" : " "); |
| 276 | } |
| 277 | printf ("\n"); |
| 278 | |
wdenk | 8b07a11 | 2004-07-10 21:45:47 +0000 | [diff] [blame^] | 279 | Done: ; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /*----------------------------------------------------------------------- |
| 283 | */ |
| 284 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 285 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 286 | { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 287 | ulong result; |
| 288 | int iflag, cflag, prot, sect; |
| 289 | int rc = ERR_OK; |
| 290 | int chip1; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 291 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 292 | /* first look for protection bits */ |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 293 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 294 | if (info->flash_id == FLASH_UNKNOWN) |
| 295 | return ERR_UNKNOWN_FLASH_TYPE; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 296 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 297 | if ((s_first < 0) || (s_first > s_last)) { |
| 298 | return ERR_INVAL; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 299 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 300 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 301 | if ((info->flash_id & FLASH_VENDMASK) != |
| 302 | (ATM_MANUFACT & FLASH_VENDMASK)) { |
| 303 | return ERR_UNKNOWN_FLASH_VENDOR; |
| 304 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 305 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 306 | prot = 0; |
| 307 | for (sect = s_first; sect <= s_last; ++sect) { |
| 308 | if (info->protect[sect]) { |
| 309 | prot++; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 310 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 311 | } |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 312 | if (prot) |
| 313 | return ERR_PROTECTED; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 314 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 315 | /* |
| 316 | * Disable interrupts which might cause a timeout |
| 317 | * here. Remember that our exception vectors are |
| 318 | * at address 0 in the flash, and we don't want a |
| 319 | * (ticker) exception to happen while the flash |
| 320 | * chip is in programming mode. |
| 321 | */ |
| 322 | cflag = icache_status (); |
| 323 | icache_disable (); |
| 324 | iflag = disable_interrupts (); |
| 325 | |
| 326 | /* Start erase on unprotected sectors */ |
| 327 | for (sect = s_first; sect <= s_last && !ctrlc (); sect++) { |
| 328 | printf ("Erasing sector %2d ... ", sect); |
| 329 | |
| 330 | /* arm simple, non interrupt dependent timer */ |
| 331 | reset_timer_masked (); |
| 332 | |
| 333 | if (info->protect[sect] == 0) { /* not protected */ |
| 334 | volatile u16 *addr = (volatile u16 *) (info->start[sect]); |
| 335 | |
| 336 | MEM_FLASH_ADDR1 = CMD_UNLOCK1; |
| 337 | MEM_FLASH_ADDR2 = CMD_UNLOCK2; |
| 338 | MEM_FLASH_ADDR1 = CMD_ERASE_SETUP; |
| 339 | |
| 340 | MEM_FLASH_ADDR1 = CMD_UNLOCK1; |
| 341 | MEM_FLASH_ADDR2 = CMD_UNLOCK2; |
| 342 | *addr = CMD_ERASE_CONFIRM; |
| 343 | |
| 344 | /* wait until flash is ready */ |
| 345 | chip1 = 0; |
| 346 | |
| 347 | do { |
| 348 | result = *addr; |
| 349 | |
| 350 | /* check timeout */ |
| 351 | if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) { |
| 352 | MEM_FLASH_ADDR1 = CMD_READ_ARRAY; |
| 353 | chip1 = TMO; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE) |
| 358 | chip1 = READY; |
| 359 | |
| 360 | } while (!chip1); |
| 361 | |
| 362 | MEM_FLASH_ADDR1 = CMD_READ_ARRAY; |
| 363 | |
| 364 | if (chip1 == ERR) { |
| 365 | rc = ERR_PROG_ERROR; |
| 366 | goto outahere; |
| 367 | } |
| 368 | if (chip1 == TMO) { |
| 369 | rc = ERR_TIMOUT; |
| 370 | goto outahere; |
| 371 | } |
| 372 | |
| 373 | printf ("ok.\n"); |
| 374 | } else { /* it was protected */ |
| 375 | printf ("protected!\n"); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (ctrlc ()) |
| 380 | printf ("User Interrupt!\n"); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 381 | |
| 382 | outahere: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 383 | /* allow flash to settle - wait 10 ms */ |
| 384 | udelay_masked (10000); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 385 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 386 | if (iflag) |
| 387 | enable_interrupts (); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 388 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 389 | if (cflag) |
| 390 | icache_enable (); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 391 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 392 | return rc; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /*----------------------------------------------------------------------- |
| 396 | * Copy memory to flash |
| 397 | */ |
| 398 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 399 | volatile static int write_word (flash_info_t * info, ulong dest, |
| 400 | ulong data) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 401 | { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 402 | volatile u16 *addr = (volatile u16 *) dest; |
| 403 | ulong result; |
| 404 | int rc = ERR_OK; |
| 405 | int cflag, iflag; |
| 406 | int chip1; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 407 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 408 | /* |
| 409 | * Check if Flash is (sufficiently) erased |
| 410 | */ |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 411 | result = *addr; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 412 | if ((result & data) != data) |
| 413 | return ERR_NOT_ERASED; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 414 | |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 415 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 416 | /* |
| 417 | * Disable interrupts which might cause a timeout |
| 418 | * here. Remember that our exception vectors are |
| 419 | * at address 0 in the flash, and we don't want a |
| 420 | * (ticker) exception to happen while the flash |
| 421 | * chip is in programming mode. |
| 422 | */ |
| 423 | cflag = icache_status (); |
| 424 | icache_disable (); |
| 425 | iflag = disable_interrupts (); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 426 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 427 | MEM_FLASH_ADDR1 = CMD_UNLOCK1; |
| 428 | MEM_FLASH_ADDR2 = CMD_UNLOCK2; |
| 429 | MEM_FLASH_ADDR1 = CMD_PROGRAM; |
| 430 | *addr = data; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 431 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 432 | /* arm simple, non interrupt dependent timer */ |
| 433 | reset_timer_masked (); |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 434 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 435 | /* wait until flash is ready */ |
| 436 | chip1 = 0; |
| 437 | do { |
| 438 | result = *addr; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 439 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 440 | /* check timeout */ |
| 441 | if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) { |
| 442 | chip1 = ERR | TMO; |
| 443 | break; |
| 444 | } |
| 445 | if (!chip1 && ((result & 0x80) == (data & 0x80))) |
| 446 | chip1 = READY; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 447 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 448 | } while (!chip1); |
| 449 | |
| 450 | *addr = CMD_READ_ARRAY; |
| 451 | |
| 452 | if (chip1 == ERR || *addr != data) |
| 453 | rc = ERR_PROG_ERROR; |
| 454 | |
| 455 | if (iflag) |
| 456 | enable_interrupts (); |
| 457 | |
| 458 | if (cflag) |
| 459 | icache_enable (); |
| 460 | |
| 461 | return rc; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /*----------------------------------------------------------------------- |
| 465 | * Copy memory to flash. |
| 466 | */ |
| 467 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 468 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 469 | { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 470 | ulong wp, data; |
| 471 | int rc; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 472 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 473 | if (addr & 1) { |
| 474 | printf ("unaligned destination not supported\n"); |
| 475 | return ERR_ALIGN; |
| 476 | }; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 477 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 478 | if ((int) src & 1) { |
| 479 | printf ("unaligned source not supported\n"); |
| 480 | return ERR_ALIGN; |
| 481 | }; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 482 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 483 | wp = addr; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 484 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 485 | while (cnt >= 2) { |
| 486 | data = *((volatile u16 *) src); |
| 487 | if ((rc = write_word (info, wp, data)) != 0) { |
| 488 | return (rc); |
| 489 | } |
| 490 | src += 2; |
| 491 | wp += 2; |
| 492 | cnt -= 2; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 493 | } |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 494 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 495 | if (cnt == 1) { |
| 496 | data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1)) << |
| 497 | 8); |
| 498 | if ((rc = write_word (info, wp, data)) != 0) { |
| 499 | return (rc); |
| 500 | } |
| 501 | src += 1; |
| 502 | wp += 1; |
| 503 | cnt -= 1; |
| 504 | }; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 505 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 506 | return ERR_OK; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 507 | } |