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