blob: 572cc9bbeb0b1acb1f3c8e1bad6f34e93a880b33 [file] [log] [blame]
wdenkefa329c2004-03-23 20:18:25 +00001/*
2 * (C) Copyright 2001
3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
5 * (C) Copyright 2001-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <linux/byteorder/swab.h>
29
30
31flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
32
33/* Board support for 1 or 2 flash devices */
34#define FLASH_PORT_WIDTH32
35#undef FLASH_PORT_WIDTH16
36
37#ifdef FLASH_PORT_WIDTH16
38#define FLASH_PORT_WIDTH ushort
39#define FLASH_PORT_WIDTHV vu_short
40#define SWAP(x) (x)
41#else
42#define FLASH_PORT_WIDTH ulong
43#define FLASH_PORT_WIDTHV vu_long
44#define SWAP(x) (x)
45#endif
46
47/* Intel-compatible flash ID */
48#define INTEL_COMPAT 0x00890089
49#define INTEL_ALT 0x00B000B0
50
51/* Intel-compatible flash commands */
52#define INTEL_PROGRAM 0x00100010
53#define INTEL_ERASE 0x00200020
54#define INTEL_CLEAR 0x00500050
55#define INTEL_LOCKBIT 0x00600060
56#define INTEL_PROTECT 0x00010001
57#define INTEL_STATUS 0x00700070
58#define INTEL_READID 0x00900090
59#define INTEL_CONFIRM 0x00D000D0
60#define INTEL_RESET 0xFFFFFFFF
61
62/* Intel-compatible flash status bits */
63#define INTEL_FINISHED 0x00800080
64#define INTEL_OK 0x00800080
65
66#define FPW FLASH_PORT_WIDTH
67#define FPWV FLASH_PORT_WIDTHV
68
69#define mb() __asm__ __volatile__ ("" : : : "memory")
70
71/*-----------------------------------------------------------------------
72 * Functions
73 */
74static ulong flash_get_size (FPW *addr, flash_info_t *info);
75static int write_data (flash_info_t *info, ulong dest, FPW data);
76static void flash_get_offsets (ulong base, flash_info_t *info);
77void inline spin_wheel (void);
78
79/*-----------------------------------------------------------------------
80 */
81
82unsigned long flash_init (void)
83{
84 int i;
85 ulong size = 0;
wdenk49822e22004-06-19 21:19:10 +000086 extern void flash_preinit(void);
87 extern void flash_afterinit(ulong, ulong);
88 ulong flashbase = CFG_FLASH_BASE;
89
90 flash_preinit();
wdenkefa329c2004-03-23 20:18:25 +000091
92 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
93 switch (i) {
94 case 0:
wdenk49822e22004-06-19 21:19:10 +000095 memset(&flash_info[i], 0, sizeof(flash_info_t));
96 flash_get_size ((FPW *) flashbase, &flash_info[i]);
97 flash_get_offsets (flash_info[i].start[0], &flash_info[i]);
wdenkefa329c2004-03-23 20:18:25 +000098 break;
99 default:
100 panic ("configured to many flash banks!\n");
101 break;
102 }
103 size += flash_info[i].size;
104 }
105
106 /* Protect monitor and environment sectors
107 */
wdenk49822e22004-06-19 21:19:10 +0000108#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
109#ifndef CONFIG_BOOT_ROM
wdenkefa329c2004-03-23 20:18:25 +0000110 flash_protect ( FLAG_PROTECT_SET,
111 CFG_MONITOR_BASE,
112 CFG_MONITOR_BASE + monitor_flash_len - 1,
113 &flash_info[0] );
wdenk49822e22004-06-19 21:19:10 +0000114#endif
115#endif
wdenkefa329c2004-03-23 20:18:25 +0000116
wdenk49822e22004-06-19 21:19:10 +0000117#ifdef CFG_ENV_IS_IN_FLASH
wdenkefa329c2004-03-23 20:18:25 +0000118 flash_protect ( FLAG_PROTECT_SET,
119 CFG_ENV_ADDR,
120 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
wdenk49822e22004-06-19 21:19:10 +0000121#endif
122
123 flash_afterinit(flash_info[0].start[0], flash_info[0].size);
wdenkefa329c2004-03-23 20:18:25 +0000124
125 return size;
126}
127
128/*-----------------------------------------------------------------------
129 */
130static void flash_get_offsets (ulong base, flash_info_t *info)
131{
132 int i;
133
134 if (info->flash_id == FLASH_UNKNOWN) {
135 return;
136 }
137
138 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
139 for (i = 0; i < info->sector_count; i++) {
140 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
141 info->protect[i] = 0;
142 }
143 }
144}
145
146/*-----------------------------------------------------------------------
147 */
148void flash_print_info (flash_info_t *info)
149{
150 int i;
151
152 if (info->flash_id == FLASH_UNKNOWN) {
153 printf ("missing or unknown FLASH type\n");
154 return;
155 }
156
157 switch (info->flash_id & FLASH_VENDMASK) {
158 case FLASH_MAN_INTEL:
159 printf ("INTEL ");
160 break;
161 default:
162 printf ("Unknown Vendor ");
163 break;
164 }
165
166 switch (info->flash_id & FLASH_TYPEMASK) {
167 case FLASH_28F128J3A:
168 printf ("28F128J3A\n");
169 break;
170
171 case FLASH_28F640J3A:
172 printf ("28F640J3A\n");
173 break;
174
175 case FLASH_28F320J3A:
176 printf ("28F320J3A\n");
177 break;
178
179 default:
180 printf ("Unknown Chip Type\n");
181 break;
182 }
183
184 printf (" Size: %ld MB in %d Sectors\n",
185 info->size >> 20, info->sector_count);
186
187 printf (" Sector Start Addresses:");
188 for (i = 0; i < info->sector_count; ++i) {
189 if ((i % 5) == 0)
190 printf ("\n ");
191 printf (" %08lX%s",
192 info->start[i],
193 info->protect[i] ? " (RO)" : " ");
194 }
195 printf ("\n");
196 return;
197}
198
199/*
200 * The following code cannot be run from FLASH!
201 */
202static ulong flash_get_size (FPW *addr, flash_info_t *info)
203{
204 volatile FPW value;
205
206 /* Write auto select command: read Manufacturer ID */
207 addr[0x5555] = (FPW) 0x00AA00AA;
208 addr[0x2AAA] = (FPW) 0x00550055;
209 addr[0x5555] = (FPW) 0x00900090;
210
211 mb ();
wdenk49822e22004-06-19 21:19:10 +0000212 udelay(100);
213
wdenkefa329c2004-03-23 20:18:25 +0000214 value = addr[0];
215
216 switch (value) {
217
218 case (FPW) INTEL_MANUFACT:
219 info->flash_id = FLASH_MAN_INTEL;
220 break;
221
222 default:
223 info->flash_id = FLASH_UNKNOWN;
224 info->sector_count = 0;
225 info->size = 0;
226 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
227 return (0); /* no or unknown flash */
228 }
229
230 mb ();
231 value = addr[1]; /* device ID */
232
233 switch (value) {
234
235 case (FPW) INTEL_ID_28F128J3A:
236 info->flash_id += FLASH_28F128J3A;
237 info->sector_count = 128;
238 info->size = 0x02000000;
wdenk49822e22004-06-19 21:19:10 +0000239 info->start[0] = CFG_FLASH_BASE;
wdenkefa329c2004-03-23 20:18:25 +0000240 break; /* => 32 MB */
241
242 case (FPW) INTEL_ID_28F640J3A:
243 info->flash_id += FLASH_28F640J3A;
244 info->sector_count = 64;
245 info->size = 0x01000000;
wdenk49822e22004-06-19 21:19:10 +0000246 info->start[0] = CFG_FLASH_BASE + 0x01000000;
wdenkefa329c2004-03-23 20:18:25 +0000247 break; /* => 16 MB */
248
249 case (FPW) INTEL_ID_28F320J3A:
250 info->flash_id += FLASH_28F320J3A;
251 info->sector_count = 32;
wdenk49822e22004-06-19 21:19:10 +0000252 info->size = 0x800000;
253 info->start[0] = CFG_FLASH_BASE + 0x01800000;
wdenkefa329c2004-03-23 20:18:25 +0000254 break; /* => 8 MB */
255
256 default:
257 info->flash_id = FLASH_UNKNOWN;
258 break;
259 }
260
261 if (info->sector_count > CFG_MAX_FLASH_SECT) {
262 printf ("** ERROR: sector count %d > max (%d) **\n",
263 info->sector_count, CFG_MAX_FLASH_SECT);
264 info->sector_count = CFG_MAX_FLASH_SECT;
265 }
266
267 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
268
269 return (info->size);
270}
271
272
273/*-----------------------------------------------------------------------
274 */
275
276int flash_erase (flash_info_t *info, int s_first, int s_last)
277{
278 int flag, prot, sect;
279 ulong type, start, last;
280 int rcode = 0;
281
282 if ((s_first < 0) || (s_first > s_last)) {
283 if (info->flash_id == FLASH_UNKNOWN) {
284 printf ("- missing\n");
285 } else {
286 printf ("- no sectors to erase\n");
287 }
288 return 1;
289 }
290
291 type = (info->flash_id & FLASH_VENDMASK);
292 if ((type != FLASH_MAN_INTEL)) {
293 printf ("Can't erase unknown flash type %08lx - aborted\n",
294 info->flash_id);
295 return 1;
296 }
297
298 prot = 0;
299 for (sect = s_first; sect <= s_last; ++sect) {
300 if (info->protect[sect]) {
301 prot++;
302 }
303 }
304
305 if (prot) {
306 printf ("- Warning: %d protected sectors will not be erased!\n",
307 prot);
308 } else {
309 printf ("\n");
310 }
311
312 start = get_timer (0);
313 last = start;
314
315 /* Disable interrupts which might cause a timeout here */
316 flag = disable_interrupts ();
317
318 /* Start erase on unprotected sectors */
319 for (sect = s_first; sect <= s_last; sect++) {
320 if (info->protect[sect] == 0) { /* not protected */
321 FPWV *addr = (FPWV *) (info->start[sect]);
322 FPW status;
323
324 printf ("Erasing sector %2d ... ", sect);
325
326 /* arm simple, non interrupt dependent timer */
327 start = get_timer(0);
328
329 *addr = (FPW) 0x00500050; /* clear status register */
330 *addr = (FPW) 0x00200020; /* erase setup */
331 *addr = (FPW) 0x00D000D0; /* erase confirm */
332
333 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
334 if (get_timer(start) > CFG_FLASH_ERASE_TOUT) {
335 printf ("Timeout\n");
336 *addr = (FPW) 0x00B000B0; /* suspend erase */
337 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
338 rcode = 1;
339 break;
340 }
341 }
342
343 *addr = 0x00500050; /* clear status register cmd. */
344 *addr = 0x00FF00FF; /* resest to read mode */
345
346 printf (" done\n");
347 }
348 }
349 return rcode;
350}
351
352/*-----------------------------------------------------------------------
353 * Copy memory to flash, returns:
354 * 0 - OK
355 * 1 - write timeout
356 * 2 - Flash not erased
357 * 4 - Flash not identified
358 */
359
360int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
361{
362 ulong cp, wp;
363 FPW data;
364 int count, i, l, rc, port_width;
365
366 if (info->flash_id == FLASH_UNKNOWN) {
367 return 4;
368 }
369/* get lower word aligned address */
370#ifdef FLASH_PORT_WIDTH16
371 wp = (addr & ~1);
372 port_width = 2;
373#else
374 wp = (addr & ~3);
375 port_width = 4;
376#endif
377
378 /*
379 * handle unaligned start bytes
380 */
381 if ((l = addr - wp) != 0) {
382 data = 0;
383 for (i = 0, cp = wp; i < l; ++i, ++cp) {
384 data = (data << 8) | (*(uchar *) cp);
385 }
386 for (; i < port_width && cnt > 0; ++i) {
387 data = (data << 8) | *src++;
388 --cnt;
389 ++cp;
390 }
391 for (; cnt == 0 && i < port_width; ++i, ++cp) {
392 data = (data << 8) | (*(uchar *) cp);
393 }
394
395 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
396 return (rc);
397 }
398 wp += port_width;
399 }
400
401 /*
402 * handle word aligned part
403 */
404 count = 0;
405 while (cnt >= port_width) {
406 data = 0;
407 for (i = 0; i < port_width; ++i) {
408 data = (data << 8) | *src++;
409 }
410 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
411 return (rc);
412 }
413 wp += port_width;
414 cnt -= port_width;
415 if (count++ > 0x800) {
416 spin_wheel ();
417 count = 0;
418 }
419 }
420
421 if (cnt == 0) {
422 return (0);
423 }
424
425 /*
426 * handle unaligned tail bytes
427 */
428 data = 0;
429 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
430 data = (data << 8) | *src++;
431 --cnt;
432 }
433 for (; i < port_width; ++i, ++cp) {
434 data = (data << 8) | (*(uchar *) cp);
435 }
436
437 return (write_data (info, wp, SWAP (data)));
438}
439
440/*-----------------------------------------------------------------------
441 * Write a word or halfword to Flash, returns:
442 * 0 - OK
443 * 1 - write timeout
444 * 2 - Flash not erased
445 */
446static int write_data (flash_info_t *info, ulong dest, FPW data)
447{
448 FPWV *addr = (FPWV *) dest;
449 ulong status;
450 ulong start;
451 int flag;
452
453 /* Check if Flash is (sufficiently) erased */
454 if ((*addr & data) != data) {
455 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
456 return (2);
457 }
458 /* Disable interrupts which might cause a timeout here */
459 flag = disable_interrupts ();
460
461 *addr = (FPW) 0x00400040; /* write setup */
462 *addr = data;
463
464 /* arm simple, non interrupt dependent timer */
465 start = get_timer(0);
466
467 /* wait while polling the status register */
468 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
469 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
470 *addr = (FPW) 0x00FF00FF; /* restore read mode */
471 return (1);
472 }
473 }
474
475 *addr = (FPW) 0x00FF00FF; /* restore read mode */
476
477 return (0);
478}
479
480void inline spin_wheel (void)
481{
482 static int p = 0;
483 static char w[] = "\\/-";
484
485 printf ("\010%c", w[p]);
486 (++p == 3) ? (p = 0) : 0;
487}
488
489/*-----------------------------------------------------------------------
490 * Set/Clear sector's lock bit, returns:
491 * 0 - OK
492 * 1 - Error (timeout, voltage problems, etc.)
493 */
494int flash_real_protect(flash_info_t *info, long sector, int prot)
495{
496 ulong start;
497 int i;
498 int rc = 0;
499 vu_long *addr = (vu_long *)(info->start[sector]);
500 int flag = disable_interrupts();
501
502 *addr = INTEL_CLEAR; /* Clear status register */
503 if (prot) { /* Set sector lock bit */
504 *addr = INTEL_LOCKBIT; /* Sector lock bit */
505 *addr = INTEL_PROTECT; /* set */
506 }
507 else { /* Clear sector lock bit */
508 *addr = INTEL_LOCKBIT; /* All sectors lock bits */
509 *addr = INTEL_CONFIRM; /* clear */
510 }
511
512 start = get_timer(0);
513
514 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
515 if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) {
516 printf("Flash lock bit operation timed out\n");
517 rc = 1;
518 break;
519 }
520 }
521
522 if (*addr != INTEL_OK) {
523 printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
524 (uint)addr, (uint)*addr);
525 rc = 1;
526 }
527
528 if (!rc)
529 info->protect[sector] = prot;
530
531 /*
532 * Clear lock bit command clears all sectors lock bits, so
533 * we have to restore lock bits of protected sectors.
534 */
535 if (!prot)
536 {
537 for (i = 0; i < info->sector_count; i++)
538 {
539 if (info->protect[i])
540 {
541 start = get_timer(0);
542 addr = (vu_long *)(info->start[i]);
543 *addr = INTEL_LOCKBIT; /* Sector lock bit */
544 *addr = INTEL_PROTECT; /* set */
545 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED)
546 {
547 if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT)
548 {
549 printf("Flash lock bit operation timed out\n");
550 rc = 1;
551 break;
552 }
553 }
554 }
555 }
556 }
557
558 if (flag)
559 enable_interrupts();
560
561 *addr = INTEL_RESET; /* Reset to read array mode */
562
563 return rc;
564}