blob: 1dfcb4151316034b1e4a49731a3ff27541ca2474 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2002
6 * Gregory E. Allen, gallen@arlut.utexas.edu
7 * Matthew E. Karger, karger@arlut.utexas.edu
8 * Applied Research Laboratories, The University of Texas at Austin
9 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +000011 */
12
13#include <common.h>
14#include <mpc824x.h>
15#include <asm/processor.h>
16
17#define ROM_CS0_START 0xFF800000
18#define ROM_CS1_START 0xFF000000
19
Jean-Christophe PLAGNIOL-VILLARD53db4cd2008-09-10 22:48:04 +020020#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020021# ifndef CONFIG_ENV_ADDR
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020022# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
wdenkfe8c2802002-11-03 00:38:21 +000023# endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020024# ifndef CONFIG_ENV_SIZE
25# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
wdenkfe8c2802002-11-03 00:38:21 +000026# endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020027# ifndef CONFIG_ENV_SECT_SIZE
28# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
wdenkfe8c2802002-11-03 00:38:21 +000029# endif
30#endif
31
wdenkabda5ca2003-05-31 18:35:21 +000032#define FLASH_BANK_SIZE ((uint)(16 * 1024 * 1024)) /* max 16Mbyte */
Wolfgang Denka1be4762008-05-20 16:00:29 +020033#define MAIN_SECT_SIZE 0x10000
wdenkfe8c2802002-11-03 00:38:21 +000034#define SECT_SIZE_32KB 0x8000
35#define SECT_SIZE_8KB 0x2000
36
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020037flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkfe8c2802002-11-03 00:38:21 +000038
wdenkabda5ca2003-05-31 18:35:21 +000039static int write_word (flash_info_t * info, ulong dest, ulong data);
40#if 0
41static void write_via_fpu (vu_long * addr, ulong * data);
42#endif
43static __inline__ unsigned long get_msr (void);
44static __inline__ void set_msr (unsigned long msr);
wdenkfe8c2802002-11-03 00:38:21 +000045
46/*flash command address offsets*/
47#define ADDR0 (0x555)
48#define ADDR1 (0xAAA)
49#define ADDR3 (0x001)
50
51#define FLASH_WORD_SIZE unsigned char
52
53/*---------------------------------------------------------------------*/
54/*#define DEBUG_FLASH 1 */
55
56/*---------------------------------------------------------------------*/
57
wdenkabda5ca2003-05-31 18:35:21 +000058unsigned long flash_init (void)
wdenkfe8c2802002-11-03 00:38:21 +000059{
wdenkabda5ca2003-05-31 18:35:21 +000060 int i; /* flash bank counter */
61 int j; /* flash device sector counter */
62 int k; /* flash size calculation loop counter */
63 int N; /* pow(2,N) is flash size, but we don't have <math.h> */
64 ulong total_size = 0, device_size = 1;
wdenkfe8c2802002-11-03 00:38:21 +000065 unsigned char manuf_id, device_id;
66
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020067 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
68 vu_char *addr = (vu_char *) (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE);
wdenkfe8c2802002-11-03 00:38:21 +000069
wdenkabda5ca2003-05-31 18:35:21 +000070 addr[0x555] = 0xAA; /* get manuf/device info command */
71 addr[0x2AA] = 0x55; /* 3-cycle command */
72 addr[0x555] = 0x90;
wdenkfe8c2802002-11-03 00:38:21 +000073
wdenkabda5ca2003-05-31 18:35:21 +000074 manuf_id = addr[0]; /* read back manuf/device info */
75 device_id = addr[1];
wdenkfe8c2802002-11-03 00:38:21 +000076
wdenkabda5ca2003-05-31 18:35:21 +000077 addr[0x55] = 0x98; /* CFI command */
78 N = addr[0x27]; /* read back device_size = pow(2,N) */
wdenkfe8c2802002-11-03 00:38:21 +000079
wdenkabda5ca2003-05-31 18:35:21 +000080 for (k = 0; k < N; k++) /* calculate device_size = pow(2,N) */
81 device_size *= 2;
82
83 flash_info[i].size = device_size;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020084 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenkfe8c2802002-11-03 00:38:21 +000085
86#if defined DEBUG_FLASH
wdenkabda5ca2003-05-31 18:35:21 +000087 printf ("manuf_id = %x, device_id = %x\n", manuf_id, device_id);
wdenkfe8c2802002-11-03 00:38:21 +000088#endif
wdenkabda5ca2003-05-31 18:35:21 +000089 /* find out what kind of flash we are using */
90 if ((manuf_id == (uchar) (AMD_MANUFACT))
91 && (device_id == AMD_ID_LV033C)) {
92 flash_info[i].flash_id =
93 ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) |
94 (FLASH_AM033C & FLASH_TYPEMASK);
wdenkfe8c2802002-11-03 00:38:21 +000095
wdenkabda5ca2003-05-31 18:35:21 +000096 /* set individual sector start addresses */
97 for (j = 0; j < flash_info[i].sector_count; j++) {
98 flash_info[i].start[j] =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099 (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE +
wdenkabda5ca2003-05-31 18:35:21 +0000100 j * MAIN_SECT_SIZE);
101 }
102 }
wdenkfe8c2802002-11-03 00:38:21 +0000103
wdenkabda5ca2003-05-31 18:35:21 +0000104 else if ((manuf_id == (uchar) (AMD_MANUFACT)) &&
105 (device_id == AMD_ID_LV116DT)) {
106 flash_info[i].flash_id =
107 ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) |
108 (FLASH_AM160T & FLASH_TYPEMASK);
wdenkfe8c2802002-11-03 00:38:21 +0000109
wdenkabda5ca2003-05-31 18:35:21 +0000110 /* set individual sector start addresses */
111 for (j = 0; j < flash_info[i].sector_count; j++) {
112 flash_info[i].start[j] =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200113 (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE +
wdenkabda5ca2003-05-31 18:35:21 +0000114 j * MAIN_SECT_SIZE);
wdenkfe8c2802002-11-03 00:38:21 +0000115
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200116 if (j < (CONFIG_SYS_MAX_FLASH_SECT - 3)) {
wdenkabda5ca2003-05-31 18:35:21 +0000117 flash_info[i].start[j] =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200118 (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE +
wdenkabda5ca2003-05-31 18:35:21 +0000119 j * MAIN_SECT_SIZE);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200120 } else if (j == (CONFIG_SYS_MAX_FLASH_SECT - 3)) {
wdenkabda5ca2003-05-31 18:35:21 +0000121 flash_info[i].start[j] =
122 (flash_info[i].start[j - 1] + SECT_SIZE_32KB);
wdenkfe8c2802002-11-03 00:38:21 +0000123
wdenkabda5ca2003-05-31 18:35:21 +0000124 } else {
125 flash_info[i].start[j] =
126 (flash_info[i].start[j - 1] + SECT_SIZE_8KB);
127 }
128 }
129 }
wdenkfe8c2802002-11-03 00:38:21 +0000130
wdenkabda5ca2003-05-31 18:35:21 +0000131 else {
132 flash_info[i].flash_id = FLASH_UNKNOWN;
133 addr[0] = 0xFF;
134 goto Done;
135 }
wdenkfe8c2802002-11-03 00:38:21 +0000136
wdenkabda5ca2003-05-31 18:35:21 +0000137#if defined DEBUG_FLASH
138 printf ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
139#endif
wdenkfe8c2802002-11-03 00:38:21 +0000140
wdenkabda5ca2003-05-31 18:35:21 +0000141 addr[0] = 0xFF;
wdenkfe8c2802002-11-03 00:38:21 +0000142
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200143 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
wdenkfe8c2802002-11-03 00:38:21 +0000144
wdenkabda5ca2003-05-31 18:35:21 +0000145 total_size += flash_info[i].size;
146 }
wdenkfe8c2802002-11-03 00:38:21 +0000147
wdenkabda5ca2003-05-31 18:35:21 +0000148 /* Protect monitor and environment sectors
149 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200150#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
151 flash_protect (FLAG_PROTECT_SET, CONFIG_SYS_MONITOR_BASE,
152 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
wdenkabda5ca2003-05-31 18:35:21 +0000153 &flash_info[0]);
wdenkfe8c2802002-11-03 00:38:21 +0000154#endif
155
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200156#if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
157 flash_protect (FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
158 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
wdenkfe8c2802002-11-03 00:38:21 +0000159#endif
160
wdenkabda5ca2003-05-31 18:35:21 +0000161 Done:
162 return total_size;
wdenkfe8c2802002-11-03 00:38:21 +0000163}
164
165/*-----------------------------------------------------------------------
166 */
wdenkabda5ca2003-05-31 18:35:21 +0000167void flash_print_info (flash_info_t * info)
wdenkfe8c2802002-11-03 00:38:21 +0000168{
wdenkabda5ca2003-05-31 18:35:21 +0000169 static const char unk[] = "Unknown";
170 const char *mfct = unk, *type = unk;
171 unsigned int i;
wdenkfe8c2802002-11-03 00:38:21 +0000172
wdenkabda5ca2003-05-31 18:35:21 +0000173 if (info->flash_id != FLASH_UNKNOWN) {
174 switch (info->flash_id & FLASH_VENDMASK) {
175 case FLASH_MAN_AMD:
176 mfct = "AMD";
177 break;
178 case FLASH_MAN_FUJ:
179 mfct = "FUJITSU";
180 break;
181 case FLASH_MAN_STM:
182 mfct = "STM";
183 break;
184 case FLASH_MAN_SST:
185 mfct = "SST";
186 break;
187 case FLASH_MAN_BM:
188 mfct = "Bright Microelectonics";
189 break;
190 case FLASH_MAN_INTEL:
191 mfct = "Intel";
192 break;
193 }
wdenkfe8c2802002-11-03 00:38:21 +0000194
wdenkabda5ca2003-05-31 18:35:21 +0000195 switch (info->flash_id & FLASH_TYPEMASK) {
196 case FLASH_AM033C:
197 type = "AM29LV033C (32 Mbit, uniform sector size)";
198 break;
199 case FLASH_AM160T:
200 type = "AM29LV160T (16 Mbit, top boot sector)";
201 break;
202 case FLASH_AM040:
203 type = "AM29F040B (512K * 8, uniform sector size)";
204 break;
205 case FLASH_AM400B:
206 type = "AM29LV400B (4 Mbit, bottom boot sect)";
207 break;
208 case FLASH_AM400T:
209 type = "AM29LV400T (4 Mbit, top boot sector)";
210 break;
211 case FLASH_AM800B:
212 type = "AM29LV800B (8 Mbit, bottom boot sect)";
213 break;
214 case FLASH_AM800T:
215 type = "AM29LV800T (8 Mbit, top boot sector)";
216 break;
217 case FLASH_AM320B:
218 type = "AM29LV320B (32 Mbit, bottom boot sect)";
219 break;
220 case FLASH_AM320T:
221 type = "AM29LV320T (32 Mbit, top boot sector)";
222 break;
223 case FLASH_STM800AB:
224 type = "M29W800AB (8 Mbit, bottom boot sect)";
225 break;
226 case FLASH_SST800A:
227 type = "SST39LF/VF800 (8 Mbit, uniform sector size)";
228 break;
229 case FLASH_SST160A:
230 type = "SST39LF/VF160 (16 Mbit, uniform sector size)";
231 break;
232 }
233 }
wdenkfe8c2802002-11-03 00:38:21 +0000234
wdenkabda5ca2003-05-31 18:35:21 +0000235 printf ("\n Brand: %s Type: %s\n"
236 " Size: %lu KB in %d Sectors\n",
237 mfct, type, info->size >> 10, info->sector_count);
wdenkfe8c2802002-11-03 00:38:21 +0000238
wdenkabda5ca2003-05-31 18:35:21 +0000239 printf (" Sector Start Addresses:");
wdenkfe8c2802002-11-03 00:38:21 +0000240
wdenkabda5ca2003-05-31 18:35:21 +0000241 for (i = 0; i < info->sector_count; i++) {
242 unsigned long size;
243 unsigned int erased;
244 unsigned long *flash = (unsigned long *) info->start[i];
wdenkfe8c2802002-11-03 00:38:21 +0000245
wdenkabda5ca2003-05-31 18:35:21 +0000246 /*
247 * Check if whole sector is erased
248 */
249 size = (i != (info->sector_count - 1)) ?
250 (info->start[i + 1] - info->start[i]) >> 2 :
251 (info->start[0] + info->size - info->start[i]) >> 2;
wdenkfe8c2802002-11-03 00:38:21 +0000252
wdenkabda5ca2003-05-31 18:35:21 +0000253 for (flash = (unsigned long *) info->start[i], erased = 1;
254 (flash != (unsigned long *) info->start[i] + size) && erased;
255 flash++)
256 erased = *flash == ~0x0UL;
wdenkfe8c2802002-11-03 00:38:21 +0000257
wdenkabda5ca2003-05-31 18:35:21 +0000258 printf ("%s %08lX %s %s",
259 (i % 5) ? "" : "\n ",
260 info->start[i],
261 erased ? "E" : " ", info->protect[i] ? "RO" : " ");
262 }
wdenkfe8c2802002-11-03 00:38:21 +0000263
wdenkabda5ca2003-05-31 18:35:21 +0000264 puts ("\n");
265 return;
wdenkfe8c2802002-11-03 00:38:21 +0000266}
267
268/*-----------------------------------------------------------------------
269 */
270
wdenkabda5ca2003-05-31 18:35:21 +0000271int flash_erase (flash_info_t * info, int s_first, int s_last)
wdenkfe8c2802002-11-03 00:38:21 +0000272{
wdenkabda5ca2003-05-31 18:35:21 +0000273 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
274 int flag, prot, sect, l_sect;
275 ulong start, now, last;
276 unsigned char sh8b;
wdenkfe8c2802002-11-03 00:38:21 +0000277
wdenkabda5ca2003-05-31 18:35:21 +0000278 if ((s_first < 0) || (s_first > s_last)) {
279 if (info->flash_id == FLASH_UNKNOWN) {
280 printf ("- missing\n");
281 } else {
282 printf ("- no sectors to erase\n");
283 }
284 return 1;
285 }
wdenkfe8c2802002-11-03 00:38:21 +0000286
wdenkabda5ca2003-05-31 18:35:21 +0000287 if ((info->flash_id == FLASH_UNKNOWN) ||
288 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
289 printf ("Can't erase unknown flash type - aborted\n");
290 return 1;
291 }
wdenkfe8c2802002-11-03 00:38:21 +0000292
wdenkabda5ca2003-05-31 18:35:21 +0000293 prot = 0;
294 for (sect = s_first; sect <= s_last; ++sect) {
295 if (info->protect[sect]) {
296 prot++;
297 }
298 }
wdenkfe8c2802002-11-03 00:38:21 +0000299
wdenkabda5ca2003-05-31 18:35:21 +0000300 if (prot) {
301 printf ("- Warning: %d protected sectors will not be erased!\n",
302 prot);
303 } else {
304 printf ("\n");
305 }
wdenkfe8c2802002-11-03 00:38:21 +0000306
wdenkabda5ca2003-05-31 18:35:21 +0000307 l_sect = -1;
wdenkfe8c2802002-11-03 00:38:21 +0000308
wdenkabda5ca2003-05-31 18:35:21 +0000309 /* Check the ROM CS */
310 if ((info->start[0] >= ROM_CS1_START)
311 && (info->start[0] < ROM_CS0_START))
312 sh8b = 3;
313 else
314 sh8b = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000315
316 /* Disable interrupts which might cause a timeout here */
wdenkabda5ca2003-05-31 18:35:21 +0000317 flag = disable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +0000318
wdenkabda5ca2003-05-31 18:35:21 +0000319 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
320 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
321 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
322 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
323 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
wdenkfe8c2802002-11-03 00:38:21 +0000324
wdenkabda5ca2003-05-31 18:35:21 +0000325 /* Start erase on unprotected sectors */
326 for (sect = s_first; sect <= s_last; sect++) {
327 if (info->protect[sect] == 0) { /* not protected */
328 addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->
329 start[sect] -
330 info->
331 start[0]) <<
332 sh8b));
wdenkfe8c2802002-11-03 00:38:21 +0000333
wdenkabda5ca2003-05-31 18:35:21 +0000334 if (info->flash_id & FLASH_MAN_SST) {
335 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
336 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
337 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
338 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
339 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
340 addr[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
341 udelay (30000); /* wait 30 ms */
342 } else {
343 addr[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
wdenkfe8c2802002-11-03 00:38:21 +0000344 }
345
wdenkfe8c2802002-11-03 00:38:21 +0000346 l_sect = sect;
wdenkabda5ca2003-05-31 18:35:21 +0000347 }
348 }
wdenkfe8c2802002-11-03 00:38:21 +0000349
wdenkabda5ca2003-05-31 18:35:21 +0000350 /* re-enable interrupts if necessary */
351 if (flag)
352 enable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +0000353
wdenkabda5ca2003-05-31 18:35:21 +0000354 /* wait at least 80us - let's wait 1 ms */
355 udelay (1000);
wdenkfe8c2802002-11-03 00:38:21 +0000356
wdenkabda5ca2003-05-31 18:35:21 +0000357 /*
358 * We wait for the last triggered sector
359 */
360 if (l_sect < 0)
361 goto DONE;
wdenkfe8c2802002-11-03 00:38:21 +0000362
wdenkabda5ca2003-05-31 18:35:21 +0000363 start = get_timer (0);
364 last = start;
365 addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->start[l_sect] -
366 info->
367 start[0]) << sh8b));
368 while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
369 (FLASH_WORD_SIZE) 0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200370 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkabda5ca2003-05-31 18:35:21 +0000371 printf ("Timeout\n");
372 return 1;
373 }
374 /* show that we're waiting */
375 if ((now - last) > 1000) { /* every second */
376 serial_putc ('.');
377 last = now;
378 }
379 }
wdenkfe8c2802002-11-03 00:38:21 +0000380
wdenkabda5ca2003-05-31 18:35:21 +0000381 DONE:
382 /* reset to read mode */
383 addr = (FLASH_WORD_SIZE *) info->start[0];
384 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
wdenkfe8c2802002-11-03 00:38:21 +0000385
wdenkabda5ca2003-05-31 18:35:21 +0000386 printf (" done\n");
387 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000388}
389
390
391/*-----------------------------------------------------------------------
392 * Copy memory to flash, returns:
393 * 0 - OK
394 * 1 - write timeout
395 * 2 - Flash not erased
396 */
397
wdenkabda5ca2003-05-31 18:35:21 +0000398int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
wdenkfe8c2802002-11-03 00:38:21 +0000399{
wdenkabda5ca2003-05-31 18:35:21 +0000400 ulong cp, wp, data;
401 int i, l, rc;
wdenkfe8c2802002-11-03 00:38:21 +0000402
wdenkabda5ca2003-05-31 18:35:21 +0000403 wp = (addr & ~3); /* get lower word aligned address */
wdenkfe8c2802002-11-03 00:38:21 +0000404
wdenkabda5ca2003-05-31 18:35:21 +0000405 /*
406 * handle unaligned start bytes
407 */
408 if ((l = addr - wp) != 0) {
409 data = 0;
410 for (i = 0, cp = wp; i < l; ++i, ++cp) {
411 data = (data << 8) | (*(uchar *) cp);
412 }
413 for (; i < 4 && cnt > 0; ++i) {
414 data = (data << 8) | *src++;
415 --cnt;
416 ++cp;
417 }
418 for (; cnt == 0 && i < 4; ++i, ++cp) {
419 data = (data << 8) | (*(uchar *) cp);
420 }
wdenkfe8c2802002-11-03 00:38:21 +0000421
wdenkabda5ca2003-05-31 18:35:21 +0000422 if ((rc = write_word (info, wp, data)) != 0) {
423 return (rc);
424 }
425 wp += 4;
426 }
wdenkfe8c2802002-11-03 00:38:21 +0000427
wdenkabda5ca2003-05-31 18:35:21 +0000428 /*
429 * handle word aligned part
430 */
431 while (cnt >= 4) {
432 data = 0;
433 for (i = 0; i < 4; ++i) {
434 data = (data << 8) | *src++;
435 }
436 if ((rc = write_word (info, wp, data)) != 0) {
437 return (rc);
438 }
439 wp += 4;
440 cnt -= 4;
441 }
wdenkfe8c2802002-11-03 00:38:21 +0000442
wdenkabda5ca2003-05-31 18:35:21 +0000443 if (cnt == 0) {
444 return (0);
445 }
wdenkfe8c2802002-11-03 00:38:21 +0000446
wdenkabda5ca2003-05-31 18:35:21 +0000447 /*
448 * handle unaligned tail bytes
449 */
450 data = 0;
451 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
452 data = (data << 8) | *src++;
453 --cnt;
454 }
455 for (; i < 4; ++i, ++cp) {
456 data = (data << 8) | (*(uchar *) cp);
457 }
wdenkfe8c2802002-11-03 00:38:21 +0000458
wdenkabda5ca2003-05-31 18:35:21 +0000459 return (write_word (info, wp, data));
wdenkfe8c2802002-11-03 00:38:21 +0000460}
461
462
463/*-----------------------------------------------------------------------
464 * Write a word to Flash, returns:
465 * 0 - OK
466 * 1 - write timeout
467 * 2 - Flash not erased
468 */
wdenkabda5ca2003-05-31 18:35:21 +0000469static int write_word (flash_info_t * info, ulong dest, ulong data)
wdenkfe8c2802002-11-03 00:38:21 +0000470{
wdenkabda5ca2003-05-31 18:35:21 +0000471 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) info->start[0];
wdenkfe8c2802002-11-03 00:38:21 +0000472 volatile FLASH_WORD_SIZE *dest2;
wdenkabda5ca2003-05-31 18:35:21 +0000473 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
wdenkfe8c2802002-11-03 00:38:21 +0000474 ulong start;
475 int flag;
476 int i;
477 unsigned char sh8b;
478
wdenkabda5ca2003-05-31 18:35:21 +0000479 /* Check the ROM CS */
480 if ((info->start[0] >= ROM_CS1_START)
481 && (info->start[0] < ROM_CS0_START))
482 sh8b = 3;
483 else
484 sh8b = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000485
wdenkabda5ca2003-05-31 18:35:21 +0000486 dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) +
487 info->start[0]);
wdenkfe8c2802002-11-03 00:38:21 +0000488
wdenkabda5ca2003-05-31 18:35:21 +0000489 /* Check if Flash is (sufficiently) erased */
490 if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
491 return (2);
492 }
493 /* Disable interrupts which might cause a timeout here */
494 flag = disable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +0000495
wdenkabda5ca2003-05-31 18:35:21 +0000496 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
497 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
498 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
499 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00A000A0;
wdenkfe8c2802002-11-03 00:38:21 +0000500
wdenkabda5ca2003-05-31 18:35:21 +0000501 dest2[i << sh8b] = data2[i];
wdenkfe8c2802002-11-03 00:38:21 +0000502
wdenkabda5ca2003-05-31 18:35:21 +0000503 /* re-enable interrupts if necessary */
504 if (flag)
505 enable_interrupts ();
wdenkfe8c2802002-11-03 00:38:21 +0000506
wdenkabda5ca2003-05-31 18:35:21 +0000507 /* data polling for D7 */
508 start = get_timer (0);
509 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE) 0x00800080) !=
510 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200511 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenkabda5ca2003-05-31 18:35:21 +0000512 return (1);
513 }
514 }
515 }
wdenkfe8c2802002-11-03 00:38:21 +0000516
wdenkabda5ca2003-05-31 18:35:21 +0000517 return (0);
wdenkfe8c2802002-11-03 00:38:21 +0000518}
wdenkabda5ca2003-05-31 18:35:21 +0000519
520/*-----------------------------------------------------------------------
521 */
522#if 0
523static void write_via_fpu (vu_long * addr, ulong * data)
524{
525 __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
526 __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
527}
528#endif
529
wdenkfe8c2802002-11-03 00:38:21 +0000530/*-----------------------------------------------------------------------
531 */
wdenkabda5ca2003-05-31 18:35:21 +0000532static __inline__ unsigned long get_msr (void)
533{
534 unsigned long msr;
535
536 __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
537
538 return msr;
539}
540
541static __inline__ void set_msr (unsigned long msr)
542{
543 __asm__ __volatile__ ("mtmsr %0"::"r" (msr));
544}