blob: a4bed61474db463566dc7346c90f638577f8d078 [file] [log] [blame]
wdenk29e7f5a2004-03-12 00:14:09 +00001/*
2 * (C) Copyright 2001
3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
5 * (C) Copyright 2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
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>
28#include <linux/byteorder/swab.h>
29
30#define PHYS_FLASH_SECT_SIZE 0x00020000 /* 256 KB sectors (x2) */
31flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
32
33/* Board support for 1 or 2 flash devices */
34#undef FLASH_PORT_WIDTH32
35#define 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);
59
60/*-----------------------------------------------------------------------
61 */
62
63unsigned long flash_init (void)
64{
65 int i;
66 ulong size = 0;
67
68 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
69 switch (i) {
70 case 0:
71 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
72 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
73 break;
74 case 1:
75 flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
76 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
77 break;
78 default:
79 panic ("configured too many flash banks!\n");
80 break;
81 }
82 size += flash_info[i].size;
83 }
84
85 /* Protect monitor and environment sectors
86 */
87 flash_protect ( FLAG_PROTECT_SET,
88 CFG_FLASH_BASE,
89 CFG_FLASH_BASE + monitor_flash_len - 1,
90 &flash_info[0]);
91
92 flash_protect ( FLAG_PROTECT_SET,
93 CFG_ENV_ADDR,
94 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
95 &flash_info[0]);
96
97 return size;
98}
99
100/*-----------------------------------------------------------------------
101 */
102static void flash_get_offsets (ulong base, flash_info_t * info)
103{
104 int i;
105
106 if (info->flash_id == FLASH_UNKNOWN) {
107 return;
108 }
109
110 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
111 for (i = 0; i < info->sector_count; i++) {
112 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
113 info->protect[i] = 0;
114 }
115 }
116}
117
118/*-----------------------------------------------------------------------
119 */
120void flash_print_info (flash_info_t * info)
121{
122 int i;
123
124 if (info->flash_id == FLASH_UNKNOWN) {
125 printf ("missing or unknown FLASH type\n");
126 return;
127 }
128
129 switch (info->flash_id & FLASH_VENDMASK) {
130 case FLASH_MAN_INTEL:
131 printf ("INTEL ");
132 break;
133 default:
134 printf ("Unknown Vendor ");
135 break;
136 }
137
138 switch (info->flash_id & FLASH_TYPEMASK) {
139 case FLASH_28F128J3A:
140 printf ("28F128J3A\n");
141 break;
142 case FLASH_28F640J3A:
143 printf ("28F640J3A\n");
144 break;
145 default:
146 printf ("Unknown Chip Type\n");
147 break;
148 }
149
150 printf (" Size: %ld MB in %d Sectors\n",
151 info->size >> 20, info->sector_count);
152
153 printf (" Sector Start Addresses:");
154 for (i = 0; i < info->sector_count; ++i) {
155 if ((i % 5) == 0)
156 printf ("\n ");
157 printf (" %08lX%s",
158 info->start[i],
159 info->protect[i] ? " (RO)" : " ");
160 }
161 printf ("\n");
162 return;
163}
164
165/*
166 * The following code cannot be run from FLASH!
167 */
168static ulong flash_get_size (FPW * addr, flash_info_t * info)
169{
170 volatile FPW value;
171
172 /* Write auto select command: read Manufacturer ID */
173 addr[0x5555] = (FPW) 0x00AA00AA;
174 addr[0x2AAA] = (FPW) 0x00550055;
175 addr[0x5555] = (FPW) 0x00900090;
176
177 mb ();
178 value = addr[0];
179
180 switch (value) {
181 case (FPW) INTEL_MANUFACT:
182 info->flash_id = FLASH_MAN_INTEL;
183 break;
184 default:
185 info->flash_id = FLASH_UNKNOWN;
186 info->sector_count = 0;
187 info->size = 0;
188 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
189 return (0); /* no or unknown flash */
190 }
191
192 mb ();
193 value = addr[1]; /* device ID */
194 switch (value) {
195 case (FPW) INTEL_ID_28F128J3A:
196 info->flash_id += FLASH_28F128J3A;
197 info->sector_count = 128;
198 info->size = 0x01000000;
199 break; /* => 16 MB */
200 case (FPW) INTEL_ID_28F640J3A:
201 info->flash_id += FLASH_28F640J3A;
202 info->sector_count = 64;
203 info->size = 0x00800000;
204 break; /* => 8 MB */
205 default:
206 info->flash_id = FLASH_UNKNOWN;
207 break;
208 }
209
210 if (info->sector_count > CFG_MAX_FLASH_SECT) {
211 printf ("** ERROR: sector count %d > max (%d) **\n",
212 info->sector_count, CFG_MAX_FLASH_SECT);
213 info->sector_count = CFG_MAX_FLASH_SECT;
214 }
215
216 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
217
218 return (info->size);
219}
220
221
222/*-----------------------------------------------------------------------
223 */
224
225int flash_erase (flash_info_t * info, int s_first, int s_last)
226{
227 int flag, prot, sect;
228 ulong type, start, last;
229 int rcode = 0;
230
231 if ((s_first < 0) || (s_first > s_last)) {
232 if (info->flash_id == FLASH_UNKNOWN) {
233 printf ("- missing\n");
234 } else {
235 printf ("- no sectors to erase\n");
236 }
237 return 1;
238 }
239
240 type = (info->flash_id & FLASH_VENDMASK);
241 if ((type != FLASH_MAN_INTEL)) {
242 printf ("Can't erase unknown flash type %08lx - aborted\n",
243 info->flash_id);
244 return 1;
245 }
246
247 prot = 0;
248 for (sect = s_first; sect <= s_last; ++sect) {
249 if (info->protect[sect]) {
250 prot++;
251 }
252 }
253
254 if (prot) {
255 printf ("- Warning: %d protected sectors will not be erased!\n",
256 prot);
257 } else {
258 printf ("\n");
259 }
260
261 /*start = get_timer (0); */
262 last = start;
263
264 /* Disable interrupts which might cause a timeout here */
265 flag = disable_interrupts ();
266
267 /* Start erase on unprotected sectors */
268 for (sect = s_first; sect <= s_last; sect++) {
269 if (info->protect[sect] == 0) { /* not protected */
270 FPWV *addr = (FPWV *) (info->start[sect]);
271 FPW status;
272
273 printf ("Erasing sector %2d ... ", sect);
274
275 /* arm simple, non interrupt dependent timer */
276 reset_timer_masked ();
277
278 *addr = (FPW) 0x00500050; /* clear status register */
279 *addr = (FPW) 0x00200020; /* erase setup */
280 *addr = (FPW) 0x00D000D0; /* erase confirm */
281
282 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
283 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
284 printf ("Timeout\n");
285 *addr = (FPW) 0x00B000B0; /* suspend erase */
286 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
287 rcode = 1;
288 break;
289 }
290 }
291
292 *addr = (FPW) 0x00500050; /* clear status register cmd. */
293 *addr = (FPW) 0x00FF00FF; /* resest to read mode */
294
295 printf (" done\n");
296 }
297 }
298 return rcode;
299}
300
301/*-----------------------------------------------------------------------
302 * Copy memory to flash, returns:
303 * 0 - OK
304 * 1 - write timeout
305 * 2 - Flash not erased
306 * 4 - Flash not identified
307 */
308
309int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
310{
311 ulong cp, wp;
312 FPW data;
313 int count, i, l, rc, port_width;
314
315 if (info->flash_id == FLASH_UNKNOWN) {
316 return 4;
317 }
318/* get lower word aligned address */
319#ifdef FLASH_PORT_WIDTH16
320 wp = (addr & ~1);
321 port_width = 2;
322#else
323 wp = (addr & ~3);
324 port_width = 4;
325#endif
326
327 /*
328 * handle unaligned start bytes
329 */
330 if ((l = addr - wp) != 0) {
331 data = 0;
332 for (i = 0, cp = wp; i < l; ++i, ++cp) {
333 data = (data << 8) | (*(uchar *) cp);
334 }
335 for (; i < port_width && cnt > 0; ++i) {
336 data = (data << 8) | *src++;
337 --cnt;
338 ++cp;
339 }
340 for (; cnt == 0 && i < port_width; ++i, ++cp) {
341 data = (data << 8) | (*(uchar *) cp);
342 }
343
344 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
345 return (rc);
346 }
347 wp += port_width;
348 }
349
350 /*
351 * handle word aligned part
352 */
353 count = 0;
354 while (cnt >= port_width) {
355 data = 0;
356 for (i = 0; i < port_width; ++i) {
357 data = (data << 8) | *src++;
358 }
359 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
360 return (rc);
361 }
362 wp += port_width;
363 cnt -= port_width;
364 if (count++ > 0x800) {
365 spin_wheel ();
366 count = 0;
367 }
368 }
369
370 if (cnt == 0) {
371 return (0);
372 }
373
374 /*
375 * handle unaligned tail bytes
376 */
377 data = 0;
378 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
379 data = (data << 8) | *src++;
380 --cnt;
381 }
382 for (; i < port_width; ++i, ++cp) {
383 data = (data << 8) | (*(uchar *) cp);
384 }
385
386 return (write_data (info, wp, SWAP (data)));
387}
388
389/*-----------------------------------------------------------------------
390 * Write a word or halfword to Flash, returns:
391 * 0 - OK
392 * 1 - write timeout
393 * 2 - Flash not erased
394 */
395static int write_data (flash_info_t * info, ulong dest, FPW data)
396{
397 FPWV *addr = (FPWV *) dest;
398 ulong status;
399 int flag;
400
401 /* Check if Flash is (sufficiently) erased */
402 if ((*addr & data) != data) {
403 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
404 return (2);
405 }
406 /* Disable interrupts which might cause a timeout here */
407 flag = disable_interrupts ();
408
409 *addr = (FPW) 0x00400040; /* write setup */
410 *addr = data;
411
412 /* arm simple, non interrupt dependent timer */
413 reset_timer_masked ();
414
415 /* wait while polling the status register */
416 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
417
418 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
419 *addr = (FPW) 0x00FF00FF; /* restore read mode */
420 return (1);
421 }
422 }
423
424 *addr = (FPW) 0x00FF00FF; /* restore read mode */
425
426 return (0);
427}
428
429void inline spin_wheel (void)
430{
431 static int p = 0;
432 static char w[] = "\\/-";
433
434 printf ("\010%c", w[p]);
435 (++p == 3) ? (p = 0) : 0;
436}