blob: fd6b9c0bbd69716c0a9bda17f96876f0ab62b5be [file] [log] [blame]
wdenk1eaeb582004-06-08 00:22:43 +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 * (C) Copyright 2003
9 * Texas Instruments, <www.ti.com>
10 * Kshitij Gupta <Kshitij@ti.com>
11
12 * (C) Copyright 2004
13 * Texas Instruments <www.ti.com>
14 * Rishi Bhattacharya
15 *
16 * See file CREDITS for list of people who contributed to this
17 * project.
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation; either version 2 of
22 * the License, or (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * MA 02111-1307 USA
33 */
34
35#include <common.h>
36#include <linux/byteorder/swab.h>
37
38#define PHYS_FLASH_SECT_SIZE 0x00020000 /* 256 KB sectors (x2) */
39flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
40
41/* Board support for 1 or 2 flash devices */
42#undef FLASH_PORT_WIDTH32
43#define FLASH_PORT_WIDTH16
44
45#ifdef FLASH_PORT_WIDTH16
46#define FLASH_PORT_WIDTH ushort
47#define FLASH_PORT_WIDTHV vu_short
48#define SWAP(x) __swab16(x)
49#else
50#define FLASH_PORT_WIDTH ulong
51#define FLASH_PORT_WIDTHV vu_long
52#define SWAP(x) __swab32(x)
53#endif
54
55#define FPW FLASH_PORT_WIDTH
56#define FPWV FLASH_PORT_WIDTHV
57
58#define mb() __asm__ __volatile__ ("" : : : "memory")
59
60
61/* Flash Organization Structure */
62typedef struct OrgDef {
63 unsigned int sector_number;
64 unsigned int sector_size;
65} OrgDef;
66
67
68/* Flash Organizations */
69OrgDef OrgIntel_28F256L18T[] = {
70 {4, 32 * 1024}, /* 4 * 32kBytes sectors */
71 {255, 128 * 1024}, /* 255 * 128kBytes sectors */
72};
73
74
75/*-----------------------------------------------------------------------
76 * Functions
77 */
78unsigned long flash_init (void);
79static ulong flash_get_size (FPW * addr, flash_info_t * info);
80static int write_data (flash_info_t * info, ulong dest, FPW data);
81static void flash_get_offsets (ulong base, flash_info_t * info);
82void inline spin_wheel (void);
83void flash_print_info (flash_info_t * info);
84void flash_unprotect_sectors (FPWV * addr);
85int flash_erase (flash_info_t * info, int s_first, int s_last);
86int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
87
88/*-----------------------------------------------------------------------
89 */
90
91unsigned long flash_init (void)
92{
93 int i;
94 ulong size = 0;
95 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
96 switch (i) {
97 case 0:
98 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
99 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
100 break;
101 default:
102 panic ("configured too many flash banks!\n");
103 break;
104 }
105 size += flash_info[i].size;
106 }
107
108 /* Protect monitor and environment sectors
109 */
110 flash_protect (FLAG_PROTECT_SET,
111 CFG_FLASH_BASE,
112 CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
113
114 flash_protect (FLAG_PROTECT_SET,
115 CFG_ENV_ADDR,
116 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
117
118 return size;
119}
120
121/*-----------------------------------------------------------------------
122 */
123static void flash_get_offsets (ulong base, flash_info_t * info)
124{
125 int i;
126 OrgDef *pOrgDef;
127
128 pOrgDef = OrgIntel_28F256L18T;
129 if (info->flash_id == FLASH_UNKNOWN) {
130 return;
131 }
132
133 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
134 for (i = 0; i < info->sector_count; i++) {
135 if (i > 255) {
136 info->start[i] = base + (i * 0x8000);
137 info->protect[i] = 0;
138 } else {
139 info->start[i] = base +
140 (i * PHYS_FLASH_SECT_SIZE);
141 info->protect[i] = 0;
142 }
143 }
144 }
145}
146
147/*-----------------------------------------------------------------------
148 */
149void flash_print_info (flash_info_t * info)
150{
151 int i;
152
153 if (info->flash_id == FLASH_UNKNOWN) {
154 printf ("missing or unknown FLASH type\n");
155 return;
156 }
157
158 switch (info->flash_id & FLASH_VENDMASK) {
159 case FLASH_MAN_INTEL:
160 printf ("INTEL ");
161 break;
162 default:
163 printf ("Unknown Vendor ");
164 break;
165 }
166
167 switch (info->flash_id & FLASH_TYPEMASK) {
168 case FLASH_28F256L18T:
169 printf ("FLASH 28F256L18T\n");
170 break;
171 case FLASH_28F128J3A:
172 printf ("FLASH 28F128J3A\n");
173 break;
174 default:
175 printf ("Unknown Chip Type\n");
176 break;
177 }
178
179 printf (" Size: %ld MB in %d Sectors\n",
180 info->size >> 20, info->sector_count);
181
182 printf (" Sector Start Addresses:");
183 for (i = 0; i < info->sector_count; ++i) {
184 if ((i % 5) == 0)
185 printf ("\n ");
186 printf (" %08lX%s",
187 info->start[i], info->protect[i] ? " (RO)" : " ");
188 }
189 printf ("\n");
190 return;
191}
192
193/*
194 * The following code cannot be run from FLASH!
195 */
196static ulong flash_get_size (FPW * addr, flash_info_t * info)
197{
198 volatile FPW value;
199
200 /* Write auto select command: read Manufacturer ID */
201 addr[0x5555] = (FPW) 0x00AA00AA;
202 addr[0x2AAA] = (FPW) 0x00550055;
203 addr[0x5555] = (FPW) 0x00900090;
204
205 mb ();
206 value = addr[0];
207
208 switch (value) {
209
210 case (FPW) INTEL_MANUFACT:
211 info->flash_id = FLASH_MAN_INTEL;
212 break;
213 case (FPW) MT2_MANUFACT:
214 info->flash_id = FLASH_MAN_INTEL;
215 break;
216
217 default:
218 info->flash_id = FLASH_UNKNOWN;
219 info->sector_count = 0;
220 info->size = 0;
221 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
222 return (0); /* no or unknown flash */
223 }
224
225 mb ();
226 value = addr[1]; /* device ID */
227 switch (value) {
228
229 case (FPW) (INTEL_ID_28F256L18T):
230 info->flash_id += FLASH_28F256L18T;
231 info->sector_count = 259;
232 info->size = 0x02000000;
233 break; /* => 32 MB */
234
235 case (FPW) (INTEL_ID_28F256K3):
236 info->flash_id +=FLASH_28F256K3;
237 info->sector_count = 259;
238 info->size = 0x02000000;
wdenk9dd41a72005-05-12 22:48:09 +0000239 debug ("Intel StrataFlash 28F256K3C device initialized\n");
wdenk1eaeb582004-06-08 00:22:43 +0000240 break; /* => 32 MB */
241
242 case (FPW) (INTEL_ID_28F128J3A):
243 info->flash_id +=FLASH_28F128J3A;
244 info->sector_count = 259;
245 info->size = 0x02000000;
wdenk9dd41a72005-05-12 22:48:09 +0000246 debug ("Micron StrataFlash MT28F128J3 device initialized\n");
wdenk1eaeb582004-06-08 00:22:43 +0000247 break; /* => 32 MB */
248
249 default:
250 info->flash_id = FLASH_UNKNOWN;
251 break;
252 }
253
254 if (info->sector_count > CFG_MAX_FLASH_SECT) {
255 printf ("** ERROR: sector count %d > max (%d) **\n",
256 info->sector_count, CFG_MAX_FLASH_SECT);
257 info->sector_count = CFG_MAX_FLASH_SECT;
258 }
259
260 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
261
262 return (info->size);
263}
264
265
266/* unprotects a sector for write and erase
267 * on some intel parts, this unprotects the entire chip, but it
268 * wont hurt to call this additional times per sector...
269 */
270void flash_unprotect_sectors (FPWV * addr)
271{
272#define PD_FINTEL_WSMS_READY_MASK 0x0080
273
274FPW status;
275
276 *addr = (FPW) 0x00500050; /* clear status register */
277
278 /* this sends the clear lock bit command */
279 *addr = (FPW) 0x00600060;
280 *addr = (FPW) 0x00D000D0;
281
282 while (((status =*addr) & (FPW) 0x00800080) != (FPW) 0x00800080);
283
284 *addr = (FPW) 0x00FF00FF;
285
286}
287
288
289/*-----------------------------------------------------------------------
290 */
291
292int flash_erase (flash_info_t * info, int s_first, int s_last)
293{
294 int flag, prot, sect;
295 ulong type, start, last;
296 int rcode = 0;
297
298 if ((s_first < 0) || (s_first > s_last)) {
299 if (info->flash_id == FLASH_UNKNOWN) {
300 printf ("- missing\n");
301 } else {
302 printf ("- no sectors to erase\n");
303 }
304 return 1;
305 }
306
307 type = (info->flash_id & FLASH_VENDMASK);
308 if ((type != FLASH_MAN_INTEL)) {
309 printf ("Can't erase unknown flash type %08lx - aborted\n",
310 info->flash_id);
311 return 1;
312 }
313
314 prot = 0;
315 for (sect = s_first; sect <= s_last; ++sect) {
316 if (info->protect[sect]) {
317 prot++;
318 }
319 }
320
321 if (prot) {
322 printf ("- Warning: %d protected sectors will not be erased!\n",
323 prot);
324 } else {
325 printf ("\n");
326 }
327
328
329 start = get_timer (0);
330 last = start;
331
332 /* Disable interrupts which might cause a timeout here */
333 flag = disable_interrupts ();
334
335 /* Start erase on unprotected sectors */
336 for (sect = s_first; sect <= s_last; sect++) {
337 if (info->protect[sect] == 0) { /* not protected */
338 FPWV *addr = (FPWV *) (info->start[sect]);
339 FPW status;
340
341 printf ("Erasing sector %2d ... ", sect);
342
343 flash_unprotect_sectors (addr);
344
345 /* arm simple, non interrupt dependent timer */
346 reset_timer_masked ();
347
348 *addr = (FPW) 0x00500050;/* clear status register */
349 *addr = (FPW) 0x00200020;/* erase setup */
350 *addr = (FPW) 0x00D000D0;/* erase confirm */
351
352 while (((status =
353 *addr) & (FPW) 0x00800080) !=
354 (FPW) 0x00800080) {
355 if (get_timer_masked () >
356 CFG_FLASH_ERASE_TOUT) {
357 printf ("Timeout\n");
358 /* suspend erase */
359 *addr = (FPW) 0x00B000B0;
360 /* reset to read mode */
361 *addr = (FPW) 0x00FF00FF;
362 rcode = 1;
363 break;
364 }
365 }
366
367 /* clear status register cmd. */
368 *addr = (FPW) 0x00500050;
369 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
370 printf (" done\n");
371 }
372 }
373 return rcode;
374}
375
376/*-----------------------------------------------------------------------
377 * Copy memory to flash, returns:
378 * 0 - OK
379 * 1 - write timeout
380 * 2 - Flash not erased
381 * 4 - Flash not identified
382 */
383
384int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
385{
386 ulong cp, wp;
387 FPW data;
388 int count, i, l, rc, port_width;
389
390 if (info->flash_id == FLASH_UNKNOWN) {
391 return 4;
392 }
393/* get lower word aligned address */
394#ifdef FLASH_PORT_WIDTH16
395 wp = (addr & ~1);
396 port_width = 2;
397#else
398 wp = (addr & ~3);
399 port_width = 4;
400#endif
401
402 /*
403 * handle unaligned start bytes
404 */
405 if ((l = addr - wp) != 0) {
406 data = 0;
407 for (i = 0, cp = wp; i < l; ++i, ++cp) {
408 data = (data << 8) | (*(uchar *) cp);
409 }
410 for (; i < port_width && cnt > 0; ++i) {
411 data = (data << 8) | *src++;
412 --cnt;
413 ++cp;
414 }
415 for (; cnt == 0 && i < port_width; ++i, ++cp) {
416 data = (data << 8) | (*(uchar *) cp);
417 }
418
419 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
420 return (rc);
421 }
422 wp += port_width;
423 }
424
425 /*
426 * handle word aligned part
427 */
428 count = 0;
429 while (cnt >= port_width) {
430 data = 0;
431 for (i = 0; i < port_width; ++i) {
432 data = (data << 8) | *src++;
433 }
434 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
435 return (rc);
436 }
437 wp += port_width;
438 cnt -= port_width;
439 if (count++ > 0x800) {
440 spin_wheel ();
441 count = 0;
442 }
443 }
444
445 if (cnt == 0) {
446 return (0);
447 }
448
449 /*
450 * handle unaligned tail bytes
451 */
452 data = 0;
453 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
454 data = (data << 8) | *src++;
455 --cnt;
456 }
457 for (; i < port_width; ++i, ++cp) {
458 data = (data << 8) | (*(uchar *) cp);
459 }
460
461 return (write_data (info, wp, SWAP (data)));
462}
463
464/*-----------------------------------------------------------------------
465 * Write a word or halfword to Flash, returns:
466 * 0 - OK
467 * 1 - write timeout
468 * 2 - Flash not erased
469 */
470static int write_data (flash_info_t * info, ulong dest, FPW data)
471{
472 FPWV *addr = (FPWV *) dest;
473 ulong status;
474 int flag;
475
476 /* Check if Flash is (sufficiently) erased */
477 if ((*addr & data) != data) {
478 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
479 return (2);
480 }
481 /* Disable interrupts which might cause a timeout here */
482 flag = disable_interrupts ();
483 *addr = (FPW) 0x00400040; /* write setup */
484 *addr = data;
485
486 /* arm simple, non interrupt dependent timer */
487 reset_timer_masked ();
488
489 /* wait while polling the status register */
490 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
491 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
492 *addr = (FPW) 0x00FF00FF; /* restore read mode */
493 return (1);
494 }
495 }
496 *addr = (FPW) 0x00FF00FF; /* restore read mode */
497 return (0);
498}
499
500void inline spin_wheel (void)
501{
502 static int p = 0;
503 static char w[] = "\\/-";
504
505 printf ("\010%c", w[p]);
506 (++p == 3) ? (p = 0) : 0;
507}