3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/processor.h>
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 /*-----------------------------------------------------------------------
16 static int write_word (flash_info_t *info, ulong dest, ulong data);
18 /*-----------------------------------------------------------------------
20 static void flash_get_offsets (ulong base, flash_info_t *info)
25 /* set up sector start address table */
26 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
27 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
28 for (i = 0; i < info->sector_count; i++)
29 info->start[i] = base + (i * 0x00010000);
30 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
31 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
32 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
33 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
34 /* set sector offsets for bottom boot block type */
35 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
36 info->start[i] = base;
39 while (i < info->sector_count) { /* 64k regular sectors */
40 info->start[i] = base;
44 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
45 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
46 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
47 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
48 /* set sector offsets for top boot block type */
50 i = info->sector_count;
51 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
54 info->start[i] = base;
56 while (i > 0) { /* 64k regular sectors */
59 info->start[i] = base;
62 if (info->flash_id & FLASH_BTYPE) {
63 /* set sector offsets for bottom boot block type */
64 info->start[0] = base + 0x00000000;
65 info->start[1] = base + 0x00004000;
66 info->start[2] = base + 0x00006000;
67 info->start[3] = base + 0x00008000;
68 for (i = 4; i < info->sector_count; i++) {
69 info->start[i] = base + (i * 0x00010000) - 0x00030000;
72 /* set sector offsets for top boot block type */
73 i = info->sector_count - 1;
74 info->start[i--] = base + info->size - 0x00004000;
75 info->start[i--] = base + info->size - 0x00006000;
76 info->start[i--] = base + info->size - 0x00008000;
78 info->start[i] = base + i * 0x00010000;
84 /*-----------------------------------------------------------------------
86 void flash_print_info (flash_info_t *info)
92 volatile unsigned long *flash;
94 if (info->flash_id == FLASH_UNKNOWN) {
95 printf ("missing or unknown FLASH type\n");
99 switch (info->flash_id & FLASH_VENDMASK) {
100 case FLASH_MAN_AMD: printf ("AMD "); break;
101 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
102 case FLASH_MAN_SST: printf ("SST "); break;
103 case FLASH_MAN_STM: printf ("ST "); break;
104 default: printf ("Unknown Vendor "); break;
107 switch (info->flash_id & FLASH_TYPEMASK) {
108 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
110 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
112 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
114 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
116 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
118 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
120 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
122 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
124 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
126 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
128 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
130 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
132 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
134 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
136 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
138 case FLASH_STMW320DT: printf ("M29W320DT (32 M, top sector)\n");
140 default: printf ("Unknown Chip Type\n");
144 printf (" Size: %ld MB in %d Sectors\n",
145 info->size >> 20, info->sector_count);
147 printf (" Sector Start Addresses:");
148 for (i=0; i<info->sector_count; ++i) {
149 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
151 * Check if whole sector is erased
153 if (i != (info->sector_count-1))
154 size = info->start[i+1] - info->start[i];
156 size = info->start[0] + info->size - info->start[i];
158 flash = (volatile unsigned long *)info->start[i];
159 size = size >> 2; /* divide by 4 for longword access */
160 for (k=0; k<size; k++)
162 if (*flash++ != 0xffffffff)
171 /* print empty and read-only info */
172 printf (" %08lX%s%s",
175 info->protect[i] ? "RO " : " ");
181 info->protect[i] ? " (RO)" : " ");
189 /*-----------------------------------------------------------------------
193 /*-----------------------------------------------------------------------
197 * The following code cannot be run from FLASH!
199 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
203 CONFIG_SYS_FLASH_WORD_SIZE value;
204 ulong base = (ulong)addr;
205 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
207 debug("[%s, %d] Entering ...\n", __FUNCTION__, __LINE__);
209 /* Write auto select command: read Manufacturer ID */
210 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
211 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
212 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
214 value = addr2[CONFIG_SYS_FLASH_READ0];
217 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
218 info->flash_id = FLASH_MAN_AMD;
220 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
221 info->flash_id = FLASH_MAN_FUJ;
223 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
224 info->flash_id = FLASH_MAN_SST;
226 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_MANUFACT:
227 info->flash_id = FLASH_MAN_STM;
230 info->flash_id = FLASH_UNKNOWN;
231 info->sector_count = 0;
233 return (0); /* no or unknown flash */
236 value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
239 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
240 info->flash_id += FLASH_AM400T;
241 info->sector_count = 11;
242 info->size = 0x00080000;
243 break; /* => 0.5 MB */
245 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
246 info->flash_id += FLASH_AM400B;
247 info->sector_count = 11;
248 info->size = 0x00080000;
249 break; /* => 0.5 MB */
251 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
252 info->flash_id += FLASH_AM800T;
253 info->sector_count = 19;
254 info->size = 0x00100000;
257 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
258 info->flash_id += FLASH_AM800B;
259 info->sector_count = 19;
260 info->size = 0x00100000;
263 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
264 info->flash_id += FLASH_AM160T;
265 info->sector_count = 35;
266 info->size = 0x00200000;
269 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
270 info->flash_id += FLASH_AM160B;
271 info->sector_count = 35;
272 info->size = 0x00200000;
275 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
276 info->flash_id += FLASH_STMW320DT;
277 info->sector_count = 67;
278 info->size = 0x00400000; break; /* => 4 MB */
280 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
281 info->flash_id += FLASH_AM320T;
282 info->sector_count = 71;
283 info->size = 0x00400000; break; /* => 4 MB */
285 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
286 info->flash_id += FLASH_AM320B;
287 info->sector_count = 71;
288 info->size = 0x00400000; break; /* => 4 MB */
290 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
291 info->flash_id += FLASH_AMDL322T;
292 info->sector_count = 71;
293 info->size = 0x00400000; break; /* => 4 MB */
295 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
296 info->flash_id += FLASH_AMDL322B;
297 info->sector_count = 71;
298 info->size = 0x00400000; break; /* => 4 MB */
300 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
301 info->flash_id += FLASH_AMDL323T;
302 info->sector_count = 71;
303 info->size = 0x00400000; break; /* => 4 MB */
305 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
306 info->flash_id += FLASH_AMDL323B;
307 info->sector_count = 71;
308 info->size = 0x00400000; break; /* => 4 MB */
310 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
311 info->flash_id += FLASH_AM640U;
312 info->sector_count = 128;
313 info->size = 0x00800000; break; /* => 8 MB */
315 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
316 info->flash_id += FLASH_SST800A;
317 info->sector_count = 16;
318 info->size = 0x00100000;
321 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
322 info->flash_id += FLASH_SST160A;
323 info->sector_count = 32;
324 info->size = 0x00200000;
328 info->flash_id = FLASH_UNKNOWN;
329 return (0); /* => no or unknown flash */
333 /* set up sector start address table */
334 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
335 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
336 for (i = 0; i < info->sector_count; i++)
337 info->start[i] = base + (i * 0x00010000);
338 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
339 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
340 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
341 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
342 /* set sector offsets for bottom boot block type */
343 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
344 info->start[i] = base;
347 while (i < info->sector_count) { /* 64k regular sectors */
348 info->start[i] = base;
352 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
353 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
354 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
355 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
356 /* set sector offsets for top boot block type */
358 i = info->sector_count;
359 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
362 info->start[i] = base;
364 while (i > 0) { /* 64k regular sectors */
367 info->start[i] = base;
369 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
370 /* set sector offsets for top boot block type */
372 i = info->sector_count;
373 /* 1 x 16k boot sector */
376 info->start[i] = base;
377 /* 2 x 8k boot sectors */
378 for (n=0; n<2; ++n) {
381 info->start[i] = base;
383 /* 1 x 32k boot sector */
386 info->start[i] = base;
388 while (i > 0) { /* 64k regular sectors */
391 info->start[i] = base;
394 if (info->flash_id & FLASH_BTYPE) {
395 /* set sector offsets for bottom boot block type */
396 info->start[0] = base + 0x00000000;
397 info->start[1] = base + 0x00004000;
398 info->start[2] = base + 0x00006000;
399 info->start[3] = base + 0x00008000;
400 for (i = 4; i < info->sector_count; i++) {
401 info->start[i] = base + (i * 0x00010000) - 0x00030000;
404 /* set sector offsets for top boot block type */
405 i = info->sector_count - 1;
406 info->start[i--] = base + info->size - 0x00004000;
407 info->start[i--] = base + info->size - 0x00006000;
408 info->start[i--] = base + info->size - 0x00008000;
409 for (; i >= 0; i--) {
410 info->start[i] = base + i * 0x00010000;
415 /* check for protected sectors */
416 for (i = 0; i < info->sector_count; i++) {
417 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
418 /* D0 = 1 if protected */
419 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
420 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
421 info->protect[i] = 0;
423 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
427 * Prevent writes to uninitialized FLASH.
429 if (info->flash_id != FLASH_UNKNOWN) {
430 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
431 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
438 /*-----------------------------------------------------------------------
441 int flash_erase (flash_info_t *info, int s_first, int s_last)
443 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
444 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
445 int flag, prot, sect, l_sect;
446 ulong start, now, last;
449 if ((s_first < 0) || (s_first > s_last)) {
450 if (info->flash_id == FLASH_UNKNOWN) {
451 printf ("- missing\n");
453 printf ("- no sectors to erase\n");
458 if (info->flash_id == FLASH_UNKNOWN) {
459 printf ("Can't erase unknown flash type - aborted\n");
464 for (sect=s_first; sect<=s_last; ++sect) {
465 if (info->protect[sect]) {
471 printf ("- Warning: %d protected sectors will not be erased!\n",
479 /* Disable interrupts which might cause a timeout here */
480 flag = disable_interrupts();
482 /* Start erase on unprotected sectors */
483 for (sect = s_first; sect<=s_last; sect++) {
484 if (info->protect[sect] == 0) { /* not protected */
485 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
486 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
487 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
488 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
489 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
490 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
491 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
492 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
494 udelay(1000); /* wait 1 ms */
496 if (sect == s_first) {
497 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
498 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
499 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
500 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
501 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
503 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
509 /* re-enable interrupts if necessary */
513 /* wait at least 80us - let's wait 1 ms */
517 * We wait for the last triggered sector
522 start = get_timer (0);
524 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
525 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
526 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
527 printf ("Timeout\n");
530 /* show that we're waiting */
531 if ((now - last) > 1000) { /* every second */
538 /* reset to read mode */
539 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
540 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
546 /*-----------------------------------------------------------------------
547 * Copy memory to flash, returns:
550 * 2 - Flash not erased
553 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
558 wp = (addr & ~3); /* get lower word aligned address */
561 * handle unaligned start bytes
563 if ((l = addr - wp) != 0) {
565 for (i=0, cp=wp; i<l; ++i, ++cp) {
567 data = data | ((*(uchar *)cp)<<(8*i));
569 data = (data << 8) | (*(uchar *)cp);
572 for (; i<4 && cnt>0; ++i) {
574 data = data | ((*src++)<<(8*i));
576 data = (data << 8) | *src++;
581 for (; cnt==0 && i<4; ++i, ++cp) {
583 data = data | ((*(uchar *)cp)<<(8*i));
585 data = (data << 8) | (*(uchar *)cp);
589 if ((rc = write_word(info, wp, data)) != 0) {
596 * handle word aligned part
601 data = (*(ulong*)src);
604 for (i=0; i<4; ++i) {
605 data = (data << 8) | *src++;
608 if ((rc = write_word(info, wp, data)) != 0) {
620 * handle unaligned tail bytes
623 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
625 data = data | ((*src++)<<(8*i));
627 data = (data << 8) | *src++;
631 for (; i<4; ++i, ++cp) {
633 data = data | ((*(uchar *)cp)<<(8*i));
635 data = (data << 8) | (*(uchar *)cp);
639 return (write_word(info, wp, data));
642 /*-----------------------------------------------------------------------
643 * Write a word to Flash, returns:
646 * 2 - Flash not erased
648 static int write_word (flash_info_t *info, ulong dest, ulong data)
650 ulong *data_ptr = &data;
651 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
652 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
653 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
658 /* Check if Flash is (sufficiently) erased */
659 if ((*((volatile ulong *)dest) & data) != data) {
662 /* Disable interrupts which might cause a timeout here */
663 flag = disable_interrupts();
665 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
667 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
668 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
669 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
673 /* re-enable interrupts if necessary */
677 /* data polling for D7 */
678 start = get_timer (0);
679 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
680 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
681 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
690 /*-----------------------------------------------------------------------