2 * (C) Copyright 2001-2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <asm/processor.h>
28 #if defined(CONFIG_ENV_IS_IN_FLASH)
29 # ifndef CONFIG_ENV_ADDR
30 # define CONFIG_ENV_ADDR (CFG_FLASH_BASE + CONFIG_ENV_OFFSET)
32 # ifndef CONFIG_ENV_SIZE
33 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
35 # ifndef CONFIG_ENV_SECT_SIZE
36 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
40 #define FLASH_BANK_SIZE 0x800000
41 #define MAIN_SECT_SIZE 0x40000
42 #define PARAM_SECT_SIZE 0x8000
44 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
46 static int write_data (flash_info_t * info, ulong dest, ulong * data);
47 static void write_via_fpu (vu_long * addr, ulong * data);
48 static __inline__ unsigned long get_msr (void);
49 static __inline__ void set_msr (unsigned long msr);
51 /*---------------------------------------------------------------------*/
54 /*---------------------------------------------------------------------*/
56 #define DEBUGF(fmt,args...) printf(fmt ,##args)
58 #define DEBUGF(fmt,args...)
60 /*---------------------------------------------------------------------*/
62 /*-----------------------------------------------------------------------
65 unsigned long flash_init (void)
72 /* Enable flash writes on CPC45 */
74 tempChar = BOARD_CTRL;
76 tempChar |= (B_CTRL_FWPT_1 | B_CTRL_FWRE_1);
78 tempChar &= ~(B_CTRL_FWPT_0 | B_CTRL_FWRE_0);
80 BOARD_CTRL = tempChar;
82 __asm__ volatile ("sync\n eieio");
84 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
85 vu_long *addr = (vu_long *) (CFG_FLASH_BASE + i * FLASH_BANK_SIZE);
89 __asm__ volatile ("sync\n eieio");
93 DEBUGF ("Flash bank # %d:\n"
94 "\tManuf. ID @ 0x%08lX: 0x%08lX\n"
95 "\tDevice ID @ 0x%08lX: 0x%08lX\n",
97 (ulong) (&addr[0]), addr[0],
98 (ulong) (&addr[2]), addr[2]);
101 if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT) &&
102 (addr[2] == addr[3]) && (addr[2] == INTEL_ID_28F160F3T)) {
104 flash_info[i].flash_id =
105 (FLASH_MAN_INTEL & FLASH_VENDMASK) |
106 (INTEL_ID_28F160F3T & FLASH_TYPEMASK);
108 } else if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT)
109 && (addr[2] == addr[3])
110 && (addr[2] == INTEL_ID_28F160C3T)) {
112 flash_info[i].flash_id =
113 (FLASH_MAN_INTEL & FLASH_VENDMASK) |
114 (INTEL_ID_28F160C3T & FLASH_TYPEMASK);
117 flash_info[i].flash_id = FLASH_UNKNOWN;
118 addr[0] = 0xFFFFFFFF;
122 DEBUGF ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
124 addr[0] = 0xFFFFFFFF;
126 flash_info[i].size = FLASH_BANK_SIZE;
127 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
128 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
129 for (j = 0; j < flash_info[i].sector_count; j++) {
131 flash_info[i].start[j] = CFG_FLASH_BASE +
132 i * FLASH_BANK_SIZE +
133 (MAIN_SECT_SIZE * 31) + (j -
137 flash_info[i].start[j] = CFG_FLASH_BASE +
138 i * FLASH_BANK_SIZE +
143 /* unlock sectors, if 160C3T */
145 for (j = 0; j < flash_info[i].sector_count; j++) {
146 tmpaddr = (vu_long *) flash_info[i].start[j];
148 if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
149 (INTEL_ID_28F160C3T & FLASH_TYPEMASK)) {
150 tmpaddr[0] = 0x00600060;
151 tmpaddr[0] = 0x00D000D0;
152 tmpaddr[1] = 0x00600060;
153 tmpaddr[1] = 0x00D000D0;
157 size += flash_info[i].size;
159 addr[0] = 0x00FF00FF;
160 addr[1] = 0x00FF00FF;
163 /* Protect monitor and environment sectors
165 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE + FLASH_BANK_SIZE
166 flash_protect (FLAG_PROTECT_SET,
168 CFG_MONITOR_BASE + monitor_flash_len - 1,
171 flash_protect (FLAG_PROTECT_SET,
173 CFG_MONITOR_BASE + monitor_flash_len - 1,
177 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
178 #if CONFIG_ENV_ADDR >= CFG_FLASH_BASE + FLASH_BANK_SIZE
179 flash_protect (FLAG_PROTECT_SET,
181 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[1]);
183 flash_protect (FLAG_PROTECT_SET,
185 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
193 /*-----------------------------------------------------------------------
195 void flash_print_info (flash_info_t * info)
199 switch ((i = info->flash_id & FLASH_VENDMASK)) {
200 case (FLASH_MAN_INTEL & FLASH_VENDMASK):
204 printf ("Unknown Vendor 0x%04x ", i);
208 switch ((i = info->flash_id & FLASH_TYPEMASK)) {
209 case (INTEL_ID_28F160F3T & FLASH_TYPEMASK):
210 printf ("28F160F3T (16Mbit)\n");
213 case (INTEL_ID_28F160C3T & FLASH_TYPEMASK):
214 printf ("28F160C3T (16Mbit)\n");
218 printf ("Unknown Chip Type 0x%04x\n", i);
223 printf (" Size: %ld MB in %d Sectors\n",
224 info->size >> 20, info->sector_count);
226 printf (" Sector Start Addresses:");
227 for (i = 0; i < info->sector_count; i++) {
231 printf (" %08lX%s", info->start[i],
232 info->protect[i] ? " (RO)" : " ");
240 /*-----------------------------------------------------------------------
243 int flash_erase (flash_info_t * info, int s_first, int s_last)
245 int flag, prot, sect;
246 ulong start, now, last;
248 DEBUGF ("Erase flash bank %d sect %d ... %d\n",
249 info - &flash_info[0], s_first, s_last);
251 if ((s_first < 0) || (s_first > s_last)) {
252 if (info->flash_id == FLASH_UNKNOWN) {
253 printf ("- missing\n");
255 printf ("- no sectors to erase\n");
260 if ((info->flash_id & FLASH_VENDMASK) !=
261 (FLASH_MAN_INTEL & FLASH_VENDMASK)) {
262 printf ("Can erase only Intel flash types - aborted\n");
267 for (sect = s_first; sect <= s_last; ++sect) {
268 if (info->protect[sect]) {
274 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
279 start = get_timer (0);
281 /* Start erase on unprotected sectors */
282 for (sect = s_first; sect <= s_last; sect++) {
283 if (info->protect[sect] == 0) { /* not protected */
284 vu_long *addr = (vu_long *) (info->start[sect]);
286 DEBUGF ("Erase sect %d @ 0x%08lX\n",
289 /* Disable interrupts which might cause a timeout
292 flag = disable_interrupts ();
294 addr[0] = 0x00500050; /* clear status register */
295 addr[0] = 0x00200020; /* erase setup */
296 addr[0] = 0x00D000D0; /* erase confirm */
298 addr[1] = 0x00500050; /* clear status register */
299 addr[1] = 0x00200020; /* erase setup */
300 addr[1] = 0x00D000D0; /* erase confirm */
302 /* re-enable interrupts if necessary */
304 enable_interrupts ();
306 /* wait at least 80us - let's wait 1 ms */
309 while (((addr[0] & 0x00800080) != 0x00800080) ||
310 ((addr[1] & 0x00800080) != 0x00800080)) {
311 if ((now = get_timer (start)) >
312 CFG_FLASH_ERASE_TOUT) {
313 printf ("Timeout\n");
314 addr[0] = 0x00B000B0; /* suspend erase */
315 addr[0] = 0x00FF00FF; /* to read mode */
319 /* show that we're waiting */
320 if ((now - last) > 1000) { /* every second */
326 addr[0] = 0x00FF00FF;
333 /*-----------------------------------------------------------------------
334 * Copy memory to flash, returns:
337 * 2 - Flash not erased
338 * 4 - Flash not identified
341 #define FLASH_WIDTH 8 /* flash bus width in bytes */
343 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
348 ulong *datah = &data[0];
349 ulong *datal = &data[1];
351 DEBUGF ("Flash write_buff: @ 0x%08lx, src 0x%08lx len %ld\n",
352 addr, (ulong) src, cnt);
354 if (info->flash_id == FLASH_UNKNOWN) {
359 set_msr (msr | MSR_FP);
361 wp = (addr & ~(FLASH_WIDTH - 1)); /* get lower aligned address */
364 * handle unaligned start bytes
366 if ((l = addr - wp) != 0) {
369 for (i = 0, cp = wp; i < l; i++, cp++) {
371 *datah = (*datah << 8) |
372 ((*datal & 0xFF000000) >> 24);
375 *datal = (*datal << 8) | (*(uchar *) cp);
377 for (; i < FLASH_WIDTH && cnt > 0; ++i) {
381 *datah = (*datah << 8) |
382 ((*datal & 0xFF000000) >> 24);
385 *datal = (*datal << 8) | tmp;
390 for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp) {
392 *datah = (*datah << 8) |
393 ((*datal & 0xFF000000) >> 24);
396 *datal = (*datah << 8) | (*(uchar *) cp);
399 if ((rc = write_data (info, wp, data)) != 0) {
408 * handle FLASH_WIDTH aligned part
410 while (cnt >= FLASH_WIDTH) {
411 *datah = *(ulong *) src;
412 *datal = *(ulong *) (src + 4);
413 if ((rc = write_data (info, wp, data)) != 0) {
428 * handle unaligned tail bytes
431 for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) {
435 *datah = (*datah << 8) | ((*datal & 0xFF000000) >>
439 *datal = (*datal << 8) | tmp;
443 for (; i < FLASH_WIDTH; ++i, ++cp) {
445 *datah = (*datah << 8) | ((*datal & 0xFF000000) >>
449 *datal = (*datal << 8) | (*(uchar *) cp);
452 rc = write_data (info, wp, data);
458 /*-----------------------------------------------------------------------
459 * Write a word to Flash, returns:
462 * 2 - Flash not erased
464 static int write_data (flash_info_t * info, ulong dest, ulong * data)
466 vu_long *addr = (vu_long *) dest;
470 /* Check if Flash is (sufficiently) erased */
471 if (((addr[0] & data[0]) != data[0]) ||
472 ((addr[1] & data[1]) != data[1])) {
475 /* Disable interrupts which might cause a timeout here */
476 flag = disable_interrupts ();
478 addr[0] = 0x00400040; /* write setup */
479 write_via_fpu (addr, data);
481 /* re-enable interrupts if necessary */
483 enable_interrupts ();
485 start = get_timer (0);
487 while (((addr[0] & 0x00800080) != 0x00800080) ||
488 ((addr[1] & 0x00800080) != 0x00800080)) {
489 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
490 addr[0] = 0x00FF00FF; /* restore read mode */
495 addr[0] = 0x00FF00FF; /* restore read mode */
500 /*-----------------------------------------------------------------------
502 static void write_via_fpu (vu_long * addr, ulong * data)
504 __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
505 __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
508 /*-----------------------------------------------------------------------
510 static __inline__ unsigned long get_msr (void)
514 __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
519 static __inline__ void set_msr (unsigned long msr)
521 __asm__ __volatile__ ("mtmsr %0"::"r" (msr));