1 /********************************************************************
3 * Unless otherwise specified, Copyright (C) 2004-2005 Barco Control Rooms
10 * Last ChangeLog Entry
12 * Revision 1.1.5.1 2011-02-28 14:41:59 lothar
13 * imported Freescale specific U-Boot additions for i.MX28,... release L2.6.31_10.08.01
15 * Revision 1.3 2005/02/21 12:48:58 mleeman
16 * update of copyright years (feedback wd)
18 * Revision 1.2 2005/02/21 11:04:04 mleeman
19 * remove dead code and Coding style (feedback wd)
21 * Revision 1.1 2005/02/14 09:23:46 mleeman
22 * - moved 'barcohydra' directory to a more generic barco; since we will be
23 * supporting and adding multiple boards
25 * Revision 1.2 2005/02/09 12:56:23 mleeman
26 * add generic header to track changes in sources
29 *******************************************************************/
33 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
35 * See file CREDITS for list of people who contributed to this
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License as
40 * published by the Free Software Foundation; either version 2 of
41 * the License, or (at your option) any later version.
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
56 #include <asm/processor.h>
59 #define ROM_CS0_START 0xFF800000
60 #define ROM_CS1_START 0xFF000000
62 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
64 #if defined(CONFIG_ENV_IS_IN_FLASH)
65 # ifndef CONFIG_ENV_ADDR
66 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
68 # ifndef CONFIG_ENV_SIZE
69 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
71 # ifndef CONFIG_ENV_SECT_SIZE
72 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
76 /*-----------------------------------------------------------------------
79 static int write_word (flash_info_t *info, ulong dest, ulong data);
81 /*flash command address offsets*/
87 #define FLASH_WORD_SIZE unsigned char
89 /*-----------------------------------------------------------------------
92 static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
95 FLASH_WORD_SIZE extval;
96 unsigned short intval;
99 static unsigned long flash_id(unsigned char mfct, unsigned char chip)
101 static const map_entry mfct_map[] = {
102 {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
103 {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
104 {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
105 {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
106 {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
107 {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
110 static const map_entry chip_map[] = {
111 {AMD_ID_F040B, FLASH_AM040},
112 {AMD_ID_F033C, FLASH_AM033},
113 {AMD_ID_F065D, FLASH_AM065},
114 {ATM_ID_LV040, FLASH_AT040},
115 {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
119 unsigned long result = FLASH_UNKNOWN;
122 for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++){
123 if(p->extval == chip){
124 result = FLASH_VENDMASK | p->intval;
130 for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++){
131 if(p->extval == mfct){
132 result &= ~FLASH_VENDMASK;
133 result |= (unsigned long) p->intval << 16;
142 unsigned long flash_init(void)
146 static const ulong flash_banks[] = CONFIG_SYS_FLASH_BANKS;
148 /* Init: no FLASHes known */
149 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++){
150 flash_info_t * const pflinfo = &flash_info[i];
151 pflinfo->flash_id = FLASH_UNKNOWN;
153 pflinfo->sector_count = 0;
156 /* Enable writes to Hydra/Argus flash */
158 register unsigned int temp;
159 CONFIG_READ_WORD(PICR1,temp);
160 temp |= PICR1_FLASH_WR_EN;
161 CONFIG_WRITE_WORD(PICR1,temp);
164 for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++){
165 flash_info_t * const pflinfo = &flash_info[i];
166 const unsigned long base_address = flash_banks[i];
167 volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
169 /* write autoselect sequence */
170 flash[0x5555] = 0xaa;
171 flash[0x2aaa] = 0x55;
172 flash[0x5555] = 0x90;
173 __asm__ __volatile__("sync");
175 pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
177 switch(pflinfo->flash_id & FLASH_TYPEMASK){
179 pflinfo->size = 0x00200000;
180 pflinfo->sector_count = 64;
181 for(j = 0; j < 64; j++){
182 pflinfo->start[j] = base_address + 0x00010000 * j;
183 pflinfo->protect[j] = flash[(j << 16) | 0x2];
187 pflinfo->size = 0x00800000;
188 pflinfo->sector_count =128;
189 for(j = 0; j < 128; j++){
190 pflinfo->start[j] = base_address + 0x00010000 * j;
191 pflinfo->protect[j] = flash[(j << 16) | 0x2];
195 pflinfo->size = 0x00080000;
196 pflinfo->sector_count = 2;
197 pflinfo->start[0] = base_address ;
198 pflinfo->start[1] = base_address + 0x00004000;
199 pflinfo->protect[0] = ((flash[0x02] & 0X01)==0) ? 0X02 : 0X01;
200 pflinfo->protect[1] = 0X02;
203 pflinfo->size = 0x00080000;
204 pflinfo->sector_count = 8;
205 for(j = 0; j < 8; j++){
206 pflinfo->start[j] = base_address + 0x00010000 * j;
207 pflinfo->protect[j] = flash[(j << 16) | 0x2];
211 pflinfo->size = 0x00100000;
212 pflinfo->sector_count = 19;
213 pflinfo->start[0] = base_address;
214 pflinfo->start[1] = base_address + 0x4000;
215 pflinfo->start[2] = base_address + 0x6000;
216 pflinfo->start[3] = base_address + 0x8000;
217 for(j = 1; j < 16; j++){
218 pflinfo->start[j+3] = base_address + 0x00010000 * j;
222 /* Protect monitor and environment sectors */
223 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
224 flash_protect(FLAG_PROTECT_SET,
225 CONFIG_SYS_MONITOR_BASE,
226 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
230 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
231 flash_protect(FLAG_PROTECT_SET,
233 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
237 /* reset device to read mode */
238 flash[0x0000] = 0xf0;
239 __asm__ __volatile__("sync");
242 return flash_info[0].size + flash_info[1].size;
245 /*-----------------------------------------------------------------------
247 void flash_print_info(flash_info_t *info)
249 static const char unk[] = "Unknown";
250 const char *mfct = unk, *type = unk;
253 if(info->flash_id != FLASH_UNKNOWN){
254 switch(info->flash_id & FLASH_VENDMASK){
271 mfct = "Bright Microelectonics";
273 case FLASH_MAN_INTEL:
278 switch(info->flash_id & FLASH_TYPEMASK){
280 type = "AT49LV040 (512K * 8, uniform sector size)";
283 type = "AM29F033C (4 Mbit * 8, uniform sector size)";
286 type = "AM29F040B (512K * 8, uniform sector size)";
289 type = "AM29F0465D ( 8 MBit * 8, uniform sector size) or part of AM29F652D( 16 MB)";
292 type = "AM29LV400B (4 Mbit, bottom boot sect)";
295 type = "AM29LV400T (4 Mbit, top boot sector)";
298 type = "AM29LV800B (8 Mbit, bottom boot sect)";
301 type = "AM29LV800T (8 Mbit, top boot sector)";
304 type = "AM29LV160T (16 Mbit, top boot sector)";
307 type = "AM29LV320B (32 Mbit, bottom boot sect)";
310 type = "AM29LV320T (32 Mbit, top boot sector)";
313 type = "M29W800AB (8 Mbit, bottom boot sect)";
316 type = "SST39LF/VF800 (8 Mbit, uniform sector size)";
319 type = "SST39LF/VF160 (16 Mbit, uniform sector size)";
325 "\n Brand: %s Type: %s\n"
326 " Size: %lu KB in %d Sectors\n",
333 printf (" Sector Start Addresses:");
335 for (i = 0; i < info->sector_count; i++){
338 unsigned long * flash = (unsigned long *) info->start[i];
341 * Check if whole sector is erased
344 (i != (info->sector_count - 1)) ?
345 (info->start[i + 1] - info->start[i]) >> 2 :
346 (info->start[0] + info->size - info->start[i]) >> 2;
349 flash = (unsigned long *) info->start[i], erased = 1;
350 (flash != (unsigned long *) info->start[i] + size) && erased;
353 erased = *flash == ~0x0UL;
358 (i % 5) ? "" : "\n ",
361 info->protect[i] ? "RO" : " "
369 int flash_erase(flash_info_t *info, int s_first, int s_last)
371 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
372 int flag, prot, sect, l_sect;
373 ulong start, now, last;
376 if ((s_first < 0) || (s_first > s_last)) {
377 if (info->flash_id == FLASH_UNKNOWN) {
378 printf ("- missing\n");
380 printf ("- no sectors to erase\n");
385 if ((info->flash_id == FLASH_UNKNOWN) ||
386 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
387 printf ("Can't erase unknown flash type - aborted\n");
392 for (sect=s_first; sect<=s_last; ++sect) {
393 if (info->protect[sect]) {
399 printf ("- Warning: %d protected sectors will not be erased!\n",
407 /* Check the ROM CS */
408 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START)){
415 /* Disable interrupts which might cause a timeout here */
416 flag = disable_interrupts();
418 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
419 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
420 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
421 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
422 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
424 /* Start erase on unprotected sectors */
425 for (sect = s_first; sect<=s_last; sect++) {
426 if (info->protect[sect] == 0) { /* not protected */
427 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
428 (info->start[sect] - info->start[0]) << sh8b));
429 if (info->flash_id & FLASH_MAN_SST){
430 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
431 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
432 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
433 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
434 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
435 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
436 udelay(30000); /* wait 30 ms */
439 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
444 /* re-enable interrupts if necessary */
449 /* wait at least 80us - let's wait 1 ms */
453 * We wait for the last triggered sector
459 start = get_timer (0);
461 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
462 (info->start[l_sect] - info->start[0]) << sh8b));
463 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
464 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
465 printf ("Timeout\n");
468 /* show that we're waiting */
469 if ((now - last) > 1000) { /* every second */
476 /* reset to read mode */
477 addr = (FLASH_WORD_SIZE *)info->start[0];
478 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
484 /*-----------------------------------------------------------------------
485 * Copy memory to flash, returns:
488 * 2 - Flash not erased
491 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
496 wp = (addr & ~3); /* get lower word aligned address */
499 * handle unaligned start bytes
501 if ((l = addr - wp) != 0) {
503 for (i=0, cp=wp; i<l; ++i, ++cp) {
504 data = (data << 8) | (*(uchar *)cp);
506 for (; i<4 && cnt>0; ++i) {
507 data = (data << 8) | *src++;
511 for (; cnt==0 && i<4; ++i, ++cp) {
512 data = (data << 8) | (*(uchar *)cp);
515 if ((rc = write_word(info, wp, data)) != 0) {
522 * handle word aligned part
526 for (i=0; i<4; ++i) {
527 data = (data << 8) | *src++;
529 if ((rc = write_word(info, wp, data)) != 0) {
541 * handle unaligned tail bytes
544 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
545 data = (data << 8) | *src++;
548 for (; i<4; ++i, ++cp) {
549 data = (data << 8) | (*(uchar *)cp);
552 return (write_word(info, wp, data));
555 /*-----------------------------------------------------------------------
556 * Write a word to Flash, returns:
559 * 2 - Flash not erased
561 static int write_word (flash_info_t *info, ulong dest, ulong data)
563 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
564 volatile FLASH_WORD_SIZE *dest2;
565 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
571 /* Check the ROM CS */
572 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START)){
579 dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
582 /* Check if Flash is (sufficiently) erased */
583 if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
586 /* Disable interrupts which might cause a timeout here */
587 flag = disable_interrupts();
589 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++){
590 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
591 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
592 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
594 dest2[i << sh8b] = data2[i];
596 /* re-enable interrupts if necessary */
601 /* data polling for D7 */
602 start = get_timer (0);
603 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
604 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
605 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
614 /*----------------------------------------------------------------------- */