5 * Flash programming routines for the Wind River PPMC 74xx/7xx
6 * based on flash.c from the TQM8260 board.
8 * By Richard Danter (richard.danter@windriver.com)
9 * Copyright (C) 2005 Wind River Systems
13 #include <asm/processor.h>
16 #define DWORD unsigned long long
18 /* Local function prototypes */
19 static int write_dword (flash_info_t* info, ulong dest, unsigned char *pdata);
20 static void write_via_fpu (volatile DWORD* addr, DWORD* data);
22 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
24 /*-----------------------------------------------------------------------
26 void flash_reset (void)
29 DWORD cmd_reset = 0x00F000F000F000F0LL;
31 if (flash_info[0].flash_id != FLASH_UNKNOWN) {
33 set_msr (msr | MSR_FP);
35 write_via_fpu ((DWORD*)flash_info[0].start[0], &cmd_reset );
41 /*-----------------------------------------------------------------------
43 ulong flash_get_size (ulong baseaddr, flash_info_t * info)
48 DWORD cmd_select[3] = { 0x00AA00AA00AA00AALL, 0x0055005500550055LL,
49 0x0090009000900090LL };
53 set_msr (msr | MSR_FP);
55 /* Write auto-select command sequence */
56 write_via_fpu ((DWORD*)(baseaddr + (0x0555 << 3)), &cmd_select[0] );
57 write_via_fpu ((DWORD*)(baseaddr + (0x02AA << 3)), &cmd_select[1] );
58 write_via_fpu ((DWORD*)(baseaddr + (0x0555 << 3)), &cmd_select[2] );
63 /* Read manufacturer ID */
64 flashtest = *(volatile DWORD*)baseaddr;
65 switch ((int)flashtest) {
67 info->flash_id = FLASH_MAN_AMD;
70 info->flash_id = FLASH_MAN_FUJ;
73 /* No, faulty or unknown flash */
74 info->flash_id = FLASH_UNKNOWN;
75 info->sector_count = 0;
81 flashtest = *(volatile DWORD*)(baseaddr + 8);
82 switch ((long)flashtest) {
84 info->flash_id += FLASH_AM800T;
85 info->sector_count = 19;
86 info->size = 0x00400000;
89 info->flash_id += FLASH_AM800B;
90 info->sector_count = 19;
91 info->size = 0x00400000;
94 info->flash_id += FLASH_AM160T;
95 info->sector_count = 35;
96 info->size = 0x00800000;
99 info->flash_id += FLASH_AM160B;
100 info->sector_count = 35;
101 info->size = 0x00800000;
104 info->flash_id += FLASH_AMDL322T;
105 info->sector_count = 71;
106 info->size = 0x01000000;
109 info->flash_id += FLASH_AMDL322B;
110 info->sector_count = 71;
111 info->size = 0x01000000;
114 info->flash_id += FLASH_AMDL323T;
115 info->sector_count = 71;
116 info->size = 0x01000000;
119 info->flash_id += FLASH_AMDL323B;
120 info->sector_count = 71;
121 info->size = 0x01000000;
124 info->flash_id += FLASH_AM640U;
125 info->sector_count = 128;
126 info->size = 0x02000000;
129 /* Unknown flash type */
130 info->flash_id = FLASH_UNKNOWN;
134 if ((long)flashtest == AMD_ID_LV640U) {
135 /* set up sector start adress table (uniform sector type) */
136 for (i = 0; i < info->sector_count; i++)
137 info->start[i] = baseaddr + (i * 0x00040000);
138 } else if (info->flash_id & FLASH_BTYPE) {
139 /* set up sector start adress table (bottom sector type) */
140 info->start[0] = baseaddr + 0x00000000;
141 info->start[1] = baseaddr + 0x00010000;
142 info->start[2] = baseaddr + 0x00018000;
143 info->start[3] = baseaddr + 0x00020000;
144 for (i = 4; i < info->sector_count; i++) {
145 info->start[i] = baseaddr + (i * 0x00040000) - 0x000C0000;
148 /* set up sector start adress table (top sector type) */
149 i = info->sector_count - 1;
150 info->start[i--] = baseaddr + info->size - 0x00010000;
151 info->start[i--] = baseaddr + info->size - 0x00018000;
152 info->start[i--] = baseaddr + info->size - 0x00020000;
153 for (; i >= 0; i--) {
154 info->start[i] = baseaddr + i * 0x00040000;
158 /* check for protected sectors */
159 for (i = 0; i < info->sector_count; i++) {
160 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
161 if (*(volatile DWORD*)(info->start[i] + 16) & 0x0001000100010001LL) {
162 info->protect[i] = 1; /* D0 = 1 if protected */
164 info->protect[i] = 0;
172 /*-----------------------------------------------------------------------
174 unsigned long flash_init (void)
176 unsigned long size_b0 = 0;
179 /* Init: no FLASHes known */
180 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
181 flash_info[i].flash_id = FLASH_UNKNOWN;
184 /* Static FLASH Bank configuration here (only one bank) */
185 size_b0 = flash_get_size (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
186 if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
187 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
188 size_b0, size_b0 >> 20);
192 * protect monitor and environment sectors
194 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
195 flash_protect (FLAG_PROTECT_SET,
196 CONFIG_SYS_MONITOR_BASE,
197 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
200 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
201 # ifndef CONFIG_ENV_SIZE
202 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
204 flash_protect (FLAG_PROTECT_SET,
206 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
212 /*-----------------------------------------------------------------------
214 void flash_print_info (flash_info_t * info)
218 if (info->flash_id == FLASH_UNKNOWN) {
219 printf ("missing or unknown FLASH type\n");
223 switch (info->flash_id & FLASH_VENDMASK) {
231 printf ("Unknown Vendor ");
235 switch (info->flash_id & FLASH_TYPEMASK) {
237 printf ("29LV800T (8 M, top sector)\n");
240 printf ("29LV800T (8 M, bottom sector)\n");
243 printf ("29LV160T (16 M, top sector)\n");
246 printf ("29LV160B (16 M, bottom sector)\n");
249 printf ("29DL322T (32 M, top sector)\n");
252 printf ("29DL322B (32 M, bottom sector)\n");
255 printf ("29DL323T (32 M, top sector)\n");
258 printf ("29DL323B (32 M, bottom sector)\n");
261 printf ("29LV640D (64 M, uniform sector)\n");
264 printf ("Unknown Chip Type\n");
268 printf (" Size: %ld MB in %d Sectors\n",
269 info->size >> 20, info->sector_count);
271 printf (" Sector Start Addresses:");
272 for (i = 0; i < info->sector_count; ++i) {
277 info->protect[i] ? " (RO)" : " "
284 /*-----------------------------------------------------------------------
286 int flash_erase (flash_info_t * info, int s_first, int s_last)
288 int flag, prot, sect, l_sect;
289 ulong start, now, last;
291 DWORD cmd_erase[6] = { 0x00AA00AA00AA00AALL, 0x0055005500550055LL,
292 0x0080008000800080LL, 0x00AA00AA00AA00AALL,
293 0x0055005500550055LL, 0x0030003000300030LL };
295 if ((s_first < 0) || (s_first > s_last)) {
296 if (info->flash_id == FLASH_UNKNOWN) {
297 printf ("- missing\n");
299 printf ("- no sectors to erase\n");
305 for (sect = s_first; sect <= s_last; sect++) {
306 if (info->protect[sect])
311 printf ("- Warning: %d protected sectors will not be erased!\n",
321 set_msr ( msr | MSR_FP );
323 /* Disable interrupts which might cause a timeout here */
324 flag = disable_interrupts ();
326 write_via_fpu ((DWORD*)(info->start[0] + (0x0555 << 3)), &cmd_erase[0] );
327 write_via_fpu ((DWORD*)(info->start[0] + (0x02AA << 3)), &cmd_erase[1] );
328 write_via_fpu ((DWORD*)(info->start[0] + (0x0555 << 3)), &cmd_erase[2] );
329 write_via_fpu ((DWORD*)(info->start[0] + (0x0555 << 3)), &cmd_erase[3] );
330 write_via_fpu ((DWORD*)(info->start[0] + (0x02AA << 3)), &cmd_erase[4] );
333 /* Start erase on unprotected sectors */
334 for (sect = s_first; sect <= s_last; sect++) {
335 if (info->protect[sect] == 0) { /* not protected */
336 write_via_fpu ((DWORD*)info->start[sect], &cmd_erase[5] );
341 /* re-enable interrupts if necessary */
343 enable_interrupts ();
348 /* wait at least 80us - let's wait 1 ms */
352 * We wait for the last triggered sector
357 start = get_timer (0);
359 while ((*(volatile DWORD*)info->start[l_sect] & 0x0080008000800080LL )
360 != 0x0080008000800080LL )
362 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
363 printf ("Timeout\n");
366 /* show that we're waiting */
367 if ((now - last) > 1000) { /* every second */
374 /* reset to read mode */
382 /*-----------------------------------------------------------------------
383 * Copy memory to flash, returns:
386 * 2 - Flash not erased
389 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
392 static unsigned char bb[8];
393 int i, l, rc, cc = cnt;
395 dp = (addr & ~7); /* get lower dword aligned address */
398 * handle unaligned start bytes
400 if ((l = addr - dp) != 0) {
401 for (i = 0; i < 8; i++)
402 bb[i] = (i < l || (i - l) >= cc) ? *(char*)(dp + i) : *src++;
403 if ((rc = write_dword (info, dp, bb)) != 0) {
411 * handle word aligned part
414 if ((rc = write_dword (info, dp, src)) != 0) {
427 * handle unaligned tail bytes
429 for (i = 0; i < 8; i++) {
430 bb[i] = (i < cc) ? *src++ : *(char*)(dp + i);
432 return (write_dword (info, dp, bb));
435 /*-----------------------------------------------------------------------
436 * Write a dword to Flash, returns:
439 * 2 - Flash not erased
441 static int write_dword (flash_info_t * info, ulong dest, unsigned char *pdata)
447 DWORD cmd_write[3] = { 0x00AA00AA00AA00AALL, 0x0055005500550055LL,
448 0x00A000A000A000A0LL };
450 for (data = 0, i = 0; i < 8; i++)
451 data = (data << 8) + *pdata++;
453 /* Check if Flash is (sufficiently) erased */
454 if ((*(DWORD*)dest & data) != data) {
460 set_msr( msr | MSR_FP );
462 /* Disable interrupts which might cause a timeout here */
463 flag = disable_interrupts ();
465 write_via_fpu ((DWORD*)(info->start[0] + (0x0555 << 3)), &cmd_write[0] );
466 write_via_fpu ((DWORD*)(info->start[0] + (0x02AA << 3)), &cmd_write[1] );
467 write_via_fpu ((DWORD*)(info->start[0] + (0x0555 << 3)), &cmd_write[2] );
468 write_via_fpu ((DWORD*)dest, &data );
470 /* re-enable interrupts if necessary */
472 enable_interrupts ();
477 /* data polling for D7 */
478 start = get_timer (0);
479 while (*(volatile DWORD*)dest != data ) {
480 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
487 /*-----------------------------------------------------------------------
489 static void write_via_fpu (volatile DWORD* addr, DWORD* data)
491 __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
492 __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
493 __asm__ __volatile__ ("eieio");