3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * 07-10-2002 Frank Gottschling: added 29F032 flash (ELPPC).
10 * fixed monitor protection part
12 * 09-18-2001 Andreas Heppel: Reduced the code in here to the usage
13 * of AMD's 29F040 and 29F016 flashes, since the BAB7xx does use
18 #include <asm/processor.h>
19 #include <asm/pci_io.h>
21 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
23 ulong flash_get_size (vu_long *addr, flash_info_t *info);
24 static int write_word (flash_info_t *info, ulong dest, ulong data);
26 /*flash command address offsets*/
32 #define FLASH_WORD_SIZE unsigned char
34 /*----------------------------------------------------------------------------*/
36 unsigned long flash_init (void)
38 unsigned long size1, size2;
41 /* Init: no FLASHes known */
42 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i)
44 flash_info[i].flash_id = FLASH_UNKNOWN;
47 /* initialise 1st flash */
48 size1 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
50 if (flash_info[0].flash_id == FLASH_UNKNOWN)
52 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
56 /* initialise 2nd flash */
57 size2 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
59 if (flash_info[1].flash_id == FLASH_UNKNOWN)
61 printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
65 /* monitor protection ON by default */
66 if (size1 == 512*1024)
68 (void)flash_protect(FLAG_PROTECT_SET,
70 FLASH_BASE0_PRELIM+monitor_flash_len-1,
73 if (size2 == 512*1024)
75 (void)flash_protect(FLAG_PROTECT_SET,
77 FLASH_BASE1_PRELIM+monitor_flash_len-1,
80 if (size2 == 4*1024*1024)
82 (void)flash_protect(FLAG_PROTECT_SET,
83 CONFIG_SYS_FLASH_BASE,
84 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
88 return (size1 + size2);
91 /*----------------------------------------------------------------------------*/
93 void flash_print_info (flash_info_t *info)
99 volatile unsigned long *flash;
101 if (info->flash_id == FLASH_UNKNOWN) {
102 printf ("missing or unknown FLASH type\n");
106 if (info->flash_id == FLASH_UNKNOWN) {
107 printf ("missing or unknown FLASH type\n");
111 switch (info->flash_id & FLASH_VENDMASK) {
116 printf ("Unknown Vendor ");
120 switch (info->flash_id & FLASH_TYPEMASK) {
122 printf ("AM29F040B (4 Mbit)\n");
125 printf ("AM29F016D (16 Mbit)\n");
128 printf ("AM29F032B (32 Mbit)\n");
131 printf ("Unknown Chip Type\n");
135 if (info->size >= (1 << 20)) {
136 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
138 printf (" Size: %ld kB in %d Sectors\n", info->size >> 10, info->sector_count);
141 printf (" Sector Start Addresses:");
142 for (i=0; i<info->sector_count; ++i) {
144 * Check if whole sector is erased
146 if (i != (info->sector_count-1))
147 size = info->start[i+1] - info->start[i];
149 size = info->start[0] + info->size - info->start[i];
152 flash = (volatile unsigned long *)info->start[i];
153 size = size >> 2; /* divide by 4 for longword access */
154 for (k=0; k<size; k++) {
155 if (*flash++ != 0xffffffff) {
164 printf (" %08lX%s%s",
167 info->protect[i] ? "RO " : " ");
172 /*----------------------------------------------------------------------------*/
174 * The following code cannot be run from FLASH!
176 ulong flash_get_size (vu_long *addr, flash_info_t *info)
180 ulong base = (ulong)addr;
181 volatile unsigned char *caddr = (unsigned char *)addr;
184 printf("flash_get_size for address 0x%lx: \n", (unsigned long)caddr);
187 /* Write auto select command: read Manufacturer ID */
188 caddr[0] = 0xF0; /* reset bank */
204 printf("Manufacturer: 0x%lx\n", vendor);
210 /* We accept only two AMD types */
212 case (FLASH_WORD_SIZE)AMD_MANUFACT:
213 info->flash_id = FLASH_MAN_AMD;
216 info->flash_id = FLASH_UNKNOWN;
217 info->sector_count = 0;
219 return (0); /* no or unknown flash */
223 case (FLASH_WORD_SIZE)AMD_ID_F040B:
224 info->flash_id |= AMD_ID_F040B;
225 info->sector_count = 8;
226 info->size = 0x00080000;
227 break; /* => 0.5 MB */
229 case (FLASH_WORD_SIZE)AMD_ID_F016D:
230 info->flash_id |= AMD_ID_F016D;
231 info->sector_count = 32;
232 info->size = 0x00200000;
235 case (FLASH_WORD_SIZE)AMD_ID_F032B:
236 info->flash_id |= AMD_ID_F032B;
237 info->sector_count = 64;
238 info->size = 0x00400000;
242 info->flash_id = FLASH_UNKNOWN;
243 return (0); /* => no or unknown flash */
248 printf("flash id 0x%lx; sector count 0x%x, size 0x%lx\n", info->flash_id, info->sector_count, info->size);
251 /* check for protected sectors */
252 for (i = 0; i < info->sector_count; i++) {
253 /* sector base address */
254 info->start[i] = base + i * (info->size / info->sector_count);
255 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
256 /* D0 = 1 if protected */
257 caddr = (volatile unsigned char *)(info->start[i]);
258 info->protect[i] = caddr[2] & 1;
262 * Prevent writes to uninitialized FLASH.
264 if (info->flash_id != FLASH_UNKNOWN) {
265 caddr = (volatile unsigned char *)info->start[0];
266 caddr[0] = 0xF0; /* reset bank */
272 /*----------------------------------------------------------------------------*/
274 int flash_erase (flash_info_t *info, int s_first, int s_last)
276 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
277 int flag, prot, sect, l_sect;
278 ulong start, now, last;
281 if ((s_first < 0) || (s_first > s_last)) {
282 if (info->flash_id == FLASH_UNKNOWN) {
283 printf ("- missing\n");
285 printf ("- no sectors to erase\n");
290 if ((info->flash_id == FLASH_UNKNOWN) ||
291 (info->flash_id > FLASH_AMD_COMP)) {
292 printf ("Can't erase unknown flash type - aborted\n");
297 for (sect=s_first; sect<=s_last; ++sect) {
298 if (info->protect[sect]) {
304 printf ("- Warning: %d protected sectors will not be erased!\n",
312 /* Disable interrupts which might cause a timeout here */
313 flag = disable_interrupts();
315 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
316 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
317 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
318 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
319 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
321 /* Start erase on unprotected sectors */
322 for (sect = s_first; sect<=s_last; sect++) {
323 if (info->protect[sect] == 0) { /* not protected */
324 addr = (FLASH_WORD_SIZE *)(info->start[sect]);
325 if (info->flash_id & FLASH_MAN_SST) {
326 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
327 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
328 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
329 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
330 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
331 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
332 udelay(30000); /* wait 30 ms */
335 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
340 /* re-enable interrupts if necessary */
344 /* wait at least 80us - let's wait 1 ms */
348 * We wait for the last triggered sector
353 start = get_timer (0);
355 addr = (FLASH_WORD_SIZE *)(info->start[l_sect]);
356 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
357 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
358 printf ("Timeout\n");
361 /* show that we're waiting */
362 if ((now - last) > 1000) { /* every second */
369 /* reset to read mode */
370 addr = (FLASH_WORD_SIZE *)info->start[0];
371 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
377 /*----------------------------------------------------------------------------*/
379 * Copy memory to flash, returns:
382 * 2 - Flash not erased
384 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
389 wp = (addr & ~3); /* get lower word aligned address */
392 * handle unaligned start bytes
394 if ((l = addr - wp) != 0) {
396 for (i=0, cp=wp; i<l; ++i, ++cp) {
397 data = (data << 8) | (*(uchar *)cp);
399 for (; i<4 && cnt>0; ++i) {
400 data = (data << 8) | *src++;
404 for (; cnt==0 && i<4; ++i, ++cp) {
405 data = (data << 8) | (*(uchar *)cp);
408 if ((rc = write_word(info, wp, data)) != 0) {
415 * handle word aligned part
419 for (i=0; i<4; ++i) {
420 data = (data << 8) | *src++;
422 if ((rc = write_word(info, wp, data)) != 0) {
434 * handle unaligned tail bytes
437 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
438 data = (data << 8) | *src++;
441 for (; i<4; ++i, ++cp) {
442 data = (data << 8) | (*(uchar *)cp);
445 return (write_word(info, wp, data));
448 /*----------------------------------------------------------------------------*/
449 /* Write a word to Flash, returns:
452 * 2 - Flash not erased
454 static int write_word (flash_info_t *info, ulong dest, ulong data)
456 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
457 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
458 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
463 /* Check if Flash is (sufficiently) erased */
464 if ((*((volatile FLASH_WORD_SIZE *)dest) &
465 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
468 /* Disable interrupts which might cause a timeout here */
469 flag = disable_interrupts();
471 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
473 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
474 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
475 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
479 /* re-enable interrupts if necessary */
483 /* data polling for D7 */
484 start = get_timer (0);
485 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
486 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
487 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
496 /*----------------------------------------------------------------------------*/