3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
32 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33 static int write_word (flash_info_t *info, ulong dest, ulong data);
34 static void flash_get_offsets (ulong base, flash_info_t *info);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init (void)
41 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size_b0, size_b1;
46 /* Init: no FLASHes known */
47 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
51 /* Static FLASH Bank configuration here - FIXME XXX */
53 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
55 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
56 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
57 size_b0, size_b0<<20);
60 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
62 if (size_b1 > size_b0) {
64 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
68 flash_info[0].flash_id = FLASH_UNKNOWN;
69 flash_info[1].flash_id = FLASH_UNKNOWN;
70 flash_info[0].sector_count = -1;
71 flash_info[1].sector_count = -1;
72 flash_info[0].size = 0;
73 flash_info[1].size = 0;
77 /* Remap FLASH according to real size */
78 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
79 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
81 /* Re-do sizing to get full correct info */
82 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
84 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
86 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
87 /* monitor protection ON by default */
88 flash_protect(FLAG_PROTECT_SET,
89 CONFIG_SYS_MONITOR_BASE,
90 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
95 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
96 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
99 /* Re-do sizing to get full correct info */
100 size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE + size_b0),
103 flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
105 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
106 /* monitor protection ON by default */
107 flash_protect(FLAG_PROTECT_SET,
108 CONFIG_SYS_MONITOR_BASE,
109 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
113 memctl->memc_br1 = 0; /* invalidate bank */
115 flash_info[1].flash_id = FLASH_UNKNOWN;
116 flash_info[1].sector_count = -1;
119 flash_info[0].size = size_b0;
120 flash_info[1].size = size_b1;
122 return (size_b0 + size_b1);
125 /*-----------------------------------------------------------------------
127 static void flash_get_offsets (ulong base, flash_info_t *info)
131 /* set up sector start address table */
132 if (info->flash_id & FLASH_BTYPE) {
133 /* set sector offsets for bottom boot block type */
134 info->start[0] = base + 0x00000000;
135 info->start[1] = base + 0x00008000;
136 info->start[2] = base + 0x0000C000;
137 info->start[3] = base + 0x00010000;
138 for (i = 4; i < info->sector_count; i++) {
139 info->start[i] = base + (i * 0x00020000) - 0x00060000;
142 /* set sector offsets for top boot block type */
143 i = info->sector_count - 1;
144 info->start[i--] = base + info->size - 0x00008000;
145 info->start[i--] = base + info->size - 0x0000C000;
146 info->start[i--] = base + info->size - 0x00010000;
147 for (; i >= 0; i--) {
148 info->start[i] = base + i * 0x00020000;
154 /*-----------------------------------------------------------------------
156 void flash_print_info (flash_info_t *info)
160 if (info->flash_id == FLASH_UNKNOWN) {
161 printf ("missing or unknown FLASH type\n");
165 switch (info->flash_id & FLASH_VENDMASK) {
166 case FLASH_MAN_AMD: printf ("AMD "); break;
167 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
168 default: printf ("Unknown Vendor "); break;
171 switch (info->flash_id & FLASH_TYPEMASK) {
172 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
174 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
176 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
178 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
180 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
182 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
184 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
186 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
188 default: printf ("Unknown Chip Type\n");
192 printf (" Size: %ld MB in %d Sectors\n",
193 info->size >> 20, info->sector_count);
195 printf (" Sector Start Addresses:");
196 for (i=0; i<info->sector_count; ++i) {
201 info->protect[i] ? " (RO)" : " "
208 /*-----------------------------------------------------------------------
212 /*-----------------------------------------------------------------------
216 * The following code cannot be run from FLASH!
219 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
223 ulong base = (ulong)addr;
226 /* Write auto select command: read Manufacturer ID */
227 addr[0x0555] = 0x00AA00AA;
228 addr[0x02AA] = 0x00550055;
229 addr[0x0555] = 0x00900090;
235 info->flash_id = FLASH_MAN_AMD;
238 info->flash_id = FLASH_MAN_FUJ;
241 info->flash_id = FLASH_UNKNOWN;
242 info->sector_count = 0;
244 return (0); /* no or unknown flash */
247 value = addr[1]; /* device ID */
251 info->flash_id += FLASH_AM400T;
252 info->sector_count = 11;
253 info->size = 0x00100000;
257 info->flash_id += FLASH_AM400B;
258 info->sector_count = 11;
259 info->size = 0x00100000;
263 info->flash_id += FLASH_AM800T;
264 info->sector_count = 19;
265 info->size = 0x00200000;
269 info->flash_id += FLASH_AM800B;
270 info->sector_count = 19;
271 info->size = 0x00200000;
275 info->flash_id += FLASH_AM160T;
276 info->sector_count = 35;
277 info->size = 0x00400000;
281 info->flash_id += FLASH_AM160B;
282 info->sector_count = 35;
283 info->size = 0x00400000;
285 #if 0 /* enable when device IDs are available */
287 info->flash_id += FLASH_AM320T;
288 info->sector_count = 67;
289 info->size = 0x00800000;
293 info->flash_id += FLASH_AM320B;
294 info->sector_count = 67;
295 info->size = 0x00800000;
299 info->flash_id = FLASH_UNKNOWN;
300 return (0); /* => no or unknown flash */
304 /* set up sector start address table */
305 if (info->flash_id & FLASH_BTYPE) {
306 /* set sector offsets for bottom boot block type */
307 info->start[0] = base + 0x00000000;
308 info->start[1] = base + 0x00008000;
309 info->start[2] = base + 0x0000C000;
310 info->start[3] = base + 0x00010000;
311 for (i = 4; i < info->sector_count; i++) {
312 info->start[i] = base + (i * 0x00020000) - 0x00060000;
315 /* set sector offsets for top boot block type */
316 i = info->sector_count - 1;
317 info->start[i--] = base + info->size - 0x00008000;
318 info->start[i--] = base + info->size - 0x0000C000;
319 info->start[i--] = base + info->size - 0x00010000;
320 for (; i >= 0; i--) {
321 info->start[i] = base + i * 0x00020000;
325 /* check for protected sectors */
326 for (i = 0; i < info->sector_count; i++) {
327 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
328 /* D0 = 1 if protected */
329 addr = (volatile unsigned long *)(info->start[i]);
330 info->protect[i] = addr[2] & 1;
334 * Prevent writes to uninitialized FLASH.
336 if (info->flash_id != FLASH_UNKNOWN) {
337 addr = (volatile unsigned long *)info->start[0];
339 *addr = 0x00F000F0; /* reset bank */
346 /*-----------------------------------------------------------------------
349 int flash_erase (flash_info_t *info, int s_first, int s_last)
351 vu_long *addr = (vu_long*)(info->start[0]);
352 int flag, prot, sect, l_sect;
353 ulong start, now, last;
355 if ((s_first < 0) || (s_first > s_last)) {
356 if (info->flash_id == FLASH_UNKNOWN) {
357 printf ("- missing\n");
359 printf ("- no sectors to erase\n");
364 if ((info->flash_id == FLASH_UNKNOWN) ||
365 (info->flash_id > FLASH_AMD_COMP)) {
366 printf ("Can't erase unknown flash type %08lx - aborted\n",
372 for (sect=s_first; sect<=s_last; ++sect) {
373 if (info->protect[sect]) {
379 printf ("- Warning: %d protected sectors will not be erased!\n",
387 /* Disable interrupts which might cause a timeout here */
388 flag = disable_interrupts();
390 addr[0x0555] = 0x00AA00AA;
391 addr[0x02AA] = 0x00550055;
392 addr[0x0555] = 0x00800080;
393 addr[0x0555] = 0x00AA00AA;
394 addr[0x02AA] = 0x00550055;
396 /* Start erase on unprotected sectors */
397 for (sect = s_first; sect<=s_last; sect++) {
398 if (info->protect[sect] == 0) { /* not protected */
399 addr = (vu_long*)(info->start[sect]);
400 addr[0] = 0x00300030;
405 /* re-enable interrupts if necessary */
409 /* wait at least 80us - let's wait 1 ms */
413 * We wait for the last triggered sector
418 start = get_timer (0);
420 addr = (vu_long*)(info->start[l_sect]);
421 while ((addr[0] & 0x00800080) != 0x00800080) {
422 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
423 printf ("Timeout\n");
426 /* show that we're waiting */
427 if ((now - last) > 1000) { /* every second */
434 /* reset to read mode */
435 addr = (volatile unsigned long *)info->start[0];
436 addr[0] = 0x00F000F0; /* reset bank */
442 /*-----------------------------------------------------------------------
443 * Copy memory to flash, returns:
446 * 2 - Flash not erased
449 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
454 wp = (addr & ~3); /* get lower word aligned address */
457 * handle unaligned start bytes
459 if ((l = addr - wp) != 0) {
461 for (i=0, cp=wp; i<l; ++i, ++cp) {
462 data = (data << 8) | (*(uchar *)cp);
464 for (; i<4 && cnt>0; ++i) {
465 data = (data << 8) | *src++;
469 for (; cnt==0 && i<4; ++i, ++cp) {
470 data = (data << 8) | (*(uchar *)cp);
473 if ((rc = write_word(info, wp, data)) != 0) {
480 * handle word aligned part
484 for (i=0; i<4; ++i) {
485 data = (data << 8) | *src++;
487 if ((rc = write_word(info, wp, data)) != 0) {
499 * handle unaligned tail bytes
502 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
503 data = (data << 8) | *src++;
506 for (; i<4; ++i, ++cp) {
507 data = (data << 8) | (*(uchar *)cp);
510 return (write_word(info, wp, data));
513 /*-----------------------------------------------------------------------
514 * Write a word to Flash, returns:
517 * 2 - Flash not erased
519 static int write_word (flash_info_t *info, ulong dest, ulong data)
521 vu_long *addr = (vu_long*)(info->start[0]);
525 /* Check if Flash is (sufficiently) erased */
526 if ((*((vu_long *)dest) & data) != data) {
529 /* Disable interrupts which might cause a timeout here */
530 flag = disable_interrupts();
532 addr[0x0555] = 0x00AA00AA;
533 addr[0x02AA] = 0x00550055;
534 addr[0x0555] = 0x00A000A0;
536 *((vu_long *)dest) = data;
538 /* re-enable interrupts if necessary */
542 /* data polling for D7 */
543 start = get_timer (0);
544 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
545 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
552 /*-----------------------------------------------------------------------