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 /*---------------------------------------------------------------------*/
36 #define DEBUGF(fmt,args...) printf(fmt ,##args)
38 #define DEBUGF(fmt,args...)
40 /*---------------------------------------------------------------------*/
42 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
44 static ulong flash_get_size (ulong addr, flash_info_t *info);
45 static int flash_get_offsets (ulong base, flash_info_t *info);
46 static int write_word (flash_info_t *info, ulong dest, ulong data);
47 static void flash_reset (ulong addr);
49 unsigned long flash_init (void)
52 unsigned long flash_size = 0;
54 /* Init: no FLASHes known */
55 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
56 flash_info[i].flash_id = FLASH_UNKNOWN;
57 flash_info[i].sector_count = 0;
58 flash_info[i].size = 0;
61 DEBUGF("\n## Get flash size @ 0x%08x\n", CONFIG_SYS_FLASH_BASE);
63 flash_size = flash_get_size (CONFIG_SYS_FLASH_BASE, flash_info);
65 DEBUGF("## Flash bank size: %08lx\n", flash_size);
68 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE && \
69 CONFIG_SYS_MONITOR_BASE < CONFIG_SYS_FLASH_BASE + CONFIG_SYS_FLASH_MAX_SIZE
70 /* monitor protection ON by default */
71 flash_protect(FLAG_PROTECT_SET,
72 CONFIG_SYS_MONITOR_BASE,
73 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
77 #ifdef CONFIG_ENV_IS_IN_FLASH
78 /* ENV protection ON by default */
79 flash_protect(FLAG_PROTECT_SET,
81 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
86 puts ("Warning: the BOOT Flash is not initialised !");
93 * The following code cannot be run from FLASH!
95 static ulong flash_get_size (ulong addr, flash_info_t *info)
100 /* Write auto select command: read Manufacturer ID */
101 out8(addr + 0x0555, 0xAA);
103 out8(addr + 0x02AA, 0x55);
105 out8(addr + 0x0555, 0x90);
111 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, value);
113 switch (value | (value << 16)) {
115 info->flash_id = FLASH_MAN_AMD;
119 info->flash_id = FLASH_MAN_FUJ;
123 info->flash_id = FLASH_MAN_STM;
127 info->flash_id = FLASH_UNKNOWN;
128 info->sector_count = 0;
134 value = in8(addr + 1); /* device ID */
137 DEBUGF("Device ID @ 0x%08lx: 0x%08x\n", addr+1, value);
139 switch ((ulong)value) {
141 DEBUGF("Am29F040B\n");
142 info->flash_id += FLASH_AM040;
143 info->sector_count = 8;
144 info->size = 0x00080000;
145 break; /* => 512 kB */
148 DEBUGF("Am29LV040B\n");
149 info->flash_id += FLASH_AM040;
150 info->sector_count = 8;
151 info->size = 0x00080000;
152 break; /* => 512 kB */
155 DEBUGF("Am29LV400T\n");
156 info->flash_id += FLASH_AM400T;
157 info->sector_count = 11;
158 info->size = 0x00100000;
162 DEBUGF("Am29LV400B\n");
163 info->flash_id += FLASH_AM400B;
164 info->sector_count = 11;
165 info->size = 0x00100000;
169 DEBUGF("Am29LV800T\n");
170 info->flash_id += FLASH_AM800T;
171 info->sector_count = 19;
172 info->size = 0x00200000;
176 DEBUGF("Am29LV400B\n");
177 info->flash_id += FLASH_AM800B;
178 info->sector_count = 19;
179 info->size = 0x00200000;
183 DEBUGF("Am29LV160T\n");
184 info->flash_id += FLASH_AM160T;
185 info->sector_count = 35;
186 info->size = 0x00400000;
190 DEBUGF("Am29LV160B\n");
191 info->flash_id += FLASH_AM160B;
192 info->sector_count = 35;
193 info->size = 0x00400000;
197 DEBUGF("Am29LV320T\n");
198 info->flash_id += FLASH_AM320T;
199 info->sector_count = 67;
200 info->size = 0x00800000;
204 /* Has the same ID as AMD_ID_LV320T, to be fixed */
206 DEBUGF("Am29LV320B\n");
207 info->flash_id += FLASH_AM320B;
208 info->sector_count = 67;
209 info->size = 0x00800000;
214 DEBUGF("Am29LV033C\n");
215 info->flash_id += FLASH_AM033C;
216 info->sector_count = 64;
217 info->size = 0x01000000;
221 DEBUGF("M29F040B\n");
222 info->flash_id += FLASH_AM040;
223 info->sector_count = 8;
224 info->size = 0x00080000;
225 break; /* => 512 kB */
228 info->flash_id = FLASH_UNKNOWN;
230 return (0); /* => no or unknown flash */
234 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
235 printf ("** ERROR: sector count %d > max (%d) **\n",
236 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
237 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
240 if (! flash_get_offsets (addr, info)) {
245 /* check for protected sectors */
246 for (i = 0; i < info->sector_count; i++) {
247 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
248 /* D0 = 1 if protected */
249 value = in8(info->start[i] + 2);
251 info->protect[i] = (value & 1) != 0;
255 * Reset bank to read mode
262 static int flash_get_offsets (ulong base, flash_info_t *info)
266 switch (info->flash_id & FLASH_TYPEMASK) {
268 /* set sector offsets for uniform sector type */
269 for (i = 0; i < info->sector_count; i++) {
270 info->start[i] = base + i * info->size /
281 int flash_erase (flash_info_t *info, int s_first, int s_last)
283 volatile ulong addr = info->start[0];
284 int flag, prot, sect, l_sect;
285 ulong start, now, last;
287 if (s_first < 0 || s_first > s_last) {
288 if (info->flash_id == FLASH_UNKNOWN) {
289 printf ("- missing\n");
291 printf ("- no sectors to erase\n");
296 if (info->flash_id == FLASH_UNKNOWN) {
297 printf ("Can't erase unknown flash type %08lx - aborted\n",
303 for (sect=s_first; sect<=s_last; ++sect) {
304 if (info->protect[sect]) {
310 printf ("- Warning: %d protected sectors will not be erased!\n",
318 /* Disable interrupts which might cause a timeout here */
319 flag = disable_interrupts();
321 out8(addr + 0x555, 0xAA);
323 out8(addr + 0x2AA, 0x55);
325 out8(addr + 0x555, 0x80);
327 out8(addr + 0x555, 0xAA);
329 out8(addr + 0x2AA, 0x55);
332 /* Start erase on unprotected sectors */
333 for (sect = s_first; sect<=s_last; sect++) {
334 if (info->protect[sect] == 0) { /* not protected */
335 addr = info->start[sect];
342 /* re-enable interrupts if necessary */
346 /* wait at least 80us - let's wait 1 ms */
350 * We wait for the last triggered sector
355 start = get_timer (0);
357 addr = info->start[l_sect];
359 DEBUGF ("Start erase timeout: %d\n", CONFIG_SYS_FLASH_ERASE_TOUT);
361 while ((in8(addr) & 0x80) != 0x80) {
362 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
363 printf ("Timeout\n");
364 flash_reset (info->start[0]);
367 /* show that we're waiting */
368 if ((now - last) > 1000) { /* every second */
376 /* reset to read mode */
377 flash_reset (info->start[0]);
384 * Copy memory to flash, returns:
387 * 2 - Flash not erased
389 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
394 wp = (addr & ~3); /* get lower word aligned address */
397 * handle unaligned start bytes
399 if ((l = addr - wp) != 0) {
401 for (i=0, cp=wp; i<l; ++i, ++cp) {
402 data = (data << 8) | (*(uchar *)cp);
404 for (; i<4 && cnt>0; ++i) {
405 data = (data << 8) | *src++;
409 for (; cnt==0 && i<4; ++i, ++cp) {
410 data = (data << 8) | (*(uchar *)cp);
413 if ((rc = write_word(info, wp, data)) != 0) {
420 * handle word aligned part
424 for (i=0; i<4; ++i) {
425 data = (data << 8) | *src++;
427 if ((rc = write_word(info, wp, data)) != 0) {
439 * handle unaligned tail bytes
442 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
443 data = (data << 8) | *src++;
446 for (; i<4; ++i, ++cp) {
447 data = (data << 8) | (*(uchar *)cp);
450 return (write_word(info, wp, data));
454 * Write a word to Flash, returns:
457 * 2 - Flash not erased
459 static int write_word (flash_info_t *info, ulong dest, ulong data)
461 volatile ulong addr = info->start[0];
465 /* Check if Flash is (sufficiently) erased */
466 if ((in32(dest) & data) != data) {
470 /* write each byte out */
471 for (i = 0; i < 4; i++) {
472 char *data_ch = (char *)&data;
473 int flag = disable_interrupts();
475 out8(addr + 0x555, 0xAA);
477 out8(addr + 0x2AA, 0x55);
479 out8(addr + 0x555, 0xA0);
481 out8(dest+i, data_ch[i]);
484 /* re-enable interrupts if necessary */
488 /* data polling for D7 */
489 start = get_timer (0);
490 while ((in8(dest+i) & 0x80) != (data_ch[i] & 0x80)) {
491 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
504 * Reset bank to read mode
506 static void flash_reset (ulong addr)
508 out8(addr, 0xF0); /* reset bank */
512 void flash_print_info (flash_info_t *info)
516 if (info->flash_id == FLASH_UNKNOWN) {
517 printf ("missing or unknown FLASH type\n");
521 switch (info->flash_id & FLASH_VENDMASK) {
522 case FLASH_MAN_AMD: printf ("AMD "); break;
523 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
524 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
525 case FLASH_MAN_STM: printf ("SGS THOMSON "); break;
526 default: printf ("Unknown Vendor "); break;
529 switch (info->flash_id & FLASH_TYPEMASK) {
530 case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
532 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
534 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
536 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
538 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
540 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
542 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
544 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
546 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
548 default: printf ("Unknown Chip Type\n");
552 if (info->size % 0x100000 == 0) {
553 printf (" Size: %ld MB in %d Sectors\n",
554 info->size / 0x100000, info->sector_count);
555 } else if (info->size % 0x400 == 0) {
556 printf (" Size: %ld KB in %d Sectors\n",
557 info->size / 0x400, info->sector_count);
559 printf (" Size: %ld B in %d Sectors\n",
560 info->size, info->sector_count);
563 printf (" Sector Start Addresses:");
564 for (i=0; i<info->sector_count; ++i) {
569 info->protect[i] ? " (RO)" : " "