blob: 4204c341933cbbeea1246e0010a952d7f58e00f4 [file] [log] [blame]
wdenked247f42002-10-07 21:58:02 +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_byte(flash_info_t * info, ulong dest, uchar data);
34static void flash_get_offsets(ulong base, flash_info_t * info);
35
36/*-----------------------------------------------------------------------
37 */
38
39unsigned long flash_init(void)
40{
41 volatile immap_t *immap = (immap_t *) CFG_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size;
44 int i;
45
46 /* Init: no FLASHes known */
47 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
49 }
50
51 /* Static FLASH Bank configuration here - FIXME XXX */
52
53 size = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
54
55 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
56 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size, size << 20);
57 }
58
59 /* Remap FLASH according to real size */
60 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
61 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
62
63 /* Re-do sizing to get full correct info */
64 size = flash_get_size((vu_long *) CFG_FLASH_BASE, &flash_info[0]);
65
66 flash_get_offsets(CFG_FLASH_BASE, &flash_info[0]);
67
68 /* monitor protection ON by default */
69 flash_protect(FLAG_PROTECT_SET, CFG_FLASH_BASE, CFG_FLASH_BASE + CFG_MONITOR_LEN - 1, &flash_info[0]);
70
71 flash_info[0].size = size;
72
73 return (size);
74}
75
76/*-----------------------------------------------------------------------
77 */
78static void flash_get_offsets(ulong base, flash_info_t * info)
79{
80 int i;
81
82 /* set up sector start address table */
83 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
84 for (i = 0; i < info->sector_count; i++) {
85 info->start[i] = base + (i * 0x00010000);
86 }
87 } else if (info->flash_id & FLASH_BTYPE) {
88 /* set sector offsets for bottom boot block type */
89 info->start[0] = base + 0x00000000;
90 info->start[1] = base + 0x00004000;
91 info->start[2] = base + 0x00006000;
92 info->start[3] = base + 0x00008000;
93 for (i = 4; i < info->sector_count; i++) {
94 info->start[i] = base + (i * 0x00010000) - 0x00030000;
95 }
96 } else {
97 /* set sector offsets for top boot block type */
98 i = info->sector_count - 1;
99 info->start[i--] = base + info->size - 0x00004000;
100 info->start[i--] = base + info->size - 0x00006000;
101 info->start[i--] = base + info->size - 0x00008000;
102 for (; i >= 0; i--) {
103 info->start[i] = base + i * 0x00010000;
104 }
105 }
106
107}
108
109/*-----------------------------------------------------------------------
110 */
111void flash_print_info(flash_info_t * info)
112{
113 int i;
114
115 if (info->flash_id == FLASH_UNKNOWN) {
116 printf("missing or unknown FLASH type\n");
117 return;
118 }
119
120 switch (info->flash_id & FLASH_VENDMASK) {
121 case FLASH_MAN_AMD:
122 printf("AMD ");
123 break;
124 case FLASH_MAN_FUJ:
125 printf("FUJITSU ");
126 break;
127 case FLASH_MAN_MX:
128 printf("MXIC ");
129 break;
130 default:
131 printf("Unknown Vendor ");
132 break;
133 }
134
135 switch (info->flash_id & FLASH_TYPEMASK) {
136 case FLASH_AM040:
137 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
138 break;
139 case FLASH_AM400B:
140 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
141 break;
142 case FLASH_AM400T:
143 printf("AM29LV400T (4 Mbit, top boot sector)\n");
144 break;
145 case FLASH_AM800B:
146 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
147 break;
148 case FLASH_AM800T:
149 printf("AM29LV800T (8 Mbit, top boot sector)\n");
150 break;
151 case FLASH_AM160B:
152 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
153 break;
154 case FLASH_AM160T:
155 printf("AM29LV160T (16 Mbit, top boot sector)\n");
156 break;
157 case FLASH_AM320B:
158 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
159 break;
160 case FLASH_AM320T:
161 printf("AM29LV320T (32 Mbit, top boot sector)\n");
162 break;
163 default:
164 printf("Unknown Chip Type\n");
165 break;
166 }
167
168 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
169
170 printf(" Sector Start Addresses:");
171 for (i = 0; i < info->sector_count; ++i) {
172 if ((i % 5) == 0)
173 printf("\n ");
174 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
175 }
176 printf("\n");
177}
178
179/*-----------------------------------------------------------------------
180 */
181
182
183/*-----------------------------------------------------------------------
184 */
185
186/*
187 * The following code cannot be run from FLASH!
188 */
189
190static ulong flash_get_size(vu_long * addr, flash_info_t * info)
191{
192 short i;
193 uchar mid;
194 uchar pid;
195 vu_char *caddr = (vu_char *) addr;
196 ulong base = (ulong) addr;
197
198
199 /* Write auto select command: read Manufacturer ID */
200 caddr[0x0555] = 0xAA;
201 caddr[0x02AA] = 0x55;
202 caddr[0x0555] = 0x90;
203
204 mid = caddr[0];
205 switch (mid) {
206 case (AMD_MANUFACT & 0xFF):
207 info->flash_id = FLASH_MAN_AMD;
208 break;
209 case (FUJ_MANUFACT & 0xFF):
210 info->flash_id = FLASH_MAN_FUJ;
211 break;
212 case (MX_MANUFACT & 0xFF):
213 info->flash_id = FLASH_MAN_MX;
214 break;
215 case (STM_MANUFACT & 0xFF):
216 info->flash_id = FLASH_MAN_STM;
217 break;
218 default:
219 info->flash_id = FLASH_UNKNOWN;
220 info->sector_count = 0;
221 info->size = 0;
222 return (0); /* no or unknown flash */
223 }
224
225 pid = caddr[1]; /* device ID */
226 switch (pid) {
227 case (AMD_ID_LV400T & 0xFF):
228 info->flash_id += FLASH_AM400T;
229 info->sector_count = 11;
230 info->size = 0x00080000;
231 break; /* => 512 kB */
232
233 case (AMD_ID_LV400B & 0xFF):
234 info->flash_id += FLASH_AM400B;
235 info->sector_count = 11;
236 info->size = 0x00080000;
237 break; /* => 512 kB */
238
239 case (AMD_ID_LV800T & 0xFF):
240 info->flash_id += FLASH_AM800T;
241 info->sector_count = 19;
242 info->size = 0x00100000;
243 break; /* => 1 MB */
244
245 case (AMD_ID_LV800B & 0xFF):
246 info->flash_id += FLASH_AM800B;
247 info->sector_count = 19;
248 info->size = 0x00100000;
249 break; /* => 1 MB */
250
251 case (AMD_ID_LV160T & 0xFF):
252 info->flash_id += FLASH_AM160T;
253 info->sector_count = 35;
254 info->size = 0x00200000;
255 break; /* => 2 MB */
256
257 case (AMD_ID_LV160B & 0xFF):
258 info->flash_id += FLASH_AM160B;
259 info->sector_count = 35;
260 info->size = 0x00200000;
261 break; /* => 2 MB */
262
263 case (AMD_ID_LV040B & 0xFF):
264 info->flash_id += FLASH_AM040;
265 info->sector_count = 8;
266 info->size = 0x00080000;
267 break;
268
269 case (STM_ID_M29W040B & 0xFF):
270 info->flash_id += FLASH_AM040;
271 info->sector_count = 8;
272 info->size = 0x00080000;
273 break;
274
275#if 0 /* enable when device IDs are available */
276 case (AMD_ID_LV320T & 0xFF):
277 info->flash_id += FLASH_AM320T;
278 info->sector_count = 67;
279 info->size = 0x00400000;
280 break; /* => 4 MB */
281
282 case (AMD_ID_LV320B & 0xFF):
283 info->flash_id += FLASH_AM320B;
284 info->sector_count = 67;
285 info->size = 0x00400000;
286 break; /* => 4 MB */
287#endif
288 default:
289 info->flash_id = FLASH_UNKNOWN;
290 return (0); /* => no or unknown flash */
291
292 }
293
294 printf(" ");
295 /* set up sector start address table */
296 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
297 for (i = 0; i < info->sector_count; i++) {
298 info->start[i] = base + (i * 0x00010000);
299 }
300 } else if (info->flash_id & FLASH_BTYPE) {
301 /* set sector offsets for bottom boot block type */
302 info->start[0] = base + 0x00000000;
303 info->start[1] = base + 0x00004000;
304 info->start[2] = base + 0x00006000;
305 info->start[3] = base + 0x00008000;
306 for (i = 4; i < info->sector_count; i++) {
307 info->start[i] = base + (i * 0x00010000) - 0x00030000;
308 }
309 } else {
310 /* set sector offsets for top boot block type */
311 i = info->sector_count - 1;
312 info->start[i--] = base + info->size - 0x00004000;
313 info->start[i--] = base + info->size - 0x00006000;
314 info->start[i--] = base + info->size - 0x00008000;
315 for (; i >= 0; i--) {
316 info->start[i] = base + i * 0x00010000;
317 }
318 }
319
320 /* check for protected sectors */
321 for (i = 0; i < info->sector_count; i++) {
322 /* read sector protection: D0 = 1 if protected */
323 caddr = (volatile unsigned char *)(info->start[i]);
324 info->protect[i] = caddr[2] & 1;
325 }
326
327 /*
328 * Prevent writes to uninitialized FLASH.
329 */
330 if (info->flash_id != FLASH_UNKNOWN) {
331 caddr = (vu_char *) info->start[0];
332
333 caddr[0x0555] = 0xAA;
334 caddr[0x02AA] = 0x55;
335 caddr[0x0555] = 0xF0;
336
337 udelay(20000);
338 }
339
340 return (info->size);
341}
342
343
344/*-----------------------------------------------------------------------
345 */
346
347int flash_erase(flash_info_t * info, int s_first, int s_last)
348{
349 vu_char *addr = (vu_char *) (info->start[0]);
350 int flag, prot, sect, l_sect;
351 ulong start, now, last;
352
353 if ((s_first < 0) || (s_first > s_last)) {
354 if (info->flash_id == FLASH_UNKNOWN) {
355 printf("- missing\n");
356 } else {
357 printf("- no sectors to erase\n");
358 }
359 return 1;
360 }
361
362 if ((info->flash_id == FLASH_UNKNOWN) ||
363 (info->flash_id > FLASH_AMD_COMP)) {
364 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
365 return 1;
366 }
367
368 prot = 0;
369 for (sect = s_first; sect <= s_last; ++sect) {
370 if (info->protect[sect]) {
371 prot++;
372 }
373 }
374
375 if (prot) {
376 printf("- Warning: %d protected sectors will not be erased!\n", prot);
377 } else {
378 printf("\n");
379 }
380
381 l_sect = -1;
382
383 /* Disable interrupts which might cause a timeout here */
384 flag = disable_interrupts();
385
386 addr[0x0555] = 0xAA;
387 addr[0x02AA] = 0x55;
388 addr[0x0555] = 0x80;
389 addr[0x0555] = 0xAA;
390 addr[0x02AA] = 0x55;
391
392 /* Start erase on unprotected sectors */
393 for (sect = s_first; sect <= s_last; sect++) {
394 if (info->protect[sect] == 0) { /* not protected */
395 addr = (vu_char *) (info->start[sect]);
396 addr[0] = 0x30;
397 l_sect = sect;
398 }
399 }
400
401 /* re-enable interrupts if necessary */
402 if (flag)
403 enable_interrupts();
404
405 /* wait at least 80us - let's wait 1 ms */
406 udelay(1000);
407
408 /*
409 * We wait for the last triggered sector
410 */
411 if (l_sect < 0)
412 goto DONE;
413
414 start = get_timer(0);
415 last = start;
416 addr = (vu_char *) (info->start[l_sect]);
417 while ((addr[0] & 0x80) != 0x80) {
418 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
419 printf("Timeout\n");
420 return 1;
421 }
422 /* show that we're waiting */
423 if ((now - last) > 1000) { /* every second */
424 putc('.');
425 last = now;
426 }
427 }
428
429 DONE:
430 /* reset to read mode */
431 addr = (vu_char *) info->start[0];
432 addr[0] = 0xF0; /* reset bank */
433
434 printf(" done\n");
435 return 0;
436}
437
438/*-----------------------------------------------------------------------
439 * Copy memory to flash, returns:
440 * 0 - OK
441 * 1 - write timeout
442 * 2 - Flash not erased
443 */
444
445int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
446{
447 int rc;
448
449 while (cnt > 0) {
450 if ((rc = write_byte(info, addr++, *src++)) != 0) {
451 return (rc);
452 }
453 --cnt;
454 }
455
456 return (0);
457}
458
459/*-----------------------------------------------------------------------
460 * Write a word to Flash, returns:
461 * 0 - OK
462 * 1 - write timeout
463 * 2 - Flash not erased
464 */
465static int write_byte(flash_info_t * info, ulong dest, uchar data)
466{
467 vu_char *addr = (vu_char *) (info->start[0]);
468 ulong start;
469 int flag;
470
471 /* Check if Flash is (sufficiently) erased */
472 if ((*((vu_char *) dest) & data) != data) {
473 return (2);
474 }
475 /* Disable interrupts which might cause a timeout here */
476 flag = disable_interrupts();
477
478 addr[0x0555] = 0xAA;
479 addr[0x02AA] = 0x55;
480 addr[0x0555] = 0xA0;
481
482 *((vu_char *) dest) = data;
483
484 /* re-enable interrupts if necessary */
485 if (flag)
486 enable_interrupts();
487
488 /* data polling for D7 */
489 start = get_timer(0);
490 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
491 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
492 return (1);
493 }
494 }
495 return (0);
496}
497
498/*-----------------------------------------------------------------------
499 */