blob: 4b2300b2ff955ddb339c5675a8a987bfc3892fb2 [file] [log] [blame]
Stefan Roese8a316c92005-08-01 16:49:12 +02001/*
2 * (C) Copyright 2004-2005
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
6 * Add support for Am29F016D and dynamic switch setting.
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese8a316c92005-08-01 16:49:12 +02009 */
10
11/*
12 * Modified 4/5/2001
13 * Wait for completion of each sector erase command issued
14 * 4/5/2001
15 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
16 */
17
18#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020019#include <asm/ppc4xx.h>
Stefan Roese8a316c92005-08-01 16:49:12 +020020#include <asm/processor.h>
21
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020022flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
Stefan Roese8a316c92005-08-01 16:49:12 +020023
24/*-----------------------------------------------------------------------
25 * Functions
26 */
27static int write_word(flash_info_t * info, ulong dest, ulong data);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Stefan Roese17f50f222005-08-04 17:09:16 +020029static int write_word_1(flash_info_t * info, ulong dest, ulong data);
30static int write_word_2(flash_info_t * info, ulong dest, ulong data);
31static int flash_erase_1(flash_info_t * info, int s_first, int s_last);
32static int flash_erase_2(flash_info_t * info, int s_first, int s_last);
33static ulong flash_get_size_1(vu_long * addr, flash_info_t * info);
34static ulong flash_get_size_2(vu_long * addr, flash_info_t * info);
35#endif
Stefan Roese8a316c92005-08-01 16:49:12 +020036
37void flash_print_info(flash_info_t * info)
38{
39 int i;
40 int k;
41 int size;
42 int erased;
43 volatile unsigned long *flash;
44
45 if (info->flash_id == FLASH_UNKNOWN) {
46 printf("missing or unknown FLASH type\n");
47 return;
48 }
49
50 switch (info->flash_id & FLASH_VENDMASK) {
51 case FLASH_MAN_AMD:
52 printf("AMD ");
53 break;
54 case FLASH_MAN_STM:
55 printf("STM ");
56 break;
57 case FLASH_MAN_FUJ:
58 printf("FUJITSU ");
59 break;
60 case FLASH_MAN_SST:
61 printf("SST ");
62 break;
Wolfgang Denk16850912006-08-27 18:10:01 +020063 case FLASH_MAN_MX:
Stefan Roese9ede3cc2006-08-07 14:31:21 +020064 printf ("MACRONIX ");
65 break;
Stefan Roese8a316c92005-08-01 16:49:12 +020066 default:
67 printf("Unknown Vendor ");
68 break;
69 }
70
71 switch (info->flash_id & FLASH_TYPEMASK) {
72 case FLASH_AM040:
73 printf("AM29F040 (512 Kbit, uniform sector size)\n");
74 break;
75 case FLASH_AM400B:
76 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
77 break;
78 case FLASH_AM400T:
79 printf("AM29LV400T (4 Mbit, top boot sector)\n");
80 break;
81 case FLASH_AM800B:
82 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
83 break;
84 case FLASH_AM800T:
85 printf("AM29LV800T (8 Mbit, top boot sector)\n");
86 break;
87 case FLASH_AMD016:
88 printf("AM29F016D (16 Mbit, uniform sector size)\n");
89 break;
90 case FLASH_AM160B:
91 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
92 break;
93 case FLASH_AM160T:
94 printf("AM29LV160T (16 Mbit, top boot sector)\n");
95 break;
96 case FLASH_AM320B:
97 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
98 break;
99 case FLASH_AM320T:
100 printf("AM29LV320T (32 Mbit, top boot sector)\n");
101 break;
102 case FLASH_AM033C:
103 printf("AM29LV033C (32 Mbit, top boot sector)\n");
104 break;
105 case FLASH_SST800A:
106 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
107 break;
108 case FLASH_SST160A:
109 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
110 break;
Stefan Roese17f50f222005-08-04 17:09:16 +0200111 case FLASH_STMW320DT:
112 printf ("M29W320DT (32 M, top sector)\n");
113 break;
Stefan Roese9ede3cc2006-08-07 14:31:21 +0200114 case FLASH_MXLV320T:
115 printf ("MXLV320T (32 Mbit, top sector)\n");
116 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200117 default:
118 printf("Unknown Chip Type\n");
119 break;
120 }
121
122 printf(" Size: %ld KB in %d Sectors\n",
123 info->size >> 10, info->sector_count);
124
125 printf(" Sector Start Addresses:");
126 for (i = 0; i < info->sector_count; ++i) {
127 /*
128 * Check if whole sector is erased
129 */
130 if (i != (info->sector_count - 1))
131 size = info->start[i + 1] - info->start[i];
132 else
133 size = info->start[0] + info->size - info->start[i];
134 erased = 1;
135 flash = (volatile unsigned long *)info->start[i];
136 size = size >> 2; /* divide by 4 for longword access */
137 for (k = 0; k < size; k++) {
138 if (*flash++ != 0xffffffff) {
139 erased = 0;
140 break;
141 }
142 }
143
144 if ((i % 5) == 0)
145 printf("\n ");
146 printf(" %08lX%s%s",
147 info->start[i],
148 erased ? " E" : " ", info->protect[i] ? "RO " : " ");
149 }
150 printf("\n");
151 return;
152}
153
154
155/*
156 * The following code cannot be run from FLASH!
157 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200158#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Stefan Roese8a316c92005-08-01 16:49:12 +0200159static ulong flash_get_size(vu_long * addr, flash_info_t * info)
160{
Stefan Roese17f50f222005-08-04 17:09:16 +0200161 /* bit 0 used for big flash marking */
162 if ((ulong)addr & 0x1) {
163 return flash_get_size_2((vu_long *)((ulong)addr & 0xfffffffe), info);
164 } else {
165 return flash_get_size_1(addr, info);
166 }
167}
168
169static ulong flash_get_size_1(vu_long * addr, flash_info_t * info)
170#else
171static ulong flash_get_size(vu_long * addr, flash_info_t * info)
172#endif
173{
Stefan Roese8a316c92005-08-01 16:49:12 +0200174 short i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200175 CONFIG_SYS_FLASH_WORD_SIZE value;
Stefan Roese8a316c92005-08-01 16:49:12 +0200176 ulong base = (ulong) addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200177 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
Stefan Roese8a316c92005-08-01 16:49:12 +0200178
179 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
180
181 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200182 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
183 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
184 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
Stefan Roese8a316c92005-08-01 16:49:12 +0200185 udelay(1000);
186
187 value = addr2[0];
188 DEBUGF("FLASH MANUFACT: %x\n", value);
189
190 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200191 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
Stefan Roese8a316c92005-08-01 16:49:12 +0200192 info->flash_id = FLASH_MAN_AMD;
193 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200194 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
Stefan Roese8a316c92005-08-01 16:49:12 +0200195 info->flash_id = FLASH_MAN_FUJ;
196 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200197 case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
Stefan Roese8a316c92005-08-01 16:49:12 +0200198 info->flash_id = FLASH_MAN_SST;
199 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200200 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
Stefan Roese8a316c92005-08-01 16:49:12 +0200201 info->flash_id = FLASH_MAN_STM;
202 break;
203 default:
204 info->flash_id = FLASH_UNKNOWN;
205 info->sector_count = 0;
206 info->size = 0;
207 return (0); /* no or unknown flash */
208 }
209
Wolfgang Denk16850912006-08-27 18:10:01 +0200210 value = addr2[1]; /* device ID */
Stefan Roese8a316c92005-08-01 16:49:12 +0200211 DEBUGF("\nFLASH DEVICEID: %x\n", value);
212
213 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200214 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
Stefan Roese8a316c92005-08-01 16:49:12 +0200215 info->flash_id += FLASH_AM040;
216 info->sector_count = 8;
Wolfgang Denk16850912006-08-27 18:10:01 +0200217 info->size = 0x0080000; /* => 512 KiB */
Stefan Roese8a316c92005-08-01 16:49:12 +0200218 break;
219
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200220 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
Stefan Roese8a316c92005-08-01 16:49:12 +0200221 info->flash_id += FLASH_AM040;
222 info->sector_count = 8;
Wolfgang Denk16850912006-08-27 18:10:01 +0200223 info->size = 0x0080000; /* => 512 KiB */
Stefan Roese8a316c92005-08-01 16:49:12 +0200224 break;
225
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200226 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
Stefan Roese8a316c92005-08-01 16:49:12 +0200227 info->flash_id += FLASH_AM040;
228 info->sector_count = 8;
Wolfgang Denk16850912006-08-27 18:10:01 +0200229 info->size = 0x0080000; /* => 512 KiB */
Stefan Roese8a316c92005-08-01 16:49:12 +0200230 break;
231
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200232 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
Stefan Roese8a316c92005-08-01 16:49:12 +0200233 info->flash_id += FLASH_AMD016;
234 info->sector_count = 32;
Wolfgang Denk16850912006-08-27 18:10:01 +0200235 info->size = 0x00200000; /* => 2 MiB */
236 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200237
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200238 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
Stefan Roese8a316c92005-08-01 16:49:12 +0200239 info->flash_id += FLASH_AMDLV033C;
240 info->sector_count = 64;
Wolfgang Denk16850912006-08-27 18:10:01 +0200241 info->size = 0x00400000; /* => 4 MiB */
242 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200243
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200244 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
Stefan Roese8a316c92005-08-01 16:49:12 +0200245 info->flash_id += FLASH_AM400T;
246 info->sector_count = 11;
Wolfgang Denk16850912006-08-27 18:10:01 +0200247 info->size = 0x00080000; /* => 512 KiB */
248 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200249
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200250 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
Stefan Roese8a316c92005-08-01 16:49:12 +0200251 info->flash_id += FLASH_AM400B;
252 info->sector_count = 11;
Wolfgang Denk16850912006-08-27 18:10:01 +0200253 info->size = 0x00080000; /* => 512 KiB */
254 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200255
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200256 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
Stefan Roese8a316c92005-08-01 16:49:12 +0200257 info->flash_id += FLASH_AM800T;
258 info->sector_count = 19;
Wolfgang Denk16850912006-08-27 18:10:01 +0200259 info->size = 0x00100000; /* => 1 MiB */
260 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200261
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200262 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
Stefan Roese8a316c92005-08-01 16:49:12 +0200263 info->flash_id += FLASH_AM800B;
264 info->sector_count = 19;
Wolfgang Denk16850912006-08-27 18:10:01 +0200265 info->size = 0x00100000; /* => 1 MiB */
266 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200267
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200268 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
Stefan Roese8a316c92005-08-01 16:49:12 +0200269 info->flash_id += FLASH_AM160T;
270 info->sector_count = 35;
Wolfgang Denk16850912006-08-27 18:10:01 +0200271 info->size = 0x00200000; /* => 2 MiB */
272 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200273
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200274 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
Stefan Roese8a316c92005-08-01 16:49:12 +0200275 info->flash_id += FLASH_AM160B;
276 info->sector_count = 35;
Wolfgang Denk16850912006-08-27 18:10:01 +0200277 info->size = 0x00200000; /* => 2 MiB */
278 break;
Stefan Roese8a316c92005-08-01 16:49:12 +0200279
280 default:
281 info->flash_id = FLASH_UNKNOWN;
282 return (0); /* => no or unknown flash */
283 }
284
285 /* set up sector start address table */
286 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
287 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
288 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
289 for (i = 0; i < info->sector_count; i++)
290 info->start[i] = base + (i * 0x00010000);
291 } else {
292 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denk16850912006-08-27 18:10:01 +0200293 /* set sector offsets for bottom boot block type */
Stefan Roese8a316c92005-08-01 16:49:12 +0200294 info->start[0] = base + 0x00000000;
295 info->start[1] = base + 0x00004000;
296 info->start[2] = base + 0x00006000;
297 info->start[3] = base + 0x00008000;
298 for (i = 4; i < info->sector_count; i++) {
299 info->start[i] =
300 base + (i * 0x00010000) - 0x00030000;
301 }
302 } else {
Wolfgang Denk16850912006-08-27 18:10:01 +0200303 /* set sector offsets for top boot block type */
Stefan Roese8a316c92005-08-01 16:49:12 +0200304 i = info->sector_count - 1;
305 info->start[i--] = base + info->size - 0x00004000;
306 info->start[i--] = base + info->size - 0x00006000;
307 info->start[i--] = base + info->size - 0x00008000;
308 for (; i >= 0; i--) {
309 info->start[i] = base + i * 0x00010000;
310 }
311 }
312 }
313
314 /* check for protected sectors */
315 for (i = 0; i < info->sector_count; i++) {
316 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
317 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200318 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
Stefan Roese8a316c92005-08-01 16:49:12 +0200319
320 /* For AMD29033C flash we need to resend the command of *
321 * reading flash protection for upper 8 Mb of flash */
322 if (i == 32) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200323 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
324 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
325 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
Stefan Roese8a316c92005-08-01 16:49:12 +0200326 }
327
328 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
329 info->protect[i] = 0;
330 else
331 info->protect[i] = addr2[2] & 1;
332 }
333
334 /* issue bank reset to return to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200335 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
Stefan Roese8a316c92005-08-01 16:49:12 +0200336
337 return (info->size);
338}
339
Stefan Roese17f50f222005-08-04 17:09:16 +0200340static int wait_for_DQ7_1(flash_info_t * info, int sect)
Stefan Roese8a316c92005-08-01 16:49:12 +0200341{
342 ulong start, now, last;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200343 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
344 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Stefan Roese8a316c92005-08-01 16:49:12 +0200345
346 start = get_timer(0);
347 last = start;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200348 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
349 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
350 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Stefan Roese8a316c92005-08-01 16:49:12 +0200351 printf("Timeout\n");
352 return -1;
353 }
354 /* show that we're waiting */
355 if ((now - last) > 1000) { /* every second */
356 putc('.');
357 last = now;
358 }
359 }
360 return 0;
361}
362
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200363#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Stefan Roese8a316c92005-08-01 16:49:12 +0200364int flash_erase(flash_info_t * info, int s_first, int s_last)
365{
Stefan Roese17f50f222005-08-04 17:09:16 +0200366 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
367 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
Stefan Roese9ede3cc2006-08-07 14:31:21 +0200368 ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) ||
Stefan Roese17f50f222005-08-04 17:09:16 +0200369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
370 return flash_erase_2(info, s_first, s_last);
371 } else {
372 return flash_erase_1(info, s_first, s_last);
373 }
374}
375
376static int flash_erase_1(flash_info_t * info, int s_first, int s_last)
377#else
378int flash_erase(flash_info_t * info, int s_first, int s_last)
379#endif
380{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200381 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
382 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
Wolfgang Denk9879d572011-11-17 09:49:59 +0000383 int flag, prot, sect;
Stefan Roese8a316c92005-08-01 16:49:12 +0200384 int i;
385
386 if ((s_first < 0) || (s_first > s_last)) {
387 if (info->flash_id == FLASH_UNKNOWN) {
388 printf("- missing\n");
389 } else {
390 printf("- no sectors to erase\n");
391 }
392 return 1;
393 }
394
395 if (info->flash_id == FLASH_UNKNOWN) {
396 printf("Can't erase unknown flash type - aborted\n");
397 return 1;
398 }
399
400 prot = 0;
401 for (sect = s_first; sect <= s_last; ++sect) {
402 if (info->protect[sect]) {
403 prot++;
404 }
405 }
406
407 if (prot) {
408 printf("- Warning: %d protected sectors will not be erased!\n",
409 prot);
410 } else {
411 printf("\n");
412 }
413
Stefan Roese8a316c92005-08-01 16:49:12 +0200414 /* Disable interrupts which might cause a timeout here */
415 flag = disable_interrupts();
416
417 /* Start erase on unprotected sectors */
418 for (sect = s_first; sect <= s_last; sect++) {
419 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200420 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Stefan Roese8a316c92005-08-01 16:49:12 +0200421
422 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200423 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
424 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
425 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
426 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
427 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
428 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
Stefan Roese8a316c92005-08-01 16:49:12 +0200429 for (i = 0; i < 50; i++)
430 udelay(1000); /* wait 1 ms */
431 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200432 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
433 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
434 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
435 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
436 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
437 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
Stefan Roese8a316c92005-08-01 16:49:12 +0200438 }
Stefan Roese8a316c92005-08-01 16:49:12 +0200439 /*
440 * Wait for each sector to complete, it's more
441 * reliable. According to AMD Spec, you must
442 * issue all erase commands within a specified
443 * timeout. This has been seen to fail, especially
444 * if printf()s are included (for debug)!!
445 */
Stefan Roese17f50f222005-08-04 17:09:16 +0200446 wait_for_DQ7_1(info, sect);
Stefan Roese8a316c92005-08-01 16:49:12 +0200447 }
448 }
449
450 /* re-enable interrupts if necessary */
451 if (flag)
452 enable_interrupts();
453
454 /* wait at least 80us - let's wait 1 ms */
455 udelay(1000);
456
457 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200458 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
459 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
Stefan Roese8a316c92005-08-01 16:49:12 +0200460
461 printf(" done\n");
462 return 0;
463}
464
465/*-----------------------------------------------------------------------
466 * Copy memory to flash, returns:
467 * 0 - OK
468 * 1 - write timeout
469 * 2 - Flash not erased
470 */
471int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
472{
473 ulong cp, wp, data;
474 int i, l, rc;
475
476 wp = (addr & ~3); /* get lower word aligned address */
477
478 /*
479 * handle unaligned start bytes
480 */
481 if ((l = addr - wp) != 0) {
482 data = 0;
483 for (i = 0, cp = wp; i < l; ++i, ++cp) {
484 data = (data << 8) | (*(uchar *) cp);
485 }
486 for (; i < 4 && cnt > 0; ++i) {
487 data = (data << 8) | *src++;
488 --cnt;
489 ++cp;
490 }
491 for (; cnt == 0 && i < 4; ++i, ++cp) {
492 data = (data << 8) | (*(uchar *) cp);
493 }
494
495 if ((rc = write_word(info, wp, data)) != 0) {
496 return (rc);
497 }
498 wp += 4;
499 }
500
501 /*
502 * handle word aligned part
503 */
504 while (cnt >= 4) {
505 data = 0;
506 for (i = 0; i < 4; ++i) {
507 data = (data << 8) | *src++;
508 }
509 if ((rc = write_word(info, wp, data)) != 0) {
510 return (rc);
511 }
512 wp += 4;
513 cnt -= 4;
514 }
515
516 if (cnt == 0) {
517 return (0);
518 }
519
520 /*
521 * handle unaligned tail bytes
522 */
523 data = 0;
524 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
525 data = (data << 8) | *src++;
526 --cnt;
527 }
528 for (; i < 4; ++i, ++cp) {
529 data = (data << 8) | (*(uchar *) cp);
530 }
531
532 return (write_word(info, wp, data));
533}
534
535/*-----------------------------------------------------------------------
Stefan Roese17f50f222005-08-04 17:09:16 +0200536 * Copy memory to flash, returns:
Stefan Roese8a316c92005-08-01 16:49:12 +0200537 * 0 - OK
538 * 1 - write timeout
539 * 2 - Flash not erased
540 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200541#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Stefan Roese8a316c92005-08-01 16:49:12 +0200542static int write_word(flash_info_t * info, ulong dest, ulong data)
543{
Stefan Roese17f50f222005-08-04 17:09:16 +0200544 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
545 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
Stefan Roese9ede3cc2006-08-07 14:31:21 +0200546 ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) ||
Stefan Roese17f50f222005-08-04 17:09:16 +0200547 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
548 return write_word_2(info, dest, data);
549 } else {
550 return write_word_1(info, dest, data);
551 }
552}
553
554static int write_word_1(flash_info_t * info, ulong dest, ulong data)
555#else
556static int write_word(flash_info_t * info, ulong dest, ulong data)
557#endif
558{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200559 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
560 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
561 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *) & data;
Stefan Roese8a316c92005-08-01 16:49:12 +0200562 ulong start;
563 int i;
564
565 /* Check if Flash is (sufficiently) erased */
566 if ((*((vu_long *)dest) & data) != data) {
567 return (2);
568 }
569
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200570 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
Stefan Roese8a316c92005-08-01 16:49:12 +0200571 int flag;
572
573 /* Disable interrupts which might cause a timeout here */
574 flag = disable_interrupts();
575
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200576 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
577 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
578 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
Stefan Roese8a316c92005-08-01 16:49:12 +0200579
580 dest2[i] = data2[i];
581
582 /* re-enable interrupts if necessary */
583 if (flag)
584 enable_interrupts();
585
586 /* data polling for D7 */
587 start = get_timer(0);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200588 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
589 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
Stefan Roese8a316c92005-08-01 16:49:12 +0200590
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200591 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Stefan Roese8a316c92005-08-01 16:49:12 +0200592 return (1);
593 }
594 }
595 }
596
597 return (0);
598}
Stefan Roese17f50f222005-08-04 17:09:16 +0200599
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200600#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Stefan Roese17f50f222005-08-04 17:09:16 +0200601
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200602#undef CONFIG_SYS_FLASH_WORD_SIZE
603#define CONFIG_SYS_FLASH_WORD_SIZE unsigned short
Stefan Roese17f50f222005-08-04 17:09:16 +0200604
605/*
606 * The following code cannot be run from FLASH!
607 */
608static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
609{
610 short i;
611 int n;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200612 CONFIG_SYS_FLASH_WORD_SIZE value;
Stefan Roese17f50f222005-08-04 17:09:16 +0200613 ulong base = (ulong) addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200614 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
Stefan Roese17f50f222005-08-04 17:09:16 +0200615
616 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
617
618 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200619 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
620 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
621 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
Stefan Roese17f50f222005-08-04 17:09:16 +0200622 udelay(1000);
623
624 value = addr2[0];
625 DEBUGF("FLASH MANUFACT: %x\n", value);
626
627 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200628 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
Stefan Roese17f50f222005-08-04 17:09:16 +0200629 info->flash_id = FLASH_MAN_AMD;
630 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200631 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
Stefan Roese17f50f222005-08-04 17:09:16 +0200632 info->flash_id = FLASH_MAN_FUJ;
633 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200634 case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
Stefan Roese17f50f222005-08-04 17:09:16 +0200635 info->flash_id = FLASH_MAN_SST;
636 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200637 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
Stefan Roese17f50f222005-08-04 17:09:16 +0200638 info->flash_id = FLASH_MAN_STM;
639 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200640 case (CONFIG_SYS_FLASH_WORD_SIZE) MX_MANUFACT:
Stefan Roese9ede3cc2006-08-07 14:31:21 +0200641 info->flash_id = FLASH_MAN_MX;
642 break;
Stefan Roese17f50f222005-08-04 17:09:16 +0200643 default:
644 info->flash_id = FLASH_UNKNOWN;
645 info->sector_count = 0;
646 info->size = 0;
647 return (0); /* no or unknown flash */
648 }
649
Wolfgang Denk16850912006-08-27 18:10:01 +0200650 value = addr2[1]; /* device ID */
Stefan Roese17f50f222005-08-04 17:09:16 +0200651
652 DEBUGF("\nFLASH DEVICEID: %x\n", value);
653
654 switch (value) {
655
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200656 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
Stefan Roese17f50f222005-08-04 17:09:16 +0200657 info->flash_id += FLASH_AM320T;
658 info->sector_count = 71;
Wolfgang Denk16850912006-08-27 18:10:01 +0200659 info->size = 0x00400000; break; /* => 4 MiB */
Stefan Roese17f50f222005-08-04 17:09:16 +0200660
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200661 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
Stefan Roese17f50f222005-08-04 17:09:16 +0200662 info->flash_id += FLASH_AM320B;
663 info->sector_count = 71;
Wolfgang Denk16850912006-08-27 18:10:01 +0200664 info->size = 0x00400000; break; /* => 4 MiB */
Stefan Roese17f50f222005-08-04 17:09:16 +0200665
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200666 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
Stefan Roese17f50f222005-08-04 17:09:16 +0200667 info->flash_id += FLASH_STMW320DT;
668 info->sector_count = 67;
Wolfgang Denk16850912006-08-27 18:10:01 +0200669 info->size = 0x00400000; break; /* => 4 MiB */
Stefan Roese17f50f222005-08-04 17:09:16 +0200670
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200671 case (CONFIG_SYS_FLASH_WORD_SIZE)MX_ID_LV320T:
Stefan Roese9ede3cc2006-08-07 14:31:21 +0200672 info->flash_id += FLASH_MXLV320T;
673 info->sector_count = 71;
674 info->size = 0x00400000;
675 break; /* => 4 MB */
676
Stefan Roese17f50f222005-08-04 17:09:16 +0200677 default:
678 info->flash_id = FLASH_UNKNOWN;
679 return (0); /* => no or unknown flash */
680 }
681
682 /* set up sector start address table */
683 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
684 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
685 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
686 for (i = 0; i < info->sector_count; i++)
687 info->start[i] = base + (i * 0x00010000);
688 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
689 /* set sector offsets for top boot block type */
690 base += info->size;
691 i = info->sector_count;
692 /* 1 x 16k boot sector */
693 base -= 16 << 10;
694 --i;
695 info->start[i] = base;
696 /* 2 x 8k boot sectors */
697 for (n=0; n<2; ++n) {
698 base -= 8 << 10;
699 --i;
700 info->start[i] = base;
701 }
702 /* 1 x 32k boot sector */
703 base -= 32 << 10;
704 --i;
705 info->start[i] = base;
706
707 while (i > 0) { /* 64k regular sectors */
708 base -= 64 << 10;
709 --i;
710 info->start[i] = base;
711 }
Stefan Roese9ede3cc2006-08-07 14:31:21 +0200712 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) {
713 i = info->sector_count - 1;
714 info->start[i--] = base + info->size - 0x00002000;
715 info->start[i--] = base + info->size - 0x00004000;
716 info->start[i--] = base + info->size - 0x00006000;
717 info->start[i--] = base + info->size - 0x00008000;
718 info->start[i--] = base + info->size - 0x0000a000;
719 info->start[i--] = base + info->size - 0x0000c000;
720 info->start[i--] = base + info->size - 0x0000e000;
721 info->start[i--] = base + info->size - 0x00010000;
722
723 for (; i >= 0; i--)
724 info->start[i] = base + i * 0x00010000;
Stefan Roese17f50f222005-08-04 17:09:16 +0200725 } else {
726 if (info->flash_id & FLASH_BTYPE) {
Wolfgang Denk16850912006-08-27 18:10:01 +0200727 /* set sector offsets for bottom boot block type */
Stefan Roese17f50f222005-08-04 17:09:16 +0200728 info->start[0] = base + 0x00000000;
Eugene OBriend2f68002007-07-31 10:24:56 +0200729 info->start[1] = base + 0x00002000;
730 info->start[2] = base + 0x00004000;
731 info->start[3] = base + 0x00006000;
732 info->start[4] = base + 0x00008000;
733 info->start[5] = base + 0x0000a000;
734 info->start[6] = base + 0x0000c000;
735 info->start[7] = base + 0x0000e000;
736 for (i = 8; i < info->sector_count; i++) {
Stefan Roese17f50f222005-08-04 17:09:16 +0200737 info->start[i] =
Eugene OBriend2f68002007-07-31 10:24:56 +0200738 base + ((i-7) * 0x00010000);
Stefan Roese17f50f222005-08-04 17:09:16 +0200739 }
740 } else {
Wolfgang Denk16850912006-08-27 18:10:01 +0200741 /* set sector offsets for top boot block type */
Stefan Roese17f50f222005-08-04 17:09:16 +0200742 i = info->sector_count - 1;
Eugene OBriend2f68002007-07-31 10:24:56 +0200743 info->start[i--] = base + info->size - 0x00002000;
Stefan Roese17f50f222005-08-04 17:09:16 +0200744 info->start[i--] = base + info->size - 0x00004000;
745 info->start[i--] = base + info->size - 0x00006000;
746 info->start[i--] = base + info->size - 0x00008000;
Eugene OBriend2f68002007-07-31 10:24:56 +0200747 info->start[i--] = base + info->size - 0x0000a000;
748 info->start[i--] = base + info->size - 0x0000c000;
749 info->start[i--] = base + info->size - 0x0000e000;
Stefan Roese17f50f222005-08-04 17:09:16 +0200750 for (; i >= 0; i--) {
751 info->start[i] = base + i * 0x00010000;
752 }
753 }
754 }
755
756 /* check for protected sectors */
757 for (i = 0; i < info->sector_count; i++) {
758 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
759 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200760 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
Stefan Roese17f50f222005-08-04 17:09:16 +0200761
762 /* For AMD29033C flash we need to resend the command of *
763 * reading flash protection for upper 8 Mb of flash */
764 if (i == 32) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200765 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
766 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
767 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
Stefan Roese17f50f222005-08-04 17:09:16 +0200768 }
769
770 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
771 info->protect[i] = 0;
772 else
773 info->protect[i] = addr2[2] & 1;
774 }
775
776 /* issue bank reset to return to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200777 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
Stefan Roese17f50f222005-08-04 17:09:16 +0200778
779 return (info->size);
780}
781
782static int wait_for_DQ7_2(flash_info_t * info, int sect)
783{
784 ulong start, now, last;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200785 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
786 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Stefan Roese17f50f222005-08-04 17:09:16 +0200787
788 start = get_timer(0);
789 last = start;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200790 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
791 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
792 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Stefan Roese17f50f222005-08-04 17:09:16 +0200793 printf("Timeout\n");
794 return -1;
795 }
796 /* show that we're waiting */
797 if ((now - last) > 1000) { /* every second */
798 putc('.');
799 last = now;
800 }
801 }
802 return 0;
803}
804
805static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
806{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200807 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
808 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
Wolfgang Denk9879d572011-11-17 09:49:59 +0000809 int flag, prot, sect;
Stefan Roese17f50f222005-08-04 17:09:16 +0200810 int i;
811
812 if ((s_first < 0) || (s_first > s_last)) {
813 if (info->flash_id == FLASH_UNKNOWN) {
814 printf("- missing\n");
815 } else {
816 printf("- no sectors to erase\n");
817 }
818 return 1;
819 }
820
821 if (info->flash_id == FLASH_UNKNOWN) {
822 printf("Can't erase unknown flash type - aborted\n");
823 return 1;
824 }
825
826 prot = 0;
827 for (sect = s_first; sect <= s_last; ++sect) {
828 if (info->protect[sect]) {
829 prot++;
830 }
831 }
832
833 if (prot) {
834 printf("- Warning: %d protected sectors will not be erased!\n",
835 prot);
836 } else {
837 printf("\n");
838 }
839
Stefan Roese17f50f222005-08-04 17:09:16 +0200840 /* Disable interrupts which might cause a timeout here */
841 flag = disable_interrupts();
842
843 /* Start erase on unprotected sectors */
844 for (sect = s_first; sect <= s_last; sect++) {
845 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200846 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Stefan Roese17f50f222005-08-04 17:09:16 +0200847
848 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200849 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
850 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
851 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
852 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
853 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
854 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
Stefan Roese17f50f222005-08-04 17:09:16 +0200855 for (i = 0; i < 50; i++)
856 udelay(1000); /* wait 1 ms */
857 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200858 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
859 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
860 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
861 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
862 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
863 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
Stefan Roese17f50f222005-08-04 17:09:16 +0200864 }
Stefan Roese17f50f222005-08-04 17:09:16 +0200865 /*
866 * Wait for each sector to complete, it's more
867 * reliable. According to AMD Spec, you must
868 * issue all erase commands within a specified
869 * timeout. This has been seen to fail, especially
870 * if printf()s are included (for debug)!!
871 */
872 wait_for_DQ7_2(info, sect);
873 }
874 }
875
876 /* re-enable interrupts if necessary */
877 if (flag)
878 enable_interrupts();
879
880 /* wait at least 80us - let's wait 1 ms */
881 udelay(1000);
882
883 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200884 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
885 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
Stefan Roese17f50f222005-08-04 17:09:16 +0200886
887 printf(" done\n");
888 return 0;
889}
890
891static int write_word_2(flash_info_t * info, ulong dest, ulong data)
892{
Wolfgang Denk6c7bc912009-09-11 11:30:34 +0200893 ulong *data_ptr = &data;
894 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
895 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
896 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
Stefan Roese17f50f222005-08-04 17:09:16 +0200897 ulong start;
898 int i;
899
900 /* Check if Flash is (sufficiently) erased */
901 if ((*((vu_long *)dest) & data) != data) {
902 return (2);
903 }
904
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200905 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
Stefan Roese17f50f222005-08-04 17:09:16 +0200906 int flag;
907
908 /* Disable interrupts which might cause a timeout here */
909 flag = disable_interrupts();
910
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200911 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
912 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
913 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
Stefan Roese17f50f222005-08-04 17:09:16 +0200914
915 dest2[i] = data2[i];
916
917 /* re-enable interrupts if necessary */
918 if (flag)
919 enable_interrupts();
920
921 /* data polling for D7 */
922 start = get_timer(0);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200923 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
924 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
Stefan Roese17f50f222005-08-04 17:09:16 +0200925
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200926 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Stefan Roese17f50f222005-08-04 17:09:16 +0200927 return (1);
928 }
929 }
930 }
931
932 return (0);
933}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200934#endif /* CONFIG_SYS_FLASH_2ND_16BIT_DEV */