blob: 917db34a4b53095e88fe1817bf2cf691cedcdb7f [file] [log] [blame]
wdenk3bac3512003-03-12 10:41:04 +00001/*
2 * (C) Copyright 2001-2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk3bac3512003-03-12 10:41:04 +00006 */
7
8#include <common.h>
9#include <mpc824x.h>
10#include <asm/processor.h>
11
Jean-Christophe PLAGNIOL-VILLARD5a1aceb2008-09-10 22:48:04 +020012#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020013# ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020014# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenk3bac3512003-03-12 10:41:04 +000015# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020016# ifndef CONFIG_ENV_SIZE
17# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
wdenk3bac3512003-03-12 10:41:04 +000018# endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020019# ifndef CONFIG_ENV_SECT_SIZE
20# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
wdenk3bac3512003-03-12 10:41:04 +000021# endif
22#endif
23
24#define FLASH_BANK_SIZE 0x800000
25#define MAIN_SECT_SIZE 0x40000
26#define PARAM_SECT_SIZE 0x8000
27
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk3bac3512003-03-12 10:41:04 +000029
wdenk49822e22004-06-19 21:19:10 +000030static int write_data (flash_info_t * info, ulong dest, ulong * data);
31static void write_via_fpu (vu_long * addr, ulong * data);
32static __inline__ unsigned long get_msr (void);
33static __inline__ void set_msr (unsigned long msr);
wdenk3bac3512003-03-12 10:41:04 +000034
35/*---------------------------------------------------------------------*/
36#undef DEBUG_FLASH
37
38/*---------------------------------------------------------------------*/
39#ifdef DEBUG_FLASH
40#define DEBUGF(fmt,args...) printf(fmt ,##args)
41#else
42#define DEBUGF(fmt,args...)
43#endif
44/*---------------------------------------------------------------------*/
45
46/*-----------------------------------------------------------------------
47 */
48
wdenk49822e22004-06-19 21:19:10 +000049unsigned long flash_init (void)
wdenk3bac3512003-03-12 10:41:04 +000050{
wdenk49822e22004-06-19 21:19:10 +000051 int i, j;
52 ulong size = 0;
53 uchar tempChar;
54 vu_long *tmpaddr;
wdenk3bac3512003-03-12 10:41:04 +000055
wdenk49822e22004-06-19 21:19:10 +000056 /* Enable flash writes on CPC45 */
wdenk3bac3512003-03-12 10:41:04 +000057
wdenk49822e22004-06-19 21:19:10 +000058 tempChar = BOARD_CTRL;
wdenk3bac3512003-03-12 10:41:04 +000059
wdenk49822e22004-06-19 21:19:10 +000060 tempChar |= (B_CTRL_FWPT_1 | B_CTRL_FWRE_1);
wdenk3bac3512003-03-12 10:41:04 +000061
wdenk49822e22004-06-19 21:19:10 +000062 tempChar &= ~(B_CTRL_FWPT_0 | B_CTRL_FWRE_0);
wdenk3bac3512003-03-12 10:41:04 +000063
wdenk49822e22004-06-19 21:19:10 +000064 BOARD_CTRL = tempChar;
65
66 __asm__ volatile ("sync\n eieio");
67
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
69 vu_long *addr = (vu_long *) (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE);
wdenk49822e22004-06-19 21:19:10 +000070
71 addr[0] = 0x00900090;
72
73 __asm__ volatile ("sync\n eieio");
74
75 udelay (100);
76
77 DEBUGF ("Flash bank # %d:\n"
78 "\tManuf. ID @ 0x%08lX: 0x%08lX\n"
79 "\tDevice ID @ 0x%08lX: 0x%08lX\n",
80 i,
81 (ulong) (&addr[0]), addr[0],
82 (ulong) (&addr[2]), addr[2]);
wdenk3bac3512003-03-12 10:41:04 +000083
84
wdenk49822e22004-06-19 21:19:10 +000085 if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT) &&
86 (addr[2] == addr[3]) && (addr[2] == INTEL_ID_28F160F3T)) {
wdenk3bac3512003-03-12 10:41:04 +000087
wdenk49822e22004-06-19 21:19:10 +000088 flash_info[i].flash_id =
89 (FLASH_MAN_INTEL & FLASH_VENDMASK) |
90 (INTEL_ID_28F160F3T & FLASH_TYPEMASK);
wdenk3bac3512003-03-12 10:41:04 +000091
wdenk49822e22004-06-19 21:19:10 +000092 } else if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT)
93 && (addr[2] == addr[3])
94 && (addr[2] == INTEL_ID_28F160C3T)) {
wdenk3bac3512003-03-12 10:41:04 +000095
wdenk49822e22004-06-19 21:19:10 +000096 flash_info[i].flash_id =
97 (FLASH_MAN_INTEL & FLASH_VENDMASK) |
98 (INTEL_ID_28F160C3T & FLASH_TYPEMASK);
wdenk3bac3512003-03-12 10:41:04 +000099
wdenk3bac3512003-03-12 10:41:04 +0000100 } else {
wdenk49822e22004-06-19 21:19:10 +0000101 flash_info[i].flash_id = FLASH_UNKNOWN;
102 addr[0] = 0xFFFFFFFF;
103 goto Done;
wdenk3bac3512003-03-12 10:41:04 +0000104 }
wdenk3bac3512003-03-12 10:41:04 +0000105
wdenk49822e22004-06-19 21:19:10 +0000106 DEBUGF ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
107
108 addr[0] = 0xFFFFFFFF;
109
110 flash_info[i].size = FLASH_BANK_SIZE;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
112 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
wdenk49822e22004-06-19 21:19:10 +0000113 for (j = 0; j < flash_info[i].sector_count; j++) {
114 if (j > 30) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200115 flash_info[i].start[j] = CONFIG_SYS_FLASH_BASE +
wdenk49822e22004-06-19 21:19:10 +0000116 i * FLASH_BANK_SIZE +
117 (MAIN_SECT_SIZE * 31) + (j -
118 31) *
119 PARAM_SECT_SIZE;
120 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200121 flash_info[i].start[j] = CONFIG_SYS_FLASH_BASE +
wdenk49822e22004-06-19 21:19:10 +0000122 i * FLASH_BANK_SIZE +
123 j * MAIN_SECT_SIZE;
124 }
125 }
126
127 /* unlock sectors, if 160C3T */
128
129 for (j = 0; j < flash_info[i].sector_count; j++) {
130 tmpaddr = (vu_long *) flash_info[i].start[j];
131
132 if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
133 (INTEL_ID_28F160C3T & FLASH_TYPEMASK)) {
134 tmpaddr[0] = 0x00600060;
135 tmpaddr[0] = 0x00D000D0;
136 tmpaddr[1] = 0x00600060;
137 tmpaddr[1] = 0x00D000D0;
138 }
139 }
140
141 size += flash_info[i].size;
142
143 addr[0] = 0x00FF00FF;
144 addr[1] = 0x00FF00FF;
145 }
146
147 /* Protect monitor and environment sectors
148 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200149#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE + FLASH_BANK_SIZE
wdenk49822e22004-06-19 21:19:10 +0000150 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200151 CONFIG_SYS_MONITOR_BASE,
152 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
wdenk49822e22004-06-19 21:19:10 +0000153 &flash_info[1]);
wdenk3bac3512003-03-12 10:41:04 +0000154#else
wdenk49822e22004-06-19 21:19:10 +0000155 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200156 CONFIG_SYS_MONITOR_BASE,
157 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
wdenk49822e22004-06-19 21:19:10 +0000158 &flash_info[0]);
wdenk3bac3512003-03-12 10:41:04 +0000159#endif
160
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200161#if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200162#if CONFIG_ENV_ADDR >= CONFIG_SYS_FLASH_BASE + FLASH_BANK_SIZE
wdenk49822e22004-06-19 21:19:10 +0000163 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200164 CONFIG_ENV_ADDR,
165 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[1]);
wdenk3bac3512003-03-12 10:41:04 +0000166#else
wdenk49822e22004-06-19 21:19:10 +0000167 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200168 CONFIG_ENV_ADDR,
169 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
wdenk3bac3512003-03-12 10:41:04 +0000170#endif
171#endif
172
173Done:
wdenk49822e22004-06-19 21:19:10 +0000174 return size;
wdenk3bac3512003-03-12 10:41:04 +0000175}
176
177/*-----------------------------------------------------------------------
178 */
179void flash_print_info (flash_info_t * info)
180{
181 int i;
182
183 switch ((i = info->flash_id & FLASH_VENDMASK)) {
184 case (FLASH_MAN_INTEL & FLASH_VENDMASK):
185 printf ("Intel: ");
186 break;
187 default:
188 printf ("Unknown Vendor 0x%04x ", i);
189 break;
190 }
191
192 switch ((i = info->flash_id & FLASH_TYPEMASK)) {
193 case (INTEL_ID_28F160F3T & FLASH_TYPEMASK):
194 printf ("28F160F3T (16Mbit)\n");
195 break;
wdenk49822e22004-06-19 21:19:10 +0000196
197 case (INTEL_ID_28F160C3T & FLASH_TYPEMASK):
198 printf ("28F160C3T (16Mbit)\n");
199 break;
200
wdenk3bac3512003-03-12 10:41:04 +0000201 default:
202 printf ("Unknown Chip Type 0x%04x\n", i);
203 goto Done;
204 break;
205 }
206
207 printf (" Size: %ld MB in %d Sectors\n",
wdenk49822e22004-06-19 21:19:10 +0000208 info->size >> 20, info->sector_count);
wdenk3bac3512003-03-12 10:41:04 +0000209
210 printf (" Sector Start Addresses:");
211 for (i = 0; i < info->sector_count; i++) {
212 if ((i % 5) == 0) {
213 printf ("\n ");
214 }
215 printf (" %08lX%s", info->start[i],
wdenk49822e22004-06-19 21:19:10 +0000216 info->protect[i] ? " (RO)" : " ");
wdenk3bac3512003-03-12 10:41:04 +0000217 }
218 printf ("\n");
219
220Done:
221 return;
222}
223
224/*-----------------------------------------------------------------------
225 */
226
wdenk49822e22004-06-19 21:19:10 +0000227int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenk3bac3512003-03-12 10:41:04 +0000228{
229 int flag, prot, sect;
230 ulong start, now, last;
231
232 DEBUGF ("Erase flash bank %d sect %d ... %d\n",
233 info - &flash_info[0], s_first, s_last);
234
235 if ((s_first < 0) || (s_first > s_last)) {
236 if (info->flash_id == FLASH_UNKNOWN) {
237 printf ("- missing\n");
238 } else {
239 printf ("- no sectors to erase\n");
240 }
241 return 1;
242 }
243
244 if ((info->flash_id & FLASH_VENDMASK) !=
245 (FLASH_MAN_INTEL & FLASH_VENDMASK)) {
246 printf ("Can erase only Intel flash types - aborted\n");
247 return 1;
248 }
249
250 prot = 0;
wdenk49822e22004-06-19 21:19:10 +0000251 for (sect = s_first; sect <= s_last; ++sect) {
wdenk3bac3512003-03-12 10:41:04 +0000252 if (info->protect[sect]) {
253 prot++;
254 }
255 }
256
257 if (prot) {
wdenk49822e22004-06-19 21:19:10 +0000258 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
wdenk3bac3512003-03-12 10:41:04 +0000259 } else {
260 printf ("\n");
261 }
262
263 start = get_timer (0);
wdenk49822e22004-06-19 21:19:10 +0000264 last = start;
wdenk3bac3512003-03-12 10:41:04 +0000265 /* Start erase on unprotected sectors */
wdenk49822e22004-06-19 21:19:10 +0000266 for (sect = s_first; sect <= s_last; sect++) {
wdenk3bac3512003-03-12 10:41:04 +0000267 if (info->protect[sect] == 0) { /* not protected */
wdenk49822e22004-06-19 21:19:10 +0000268 vu_long *addr = (vu_long *) (info->start[sect]);
wdenk3bac3512003-03-12 10:41:04 +0000269
270 DEBUGF ("Erase sect %d @ 0x%08lX\n",
wdenk49822e22004-06-19 21:19:10 +0000271 sect, (ulong) addr);
wdenk3bac3512003-03-12 10:41:04 +0000272
273 /* Disable interrupts which might cause a timeout
274 * here.
275 */
wdenk49822e22004-06-19 21:19:10 +0000276 flag = disable_interrupts ();
wdenk3bac3512003-03-12 10:41:04 +0000277
278 addr[0] = 0x00500050; /* clear status register */
279 addr[0] = 0x00200020; /* erase setup */
280 addr[0] = 0x00D000D0; /* erase confirm */
281
282 addr[1] = 0x00500050; /* clear status register */
283 addr[1] = 0x00200020; /* erase setup */
284 addr[1] = 0x00D000D0; /* erase confirm */
285
286 /* re-enable interrupts if necessary */
287 if (flag)
wdenk49822e22004-06-19 21:19:10 +0000288 enable_interrupts ();
wdenk3bac3512003-03-12 10:41:04 +0000289
290 /* wait at least 80us - let's wait 1 ms */
291 udelay (1000);
292
293 while (((addr[0] & 0x00800080) != 0x00800080) ||
wdenk49822e22004-06-19 21:19:10 +0000294 ((addr[1] & 0x00800080) != 0x00800080)) {
295 if ((now = get_timer (start)) >
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200296 CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk3bac3512003-03-12 10:41:04 +0000297 printf ("Timeout\n");
wdenk49822e22004-06-19 21:19:10 +0000298 addr[0] = 0x00B000B0; /* suspend erase */
299 addr[0] = 0x00FF00FF; /* to read mode */
wdenk3bac3512003-03-12 10:41:04 +0000300 return 1;
301 }
302
303 /* show that we're waiting */
wdenk49822e22004-06-19 21:19:10 +0000304 if ((now - last) > 1000) { /* every second */
wdenk3bac3512003-03-12 10:41:04 +0000305 putc ('.');
306 last = now;
307 }
308 }
309
310 addr[0] = 0x00FF00FF;
311 }
312 }
313 printf (" done\n");
314 return 0;
315}
316
317/*-----------------------------------------------------------------------
318 * Copy memory to flash, returns:
319 * 0 - OK
320 * 1 - write timeout
321 * 2 - Flash not erased
322 * 4 - Flash not identified
323 */
324
325#define FLASH_WIDTH 8 /* flash bus width in bytes */
326
wdenk49822e22004-06-19 21:19:10 +0000327int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenk3bac3512003-03-12 10:41:04 +0000328{
329 ulong wp, cp, msr;
330 int l, rc, i;
331 ulong data[2];
332 ulong *datah = &data[0];
333 ulong *datal = &data[1];
334
335 DEBUGF ("Flash write_buff: @ 0x%08lx, src 0x%08lx len %ld\n",
wdenk49822e22004-06-19 21:19:10 +0000336 addr, (ulong) src, cnt);
wdenk3bac3512003-03-12 10:41:04 +0000337
338 if (info->flash_id == FLASH_UNKNOWN) {
339 return 4;
340 }
341
wdenk49822e22004-06-19 21:19:10 +0000342 msr = get_msr ();
343 set_msr (msr | MSR_FP);
wdenk3bac3512003-03-12 10:41:04 +0000344
wdenk49822e22004-06-19 21:19:10 +0000345 wp = (addr & ~(FLASH_WIDTH - 1)); /* get lower aligned address */
wdenk3bac3512003-03-12 10:41:04 +0000346
347 /*
348 * handle unaligned start bytes
349 */
350 if ((l = addr - wp) != 0) {
351 *datah = *datal = 0;
352
353 for (i = 0, cp = wp; i < l; i++, cp++) {
354 if (i >= 4) {
355 *datah = (*datah << 8) |
wdenk49822e22004-06-19 21:19:10 +0000356 ((*datal & 0xFF000000) >> 24);
wdenk3bac3512003-03-12 10:41:04 +0000357 }
358
wdenk49822e22004-06-19 21:19:10 +0000359 *datal = (*datal << 8) | (*(uchar *) cp);
wdenk3bac3512003-03-12 10:41:04 +0000360 }
361 for (; i < FLASH_WIDTH && cnt > 0; ++i) {
wdenk49822e22004-06-19 21:19:10 +0000362 char tmp = *src++;
wdenk3bac3512003-03-12 10:41:04 +0000363
364 if (i >= 4) {
365 *datah = (*datah << 8) |
wdenk49822e22004-06-19 21:19:10 +0000366 ((*datal & 0xFF000000) >> 24);
wdenk3bac3512003-03-12 10:41:04 +0000367 }
368
369 *datal = (*datal << 8) | tmp;
wdenk49822e22004-06-19 21:19:10 +0000370 --cnt;
371 ++cp;
wdenk3bac3512003-03-12 10:41:04 +0000372 }
373
374 for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp) {
375 if (i >= 4) {
376 *datah = (*datah << 8) |
wdenk49822e22004-06-19 21:19:10 +0000377 ((*datal & 0xFF000000) >> 24);
wdenk3bac3512003-03-12 10:41:04 +0000378 }
379
wdenk49822e22004-06-19 21:19:10 +0000380 *datal = (*datah << 8) | (*(uchar *) cp);
wdenk3bac3512003-03-12 10:41:04 +0000381 }
382
wdenk49822e22004-06-19 21:19:10 +0000383 if ((rc = write_data (info, wp, data)) != 0) {
384 set_msr (msr);
wdenk3bac3512003-03-12 10:41:04 +0000385 return (rc);
386 }
387
388 wp += FLASH_WIDTH;
389 }
390
391 /*
392 * handle FLASH_WIDTH aligned part
393 */
394 while (cnt >= FLASH_WIDTH) {
wdenk49822e22004-06-19 21:19:10 +0000395 *datah = *(ulong *) src;
396 *datal = *(ulong *) (src + 4);
397 if ((rc = write_data (info, wp, data)) != 0) {
398 set_msr (msr);
wdenk3bac3512003-03-12 10:41:04 +0000399 return (rc);
400 }
wdenk49822e22004-06-19 21:19:10 +0000401 wp += FLASH_WIDTH;
wdenk3bac3512003-03-12 10:41:04 +0000402 cnt -= FLASH_WIDTH;
403 src += FLASH_WIDTH;
404 }
405
406 if (cnt == 0) {
wdenk49822e22004-06-19 21:19:10 +0000407 set_msr (msr);
wdenk3bac3512003-03-12 10:41:04 +0000408 return (0);
409 }
410
411 /*
412 * handle unaligned tail bytes
413 */
414 *datah = *datal = 0;
415 for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) {
wdenk49822e22004-06-19 21:19:10 +0000416 char tmp = *src++;
wdenk3bac3512003-03-12 10:41:04 +0000417
418 if (i >= 4) {
wdenk49822e22004-06-19 21:19:10 +0000419 *datah = (*datah << 8) | ((*datal & 0xFF000000) >>
420 24);
wdenk3bac3512003-03-12 10:41:04 +0000421 }
422
423 *datal = (*datal << 8) | tmp;
wdenk3bac3512003-03-12 10:41:04 +0000424 --cnt;
425 }
426
427 for (; i < FLASH_WIDTH; ++i, ++cp) {
428 if (i >= 4) {
wdenk49822e22004-06-19 21:19:10 +0000429 *datah = (*datah << 8) | ((*datal & 0xFF000000) >>
430 24);
wdenk3bac3512003-03-12 10:41:04 +0000431 }
432
wdenk49822e22004-06-19 21:19:10 +0000433 *datal = (*datal << 8) | (*(uchar *) cp);
wdenk3bac3512003-03-12 10:41:04 +0000434 }
435
wdenk49822e22004-06-19 21:19:10 +0000436 rc = write_data (info, wp, data);
437 set_msr (msr);
wdenk3bac3512003-03-12 10:41:04 +0000438
439 return (rc);
440}
441
442/*-----------------------------------------------------------------------
443 * Write a word to Flash, returns:
444 * 0 - OK
445 * 1 - write timeout
446 * 2 - Flash not erased
447 */
wdenk49822e22004-06-19 21:19:10 +0000448static int write_data (flash_info_t * info, ulong dest, ulong * data)
wdenk3bac3512003-03-12 10:41:04 +0000449{
wdenk49822e22004-06-19 21:19:10 +0000450 vu_long *addr = (vu_long *) dest;
wdenk3bac3512003-03-12 10:41:04 +0000451 ulong start;
452 int flag;
453
454 /* Check if Flash is (sufficiently) erased */
455 if (((addr[0] & data[0]) != data[0]) ||
wdenk49822e22004-06-19 21:19:10 +0000456 ((addr[1] & data[1]) != data[1])) {
wdenk3bac3512003-03-12 10:41:04 +0000457 return (2);
458 }
459 /* Disable interrupts which might cause a timeout here */
wdenk49822e22004-06-19 21:19:10 +0000460 flag = disable_interrupts ();
wdenk3bac3512003-03-12 10:41:04 +0000461
wdenk49822e22004-06-19 21:19:10 +0000462 addr[0] = 0x00400040; /* write setup */
463 write_via_fpu (addr, data);
wdenk3bac3512003-03-12 10:41:04 +0000464
465 /* re-enable interrupts if necessary */
466 if (flag)
wdenk49822e22004-06-19 21:19:10 +0000467 enable_interrupts ();
wdenk3bac3512003-03-12 10:41:04 +0000468
469 start = get_timer (0);
470
471 while (((addr[0] & 0x00800080) != 0x00800080) ||
wdenk49822e22004-06-19 21:19:10 +0000472 ((addr[1] & 0x00800080) != 0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200473 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk3bac3512003-03-12 10:41:04 +0000474 addr[0] = 0x00FF00FF; /* restore read mode */
475 return (1);
476 }
477 }
478
479 addr[0] = 0x00FF00FF; /* restore read mode */
480
481 return (0);
482}
483
484/*-----------------------------------------------------------------------
485 */
wdenk49822e22004-06-19 21:19:10 +0000486static void write_via_fpu (vu_long * addr, ulong * data)
wdenk3bac3512003-03-12 10:41:04 +0000487{
wdenk49822e22004-06-19 21:19:10 +0000488 __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
489 __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
wdenk3bac3512003-03-12 10:41:04 +0000490}
wdenk49822e22004-06-19 21:19:10 +0000491
wdenk3bac3512003-03-12 10:41:04 +0000492/*-----------------------------------------------------------------------
493 */
wdenk49822e22004-06-19 21:19:10 +0000494static __inline__ unsigned long get_msr (void)
wdenk3bac3512003-03-12 10:41:04 +0000495{
wdenk49822e22004-06-19 21:19:10 +0000496 unsigned long msr;
wdenk3bac3512003-03-12 10:41:04 +0000497
wdenk49822e22004-06-19 21:19:10 +0000498 __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
499
500 return msr;
wdenk3bac3512003-03-12 10:41:04 +0000501}
502
wdenk49822e22004-06-19 21:19:10 +0000503static __inline__ void set_msr (unsigned long msr)
wdenk3bac3512003-03-12 10:41:04 +0000504{
wdenk49822e22004-06-19 21:19:10 +0000505 __asm__ __volatile__ ("mtmsr %0"::"r" (msr));
wdenk3bac3512003-03-12 10:41:04 +0000506}