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 #if defined(CONFIG_ENV_IS_IN_FLASH)
30 # ifndef CONFIG_ENV_ADDR
31 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
33 # ifndef CONFIG_ENV_SIZE
34 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
36 # ifndef CONFIG_ENV_SECT_SIZE
37 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
41 /*-----------------------------------------------------------------------
44 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
45 static int write_word (flash_info_t *info, ulong dest, ulong data);
46 static void flash_get_offsets (ulong base, flash_info_t *info);
48 /*-----------------------------------------------------------------------
51 unsigned long flash_init (void)
53 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
54 volatile memctl8xx_t *memctl = &immap->im_memctl;
55 unsigned long size_b0;
58 /* Init: no FLASHes known */
59 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
60 flash_info[i].flash_id = FLASH_UNKNOWN;
62 /* Static FLASH Bank configuration here - FIXME XXX */
64 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
66 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
67 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
72 /* Remap FLASH according to real size */
73 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
74 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
76 /* Re-do sizing to get full correct info */
77 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
79 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
81 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
82 /* monitor protection ON by default */
83 flash_protect(FLAG_PROTECT_SET,
84 CONFIG_SYS_MONITOR_BASE,
85 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
89 #ifdef CONFIG_ENV_IS_IN_FLASH
90 /* ENV protection ON by default */
91 flash_protect(FLAG_PROTECT_SET,
93 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
97 /* ICU862 Board has only one Flash Bank */
98 flash_info[0].size = size_b0;
104 /*-----------------------------------------------------------------------
106 static void flash_get_offsets (ulong base, flash_info_t *info)
110 /* set up sector start address table */
111 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
112 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM033C)) {
113 /* set sector offsets for uniform sector type */
114 for (i = 0; i < info->sector_count; i++) {
115 info->start[i] = base + (i * 0x00040000);
120 /*-----------------------------------------------------------------------
122 void flash_print_info (flash_info_t *info)
126 if (info->flash_id == FLASH_UNKNOWN) {
127 puts ("missing or unknown FLASH type\n");
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_AMD: puts ("AMD "); break;
133 case FLASH_MAN_FUJ: puts ("FUJITSU "); break;
134 case FLASH_MAN_BM: puts ("BRIGHT MICRO "); break;
135 default: puts ("Unknown Vendor "); break;
138 switch (info->flash_id & FLASH_TYPEMASK) {
139 case FLASH_AM040: puts ("29F040/29LV040 (4 Mbit, uniform sectors)\n");
141 case FLASH_AM400B: puts ("AM29LV400B (4 Mbit, bottom boot sect)\n");
143 case FLASH_AM400T: puts ("AM29LV400T (4 Mbit, top boot sector)\n");
145 case FLASH_AM800B: puts ("AM29LV800B (8 Mbit, bottom boot sect)\n");
147 case FLASH_AM800T: puts ("AM29LV800T (8 Mbit, top boot sector)\n");
149 case FLASH_AM160B: puts ("AM29LV160B (16 Mbit, bottom boot sect)\n");
151 case FLASH_AM160T: puts ("AM29LV160T (16 Mbit, top boot sector)\n");
153 case FLASH_AM320B: puts ("AM29LV320B (32 Mbit, bottom boot sect)\n");
155 case FLASH_AM320T: puts ("AM29LV320T (32 Mbit, top boot sector)\n");
157 case FLASH_AM033C: puts ("AM29LV033C (32 Mbit)\n");
159 default: puts ("Unknown Chip Type\n");
163 printf (" Size: %ld MB in %d Sectors\n",
164 info->size >> 20, info->sector_count);
166 puts (" Sector Start Addresses:");
168 for (i=0; i<info->sector_count; ++i) {
175 info->protect[i] ? " (RO)" : " "
182 /*-----------------------------------------------------------------------
186 * The following code cannot be run from FLASH!
189 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
193 ulong base = (ulong)addr;
197 /* Write auto select command: read Manufacturer ID */
199 addr[0x0555] = 0x00AA00AA;
200 addr[0x02AA] = 0x00550055;
201 addr[0x0555] = 0x00900090;
203 addr[0x0555] = 0xAAAAAAAA;
204 addr[0x02AA] = 0x55555555;
205 addr[0x0555] = 0x90909090;
210 switch (value + (value << 16)) {
212 info->flash_id = FLASH_MAN_AMD;
216 info->flash_id = FLASH_MAN_FUJ;
220 info->flash_id = FLASH_UNKNOWN;
221 info->sector_count = 0;
226 value = addr[1]; /* device ID */
228 switch ((unsigned long)value) {
230 info->flash_id += FLASH_AM040;
231 info->sector_count = 8;
232 info->size = 0x00200000;
236 info->flash_id += FLASH_AM400T;
237 info->sector_count = 11;
238 info->size = 0x00100000;
242 info->flash_id += FLASH_AM400B;
243 info->sector_count = 11;
244 info->size = 0x00100000;
248 info->flash_id += FLASH_AM800T;
249 info->sector_count = 19;
250 info->size = 0x00200000;
254 info->flash_id += FLASH_AM800B;
255 info->sector_count = 19;
256 info->size = 0x00200000;
260 info->flash_id += FLASH_AM160T;
261 info->sector_count = 35;
262 info->size = 0x00400000;
266 info->flash_id += FLASH_AM160B;
267 info->sector_count = 35;
268 info->size = 0x00400000;
270 #if 0 /* enable when device IDs are available */
272 info->flash_id += FLASH_AM320T;
273 info->sector_count = 67;
274 info->size = 0x00800000;
278 info->flash_id += FLASH_AM320B;
279 info->sector_count = 67;
280 info->size = 0x00800000;
284 info->flash_id += FLASH_AM033C;
285 info->sector_count = 64;
286 info->size = 0x01000000;
289 info->flash_id = FLASH_UNKNOWN;
290 return (0); /* => no or unknown flash */
295 /* set up sector start address table */
296 if (info->flash_id & FLASH_BTYPE) {
297 /* set sector offsets for bottom boot block type */
298 info->start[0] = base + 0x00000000;
299 info->start[1] = base + 0x00008000;
300 info->start[2] = base + 0x0000C000;
301 info->start[3] = base + 0x00010000;
302 for (i = 4; i < info->sector_count; i++) {
303 info->start[i] = base + (i * 0x00020000) - 0x00060000;
306 /* set sector offsets for top boot block type */
307 i = info->sector_count - 1;
308 info->start[i--] = base + info->size - 0x00008000;
309 info->start[i--] = base + info->size - 0x0000C000;
310 info->start[i--] = base + info->size - 0x00010000;
311 for (; i >= 0; i--) {
312 info->start[i] = base + i * 0x00020000;
316 flash_get_offsets ((ulong)addr, &flash_info[0]);
319 /* check for protected sectors */
320 for (i = 0; i < info->sector_count; i++) {
321 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
322 /* D0 = 1 if protected */
323 addr = (volatile unsigned long *)(info->start[i]);
325 /* We don't know why it happens, but on ICU Board *
326 * for AMD29033C flash we need to resend the command of *
327 * reading flash protection for upper 8 Mb of flash */
329 addr[0x0555] = 0xAAAAAAAA;
330 addr[0x02AA] = 0x55555555;
331 addr[0x0555] = 0x90909090;
334 info->protect[i] = addr[2] & 1;
338 * Prevent writes to uninitialized FLASH.
340 if (info->flash_id != FLASH_UNKNOWN) {
341 addr = (volatile unsigned long *)info->start[0];
343 *addr = 0x00F000F0; /* reset bank */
345 *addr = 0xF0F0F0F0; /* reset bank */
353 /*-----------------------------------------------------------------------
356 int flash_erase (flash_info_t *info, int s_first, int s_last)
358 vu_long *addr = (vu_long*)(info->start[0]);
359 int flag, prot, sect, l_sect;
360 ulong start, now, last;
362 if ((s_first < 0) || (s_first > s_last)) {
363 if (info->flash_id == FLASH_UNKNOWN) {
364 puts ("- missing\n");
366 puts ("- no sectors to erase\n");
371 if ((info->flash_id == FLASH_UNKNOWN) ||
372 (info->flash_id > FLASH_AMD_COMP)) {
373 puts ("Can't erase unknown flash type - aborted\n");
378 for (sect=s_first; sect<=s_last; ++sect) {
379 if (info->protect[sect]) {
385 printf ("- Warning: %d protected sectors will not be erased!\n",
393 /* Disable interrupts which might cause a timeout here */
394 flag = disable_interrupts();
397 addr[0x0555] = 0x00AA00AA;
398 addr[0x02AA] = 0x00550055;
399 addr[0x0555] = 0x00800080;
400 addr[0x0555] = 0x00AA00AA;
401 addr[0x02AA] = 0x00550055;
403 addr[0x0555] = 0xAAAAAAAA;
404 addr[0x02AA] = 0x55555555;
405 addr[0x0555] = 0x80808080;
406 addr[0x0555] = 0xAAAAAAAA;
407 addr[0x02AA] = 0x55555555;
410 /* Start erase on unprotected sectors */
411 for (sect = s_first; sect<=s_last; sect++) {
412 if (info->protect[sect] == 0) { /* not protected */
413 addr = (vu_long*)(info->start[sect]);
415 addr[0] = 0x00300030;
417 addr[0] = 0x30303030;
423 /* re-enable interrupts if necessary */
427 /* wait at least 80us - let's wait 1 ms */
431 * We wait for the last triggered sector
436 start = get_timer (0);
438 addr = (vu_long*)(info->start[l_sect]);
440 while ((addr[0] & 0x00800080) != 0x00800080)
442 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
445 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
449 /* show that we're waiting */
450 if ((now - last) > 1000) { /* every second */
457 /* reset to read mode */
458 addr = (volatile unsigned long *)info->start[0];
460 addr[0] = 0x00F000F0; /* reset bank */
462 addr[0] = 0xF0F0F0F0; /* reset bank */
469 /*-----------------------------------------------------------------------
470 * Copy memory to flash, returns:
473 * 2 - Flash not erased
476 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
481 wp = (addr & ~3); /* get lower word aligned address */
484 * handle unaligned start bytes
486 if ((l = addr - wp) != 0) {
488 for (i=0, cp=wp; i<l; ++i, ++cp) {
489 data = (data << 8) | (*(uchar *)cp);
491 for (; i<4 && cnt>0; ++i) {
492 data = (data << 8) | *src++;
496 for (; cnt==0 && i<4; ++i, ++cp) {
497 data = (data << 8) | (*(uchar *)cp);
500 if ((rc = write_word(info, wp, data)) != 0) {
507 * handle word aligned part
511 for (i=0; i<4; ++i) {
512 data = (data << 8) | *src++;
514 if ((rc = write_word(info, wp, data)) != 0) {
526 * handle unaligned tail bytes
529 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
530 data = (data << 8) | *src++;
533 for (; i<4; ++i, ++cp) {
534 data = (data << 8) | (*(uchar *)cp);
537 return (write_word(info, wp, data));
540 /*-----------------------------------------------------------------------
541 * Write a word to Flash, returns:
544 * 2 - Flash not erased
546 static int write_word (flash_info_t *info, ulong dest, ulong data)
548 vu_long *addr = (vu_long*)(info->start[0]);
552 /* Check if Flash is (sufficiently) erased */
553 if ((*((vu_long *)dest) & data) != data) {
556 /* Disable interrupts which might cause a timeout here */
557 flag = disable_interrupts();
560 addr[0x0555] = 0x00AA00AA;
561 addr[0x02AA] = 0x00550055;
562 addr[0x0555] = 0x00A000A0;
564 addr[0x0555] = 0xAAAAAAAA;
565 addr[0x02AA] = 0x55555555;
566 addr[0x0555] = 0xA0A0A0A0;
569 *((vu_long *)dest) = data;
571 /* re-enable interrupts if necessary */
575 /* data polling for D7 */
576 start = get_timer (0);
578 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
580 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
583 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
590 /*-----------------------------------------------------------------------