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