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(CFG_ENV_IS_IN_FLASH)
30 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
33 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
35 # ifndef CFG_ENV_SECT_SIZE
36 # define CFG_ENV_SECT_SIZE CFG_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)
71 /* Enable flash writes on CPC45 */
73 tempChar = BOARD_CTRL;
75 tempChar |= (B_CTRL_FWPT_1 | B_CTRL_FWRE_1);
77 tempChar &= ~(B_CTRL_FWPT_0 | B_CTRL_FWRE_0);
79 BOARD_CTRL = tempChar;
82 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
83 vu_long *addr = (vu_long *)(CFG_FLASH_BASE + i * FLASH_BANK_SIZE);
87 DEBUGF ("Flash bank # %d:\n"
88 "\tManuf. ID @ 0x%08lX: 0x%08lX\n"
89 "\tDevice ID @ 0x%08lX: 0x%08lX\n",
91 (ulong)(&addr[0]), addr[0],
92 (ulong)(&addr[2]), addr[2]);
95 if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT) &&
96 (addr[2] == addr[3]) && (addr[2] == INTEL_ID_28F160F3T))
99 flash_info[i].flash_id = (FLASH_MAN_INTEL & FLASH_VENDMASK) |
100 (INTEL_ID_28F160F3T & FLASH_TYPEMASK);
103 flash_info[i].flash_id = FLASH_UNKNOWN;
104 addr[0] = 0xFFFFFFFF;
108 DEBUGF ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
110 addr[0] = 0xFFFFFFFF;
112 flash_info[i].size = FLASH_BANK_SIZE;
113 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
114 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
115 for (j = 0; j < flash_info[i].sector_count; j++) {
117 flash_info[i].start[j] = CFG_FLASH_BASE +
118 i * FLASH_BANK_SIZE +
119 (MAIN_SECT_SIZE * 31) + (j - 31) * PARAM_SECT_SIZE;
121 flash_info[i].start[j] = CFG_FLASH_BASE +
122 i * FLASH_BANK_SIZE +
126 size += flash_info[i].size;
129 /* Protect monitor and environment sectors
131 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
132 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE + FLASH_BANK_SIZE
133 flash_protect(FLAG_PROTECT_SET,
135 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
138 flash_protect(FLAG_PROTECT_SET,
140 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
145 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
146 #if CFG_ENV_ADDR >= CFG_FLASH_BASE + FLASH_BANK_SIZE
147 flash_protect(FLAG_PROTECT_SET,
149 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
152 flash_protect(FLAG_PROTECT_SET,
154 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
163 /*-----------------------------------------------------------------------
165 void flash_print_info (flash_info_t * info)
169 switch ((i = info->flash_id & FLASH_VENDMASK)) {
170 case (FLASH_MAN_INTEL & FLASH_VENDMASK):
174 printf ("Unknown Vendor 0x%04x ", i);
178 switch ((i = info->flash_id & FLASH_TYPEMASK)) {
179 case (INTEL_ID_28F160F3T & FLASH_TYPEMASK):
180 printf ("28F160F3T (16Mbit)\n");
183 printf ("Unknown Chip Type 0x%04x\n", i);
188 printf (" Size: %ld MB in %d Sectors\n",
189 info->size >> 20, info->sector_count);
191 printf (" Sector Start Addresses:");
192 for (i = 0; i < info->sector_count; i++) {
196 printf (" %08lX%s", info->start[i],
197 info->protect[i] ? " (RO)" : " ");
205 /*-----------------------------------------------------------------------
208 int flash_erase (flash_info_t *info, int s_first, int s_last)
210 int flag, prot, sect;
211 ulong start, now, last;
213 DEBUGF ("Erase flash bank %d sect %d ... %d\n",
214 info - &flash_info[0], s_first, s_last);
216 if ((s_first < 0) || (s_first > s_last)) {
217 if (info->flash_id == FLASH_UNKNOWN) {
218 printf ("- missing\n");
220 printf ("- no sectors to erase\n");
225 if ((info->flash_id & FLASH_VENDMASK) !=
226 (FLASH_MAN_INTEL & FLASH_VENDMASK)) {
227 printf ("Can erase only Intel flash types - aborted\n");
232 for (sect=s_first; sect<=s_last; ++sect) {
233 if (info->protect[sect]) {
239 printf ("- Warning: %d protected sectors will not be erased!\n",
245 start = get_timer (0);
247 /* Start erase on unprotected sectors */
248 for (sect = s_first; sect<=s_last; sect++) {
249 if (info->protect[sect] == 0) { /* not protected */
250 vu_long *addr = (vu_long *)(info->start[sect]);
252 DEBUGF ("Erase sect %d @ 0x%08lX\n",
255 /* Disable interrupts which might cause a timeout
258 flag = disable_interrupts();
260 addr[0] = 0x00500050; /* clear status register */
261 addr[0] = 0x00200020; /* erase setup */
262 addr[0] = 0x00D000D0; /* erase confirm */
264 addr[1] = 0x00500050; /* clear status register */
265 addr[1] = 0x00200020; /* erase setup */
266 addr[1] = 0x00D000D0; /* erase confirm */
268 /* re-enable interrupts if necessary */
272 /* wait at least 80us - let's wait 1 ms */
275 while (((addr[0] & 0x00800080) != 0x00800080) ||
276 ((addr[1] & 0x00800080) != 0x00800080) ) {
277 if ((now=get_timer(start)) >
278 CFG_FLASH_ERASE_TOUT) {
279 printf ("Timeout\n");
280 addr[0] = 0x00B000B0; /* suspend erase */
281 addr[0] = 0x00FF00FF; /* to read mode */
285 /* show that we're waiting */
286 if ((now - last) > 1000) { /* every second */
292 addr[0] = 0x00FF00FF;
299 /*-----------------------------------------------------------------------
300 * Copy memory to flash, returns:
303 * 2 - Flash not erased
304 * 4 - Flash not identified
307 #define FLASH_WIDTH 8 /* flash bus width in bytes */
309 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
314 ulong *datah = &data[0];
315 ulong *datal = &data[1];
317 DEBUGF ("Flash write_buff: @ 0x%08lx, src 0x%08lx len %ld\n",
318 addr, (ulong)src, cnt);
320 if (info->flash_id == FLASH_UNKNOWN) {
325 set_msr(msr | MSR_FP);
327 wp = (addr & ~(FLASH_WIDTH-1)); /* get lower aligned address */
330 * handle unaligned start bytes
332 if ((l = addr - wp) != 0) {
335 for (i = 0, cp = wp; i < l; i++, cp++) {
337 *datah = (*datah << 8) |
338 ((*datal & 0xFF000000) >> 24);
341 *datal = (*datal << 8) | (*(uchar *)cp);
343 for (; i < FLASH_WIDTH && cnt > 0; ++i) {
351 *datah = (*datah << 8) |
352 ((*datal & 0xFF000000) >> 24);
355 *datal = (*datal << 8) | tmp;
360 for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp) {
362 *datah = (*datah << 8) |
363 ((*datal & 0xFF000000) >> 24);
366 *datal = (*datah << 8) | (*(uchar *)cp);
369 if ((rc = write_data(info, wp, data)) != 0) {
378 * handle FLASH_WIDTH aligned part
380 while (cnt >= FLASH_WIDTH) {
381 *datah = *(ulong *)src;
382 *datal = *(ulong *)(src + 4);
383 if ((rc = write_data(info, wp, data)) != 0) {
398 * handle unaligned tail bytes
401 for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) {
409 *datah = (*datah << 8) | ((*datal & 0xFF000000) >> 24);
412 *datal = (*datal << 8) | tmp;
417 for (; i < FLASH_WIDTH; ++i, ++cp) {
419 *datah = (*datah << 8) | ((*datal & 0xFF000000) >> 24);
422 *datal = (*datal << 8) | (*(uchar *)cp);
425 rc = write_data(info, wp, data);
431 /*-----------------------------------------------------------------------
432 * Write a word to Flash, returns:
435 * 2 - Flash not erased
437 static int write_data (flash_info_t *info, ulong dest, ulong *data)
439 vu_long *addr = (vu_long *)dest;
443 /* Check if Flash is (sufficiently) erased */
444 if (((addr[0] & data[0]) != data[0]) ||
445 ((addr[1] & data[1]) != data[1]) ) {
448 /* Disable interrupts which might cause a timeout here */
449 flag = disable_interrupts();
451 addr[0] = 0x00400040; /* write setup */
452 write_via_fpu(addr, data);
454 /* re-enable interrupts if necessary */
458 start = get_timer (0);
460 while (((addr[0] & 0x00800080) != 0x00800080) ||
461 ((addr[1] & 0x00800080) != 0x00800080) ) {
462 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
463 addr[0] = 0x00FF00FF; /* restore read mode */
468 addr[0] = 0x00FF00FF; /* restore read mode */
473 /*-----------------------------------------------------------------------
475 static void write_via_fpu(vu_long *addr, ulong *data)
477 __asm__ __volatile__ ("lfd 1, 0(%0)" : : "r" (data));
478 __asm__ __volatile__ ("stfd 1, 0(%0)" : : "r" (addr));
480 /*-----------------------------------------------------------------------
482 static __inline__ unsigned long get_msr(void)
486 __asm__ __volatile__ ("mfmsr %0" : "=r" (msr) :);
490 static __inline__ void set_msr(unsigned long msr)
492 __asm__ __volatile__ ("mtmsr %0" : : "r" (msr));