3 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
16 /*---------------------------------------------------------------------*/
20 #define DEBUGF(fmt,args...) printf(fmt ,##args)
22 #define DEBUGF(fmt,args...)
24 /*---------------------------------------------------------------------*/
26 flash_info_t flash_info[];
28 static ulong flash_get_size (ulong addr, flash_info_t *info);
29 static int flash_get_offsets (ulong base, flash_info_t *info);
30 static int write_word (flash_info_t *info, ulong dest, ulong data);
31 static void flash_reset (ulong addr);
35 static void flash_to_xd(void)
41 if (flash_xd_nest == 1)
43 DEBUGF("Flash on XD\n");
44 x = pci_read_cfg_byte(0, 0, 0x74);
45 pci_write_cfg_byte(0, 0, 0x74, x|1);
49 static void flash_to_mem(void)
55 if (flash_xd_nest == 0)
57 DEBUGF("Flash on memory bus\n");
58 x = pci_read_cfg_byte(0, 0, 0x74);
59 pci_write_cfg_byte(0, 0, 0x74, x&0xFE);
63 unsigned long flash_init_old(void)
67 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++)
69 flash_info[i].flash_id = FLASH_UNKNOWN;
70 flash_info[i].sector_count = 0;
71 flash_info[i].size = 0;
78 unsigned long flash_init (void)
81 unsigned long flash_size = 0;
87 /* Init: no FLASHes known */
88 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
89 flash_info[i].flash_id = FLASH_UNKNOWN;
90 flash_info[i].sector_count = 0;
91 flash_info[i].size = 0;
94 DEBUGF("\n## Get flash size @ 0x%08x\n", CONFIG_SYS_FLASH_BASE);
96 flash_size = flash_get_size (CONFIG_SYS_FLASH_BASE, flash_info);
98 DEBUGF("## Flash bank size: %08lx\n", flash_size);
101 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE && \
102 CONFIG_SYS_MONITOR_BASE < CONFIG_SYS_FLASH_BASE + CONFIG_SYS_FLASH_MAX_SIZE
103 /* monitor protection ON by default */
104 flash_protect(FLAG_PROTECT_SET,
105 CONFIG_SYS_MONITOR_BASE,
106 CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
110 #ifdef CONFIG_ENV_IS_IN_FLASH
111 /* ENV protection ON by default */
112 flash_protect(FLAG_PROTECT_SET,
114 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
119 printf ("Warning: the BOOT Flash is not initialised !");
128 * The following code cannot be run from FLASH!
130 static ulong flash_get_size (ulong addr, flash_info_t *info)
134 uchar *x = (uchar *)addr;
138 /* Write auto select command: read Manufacturer ID */
140 __asm__ volatile ("sync\n eieio");
142 __asm__ volatile ("sync\n eieio");
144 __asm__ volatile ("sync\n eieio");
147 __asm__ volatile ("sync\n eieio");
149 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, value);
151 switch (value | (value << 16)) {
153 info->flash_id = FLASH_MAN_AMD;
157 info->flash_id = FLASH_MAN_FUJ;
161 info->flash_id = FLASH_MAN_STM;
165 info->flash_id = FLASH_UNKNOWN;
166 info->sector_count = 0;
173 __asm__ volatile ("sync\n eieio");
175 DEBUGF("Device ID @ 0x%08lx: 0x%08x\n", addr+1, value);
179 DEBUGF("Am29F040B\n");
180 info->flash_id += FLASH_AM040;
181 info->sector_count = 8;
182 info->size = 0x00080000;
183 break; /* => 512 kB */
186 DEBUGF("Am29LV040B\n");
187 info->flash_id += FLASH_AM040;
188 info->sector_count = 8;
189 info->size = 0x00080000;
190 break; /* => 512 kB */
193 DEBUGF("Am29LV400T\n");
194 info->flash_id += FLASH_AM400T;
195 info->sector_count = 11;
196 info->size = 0x00100000;
200 DEBUGF("Am29LV400B\n");
201 info->flash_id += FLASH_AM400B;
202 info->sector_count = 11;
203 info->size = 0x00100000;
207 DEBUGF("Am29LV800T\n");
208 info->flash_id += FLASH_AM800T;
209 info->sector_count = 19;
210 info->size = 0x00200000;
214 DEBUGF("Am29LV400B\n");
215 info->flash_id += FLASH_AM800B;
216 info->sector_count = 19;
217 info->size = 0x00200000;
221 DEBUGF("Am29LV160T\n");
222 info->flash_id += FLASH_AM160T;
223 info->sector_count = 35;
224 info->size = 0x00400000;
228 DEBUGF("Am29LV160B\n");
229 info->flash_id += FLASH_AM160B;
230 info->sector_count = 35;
231 info->size = 0x00400000;
235 DEBUGF("Am29LV320T\n");
236 info->flash_id += FLASH_AM320T;
237 info->sector_count = 67;
238 info->size = 0x00800000;
242 /* Has the same ID as AMD_ID_LV320T, to be fixed */
244 DEBUGF("Am29LV320B\n");
245 info->flash_id += FLASH_AM320B;
246 info->sector_count = 67;
247 info->size = 0x00800000;
252 DEBUGF("Am29LV033C\n");
253 info->flash_id += FLASH_AM033C;
254 info->sector_count = 64;
255 info->size = 0x01000000;
259 DEBUGF("M29F040B\n");
260 info->flash_id += FLASH_AM040;
261 info->sector_count = 8;
262 info->size = 0x00080000;
263 break; /* => 512 kB */
266 info->flash_id = FLASH_UNKNOWN;
269 return (0); /* => no or unknown flash */
273 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
274 printf ("** ERROR: sector count %d > max (%d) **\n",
275 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
276 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
279 if (! flash_get_offsets (addr, info)) {
285 /* check for protected sectors */
286 for (i = 0; i < info->sector_count; i++) {
287 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
288 /* D0 = 1 if protected */
289 value = in8(info->start[i] + 2);
291 info->protect[i] = (value & 1) != 0;
295 * Reset bank to read mode
304 static int flash_get_offsets (ulong base, flash_info_t *info)
308 switch (info->flash_id & FLASH_TYPEMASK) {
310 /* set sector offsets for uniform sector type */
311 for (i = 0; i < info->sector_count; i++) {
312 info->start[i] = base + i * info->size /
323 int flash_erase (flash_info_t *info, int s_first, int s_last)
325 volatile ulong addr = info->start[0];
326 int flag, prot, sect, l_sect;
327 ulong start, now, last;
331 if (s_first < 0 || s_first > s_last) {
332 if (info->flash_id == FLASH_UNKNOWN) {
333 printf ("- missing\n");
335 printf ("- no sectors to erase\n");
341 if (info->flash_id == FLASH_UNKNOWN) {
342 printf ("Can't erase unknown flash type %08lx - aborted\n",
349 for (sect=s_first; sect<=s_last; ++sect) {
350 if (info->protect[sect]) {
356 printf ("- Warning: %d protected sectors will not be erased!\n",
364 /* Disable interrupts which might cause a timeout here */
365 flag = disable_interrupts();
367 out8(addr + 0x555, 0xAA);
369 out8(addr + 0x2AA, 0x55);
371 out8(addr + 0x555, 0x80);
373 out8(addr + 0x555, 0xAA);
375 out8(addr + 0x2AA, 0x55);
378 /* Start erase on unprotected sectors */
379 for (sect = s_first; sect<=s_last; sect++) {
380 if (info->protect[sect] == 0) { /* not protected */
381 addr = info->start[sect];
388 /* re-enable interrupts if necessary */
392 /* wait at least 80us - let's wait 1 ms */
396 * We wait for the last triggered sector
401 start = get_timer (0);
403 addr = info->start[l_sect];
405 DEBUGF ("Start erase timeout: %d\n", CONFIG_SYS_FLASH_ERASE_TOUT);
407 while ((in8(addr) & 0x80) != 0x80) {
408 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
409 printf ("Timeout\n");
410 flash_reset (info->start[0]);
414 /* show that we're waiting */
415 if ((now - last) > 1000) { /* every second */
423 /* reset to read mode */
424 flash_reset (info->start[0]);
432 * Copy memory to flash, returns:
435 * 2 - Flash not erased
437 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
445 wp = (addr & ~3); /* get lower word aligned address */
448 * handle unaligned start bytes
450 if ((l = addr - wp) != 0) {
452 for (i=0, cp=wp; i<l; ++i, ++cp) {
453 data = (data << 8) | (*(uchar *)cp);
455 for (; i<4 && cnt>0; ++i) {
456 data = (data << 8) | *src++;
460 for (; cnt==0 && i<4; ++i, ++cp) {
461 data = (data << 8) | (*(uchar *)cp);
464 if ((rc = write_word(info, wp, data)) != 0) {
474 * handle word aligned part
483 for (i=0; i<4; ++i) {
484 data = (data << 8) | *src++;
486 if ((rc = write_word(info, wp, data)) != 0) {
501 * handle unaligned tail bytes
504 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
505 data = (data << 8) | *src++;
508 for (; i<4; ++i, ++cp) {
509 data = (data << 8) | (*(uchar *)cp);
513 return (write_word(info, wp, data));
517 * Write a word to Flash, returns:
520 * 2 - Flash not erased
522 static int write_word (flash_info_t *info, ulong dest, ulong data)
524 volatile ulong addr = info->start[0];
530 /* Check if Flash is (sufficiently) erased */
531 if ((in32(dest) & data) != data) {
536 /* write each byte out */
537 for (i = 0; i < 4; i++) {
538 char *data_ch = (char *)&data;
539 int flag = disable_interrupts();
541 out8(addr + 0x555, 0xAA);
543 out8(addr + 0x2AA, 0x55);
545 out8(addr + 0x555, 0xA0);
547 out8(dest+i, data_ch[i]);
550 /* re-enable interrupts if necessary */
554 /* data polling for D7 */
555 start = get_timer (0);
556 while ((in8(dest+i) & 0x80) != (data_ch[i] & 0x80)) {
557 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
572 * Reset bank to read mode
574 static void flash_reset (ulong addr)
577 out8(addr, 0xF0); /* reset bank */
582 void flash_print_info (flash_info_t *info)
586 if (info->flash_id == FLASH_UNKNOWN) {
587 printf ("missing or unknown FLASH type\n");
591 switch (info->flash_id & FLASH_VENDMASK) {
592 case FLASH_MAN_AMD: printf ("AMD "); break;
593 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
594 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
595 case FLASH_MAN_STM: printf ("SGS THOMSON "); break;
596 default: printf ("Unknown Vendor "); break;
599 switch (info->flash_id & FLASH_TYPEMASK) {
600 case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
602 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
604 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
606 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
608 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
610 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
612 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
614 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
616 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
618 default: printf ("Unknown Chip Type\n");
622 if (info->size % 0x100000 == 0) {
623 printf (" Size: %ld MB in %d Sectors\n",
624 info->size / 0x100000, info->sector_count);
625 } else if (info->size % 0x400 == 0) {
626 printf (" Size: %ld KB in %d Sectors\n",
627 info->size / 0x400, info->sector_count);
629 printf (" Size: %ld B in %d Sectors\n",
630 info->size, info->sector_count);
633 printf (" Sector Start Addresses:");
634 for (i=0; i<info->sector_count; ++i) {
639 info->protect[i] ? " (RO)" : " "