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