blob: 056fe810b38a165a041c0028d5a8628304ef0325 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001, 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * Flash Routines for AMD devices on the TQM8260 board
6 *
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 <mpc8xx.h>
29
30#define V_ULONG(a) (*(volatile unsigned long *)( a ))
31#define V_BYTE(a) (*(volatile unsigned char *)( a ))
32
33
34flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
35
36
37/*-----------------------------------------------------------------------
38 */
39void flash_reset (void)
40{
41 if (flash_info[0].flash_id != FLASH_UNKNOWN) {
42 V_ULONG (flash_info[0].start[0]) = 0x00F000F0;
43 V_ULONG (flash_info[0].start[0] + 4) = 0x00F000F0;
44 }
45}
46
47/*-----------------------------------------------------------------------
48 */
49ulong flash_get_size (ulong baseaddr, flash_info_t * info)
50{
51 short i;
52 unsigned long flashtest_h, flashtest_l;
53
54 /* Write auto select command sequence and test FLASH answer */
55 V_ULONG (baseaddr + ((ulong) 0x0555 << 3)) = 0x00AA00AA;
56 V_ULONG (baseaddr + ((ulong) 0x02AA << 3)) = 0x00550055;
57 V_ULONG (baseaddr + ((ulong) 0x0555 << 3)) = 0x00900090;
58 V_ULONG (baseaddr + 4 + ((ulong) 0x0555 << 3)) = 0x00AA00AA;
59 V_ULONG (baseaddr + 4 + ((ulong) 0x02AA << 3)) = 0x00550055;
60 V_ULONG (baseaddr + 4 + ((ulong) 0x0555 << 3)) = 0x00900090;
61
62 flashtest_h = V_ULONG (baseaddr); /* manufacturer ID */
63 flashtest_l = V_ULONG (baseaddr + 4);
64
65 switch ((int) flashtest_h) {
66 case AMD_MANUFACT:
67 info->flash_id = FLASH_MAN_AMD;
68 break;
69 case FUJ_MANUFACT:
70 info->flash_id = FLASH_MAN_FUJ;
71 break;
72 default:
73 info->flash_id = FLASH_UNKNOWN;
74 info->sector_count = 0;
75 info->size = 0;
76 return (0); /* no or unknown flash */
77 }
78
79 flashtest_h = V_ULONG (baseaddr + 8); /* device ID */
80 flashtest_l = V_ULONG (baseaddr + 12);
81 if (flashtest_h != flashtest_l) {
82 info->flash_id = FLASH_UNKNOWN;
83 } else {
84 switch (flashtest_h) {
85 case AMD_ID_LV800T:
86 info->flash_id += FLASH_AM800T;
87 info->sector_count = 19;
88 info->size = 0x00400000;
89 break; /* 4 * 1 MB = 4 MB */
90 case AMD_ID_LV800B:
91 info->flash_id += FLASH_AM800B;
92 info->sector_count = 19;
93 info->size = 0x00400000;
94 break; /* 4 * 1 MB = 4 MB */
95 case AMD_ID_LV160T:
96 info->flash_id += FLASH_AM160T;
97 info->sector_count = 35;
98 info->size = 0x00800000;
99 break; /* 4 * 2 MB = 8 MB */
100 case AMD_ID_LV160B:
101 info->flash_id += FLASH_AM160B;
102 info->sector_count = 35;
103 info->size = 0x00800000;
104 break; /* 4 * 2 MB = 8 MB */
105 case AMD_ID_DL322T:
106 info->flash_id += FLASH_AMDL322T;
107 info->sector_count = 71;
108 info->size = 0x01000000;
109 break; /* 4 * 4 MB = 16 MB */
110 case AMD_ID_DL322B:
111 info->flash_id += FLASH_AMDL322B;
112 info->sector_count = 71;
113 info->size = 0x01000000;
114 break; /* 4 * 4 MB = 16 MB */
115 case AMD_ID_DL323T:
116 info->flash_id += FLASH_AMDL323T;
117 info->sector_count = 71;
118 info->size = 0x01000000;
119 break; /* 4 * 4 MB = 16 MB */
120 case AMD_ID_DL323B:
121 info->flash_id += FLASH_AMDL323B;
122 info->sector_count = 71;
123 info->size = 0x01000000;
124 break; /* 4 * 4 MB = 16 MB */
125 case AMD_ID_LV640U:
126 info->flash_id += FLASH_AM640U;
127 info->sector_count = 128;
128 info->size = 0x02000000;
129 break; /* 4 * 8 MB = 32 MB */
130 default:
131 info->flash_id = FLASH_UNKNOWN;
132 return (0); /* no or unknown flash */
133 }
134 }
135
136 if (flashtest_h == AMD_ID_LV640U) {
137
138 /* set up sector start adress table (uniform sector type) */
139 for (i = 0; i < info->sector_count; i++)
140 info->start[i] = baseaddr + (i * 0x00040000);
141
142 } else if (info->flash_id & FLASH_BTYPE) {
143
144 /* set up sector start adress table (bottom sector type) */
145 info->start[0] = baseaddr + 0x00000000;
146 info->start[1] = baseaddr + 0x00010000;
147 info->start[2] = baseaddr + 0x00018000;
148 info->start[3] = baseaddr + 0x00020000;
149 for (i = 4; i < info->sector_count; i++) {
150 info->start[i] = baseaddr + (i * 0x00040000) - 0x000C0000;
151 }
152
153 } else {
154
155 /* set up sector start adress table (top sector type) */
156 i = info->sector_count - 1;
157 info->start[i--] = baseaddr + info->size - 0x00010000;
158 info->start[i--] = baseaddr + info->size - 0x00018000;
159 info->start[i--] = baseaddr + info->size - 0x00020000;
160 for (; i >= 0; i--) {
161 info->start[i] = baseaddr + i * 0x00040000;
162 }
163 }
164
165 /* check for protected sectors */
166 for (i = 0; i < info->sector_count; i++) {
167 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
168 if ((V_ULONG (info->start[i] + 16) & 0x00010001) ||
169 (V_ULONG (info->start[i] + 20) & 0x00010001)) {
170 info->protect[i] = 1; /* D0 = 1 if protected */
171 } else {
172 info->protect[i] = 0;
173 }
174 }
175
176 flash_reset ();
177 return (info->size);
178}
179
180/*-----------------------------------------------------------------------
181 */
182unsigned long flash_init (void)
183{
184 unsigned long size_b0 = 0;
185 int i;
186
187 /* Init: no FLASHes known */
188 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
189 flash_info[i].flash_id = FLASH_UNKNOWN;
190 }
191
192 /* Static FLASH Bank configuration here (only one bank) */
193
194 size_b0 = flash_get_size (CFG_FLASH0_BASE, &flash_info[0]);
195 if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
196 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
197 size_b0, size_b0 >> 20);
198 }
199
200 /*
201 * protect monitor and environment sectors
202 */
203
204#if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
205 flash_protect (FLAG_PROTECT_SET,
206 CFG_MONITOR_BASE,
wdenk3b57fe02003-05-30 12:48:29 +0000207 CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
wdenkaffae2b2002-08-17 09:36:01 +0000208#endif
209
210#if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
211# ifndef CFG_ENV_SIZE
212# define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
213# endif
214 flash_protect (FLAG_PROTECT_SET,
215 CFG_ENV_ADDR,
216 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
217#endif
218
219 return (size_b0);
220}
221
222/*-----------------------------------------------------------------------
223 */
224void flash_print_info (flash_info_t * info)
225{
226 int i;
227
228 if (info->flash_id == FLASH_UNKNOWN) {
229 printf ("missing or unknown FLASH type\n");
230 return;
231 }
232
233 switch (info->flash_id & FLASH_VENDMASK) {
234 case FLASH_MAN_AMD:
235 printf ("AMD ");
236 break;
237 case FLASH_MAN_FUJ:
238 printf ("FUJITSU ");
239 break;
240 default:
241 printf ("Unknown Vendor ");
242 break;
243 }
244
245 switch (info->flash_id & FLASH_TYPEMASK) {
246 case FLASH_AM800T:
247 printf ("29LV800T (8 M, top sector)\n");
248 break;
249 case FLASH_AM800B:
250 printf ("29LV800T (8 M, bottom sector)\n");
251 break;
252 case FLASH_AM160T:
253 printf ("29LV160T (16 M, top sector)\n");
254 break;
255 case FLASH_AM160B:
256 printf ("29LV160B (16 M, bottom sector)\n");
257 break;
258 case FLASH_AMDL322T:
259 printf ("29DL322T (32 M, top sector)\n");
260 break;
261 case FLASH_AMDL322B:
262 printf ("29DL322B (32 M, bottom sector)\n");
263 break;
264 case FLASH_AMDL323T:
265 printf ("29DL323T (32 M, top sector)\n");
266 break;
267 case FLASH_AMDL323B:
268 printf ("29DL323B (32 M, bottom sector)\n");
269 break;
270 case FLASH_AM640U:
271 printf ("29LV640D (64 M, uniform sector)\n");
272 break;
273 default:
274 printf ("Unknown Chip Type\n");
275 break;
276 }
277
278 printf (" Size: %ld MB in %d Sectors\n",
279 info->size >> 20, info->sector_count);
280
281 printf (" Sector Start Addresses:");
282 for (i = 0; i < info->sector_count; ++i) {
283 if ((i % 5) == 0)
284 printf ("\n ");
285 printf (" %08lX%s",
286 info->start[i],
287 info->protect[i] ? " (RO)" : " "
288 );
289 }
290 printf ("\n");
291 return;
292}
293
294/*-----------------------------------------------------------------------
295 */
296int flash_erase (flash_info_t * info, int s_first, int s_last)
297{
298 int flag, prot, sect, l_sect;
299 ulong start, now, last;
300
301 if ((s_first < 0) || (s_first > s_last)) {
302 if (info->flash_id == FLASH_UNKNOWN) {
303 printf ("- missing\n");
304 } else {
305 printf ("- no sectors to erase\n");
306 }
307 return 1;
308 }
309
310 prot = 0;
311 for (sect = s_first; sect <= s_last; sect++) {
312 if (info->protect[sect])
313 prot++;
314 }
315
316 if (prot) {
317 printf ("- Warning: %d protected sectors will not be erased!\n",
318 prot);
319 } else {
320 printf ("\n");
321 }
322
323 l_sect = -1;
324
325 /* Disable interrupts which might cause a timeout here */
326 flag = disable_interrupts ();
327
328 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
329 V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
330 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00800080;
331 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
332 V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
333 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
334 V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
335 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00800080;
336 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
337 V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
338 udelay (1000);
339
340 /* Start erase on unprotected sectors */
341 for (sect = s_first; sect <= s_last; sect++) {
342 if (info->protect[sect] == 0) { /* not protected */
343 V_ULONG (info->start[sect]) = 0x00300030;
344 V_ULONG (info->start[sect] + 4) = 0x00300030;
345 l_sect = sect;
346 }
347 }
348
349 /* re-enable interrupts if necessary */
350 if (flag)
351 enable_interrupts ();
352
353 /* wait at least 80us - let's wait 1 ms */
354 udelay (1000);
355
356 /*
357 * We wait for the last triggered sector
358 */
359 if (l_sect < 0)
360 goto DONE;
361
362 start = get_timer (0);
363 last = start;
364 while ((V_ULONG (info->start[l_sect]) & 0x00800080) != 0x00800080 ||
365 (V_ULONG (info->start[l_sect] + 4) & 0x00800080) != 0x00800080)
366 {
367 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
368 printf ("Timeout\n");
369 return 1;
370 }
371 /* show that we're waiting */
372 if ((now - last) > 1000) { /* every second */
373 serial_putc ('.');
374 last = now;
375 }
376 }
377
378 DONE:
379 /* reset to read mode */
380 flash_reset ();
381
382 printf (" done\n");
383 return 0;
384}
385
386static int write_dword (flash_info_t *, ulong, unsigned char *);
387
388/*-----------------------------------------------------------------------
389 * Copy memory to flash, returns:
390 * 0 - OK
391 * 1 - write timeout
392 * 2 - Flash not erased
393 */
394
395int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
396{
397 ulong dp;
398 static unsigned char bb[8];
399 int i, l, rc, cc = cnt;
400
401 dp = (addr & ~7); /* get lower dword aligned address */
402
403 /*
404 * handle unaligned start bytes
405 */
406 if ((l = addr - dp) != 0) {
407 for (i = 0; i < 8; i++)
408 bb[i] = (i < l || (i - l) >= cc) ? V_BYTE (dp + i) : *src++;
409 if ((rc = write_dword (info, dp, bb)) != 0) {
410 return (rc);
411 }
412 dp += 8;
413 cc -= 8 - l;
414 }
415
416 /*
417 * handle word aligned part
418 */
419 while (cc >= 8) {
420 if ((rc = write_dword (info, dp, src)) != 0) {
421 return (rc);
422 }
423 dp += 8;
424 src += 8;
425 cc -= 8;
426 }
427
428 if (cc <= 0) {
429 return (0);
430 }
431
432 /*
433 * handle unaligned tail bytes
434 */
435 for (i = 0; i < 8; i++) {
436 bb[i] = (i < cc) ? *src++ : V_BYTE (dp + i);
437 }
438 return (write_dword (info, dp, bb));
439}
440
441/*-----------------------------------------------------------------------
442 * Write a dword to Flash, returns:
443 * 0 - OK
444 * 1 - write timeout
445 * 2 - Flash not erased
446 */
447static int write_dword (flash_info_t * info, ulong dest, unsigned char *pdata)
448{
449 ulong start, cl, ch;
450 int flag, i;
451
452 for (ch = 0, i = 0; i < 4; i++)
453 ch = (ch << 8) + *pdata++; /* high word */
454 for (cl = 0, i = 0; i < 4; i++)
455 cl = (cl << 8) + *pdata++; /* low word */
456
457 /* Check if Flash is (sufficiently) erased */
458 if ((*((vu_long *) dest) & ch) != ch
459 || (*((vu_long *) (dest + 4)) & cl) != cl) {
460 return (2);
461 }
462
463 /* Disable interrupts which might cause a timeout here */
464 flag = disable_interrupts ();
465
466 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
467 V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
468 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00A000A0;
469 V_ULONG (dest) = ch;
470 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
471 V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
472 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00A000A0;
473 V_ULONG (dest + 4) = cl;
474
475 /* re-enable interrupts if necessary */
476 if (flag)
477 enable_interrupts ();
478
479 /* data polling for D7 */
480 start = get_timer (0);
481 while (((V_ULONG (dest) & 0x00800080) != (ch & 0x00800080)) ||
482 ((V_ULONG (dest + 4) & 0x00800080) != (cl & 0x00800080))) {
483 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
484 return (1);
485 }
486 }
487 return (0);
488}