blob: c9dfb4d142c4621bd4a770c19cb5b644cf25a560 [file] [log] [blame]
wdenk42d1f032003-10-15 23:53:47 +00001/*
2 * (C) Copyright 2003 Motorola Inc.
3 * Xianghua Xiao,(X.Xiao@motorola.com)
4 *
5 * (C) Copyright 2000, 2001
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
9 * Add support the Sharp chips on the mpc8260ads.
10 * I started with board/ip860/flash.c and made changes I found in
11 * the MTD project by David Schleef.
12 *
13 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 */
31
32#include <common.h>
33
34#if !defined(CFG_NO_FLASH)
35
36flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
37
38#if defined(CFG_ENV_IS_IN_FLASH)
39# ifndef CFG_ENV_ADDR
40# define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
41# endif
42# ifndef CFG_ENV_SIZE
43# define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
44# endif
45# ifndef CFG_ENV_SECT_SIZE
46# define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
47# endif
48#endif
49
50#undef DEBUG
51
52/*-----------------------------------------------------------------------
53 * Functions
54 */
55static ulong flash_get_size (vu_long *addr, flash_info_t *info);
56static int write_word (flash_info_t *info, ulong dest, ulong data);
57static int clear_block_lock_bit(vu_long * addr);
58/*-----------------------------------------------------------------------
59 */
60
61unsigned long flash_init (void)
62{
63 unsigned long size;
64 int i;
65
66 /* Init: enable write,
67 * or we cannot even write flash commands
68 */
69 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
70 flash_info[i].flash_id = FLASH_UNKNOWN;
71
72 /* set the default sector offset */
73 }
74
75 /* Static FLASH Bank configuration here - FIXME XXX */
76
77 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
78
79 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
80 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
81 size, size<<20);
82 }
83
84 /* Re-do sizing to get full correct info */
85 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
86
87 flash_info[0].size = size;
88
89#if !defined(CONFIG_RAM_AS_FLASH)
90#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
91 /* monitor protection ON by default */
92 flash_protect(FLAG_PROTECT_SET,
93 CFG_MONITOR_BASE,
94 CFG_MONITOR_BASE+monitor_flash_len-1,
95 &flash_info[0]);
96#endif
97
98#ifdef CFG_ENV_IS_IN_FLASH
99 /* ENV protection ON by default */
100 flash_protect(FLAG_PROTECT_SET,
101 CFG_ENV_ADDR,
102 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
103 &flash_info[0]);
104#endif
105#endif
106 return (size);
107}
108
109/*-----------------------------------------------------------------------
110 */
111void flash_print_info (flash_info_t *info)
112{
113 int i;
114
115 if (info->flash_id == FLASH_UNKNOWN) {
116 printf ("missing or unknown FLASH type\n");
117 return;
118 }
119
120 switch (info->flash_id & FLASH_VENDMASK) {
121 case FLASH_MAN_INTEL: printf ("Intel "); break;
122 case FLASH_MAN_SHARP: printf ("Sharp "); break;
123 default: printf ("Unknown Vendor "); break;
124 }
125
126 switch (info->flash_id & FLASH_TYPEMASK) {
127 case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
128 break;
129 case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
130 break;
131 case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
132 break;
133 case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
134 break;
135 case FLASH_28F640J3A: printf ("28F640J3A (64 Mbit, 64 x 128K)\n");
136 break;
137 default: printf ("Unknown Chip Type\n");
138 break;
139 }
140
141 printf (" Size: %ld MB in %d Sectors\n",
142 info->size >> 20, info->sector_count);
143
144 printf (" Sector Start Addresses:");
145 for (i=0; i<info->sector_count; ++i) {
146 if ((i % 5) == 0)
147 printf ("\n ");
148 printf (" %08lX%s",
149 info->start[i],
150 info->protect[i] ? " (RO)" : " "
151 );
152 }
153 printf ("\n");
154}
155
156/*
157 * The following code cannot be run from FLASH!
158 */
159
160static ulong flash_get_size (vu_long *addr, flash_info_t *info)
161{
162 short i;
163 ulong value;
164 ulong base = (ulong)addr;
165 ulong sector_offset;
166
167#ifdef DEBUG
168 printf("Check flash at 0x%08x\n",(uint)addr);
169#endif
170 /* Write "Intelligent Identifier" command: read Manufacturer ID */
171 *addr = 0x90909090;
172 udelay(20);
173 asm("sync");
174
175 value = addr[0] & 0x00FF00FF;
176
177#ifdef DEBUG
178 printf("manufacturer=0x%x\n",(uint)value);
179#endif
180 switch (value) {
181 case MT_MANUFACT: /* SHARP, MT or => Intel */
182 case INTEL_ALT_MANU:
183 info->flash_id = FLASH_MAN_INTEL;
184 break;
185 default:
186 printf("unknown manufacturer: %x\n", (unsigned int)value);
187 info->flash_id = FLASH_UNKNOWN;
188 info->sector_count = 0;
189 info->size = 0;
190 return (0); /* no or unknown flash */
191 }
192
193 value = addr[1] & 0x00FF00FF; /* device ID */
194
195#ifdef DEBUG
196 printf("deviceID=0x%x\n",(uint)value);
197#endif
198 switch (value) {
199 case (INTEL_ID_28F016S):
200 info->flash_id += FLASH_28F016SV;
201 info->sector_count = 32;
202 info->size = 0x00400000;
203 sector_offset = 0x20000;
204 break; /* => 2x2 MB */
205
206 case (INTEL_ID_28F160S3):
207 info->flash_id += FLASH_28F160S3;
208 info->sector_count = 32;
209 info->size = 0x00400000;
210 sector_offset = 0x20000;
211 break; /* => 2x2 MB */
212
213 case (INTEL_ID_28F320S3):
214 info->flash_id += FLASH_28F320S3;
215 info->sector_count = 64;
216 info->size = 0x00800000;
217 sector_offset = 0x20000;
218 break; /* => 2x4 MB */
219
220 case (INTEL_ID_28F640J3A):
221 info->flash_id += FLASH_28F640J3A;
222 info->sector_count = 64;
223 info->size = 0x01000000;
224 sector_offset = 0x40000;
225 break; /* => 8 MB */
226
227 case SHARP_ID_28F016SCL:
228 case SHARP_ID_28F016SCZ:
229 info->flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
230 info->sector_count = 32;
231 info->size = 0x00800000;
232 sector_offset = 0x40000;
233 break; /* => 4x2 MB */
234
235
236 default:
237 info->flash_id = FLASH_UNKNOWN;
238 return (0); /* => no or unknown flash */
239
240 }
241
242 /* set up sector start address table */
243 for (i = 0; i < info->sector_count; i++) {
244 info->start[i] = base;
245 base += sector_offset;
246 /* don't know how to check sector protection */
247 info->protect[i] = 0;
248 }
249
250 /*
251 * Prevent writes to uninitialized FLASH.
252 */
253 if (info->flash_id != FLASH_UNKNOWN) {
254 addr = (vu_long *)info->start[0];
255 *addr = 0xFFFFFF; /* reset bank to read array mode */
256 asm("sync");
257 }
258
259 return (info->size);
260}
261
262
263/*-----------------------------------------------------------------------
264 */
265
266int flash_erase (flash_info_t *info, int s_first, int s_last)
267{
268 int flag, prot, sect;
269 ulong start, now, last;
270
271 if ((s_first < 0) || (s_first > s_last)) {
272 if (info->flash_id == FLASH_UNKNOWN) {
273 printf ("- missing\n");
274 } else {
275 printf ("- no sectors to erase\n");
276 }
277 return 1;
278 }
279
280 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
281 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
282 printf ("Can't erase unknown flash type %08lx - aborted\n",
283 info->flash_id);
284 return 1;
285 }
286
287 prot = 0;
288 for (sect=s_first; sect<=s_last; ++sect) {
289 if (info->protect[sect]) {
290 prot++;
291 }
292 }
293
294 if (prot) {
295 printf ("- Warning: %d protected sectors will not be erased!\n",
296 prot);
297 } else {
298 printf ("\n");
299 }
300
301#ifdef DEBUG
302 printf("\nFlash Erase:\n");
303#endif
304 /* Make Sure Block Lock Bit is not set. */
305 if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
306 return 1;
307 }
308
309 /* Start erase on unprotected sectors */
310#if defined(DEBUG)
311 printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
312#endif
313 for (sect = s_first; sect<=s_last; sect++) {
314 if (info->protect[sect] == 0) { /* not protected */
315 vu_long *addr = (vu_long *)(info->start[sect]);
316 asm("sync");
317
318 last = start = get_timer (0);
319
320 /* Disable interrupts which might cause a timeout here */
321 flag = disable_interrupts();
322
323 /* Reset Array */
324 *addr = 0xffffffff;
325 asm("sync");
326 /* Clear Status Register */
327 *addr = 0x50505050;
328 asm("sync");
329 /* Single Block Erase Command */
330 *addr = 0x20202020;
331 asm("sync");
332 /* Confirm */
333 *addr = 0xD0D0D0D0;
334 asm("sync");
335
336 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
337 /* Resume Command, as per errata update */
338 *addr = 0xD0D0D0D0;
339 asm("sync");
340 }
341
342 /* re-enable interrupts if necessary */
343 if (flag)
344 enable_interrupts();
345
346 /* wait at least 80us - let's wait 1 ms */
347 udelay (1000);
348 while ((*addr & 0x00800080) != 0x00800080) {
349 if(*addr & 0x00200020){
350 printf("Error in Block Erase - Lock Bit may be set!\n");
351 printf("Status Register = 0x%X\n", (uint)*addr);
352 *addr = 0xFFFFFFFF; /* reset bank */
353 asm("sync");
354 return 1;
355 }
356 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
357 printf ("Timeout\n");
358 *addr = 0xFFFFFFFF; /* reset bank */
359 asm("sync");
360 return 1;
361 }
362 /* show that we're waiting */
363 if ((now - last) > 1000) { /* every second */
364 putc ('.');
365 last = now;
366 }
367 }
368
369 /* reset to read mode */
370 *addr = 0xFFFFFFFF;
371 asm("sync");
372 }
373 }
374
375 printf ("flash erase done\n");
376 return 0;
377}
378
379/*-----------------------------------------------------------------------
380 * Copy memory to flash, returns:
381 * 0 - OK
382 * 1 - write timeout
383 * 2 - Flash not erased
384 */
385
386int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
387{
388 ulong cp, wp, data;
389 int i, l, rc;
390
391 wp = (addr & ~3); /* get lower word aligned address */
392
393 /*
394 * handle unaligned start bytes
395 */
396 if ((l = addr - wp) != 0) {
397 data = 0;
398 for (i=0, cp=wp; i<l; ++i, ++cp) {
399 data = (data << 8) | (*(uchar *)cp);
400 }
401 for (; i<4 && cnt>0; ++i) {
402 data = (data << 8) | *src++;
403 --cnt;
404 ++cp;
405 }
406 for (; cnt==0 && i<4; ++i, ++cp) {
407 data = (data << 8) | (*(uchar *)cp);
408 }
409
410 if ((rc = write_word(info, wp, data)) != 0) {
411 return (rc);
412 }
413 wp += 4;
414 }
415
416 /*
417 * handle word aligned part
418 */
419 while (cnt >= 4) {
420 data = 0;
421 for (i=0; i<4; ++i) {
422 data = (data << 8) | *src++;
423 }
424 if ((rc = write_word(info, wp, data)) != 0) {
425 return (rc);
426 }
427 wp += 4;
428 cnt -= 4;
429 }
430
431 if (cnt == 0) {
432 return (0);
433 }
434
435 /*
436 * handle unaligned tail bytes
437 */
438 data = 0;
439 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
440 data = (data << 8) | *src++;
441 --cnt;
442 }
443 for (; i<4; ++i, ++cp) {
444 data = (data << 8) | (*(uchar *)cp);
445 }
446
447 return (write_word(info, wp, data));
448}
449
450/*-----------------------------------------------------------------------
451 * Write a word to Flash, returns:
452 * 0 - OK
453 * 1 - write timeout
454 * 2 - Flash not erased
455 */
456static int write_word (flash_info_t *info, ulong dest, ulong data)
457{
458 vu_long *addr = (vu_long *)dest;
459 ulong start, csr;
460 int flag;
461
462 /* Check if Flash is (sufficiently) erased */
463 if ((*addr & data) != data) {
464 return (2);
465 }
466 /* Disable interrupts which might cause a timeout here */
467 flag = disable_interrupts();
468
469 /* Write Command */
470 *addr = 0x10101010;
471 asm("sync");
472
473 /* Write Data */
474 *addr = data;
475
476 /* re-enable interrupts if necessary */
477 if (flag)
478 enable_interrupts();
479
480 /* data polling for D7 */
481 start = get_timer (0);
482 flag = 0;
483
484 while (((csr = *addr) & 0x00800080) != 0x00800080) {
485 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
486 flag = 1;
487 break;
488 }
489 }
490 if (csr & 0x40404040) {
491 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
492 flag = 1;
493 }
494
495 /* Clear Status Registers Command */
496 *addr = 0x50505050;
497 asm("sync");
498 /* Reset to read array mode */
499 *addr = 0xFFFFFFFF;
500 asm("sync");
501
502 return (flag);
503}
504
505/*-----------------------------------------------------------------------
506 * Clear Block Lock Bit, returns:
507 * 0 - OK
508 * 1 - Timeout
509 */
510
511static int clear_block_lock_bit(vu_long * addr)
512{
513 ulong start, now;
514
515 /* Reset Array */
516 *addr = 0xffffffff;
517 asm("sync");
518 /* Clear Status Register */
519 *addr = 0x50505050;
520 asm("sync");
521
522 *addr = 0x60606060;
523 asm("sync");
524 *addr = 0xd0d0d0d0;
525 asm("sync");
526
527 start = get_timer (0);
528 while((*addr & 0x00800080) != 0x00800080){
529 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
530 printf ("Timeout on clearing Block Lock Bit\n");
531 *addr = 0xFFFFFFFF; /* reset bank */
532 asm("sync");
533 return 1;
534 }
535 }
536 return 0;
537}
538
539#endif /* !CFG_NO_FLASH */