Merge branch 'next'
[u-boot/qq2440-u-boot.git] / board / LEOX / elpt860 / flash.c
blob0377c8970c5e6db8e1531d422f36988f8d5164d2
1 /*
2 **=====================================================================
3 **
4 ** Copyright (C) 2000, 2001, 2002, 2003
5 ** The LEOX team <team@leox.org>, http://www.leox.org
6 **
7 ** LEOX.org is about the development of free hardware and software resources
8 ** for system on chip.
9 **
10 ** Description: U-Boot port on the LEOX's ELPT860 CPU board
11 ** ~~~~~~~~~~~
13 **=====================================================================
15 * SPDX-License-Identifier: GPL-2.0+
17 **=====================================================================
21 ** Note 1: In this file, you have to provide the following variable:
22 ** ------
23 ** flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]
24 ** 'flash_info_t' structure is defined into 'include/flash.h'
25 ** and defined as extern into 'common/cmd_flash.c'
27 ** Note 2: In this file, you have to provide the following functions:
28 ** ------
29 ** unsigned long flash_init(void)
30 ** called from 'board_init_r()' into 'common/board.c'
32 ** void flash_print_info(flash_info_t *info)
33 ** called from 'do_flinfo()' into 'common/cmd_flash.c'
35 ** int flash_erase(flash_info_t *info,
36 ** int s_first,
37 ** int s_last)
38 ** called from 'do_flerase()' & 'flash_sect_erase()' into 'common/cmd_flash.c'
40 ** int write_buff (flash_info_t *info,
41 ** uchar *src,
42 ** ulong addr,
43 ** ulong cnt)
44 ** called from 'flash_write()' into 'common/cmd_flash.c'
47 #include <common.h>
48 #include <mpc8xx.h>
51 #ifndef CONFIG_ENV_ADDR
52 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
53 #endif
55 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
57 /*-----------------------------------------------------------------------
58 * Internal Functions
60 static void flash_get_offsets (ulong base, flash_info_t *info);
61 static ulong flash_get_size (volatile unsigned char *addr, flash_info_t *info);
63 static int write_word (flash_info_t *info, ulong dest, ulong data);
64 static int write_byte (flash_info_t *info, ulong dest, uchar data);
66 /*-----------------------------------------------------------------------
69 unsigned long
70 flash_init (void)
72 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
73 volatile memctl8xx_t *memctl = &immap->im_memctl;
74 unsigned long size_b0;
75 int i;
77 /* Init: no FLASHes known */
78 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i)
80 flash_info[i].flash_id = FLASH_UNKNOWN;
83 /* Static FLASH Bank configuration here - FIXME XXX */
85 size_b0 = flash_get_size ((volatile unsigned char *)FLASH_BASE0_PRELIM,
86 &flash_info[0]);
88 if ( flash_info[0].flash_id == FLASH_UNKNOWN )
90 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
91 size_b0, size_b0<<20);
94 /* Remap FLASH according to real size */
95 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
96 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_PS_8 | BR_V;
98 /* Re-do sizing to get full correct info */
99 size_b0 = flash_get_size ((volatile unsigned char *)CONFIG_SYS_FLASH_BASE,
100 &flash_info[0]);
102 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
104 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
105 /* monitor protection ON by default */
106 flash_protect (FLAG_PROTECT_SET,
107 CONFIG_SYS_MONITOR_BASE,
108 CONFIG_SYS_MONITOR_BASE + monitor_flash_len-1,
109 &flash_info[0]);
110 #endif
112 #ifdef CONFIG_ENV_IS_IN_FLASH
113 /* ENV protection ON by default */
114 flash_protect(FLAG_PROTECT_SET,
115 CONFIG_ENV_ADDR,
116 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE-1,
117 &flash_info[0]);
118 #endif
120 flash_info[0].size = size_b0;
122 return (size_b0);
125 /*-----------------------------------------------------------------------
127 static void
128 flash_get_offsets (ulong base,
129 flash_info_t *info)
131 int i;
133 #define SECTOR_64KB 0x00010000
135 /* set up sector start adress table */
136 for (i = 0; i < info->sector_count; i++)
138 info->start[i] = base + (i * SECTOR_64KB);
142 /*-----------------------------------------------------------------------
144 void
145 flash_print_info (flash_info_t *info)
147 int i;
149 if ( info->flash_id == FLASH_UNKNOWN )
151 printf ("missing or unknown FLASH type\n");
152 return;
155 switch ( info->flash_id & FLASH_VENDMASK )
157 case FLASH_MAN_AMD: printf ("AMD "); break;
158 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
159 case FLASH_MAN_STM: printf ("STM (Thomson) "); break;
160 default: printf ("Unknown Vendor "); break;
163 switch ( info->flash_id & FLASH_TYPEMASK )
165 case FLASH_AM040: printf ("AM29F040 (4 Mbits)\n");
166 break;
167 default: printf ("Unknown Chip Type\n");
168 break;
171 printf (" Size: %ld KB in %d Sectors\n",
172 info->size >> 10, info->sector_count);
174 printf (" Sector Start Addresses:");
175 for (i=0; i<info->sector_count; ++i)
177 if ((i % 5) == 0)
178 printf ("\n ");
179 printf (" %08lX%s",
180 info->start[i],
181 info->protect[i] ? " (RO)" : " "
184 printf ("\n");
186 return;
189 /*-----------------------------------------------------------------------
193 /*-----------------------------------------------------------------------
197 * The following code cannot be run from FLASH!
200 static ulong
201 flash_get_size (volatile unsigned char *addr,
202 flash_info_t *info)
204 short i;
205 uchar value;
206 ulong base = (ulong)addr;
208 /* Write auto select command: read Manufacturer ID */
209 addr[0x0555] = 0xAA;
210 addr[0x02AA] = 0x55;
211 addr[0x0555] = 0x90;
213 value = addr[0];
215 switch ( value )
217 /* case AMD_MANUFACT: */
218 case 0x01:
219 info->flash_id = FLASH_MAN_AMD;
220 break;
221 /* case FUJ_MANUFACT: */
222 case 0x04:
223 info->flash_id = FLASH_MAN_FUJ;
224 break;
225 /* case STM_MANUFACT: */
226 case 0x20:
227 info->flash_id = FLASH_MAN_STM;
228 break;
230 default:
231 info->flash_id = FLASH_UNKNOWN;
232 info->sector_count = 0;
233 info->size = 0;
234 return (0); /* no or unknown flash */
237 value = addr[1]; /* device ID */
239 switch ( value )
241 case STM_ID_F040B:
242 case AMD_ID_F040B:
243 info->flash_id += FLASH_AM040; /* 4 Mbits = 512k * 8 */
244 info->sector_count = 8;
245 info->size = 0x00080000;
246 break;
248 default:
249 info->flash_id = FLASH_UNKNOWN;
250 return (0); /* => no or unknown flash */
253 /* set up sector start adress table */
254 for (i = 0; i < info->sector_count; i++)
256 info->start[i] = base + (i * 0x00010000);
259 /* check for protected sectors */
260 for (i = 0; i < info->sector_count; i++)
262 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
263 /* D0 = 1 if protected */
264 addr = (volatile unsigned char *)(info->start[i]);
265 info->protect[i] = addr[2] & 1;
269 * Prevent writes to uninitialized FLASH.
271 if ( info->flash_id != FLASH_UNKNOWN )
273 addr = (volatile unsigned char *)info->start[0];
275 *addr = 0xF0; /* reset bank */
278 return (info->size);
282 /*-----------------------------------------------------------------------
286 flash_erase (flash_info_t *info,
287 int s_first,
288 int s_last)
290 volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
291 int flag, prot, sect, l_sect;
292 ulong start, now, last;
294 if ( (s_first < 0) || (s_first > s_last) )
296 if ( info->flash_id == FLASH_UNKNOWN )
298 printf ("- missing\n");
300 else
302 printf ("- no sectors to erase\n");
304 return ( 1 );
307 if ( (info->flash_id == FLASH_UNKNOWN) ||
308 (info->flash_id > FLASH_AMD_COMP) )
310 printf ("Can't erase unknown flash type %08lx - aborted\n",
311 info->flash_id);
312 return ( 1 );
315 prot = 0;
316 for (sect=s_first; sect<=s_last; ++sect)
318 if ( info->protect[sect] )
320 prot++;
324 if ( prot )
326 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
328 else
330 printf ("\n");
333 l_sect = -1;
335 /* Disable interrupts which might cause a timeout here */
336 flag = disable_interrupts();
338 addr[0x0555] = 0xAA;
339 addr[0x02AA] = 0x55;
340 addr[0x0555] = 0x80;
341 addr[0x0555] = 0xAA;
342 addr[0x02AA] = 0x55;
344 /* Start erase on unprotected sectors */
345 for (sect = s_first; sect<=s_last; sect++)
347 if (info->protect[sect] == 0) /* not protected */
349 addr = (volatile unsigned char *)(info->start[sect]);
350 addr[0] = 0x30;
351 l_sect = sect;
355 /* re-enable interrupts if necessary */
356 if ( flag )
357 enable_interrupts();
359 /* wait at least 80us - let's wait 1 ms */
360 udelay (1000);
363 * We wait for the last triggered sector
365 if ( l_sect < 0 )
366 goto DONE;
368 start = get_timer (0);
369 last = start;
370 addr = (volatile unsigned char *)(info->start[l_sect]);
371 while ( (addr[0] & 0x80) != 0x80 )
373 if ( (now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT )
375 printf ("Timeout\n");
376 return ( 1 );
378 /* show that we're waiting */
379 if ( (now - last) > 1000 ) /* every second */
381 putc ('.');
382 last = now;
386 DONE:
387 /* reset to read mode */
388 addr = (volatile unsigned char *)info->start[0];
389 addr[0] = 0xF0; /* reset bank */
391 printf (" done\n");
393 return ( 0 );
396 /*-----------------------------------------------------------------------
397 * Copy memory to flash, returns:
398 * 0 - OK
399 * 1 - write timeout
400 * 2 - Flash not erased
404 write_buff (flash_info_t *info,
405 uchar *src,
406 ulong addr,
407 ulong cnt)
409 ulong cp, wp, data;
410 uchar bdata;
411 int i, l, rc;
413 if ( (info->flash_id & FLASH_TYPEMASK) == FLASH_AM040 )
415 /* Width of the data bus: 8 bits */
417 wp = addr;
419 while ( cnt )
421 bdata = *src++;
423 if ( (rc = write_byte(info, wp, bdata)) != 0 )
425 return (rc);
428 ++wp;
429 --cnt;
432 return ( 0 );
434 else
436 /* Width of the data bus: 32 bits */
438 wp = (addr & ~3); /* get lower word aligned address */
441 * handle unaligned start bytes
443 if ( (l = addr - wp) != 0 )
445 data = 0;
446 for (i=0, cp=wp; i<l; ++i, ++cp)
448 data = (data << 8) | (*(uchar *)cp);
450 for (; i<4 && cnt>0; ++i)
452 data = (data << 8) | *src++;
453 --cnt;
454 ++cp;
456 for (; cnt==0 && i<4; ++i, ++cp)
458 data = (data << 8) | (*(uchar *)cp);
461 if ( (rc = write_word(info, wp, data)) != 0 )
463 return (rc);
465 wp += 4;
469 * handle word aligned part
471 while ( cnt >= 4 )
473 data = 0;
474 for (i=0; i<4; ++i)
476 data = (data << 8) | *src++;
478 if ( (rc = write_word(info, wp, data)) != 0 )
480 return (rc);
482 wp += 4;
483 cnt -= 4;
486 if ( cnt == 0 )
488 return (0);
492 * handle unaligned tail bytes
494 data = 0;
495 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp)
497 data = (data << 8) | *src++;
498 --cnt;
500 for (; i<4; ++i, ++cp)
502 data = (data << 8) | (*(uchar *)cp);
505 return (write_word(info, wp, data));
509 /*-----------------------------------------------------------------------
510 * Write a word to Flash, returns:
511 * 0 - OK
512 * 1 - write timeout
513 * 2 - Flash not erased
515 static int
516 write_word (flash_info_t *info,
517 ulong dest,
518 ulong data)
520 vu_long *addr = (vu_long*)(info->start[0]);
521 ulong start;
522 int flag;
524 /* Check if Flash is (sufficiently) erased */
525 if ( (*((vu_long *)dest) & data) != data )
527 return (2);
529 /* Disable interrupts which might cause a timeout here */
530 flag = disable_interrupts();
532 addr[0x0555] = 0x00AA00AA;
533 addr[0x02AA] = 0x00550055;
534 addr[0x0555] = 0x00A000A0;
536 *((vu_long *)dest) = data;
538 /* re-enable interrupts if necessary */
539 if ( flag )
540 enable_interrupts();
542 /* data polling for D7 */
543 start = get_timer (0);
544 while ( (*((vu_long *)dest) & 0x00800080) != (data & 0x00800080) )
546 if ( get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT )
548 return (1);
552 return (0);
555 /*-----------------------------------------------------------------------
556 * Write a byte to Flash, returns:
557 * 0 - OK
558 * 1 - write timeout
559 * 2 - Flash not erased
561 static int
562 write_byte (flash_info_t *info,
563 ulong dest,
564 uchar data)
566 volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
567 ulong start;
568 int flag;
570 /* Check if Flash is (sufficiently) erased */
571 if ( (*((volatile unsigned char *)dest) & data) != data )
573 return (2);
575 /* Disable interrupts which might cause a timeout here */
576 flag = disable_interrupts();
578 addr[0x0555] = 0xAA;
579 addr[0x02AA] = 0x55;
580 addr[0x0555] = 0xA0;
582 *((volatile unsigned char *)dest) = data;
584 /* re-enable interrupts if necessary */
585 if ( flag )
586 enable_interrupts();
588 /* data polling for D7 */
589 start = get_timer (0);
590 while ( (*((volatile unsigned char *)dest) & 0x80) != (data & 0x80) )
592 if ( get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT )
594 return (1);
598 return (0);
601 /*-----------------------------------------------------------------------