blob: e3512c78da6fffb2d8c7e0c6b428d33e639d9bd6 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
Stefan Roesef2302d42008-08-06 14:05:38 +02009#ifdef __PPC__
Stefan Roeseb36df562010-09-09 19:18:00 +020010#include <asm/ppc4xx.h>
Stefan Roesef2302d42008-08-06 14:05:38 +020011#endif
wdenkaffae2b2002-08-17 09:36:01 +000012#include <asm/processor.h>
13
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020014flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkaffae2b2002-08-17 09:36:01 +000015
16/*-----------------------------------------------------------------------
17 * Functions
18 */
19static int write_word (flash_info_t *info, ulong dest, ulong data);
20
21/*-----------------------------------------------------------------------
22 */
23static void flash_get_offsets (ulong base, flash_info_t *info)
24{
25 int i;
26 short n;
27
28 /* set up sector start address table */
wdenk8bde7f72003-06-27 21:31:46 +000029 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
wdenkaffae2b2002-08-17 09:36:01 +000030 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
31 for (i = 0; i < info->sector_count; i++)
32 info->start[i] = base + (i * 0x00010000);
wdenk8bde7f72003-06-27 21:31:46 +000033 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
wdenkaffae2b2002-08-17 09:36:01 +000034 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
35 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
36 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
37 /* set sector offsets for bottom boot block type */
38 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
39 info->start[i] = base;
40 base += 8 << 10;
41 }
42 while (i < info->sector_count) { /* 64k regular sectors */
43 info->start[i] = base;
44 base += 64 << 10;
45 ++i;
46 }
wdenk8bde7f72003-06-27 21:31:46 +000047 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
wdenkaffae2b2002-08-17 09:36:01 +000048 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
49 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
50 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
51 /* set sector offsets for top boot block type */
52 base += info->size;
53 i = info->sector_count;
54 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
55 base -= 8 << 10;
56 --i;
57 info->start[i] = base;
58 }
59 while (i > 0) { /* 64k regular sectors */
60 base -= 64 << 10;
61 --i;
62 info->start[i] = base;
63 }
wdenk8bde7f72003-06-27 21:31:46 +000064 } else {
wdenkaffae2b2002-08-17 09:36:01 +000065 if (info->flash_id & FLASH_BTYPE) {
66 /* set sector offsets for bottom boot block type */
67 info->start[0] = base + 0x00000000;
68 info->start[1] = base + 0x00004000;
69 info->start[2] = base + 0x00006000;
70 info->start[3] = base + 0x00008000;
71 for (i = 4; i < info->sector_count; i++) {
72 info->start[i] = base + (i * 0x00010000) - 0x00030000;
73 }
74 } else {
75 /* set sector offsets for top boot block type */
76 i = info->sector_count - 1;
77 info->start[i--] = base + info->size - 0x00004000;
78 info->start[i--] = base + info->size - 0x00006000;
79 info->start[i--] = base + info->size - 0x00008000;
80 for (; i >= 0; i--) {
81 info->start[i] = base + i * 0x00010000;
82 }
83 }
84 }
85}
86
87/*-----------------------------------------------------------------------
88 */
89void flash_print_info (flash_info_t *info)
90{
91 int i;
wdenk8bde7f72003-06-27 21:31:46 +000092 int k;
93 int size;
94 int erased;
95 volatile unsigned long *flash;
wdenkaffae2b2002-08-17 09:36:01 +000096
97 if (info->flash_id == FLASH_UNKNOWN) {
98 printf ("missing or unknown FLASH type\n");
99 return;
100 }
101
102 switch (info->flash_id & FLASH_VENDMASK) {
103 case FLASH_MAN_AMD: printf ("AMD "); break;
104 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
105 case FLASH_MAN_SST: printf ("SST "); break;
stroese0621f6f2004-12-16 18:43:13 +0000106 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
wdenkaffae2b2002-08-17 09:36:01 +0000107 default: printf ("Unknown Vendor "); break;
108 }
109
110 switch (info->flash_id & FLASH_TYPEMASK) {
111 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
112 break;
113 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
114 break;
115 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
116 break;
117 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
118 break;
119 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
120 break;
121 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
122 break;
123 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
124 break;
125 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
126 break;
127 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
128 break;
129 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
130 break;
131 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
132 break;
133 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
134 break;
135 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
136 break;
137 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
138 break;
139 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
140 break;
stroese0621f6f2004-12-16 18:43:13 +0000141 case FLASH_SST320: printf ("SST39LF/VF320 (32 Mbit, uniform sector size)\n");
142 break;
143 case FLASH_SST640: printf ("SST39LF/VF640 (64 Mbit, uniform sector size)\n");
144 break;
wdenkaffae2b2002-08-17 09:36:01 +0000145 default: printf ("Unknown Chip Type\n");
146 break;
147 }
148
149 printf (" Size: %ld MB in %d Sectors\n",
150 info->size >> 20, info->sector_count);
151
152 printf (" Sector Start Addresses:");
153 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
wdenk8bde7f72003-06-27 21:31:46 +0000155 /*
156 * Check if whole sector is erased
157 */
158 if (i != (info->sector_count-1))
159 size = info->start[i+1] - info->start[i];
160 else
161 size = info->start[0] + info->size - info->start[i];
162 erased = 1;
163 flash = (volatile unsigned long *)info->start[i];
164 size = size >> 2; /* divide by 4 for longword access */
165 for (k=0; k<size; k++)
166 {
167 if (*flash++ != 0xffffffff)
168 {
169 erased = 0;
170 break;
171 }
172 }
wdenkaffae2b2002-08-17 09:36:01 +0000173
174 if ((i % 5) == 0)
175 printf ("\n ");
wdenk8bde7f72003-06-27 21:31:46 +0000176 /* print empty and read-only info */
wdenkaffae2b2002-08-17 09:36:01 +0000177 printf (" %08lX%s%s",
178 info->start[i],
179 erased ? " E" : " ",
180 info->protect[i] ? "RO " : " ");
181#else
182 if ((i % 5) == 0)
183 printf ("\n ");
184 printf (" %08lX%s",
185 info->start[i],
186 info->protect[i] ? " (RO)" : " ");
187#endif
188
189 }
190 printf ("\n");
191 return;
192}
193
194/*-----------------------------------------------------------------------
195 */
196
197
198/*-----------------------------------------------------------------------
199 */
200
201/*
202 * The following code cannot be run from FLASH!
203 */
204static ulong flash_get_size (vu_long *addr, flash_info_t *info)
205{
206 short i;
207 short n;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200208 CONFIG_SYS_FLASH_WORD_SIZE value;
wdenkaffae2b2002-08-17 09:36:01 +0000209 ulong base = (ulong)addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200210 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
wdenkaffae2b2002-08-17 09:36:01 +0000211
212 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200213 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
214 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
215 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
wdenkaffae2b2002-08-17 09:36:01 +0000216
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200217 value = addr2[CONFIG_SYS_FLASH_READ0];
wdenkaffae2b2002-08-17 09:36:01 +0000218
219 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200220 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
wdenkaffae2b2002-08-17 09:36:01 +0000221 info->flash_id = FLASH_MAN_AMD;
222 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200223 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
wdenkaffae2b2002-08-17 09:36:01 +0000224 info->flash_id = FLASH_MAN_FUJ;
225 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200226 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
wdenkaffae2b2002-08-17 09:36:01 +0000227 info->flash_id = FLASH_MAN_SST;
228 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200229 case (CONFIG_SYS_FLASH_WORD_SIZE)EXCEL_MANUFACT:
stroese0621f6f2004-12-16 18:43:13 +0000230 info->flash_id = FLASH_MAN_EXCEL;
231 break;
wdenkaffae2b2002-08-17 09:36:01 +0000232 default:
233 info->flash_id = FLASH_UNKNOWN;
234 info->sector_count = 0;
235 info->size = 0;
236 return (0); /* no or unknown flash */
237 }
238
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200239 value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
wdenkaffae2b2002-08-17 09:36:01 +0000240
241 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200242 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
wdenkaffae2b2002-08-17 09:36:01 +0000243 info->flash_id += FLASH_AM400T;
244 info->sector_count = 11;
245 info->size = 0x00080000;
246 break; /* => 0.5 MB */
247
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200248 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
wdenkaffae2b2002-08-17 09:36:01 +0000249 info->flash_id += FLASH_AM400B;
250 info->sector_count = 11;
251 info->size = 0x00080000;
252 break; /* => 0.5 MB */
253
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200254 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
wdenkaffae2b2002-08-17 09:36:01 +0000255 info->flash_id += FLASH_AM800T;
256 info->sector_count = 19;
257 info->size = 0x00100000;
258 break; /* => 1 MB */
259
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200260 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
wdenkaffae2b2002-08-17 09:36:01 +0000261 info->flash_id += FLASH_AM800B;
262 info->sector_count = 19;
263 info->size = 0x00100000;
264 break; /* => 1 MB */
265
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200266 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
wdenkaffae2b2002-08-17 09:36:01 +0000267 info->flash_id += FLASH_AM160T;
268 info->sector_count = 35;
269 info->size = 0x00200000;
270 break; /* => 2 MB */
271
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200272 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
wdenkaffae2b2002-08-17 09:36:01 +0000273 info->flash_id += FLASH_AM160B;
274 info->sector_count = 35;
275 info->size = 0x00200000;
276 break; /* => 2 MB */
277
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200278 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
wdenk8bde7f72003-06-27 21:31:46 +0000279 info->flash_id += FLASH_AM320T;
280 info->sector_count = 71;
wdenkaffae2b2002-08-17 09:36:01 +0000281 info->size = 0x00400000; break; /* => 4 MB */
282
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200283 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
wdenk8bde7f72003-06-27 21:31:46 +0000284 info->flash_id += FLASH_AM320B;
wdenkaffae2b2002-08-17 09:36:01 +0000285 info->sector_count = 71;
286 info->size = 0x00400000; break; /* => 4 MB */
287
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200288 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
wdenk8bde7f72003-06-27 21:31:46 +0000289 info->flash_id += FLASH_AMDL322T;
290 info->sector_count = 71;
wdenkaffae2b2002-08-17 09:36:01 +0000291 info->size = 0x00400000; break; /* => 4 MB */
292
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200293 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
wdenk8bde7f72003-06-27 21:31:46 +0000294 info->flash_id += FLASH_AMDL322B;
wdenkaffae2b2002-08-17 09:36:01 +0000295 info->sector_count = 71;
296 info->size = 0x00400000; break; /* => 4 MB */
297
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200298 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
wdenk8bde7f72003-06-27 21:31:46 +0000299 info->flash_id += FLASH_AMDL323T;
wdenkaffae2b2002-08-17 09:36:01 +0000300 info->sector_count = 71;
301 info->size = 0x00400000; break; /* => 4 MB */
302
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200303 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
wdenk8bde7f72003-06-27 21:31:46 +0000304 info->flash_id += FLASH_AMDL323B;
wdenkaffae2b2002-08-17 09:36:01 +0000305 info->sector_count = 71;
306 info->size = 0x00400000; break; /* => 4 MB */
307
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200308 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
wdenk8bde7f72003-06-27 21:31:46 +0000309 info->flash_id += FLASH_AM640U;
wdenkaffae2b2002-08-17 09:36:01 +0000310 info->sector_count = 128;
311 info->size = 0x00800000; break; /* => 8 MB */
312
stroese0621f6f2004-12-16 18:43:13 +0000313#if !(defined(CONFIG_ADCIOP) || defined(CONFIG_DASA_SIM))
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200314 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
wdenkaffae2b2002-08-17 09:36:01 +0000315 info->flash_id += FLASH_SST800A;
316 info->sector_count = 16;
317 info->size = 0x00100000;
318 break; /* => 1 MB */
319
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200320 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
321 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF1601:
322 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF1602:
wdenkaffae2b2002-08-17 09:36:01 +0000323 info->flash_id += FLASH_SST160A;
324 info->sector_count = 32;
325 info->size = 0x00200000;
326 break; /* => 2 MB */
327
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200328 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF3201:
329 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF3202:
stroese0621f6f2004-12-16 18:43:13 +0000330 info->flash_id += FLASH_SST320;
331 info->sector_count = 64;
332 info->size = 0x00400000;
333 break; /* => 4 MB */
334
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200335 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF6401:
336 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF6402:
stroese0621f6f2004-12-16 18:43:13 +0000337 info->flash_id += FLASH_SST640;
338 info->sector_count = 128;
339 info->size = 0x00800000;
340 break; /* => 8 MB */
341#endif
342
wdenkaffae2b2002-08-17 09:36:01 +0000343 default:
344 info->flash_id = FLASH_UNKNOWN;
345 return (0); /* => no or unknown flash */
346
347 }
348
349 /* set up sector start address table */
wdenk8bde7f72003-06-27 21:31:46 +0000350 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
wdenkaffae2b2002-08-17 09:36:01 +0000351 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
352 for (i = 0; i < info->sector_count; i++)
353 info->start[i] = base + (i * 0x00010000);
wdenk8bde7f72003-06-27 21:31:46 +0000354 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
wdenkaffae2b2002-08-17 09:36:01 +0000355 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
356 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
357 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
358 /* set sector offsets for bottom boot block type */
359 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
360 info->start[i] = base;
361 base += 8 << 10;
362 }
363 while (i < info->sector_count) { /* 64k regular sectors */
364 info->start[i] = base;
365 base += 64 << 10;
366 ++i;
367 }
wdenk8bde7f72003-06-27 21:31:46 +0000368 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
wdenkaffae2b2002-08-17 09:36:01 +0000369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
370 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
371 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
372 /* set sector offsets for top boot block type */
373 base += info->size;
374 i = info->sector_count;
375 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
376 base -= 8 << 10;
377 --i;
378 info->start[i] = base;
379 }
380 while (i > 0) { /* 64k regular sectors */
381 base -= 64 << 10;
382 --i;
383 info->start[i] = base;
384 }
wdenk8bde7f72003-06-27 21:31:46 +0000385 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000386 if (info->flash_id & FLASH_BTYPE) {
387 /* set sector offsets for bottom boot block type */
388 info->start[0] = base + 0x00000000;
389 info->start[1] = base + 0x00004000;
390 info->start[2] = base + 0x00006000;
391 info->start[3] = base + 0x00008000;
392 for (i = 4; i < info->sector_count; i++) {
393 info->start[i] = base + (i * 0x00010000) - 0x00030000;
394 }
395 } else {
396 /* set sector offsets for top boot block type */
397 i = info->sector_count - 1;
398 info->start[i--] = base + info->size - 0x00004000;
399 info->start[i--] = base + info->size - 0x00006000;
400 info->start[i--] = base + info->size - 0x00008000;
401 for (; i >= 0; i--) {
402 info->start[i] = base + i * 0x00010000;
403 }
404 }
405 }
406
407 /* check for protected sectors */
408 for (i = 0; i < info->sector_count; i++) {
409 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
410 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200411 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
stroese0621f6f2004-12-16 18:43:13 +0000412 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
wdenk8bde7f72003-06-27 21:31:46 +0000413 info->protect[i] = 0;
414 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200415 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
wdenkaffae2b2002-08-17 09:36:01 +0000416 }
417
418 /*
419 * Prevent writes to uninitialized FLASH.
420 */
421 if (info->flash_id != FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200422 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
423 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000424 }
425
426 return (info->size);
427}
428
429
430/*-----------------------------------------------------------------------
431 */
432
433int flash_erase (flash_info_t *info, int s_first, int s_last)
434{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200435 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
436 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
wdenkaffae2b2002-08-17 09:36:01 +0000437 int flag, prot, sect, l_sect;
438 ulong start, now, last;
wdenk8bde7f72003-06-27 21:31:46 +0000439 int i;
wdenkaffae2b2002-08-17 09:36:01 +0000440
441 if ((s_first < 0) || (s_first > s_last)) {
442 if (info->flash_id == FLASH_UNKNOWN) {
443 printf ("- missing\n");
444 } else {
445 printf ("- no sectors to erase\n");
446 }
447 return 1;
448 }
449
450 if (info->flash_id == FLASH_UNKNOWN) {
451 printf ("Can't erase unknown flash type - aborted\n");
452 return 1;
453 }
454
455 prot = 0;
456 for (sect=s_first; sect<=s_last; ++sect) {
457 if (info->protect[sect]) {
458 prot++;
459 }
460 }
461
462 if (prot) {
463 printf ("- Warning: %d protected sectors will not be erased!\n",
464 prot);
465 } else {
466 printf ("\n");
467 }
468
469 l_sect = -1;
470
471 /* Disable interrupts which might cause a timeout here */
472 flag = disable_interrupts();
473
474 /* Start erase on unprotected sectors */
475 for (sect = s_first; sect<=s_last; sect++) {
476 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200477 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
wdenk8bde7f72003-06-27 21:31:46 +0000478 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200479 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
480 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
481 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
482 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
483 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
484 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
wdenk8bde7f72003-06-27 21:31:46 +0000485 for (i=0; i<50; i++)
486 udelay(1000); /* wait 1 ms */
487 } else {
488 if (sect == s_first) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200489 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
490 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
491 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
492 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
493 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
wdenk8bde7f72003-06-27 21:31:46 +0000494 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200495 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
wdenk8bde7f72003-06-27 21:31:46 +0000496 }
wdenkaffae2b2002-08-17 09:36:01 +0000497 l_sect = sect;
498 }
499 }
500
501 /* re-enable interrupts if necessary */
502 if (flag)
503 enable_interrupts();
504
505 /* wait at least 80us - let's wait 1 ms */
506 udelay (1000);
507
508 /*
509 * We wait for the last triggered sector
510 */
511 if (l_sect < 0)
512 goto DONE;
513
514 start = get_timer (0);
515 last = start;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200516 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
517 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
518 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkaffae2b2002-08-17 09:36:01 +0000519 printf ("Timeout\n");
520 return 1;
521 }
522 /* show that we're waiting */
523 if ((now - last) > 1000) { /* every second */
524 putc ('.');
525 last = now;
526 }
527 }
528
529DONE:
530 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200531 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
532 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
wdenkaffae2b2002-08-17 09:36:01 +0000533
534 printf (" done\n");
535 return 0;
536}
537
538/*-----------------------------------------------------------------------
539 * Copy memory to flash, returns:
540 * 0 - OK
541 * 1 - write timeout
542 * 2 - Flash not erased
543 */
544
545int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
546{
547 ulong cp, wp, data;
548 int i, l, rc;
549
550 wp = (addr & ~3); /* get lower word aligned address */
551
552 /*
553 * handle unaligned start bytes
554 */
555 if ((l = addr - wp) != 0) {
556 data = 0;
557 for (i=0, cp=wp; i<l; ++i, ++cp) {
558 data = (data << 8) | (*(uchar *)cp);
559 }
560 for (; i<4 && cnt>0; ++i) {
561 data = (data << 8) | *src++;
562 --cnt;
563 ++cp;
564 }
565 for (; cnt==0 && i<4; ++i, ++cp) {
566 data = (data << 8) | (*(uchar *)cp);
567 }
568
569 if ((rc = write_word(info, wp, data)) != 0) {
570 return (rc);
571 }
572 wp += 4;
573 }
574
575 /*
576 * handle word aligned part
577 */
578 while (cnt >= 4) {
579 data = 0;
580 for (i=0; i<4; ++i) {
581 data = (data << 8) | *src++;
582 }
583 if ((rc = write_word(info, wp, data)) != 0) {
584 return (rc);
585 }
586 wp += 4;
587 cnt -= 4;
588 }
589
590 if (cnt == 0) {
591 return (0);
592 }
593
594 /*
595 * handle unaligned tail bytes
596 */
597 data = 0;
598 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
599 data = (data << 8) | *src++;
600 --cnt;
601 }
602 for (; i<4; ++i, ++cp) {
603 data = (data << 8) | (*(uchar *)cp);
604 }
605
606 return (write_word(info, wp, data));
607}
608
609/*-----------------------------------------------------------------------
610 * Write a word to Flash, returns:
611 * 0 - OK
612 * 1 - write timeout
613 * 2 - Flash not erased
614 */
615static int write_word (flash_info_t *info, ulong dest, ulong data)
616{
Wolfgang Denkba730602009-09-11 10:13:26 +0200617 ulong *data_ptr = &data;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200618 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
619 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
Wolfgang Denkba730602009-09-11 10:13:26 +0200620 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
wdenkaffae2b2002-08-17 09:36:01 +0000621 ulong start;
622 int flag;
wdenk8bde7f72003-06-27 21:31:46 +0000623 int i;
wdenkaffae2b2002-08-17 09:36:01 +0000624
625 /* Check if Flash is (sufficiently) erased */
stroese0621f6f2004-12-16 18:43:13 +0000626 if ((*((vu_long *)dest) & data) != data) {
wdenkaffae2b2002-08-17 09:36:01 +0000627 return (2);
628 }
stroese0621f6f2004-12-16 18:43:13 +0000629
wdenkaffae2b2002-08-17 09:36:01 +0000630 /* Disable interrupts which might cause a timeout here */
631 flag = disable_interrupts();
632
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200633 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
wdenk8bde7f72003-06-27 21:31:46 +0000634 {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200635 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
636 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
637 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
wdenkaffae2b2002-08-17 09:36:01 +0000638
wdenk8bde7f72003-06-27 21:31:46 +0000639 dest2[i] = data2[i];
wdenkaffae2b2002-08-17 09:36:01 +0000640
wdenk8bde7f72003-06-27 21:31:46 +0000641 /* re-enable interrupts if necessary */
642 if (flag)
643 enable_interrupts();
wdenkaffae2b2002-08-17 09:36:01 +0000644
wdenk8bde7f72003-06-27 21:31:46 +0000645 /* data polling for D7 */
646 start = get_timer (0);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200647 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
648 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
649 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk8bde7f72003-06-27 21:31:46 +0000650 return (1);
651 }
652 }
653 }
wdenkaffae2b2002-08-17 09:36:01 +0000654
655 return (0);
656}
657
658/*-----------------------------------------------------------------------
659 */