blob: 27cd4698edbcdb6064b36869609c594456c52ae9 [file] [log] [blame]
wdenkdb01a2e2004-04-15 23:14:49 +00001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Modified 4/5/2001
26 * Wait for completion of each sector erase command issued
27 * 4/5/2001
28 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
29 */
30
31#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020032#include <asm/ppc4xx.h>
wdenkdb01a2e2004-04-15 23:14:49 +000033#include <asm/processor.h>
34
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020035#if CONFIG_SYS_MAX_FLASH_BANKS != 1
36#error "CONFIG_SYS_MAX_FLASH_BANKS must be 1"
wdenkdb01a2e2004-04-15 23:14:49 +000037#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkdb01a2e2004-04-15 23:14:49 +000039
40/*-----------------------------------------------------------------------
41 * Functions
42 */
43static ulong flash_get_size (vu_long * addr, flash_info_t * info);
44static int write_word (flash_info_t * info, ulong dest, ulong data);
45static void flash_get_offsets (ulong base, flash_info_t * info);
46
wdenk6bdd1372004-06-09 00:15:33 +000047#define ADDR0 0x5555
48#define ADDR1 0x2aaa
wdenkdb01a2e2004-04-15 23:14:49 +000049#define FLASH_WORD_SIZE unsigned char
50
51/*-----------------------------------------------------------------------
52 */
53
54unsigned long flash_init (void)
55{
56 unsigned long size_b0;
wdenkdb01a2e2004-04-15 23:14:49 +000057
58 /* Init: no FLASHes known */
59 flash_info[0].flash_id = FLASH_UNKNOWN;
60
61 /* Static FLASH Bank configuration here - FIXME XXX */
62
wdenk08f10802004-04-25 16:40:11 +000063 size_b0 = flash_get_size ((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
wdenkdb01a2e2004-04-15 23:14:49 +000064
65 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
wdenk08f10802004-04-25 16:40:11 +000066 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
67 size_b0, size_b0 << 20);
wdenkdb01a2e2004-04-15 23:14:49 +000068 }
69
70 /* Only one bank */
71 /* Setup offsets */
72 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
73
74 /* Monitor protection ON by default */
75 (void) flash_protect (FLAG_PROTECT_SET,
76 FLASH_BASE0_PRELIM,
77 FLASH_BASE0_PRELIM + monitor_flash_len - 1,
78 &flash_info[0]);
79 flash_info[0].size = size_b0;
80
81 return size_b0;
82}
83
84
85/*-----------------------------------------------------------------------
86 */
wdenk6bdd1372004-06-09 00:15:33 +000087/*
88 * This implementation assumes that the flash chips are uniform sector
89 * devices. This is true for all likely JSE devices.
90 */
wdenkdb01a2e2004-04-15 23:14:49 +000091static void flash_get_offsets (ulong base, flash_info_t * info)
92{
wdenk6bdd1372004-06-09 00:15:33 +000093 unsigned idx;
94 unsigned long sector_size = info->size / info->sector_count;
wdenkdb01a2e2004-04-15 23:14:49 +000095
wdenk6bdd1372004-06-09 00:15:33 +000096 for (idx = 0; idx < info->sector_count; idx += 1) {
97 info->start[idx] = base + (idx * sector_size);
wdenkdb01a2e2004-04-15 23:14:49 +000098 }
99}
100
101/*-----------------------------------------------------------------------
102 */
103void flash_print_info (flash_info_t * info)
104{
105 int i;
106 int k;
107 int size;
108 int erased;
109 volatile unsigned long *flash;
110
111 if (info->flash_id == FLASH_UNKNOWN) {
112 printf ("missing or unknown FLASH type\n");
113 return;
114 }
115
116 switch (info->flash_id & FLASH_VENDMASK) {
117 case FLASH_MAN_AMD:
118 printf ("AMD ");
119 break;
120 case FLASH_MAN_FUJ:
121 printf ("FUJITSU ");
122 break;
123 case FLASH_MAN_SST:
124 printf ("SST ");
125 break;
wdenk6bdd1372004-06-09 00:15:33 +0000126 case FLASH_MAN_STM:
127 printf ("ST Micro ");
128 break;
wdenkdb01a2e2004-04-15 23:14:49 +0000129 default:
130 printf ("Unknown Vendor ");
131 break;
132 }
133
wdenk6bdd1372004-06-09 00:15:33 +0000134 /* (Reduced table of only parts expected in JSE boards.) */
135 switch (info->flash_id) {
136 case FLASH_MAN_AMD | FLASH_AM040:
wdenkdb01a2e2004-04-15 23:14:49 +0000137 printf ("AM29F040 (512 Kbit, uniform sector size)\n");
138 break;
wdenk6bdd1372004-06-09 00:15:33 +0000139 case FLASH_MAN_STM | FLASH_AM040:
140 printf ("MM29W040W (512 Kbit, uniform sector size)\n");
wdenkdb01a2e2004-04-15 23:14:49 +0000141 break;
142 default:
143 printf ("Unknown Chip Type\n");
144 break;
145 }
146
147 printf (" Size: %ld KB in %d Sectors\n",
148 info->size >> 10, info->sector_count);
149
150 printf (" Sector Start Addresses:");
151 for (i = 0; i < info->sector_count; ++i) {
152 /*
153 * Check if whole sector is erased
154 */
155 if (i != (info->sector_count - 1))
156 size = info->start[i + 1] - info->start[i];
157 else
158 size = info->start[0] + info->size - info->start[i];
159 erased = 1;
160 flash = (volatile unsigned long *) info->start[i];
161 size = size >> 2; /* divide by 4 for longword access */
162 for (k = 0; k < size; k++) {
163 if (*flash++ != 0xffffffff) {
164 erased = 0;
165 break;
166 }
167 }
168
169 if ((i % 5) == 0)
170 printf ("\n ");
wdenkdb01a2e2004-04-15 23:14:49 +0000171 printf (" %08lX%s%s",
172 info->start[i],
173 erased ? " E" : " ", info->protect[i] ? "RO " : " "
wdenk08f10802004-04-25 16:40:11 +0000174 );
wdenkdb01a2e2004-04-15 23:14:49 +0000175 }
176 printf ("\n");
177 return;
178}
179
180/*-----------------------------------------------------------------------
181 */
182
183
184/*-----------------------------------------------------------------------
185 */
186
187/*
188 * The following code cannot be run from FLASH!
189 */
190static ulong flash_get_size (vu_long * addr, flash_info_t * info)
191{
192 short i;
193 FLASH_WORD_SIZE value;
194 ulong base = (ulong) addr;
195 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) addr;
196
197 /* Write auto select command: read Manufacturer ID */
198 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
199 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
200 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00900090;
201
wdenkdb01a2e2004-04-15 23:14:49 +0000202 value = addr2[0];
wdenkdb01a2e2004-04-15 23:14:49 +0000203
204 switch (value) {
205 case (FLASH_WORD_SIZE) AMD_MANUFACT:
206 info->flash_id = FLASH_MAN_AMD;
207 break;
208 case (FLASH_WORD_SIZE) FUJ_MANUFACT:
209 info->flash_id = FLASH_MAN_FUJ;
210 break;
211 case (FLASH_WORD_SIZE) SST_MANUFACT:
212 info->flash_id = FLASH_MAN_SST;
213 break;
wdenk6bdd1372004-06-09 00:15:33 +0000214 case (FLASH_WORD_SIZE)STM_MANUFACT:
215 info->flash_id = FLASH_MAN_STM;
216 break;
wdenkdb01a2e2004-04-15 23:14:49 +0000217 default:
218 info->flash_id = FLASH_UNKNOWN;
219 info->sector_count = 0;
220 info->size = 0;
wdenk6bdd1372004-06-09 00:15:33 +0000221 printf("Unknown flash manufacturer code: 0x%x\n", value);
wdenkdb01a2e2004-04-15 23:14:49 +0000222 return (0); /* no or unknown flash */
223 }
224
wdenk6bdd1372004-06-09 00:15:33 +0000225 value = addr2[1]; /* device ID */
wdenkdb01a2e2004-04-15 23:14:49 +0000226
227 switch (value) {
228 case (FLASH_WORD_SIZE) AMD_ID_F040B:
229 info->flash_id += FLASH_AM040;
230 info->sector_count = 8;
231 info->size = 0x0080000; /* => 512 ko */
232 break;
233 case (FLASH_WORD_SIZE) AMD_ID_LV040B:
234 info->flash_id += FLASH_AM040;
235 info->sector_count = 8;
236 info->size = 0x0080000; /* => 512 ko */
237 break;
wdenk6bdd1372004-06-09 00:15:33 +0000238 case (FLASH_WORD_SIZE)STM_ID_M29W040B: /* most likele JSE chip */
239 info->flash_id += FLASH_AM040;
240 info->sector_count = 8;
241 info->size = 0x0080000; /* => 512 ko */
242 break;
wdenkdb01a2e2004-04-15 23:14:49 +0000243 default:
244 info->flash_id = FLASH_UNKNOWN;
245 return (0); /* => no or unknown flash */
246
247 }
248
wdenk6bdd1372004-06-09 00:15:33 +0000249 /* Calculate the sector offsets (Use JSE Optimized code). */
250 flash_get_offsets(base, info);
wdenkdb01a2e2004-04-15 23:14:49 +0000251
252 /* check for protected sectors */
253 for (i = 0; i < info->sector_count; i++) {
254 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
255 /* D0 = 1 if protected */
wdenkdb01a2e2004-04-15 23:14:49 +0000256 addr2 = (volatile FLASH_WORD_SIZE *) (info->start[i]);
257 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
258 info->protect[i] = 0;
259 else
260 info->protect[i] = addr2[2] & 1;
wdenkdb01a2e2004-04-15 23:14:49 +0000261 }
262
263 /*
264 * Prevent writes to uninitialized FLASH.
265 */
266 if (info->flash_id != FLASH_UNKNOWN) {
wdenkdb01a2e2004-04-15 23:14:49 +0000267 addr2 = (FLASH_WORD_SIZE *) info->start[0];
268 *addr2 = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
wdenkdb01a2e2004-04-15 23:14:49 +0000269 }
270
271 return (info->size);
272}
273
274int wait_for_DQ7 (flash_info_t * info, int sect)
275{
276 ulong start, now, last;
277 volatile FLASH_WORD_SIZE *addr =
278 (FLASH_WORD_SIZE *) (info->start[sect]);
279
280 start = get_timer (0);
281 last = start;
282 while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
283 (FLASH_WORD_SIZE) 0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200284 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkdb01a2e2004-04-15 23:14:49 +0000285 printf ("Timeout\n");
286 return -1;
287 }
288 /* show that we're waiting */
289 if ((now - last) > 1000) { /* every second */
290 putc ('.');
291 last = now;
292 }
293 }
294 return 0;
295}
296
297/*-----------------------------------------------------------------------
298 */
299
300int flash_erase (flash_info_t * info, int s_first, int s_last)
301{
302 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
303 volatile FLASH_WORD_SIZE *addr2;
Wolfgang Denk8e119b02011-10-29 09:37:40 +0000304 int flag, prot, sect;
wdenkdb01a2e2004-04-15 23:14:49 +0000305 int i;
306
307 if ((s_first < 0) || (s_first > s_last)) {
308 if (info->flash_id == FLASH_UNKNOWN) {
309 printf ("- missing\n");
310 } else {
311 printf ("- no sectors to erase\n");
312 }
313 return 1;
314 }
315
316 if (info->flash_id == FLASH_UNKNOWN) {
317 printf ("Can't erase unknown flash type - aborted\n");
318 return 1;
319 }
320
321 prot = 0;
322 for (sect = s_first; sect <= s_last; ++sect) {
323 if (info->protect[sect]) {
324 prot++;
325 }
326 }
327
328 if (prot) {
329 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
330 } else {
331 printf ("\n");
332 }
333
wdenkdb01a2e2004-04-15 23:14:49 +0000334 /* Disable interrupts which might cause a timeout here */
335 flag = disable_interrupts ();
336
337 /* Start erase on unprotected sectors */
338 for (sect = s_first; sect <= s_last; sect++) {
339 if (info->protect[sect] == 0) { /* not protected */
340 addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
341 printf ("Erasing sector %p\n", addr2); /* CLH */
342
343 if ((info->flash_id & FLASH_VENDMASK) ==
344 FLASH_MAN_SST) {
345 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
346 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
347 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
348 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
349 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
350 addr2[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
351 for (i = 0; i < 50; i++)
352 udelay (1000); /* wait 1 ms */
353 } else {
354 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
355 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
356 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
357 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
358 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
359 addr2[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
360 }
wdenkdb01a2e2004-04-15 23:14:49 +0000361 /*
362 * Wait for each sector to complete, it's more
363 * reliable. According to AMD Spec, you must
364 * issue all erase commands within a specified
365 * timeout. This has been seen to fail, especially
366 * if printf()s are included (for debug)!!
367 */
368 wait_for_DQ7 (info, sect);
369 }
370 }
371
372 /* re-enable interrupts if necessary */
373 if (flag)
374 enable_interrupts ();
375
376 /* wait at least 80us - let's wait 1 ms */
377 udelay (1000);
378
wdenkdb01a2e2004-04-15 23:14:49 +0000379 /* reset to read mode */
380 addr = (FLASH_WORD_SIZE *) info->start[0];
381 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
382
383 printf (" done\n");
384 return 0;
385}
386
387/*-----------------------------------------------------------------------
388 * Copy memory to flash, returns:
389 * 0 - OK
390 * 1 - write timeout
391 * 2 - Flash not erased
392 */
393
394int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
395{
396 ulong cp, wp, data;
397 int i, l, rc;
398
399 wp = (addr & ~3); /* get lower word aligned address */
400
401 /*
402 * handle unaligned start bytes
403 */
404 if ((l = addr - wp) != 0) {
405 data = 0;
406 for (i = 0, cp = wp; i < l; ++i, ++cp) {
407 data = (data << 8) | (*(uchar *) cp);
408 }
409 for (; i < 4 && cnt > 0; ++i) {
410 data = (data << 8) | *src++;
411 --cnt;
412 ++cp;
413 }
414 for (; cnt == 0 && i < 4; ++i, ++cp) {
415 data = (data << 8) | (*(uchar *) cp);
416 }
417
418 if ((rc = write_word (info, wp, data)) != 0) {
419 return (rc);
420 }
421 wp += 4;
422 }
423
424 /*
425 * handle word aligned part
426 */
427 while (cnt >= 4) {
428 data = 0;
429 for (i = 0; i < 4; ++i) {
430 data = (data << 8) | *src++;
431 }
432 if ((rc = write_word (info, wp, data)) != 0) {
433 return (rc);
434 }
435 wp += 4;
436 cnt -= 4;
437 }
438
439 if (cnt == 0) {
440 return (0);
441 }
442
443 /*
444 * handle unaligned tail bytes
445 */
446 data = 0;
447 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
448 data = (data << 8) | *src++;
449 --cnt;
450 }
451 for (; i < 4; ++i, ++cp) {
452 data = (data << 8) | (*(uchar *) cp);
453 }
454
455 return (write_word (info, wp, data));
456}
457
458/*-----------------------------------------------------------------------
459 * Write a word to Flash, returns:
460 * 0 - OK
461 * 1 - write timeout
462 * 2 - Flash not erased
463 */
464static int write_word (flash_info_t * info, ulong dest, ulong data)
465{
466 volatile FLASH_WORD_SIZE *addr2 =
467 (FLASH_WORD_SIZE *) (info->start[0]);
468 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
469 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
470 ulong start;
471 int i;
472
473 /* Check if Flash is (sufficiently) erased */
474 if ((*((volatile FLASH_WORD_SIZE *) dest) &
wdenk6bdd1372004-06-09 00:15:33 +0000475 (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
wdenkdb01a2e2004-04-15 23:14:49 +0000476 return (2);
477 }
478
479 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
480 int flag;
481
482 /* Disable interrupts which might cause a timeout here */
483 flag = disable_interrupts ();
484
485 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
486 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
487 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
488
489 dest2[i] = data2[i];
490
491 /* re-enable interrupts if necessary */
492 if (flag)
493 enable_interrupts ();
494
495 /* data polling for D7 */
496 start = get_timer (0);
497 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
498 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
499
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200500 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkdb01a2e2004-04-15 23:14:49 +0000501 return (1);
502 }
503 }
504 }
505
506 return (0);
507}