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