blob: e7d65157f75d3015600ecbc35f1ff4a05f52994e [file] [log] [blame]
wdenk80885a92004-02-26 23:46:20 +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
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * 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) */
35flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
36
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
57/* 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]);
96 break;
97 default:
98 panic ("configured too many flash banks!\n");
99 break;
100 }
101 size += flash_info[i].size;
102 }
103
104 /* Protect monitor and environment sectors
105 */
106 flash_protect (FLAG_PROTECT_SET,
107 CFG_FLASH_BASE,
108 CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
109
110 flash_protect (FLAG_PROTECT_SET,
111 CFG_ENV_ADDR,
112 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
113
114 return size;
115}
116
117/*-----------------------------------------------------------------------
118 */
119static void flash_get_offsets (ulong base, flash_info_t * info)
120{
121 int i;
122 OrgDef *pOrgDef;
123
124 pOrgDef = OrgIntel_28F256L18T;
125 if (info->flash_id == FLASH_UNKNOWN) {
126 return;
127 }
128
129 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
130 for (i = 0; i < info->sector_count; i++) {
131 if (i > 255) {
132 info->start[i] = base + (i * 0x8000);
133 info->protect[i] = 0;
134 } else {
135 info->start[i] = base +
136 (i * PHYS_FLASH_SECT_SIZE);
137 info->protect[i] = 0;
138 }
139 }
140 }
141}
142
143/*-----------------------------------------------------------------------
144 */
145void flash_print_info (flash_info_t * info)
146{
147 int i;
148
149 if (info->flash_id == FLASH_UNKNOWN) {
150 printf ("missing or unknown FLASH type\n");
151 return;
152 }
153
154 switch (info->flash_id & FLASH_VENDMASK) {
155 case FLASH_MAN_INTEL:
156 printf ("INTEL ");
157 break;
158 default:
159 printf ("Unknown Vendor ");
160 break;
161 }
162
163 switch (info->flash_id & FLASH_TYPEMASK) {
164 case FLASH_28F256L18T:
165 printf ("FLASH 28F256L18T\n");
166 break;
167 default:
168 printf ("Unknown Chip Type\n");
169 break;
170 }
171
172 printf (" Size: %ld MB in %d Sectors\n",
173 info->size >> 20, info->sector_count);
174
175 printf (" Sector Start Addresses:");
176 for (i = 0; i < info->sector_count; ++i) {
177 if ((i % 5) == 0)
178 printf ("\n ");
179 printf (" %08lX%s",
180 info->start[i], info->protect[i] ? " (RO)" : " ");
181 }
182 printf ("\n");
183 return;
184}
185
186/*
187 * The following code cannot be run from FLASH!
188 */
189static ulong flash_get_size (FPW * addr, flash_info_t * info)
190{
191 volatile FPW value;
192
193 /* Write auto select command: read Manufacturer ID */
194 addr[0x5555] = (FPW) 0x00AA00AA;
195 addr[0x2AAA] = (FPW) 0x00550055;
196 addr[0x5555] = (FPW) 0x00900090;
197
198 mb ();
199 value = addr[0];
200
201 switch (value) {
202
203 case (FPW) INTEL_MANUFACT:
204 info->flash_id = FLASH_MAN_INTEL;
205 break;
206
207 default:
208 info->flash_id = FLASH_UNKNOWN;
209 info->sector_count = 0;
210 info->size = 0;
211 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
212 return (0); /* no or unknown flash */
213 }
214
215 mb ();
216 value = addr[1]; /* device ID */
217 switch (value) {
218
219 case (FPW) (INTEL_ID_28F256L18T):
220 info->flash_id += FLASH_28F256L18T;
221 info->sector_count = 259;
222 info->size = 0x02000000;
223 break; /* => 32 MB */
224
225 default:
226 info->flash_id = FLASH_UNKNOWN;
227 break;
228 }
229
230 if (info->sector_count > CFG_MAX_FLASH_SECT) {
231 printf ("** ERROR: sector count %d > max (%d) **\n",
232 info->sector_count, CFG_MAX_FLASH_SECT);
233 info->sector_count = CFG_MAX_FLASH_SECT;
234 }
235
236 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
237
238 return (info->size);
239}
240
241
242/* unprotects a sector for write and erase
243 * on some intel parts, this unprotects the entire chip, but it
244 * wont hurt to call this additional times per sector...
245 */
246void flash_unprotect_sectors (FPWV * addr)
247{
248#define PD_FINTEL_WSMS_READY_MASK 0x0080
249
250 *addr = (FPW) 0x00500050; /* clear status register */
251
252 /* this sends the clear lock bit command */
253 *addr = (FPW) 0x00600060;
254 *addr = (FPW) 0x00D000D0;
255}
256
257
258/*-----------------------------------------------------------------------
259 */
260
261int flash_erase (flash_info_t * info, int s_first, int s_last)
262{
263 int flag, prot, sect;
264 ulong type, start, last;
265 int rcode = 0;
266
267 if ((s_first < 0) || (s_first > s_last)) {
268 if (info->flash_id == FLASH_UNKNOWN) {
269 printf ("- missing\n");
270 } else {
271 printf ("- no sectors to erase\n");
272 }
273 return 1;
274 }
275
276 type = (info->flash_id & FLASH_VENDMASK);
277 if ((type != FLASH_MAN_INTEL)) {
278 printf ("Can't erase unknown flash type %08lx - aborted\n",
279 info->flash_id);
280 return 1;
281 }
282
283 prot = 0;
284 for (sect = s_first; sect <= s_last; ++sect) {
285 if (info->protect[sect]) {
286 prot++;
287 }
288 }
289
290 if (prot) {
291 printf ("- Warning: %d protected sectors will not be erased!\n",
292 prot);
293 } else {
294 printf ("\n");
295 }
296
297
298 start = get_timer (0);
299 last = start;
300
301 /* Disable interrupts which might cause a timeout here */
302 flag = disable_interrupts ();
303
304 /* Start erase on unprotected sectors */
305 for (sect = s_first; sect <= s_last; sect++) {
306 if (info->protect[sect] == 0) { /* not protected */
307 FPWV *addr = (FPWV *) (info->start[sect]);
308 FPW status;
309
310 printf ("Erasing sector %2d ... ", sect);
311
312 flash_unprotect_sectors (addr);
313
314 /* arm simple, non interrupt dependent timer */
315 reset_timer_masked ();
316
317 *addr = (FPW) 0x00500050;/* clear status register */
318 *addr = (FPW) 0x00200020;/* erase setup */
319 *addr = (FPW) 0x00D000D0;/* erase confirm */
320
321 while (((status =
322 *addr) & (FPW) 0x00800080) !=
323 (FPW) 0x00800080) {
324 if (get_timer_masked () >
325 CFG_FLASH_ERASE_TOUT) {
326 printf ("Timeout\n");
327 /* suspend erase */
328 *addr = (FPW) 0x00B000B0;
329 /* reset to read mode */
330 *addr = (FPW) 0x00FF00FF;
331 rcode = 1;
332 break;
333 }
334 }
335
336 /* clear status register cmd. */
337 *addr = (FPW) 0x00500050;
338 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
339 printf (" done\n");
340 }
341 }
342 return rcode;
343}
344
345/*-----------------------------------------------------------------------
346 * Copy memory to flash, returns:
347 * 0 - OK
348 * 1 - write timeout
349 * 2 - Flash not erased
350 * 4 - Flash not identified
351 */
352
353int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
354{
355 ulong cp, wp;
356 FPW data;
357 int count, i, l, rc, port_width;
358
359 if (info->flash_id == FLASH_UNKNOWN) {
360 return 4;
361 }
362/* get lower word aligned address */
363#ifdef FLASH_PORT_WIDTH16
364 wp = (addr & ~1);
365 port_width = 2;
366#else
367 wp = (addr & ~3);
368 port_width = 4;
369#endif
370
371 /*
372 * handle unaligned start bytes
373 */
374 if ((l = addr - wp) != 0) {
375 data = 0;
376 for (i = 0, cp = wp; i < l; ++i, ++cp) {
377 data = (data << 8) | (*(uchar *) cp);
378 }
379 for (; i < port_width && cnt > 0; ++i) {
380 data = (data << 8) | *src++;
381 --cnt;
382 ++cp;
383 }
384 for (; cnt == 0 && i < port_width; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *) cp);
386 }
387
388 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
389 return (rc);
390 }
391 wp += port_width;
392 }
393
394 /*
395 * handle word aligned part
396 */
397 count = 0;
398 while (cnt >= port_width) {
399 data = 0;
400 for (i = 0; i < port_width; ++i) {
401 data = (data << 8) | *src++;
402 }
403 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
404 return (rc);
405 }
406 wp += port_width;
407 cnt -= port_width;
408 if (count++ > 0x800) {
409 spin_wheel ();
410 count = 0;
411 }
412 }
413
414 if (cnt == 0) {
415 return (0);
416 }
417
418 /*
419 * handle unaligned tail bytes
420 */
421 data = 0;
422 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
423 data = (data << 8) | *src++;
424 --cnt;
425 }
426 for (; i < port_width; ++i, ++cp) {
427 data = (data << 8) | (*(uchar *) cp);
428 }
429
430 return (write_data (info, wp, SWAP (data)));
431}
432
433/*-----------------------------------------------------------------------
434 * Write a word or halfword to Flash, returns:
435 * 0 - OK
436 * 1 - write timeout
437 * 2 - Flash not erased
438 */
439static int write_data (flash_info_t * info, ulong dest, FPW data)
440{
441 FPWV *addr = (FPWV *) dest;
442 ulong status;
443 int flag;
444
445 /* Check if Flash is (sufficiently) erased */
446 if ((*addr & data) != data) {
447 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
448 return (2);
449 }
450 flash_unprotect_sectors (addr);
451 /* Disable interrupts which might cause a timeout here */
452 flag = disable_interrupts ();
453 *addr = (FPW) 0x00400040; /* write setup */
454 *addr = data;
455
456 /* arm simple, non interrupt dependent timer */
457 reset_timer_masked ();
458
459 /* wait while polling the status register */
460 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
461 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
462 *addr = (FPW) 0x00FF00FF; /* restore read mode */
463 return (1);
464 }
465 }
466 *addr = (FPW) 0x00FF00FF; /* restore read mode */
467 return (0);
468}
469
470void inline spin_wheel (void)
471{
472 static int p = 0;
473 static char w[] = "\\/-";
474
475 printf ("\010%c", w[p]);
476 (++p == 3) ? (p = 0) : 0;
477}