blob: a93875083b68953dad6051abf034d2a4d751e3f7 [file] [log] [blame]
wdenkea8015b2002-10-26 16:43:06 +00001/*
wdenkdb2f721f2003-03-06 00:58:30 +00002 * (C) Copyright 2001
wdenkea8015b2002-10-26 16:43:06 +00003 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
wdenkdb2f721f2003-03-06 00:58:30 +00005 * (C) Copyright 2001
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkea8015b2002-10-26 16:43:06 +00007 *
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>
wdenkdb2f721f2003-03-06 00:58:30 +000028#include <linux/byteorder/swab.h>
wdenkea8015b2002-10-26 16:43:06 +000029
wdenkea8015b2002-10-26 16:43:06 +000030
wdenkdb2f721f2003-03-06 00:58:30 +000031flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkea8015b2002-10-26 16:43:06 +000032
wdenkdb2f721f2003-03-06 00:58:30 +000033/* 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) __swab16(x)
41#else
42#define FLASH_PORT_WIDTH ulong
43#define FLASH_PORT_WIDTHV vu_long
44#define SWAP(x) __swab32(x)
45#endif
46
47#define FPW FLASH_PORT_WIDTH
48#define FPWV FLASH_PORT_WIDTHV
49
50#define mb() __asm__ __volatile__ ("" : : : "memory")
51
52/*-----------------------------------------------------------------------
53 * Functions
54 */
55static ulong flash_get_size (FPW *addr, flash_info_t *info);
56static int write_data (flash_info_t *info, ulong dest, FPW data);
57static void flash_get_offsets (ulong base, flash_info_t *info);
58void inline spin_wheel(void);
wdenkea8015b2002-10-26 16:43:06 +000059
60/*-----------------------------------------------------------------------
61 */
62
wdenkdb2f721f2003-03-06 00:58:30 +000063unsigned long flash_init (void)
wdenkea8015b2002-10-26 16:43:06 +000064{
wdenkdb2f721f2003-03-06 00:58:30 +000065 int i;
wdenkea8015b2002-10-26 16:43:06 +000066 ulong size = 0;
67
68 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
69 {
wdenkea8015b2002-10-26 16:43:06 +000070 switch (i)
71 {
72 case 0:
wdenkdb2f721f2003-03-06 00:58:30 +000073 flash_get_size((FPW *)PHYS_FLASH_1, &flash_info[i]);
74 flash_get_offsets(PHYS_FLASH_1, &flash_info[i]);
wdenkea8015b2002-10-26 16:43:06 +000075 break;
76 case 1:
wdenkdb2f721f2003-03-06 00:58:30 +000077 flash_get_size((FPW *)PHYS_FLASH_2, &flash_info[i]);
78 flash_get_offsets(PHYS_FLASH_2, &flash_info[i]);
wdenkea8015b2002-10-26 16:43:06 +000079 break;
80 default:
81 panic("configured to many flash banks!\n");
82 break;
83 }
wdenkea8015b2002-10-26 16:43:06 +000084 size += flash_info[i].size;
85 }
86
87 /* Protect monitor and environment sectors
88 */
89 flash_protect(FLAG_PROTECT_SET,
90 CFG_FLASH_BASE,
91 CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
92 &flash_info[0]);
93
94 flash_protect(FLAG_PROTECT_SET,
95 CFG_ENV_ADDR,
96 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
97 &flash_info[0]);
98
99 return size;
100}
101
102/*-----------------------------------------------------------------------
103 */
wdenkdb2f721f2003-03-06 00:58:30 +0000104static void flash_get_offsets (ulong base, flash_info_t *info)
105{
106 int i;
107
108 if (info->flash_id == FLASH_UNKNOWN) {
109 return;
110 }
111
112 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
113 for (i = 0; i < info->sector_count; i++) {
114 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
115 info->protect[i] = 0;
116 }
117 }
118}
119
120/*-----------------------------------------------------------------------
121 */
wdenkea8015b2002-10-26 16:43:06 +0000122void flash_print_info (flash_info_t *info)
123{
wdenkdb2f721f2003-03-06 00:58:30 +0000124 int i;
wdenkea8015b2002-10-26 16:43:06 +0000125
wdenkdb2f721f2003-03-06 00:58:30 +0000126 if (info->flash_id == FLASH_UNKNOWN) {
127 printf ("missing or unknown FLASH type\n");
128 return;
wdenkea8015b2002-10-26 16:43:06 +0000129 }
130
wdenkdb2f721f2003-03-06 00:58:30 +0000131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_INTEL: printf ("INTEL "); break;
133 default: printf ("Unknown Vendor "); break;
134 }
135
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case FLASH_28F128J3A:
138 printf ("28F128J3A\n"); break;
139 default: printf ("Unknown Chip Type\n"); break;
wdenkea8015b2002-10-26 16:43:06 +0000140 }
141
wdenkdb2f721f2003-03-06 00:58:30 +0000142 printf (" Size: %ld MB in %d Sectors\n",
wdenkea8015b2002-10-26 16:43:06 +0000143 info->size >> 20, info->sector_count);
144
wdenkdb2f721f2003-03-06 00:58:30 +0000145 printf (" Sector Start Addresses:");
146 for (i=0; i<info->sector_count; ++i) {
wdenkea8015b2002-10-26 16:43:06 +0000147 if ((i % 5) == 0)
wdenkea8015b2002-10-26 16:43:06 +0000148 printf ("\n ");
wdenkdb2f721f2003-03-06 00:58:30 +0000149 printf (" %08lX%s",
150 info->start[i],
151 info->protect[i] ? " (RO)" : " "
152 );
wdenkea8015b2002-10-26 16:43:06 +0000153 }
154 printf ("\n");
wdenkdb2f721f2003-03-06 00:58:30 +0000155 return;
156}
157
158/*
159 * The following code cannot be run from FLASH!
160 */
161static ulong flash_get_size (FPW *addr, flash_info_t *info)
162{
163 volatile FPW value;
164
165 /* Write auto select command: read Manufacturer ID */
166 addr[0x5555] = (FPW)0x00AA00AA;
167 addr[0x2AAA] = (FPW)0x00550055;
168 addr[0x5555] = (FPW)0x00900090;
169
170 mb();
171 value = addr[0];
172
173 switch (value) {
174
175 case (FPW)INTEL_MANUFACT:
176 info->flash_id = FLASH_MAN_INTEL;
177 break;
178
179 default:
180 info->flash_id = FLASH_UNKNOWN;
181 info->sector_count = 0;
182 info->size = 0;
183 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
184 return (0); /* no or unknown flash */
185 }
186
187 mb();
188 value = addr[1]; /* device ID */
189
190 switch (value) {
191
192 case (FPW)INTEL_ID_28F128J3A:
193 info->flash_id += FLASH_28F128J3A;
194 info->sector_count = 128;
195 info->size = 0x02000000;
196 break; /* => 16 MB */
197
198 default:
199 info->flash_id = FLASH_UNKNOWN;
200 break;
201 }
202
203 if (info->sector_count > CFG_MAX_FLASH_SECT) {
204 printf ("** ERROR: sector count %d > max (%d) **\n",
205 info->sector_count, CFG_MAX_FLASH_SECT);
206 info->sector_count = CFG_MAX_FLASH_SECT;
wdenkea8015b2002-10-26 16:43:06 +0000207 }
208
wdenkdb2f721f2003-03-06 00:58:30 +0000209 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
210
211 return (info->size);
wdenkea8015b2002-10-26 16:43:06 +0000212}
213
wdenkdb2f721f2003-03-06 00:58:30 +0000214
wdenkea8015b2002-10-26 16:43:06 +0000215/*-----------------------------------------------------------------------
216 */
217
218int flash_erase (flash_info_t *info, int s_first, int s_last)
219{
220 int flag, prot, sect;
wdenkdb2f721f2003-03-06 00:58:30 +0000221 ulong type, start, now, last;
222 int rcode = 0;
wdenkea8015b2002-10-26 16:43:06 +0000223
224 if ((s_first < 0) || (s_first > s_last)) {
wdenkdb2f721f2003-03-06 00:58:30 +0000225 if (info->flash_id == FLASH_UNKNOWN) {
226 printf ("- missing\n");
227 } else {
228 printf ("- no sectors to erase\n");
229 }
230 return 1;
wdenkea8015b2002-10-26 16:43:06 +0000231 }
232
wdenkdb2f721f2003-03-06 00:58:30 +0000233 type = (info->flash_id & FLASH_VENDMASK);
234 if ((type != FLASH_MAN_INTEL)) {
235 printf ("Can't erase unknown flash type %08lx - aborted\n",
236 info->flash_id);
237 return 1;
wdenkea8015b2002-10-26 16:43:06 +0000238 }
239
240 prot = 0;
241 for (sect=s_first; sect<=s_last; ++sect) {
242 if (info->protect[sect]) {
243 prot++;
244 }
245 }
wdenkea8015b2002-10-26 16:43:06 +0000246
wdenkdb2f721f2003-03-06 00:58:30 +0000247 if (prot) {
248 printf ("- Warning: %d protected sectors will not be erased!\n",
249 prot);
250 } else {
251 printf ("\n");
252 }
253
254 start = get_timer (0);
255 last = start;
256
257 /* Disable interrupts which might cause a timeout here */
wdenkea8015b2002-10-26 16:43:06 +0000258 flag = disable_interrupts();
259
260 /* Start erase on unprotected sectors */
wdenkdb2f721f2003-03-06 00:58:30 +0000261 for (sect = s_first; sect<=s_last; sect++) {
262 if (info->protect[sect] == 0) { /* not protected */
263 FPWV *addr = (FPWV *)(info->start[sect]);
264 FPW status;
wdenkea8015b2002-10-26 16:43:06 +0000265
266 printf("Erasing sector %2d ... ", sect);
267
268 /* arm simple, non interrupt dependent timer */
269 reset_timer_masked();
270
wdenkdb2f721f2003-03-06 00:58:30 +0000271 *addr = (FPW)0x00500050; /* clear status register */
272 *addr = (FPW)0x00200020; /* erase setup */
273 *addr = (FPW)0x00D000D0; /* erase confirm */
wdenkea8015b2002-10-26 16:43:06 +0000274
wdenkdb2f721f2003-03-06 00:58:30 +0000275 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
wdenkea8015b2002-10-26 16:43:06 +0000276 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
wdenkdb2f721f2003-03-06 00:58:30 +0000277 printf ("Timeout\n");
278 *addr = (FPW)0x00B000B0; /* suspend erase */
279 *addr = (FPW)0x00FF00FF; /* reset to read mode */
280 rcode = 1;
281 break;
wdenkea8015b2002-10-26 16:43:06 +0000282 }
283 }
284
wdenkdb2f721f2003-03-06 00:58:30 +0000285 *addr = 0x00500050; /* clear status register cmd. */
286 *addr = 0x00FF00FF; /* resest to read mode */
wdenkea8015b2002-10-26 16:43:06 +0000287
wdenkdb2f721f2003-03-06 00:58:30 +0000288 printf (" done\n");
289 }
290 }
291 return rcode;
wdenkea8015b2002-10-26 16:43:06 +0000292}
293
294/*-----------------------------------------------------------------------
wdenkdb2f721f2003-03-06 00:58:30 +0000295 * Copy memory to flash, returns:
296 * 0 - OK
297 * 1 - write timeout
298 * 2 - Flash not erased
299 * 4 - Flash not identified
wdenkea8015b2002-10-26 16:43:06 +0000300 */
301
302int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
303{
304 ulong cp, wp;
wdenkdb2f721f2003-03-06 00:58:30 +0000305 FPW data;
306 int count, i, l, rc, port_width;
wdenkea8015b2002-10-26 16:43:06 +0000307
wdenkdb2f721f2003-03-06 00:58:30 +0000308 if (info->flash_id == FLASH_UNKNOWN) {
309 return 4;
310 }
311/* get lower word aligned address */
312#ifdef FLASH_PORT_WIDTH16
313 wp = (addr & ~1);
314 port_width = 2;
315#else
316 wp = (addr & ~3);
317 port_width = 4;
318#endif
wdenkea8015b2002-10-26 16:43:06 +0000319
320 /*
321 * handle unaligned start bytes
322 */
323 if ((l = addr - wp) != 0) {
324 data = 0;
325 for (i=0, cp=wp; i<l; ++i, ++cp) {
wdenkdb2f721f2003-03-06 00:58:30 +0000326 data = (data << 8) | (*(uchar *)cp);
wdenkea8015b2002-10-26 16:43:06 +0000327 }
wdenkdb2f721f2003-03-06 00:58:30 +0000328 for (; i<port_width && cnt>0; ++i) {
329 data = (data << 8) | *src++;
wdenkea8015b2002-10-26 16:43:06 +0000330 --cnt;
331 ++cp;
332 }
wdenkdb2f721f2003-03-06 00:58:30 +0000333 for (; cnt==0 && i<port_width; ++i, ++cp) {
334 data = (data << 8) | (*(uchar *)cp);
wdenkea8015b2002-10-26 16:43:06 +0000335 }
336
wdenkdb2f721f2003-03-06 00:58:30 +0000337 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
wdenkea8015b2002-10-26 16:43:06 +0000338 return (rc);
339 }
wdenkdb2f721f2003-03-06 00:58:30 +0000340 wp += port_width;
wdenkea8015b2002-10-26 16:43:06 +0000341 }
342
343 /*
344 * handle word aligned part
345 */
wdenkdb2f721f2003-03-06 00:58:30 +0000346 count = 0;
347 while (cnt >= port_width) {
348 data = 0;
349 for (i=0; i<port_width; ++i) {
350 data = (data << 8) | *src++;
351 }
352 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
wdenkea8015b2002-10-26 16:43:06 +0000353 return (rc);
354 }
wdenkdb2f721f2003-03-06 00:58:30 +0000355 wp += port_width;
356 cnt -= port_width;
357 if (count++ > 0x800)
358 {
359 spin_wheel();
360 count = 0;
361 }
wdenkea8015b2002-10-26 16:43:06 +0000362 }
363
364 if (cnt == 0) {
wdenkdb2f721f2003-03-06 00:58:30 +0000365 return (0);
wdenkea8015b2002-10-26 16:43:06 +0000366 }
367
368 /*
369 * handle unaligned tail bytes
370 */
371 data = 0;
wdenkdb2f721f2003-03-06 00:58:30 +0000372 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
373 data = (data << 8) | *src++;
wdenkea8015b2002-10-26 16:43:06 +0000374 --cnt;
375 }
wdenkdb2f721f2003-03-06 00:58:30 +0000376 for (; i<port_width; ++i, ++cp) {
377 data = (data << 8) | (*(uchar *)cp);
378 }
379
380 return (write_data(info, wp, SWAP(data)));
381}
382
383/*-----------------------------------------------------------------------
384 * Write a word or halfword to Flash, returns:
385 * 0 - OK
386 * 1 - write timeout
387 * 2 - Flash not erased
388 */
389static int write_data (flash_info_t *info, ulong dest, FPW data)
390{
391 FPWV *addr = (FPWV *)dest;
392 ulong status;
393 ulong start;
394 int flag;
395
396 /* Check if Flash is (sufficiently) erased */
397 if ((*addr & data) != data) {
398 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
399 return (2);
400 }
401 /* Disable interrupts which might cause a timeout here */
402 flag = disable_interrupts();
403
404 *addr = (FPW)0x00400040; /* write setup */
405 *addr = data;
406
407 /* arm simple, non interrupt dependent timer */
408 reset_timer_masked();
409
410 /* wait while polling the status register */
411 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
412 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
413 *addr = (FPW)0x00FF00FF; /* restore read mode */
414 return (1);
415 }
wdenkea8015b2002-10-26 16:43:06 +0000416 }
417
wdenkdb2f721f2003-03-06 00:58:30 +0000418 *addr = (FPW)0x00FF00FF; /* restore read mode */
419
420 return (0);
wdenkea8015b2002-10-26 16:43:06 +0000421}
wdenkdb2f721f2003-03-06 00:58:30 +0000422
423void inline
424spin_wheel(void)
425{
426 static int r=0,p=0;
427 static char w[] = "\\/-";
428
429 printf("\010%c", w[p]);
430 (++p == 3) ? (p = 0) : 0;
431}
432