2 * (C) Copyright 2000-2002
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,
28 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
31 #define CONFIG_FLASH_16BIT
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
35 /*-----------------------------------------------------------------------
38 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
39 static int write_word (flash_info_t *info, ulong dest, ulong data);
41 /*-----------------------------------------------------------------------
44 unsigned long flash_init (void)
46 volatile immap_t *immap = (immap_t *)CFG_IMMR;
47 volatile memctl8xx_t *memctl = &immap->im_memctl;
48 unsigned long size_b0;
51 /* Init: no FLASHes known */
52 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
53 flash_info[i].flash_id = FLASH_UNKNOWN;
56 /* Static FLASH Bank configuration here - FIXME XXX */
58 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
60 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
61 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
62 size_b0, size_b0<<20);
66 /* Remap FLASH according to real size */
67 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
68 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16;
70 /* Re-do sizing to get full correct info */
71 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
73 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
74 /* monitor protection ON by default */
75 flash_protect(FLAG_PROTECT_SET,
77 CFG_MONITOR_BASE+monitor_flash_len-1,
81 #ifdef CFG_ENV_IS_IN_FLASH
82 /* ENV protection ON by default */
83 flash_protect(FLAG_PROTECT_SET,
85 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
89 flash_info[0].size = size_b0;
94 /*-----------------------------------------------------------------------
96 void flash_print_info (flash_info_t *info)
100 if (info->flash_id == FLASH_UNKNOWN) {
101 printf ("missing or unknown FLASH type\n");
105 switch (info->flash_id & FLASH_VENDMASK) {
106 case FLASH_MAN_AMD: printf ("AMD "); break;
107 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
108 default: printf ("Unknown Vendor "); break;
111 switch (info->flash_id & FLASH_TYPEMASK) {
112 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
114 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
116 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
118 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
120 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
122 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
124 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
126 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
128 default: printf ("Unknown Chip Type\n");
132 printf (" Size: %ld MB in %d Sectors\n",
133 info->size >> 20, info->sector_count);
135 printf (" Sector Start Addresses:");
136 for (i=0; i<info->sector_count; ++i) {
141 info->protect[i] ? " (RO)" : " "
148 /*-----------------------------------------------------------------------
152 /*-----------------------------------------------------------------------
156 * The following code cannot be run from FLASH!
159 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
163 ulong base = (ulong)addr;
165 /* Write auto select command: read Manufacturer ID */
166 vu_short *s_addr=(vu_short*)addr;
167 s_addr[0x5555] = 0x00AA;
168 s_addr[0x2AAA] = 0x0055;
169 s_addr[0x5555] = 0x0090;
172 value = value|(value<<16);
176 info->flash_id = FLASH_MAN_FUJ;
179 info->flash_id = FLASH_UNKNOWN;
180 info->sector_count = 0;
182 return (0); /* no or unknown flash */
186 value = value|(value<<16);
189 case FUJI_ID_29F800BA:
190 info->flash_id += FLASH_AM400T;
191 info->sector_count = 19;
192 info->size = 0x00100000;
195 info->flash_id = FLASH_UNKNOWN;
196 return (0); /* => no or unknown flash */
199 /* set up sector start address table */
200 /* set sector offsets for bottom boot block type */
201 info->start[0] = base + 0x00000000;
202 info->start[1] = base + 0x00004000;
203 info->start[2] = base + 0x00006000;
204 info->start[3] = base + 0x00008000;
205 for (i = 4; i < info->sector_count; i++) {
206 info->start[i] = base + (i * 0x00010000) - 0x00030000;
210 /* check for protected sectors */
211 for (i = 0; i < info->sector_count; i++) {
212 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
213 /* D0 = 1 if protected */
214 s_addr = (volatile unsigned short *)(info->start[i]);
215 info->protect[i] = s_addr[2] & 1;
219 * Prevent writes to uninitialized FLASH.
221 if (info->flash_id != FLASH_UNKNOWN) {
222 s_addr = (volatile unsigned short *)info->start[0];
223 *s_addr = 0x00F0; /* reset bank */
229 /*-----------------------------------------------------------------------
233 int flash_erase (flash_info_t *info, int s_first, int s_last)
235 vu_long *addr = (vu_long*)(info->start[0]);
236 int flag, prot, sect;
237 ulong start, now, last;
238 #ifdef CONFIG_FLASH_16BIT
239 vu_short *s_addr = (vu_short*)addr;
242 if ((s_first < 0) || (s_first > s_last)) {
243 if (info->flash_id == FLASH_UNKNOWN) {
244 printf ("- missing\n");
246 printf ("- no sectors to erase\n");
250 /*#ifndef CONFIG_FLASH_16BIT
252 type = (info->flash_id & FLASH_VENDMASK);
253 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
254 printf ("Can't erase unknown flash type %08lx - aborted\n",
260 for (sect=s_first; sect<=s_last; ++sect) {
261 if (info->protect[sect]) {
267 printf ("- Warning: %d protected sectors will not be erased!\n",
273 start = get_timer (0);
275 /* Start erase on unprotected sectors */
276 for (sect = s_first; sect<=s_last; sect++) {
277 if (info->protect[sect] == 0) { /* not protected */
278 #ifdef CONFIG_FLASH_16BIT
279 vu_short *s_sect_addr = (vu_short*)(info->start[sect]);
281 vu_long *sect_addr = (vu_long*)(info->start[sect]);
283 /* Disable interrupts which might cause a timeout here */
284 flag = disable_interrupts();
286 #ifdef CONFIG_FLASH_16BIT
288 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
289 s_addr[0x5555] = 0x00AA;
290 s_addr[0x2AAA] = 0x0055;
291 s_addr[0x5555] = 0x0080;
292 s_addr[0x5555] = 0x00AA;
293 s_addr[0x2AAA] = 0x0055;
294 s_sect_addr[0] = 0x0030;
296 addr[0x5555] = 0x00AA00AA;
297 addr[0x2AAA] = 0x00550055;
298 addr[0x5555] = 0x00800080;
299 addr[0x5555] = 0x00AA00AA;
300 addr[0x2AAA] = 0x00550055;
301 sect_addr[0] = 0x00300030;
303 /* re-enable interrupts if necessary */
307 /* wait at least 80us - let's wait 1 ms */
310 #ifdef CONFIG_FLASH_16BIT
311 while ((s_sect_addr[0] & 0x0080) != 0x0080) {
313 while ((sect_addr[0] & 0x00800080) != 0x00800080) {
315 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
316 printf ("Timeout\n");
319 /* show that we're waiting */
320 if ((now - last) > 1000) { /* every second */
328 /* reset to read mode */
329 addr = (volatile unsigned long *)info->start[0];
330 #ifdef CONFIG_FLASH_16BIT
331 s_addr[0] = 0x00F0; /* reset bank */
333 addr[0] = 0x00F000F0; /* reset bank */
341 /*-----------------------------------------------------------------------
342 * Copy memory to flash, returns:
345 * 2 - Flash not erased
348 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
353 wp = (addr & ~3); /* get lower word aligned address */
356 * handle unaligned start bytes
358 if ((l = addr - wp) != 0) {
360 for (i=0, cp=wp; i<l; ++i, ++cp) {
361 data = (data << 8) | (*(uchar *)cp);
363 for (; i<4 && cnt>0; ++i) {
364 data = (data << 8) | *src++;
368 for (; cnt==0 && i<4; ++i, ++cp) {
369 data = (data << 8) | (*(uchar *)cp);
372 if ((rc = write_word(info, wp, data)) != 0) {
379 * handle word aligned part
383 for (i=0; i<4; ++i) {
384 data = (data << 8) | *src++;
386 if ((rc = write_word(info, wp, data)) != 0) {
398 * handle unaligned tail bytes
401 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
402 data = (data << 8) | *src++;
405 for (; i<4; ++i, ++cp) {
406 data = (data << 8) | (*(uchar *)cp);
409 return (write_word(info, wp, data));
413 /*-----------------------------------------------------------------------
414 * Write a word to Flash, returns:
417 * 2 - Flash not erased
419 static int write_word (flash_info_t *info, ulong dest, ulong data)
421 vu_long *addr = (vu_long*)(info->start[0]);
423 #ifdef CONFIG_FLASH_16BIT
426 vu_short *s_addr = (vu_short*)addr;
431 /* Check if Flash is (sufficiently) erased */
432 if ((*((vu_long *)dest) & data) != data) {
436 #ifdef CONFIG_FLASH_16BIT
437 /* Write the 16 higher-bits */
438 /* Disable interrupts which might cause a timeout here */
439 flag = disable_interrupts();
441 high_data = ((data>>16) & 0x0000ffff);
443 s_addr[0x5555] = 0x00AA;
444 s_addr[0x2AAA] = 0x0055;
445 s_addr[0x5555] = 0x00A0;
447 *((vu_short *)dest) = high_data;
450 /* re-enable interrupts if necessary */
454 /* data polling for D7 */
455 start = get_timer (0);
456 while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
457 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
463 /* Write the 16 lower-bits */
466 /* Disable interrupts which might cause a timeout here */
467 flag = disable_interrupts();
468 #ifdef CONFIG_FLASH_16BIT
470 low_data = (data & 0x0000ffff);
472 s_addr[0x5555] = 0x00AA;
473 s_addr[0x2AAA] = 0x0055;
474 s_addr[0x5555] = 0x00A0;
475 *((vu_short *)dest) = low_data;
478 addr[0x5555] = 0x00AA00AA;
479 addr[0x2AAA] = 0x00550055;
480 addr[0x5555] = 0x00A000A0;
481 *((vu_long *)dest) = data;
484 /* re-enable interrupts if necessary */
488 /* data polling for D7 */
489 start = get_timer (0);
491 #ifdef CONFIG_FLASH_16BIT
492 while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
494 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
497 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {