]>
Commit | Line | Data |
---|---|---|
c609719b WD |
1 | /* |
2 | * (C) Copyright 2001 | |
3 | * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com. | |
4 | * Based on code by: | |
5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
6 | * | |
1a459660 | 7 | * SPDX-License-Identifier: GPL-2.0+ |
c609719b WD |
8 | */ |
9 | ||
10 | #include <common.h> | |
b36df561 | 11 | #include <asm/ppc4xx.h> |
c609719b WD |
12 | #include <asm/processor.h> |
13 | ||
14 | #include <watchdog.h> | |
15 | ||
75a69acb WD |
16 | /* info for FLASH chips */ |
17 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; | |
c609719b | 18 | |
75a69acb | 19 | /* |
c609719b WD |
20 | * Functions |
21 | */ | |
75a69acb | 22 | static ulong flash_get_size(vu_long *addr, flash_info_t *info); |
c609719b | 23 | static int write_word8(flash_info_t *info, ulong dest, ulong data); |
75a69acb WD |
24 | static int write_word32(flash_info_t *info, ulong dest, ulong data); |
25 | static void flash_get_offsets(ulong base, flash_info_t *info); | |
c609719b | 26 | |
75a69acb | 27 | unsigned long flash_init(void) |
c609719b | 28 | { |
75a69acb WD |
29 | int i; |
30 | unsigned long size_b0, base_b0; | |
fc86bbf6 | 31 | unsigned long size_b1; |
75a69acb WD |
32 | |
33 | /* Init: no FLASHes known */ | |
34 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) | |
35 | flash_info[i].flash_id = FLASH_UNKNOWN; | |
36 | ||
37 | /* Get Size of Boot and Main Flashes */ | |
38 | size_b0 = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, | |
39 | &flash_info[0]); | |
40 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { | |
41 | printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", | |
42 | size_b0, size_b0 << 20); | |
43 | return 0; | |
44 | } | |
45 | size_b1 = | |
46 | flash_get_size((vu_long *) FLASH_BASE1_PRELIM, | |
47 | &flash_info[1]); | |
48 | if (flash_info[1].flash_id == FLASH_UNKNOWN) { | |
49 | printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n", | |
50 | size_b1, size_b1 << 20); | |
51 | return 0; | |
52 | } | |
c609719b | 53 | |
75a69acb WD |
54 | /* Calculate base addresses */ |
55 | base_b0 = -size_b0; | |
c609719b | 56 | |
75a69acb WD |
57 | /* Setup offsets for Boot Flash */ |
58 | flash_get_offsets(base_b0, &flash_info[0]); | |
c609719b | 59 | |
75a69acb WD |
60 | /* Protect board level data */ |
61 | (void) flash_protect(FLAG_PROTECT_SET, | |
62 | base_b0, | |
63 | flash_info[0].start[1] - 1, &flash_info[0]); | |
c609719b | 64 | |
75a69acb WD |
65 | /* Monitor protection ON by default */ |
66 | (void) flash_protect(FLAG_PROTECT_SET, | |
67 | base_b0 + size_b0 - monitor_flash_len, | |
68 | base_b0 + size_b0 - 1, &flash_info[0]); | |
c609719b | 69 | |
75a69acb WD |
70 | /* Protect the FPGA image */ |
71 | (void) flash_protect(FLAG_PROTECT_SET, | |
72 | FLASH_BASE1_PRELIM, | |
73 | FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN - | |
74 | 1, &flash_info[1]); | |
c609719b | 75 | |
75a69acb WD |
76 | /* Protect the default boot image */ |
77 | (void) flash_protect(FLAG_PROTECT_SET, | |
78 | FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN, | |
79 | FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN + | |
80 | 0x600000 - 1, &flash_info[1]); | |
c609719b | 81 | |
75a69acb WD |
82 | /* Setup offsets for Main Flash */ |
83 | flash_get_offsets(FLASH_BASE1_PRELIM, &flash_info[1]); | |
c609719b | 84 | |
75a69acb WD |
85 | return size_b0 + size_b1; |
86 | } | |
c609719b | 87 | |
75a69acb | 88 | static void flash_get_offsets(ulong base, flash_info_t *info) |
c609719b | 89 | { |
75a69acb | 90 | int i; |
c609719b | 91 | |
75a69acb WD |
92 | /* set up sector start address table - FOR BOOT ROM ONLY!!! */ |
93 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) { | |
94 | for (i = 0; i < info->sector_count; i++) | |
95 | info->start[i] = base + (i * 0x00010000); | |
96 | } | |
97 | } /* end flash_get_offsets() */ | |
c609719b | 98 | |
75a69acb | 99 | void flash_print_info(flash_info_t *info) |
c609719b | 100 | { |
75a69acb WD |
101 | int i; |
102 | int k; | |
103 | int size; | |
104 | int erased; | |
105 | volatile unsigned long *flash; | |
106 | ||
107 | if (info->flash_id == FLASH_UNKNOWN) { | |
108 | printf("missing or unknown FLASH type\n"); | |
109 | return; | |
110 | } | |
111 | ||
112 | switch (info->flash_id & FLASH_VENDMASK) { | |
113 | case FLASH_MAN_AMD: | |
114 | printf("1 x AMD "); | |
115 | break; | |
116 | case FLASH_MAN_STM: | |
117 | printf("1 x STM "); | |
118 | break; | |
119 | case FLASH_MAN_INTEL: | |
120 | printf("2 x Intel "); | |
121 | break; | |
122 | default: | |
123 | printf("Unknown Vendor "); | |
124 | } | |
125 | ||
126 | switch (info->flash_id & FLASH_TYPEMASK) { | |
8bde7f77 | 127 | case FLASH_AM040: |
75a69acb WD |
128 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) |
129 | printf("AM29LV040 (4096 Kbit, uniform sector size)\n"); | |
130 | else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM) | |
131 | printf("M29W040B (4096 Kbit, uniform block size)\n"); | |
132 | else | |
133 | printf("UNKNOWN 29x040x (4096 Kbit, uniform sector size)\n"); | |
134 | break; | |
8bde7f77 | 135 | case FLASH_28F320J3A: |
75a69acb WD |
136 | printf("28F320J3A (32 Mbit = 128K x 32)\n"); |
137 | break; | |
8bde7f77 | 138 | case FLASH_28F640J3A: |
75a69acb WD |
139 | printf("28F640J3A (64 Mbit = 128K x 64)\n"); |
140 | break; | |
8bde7f77 | 141 | case FLASH_28F128J3A: |
75a69acb | 142 | printf("28F128J3A (128 Mbit = 128K x 128)\n"); |
8bde7f77 | 143 | break; |
75a69acb WD |
144 | default: |
145 | printf("Unknown Chip Type\n"); | |
146 | } | |
147 | ||
148 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM) { | |
149 | printf(" Size: %ld KB in %d Blocks\n", | |
150 | info->size >> 10, info->sector_count); | |
151 | } else { | |
152 | printf(" Size: %ld KB in %d Sectors\n", | |
153 | info->size >> 10, info->sector_count); | |
8bde7f77 WD |
154 | } |
155 | ||
75a69acb WD |
156 | printf(" Sector Start Addresses:"); |
157 | for (i = 0; i < info->sector_count; ++i) { | |
158 | /* | |
159 | * Check if whole sector is erased | |
160 | */ | |
161 | if (i != (info->sector_count - 1)) | |
162 | size = info->start[i + 1] - info->start[i]; | |
163 | else | |
164 | size = info->start[0] + info->size - info->start[i]; | |
165 | erased = 1; | |
166 | flash = (volatile unsigned long *) info->start[i]; | |
167 | size = size >> 2; /* divide by 4 for longword access */ | |
168 | for (k = 0; k < size; k++) { | |
169 | if (*flash++ != 0xffffffff) { | |
170 | erased = 0; | |
171 | break; | |
172 | } | |
173 | } | |
174 | ||
175 | if ((i % 5) == 0) | |
176 | printf("\n "); | |
177 | printf(" %08lX%s%s", | |
178 | info->start[i], | |
179 | erased ? " E" : " ", | |
180 | info->protect[i] ? "RO " : " "); | |
181 | } | |
182 | printf("\n"); | |
183 | } /* end flash_print_info() */ | |
c609719b WD |
184 | |
185 | /* | |
186 | * The following code cannot be run from FLASH! | |
187 | */ | |
75a69acb | 188 | static ulong flash_get_size(vu_long *addr, flash_info_t *info) |
c609719b | 189 | { |
75a69acb WD |
190 | short i; |
191 | ulong base = (ulong) addr; | |
192 | ||
193 | /* Setup default type */ | |
194 | info->flash_id = FLASH_UNKNOWN; | |
195 | info->sector_count = 0; | |
196 | info->size = 0; | |
197 | ||
198 | /* Test for Boot Flash */ | |
199 | if (base == FLASH_BASE0_PRELIM) { | |
200 | unsigned char value; | |
201 | volatile unsigned char *addr2 = (unsigned char *) addr; | |
202 | ||
203 | /* Write auto select command: read Manufacturer ID */ | |
204 | *(addr2 + 0x555) = 0xaa; | |
205 | *(addr2 + 0x2aa) = 0x55; | |
206 | *(addr2 + 0x555) = 0x90; | |
207 | ||
208 | /* Manufacture ID */ | |
209 | value = *addr2; | |
210 | switch (value) { | |
211 | case (unsigned char) AMD_MANUFACT: | |
212 | info->flash_id = FLASH_MAN_AMD; | |
213 | break; | |
214 | case (unsigned char) STM_MANUFACT: | |
215 | info->flash_id = FLASH_MAN_STM; | |
216 | break; | |
217 | default: | |
218 | *addr2 = 0xf0; /* no or unknown flash */ | |
219 | return 0; | |
220 | } | |
8bde7f77 | 221 | |
75a69acb WD |
222 | /* Device ID */ |
223 | value = *(addr2 + 1); | |
224 | switch (value) { | |
225 | case (unsigned char) AMD_ID_LV040B: | |
226 | case (unsigned char) STM_ID_29W040B: | |
227 | info->flash_id += FLASH_AM040; | |
228 | info->sector_count = 8; | |
229 | info->size = 0x00080000; | |
230 | break; /* => 512Kb */ | |
231 | default: | |
232 | *addr2 = 0xf0; /* => no or unknown flash */ | |
233 | return 0; | |
234 | } | |
235 | } else { /* MAIN Flash */ | |
236 | unsigned long value; | |
237 | volatile unsigned long *addr2 = (unsigned long *) addr; | |
238 | ||
239 | /* Write auto select command: read Manufacturer ID */ | |
240 | *addr2 = 0x90909090; | |
241 | ||
242 | /* Manufacture ID */ | |
243 | value = *addr2; | |
244 | switch (value) { | |
245 | case (unsigned long) INTEL_MANUFACT: | |
246 | info->flash_id = FLASH_MAN_INTEL; | |
247 | break; | |
248 | default: | |
249 | *addr2 = 0xff; /* no or unknown flash */ | |
250 | return 0; | |
251 | } | |
8bde7f77 | 252 | |
75a69acb WD |
253 | /* Device ID - This shit is interleaved... */ |
254 | value = *(addr2 + 1); | |
255 | switch (value) { | |
256 | case (unsigned long) INTEL_ID_28F320J3A: | |
257 | info->flash_id += FLASH_28F320J3A; | |
258 | info->sector_count = 32; | |
259 | info->size = 0x00400000 * 2; | |
260 | break; /* => 2 X 4 MB */ | |
261 | case (unsigned long) INTEL_ID_28F640J3A: | |
262 | info->flash_id += FLASH_28F640J3A; | |
263 | info->sector_count = 64; | |
264 | info->size = 0x00800000 * 2; | |
265 | break; /* => 2 X 8 MB */ | |
266 | case (unsigned long) INTEL_ID_28F128J3A: | |
267 | info->flash_id += FLASH_28F128J3A; | |
268 | info->sector_count = 128; | |
269 | info->size = 0x01000000 * 2; | |
270 | break; /* => 2 X 16 MB */ | |
271 | default: | |
272 | *addr2 = 0xff; /* => no or unknown flash */ | |
273 | } | |
274 | } | |
8bde7f77 | 275 | |
75a69acb WD |
276 | /* Make sure we don't exceed CONFIG_SYS_MAX_FLASH_SECT */ |
277 | if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) { | |
278 | printf("** ERROR: sector count %d > max (%d) **\n", | |
279 | info->sector_count, CONFIG_SYS_MAX_FLASH_SECT); | |
280 | info->sector_count = CONFIG_SYS_MAX_FLASH_SECT; | |
8bde7f77 WD |
281 | } |
282 | ||
75a69acb WD |
283 | /* set up sector start address table */ |
284 | switch (info->flash_id & FLASH_TYPEMASK) { | |
8bde7f77 | 285 | case FLASH_AM040: |
75a69acb WD |
286 | for (i = 0; i < info->sector_count; i++) |
287 | info->start[i] = base + (i * 0x00010000); | |
288 | break; | |
8bde7f77 WD |
289 | case FLASH_28F320J3A: |
290 | case FLASH_28F640J3A: | |
291 | case FLASH_28F128J3A: | |
75a69acb WD |
292 | for (i = 0; i < info->sector_count; i++) |
293 | info->start[i] = base + | |
294 | (i * 0x00020000 * 2); /* 2 Banks */ | |
295 | break; | |
8bde7f77 | 296 | } |
c609719b | 297 | |
75a69acb WD |
298 | /* Test for Boot Flash */ |
299 | if (base == FLASH_BASE0_PRELIM) { | |
300 | volatile unsigned char *addr2; | |
301 | ||
302 | /* check for protected sectors */ | |
303 | for (i = 0; i < info->sector_count; i++) { | |
304 | /* | |
305 | * read sector protection at sector address, | |
306 | * (AX .. A0) = 0x02 | |
307 | * D0 = 1 if protected | |
308 | */ | |
309 | addr2 = (volatile unsigned char *) (info->start[i]); | |
310 | info->protect[i] = *(addr2 + 2) & 1; | |
311 | } | |
312 | ||
313 | /* Restore read mode */ | |
314 | *(unsigned char *) base = 0xF0; /* Reset NORMAL Flash */ | |
315 | } else { /* Main Flash */ | |
316 | volatile unsigned long *addr2; | |
317 | ||
318 | /* check for protected sectors */ | |
319 | for (i = 0; i < info->sector_count; i++) { | |
320 | /* | |
321 | * read sector protection at sector address, | |
322 | * (AX .. A0) = 0x02 | |
323 | * D0 = 1 if protected | |
324 | */ | |
325 | addr2 = (volatile unsigned long *) (info->start[i]); | |
326 | info->protect[i] = *(addr2 + 2) & 0x1; | |
327 | } | |
c609719b | 328 | |
75a69acb WD |
329 | /* Restore read mode */ |
330 | *(unsigned long *) base = 0xFFFFFFFF; /* Reset Flash */ | |
331 | } | |
c609719b | 332 | |
75a69acb WD |
333 | return info->size; |
334 | } /* end flash_get_size() */ | |
c609719b WD |
335 | |
336 | static int wait_for_DQ7(ulong addr, uchar cmp_val, ulong tout) | |
337 | { | |
75a69acb | 338 | int i; |
c609719b | 339 | |
75a69acb | 340 | volatile uchar *vaddr = (uchar *) addr; |
c609719b | 341 | |
75a69acb WD |
342 | /* Loop X times */ |
343 | for (i = 1; i <= (100 * tout); i++) { /* Wait up to tout ms */ | |
344 | udelay(10); | |
345 | /* Pause 10 us */ | |
c609719b | 346 | |
75a69acb WD |
347 | /* Check for completion */ |
348 | if ((vaddr[0] & 0x80) == (cmp_val & 0x80)) | |
349 | return 0; | |
c609719b | 350 | |
75a69acb WD |
351 | /* KEEP THE LUSER HAPPY - Print a dot every 1.1 seconds */ |
352 | if (!(i % 110000)) | |
353 | putc('.'); | |
c609719b | 354 | |
75a69acb WD |
355 | /* Kick the dog if needed */ |
356 | WATCHDOG_RESET(); | |
357 | } | |
c609719b | 358 | |
75a69acb WD |
359 | return 1; |
360 | } /* wait_for_DQ7() */ | |
c609719b WD |
361 | |
362 | static int flash_erase8(flash_info_t *info, int s_first, int s_last) | |
363 | { | |
75a69acb WD |
364 | int tcode, rcode = 0; |
365 | volatile uchar *addr = (uchar *) (info->start[0]); | |
366 | volatile uchar *sector_addr; | |
367 | int flag, prot, sect; | |
368 | ||
369 | /* Validate arguments */ | |
370 | if ((s_first < 0) || (s_first > s_last)) { | |
371 | if (info->flash_id == FLASH_UNKNOWN) | |
372 | printf("- missing\n"); | |
373 | else | |
374 | printf("- no sectors to erase\n"); | |
375 | return 1; | |
376 | } | |
c609719b | 377 | |
75a69acb WD |
378 | /* Check for KNOWN flash type */ |
379 | if (info->flash_id == FLASH_UNKNOWN) { | |
380 | printf("Can't erase unknown flash type - aborted\n"); | |
381 | return 1; | |
382 | } | |
c609719b | 383 | |
75a69acb WD |
384 | /* Check for protected sectors */ |
385 | prot = 0; | |
386 | for (sect = s_first; sect <= s_last; ++sect) { | |
387 | if (info->protect[sect]) | |
388 | prot++; | |
389 | } | |
390 | if (prot) { | |
391 | printf("- Warning: %d protected sectors will not be erased!\n", | |
392 | prot); | |
393 | } else { | |
394 | printf("\n"); | |
395 | } | |
8bde7f77 | 396 | |
75a69acb WD |
397 | /* Start erase on unprotected sectors */ |
398 | for (sect = s_first; sect <= s_last; sect++) { | |
399 | if (info->protect[sect] == 0) { /* not protected */ | |
400 | sector_addr = (uchar *) (info->start[sect]); | |
401 | ||
402 | if ((info->flash_id & FLASH_VENDMASK) == | |
403 | FLASH_MAN_STM) | |
404 | printf("Erasing block %p\n", sector_addr); | |
405 | else | |
406 | printf("Erasing sector %p\n", sector_addr); | |
407 | ||
408 | /* Disable interrupts which might cause timeout */ | |
409 | flag = disable_interrupts(); | |
410 | ||
411 | *(addr + 0x555) = (uchar) 0xAA; | |
412 | *(addr + 0x2aa) = (uchar) 0x55; | |
413 | *(addr + 0x555) = (uchar) 0x80; | |
414 | *(addr + 0x555) = (uchar) 0xAA; | |
415 | *(addr + 0x2aa) = (uchar) 0x55; | |
416 | *sector_addr = (uchar) 0x30; /* sector erase */ | |
417 | ||
418 | /* | |
419 | * Wait for each sector to complete, it's more | |
420 | * reliable. According to AMD Spec, you must | |
421 | * issue all erase commands within a specified | |
422 | * timeout. This has been seen to fail, especially | |
423 | * if printf()s are included (for debug)!! | |
424 | * Takes up to 6 seconds. | |
425 | */ | |
426 | tcode = wait_for_DQ7((ulong) sector_addr, 0x80, 6000); | |
427 | ||
428 | /* re-enable interrupts if necessary */ | |
429 | if (flag) | |
430 | enable_interrupts(); | |
431 | ||
432 | /* Make sure we didn't timeout */ | |
433 | if (tcode) { | |
434 | printf("Timeout\n"); | |
435 | rcode = 1; | |
436 | } | |
437 | } | |
8bde7f77 | 438 | } |
c609719b | 439 | |
75a69acb WD |
440 | /* wait at least 80us - let's wait 1 ms */ |
441 | udelay(1000); | |
c609719b | 442 | |
75a69acb WD |
443 | /* reset to read mode */ |
444 | addr = (uchar *) info->start[0]; | |
445 | *addr = (uchar) 0xF0; /* reset bank */ | |
c609719b | 446 | |
75a69acb WD |
447 | printf(" done\n"); |
448 | return rcode; | |
449 | } /* end flash_erase8() */ | |
c609719b WD |
450 | |
451 | static int flash_erase32(flash_info_t *info, int s_first, int s_last) | |
452 | { | |
75a69acb WD |
453 | int flag, sect; |
454 | ulong start, now, last; | |
455 | int prot = 0; | |
456 | ||
457 | /* Validate arguments */ | |
458 | if ((s_first < 0) || (s_first > s_last)) { | |
459 | if (info->flash_id == FLASH_UNKNOWN) | |
460 | printf("- missing\n"); | |
461 | else | |
462 | printf("- no sectors to erase\n"); | |
463 | return 1; | |
464 | } | |
8bde7f77 | 465 | |
75a69acb WD |
466 | /* Check for KNOWN flash type */ |
467 | if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) { | |
468 | printf("Can erase only Intel flash types - aborted\n"); | |
469 | return 1; | |
470 | } | |
8bde7f77 | 471 | |
75a69acb WD |
472 | /* Check for protected sectors */ |
473 | for (sect = s_first; sect <= s_last; ++sect) { | |
474 | if (info->protect[sect]) | |
475 | prot++; | |
476 | } | |
477 | if (prot) { | |
478 | printf("- Warning: %d protected sectors will not be erased!\n", | |
479 | prot); | |
480 | } else { | |
481 | printf("\n"); | |
482 | } | |
8bde7f77 | 483 | |
75a69acb WD |
484 | start = get_timer(0); |
485 | last = start; | |
486 | /* Start erase on unprotected sectors */ | |
487 | for (sect = s_first; sect <= s_last; sect++) { | |
488 | WATCHDOG_RESET(); | |
489 | if (info->protect[sect] == 0) { /* not protected */ | |
490 | vu_long *addr = (vu_long *) (info->start[sect]); | |
491 | unsigned long status; | |
492 | ||
493 | /* Disable interrupts which might cause a timeout */ | |
494 | flag = disable_interrupts(); | |
495 | ||
496 | *addr = 0x00500050; /* clear status register */ | |
497 | *addr = 0x00200020; /* erase setup */ | |
498 | *addr = 0x00D000D0; /* erase confirm */ | |
499 | ||
500 | /* re-enable interrupts if necessary */ | |
501 | if (flag) | |
502 | enable_interrupts(); | |
503 | ||
504 | /* Wait at least 80us - let's wait 1 ms */ | |
505 | udelay(1000); | |
506 | ||
507 | while (((status = *addr) & 0x00800080) != 0x00800080) { | |
508 | now = get_timer(start); | |
509 | if (now > CONFIG_SYS_FLASH_ERASE_TOUT) { | |
510 | printf("Timeout\n"); | |
511 | /* suspend erase */ | |
512 | *addr = 0x00B000B0; | |
513 | /* reset to read mode */ | |
514 | *addr = 0x00FF00FF; | |
515 | return 1; | |
516 | } | |
517 | ||
518 | /* | |
519 | * show that we're waiting | |
520 | * every second (?) | |
521 | */ | |
522 | if ((now - last) > 990) { | |
523 | putc('.'); | |
524 | last = now; | |
525 | } | |
526 | } | |
527 | *addr = 0x00FF00FF; /* reset to read mode */ | |
8bde7f77 | 528 | } |
8bde7f77 | 529 | } |
75a69acb WD |
530 | printf(" done\n"); |
531 | return 0; | |
532 | } | |
c609719b WD |
533 | |
534 | int flash_erase(flash_info_t *info, int s_first, int s_last) | |
535 | { | |
75a69acb WD |
536 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) |
537 | return flash_erase8(info, s_first, s_last); | |
538 | else | |
539 | return flash_erase32(info, s_first, s_last); | |
540 | } | |
c609719b | 541 | |
75a69acb | 542 | /* |
c609719b WD |
543 | * Copy memory to flash, returns: |
544 | * 0 - OK | |
545 | * 1 - write timeout | |
546 | * 2 - Flash not erased | |
547 | */ | |
75a69acb WD |
548 | static int write_buff8(flash_info_t *info, uchar *src, ulong addr, |
549 | ulong cnt) | |
c609719b | 550 | { |
75a69acb WD |
551 | ulong cp, wp, data; |
552 | ulong start; | |
553 | int i, l, rc; | |
c609719b | 554 | |
75a69acb | 555 | start = get_timer(0); |
c609719b | 556 | |
75a69acb WD |
557 | wp = (addr & ~3); /* get lower word |
558 | aligned address */ | |
c609719b | 559 | |
75a69acb WD |
560 | /* |
561 | * handle unaligned start bytes | |
562 | */ | |
563 | l = addr - wp; | |
564 | if (l != 0) { | |
565 | data = 0; | |
566 | for (i = 0, cp = wp; i < l; ++i, ++cp) | |
567 | data = (data << 8) | (*(uchar *) cp); | |
568 | ||
569 | for (; i < 4 && cnt > 0; ++i) { | |
570 | data = (data << 8) | *src++; | |
571 | --cnt; | |
572 | ++cp; | |
573 | } | |
574 | ||
575 | for (; cnt == 0 && i < 4; ++i, ++cp) | |
576 | data = (data << 8) | (*(uchar *) cp); | |
577 | ||
578 | rc = write_word8(info, wp, data); | |
579 | if (rc != 0) | |
580 | return rc; | |
581 | ||
582 | wp += 4; | |
8bde7f77 WD |
583 | } |
584 | ||
75a69acb WD |
585 | /* |
586 | * handle word aligned part | |
587 | */ | |
588 | while (cnt >= 4) { | |
589 | data = 0; | |
590 | for (i = 0; i < 4; ++i) | |
591 | data = (data << 8) | *src++; | |
592 | ||
593 | rc = write_word8(info, wp, data); | |
594 | if (rc != 0) | |
595 | return rc; | |
596 | ||
597 | wp += 4; | |
598 | cnt -= 4; | |
599 | if (get_timer(start) > 1000) { /* every second */ | |
600 | WATCHDOG_RESET(); | |
601 | putc('.'); | |
602 | start = get_timer(0); | |
603 | } | |
8bde7f77 | 604 | } |
c609719b | 605 | |
75a69acb WD |
606 | if (cnt == 0) |
607 | return 0; | |
608 | ||
609 | /* | |
610 | * handle unaligned tail bytes | |
611 | */ | |
8bde7f77 | 612 | data = 0; |
75a69acb WD |
613 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
614 | data = (data << 8) | *src++; | |
615 | --cnt; | |
616 | } | |
617 | ||
618 | for (; i < 4; ++i, ++cp) | |
619 | data = (data << 8) | (*(uchar *) cp); | |
620 | ||
621 | return write_word8(info, wp, data); | |
622 | } | |
c609719b WD |
623 | |
624 | #define FLASH_WIDTH 4 /* flash bus width in bytes */ | |
75a69acb WD |
625 | static int write_buff32(flash_info_t *info, uchar *src, ulong addr, |
626 | ulong cnt) | |
c609719b WD |
627 | { |
628 | ulong cp, wp, data; | |
629 | int i, l, rc; | |
630 | ulong start; | |
631 | ||
75a69acb | 632 | start = get_timer(0); |
c609719b | 633 | |
75a69acb | 634 | if (info->flash_id == FLASH_UNKNOWN) |
c609719b | 635 | return 4; |
c609719b | 636 | |
75a69acb WD |
637 | /* get lower FLASH_WIDTH aligned address */ |
638 | wp = (addr & ~(FLASH_WIDTH - 1)); | |
c609719b WD |
639 | |
640 | /* | |
641 | * handle unaligned start bytes | |
642 | */ | |
643 | if ((l = addr - wp) != 0) { | |
644 | data = 0; | |
75a69acb WD |
645 | for (i = 0, cp = wp; i < l; ++i, ++cp) |
646 | data = (data << 8) | (*(uchar *) cp); | |
647 | ||
648 | for (; i < FLASH_WIDTH && cnt > 0; ++i) { | |
c609719b WD |
649 | data = (data << 8) | *src++; |
650 | --cnt; | |
651 | ++cp; | |
652 | } | |
c609719b | 653 | |
75a69acb WD |
654 | for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp) |
655 | data = (data << 8) | (*(uchar *) cp); | |
656 | ||
657 | rc = write_word32(info, wp, data); | |
658 | if (rc != 0) | |
659 | return rc; | |
660 | ||
c609719b WD |
661 | wp += FLASH_WIDTH; |
662 | } | |
663 | ||
664 | /* | |
665 | * handle FLASH_WIDTH aligned part | |
666 | */ | |
667 | while (cnt >= FLASH_WIDTH) { | |
668 | data = 0; | |
75a69acb | 669 | for (i = 0; i < FLASH_WIDTH; ++i) |
c609719b | 670 | data = (data << 8) | *src++; |
75a69acb WD |
671 | |
672 | rc = write_word32(info, wp, data); | |
673 | if (rc != 0) | |
674 | return rc; | |
675 | ||
676 | wp += FLASH_WIDTH; | |
c609719b | 677 | cnt -= FLASH_WIDTH; |
75a69acb WD |
678 | if (get_timer(start) > 990) { /* every second */ |
679 | putc('.'); | |
c609719b WD |
680 | start = get_timer(0); |
681 | } | |
682 | } | |
683 | ||
75a69acb WD |
684 | if (cnt == 0) |
685 | return 0; | |
c609719b WD |
686 | |
687 | /* | |
688 | * handle unaligned tail bytes | |
689 | */ | |
690 | data = 0; | |
75a69acb | 691 | for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) { |
c609719b WD |
692 | data = (data << 8) | *src++; |
693 | --cnt; | |
694 | } | |
c609719b | 695 | |
75a69acb WD |
696 | for (; i < FLASH_WIDTH; ++i, ++cp) |
697 | data = (data << 8) | (*(uchar *) cp); | |
698 | ||
699 | return write_word32(info, wp, data); | |
700 | } | |
c609719b WD |
701 | |
702 | int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) | |
703 | { | |
75a69acb | 704 | int retval; |
c609719b | 705 | |
75a69acb WD |
706 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) |
707 | retval = write_buff8(info, src, addr, cnt); | |
708 | else | |
709 | retval = write_buff32(info, src, addr, cnt); | |
c609719b | 710 | |
75a69acb WD |
711 | return retval; |
712 | } | |
c609719b | 713 | |
75a69acb | 714 | /* |
c609719b WD |
715 | * Write a word to Flash, returns: |
716 | * 0 - OK | |
717 | * 1 - write timeout | |
718 | * 2 - Flash not erased | |
719 | */ | |
720 | ||
721 | static int write_word8(flash_info_t *info, ulong dest, ulong data) | |
722 | { | |
75a69acb WD |
723 | volatile uchar *addr2 = (uchar *) (info->start[0]); |
724 | volatile uchar *dest2 = (uchar *) dest; | |
725 | volatile uchar *data2 = (uchar *) &data; | |
726 | int flag; | |
727 | int i, tcode, rcode = 0; | |
c609719b | 728 | |
75a69acb WD |
729 | /* Check if Flash is (sufficently) erased */ |
730 | if ((*((volatile uchar *)dest) & (uchar)data) != (uchar)data) | |
731 | return 2; | |
c609719b | 732 | |
75a69acb WD |
733 | for (i = 0; i < (4 / sizeof(uchar)); i++) { |
734 | /* Disable interrupts which might cause a timeout here */ | |
735 | flag = disable_interrupts(); | |
c609719b | 736 | |
75a69acb WD |
737 | *(addr2 + 0x555) = (uchar) 0xAA; |
738 | *(addr2 + 0x2aa) = (uchar) 0x55; | |
739 | *(addr2 + 0x555) = (uchar) 0xA0; | |
c609719b | 740 | |
75a69acb WD |
741 | dest2[i] = data2[i]; |
742 | ||
743 | /* Wait for write to complete, up to 1ms */ | |
744 | tcode = wait_for_DQ7((ulong) &dest2[i], data2[i], 1); | |
c609719b | 745 | |
75a69acb WD |
746 | /* re-enable interrupts if necessary */ |
747 | if (flag) | |
748 | enable_interrupts(); | |
749 | ||
750 | /* Make sure we didn't timeout */ | |
751 | if (tcode) | |
752 | rcode = 1; | |
8bde7f77 | 753 | } |
c609719b | 754 | |
75a69acb WD |
755 | return rcode; |
756 | } | |
c609719b WD |
757 | |
758 | static int write_word32(flash_info_t *info, ulong dest, ulong data) | |
759 | { | |
75a69acb WD |
760 | vu_long *addr = (vu_long *) dest; |
761 | ulong status; | |
762 | ulong start; | |
763 | int flag; | |
764 | ||
765 | /* Check if Flash is (sufficiently) erased */ | |
766 | if ((*addr & data) != data) | |
767 | return 2; | |
c609719b | 768 | |
75a69acb WD |
769 | /* Disable interrupts which might cause a timeout here */ |
770 | flag = disable_interrupts(); | |
c609719b | 771 | |
75a69acb WD |
772 | *addr = 0x00400040; /* write setup */ |
773 | *addr = data; | |
c609719b | 774 | |
75a69acb WD |
775 | /* re-enable interrupts if necessary */ |
776 | if (flag) | |
777 | enable_interrupts(); | |
c609719b | 778 | |
75a69acb | 779 | start = get_timer(0); |
c609719b | 780 | |
75a69acb WD |
781 | while (((status = *addr) & 0x00800080) != 0x00800080) { |
782 | WATCHDOG_RESET(); | |
783 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) { | |
784 | *addr = 0x00FF00FF; /* restore read mode */ | |
785 | return 1; | |
786 | } | |
8bde7f77 | 787 | } |
c609719b | 788 | |
75a69acb | 789 | *addr = 0x00FF00FF; /* restore read mode */ |
c609719b | 790 | |
75a69acb WD |
791 | return 0; |
792 | } | |
c609719b WD |
793 | |
794 | static int _flash_protect(flash_info_t *info, long sector) | |
795 | { | |
75a69acb WD |
796 | int i; |
797 | int flag; | |
798 | ulong status; | |
799 | int rcode = 0; | |
800 | volatile long *addr = (long *)sector; | |
c609719b | 801 | |
75a69acb | 802 | switch (info->flash_id & FLASH_TYPEMASK) { |
8bde7f77 WD |
803 | case FLASH_28F320J3A: |
804 | case FLASH_28F640J3A: | |
805 | case FLASH_28F128J3A: | |
75a69acb WD |
806 | /* Disable interrupts which might cause Flash to timeout */ |
807 | flag = disable_interrupts(); | |
808 | ||
809 | /* Issue command */ | |
810 | *addr = 0x00500050L; /* Clear the status register */ | |
811 | *addr = 0x00600060L; /* Set lock bit setup */ | |
812 | *addr = 0x00010001L; /* Set lock bit confirm */ | |
813 | ||
814 | /* Wait for command completion */ | |
815 | for (i = 0; i < 10; i++) { /* 75us timeout, wait 100us */ | |
816 | udelay(10); | |
817 | if ((*addr & 0x00800080L) == 0x00800080L) | |
818 | break; | |
819 | } | |
8bde7f77 | 820 | |
75a69acb WD |
821 | /* Not successful? */ |
822 | status = *addr; | |
823 | if (status != 0x00800080L) { | |
824 | printf("Protect %x sector failed: %x\n", | |
825 | (uint) sector, (uint) status); | |
826 | rcode = 1; | |
827 | } | |
8bde7f77 | 828 | |
75a69acb WD |
829 | /* Restore read mode */ |
830 | *addr = 0x00ff00ffL; | |
831 | ||
832 | /* re-enable interrupts if necessary */ | |
833 | if (flag) | |
834 | enable_interrupts(); | |
8bde7f77 | 835 | |
75a69acb WD |
836 | break; |
837 | case FLASH_AM040: /* No soft sector protection */ | |
838 | break; | |
839 | } | |
c609719b | 840 | |
75a69acb WD |
841 | /* Turn protection on for this sector */ |
842 | for (i = 0; i < info->sector_count; i++) { | |
843 | if (info->start[i] == sector) { | |
844 | info->protect[i] = 1; | |
845 | break; | |
846 | } | |
8bde7f77 | 847 | } |
c609719b | 848 | |
75a69acb WD |
849 | return rcode; |
850 | } | |
c609719b WD |
851 | |
852 | static int _flash_unprotect(flash_info_t *info, long sector) | |
853 | { | |
75a69acb WD |
854 | int i; |
855 | int flag; | |
856 | ulong status; | |
857 | int rcode = 0; | |
858 | volatile long *addr = (long *) sector; | |
c609719b | 859 | |
75a69acb | 860 | switch (info->flash_id & FLASH_TYPEMASK) { |
8bde7f77 WD |
861 | case FLASH_28F320J3A: |
862 | case FLASH_28F640J3A: | |
863 | case FLASH_28F128J3A: | |
75a69acb WD |
864 | /* Disable interrupts which might cause Flash to timeout */ |
865 | flag = disable_interrupts(); | |
866 | ||
867 | *addr = 0x00500050L; /* Clear the status register */ | |
868 | *addr = 0x00600060L; /* Clear lock bit setup */ | |
869 | *addr = 0x00D000D0L; /* Clear lock bit confirm */ | |
870 | ||
871 | /* Wait for command completion */ | |
872 | for (i = 0; i < 80; i++) { /* 700ms timeout, wait 800 */ | |
873 | udelay(10000); /* Delay 10ms */ | |
874 | if ((*addr & 0x00800080L) == 0x00800080L) | |
875 | break; | |
876 | } | |
8bde7f77 | 877 | |
75a69acb WD |
878 | /* Not successful? */ |
879 | status = *addr; | |
880 | if (status != 0x00800080L) { | |
881 | printf("Un-protect %x sector failed: %x\n", | |
882 | (uint) sector, (uint) status); | |
883 | *addr = 0x00ff00ffL; | |
884 | rcode = 1; | |
885 | } | |
8bde7f77 | 886 | |
75a69acb WD |
887 | /* restore read mode */ |
888 | *addr = 0x00ff00ffL; | |
8bde7f77 | 889 | |
75a69acb WD |
890 | /* re-enable interrupts if necessary */ |
891 | if (flag) | |
892 | enable_interrupts(); | |
c609719b | 893 | |
75a69acb WD |
894 | break; |
895 | case FLASH_AM040: /* No soft sector protection */ | |
896 | break; | |
8bde7f77 | 897 | } |
c609719b | 898 | |
75a69acb WD |
899 | /* |
900 | * Fix Intel's little red wagon. Reprotect | |
901 | * sectors that were protected before we undid | |
902 | * protection on a specific sector. | |
903 | */ | |
904 | for (i = 0; i < info->sector_count; i++) { | |
905 | if (info->start[i] != sector) { | |
906 | if (info->protect[i]) { | |
907 | if (_flash_protect(info, info->start[i])) | |
908 | rcode = 1; | |
909 | } | |
910 | } else /* Turn protection off for this sector */ | |
911 | info->protect[i] = 0; | |
912 | } | |
c609719b | 913 | |
75a69acb WD |
914 | return rcode; |
915 | } | |
c609719b WD |
916 | |
917 | int flash_real_protect(flash_info_t *info, long sector, int prot) | |
918 | { | |
75a69acb | 919 | int rcode; |
c609719b | 920 | |
75a69acb WD |
921 | if (prot) |
922 | rcode = _flash_protect(info, info->start[sector]); | |
923 | else | |
924 | rcode = _flash_unprotect(info, info->start[sector]); | |
c609719b | 925 | |
75a69acb WD |
926 | return rcode; |
927 | } |