wdenk | f9087a3 | 2002-11-03 00:30:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Marius Groeger <mgroeger@sysgo.de> |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | |
| 30 | #define FLASH_BANK_SIZE 0x02000000 |
| 31 | #define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */ |
| 32 | |
| 33 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; |
| 34 | |
| 35 | |
| 36 | /*----------------------------------------------------------------------- |
| 37 | */ |
| 38 | |
| 39 | ulong flash_init(void) |
| 40 | { |
| 41 | int i, j; |
| 42 | ulong size = 0; |
| 43 | |
| 44 | for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) |
| 45 | { |
| 46 | ulong flashbase = 0; |
| 47 | flash_info[i].flash_id = |
| 48 | (INTEL_MANUFACT & FLASH_VENDMASK) | |
| 49 | (INTEL_ID_28F128J3 & FLASH_TYPEMASK); |
| 50 | flash_info[i].size = FLASH_BANK_SIZE; |
| 51 | flash_info[i].sector_count = CFG_MAX_FLASH_SECT; |
| 52 | memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT); |
| 53 | |
| 54 | switch (i) |
| 55 | { |
| 56 | case 0: |
| 57 | flashbase = PHYS_FLASH_1; |
| 58 | break; |
| 59 | default: |
| 60 | panic("configured to many flash banks!\n"); |
| 61 | break; |
| 62 | } |
| 63 | for (j = 0; j < flash_info[i].sector_count; j++) |
| 64 | { |
| 65 | flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE; |
| 66 | } |
| 67 | size += flash_info[i].size; |
| 68 | } |
| 69 | |
| 70 | /* Protect monitor and environment sectors |
| 71 | */ |
| 72 | flash_protect(FLAG_PROTECT_SET, |
| 73 | CFG_FLASH_BASE, |
| 74 | CFG_FLASH_BASE + _armboot_end_data - _armboot_start, |
| 75 | &flash_info[0]); |
| 76 | |
| 77 | flash_protect(FLAG_PROTECT_SET, |
| 78 | CFG_ENV_ADDR, |
| 79 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, |
| 80 | &flash_info[0]); |
| 81 | |
| 82 | return size; |
| 83 | } |
| 84 | |
| 85 | /*----------------------------------------------------------------------- |
| 86 | */ |
| 87 | void flash_print_info (flash_info_t *info) |
| 88 | { |
| 89 | int i, j; |
| 90 | |
| 91 | for (j=0; j<CFG_MAX_FLASH_BANKS; j++) |
| 92 | { |
| 93 | switch (info->flash_id & FLASH_VENDMASK) |
| 94 | { |
| 95 | case (INTEL_MANUFACT & FLASH_VENDMASK): |
| 96 | printf("Intel: "); |
| 97 | break; |
| 98 | default: |
| 99 | printf("Unknown Vendor "); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | switch (info->flash_id & FLASH_TYPEMASK) |
| 104 | { |
| 105 | case (INTEL_ID_28F128J3 & FLASH_TYPEMASK): |
| 106 | printf("28F128J3 (128Mbit)\n"); |
| 107 | break; |
| 108 | default: |
| 109 | printf("Unknown Chip Type\n"); |
| 110 | goto Done; |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | printf(" Size: %ld MB in %d Sectors\n", |
| 115 | info->size >> 20, info->sector_count); |
| 116 | |
| 117 | printf(" Sector Start Addresses:"); |
| 118 | for (i = 0; i < info->sector_count; i++) |
| 119 | { |
| 120 | if ((i % 5) == 0) |
| 121 | { |
| 122 | printf ("\n "); |
| 123 | } |
| 124 | printf (" %08lX%s", info->start[i], |
| 125 | info->protect[i] ? " (RO)" : " "); |
| 126 | } |
| 127 | printf ("\n"); |
| 128 | info++; |
| 129 | } |
| 130 | |
| 131 | Done: |
| 132 | } |
| 133 | |
| 134 | /*----------------------------------------------------------------------- |
| 135 | */ |
| 136 | |
| 137 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 138 | { |
| 139 | int flag, prot, sect; |
| 140 | int rc = ERR_OK; |
| 141 | |
| 142 | if (info->flash_id == FLASH_UNKNOWN) |
| 143 | return ERR_UNKNOWN_FLASH_TYPE; |
| 144 | |
| 145 | if ((s_first < 0) || (s_first > s_last)) { |
| 146 | return ERR_INVAL; |
| 147 | } |
| 148 | |
| 149 | if ((info->flash_id & FLASH_VENDMASK) != |
| 150 | (INTEL_MANUFACT & FLASH_VENDMASK)) { |
| 151 | return ERR_UNKNOWN_FLASH_VENDOR; |
| 152 | } |
| 153 | |
| 154 | prot = 0; |
| 155 | for (sect=s_first; sect<=s_last; ++sect) { |
| 156 | if (info->protect[sect]) { |
| 157 | prot++; |
| 158 | } |
| 159 | } |
| 160 | if (prot) |
| 161 | return ERR_PROTECTED; |
| 162 | |
| 163 | /* |
| 164 | * Disable interrupts which might cause a timeout |
| 165 | * here. Remember that our exception vectors are |
| 166 | * at address 0 in the flash, and we don't want a |
| 167 | * (ticker) exception to happen while the flash |
| 168 | * chip is in programming mode. |
| 169 | */ |
| 170 | flag = disable_interrupts(); |
| 171 | |
| 172 | /* Start erase on unprotected sectors */ |
| 173 | for (sect = s_first; sect<=s_last && !ctrlc(); sect++) { |
| 174 | |
| 175 | printf("Erasing sector %2d ... ", sect); |
| 176 | |
| 177 | /* arm simple, non interrupt dependent timer */ |
| 178 | reset_timer_masked(); |
| 179 | |
| 180 | if (info->protect[sect] == 0) { /* not protected */ |
| 181 | /* vushort *addr = (vushort *)(info->start[sect]); */ |
| 182 | ushort *addr = (ushort *)(info->start[sect]); |
| 183 | |
| 184 | *addr = 0x20; /* erase setup */ |
| 185 | *addr = 0xD0; /* erase confirm */ |
| 186 | |
| 187 | while ((*addr & 0x80) != 0x80) { |
| 188 | if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) { |
| 189 | *addr = 0xB0; /* suspend erase */ |
| 190 | *addr = 0xFF; /* reset to read mode */ |
| 191 | rc = ERR_TIMOUT; |
| 192 | goto outahere; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* clear status register command */ |
| 197 | *addr = 0x50; |
| 198 | /* reset to read mode */ |
| 199 | *addr = 0xFF; |
| 200 | } |
| 201 | printf("ok.\n"); |
| 202 | } |
| 203 | if (ctrlc()) |
| 204 | printf("User Interrupt!\n"); |
| 205 | |
| 206 | outahere: |
| 207 | |
| 208 | /* allow flash to settle - wait 10 ms */ |
| 209 | udelay_masked(10000); |
| 210 | |
| 211 | if (flag) |
| 212 | enable_interrupts(); |
| 213 | |
| 214 | return rc; |
| 215 | } |
| 216 | |
| 217 | /*----------------------------------------------------------------------- |
| 218 | * Copy memory to flash |
| 219 | */ |
| 220 | |
| 221 | static int write_word (flash_info_t *info, ulong dest, ushort data) |
| 222 | { |
| 223 | /* vushort *addr = (vushort *)dest, val; */ |
| 224 | ushort *addr = (ushort *)dest, val; |
| 225 | int rc = ERR_OK; |
| 226 | int flag; |
| 227 | |
| 228 | /* Check if Flash is (sufficiently) erased |
| 229 | */ |
| 230 | if ((*addr & data) != data) |
| 231 | return ERR_NOT_ERASED; |
| 232 | |
| 233 | /* |
| 234 | * Disable interrupts which might cause a timeout |
| 235 | * here. Remember that our exception vectors are |
| 236 | * at address 0 in the flash, and we don't want a |
| 237 | * (ticker) exception to happen while the flash |
| 238 | * chip is in programming mode. |
| 239 | */ |
| 240 | flag = disable_interrupts(); |
| 241 | |
| 242 | /* clear status register command */ |
| 243 | *addr = 0x50; |
| 244 | |
| 245 | /* program set-up command */ |
| 246 | *addr = 0x40; |
| 247 | |
| 248 | /* latch address/data */ |
| 249 | *addr = data; |
| 250 | |
| 251 | /* arm simple, non interrupt dependent timer */ |
| 252 | reset_timer_masked(); |
| 253 | |
| 254 | /* wait while polling the status register */ |
| 255 | while(((val = *addr) & 0x80) != 0x80) |
| 256 | { |
| 257 | if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) { |
| 258 | rc = ERR_TIMOUT; |
| 259 | /* suspend program command */ |
| 260 | *addr = 0xB0; |
| 261 | goto outahere; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if(val & 0x1A) { /* check for error */ |
| 266 | printf("\nFlash write error %02x at address %08lx\n", |
| 267 | (int)val, (unsigned long)dest); |
| 268 | if(val & (1<<3)) { |
| 269 | printf("Voltage range error.\n"); |
| 270 | rc = ERR_PROG_ERROR; |
| 271 | goto outahere; |
| 272 | } |
| 273 | if(val & (1<<1)) { |
| 274 | printf("Device protect error.\n"); |
| 275 | rc = ERR_PROTECTED; |
| 276 | goto outahere; |
| 277 | } |
| 278 | if(val & (1<<4)) { |
| 279 | printf("Programming error.\n"); |
| 280 | rc = ERR_PROG_ERROR; |
| 281 | goto outahere; |
| 282 | } |
| 283 | rc = ERR_PROG_ERROR; |
| 284 | goto outahere; |
| 285 | } |
| 286 | |
| 287 | outahere: |
| 288 | /* read array command */ |
| 289 | *addr = 0xFF; |
| 290 | |
| 291 | if (flag) |
| 292 | enable_interrupts(); |
| 293 | |
| 294 | return rc; |
| 295 | } |
| 296 | |
| 297 | /*----------------------------------------------------------------------- |
| 298 | * Copy memory to flash. |
| 299 | */ |
| 300 | |
| 301 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 302 | { |
| 303 | ulong cp, wp; |
| 304 | ushort data; |
| 305 | int l; |
| 306 | int i, rc; |
| 307 | |
| 308 | wp = (addr & ~1); /* get lower word aligned address */ |
| 309 | |
| 310 | /* |
| 311 | * handle unaligned start bytes |
| 312 | */ |
| 313 | if ((l = addr - wp) != 0) { |
| 314 | data = 0; |
| 315 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
| 316 | data = (data >> 8) | (*(uchar *)cp << 8); |
| 317 | } |
| 318 | for (; i<2 && cnt>0; ++i) { |
| 319 | data = (data >> 8) | (*src++ << 8); |
| 320 | --cnt; |
| 321 | ++cp; |
| 322 | } |
| 323 | for (; cnt==0 && i<2; ++i, ++cp) { |
| 324 | data = (data >> 8) | (*(uchar *)cp << 8); |
| 325 | } |
| 326 | |
| 327 | if ((rc = write_word(info, wp, data)) != 0) { |
| 328 | return (rc); |
| 329 | } |
| 330 | wp += 2; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * handle word aligned part |
| 335 | */ |
| 336 | while (cnt >= 2) { |
| 337 | /* data = *((vushort*)src); */ |
| 338 | data = *((ushort*)src); |
| 339 | if ((rc = write_word(info, wp, data)) != 0) { |
| 340 | return (rc); |
| 341 | } |
| 342 | src += 2; |
| 343 | wp += 2; |
| 344 | cnt -= 2; |
| 345 | } |
| 346 | |
| 347 | if (cnt == 0) { |
| 348 | return ERR_OK; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * handle unaligned tail bytes |
| 353 | */ |
| 354 | data = 0; |
| 355 | for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) { |
| 356 | data = (data >> 8) | (*src++ << 8); |
| 357 | --cnt; |
| 358 | } |
| 359 | for (; i<2; ++i, ++cp) { |
| 360 | data = (data >> 8) | (*(uchar *)cp << 8); |
| 361 | } |
| 362 | |
| 363 | return write_word(info, wp, data); |
| 364 | } |