blob: f12d0be552598830523578de19d280ccc9ab9763 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <mpc8xx.h>
26
27flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
28
29/*-----------------------------------------------------------------------
30 * Functions
31 */
32static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33static int write_word (flash_info_t *info, ulong dest, ulong data);
34static void flash_get_offsets (ulong base, flash_info_t *info);
35
36/*-----------------------------------------------------------------------
37 */
38
39unsigned long flash_init (void)
40{
41 unsigned long size_b0, size_b1;
42 int i;
43
44 /* Init: no FLASHes known */
45 for (i=0; i < CFG_MAX_FLASH_BANKS; ++i)
46 flash_info[i].flash_id = FLASH_UNKNOWN;
47
48 /* Detect size */
49 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
50
51 /* Setup offsets */
52 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
53
54#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
55 /* Monitor protection ON by default */
56 flash_protect(FLAG_PROTECT_SET,
57 CFG_MONITOR_BASE,
wdenk3b57fe02003-05-30 12:48:29 +000058 CFG_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000059 &flash_info[0]);
60#endif
61
62 size_b1 = 0 ;
63
64 flash_info[1].flash_id = FLASH_UNKNOWN;
65 flash_info[1].sector_count = -1;
66
67 flash_info[0].size = size_b0;
68 flash_info[1].size = size_b1;
69
70 return (size_b0 + size_b1);
71}
72
73/*-----------------------------------------------------------------------
74 * Fix this to support variable sector sizes
75*/
76static void flash_get_offsets (ulong base, flash_info_t *info)
77{
78 int i;
79
80 /* set up sector start address table */
81 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
82 /* set sector offsets for bottom boot block type */
83 for (i = 0; i < info->sector_count; i++)
84 info->start[i] = base + (i * 0x00010000);
85 }
86}
87
88/*-----------------------------------------------------------------------
89 */
90void flash_print_info (flash_info_t *info)
91{
92 int i;
93
94 if (info->flash_id == FLASH_UNKNOWN)
95 {
96 puts ("missing or unknown FLASH type\n");
97 return;
98 }
99
100 switch (info->flash_id & FLASH_VENDMASK)
101 {
102 case FLASH_MAN_AMD: printf ("AMD "); break;
103 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
104 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
105 default: printf ("Unknown Vendor "); break;
106 }
107
108 switch (info->flash_id & FLASH_TYPEMASK)
109 {
110 case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
111 break;
112 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
113 break;
114 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
115 break;
116 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
117 break;
118 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
119 break;
120 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
121 break;
122 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
123 break;
124 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
125 break;
126 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
127 break;
128 default: printf ("Unknown Chip Type\n");
129 break;
130 }
131
132 if (info->size >> 20) {
133 printf (" Size: %ld MB in %d Sectors\n",
134 info->size >> 20,
135 info->sector_count);
136 } else {
137 printf (" Size: %ld KB in %d Sectors\n",
138 info->size >> 10,
139 info->sector_count);
140 }
141
142 puts (" Sector Start Addresses:");
143
144 for (i=0; i<info->sector_count; ++i)
145 {
146 if ((i % 5) == 0)
147 {
148 puts ("\n ");
149 }
150
151 printf (" %08lX%s",
152 info->start[i],
153 info->protect[i] ? " (RO)" : " ");
154 }
155
156 putc ('\n');
157 return;
158}
159/*-----------------------------------------------------------------------
160 */
161
162/*
163 * The following code cannot be run from FLASH!
164 */
165
166static ulong flash_get_size (vu_long *addr, flash_info_t *info)
167{
168 short i;
169 volatile unsigned char *caddr;
170 char value;
171
172 caddr = (volatile unsigned char *)addr ;
173
174 /* Write auto select command: read Manufacturer ID */
175
176#if 0
177 printf("Base address is: %08x\n", caddr);
178#endif
179
180 caddr[0x0555] = 0xAA;
181 caddr[0x02AA] = 0x55;
182 caddr[0x0555] = 0x90;
183
184 value = caddr[0];
185
186#if 0
187 printf("Manufact ID: %02x\n", value);
188#endif
189 switch (value)
190 {
191 case 0x01:
192 case AMD_MANUFACT:
193 info->flash_id = FLASH_MAN_AMD;
194 break;
195
196 case FUJ_MANUFACT:
197 info->flash_id = FLASH_MAN_FUJ;
198 break;
199
200 default:
201 info->flash_id = FLASH_UNKNOWN;
202 info->sector_count = 0;
203 info->size = 0;
204 break;
205 }
206
207 value = caddr[1]; /* device ID */
208#if 0
209 printf("Device ID: %02x\n", value);
210#endif
211 switch (value)
212 {
213 case AMD_ID_LV040B:
214 info->flash_id += FLASH_AM040;
215 info->sector_count = 8;
216 info->size = 0x00080000;
217 break; /* => 512Kb */
218
219 default:
220 info->flash_id = FLASH_UNKNOWN;
221 return (0); /* => no or unknown flash */
222
223 }
224
225 flash_get_offsets ((ulong)addr, &flash_info[0]);
226
227 /* check for protected sectors */
228 for (i = 0; i < info->sector_count; i++)
229 {
230 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
231 /* D0 = 1 if protected */
232 caddr = (volatile unsigned char *)(info->start[i]);
233 info->protect[i] = caddr[2] & 1;
234 }
235
236 /*
237 * Prevent writes to uninitialized FLASH.
238 */
239 if (info->flash_id != FLASH_UNKNOWN)
240 {
241 caddr = (volatile unsigned char *)info->start[0];
242 *caddr = 0xF0; /* reset bank */
243 }
244
245 return (info->size);
246}
247
248
249/*-----------------------------------------------------------------------
250 */
251
252int flash_erase (flash_info_t *info, int s_first, int s_last)
253{
254 volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
255 int flag, prot, sect, l_sect;
256 ulong start, now, last;
257
258 if ((s_first < 0) || (s_first > s_last)) {
259 if (info->flash_id == FLASH_UNKNOWN) {
260 printf ("- missing\n");
261 } else {
262 printf ("- no sectors to erase\n");
263 }
264 return 1;
265 }
266
267 if ((info->flash_id == FLASH_UNKNOWN) ||
268 (info->flash_id > FLASH_AMD_COMP)) {
269 printf ("Can't erase unknown flash type - aborted\n");
270 return 1;
271 }
272
273 prot = 0;
274 for (sect=s_first; sect<=s_last; ++sect) {
275 if (info->protect[sect]) {
276 prot++;
277 }
278 }
279
280 if (prot) {
281 printf ("- Warning: %d protected sectors will not be erased!\n",
282 prot);
283 } else {
284 printf ("\n");
285 }
286
287 l_sect = -1;
288
289 /* Disable interrupts which might cause a timeout here */
290 flag = disable_interrupts();
291
292 addr[0x0555] = 0xAA;
293 addr[0x02AA] = 0x55;
294 addr[0x0555] = 0x80;
295 addr[0x0555] = 0xAA;
296 addr[0x02AA] = 0x55;
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 addr = (volatile unsigned char *)(info->start[sect]);
302 addr[0] = 0x30;
303 l_sect = sect;
304 }
305 }
306
307 /* re-enable interrupts if necessary */
308 if (flag)
309 enable_interrupts();
310
311 /* wait at least 80us - let's wait 1 ms */
312 udelay (1000);
313
314 /*
315 * We wait for the last triggered sector
316 */
317 if (l_sect < 0)
318 goto DONE;
319
320 start = get_timer (0);
321 last = start;
322 addr = (volatile unsigned char *)(info->start[l_sect]);
323
324 while ((addr[0] & 0xFF) != 0xFF)
325 {
326 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
327 printf ("Timeout\n");
328 return 1;
329 }
330 /* show that we're waiting */
331 if ((now - last) > 1000) { /* every second */
332 putc ('.');
333 last = now;
334 }
335 }
336
337DONE:
338 /* reset to read mode */
339 addr = (volatile unsigned char *)info->start[0];
340
341 addr[0] = 0xF0; /* reset bank */
342
343 printf (" done\n");
344 return 0;
345}
346
347/*-----------------------------------------------------------------------
348 * Copy memory to flash, returns:
349 * 0 - OK
350 * 1 - write timeout
351 * 2 - Flash not erased
352 */
353
354int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
355{
356 ulong cp, wp, data;
357 int i, l, rc;
358
359 wp = (addr & ~3); /* get lower word aligned address */
360
361 /*
362 * handle unaligned start bytes
363 */
364 if ((l = addr - wp) != 0) {
365 data = 0;
366 for (i=0, cp=wp; i<l; ++i, ++cp) {
367 data = (data << 8) | (*(uchar *)cp);
368 }
369 for (; i<4 && cnt>0; ++i) {
370 data = (data << 8) | *src++;
371 --cnt;
372 ++cp;
373 }
374 for (; cnt==0 && i<4; ++i, ++cp) {
375 data = (data << 8) | (*(uchar *)cp);
376 }
377
378 if ((rc = write_word(info, wp, data)) != 0) {
379 return (rc);
380 }
381 wp += 4;
382 }
383
384 /*
385 * handle word aligned part
386 */
387 while (cnt >= 4) {
388 data = 0;
389 for (i=0; i<4; ++i) {
390 data = (data << 8) | *src++;
391 }
392 if ((rc = write_word(info, wp, data)) != 0) {
393 return (rc);
394 }
395 wp += 4;
396 cnt -= 4;
397 }
398
399 if (cnt == 0) {
400 return (0);
401 }
402
403 /*
404 * handle unaligned tail bytes
405 */
406 data = 0;
407 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
408 data = (data << 8) | *src++;
409 --cnt;
410 }
411 for (; i<4; ++i, ++cp) {
412 data = (data << 8) | (*(uchar *)cp);
413 }
414
415 return (write_word(info, wp, data));
416}
417
418/*-----------------------------------------------------------------------
419 * Write a word to Flash, returns:
420 * 0 - OK
421 * 1 - write timeout
422 * 2 - Flash not erased
423 */
424static int write_word (flash_info_t *info, ulong dest, ulong data)
425{
426 volatile unsigned char *addr = (volatile unsigned char*)(info->start[0]),
427 *cdest,*cdata;
428 ulong start;
429 int flag, count = 4 ;
430
431 cdest = (volatile unsigned char *)dest ;
432 cdata = (volatile unsigned char *)&data ;
433
434 /* Check if Flash is (sufficiently) erased */
435 if ((*((vu_long *)dest) & data) != data) {
436 return (2);
437 }
438
439 while(count--)
440 {
441 /* Disable interrupts which might cause a timeout here */
442 flag = disable_interrupts();
443
444 addr[0x0555] = 0xAA;
445 addr[0x02AA] = 0x55;
446 addr[0x0555] = 0xA0;
447
448 *cdest = *cdata;
449
450 /* re-enable interrupts if necessary */
451 if (flag)
452 enable_interrupts();
453
454 /* data polling for D7 */
455 start = get_timer (0);
456 while ((*cdest ^ *cdata) & 0x80)
457 {
458 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
459 return (1);
460 }
461 }
462
463 cdata++ ;
464 cdest++ ;
465 }
466 return (0);
467}
468
469/*-----------------------------------------------------------------------
470 */