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