3 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 /*---------------------------------------------------------------------*/
37 #define DEBUGF(fmt,args...) printf(fmt ,##args)
39 #define DEBUGF(fmt,args...)
41 /*---------------------------------------------------------------------*/
43 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
45 static ulong flash_get_size (ulong addr, flash_info_t *info);
46 static int flash_get_offsets (ulong base, flash_info_t *info);
47 static int write_word (flash_info_t *info, ulong dest, ulong data);
48 static void flash_reset (ulong addr);
52 static void flash_to_xd(void)
58 if (flash_xd_nest == 1)
60 DEBUGF("Flash on XD\n");
61 x = pci_read_cfg_byte(0, 0, 0x74);
62 pci_write_cfg_byte(0, 0, 0x74, x|1);
66 static void flash_to_mem(void)
72 if (flash_xd_nest == 0)
74 DEBUGF("Flash on memory bus\n");
75 x = pci_read_cfg_byte(0, 0, 0x74);
76 pci_write_cfg_byte(0, 0, 0x74, x&0xFE);
80 unsigned long flash_init_old(void)
84 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
86 flash_info[i].flash_id = FLASH_UNKNOWN;
87 flash_info[i].sector_count = 0;
88 flash_info[i].size = 0;
95 unsigned long flash_init (void)
98 unsigned long flash_size = 0;
104 /* Init: no FLASHes known */
105 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
106 flash_info[i].flash_id = FLASH_UNKNOWN;
107 flash_info[i].sector_count = 0;
108 flash_info[i].size = 0;
111 DEBUGF("\n## Get flash size @ 0x%08x\n", CFG_FLASH_BASE);
113 flash_size = flash_get_size (CFG_FLASH_BASE, flash_info);
115 DEBUGF("## Flash bank size: %08lx\n", flash_size);
118 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE && \
119 CFG_MONITOR_BASE < CFG_FLASH_BASE + CFG_FLASH_MAX_SIZE
120 /* monitor protection ON by default */
121 flash_protect(FLAG_PROTECT_SET,
123 CFG_MONITOR_BASE + monitor_flash_len - 1,
127 #ifdef CFG_ENV_IS_IN_FLASH
128 /* ENV protection ON by default */
129 flash_protect(FLAG_PROTECT_SET,
131 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
136 puts ("Warning: the BOOT Flash is not initialised !");
145 * The following code cannot be run from FLASH!
147 static ulong flash_get_size (ulong addr, flash_info_t *info)
151 uchar *x = (uchar *)addr;
155 /* Write auto select command: read Manufacturer ID */
157 __asm volatile ("sync\n eieio");
159 __asm volatile ("sync\n eieio");
161 __asm volatile ("sync\n eieio");
164 __asm volatile ("sync\n eieio");
166 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, value);
168 switch (value | (value << 16)) {
170 info->flash_id = FLASH_MAN_AMD;
174 info->flash_id = FLASH_MAN_FUJ;
178 info->flash_id = FLASH_MAN_STM;
182 info->flash_id = FLASH_UNKNOWN;
183 info->sector_count = 0;
190 __asm volatile ("sync\n eieio");
192 DEBUGF("Device ID @ 0x%08lx: 0x%08x\n", addr+1, value);
196 DEBUGF("Am29F040B\n");
197 info->flash_id += FLASH_AM040;
198 info->sector_count = 8;
199 info->size = 0x00080000;
200 break; /* => 512 kB */
203 DEBUGF("Am29LV040B\n");
204 info->flash_id += FLASH_AM040;
205 info->sector_count = 8;
206 info->size = 0x00080000;
207 break; /* => 512 kB */
210 DEBUGF("Am29LV400T\n");
211 info->flash_id += FLASH_AM400T;
212 info->sector_count = 11;
213 info->size = 0x00100000;
217 DEBUGF("Am29LV400B\n");
218 info->flash_id += FLASH_AM400B;
219 info->sector_count = 11;
220 info->size = 0x00100000;
224 DEBUGF("Am29LV800T\n");
225 info->flash_id += FLASH_AM800T;
226 info->sector_count = 19;
227 info->size = 0x00200000;
231 DEBUGF("Am29LV400B\n");
232 info->flash_id += FLASH_AM800B;
233 info->sector_count = 19;
234 info->size = 0x00200000;
238 DEBUGF("Am29LV160T\n");
239 info->flash_id += FLASH_AM160T;
240 info->sector_count = 35;
241 info->size = 0x00400000;
245 DEBUGF("Am29LV160B\n");
246 info->flash_id += FLASH_AM160B;
247 info->sector_count = 35;
248 info->size = 0x00400000;
252 DEBUGF("Am29LV320T\n");
253 info->flash_id += FLASH_AM320T;
254 info->sector_count = 67;
255 info->size = 0x00800000;
259 /* Has the same ID as AMD_ID_LV320T, to be fixed */
261 DEBUGF("Am29LV320B\n");
262 info->flash_id += FLASH_AM320B;
263 info->sector_count = 67;
264 info->size = 0x00800000;
269 DEBUGF("Am29LV033C\n");
270 info->flash_id += FLASH_AM033C;
271 info->sector_count = 64;
272 info->size = 0x01000000;
276 DEBUGF("M29F040B\n");
277 info->flash_id += FLASH_AM040;
278 info->sector_count = 8;
279 info->size = 0x00080000;
280 break; /* => 512 kB */
283 info->flash_id = FLASH_UNKNOWN;
286 return (0); /* => no or unknown flash */
290 if (info->sector_count > CFG_MAX_FLASH_SECT) {
291 printf ("** ERROR: sector count %d > max (%d) **\n",
292 info->sector_count, CFG_MAX_FLASH_SECT);
293 info->sector_count = CFG_MAX_FLASH_SECT;
296 if (! flash_get_offsets (addr, info)) {
302 /* check for protected sectors */
303 for (i = 0; i < info->sector_count; i++) {
304 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
305 /* D0 = 1 if protected */
306 value = in8(info->start[i] + 2);
308 info->protect[i] = (value & 1) != 0;
312 * Reset bank to read mode
321 static int flash_get_offsets (ulong base, flash_info_t *info)
325 switch (info->flash_id & FLASH_TYPEMASK) {
327 /* set sector offsets for uniform sector type */
328 for (i = 0; i < info->sector_count; i++) {
329 info->start[i] = base + i * info->size /
340 int flash_erase (flash_info_t *info, int s_first, int s_last)
342 volatile ulong addr = info->start[0];
343 int flag, prot, sect, l_sect;
344 ulong start, now, last;
348 if (s_first < 0 || s_first > s_last) {
349 if (info->flash_id == FLASH_UNKNOWN) {
350 printf ("- missing\n");
352 printf ("- no sectors to erase\n");
358 if (info->flash_id == FLASH_UNKNOWN) {
359 printf ("Can't erase unknown flash type %08lx - aborted\n",
366 for (sect=s_first; sect<=s_last; ++sect) {
367 if (info->protect[sect]) {
373 printf ("- Warning: %d protected sectors will not be erased!\n",
381 /* Disable interrupts which might cause a timeout here */
382 flag = disable_interrupts();
384 out8(addr + 0x555, 0xAA);
386 out8(addr + 0x2AA, 0x55);
388 out8(addr + 0x555, 0x80);
390 out8(addr + 0x555, 0xAA);
392 out8(addr + 0x2AA, 0x55);
395 /* Start erase on unprotected sectors */
396 for (sect = s_first; sect<=s_last; sect++) {
397 if (info->protect[sect] == 0) { /* not protected */
398 addr = info->start[sect];
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 = info->start[l_sect];
422 DEBUGF ("Start erase timeout: %d\n", CFG_FLASH_ERASE_TOUT);
424 while ((in8(addr) & 0x80) != 0x80) {
425 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
426 printf ("Timeout\n");
427 flash_reset (info->start[0]);
431 /* show that we're waiting */
432 if ((now - last) > 1000) { /* every second */
440 /* reset to read mode */
441 flash_reset (info->start[0]);
449 * Copy memory to flash, returns:
452 * 2 - Flash not erased
454 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
461 wp = (addr & ~3); /* get lower word aligned address */
464 * handle unaligned start bytes
466 if ((l = addr - wp) != 0) {
468 for (i=0, cp=wp; i<l; ++i, ++cp) {
469 data = (data << 8) | (*(uchar *)cp);
471 for (; i<4 && cnt>0; ++i) {
472 data = (data << 8) | *src++;
476 for (; cnt==0 && i<4; ++i, ++cp) {
477 data = (data << 8) | (*(uchar *)cp);
480 if ((rc = write_word(info, wp, data)) != 0) {
488 * handle word aligned part
492 for (i=0; i<4; ++i) {
493 data = (data << 8) | *src++;
495 if ((rc = write_word(info, wp, data)) != 0) {
509 * handle unaligned tail bytes
512 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
513 data = (data << 8) | *src++;
516 for (; i<4; ++i, ++cp) {
517 data = (data << 8) | (*(uchar *)cp);
522 return (write_word(info, wp, data));
526 * Write a word to Flash, returns:
529 * 2 - Flash not erased
531 static int write_word (flash_info_t *info, ulong dest, ulong data)
533 volatile ulong addr = info->start[0];
539 /* Check if Flash is (sufficiently) erased */
540 if ((in32(dest) & data) != data) {
545 /* write each byte out */
546 for (i = 0; i < 4; i++) {
547 char *data_ch = (char *)&data;
548 int flag = disable_interrupts();
550 out8(addr + 0x555, 0xAA);
552 out8(addr + 0x2AA, 0x55);
554 out8(addr + 0x555, 0xA0);
556 out8(dest+i, data_ch[i]);
559 /* re-enable interrupts if necessary */
563 /* data polling for D7 */
564 start = get_timer (0);
565 while ((in8(dest+i) & 0x80) != (data_ch[i] & 0x80)) {
566 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
581 * Reset bank to read mode
583 static void flash_reset (ulong addr)
586 out8(addr, 0xF0); /* reset bank */
591 void flash_print_info (flash_info_t *info)
595 if (info->flash_id == FLASH_UNKNOWN) {
596 printf ("missing or unknown FLASH type\n");
600 switch (info->flash_id & FLASH_VENDMASK) {
601 case FLASH_MAN_AMD: printf ("AMD "); break;
602 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
603 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
604 case FLASH_MAN_STM: printf ("SGS THOMSON "); break;
605 default: printf ("Unknown Vendor "); break;
608 switch (info->flash_id & FLASH_TYPEMASK) {
609 case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
611 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
613 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
615 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
617 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
619 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
621 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
623 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
625 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
627 default: printf ("Unknown Chip Type\n");
631 if (info->size % 0x100000 == 0) {
632 printf (" Size: %ld MB in %d Sectors\n",
633 info->size / 0x100000, info->sector_count);
634 } else if (info->size % 0x400 == 0) {
635 printf (" Size: %ld KB in %d Sectors\n",
636 info->size / 0x400, info->sector_count);
638 printf (" Size: %ld B in %d Sectors\n",
639 info->size, info->sector_count);
642 printf (" Sector Start Addresses:");
643 for (i=0; i<info->sector_count; ++i) {
648 info->protect[i] ? " (RO)" : " "