blob: f05adf9f1c23eafffd9af449fd5452dbef6e24ae [file] [log] [blame]
wdenk12f34242003-09-02 22:48:03 +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+
wdenk12f34242003-09-02 22:48:03 +00006 */
7
8#include <common.h>
wdenk12f34242003-09-02 22:48:03 +00009#include <asm/processor.h>
10
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020011flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk12f34242003-09-02 22:48:03 +000012
13/*-----------------------------------------------------------------------
14 * Functions
15 */
16static int write_word (flash_info_t *info, ulong dest, ulong data);
17
18/*-----------------------------------------------------------------------
19 */
20static void flash_get_offsets (ulong base, flash_info_t *info)
21{
22 int i;
23 short n;
24
25 /* set up sector start address table */
wdenk42d1f032003-10-15 23:53:47 +000026 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
wdenk12f34242003-09-02 22:48:03 +000027 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
28 for (i = 0; i < info->sector_count; i++)
29 info->start[i] = base + (i * 0x00010000);
wdenk42d1f032003-10-15 23:53:47 +000030 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
wdenk12f34242003-09-02 22:48:03 +000031 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
32 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
33 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
34 /* set sector offsets for bottom boot block type */
35 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
36 info->start[i] = base;
37 base += 8 << 10;
38 }
39 while (i < info->sector_count) { /* 64k regular sectors */
40 info->start[i] = base;
41 base += 64 << 10;
42 ++i;
43 }
wdenk42d1f032003-10-15 23:53:47 +000044 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
wdenk12f34242003-09-02 22:48:03 +000045 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
46 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
47 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
48 /* set sector offsets for top boot block type */
49 base += info->size;
50 i = info->sector_count;
51 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
52 base -= 8 << 10;
53 --i;
54 info->start[i] = base;
55 }
56 while (i > 0) { /* 64k regular sectors */
57 base -= 64 << 10;
58 --i;
59 info->start[i] = base;
60 }
wdenk42d1f032003-10-15 23:53:47 +000061 } else {
wdenk12f34242003-09-02 22:48:03 +000062 if (info->flash_id & FLASH_BTYPE) {
63 /* set sector offsets for bottom boot block type */
64 info->start[0] = base + 0x00000000;
65 info->start[1] = base + 0x00004000;
66 info->start[2] = base + 0x00006000;
67 info->start[3] = base + 0x00008000;
68 for (i = 4; i < info->sector_count; i++) {
69 info->start[i] = base + (i * 0x00010000) - 0x00030000;
70 }
71 } else {
72 /* set sector offsets for top boot block type */
73 i = info->sector_count - 1;
74 info->start[i--] = base + info->size - 0x00004000;
75 info->start[i--] = base + info->size - 0x00006000;
76 info->start[i--] = base + info->size - 0x00008000;
77 for (; i >= 0; i--) {
78 info->start[i] = base + i * 0x00010000;
79 }
80 }
81 }
82}
83
84/*-----------------------------------------------------------------------
85 */
86void flash_print_info (flash_info_t *info)
87{
88 int i;
wdenk42d1f032003-10-15 23:53:47 +000089 int k;
90 int size;
91 int erased;
92 volatile unsigned long *flash;
wdenk12f34242003-09-02 22:48:03 +000093
94 if (info->flash_id == FLASH_UNKNOWN) {
95 printf ("missing or unknown FLASH type\n");
96 return;
97 }
98
99 switch (info->flash_id & FLASH_VENDMASK) {
100 case FLASH_MAN_AMD: printf ("AMD "); break;
101 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
102 case FLASH_MAN_SST: printf ("SST "); break;
103 case FLASH_MAN_STM: printf ("ST "); break;
104 default: printf ("Unknown Vendor "); break;
105 }
106
107 switch (info->flash_id & FLASH_TYPEMASK) {
108 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
109 break;
110 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
111 break;
112 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
113 break;
114 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
115 break;
116 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
117 break;
118 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
119 break;
120 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
121 break;
122 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
123 break;
124 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
125 break;
126 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
127 break;
128 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
129 break;
130 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
131 break;
132 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
133 break;
134 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
135 break;
136 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
137 break;
138 case FLASH_STMW320DT: printf ("M29W320DT (32 M, top sector)\n");
139 break;
140 default: printf ("Unknown Chip Type\n");
141 break;
142 }
143
144 printf (" Size: %ld MB in %d Sectors\n",
145 info->size >> 20, info->sector_count);
146
147 printf (" Sector Start Addresses:");
148 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200149#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
wdenk42d1f032003-10-15 23:53:47 +0000150 /*
151 * Check if whole sector is erased
152 */
153 if (i != (info->sector_count-1))
154 size = info->start[i+1] - info->start[i];
155 else
156 size = info->start[0] + info->size - info->start[i];
157 erased = 1;
158 flash = (volatile unsigned long *)info->start[i];
159 size = size >> 2; /* divide by 4 for longword access */
160 for (k=0; k<size; k++)
161 {
162 if (*flash++ != 0xffffffff)
163 {
164 erased = 0;
165 break;
166 }
167 }
wdenk12f34242003-09-02 22:48:03 +0000168
169 if ((i % 5) == 0)
170 printf ("\n ");
wdenk42d1f032003-10-15 23:53:47 +0000171 /* print empty and read-only info */
wdenk12f34242003-09-02 22:48:03 +0000172 printf (" %08lX%s%s",
173 info->start[i],
174 erased ? " E" : " ",
175 info->protect[i] ? "RO " : " ");
176#else
177 if ((i % 5) == 0)
178 printf ("\n ");
179 printf (" %08lX%s",
180 info->start[i],
181 info->protect[i] ? " (RO)" : " ");
182#endif
183
184 }
185 printf ("\n");
186 return;
187}
188
189/*-----------------------------------------------------------------------
190 */
191
192
193/*-----------------------------------------------------------------------
194 */
195
196/*
197 * The following code cannot be run from FLASH!
198 */
199static ulong flash_get_size (vu_long *addr, flash_info_t *info)
200{
201 short i;
202 short n;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200203 CONFIG_SYS_FLASH_WORD_SIZE value;
wdenk12f34242003-09-02 22:48:03 +0000204 ulong base = (ulong)addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200205 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
wdenk12f34242003-09-02 22:48:03 +0000206
wdenke55ca7e2004-07-01 21:40:08 +0000207 debug("[%s, %d] Entering ...\n", __FUNCTION__, __LINE__);
208
wdenk12f34242003-09-02 22:48:03 +0000209 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200210 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
211 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
212 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
wdenk12f34242003-09-02 22:48:03 +0000213
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200214 value = addr2[CONFIG_SYS_FLASH_READ0];
wdenk12f34242003-09-02 22:48:03 +0000215
216 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200217 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
wdenk12f34242003-09-02 22:48:03 +0000218 info->flash_id = FLASH_MAN_AMD;
219 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200220 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
wdenk12f34242003-09-02 22:48:03 +0000221 info->flash_id = FLASH_MAN_FUJ;
222 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200223 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
wdenk12f34242003-09-02 22:48:03 +0000224 info->flash_id = FLASH_MAN_SST;
225 break;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200226 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_MANUFACT:
wdenk12f34242003-09-02 22:48:03 +0000227 info->flash_id = FLASH_MAN_STM;
228 break;
229 default:
230 info->flash_id = FLASH_UNKNOWN;
231 info->sector_count = 0;
232 info->size = 0;
233 return (0); /* no or unknown flash */
234 }
235
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200236 value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
wdenk12f34242003-09-02 22:48:03 +0000237
238 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200239 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
wdenk12f34242003-09-02 22:48:03 +0000240 info->flash_id += FLASH_AM400T;
241 info->sector_count = 11;
242 info->size = 0x00080000;
243 break; /* => 0.5 MB */
244
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200245 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
wdenk12f34242003-09-02 22:48:03 +0000246 info->flash_id += FLASH_AM400B;
247 info->sector_count = 11;
248 info->size = 0x00080000;
249 break; /* => 0.5 MB */
250
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200251 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
wdenk12f34242003-09-02 22:48:03 +0000252 info->flash_id += FLASH_AM800T;
253 info->sector_count = 19;
254 info->size = 0x00100000;
255 break; /* => 1 MB */
256
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200257 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
wdenk12f34242003-09-02 22:48:03 +0000258 info->flash_id += FLASH_AM800B;
259 info->sector_count = 19;
260 info->size = 0x00100000;
261 break; /* => 1 MB */
262
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200263 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
wdenk12f34242003-09-02 22:48:03 +0000264 info->flash_id += FLASH_AM160T;
265 info->sector_count = 35;
266 info->size = 0x00200000;
267 break; /* => 2 MB */
268
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200269 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
wdenk12f34242003-09-02 22:48:03 +0000270 info->flash_id += FLASH_AM160B;
271 info->sector_count = 35;
272 info->size = 0x00200000;
273 break; /* => 2 MB */
274
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200275 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
wdenk42d1f032003-10-15 23:53:47 +0000276 info->flash_id += FLASH_STMW320DT;
277 info->sector_count = 67;
wdenk12f34242003-09-02 22:48:03 +0000278 info->size = 0x00400000; break; /* => 4 MB */
279
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200280 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
wdenk42d1f032003-10-15 23:53:47 +0000281 info->flash_id += FLASH_AM320T;
282 info->sector_count = 71;
wdenk12f34242003-09-02 22:48:03 +0000283 info->size = 0x00400000; break; /* => 4 MB */
284
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200285 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
wdenk42d1f032003-10-15 23:53:47 +0000286 info->flash_id += FLASH_AM320B;
wdenk12f34242003-09-02 22:48:03 +0000287 info->sector_count = 71;
288 info->size = 0x00400000; break; /* => 4 MB */
289
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200290 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
wdenk42d1f032003-10-15 23:53:47 +0000291 info->flash_id += FLASH_AMDL322T;
292 info->sector_count = 71;
wdenk12f34242003-09-02 22:48:03 +0000293 info->size = 0x00400000; break; /* => 4 MB */
294
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200295 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
wdenk42d1f032003-10-15 23:53:47 +0000296 info->flash_id += FLASH_AMDL322B;
wdenk12f34242003-09-02 22:48:03 +0000297 info->sector_count = 71;
298 info->size = 0x00400000; break; /* => 4 MB */
299
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200300 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
wdenk42d1f032003-10-15 23:53:47 +0000301 info->flash_id += FLASH_AMDL323T;
wdenk12f34242003-09-02 22:48:03 +0000302 info->sector_count = 71;
303 info->size = 0x00400000; break; /* => 4 MB */
304
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200305 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
wdenk42d1f032003-10-15 23:53:47 +0000306 info->flash_id += FLASH_AMDL323B;
wdenk12f34242003-09-02 22:48:03 +0000307 info->sector_count = 71;
308 info->size = 0x00400000; break; /* => 4 MB */
309
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200310 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
wdenk42d1f032003-10-15 23:53:47 +0000311 info->flash_id += FLASH_AM640U;
wdenk12f34242003-09-02 22:48:03 +0000312 info->sector_count = 128;
313 info->size = 0x00800000; break; /* => 8 MB */
314
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200315 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
wdenk12f34242003-09-02 22:48:03 +0000316 info->flash_id += FLASH_SST800A;
317 info->sector_count = 16;
318 info->size = 0x00100000;
319 break; /* => 1 MB */
320
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200321 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
wdenk12f34242003-09-02 22:48:03 +0000322 info->flash_id += FLASH_SST160A;
323 info->sector_count = 32;
324 info->size = 0x00200000;
325 break; /* => 2 MB */
326
327 default:
328 info->flash_id = FLASH_UNKNOWN;
329 return (0); /* => no or unknown flash */
330
331 }
332
333 /* set up sector start address table */
wdenk42d1f032003-10-15 23:53:47 +0000334 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
wdenk12f34242003-09-02 22:48:03 +0000335 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
336 for (i = 0; i < info->sector_count; i++)
337 info->start[i] = base + (i * 0x00010000);
wdenk42d1f032003-10-15 23:53:47 +0000338 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
wdenk12f34242003-09-02 22:48:03 +0000339 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
340 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
341 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
342 /* set sector offsets for bottom boot block type */
343 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
344 info->start[i] = base;
345 base += 8 << 10;
346 }
347 while (i < info->sector_count) { /* 64k regular sectors */
348 info->start[i] = base;
349 base += 64 << 10;
350 ++i;
351 }
wdenk42d1f032003-10-15 23:53:47 +0000352 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
wdenk12f34242003-09-02 22:48:03 +0000353 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
354 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
355 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
356 /* set sector offsets for top boot block type */
357 base += info->size;
358 i = info->sector_count;
359 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
360 base -= 8 << 10;
361 --i;
362 info->start[i] = base;
363 }
364 while (i > 0) { /* 64k regular sectors */
365 base -= 64 << 10;
366 --i;
367 info->start[i] = base;
368 }
wdenk42d1f032003-10-15 23:53:47 +0000369 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
wdenk12f34242003-09-02 22:48:03 +0000370 /* set sector offsets for top boot block type */
371 base += info->size;
372 i = info->sector_count;
wdenk42d1f032003-10-15 23:53:47 +0000373 /* 1 x 16k boot sector */
wdenk12f34242003-09-02 22:48:03 +0000374 base -= 16 << 10;
wdenk42d1f032003-10-15 23:53:47 +0000375 --i;
wdenk12f34242003-09-02 22:48:03 +0000376 info->start[i] = base;
377 /* 2 x 8k boot sectors */
378 for (n=0; n<2; ++n) {
379 base -= 8 << 10;
380 --i;
381 info->start[i] = base;
382 }
wdenk42d1f032003-10-15 23:53:47 +0000383 /* 1 x 32k boot sector */
wdenk12f34242003-09-02 22:48:03 +0000384 base -= 32 << 10;
wdenk42d1f032003-10-15 23:53:47 +0000385 --i;
wdenk12f34242003-09-02 22:48:03 +0000386 info->start[i] = base;
387
388 while (i > 0) { /* 64k regular sectors */
389 base -= 64 << 10;
390 --i;
391 info->start[i] = base;
392 }
wdenk42d1f032003-10-15 23:53:47 +0000393 } else {
wdenk12f34242003-09-02 22:48:03 +0000394 if (info->flash_id & FLASH_BTYPE) {
395 /* set sector offsets for bottom boot block type */
396 info->start[0] = base + 0x00000000;
397 info->start[1] = base + 0x00004000;
398 info->start[2] = base + 0x00006000;
399 info->start[3] = base + 0x00008000;
400 for (i = 4; i < info->sector_count; i++) {
401 info->start[i] = base + (i * 0x00010000) - 0x00030000;
402 }
403 } else {
404 /* set sector offsets for top boot block type */
405 i = info->sector_count - 1;
406 info->start[i--] = base + info->size - 0x00004000;
407 info->start[i--] = base + info->size - 0x00006000;
408 info->start[i--] = base + info->size - 0x00008000;
409 for (; i >= 0; i--) {
410 info->start[i] = base + i * 0x00010000;
411 }
412 }
413 }
414
415 /* check for protected sectors */
416 for (i = 0; i < info->sector_count; i++) {
417 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
418 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200419 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
wdenk42d1f032003-10-15 23:53:47 +0000420 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
421 info->protect[i] = 0;
422 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200423 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
wdenk12f34242003-09-02 22:48:03 +0000424 }
425
426 /*
427 * Prevent writes to uninitialized FLASH.
428 */
429 if (info->flash_id != FLASH_UNKNOWN) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200430 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
431 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
wdenk12f34242003-09-02 22:48:03 +0000432 }
433
434 return (info->size);
435}
436
437
438/*-----------------------------------------------------------------------
439 */
440
441int flash_erase (flash_info_t *info, int s_first, int s_last)
442{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200443 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
444 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
wdenk12f34242003-09-02 22:48:03 +0000445 int flag, prot, sect, l_sect;
446 ulong start, now, last;
wdenk42d1f032003-10-15 23:53:47 +0000447 int i;
wdenk12f34242003-09-02 22:48:03 +0000448
449 if ((s_first < 0) || (s_first > s_last)) {
450 if (info->flash_id == FLASH_UNKNOWN) {
451 printf ("- missing\n");
452 } else {
453 printf ("- no sectors to erase\n");
454 }
455 return 1;
456 }
457
458 if (info->flash_id == FLASH_UNKNOWN) {
459 printf ("Can't erase unknown flash type - aborted\n");
460 return 1;
461 }
462
463 prot = 0;
464 for (sect=s_first; sect<=s_last; ++sect) {
465 if (info->protect[sect]) {
466 prot++;
467 }
468 }
469
470 if (prot) {
471 printf ("- Warning: %d protected sectors will not be erased!\n",
472 prot);
473 } else {
474 printf ("\n");
475 }
476
477 l_sect = -1;
478
479 /* Disable interrupts which might cause a timeout here */
480 flag = disable_interrupts();
481
482 /* Start erase on unprotected sectors */
483 for (sect = s_first; sect<=s_last; sect++) {
484 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200485 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
wdenk42d1f032003-10-15 23:53:47 +0000486 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200487 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
488 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
489 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
490 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
491 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
492 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
wdenk42d1f032003-10-15 23:53:47 +0000493 for (i=0; i<50; i++)
494 udelay(1000); /* wait 1 ms */
495 } else {
496 if (sect == s_first) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200497 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
498 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
499 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
500 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
501 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
wdenk42d1f032003-10-15 23:53:47 +0000502 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200503 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
wdenk42d1f032003-10-15 23:53:47 +0000504 }
wdenk12f34242003-09-02 22:48:03 +0000505 l_sect = sect;
506 }
507 }
508
509 /* re-enable interrupts if necessary */
510 if (flag)
511 enable_interrupts();
512
513 /* wait at least 80us - let's wait 1 ms */
514 udelay (1000);
515
516 /*
517 * We wait for the last triggered sector
518 */
519 if (l_sect < 0)
520 goto DONE;
521
522 start = get_timer (0);
523 last = start;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200524 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
525 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
526 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk12f34242003-09-02 22:48:03 +0000527 printf ("Timeout\n");
528 return 1;
529 }
530 /* show that we're waiting */
531 if ((now - last) > 1000) { /* every second */
532 putc ('.');
533 last = now;
534 }
535 }
536
537DONE:
538 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200539 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
540 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
wdenk12f34242003-09-02 22:48:03 +0000541
542 printf (" done\n");
543 return 0;
544}
545
546/*-----------------------------------------------------------------------
547 * Copy memory to flash, returns:
548 * 0 - OK
549 * 1 - write timeout
550 * 2 - Flash not erased
551 */
552
553int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
554{
555 ulong cp, wp, data;
556 int i, l, rc;
557
558 wp = (addr & ~3); /* get lower word aligned address */
559
560 /*
561 * handle unaligned start bytes
562 */
563 if ((l = addr - wp) != 0) {
564 data = 0;
565 for (i=0, cp=wp; i<l; ++i, ++cp) {
wdenk074cff02004-02-24 00:16:43 +0000566#ifdef CONFIG_B2
567 data = data | ((*(uchar *)cp)<<(8*i));
568#else
wdenk12f34242003-09-02 22:48:03 +0000569 data = (data << 8) | (*(uchar *)cp);
wdenk074cff02004-02-24 00:16:43 +0000570#endif
wdenk12f34242003-09-02 22:48:03 +0000571 }
572 for (; i<4 && cnt>0; ++i) {
wdenk074cff02004-02-24 00:16:43 +0000573#ifdef CONFIG_B2
574 data = data | ((*src++)<<(8*i));
575#else
wdenk12f34242003-09-02 22:48:03 +0000576 data = (data << 8) | *src++;
wdenk074cff02004-02-24 00:16:43 +0000577#endif
wdenk12f34242003-09-02 22:48:03 +0000578 --cnt;
579 ++cp;
580 }
581 for (; cnt==0 && i<4; ++i, ++cp) {
wdenk074cff02004-02-24 00:16:43 +0000582#ifdef CONFIG_B2
583 data = data | ((*(uchar *)cp)<<(8*i));
584#else
wdenk12f34242003-09-02 22:48:03 +0000585 data = (data << 8) | (*(uchar *)cp);
wdenk074cff02004-02-24 00:16:43 +0000586#endif
wdenk12f34242003-09-02 22:48:03 +0000587 }
588
589 if ((rc = write_word(info, wp, data)) != 0) {
590 return (rc);
591 }
592 wp += 4;
593 }
594
595 /*
596 * handle word aligned part
597 */
598 while (cnt >= 4) {
599 data = 0;
wdenk074cff02004-02-24 00:16:43 +0000600#ifdef CONFIG_B2
601 data = (*(ulong*)src);
602 src += 4;
603#else
wdenk12f34242003-09-02 22:48:03 +0000604 for (i=0; i<4; ++i) {
605 data = (data << 8) | *src++;
606 }
wdenk074cff02004-02-24 00:16:43 +0000607#endif
wdenk12f34242003-09-02 22:48:03 +0000608 if ((rc = write_word(info, wp, data)) != 0) {
609 return (rc);
610 }
611 wp += 4;
612 cnt -= 4;
613 }
614
615 if (cnt == 0) {
616 return (0);
617 }
618
619 /*
620 * handle unaligned tail bytes
621 */
622 data = 0;
623 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
wdenk074cff02004-02-24 00:16:43 +0000624#ifdef CONFIG_B2
625 data = data | ((*src++)<<(8*i));
626#else
wdenk12f34242003-09-02 22:48:03 +0000627 data = (data << 8) | *src++;
wdenk074cff02004-02-24 00:16:43 +0000628#endif
wdenk12f34242003-09-02 22:48:03 +0000629 --cnt;
630 }
631 for (; i<4; ++i, ++cp) {
wdenk074cff02004-02-24 00:16:43 +0000632#ifdef CONFIG_B2
633 data = data | ((*(uchar *)cp)<<(8*i));
634#else
wdenk12f34242003-09-02 22:48:03 +0000635 data = (data << 8) | (*(uchar *)cp);
wdenk074cff02004-02-24 00:16:43 +0000636#endif
wdenk12f34242003-09-02 22:48:03 +0000637 }
638
639 return (write_word(info, wp, data));
640}
641
642/*-----------------------------------------------------------------------
643 * Write a word to Flash, returns:
644 * 0 - OK
645 * 1 - write timeout
646 * 2 - Flash not erased
647 */
648static int write_word (flash_info_t *info, ulong dest, ulong data)
649{
Wolfgang Denk51688012009-09-11 11:28:21 +0200650 ulong *data_ptr = &data;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200651 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
652 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
Wolfgang Denk51688012009-09-11 11:28:21 +0200653 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
wdenk12f34242003-09-02 22:48:03 +0000654 ulong start;
655 int flag;
wdenk42d1f032003-10-15 23:53:47 +0000656 int i;
wdenk12f34242003-09-02 22:48:03 +0000657
658 /* Check if Flash is (sufficiently) erased */
wdenk998eaae2004-04-18 19:43:36 +0000659 if ((*((volatile ulong *)dest) & data) != data) {
wdenk12f34242003-09-02 22:48:03 +0000660 return (2);
661 }
662 /* Disable interrupts which might cause a timeout here */
663 flag = disable_interrupts();
664
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200665 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
wdenk42d1f032003-10-15 23:53:47 +0000666 {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200667 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
668 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
669 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
wdenk12f34242003-09-02 22:48:03 +0000670
wdenk42d1f032003-10-15 23:53:47 +0000671 dest2[i] = data2[i];
wdenk12f34242003-09-02 22:48:03 +0000672
wdenk42d1f032003-10-15 23:53:47 +0000673 /* re-enable interrupts if necessary */
674 if (flag)
675 enable_interrupts();
wdenk12f34242003-09-02 22:48:03 +0000676
wdenk42d1f032003-10-15 23:53:47 +0000677 /* data polling for D7 */
678 start = get_timer (0);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200679 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
680 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
681 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk42d1f032003-10-15 23:53:47 +0000682 return (1);
683 }
684 }
685 }
wdenk12f34242003-09-02 22:48:03 +0000686
687 return (0);
688}
689
690/*-----------------------------------------------------------------------
691 */