blob: 07804888883724146797a5eeef22ef5dedf5e944 [file] [log] [blame]
John Otkend4024bb2007-07-26 17:49:11 +02001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
John Otkend4024bb2007-07-26 17:49:11 +02006 */
7
8/*
9 * Modified 4/5/2001
10 * Wait for completion of each sector erase command issued
11 * 4/5/2001
12 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
13 */
14
15#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020016#include <asm/ppc4xx.h>
John Otkend4024bb2007-07-26 17:49:11 +020017#include <asm/processor.h>
18
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020019flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
John Otkend4024bb2007-07-26 17:49:11 +020020
21#undef DEBUG
22#ifdef DEBUG
23#define DEBUGF(x...) printf(x)
24#else
25#define DEBUGF(x...)
26#endif /* DEBUG */
27
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028#define CONFIG_SYS_FLASH_CHAR_SIZE unsigned char
29#define CONFIG_SYS_FLASH_CHAR_ADDR0 (0x0aaa)
30#define CONFIG_SYS_FLASH_CHAR_ADDR1 (0x0555)
John Otkend4024bb2007-07-26 17:49:11 +020031/*-----------------------------------------------------------------------
32 * Functions
33 */
34static ulong flash_get_size(vu_long * addr, flash_info_t * info);
35static void flash_get_offsets(ulong base, flash_info_t * info);
36static int write_word(flash_info_t * info, ulong dest, ulong data);
37#ifdef FLASH_BASE1_PRELIM
38static int write_word_1(flash_info_t * info, ulong dest, ulong data);
39static int write_word_2(flash_info_t * info, ulong dest, ulong data);
40static int flash_erase_1(flash_info_t * info, int s_first, int s_last);
41static int flash_erase_2(flash_info_t * info, int s_first, int s_last);
42static ulong flash_get_size_1(vu_long * addr, flash_info_t * info);
43static ulong flash_get_size_2(vu_long * addr, flash_info_t * info);
44#endif
45
46unsigned long flash_init(void)
47{
48 unsigned long size_b0, size_b1=0;
49 int i;
50
51 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
John Otkend4024bb2007-07-26 17:49:11 +020053 flash_info[i].flash_id = FLASH_UNKNOWN;
54 }
55
56 /* Static FLASH Bank configuration here - FIXME XXX */
57
58 size_b0 =
59 flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
60
61 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
62 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
63 size_b0, size_b0 << 20);
64 }
65
66 if (size_b0) {
67 /* Setup offsets */
68 flash_get_offsets(FLASH_BASE0_PRELIM, &flash_info[0]);
69 /* Monitor protection ON by default */
70 (void)flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020071 CONFIG_SYS_MONITOR_BASE,
72 CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
John Otkend4024bb2007-07-26 17:49:11 +020073 &flash_info[0]);
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020074#ifdef CONFIG_ENV_IS_IN_FLASH
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020075 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
76 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
John Otkend4024bb2007-07-26 17:49:11 +020077 &flash_info[0]);
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020078 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
79 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
John Otkend4024bb2007-07-26 17:49:11 +020080 &flash_info[0]);
81#endif
82 /* Also protect sector containing initial power-up instruction */
83 /* (flash_protect() checks address range - other call ignored) */
84 (void)flash_protect(FLAG_PROTECT_SET,
85 0xFFFFFFFC, 0xFFFFFFFF, &flash_info[0]);
86
87 flash_info[0].size = size_b0;
88 }
89#ifdef FLASH_BASE1_PRELIM
90 size_b1 =
91 flash_get_size((vu_long *) FLASH_BASE1_PRELIM, &flash_info[1])*2;
92
93 if (flash_info[1].flash_id == FLASH_UNKNOWN) {
94 printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
95 size_b1, size_b1 << 20);
96 }
97
98 if (size_b1) {
99 /* Setup offsets */
100 flash_get_offsets(FLASH_BASE1_PRELIM, &flash_info[1]);
101 flash_info[1].size = size_b1;
102 }
103#endif
104 return (size_b0 + size_b1);
105}
106
107static void flash_get_offsets(ulong base, flash_info_t * info)
108{
109 int i;
110
111 /* set up sector start address table */
112 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
113 (info->flash_id == FLASH_AM040)) {
114 for (i = 0; i < info->sector_count; i++)
115 info->start[i] = base + (i * 0x00010000);
116 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
117 for (i = 0; i < info->sector_count; i++) {
118 info->start[i] = base + (i * 0x00010000*2);
119 }
120 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N ) {
121 for (i = 0; i < info->sector_count; i++) {
122 info->start[i] = base + (i * 0x00020000*2);
123 }
124 } else {
125 if (info->flash_id & FLASH_BTYPE) {
126 /* set sector offsets for bottom boot block type */
127 info->start[0] = base + 0x00000000;
128 info->start[1] = base + 0x00004000;
129 info->start[2] = base + 0x00006000;
130 info->start[3] = base + 0x00008000;
131 for (i = 4; i < info->sector_count; i++) {
132 info->start[i] =
133 base + (i * 0x00010000) - 0x00030000;
134 }
135 } else {
136 /* set sector offsets for top boot block type */
137 i = info->sector_count - 1;
138 info->start[i--] = base + info->size - 0x00004000;
139 info->start[i--] = base + info->size - 0x00006000;
140 info->start[i--] = base + info->size - 0x00008000;
141 for (; i >= 0; i--) {
142 info->start[i] = base + i * 0x00010000;
143 }
144 }
145 }
146}
147
148
149void flash_print_info(flash_info_t * info)
150{
151 int i;
152 int k;
153 int size;
154 int erased;
155 volatile unsigned long *flash;
156
157 if (info->flash_id == FLASH_UNKNOWN) {
158 printf("missing or unknown FLASH type\n");
159 return;
160 }
161
162 switch (info->flash_id & FLASH_VENDMASK) {
163 case FLASH_MAN_AMD:
164 printf("AMD ");
165 break;
166 case FLASH_MAN_STM:
167 printf("STM ");
168 break;
169 case FLASH_MAN_FUJ:
170 printf("FUJITSU ");
171 break;
172 case FLASH_MAN_SST:
173 printf("SST ");
174 break;
175 default:
176 printf("Unknown Vendor ");
177 break;
178 }
179
180 switch (info->flash_id & FLASH_TYPEMASK) {
181 case FLASH_AM040:
182 printf("AM29F040 (512 Kbit, uniform sector size)\n");
183 break;
184 case FLASH_AM400B:
185 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
186 break;
187 case FLASH_AM400T:
188 printf("AM29LV400T (4 Mbit, top boot sector)\n");
189 break;
190 case FLASH_AM800B:
191 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
192 break;
193 case FLASH_AM800T:
194 printf("AM29LV800T (8 Mbit, top boot sector)\n");
195 break;
196 case FLASH_AMD016:
197 printf("AM29F016D (16 Mbit, uniform sector size)\n");
198 break;
199 case FLASH_AM160B:
200 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
201 break;
202 case FLASH_AM160T:
203 printf("AM29LV160T (16 Mbit, top boot sector)\n");
204 break;
205 case FLASH_AM320B:
206 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
207 break;
208 case FLASH_AM320T:
209 printf("AM29LV320T (32 Mbit, top boot sector)\n");
210 break;
211 case FLASH_AM033C:
212 printf("AM29LV033C (32 Mbit, top boot sector)\n");
213 break;
214 case FLASH_AMLV128U:
215 printf("AM29LV128U (128 Mbit * 2, top boot sector)\n");
216 break;
217 case FLASH_SST800A:
218 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
219 break;
220 case FLASH_SST160A:
221 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
222 break;
223 case FLASH_STMW320DT:
224 printf ("M29W320DT (32 M, top sector)\n");
225 break;
226 case FLASH_S29GL128N:
227 printf ("S29GL128N (256 Mbit, uniform sector size)\n");
228 break;
229 default:
230 printf("Unknown Chip Type\n");
231 break;
232 }
233
234 printf(" Size: %ld KB in %d Sectors\n",
235 info->size >> 10, info->sector_count);
236
237 printf(" Sector Start Addresses:");
238 for (i = 0; i < info->sector_count; ++i) {
239 /*
240 * Check if whole sector is erased
241 */
242 if (i != (info->sector_count - 1))
243 size = info->start[i + 1] - info->start[i];
244 else
245 size = info->start[0] + info->size - info->start[i];
246 erased = 1;
247 flash = (volatile unsigned long *)info->start[i];
248 size = size >> 2; /* divide by 4 for longword access */
249 for (k = 0; k < size; k++) {
250 if (*flash++ != 0xffffffff) {
251 erased = 0;
252 break;
253 }
254 }
255
256 if ((i % 5) == 0)
257 printf("\n ");
258 printf(" %08lX%s%s",
259 info->start[i],
260 erased ? " E" : " ", info->protect[i] ? "RO " : " ");
261 }
262 printf("\n");
263 return;
264}
265
266
267/*
268 * The following code cannot be run from FLASH!
269 */
270#ifdef FLASH_BASE1_PRELIM
271static ulong flash_get_size(vu_long * addr, flash_info_t * info)
272{
273 if ((ulong)addr == FLASH_BASE1_PRELIM) {
274 return flash_get_size_2(addr, info);
275 } else {
276 return flash_get_size_1(addr, info);
277 }
278}
279
280static ulong flash_get_size_1(vu_long * addr, flash_info_t * info)
281#else
282static ulong flash_get_size(vu_long * addr, flash_info_t * info)
283#endif
284{
285 short i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200286 CONFIG_SYS_FLASH_WORD_SIZE value;
John Otkend4024bb2007-07-26 17:49:11 +0200287 ulong base = (ulong) addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200288 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
John Otkend4024bb2007-07-26 17:49:11 +0200289
290 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
291
292 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200293 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
294 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
295 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
John Otkend4024bb2007-07-26 17:49:11 +0200296 udelay(1000);
297
298 value = addr2[0];
299 DEBUGF("FLASH MANUFACT: %x\n", value);
300
301 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200302 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200303 info->flash_id = FLASH_MAN_AMD;
304 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200305 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200306 info->flash_id = FLASH_MAN_FUJ;
307 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200308 case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200309 info->flash_id = FLASH_MAN_SST;
310 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200311 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200312 info->flash_id = FLASH_MAN_STM;
313 break;
314 default:
315 info->flash_id = FLASH_UNKNOWN;
316 info->sector_count = 0;
317 info->size = 0;
318 return 0; /* no or unknown flash */
319 }
320
321 value = addr2[1]; /* device ID */
322 DEBUGF("\nFLASH DEVICEID: %x\n", value);
323
324 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200325 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
John Otkend4024bb2007-07-26 17:49:11 +0200326 info->flash_id += FLASH_AM040;
327 info->sector_count = 8;
328 info->size = 0x0080000; /* => 512 ko */
329 break;
330
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200331 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
John Otkend4024bb2007-07-26 17:49:11 +0200332 info->flash_id += FLASH_AM040;
333 info->sector_count = 8;
334 info->size = 0x0080000; /* => 512 ko */
335 break;
336
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200337 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
John Otkend4024bb2007-07-26 17:49:11 +0200338 info->flash_id += FLASH_AM040;
339 info->sector_count = 8;
340 info->size = 0x0080000; /* => 512 ko */
341 break;
342
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200343 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
John Otkend4024bb2007-07-26 17:49:11 +0200344 info->flash_id += FLASH_AMD016;
345 info->sector_count = 32;
346 info->size = 0x00200000;
347 break; /* => 2 MB */
348
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200349 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
John Otkend4024bb2007-07-26 17:49:11 +0200350 info->flash_id += FLASH_AMDLV033C;
351 info->sector_count = 64;
352 info->size = 0x00400000;
353 break; /* => 4 MB */
354
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200355 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
John Otkend4024bb2007-07-26 17:49:11 +0200356 info->flash_id += FLASH_AM400T;
357 info->sector_count = 11;
358 info->size = 0x00080000;
359 break; /* => 0.5 MB */
360
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200361 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
John Otkend4024bb2007-07-26 17:49:11 +0200362 info->flash_id += FLASH_AM400B;
363 info->sector_count = 11;
364 info->size = 0x00080000;
365 break; /* => 0.5 MB */
366
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200367 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
John Otkend4024bb2007-07-26 17:49:11 +0200368 info->flash_id += FLASH_AM800T;
369 info->sector_count = 19;
370 info->size = 0x00100000;
371 break; /* => 1 MB */
372
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200373 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
John Otkend4024bb2007-07-26 17:49:11 +0200374 info->flash_id += FLASH_AM800B;
375 info->sector_count = 19;
376 info->size = 0x00100000;
377 break; /* => 1 MB */
378
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200379 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
John Otkend4024bb2007-07-26 17:49:11 +0200380 info->flash_id += FLASH_AM160T;
381 info->sector_count = 35;
382 info->size = 0x00200000;
383 break; /* => 2 MB */
384
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200385 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
John Otkend4024bb2007-07-26 17:49:11 +0200386 info->flash_id += FLASH_AM160B;
387 info->sector_count = 35;
388 info->size = 0x00200000;
389 break; /* => 2 MB */
390 default:
391 info->flash_id = FLASH_UNKNOWN;
392 return 0; /* => no or unknown flash */
393 }
394
395 /* set up sector start address table */
396 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
397 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
398 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
399 for (i = 0; i < info->sector_count; i++)
400 info->start[i] = base + (i * 0x00010000);
401 }
402 else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
403 for (i = 0; i < info->sector_count; i++)
404 info->start[i] = base + (i * 0x00010000 * 2);
405 } else {
406 if (info->flash_id & FLASH_BTYPE) {
407 /* set sector offsets for bottom boot block type */
408 info->start[0] = base + 0x00000000;
409 info->start[1] = base + 0x00004000;
410 info->start[2] = base + 0x00006000;
411 info->start[3] = base + 0x00008000;
412 for (i = 4; i < info->sector_count; i++) {
413 info->start[i] =
414 base + (i * 0x00010000) - 0x00030000;
415 }
416 } else {
417 /* set sector offsets for top boot block type */
418 i = info->sector_count - 1;
419 info->start[i--] = base + info->size - 0x00004000;
420 info->start[i--] = base + info->size - 0x00006000;
421 info->start[i--] = base + info->size - 0x00008000;
422 for (; i >= 0; i--) {
423 info->start[i] = base + i * 0x00010000;
424 }
425 }
426 }
427
428 /* check for protected sectors */
429 for (i = 0; i < info->sector_count; i++) {
430 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
431 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200432 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
John Otkend4024bb2007-07-26 17:49:11 +0200433
434 /* For AMD29033C flash we need to resend the command of *
435 * reading flash protection for upper 8 Mb of flash */
436 if (i == 32) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200437 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
438 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
439 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
John Otkend4024bb2007-07-26 17:49:11 +0200440 }
441
442 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
443 info->protect[i] = 0;
444 else
445 info->protect[i] = addr2[2] & 1;
446 }
447
448 /* issue bank reset to return to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200449 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
John Otkend4024bb2007-07-26 17:49:11 +0200450
451 return info->size;
452}
453
454static int wait_for_DQ7_1(flash_info_t * info, int sect)
455{
456 ulong start, now, last;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200457 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
458 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
John Otkend4024bb2007-07-26 17:49:11 +0200459
460 start = get_timer(0);
461 last = start;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200462 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
463 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
464 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
John Otkend4024bb2007-07-26 17:49:11 +0200465 printf("Timeout\n");
466 return -1;
467 }
468 /* show that we're waiting */
469 if ((now - last) > 1000) { /* every second */
470 putc('.');
471 last = now;
472 }
473 }
474 return 0;
475}
476
477#ifdef FLASH_BASE1_PRELIM
478int flash_erase(flash_info_t * info, int s_first, int s_last)
479{
480 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
481 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
482 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) ||
483 ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N) ||
484 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
485 return flash_erase_2(info, s_first, s_last);
486 } else {
487 return flash_erase_1(info, s_first, s_last);
488 }
489}
490
491static int flash_erase_1(flash_info_t * info, int s_first, int s_last)
492#else
493int flash_erase(flash_info_t * info, int s_first, int s_last)
494#endif
495{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200496 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
497 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
Wolfgang Denkb63cb092011-11-17 09:50:01 +0000498 int flag, prot, sect;
John Otkend4024bb2007-07-26 17:49:11 +0200499 int i;
500
501 if ((s_first < 0) || (s_first > s_last)) {
502 if (info->flash_id == FLASH_UNKNOWN) {
503 printf("- missing\n");
504 } else {
505 printf("- no sectors to erase\n");
506 }
507 return 1;
508 }
509
510 if (info->flash_id == FLASH_UNKNOWN) {
511 printf("Can't erase unknown flash type - aborted\n");
512 return 1;
513 }
514
515 prot = 0;
516 for (sect = s_first; sect <= s_last; ++sect) {
517 if (info->protect[sect]) {
518 prot++;
519 }
520 }
521
522 if (prot) {
523 printf("- Warning: %d protected sectors will not be erased!\n",
524 prot);
525 } else {
526 printf("\n");
527 }
528
John Otkend4024bb2007-07-26 17:49:11 +0200529 /* Disable interrupts which might cause a timeout here */
530 flag = disable_interrupts();
531
532 /* Start erase on unprotected sectors */
533 for (sect = s_first; sect <= s_last; sect++) {
534 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200535 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
John Otkend4024bb2007-07-26 17:49:11 +0200536
537 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200538 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
539 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
540 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
541 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
542 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
543 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
John Otkend4024bb2007-07-26 17:49:11 +0200544 for (i = 0; i < 50; i++)
545 udelay(1000); /* wait 1 ms */
546 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200547 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
548 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
549 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
550 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
551 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
552 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
John Otkend4024bb2007-07-26 17:49:11 +0200553 }
John Otkend4024bb2007-07-26 17:49:11 +0200554 /*
555 * Wait for each sector to complete, it's more
556 * reliable. According to AMD Spec, you must
557 * issue all erase commands within a specified
558 * timeout. This has been seen to fail, especially
559 * if printf()s are included (for debug)!!
560 */
561 wait_for_DQ7_1(info, sect);
562 }
563 }
564
565 /* re-enable interrupts if necessary */
566 if (flag)
567 enable_interrupts();
568
569 /* wait at least 80us - let's wait 1 ms */
570 udelay(1000);
571
572 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200573 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
574 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
John Otkend4024bb2007-07-26 17:49:11 +0200575
576 printf(" done\n");
577 return 0;
578}
579
580/*-----------------------------------------------------------------------
581 * Copy memory to flash, returns:
582 * 0 - OK
583 * 1 - write timeout
584 * 2 - Flash not erased
585 */
586int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
587{
588 ulong cp, wp, data;
589 int i, l, rc;
590
591 wp = (addr & ~3); /* get lower word aligned address */
592
593 /*
594 * handle unaligned start bytes
595 */
596 if ((l = addr - wp) != 0) {
597 data = 0;
598 for (i = 0, cp = wp; i < l; ++i, ++cp) {
599 data = (data << 8) | (*(uchar *) cp);
600 }
601 for (; i < 4 && cnt > 0; ++i) {
602 data = (data << 8) | *src++;
603 --cnt;
604 ++cp;
605 }
606 for (; cnt == 0 && i < 4; ++i, ++cp) {
607 data = (data << 8) | (*(uchar *) cp);
608 }
609
610 if ((rc = write_word(info, wp, data)) != 0) {
611 return rc;
612 }
613 wp += 4;
614 }
615
616 /*
617 * handle word aligned part
618 */
619 while (cnt >= 4) {
620 data = 0;
621 for (i = 0; i < 4; ++i) {
622 data = (data << 8) | *src++;
623 }
624 if ((rc = write_word(info, wp, data)) != 0) {
625 return rc;
626 }
627 wp += 4;
628 cnt -= 4;
629 }
630
631 if (cnt == 0) {
632 return 0;
633 }
634
635 /*
636 * handle unaligned tail bytes
637 */
638 data = 0;
639 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
640 data = (data << 8) | *src++;
641 --cnt;
642 }
643 for (; i < 4; ++i, ++cp) {
644 data = (data << 8) | (*(uchar *) cp);
645 }
646
647 return (write_word(info, wp, data));
648}
649
650/*-----------------------------------------------------------------------
651 * Copy memory to flash, returns:
652 * 0 - OK
653 * 1 - write timeout
654 * 2 - Flash not erased
655 */
656#ifdef FLASH_BASE1_PRELIM
657static int write_word(flash_info_t * info, ulong dest, ulong data)
658{
659 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
660 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
661 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) ||
662 ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N) ||
663 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
664 return write_word_2(info, dest, data);
665 } else {
666 return write_word_1(info, dest, data);
667 }
668}
669
670static int write_word_1(flash_info_t * info, ulong dest, ulong data)
671#else
672static int write_word(flash_info_t * info, ulong dest, ulong data)
673#endif
674{
Wolfgang Denk030ec522009-09-11 11:44:39 +0200675 ulong *data_ptr = &data;
676 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
677 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
678 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
John Otkend4024bb2007-07-26 17:49:11 +0200679 ulong start;
680 int i;
681
682 /* Check if Flash is (sufficiently) erased */
683 if ((*((vu_long *)dest) & data) != data) {
684 return 2;
685 }
686
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200687 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
John Otkend4024bb2007-07-26 17:49:11 +0200688 int flag;
689
690 /* Disable interrupts which might cause a timeout here */
691 flag = disable_interrupts();
692
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200693 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
694 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
695 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
John Otkend4024bb2007-07-26 17:49:11 +0200696
697 dest2[i] = data2[i];
698
699 /* re-enable interrupts if necessary */
700 if (flag)
701 enable_interrupts();
702
703 /* data polling for D7 */
704 start = get_timer(0);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200705 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
706 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
John Otkend4024bb2007-07-26 17:49:11 +0200707
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200708 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
John Otkend4024bb2007-07-26 17:49:11 +0200709 return 1;
710 }
711 }
712 }
713
714 return 0;
715}
716
717#ifdef FLASH_BASE1_PRELIM
718
719/*
720 * The following code cannot be run from FLASH!
721 */
722static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
723{
724 short i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200725 CONFIG_SYS_FLASH_CHAR_SIZE value;
John Otkend4024bb2007-07-26 17:49:11 +0200726 ulong base = (ulong) addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200727 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
John Otkend4024bb2007-07-26 17:49:11 +0200728
729 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
730
731 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200732 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
733 addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
734 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
John Otkend4024bb2007-07-26 17:49:11 +0200735 udelay(1000);
736
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200737 value = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0];
John Otkend4024bb2007-07-26 17:49:11 +0200738 DEBUGF("FLASH MANUFACT: %x\n", value);
739
740 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200741 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200742 info->flash_id = FLASH_MAN_AMD;
743 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200744 case (CONFIG_SYS_FLASH_CHAR_SIZE) FUJ_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200745 info->flash_id = FLASH_MAN_FUJ;
746 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200747 case (CONFIG_SYS_FLASH_CHAR_SIZE) SST_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200748 info->flash_id = FLASH_MAN_SST;
749 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200750 case (CONFIG_SYS_FLASH_CHAR_SIZE) STM_MANUFACT:
John Otkend4024bb2007-07-26 17:49:11 +0200751 info->flash_id = FLASH_MAN_STM;
752 break;
753 default:
754 info->flash_id = FLASH_UNKNOWN;
755 info->sector_count = 0;
756 info->size = 0;
757 return 0; /* no or unknown flash */
758 }
759
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200760 value = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[2]; /* device ID */
John Otkend4024bb2007-07-26 17:49:11 +0200761 DEBUGF("\nFLASH DEVICEID: %x\n", value);
762
763 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200764 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV040B:
John Otkend4024bb2007-07-26 17:49:11 +0200765 info->flash_id += FLASH_AM040;
766 info->sector_count = 8;
767 info->size = 0x0080000; /* => 512 ko */
768 break;
769
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200770 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_F040B:
John Otkend4024bb2007-07-26 17:49:11 +0200771 info->flash_id += FLASH_AM040;
772 info->sector_count = 8;
773 info->size = 0x0080000; /* => 512 ko */
774 break;
775
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200776 case (CONFIG_SYS_FLASH_CHAR_SIZE) STM_ID_M29W040B:
John Otkend4024bb2007-07-26 17:49:11 +0200777 info->flash_id += FLASH_AM040;
778 info->sector_count = 8;
779 info->size = 0x0080000; /* => 512 ko */
780 break;
781
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200782 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_F016D:
John Otkend4024bb2007-07-26 17:49:11 +0200783 info->flash_id += FLASH_AMD016;
784 info->sector_count = 32;
785 info->size = 0x00200000;
786 break; /* => 2 MB */
787
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200788 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV033C:
John Otkend4024bb2007-07-26 17:49:11 +0200789 info->flash_id += FLASH_AMDLV033C;
790 info->sector_count = 64;
791 info->size = 0x00400000;
792 break; /* => 4 MB */
793
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200794 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV400T:
John Otkend4024bb2007-07-26 17:49:11 +0200795 info->flash_id += FLASH_AM400T;
796 info->sector_count = 11;
797 info->size = 0x00080000;
798 break; /* => 0.5 MB */
799
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200800 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV400B:
John Otkend4024bb2007-07-26 17:49:11 +0200801 info->flash_id += FLASH_AM400B;
802 info->sector_count = 11;
803 info->size = 0x00080000;
804 break; /* => 0.5 MB */
805
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200806 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV800T:
John Otkend4024bb2007-07-26 17:49:11 +0200807 info->flash_id += FLASH_AM800T;
808 info->sector_count = 19;
809 info->size = 0x00100000;
810 break; /* => 1 MB */
811
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200812 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV800B:
John Otkend4024bb2007-07-26 17:49:11 +0200813 info->flash_id += FLASH_AM800B;
814 info->sector_count = 19;
815 info->size = 0x00100000;
816 break; /* => 1 MB */
817
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200818 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV160T:
John Otkend4024bb2007-07-26 17:49:11 +0200819 info->flash_id += FLASH_AM160T;
820 info->sector_count = 35;
821 info->size = 0x00200000;
822 break; /* => 2 MB */
823
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200824 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV160B:
John Otkend4024bb2007-07-26 17:49:11 +0200825 info->flash_id += FLASH_AM160B;
826 info->sector_count = 35;
827 info->size = 0x00200000;
828 break; /* => 2 MB */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200829 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_MIRROR:
830 if ((CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1c] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_LV128U_2
831 && (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1e] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_LV128U_3) {
John Otkend4024bb2007-07-26 17:49:11 +0200832 info->flash_id += FLASH_AMLV128U;
833 info->sector_count = 256;
834 info->size = 0x01000000;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200835 } else if ((CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1c] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_GL128N_2
836 && (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1e] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_GL128N_3 ) {
John Otkend4024bb2007-07-26 17:49:11 +0200837 info->flash_id += FLASH_S29GL128N;
838 info->sector_count = 128;
839 info->size = 0x01000000;
840 }
841 else
842 info->flash_id = FLASH_UNKNOWN;
843 break; /* => 2 MB */
844
845 default:
846 info->flash_id = FLASH_UNKNOWN;
847 return 0; /* => no or unknown flash */
848 }
849
850 /* set up sector start address table */
851 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
852 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
853 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
854 for (i = 0; i < info->sector_count; i++)
855 info->start[i] = base + (i * 0x00010000);
856 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
857 for (i = 0; i < info->sector_count; i++)
858 info->start[i] = base + (i * 0x00010000);
859 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N ) {
860 for (i = 0; i < info->sector_count; i++)
861 info->start[i] = base + (i * 0x00020000);
862 } else {
863 if (info->flash_id & FLASH_BTYPE) {
864 /* set sector offsets for bottom boot block type */
865 info->start[0] = base + 0x00000000;
866 info->start[1] = base + 0x00004000;
867 info->start[2] = base + 0x00006000;
868 info->start[3] = base + 0x00008000;
869 for (i = 4; i < info->sector_count; i++) {
870 info->start[i] =
871 base + (i * 0x00010000) - 0x00030000;
872 }
873 } else {
874 /* set sector offsets for top boot block type */
875 i = info->sector_count - 1;
876 info->start[i--] = base + info->size - 0x00004000;
877 info->start[i--] = base + info->size - 0x00006000;
878 info->start[i--] = base + info->size - 0x00008000;
879 for (; i >= 0; i--) {
880 info->start[i] = base + i * 0x00010000;
881 }
882 }
883 }
884
885 /* check for protected sectors */
886 for (i = 0; i < info->sector_count; i++) {
887 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
888 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200889 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
John Otkend4024bb2007-07-26 17:49:11 +0200890
891 /* For AMD29033C flash we need to resend the command of *
892 * reading flash protection for upper 8 Mb of flash */
893 if (i == 32) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200894 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
895 addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
896 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
John Otkend4024bb2007-07-26 17:49:11 +0200897 }
898
899 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
900 info->protect[i] = 0;
901 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200902 info->protect[i] = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[4] & 1;
John Otkend4024bb2007-07-26 17:49:11 +0200903 }
904
905 /* issue bank reset to return to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200906 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF0F0F0F0;
John Otkend4024bb2007-07-26 17:49:11 +0200907 return info->size;
908}
909
910static int wait_for_DQ7_2(flash_info_t * info, int sect)
911{
912 ulong start, now, last;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200913 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
914 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
John Otkend4024bb2007-07-26 17:49:11 +0200915
916 start = get_timer(0);
917 last = start;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200918 while (((CONFIG_SYS_FLASH_WORD_SIZE)addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) !=
919 (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) {
920 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
John Otkend4024bb2007-07-26 17:49:11 +0200921 printf("Timeout\n");
922 return -1;
923 }
924 /* show that we're waiting */
925 if ((now - last) > 1000) { /* every second */
926 putc('.');
927 last = now;
928 }
929 }
930 return 0;
931}
932
933static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
934{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200935 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
936 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
Wolfgang Denkb63cb092011-11-17 09:50:01 +0000937 int flag, prot, sect;
John Otkend4024bb2007-07-26 17:49:11 +0200938 int i;
939
940 if ((s_first < 0) || (s_first > s_last)) {
941 if (info->flash_id == FLASH_UNKNOWN) {
942 printf("- missing\n");
943 } else {
944 printf("- no sectors to erase\n");
945 }
946 return 1;
947 }
948
949 if (info->flash_id == FLASH_UNKNOWN) {
950 printf("Can't erase unknown flash type - aborted\n");
951 return 1;
952 }
953
954 prot = 0;
955 for (sect = s_first; sect <= s_last; ++sect) {
956 if (info->protect[sect]) {
957 prot++;
958 }
959 }
960
961 if (prot) {
962 printf("- Warning: %d protected sectors will not be erased!\n",
963 prot);
964 } else {
965 printf("\n");
966 }
967
John Otkend4024bb2007-07-26 17:49:11 +0200968 /* Disable interrupts which might cause a timeout here */
969 flag = disable_interrupts();
970
971 /* Start erase on unprotected sectors */
972 for (sect = s_first; sect <= s_last; sect++) {
973 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200974 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
John Otkend4024bb2007-07-26 17:49:11 +0200975
976 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200977 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
978 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
979 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080;
980 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
981 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
982 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x50505050; /* block erase */
John Otkend4024bb2007-07-26 17:49:11 +0200983 for (i = 0; i < 50; i++)
984 udelay(1000); /* wait 1 ms */
985 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200986 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
987 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
988 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080;
989 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
990 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
991 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x30303030; /* sector erase */
John Otkend4024bb2007-07-26 17:49:11 +0200992 }
John Otkend4024bb2007-07-26 17:49:11 +0200993 /*
994 * Wait for each sector to complete, it's more
995 * reliable. According to AMD Spec, you must
996 * issue all erase commands within a specified
997 * timeout. This has been seen to fail, especially
998 * if printf()s are included (for debug)!!
999 */
1000 wait_for_DQ7_2(info, sect);
1001 }
1002 }
1003
1004 /* re-enable interrupts if necessary */
1005 if (flag)
1006 enable_interrupts();
1007
1008 /* wait at least 80us - let's wait 1 ms */
1009 udelay(1000);
1010
1011 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001012 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
1013 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF0F0F0F0; /* reset bank */
John Otkend4024bb2007-07-26 17:49:11 +02001014
1015 printf(" done\n");
1016 return 0;
1017}
1018
1019static int write_word_2(flash_info_t * info, ulong dest, ulong data)
1020{
Wolfgang Denk030ec522009-09-11 11:44:39 +02001021 ulong *data_ptr = &data;
1022 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
1023 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
1024 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
John Otkend4024bb2007-07-26 17:49:11 +02001025 ulong start;
1026 int i;
1027
1028 /* Check if Flash is (sufficiently) erased */
1029 if ((*((vu_long *)dest) & data) != data) {
1030 return 2;
1031 }
1032
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001033 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
John Otkend4024bb2007-07-26 17:49:11 +02001034 int flag;
1035
1036 /* Disable interrupts which might cause a timeout here */
1037 flag = disable_interrupts();
1038
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001039 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
1040 addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
1041 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xA0A0A0A0;
John Otkend4024bb2007-07-26 17:49:11 +02001042
1043 dest2[i] = data2[i];
1044
1045 /* re-enable interrupts if necessary */
1046 if (flag)
1047 enable_interrupts();
1048
1049 /* data polling for D7 */
1050 start = get_timer(0);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001051 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) !=
1052 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080)) {
John Otkend4024bb2007-07-26 17:49:11 +02001053
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001054 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
John Otkend4024bb2007-07-26 17:49:11 +02001055 return 1;
1056 }
1057 }
1058 }
1059
1060 return 0;
1061}
1062
1063#endif /* FLASH_BASE1_PRELIM */