2 * (C) Copyright 2001, 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Flash Routines for AMD devices on the TQM8260 board
7 *--------------------------------------------------------------------
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #define V_ULONG(a) (*(volatile unsigned long *)( a ))
31 #define V_BYTE(a) (*(volatile unsigned char *)( a ))
34 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
37 /*-----------------------------------------------------------------------
39 void flash_reset (void)
41 if (flash_info[0].flash_id != FLASH_UNKNOWN) {
42 V_ULONG (flash_info[0].start[0]) = 0x00F000F0;
43 V_ULONG (flash_info[0].start[0] + 4) = 0x00F000F0;
47 /*-----------------------------------------------------------------------
49 ulong flash_get_size (ulong baseaddr, flash_info_t * info)
52 unsigned long flashtest_h, flashtest_l;
54 /* Write auto select command sequence and test FLASH answer */
55 V_ULONG (baseaddr + ((ulong) 0x0555 << 3)) = 0x00AA00AA;
56 V_ULONG (baseaddr + ((ulong) 0x02AA << 3)) = 0x00550055;
57 V_ULONG (baseaddr + ((ulong) 0x0555 << 3)) = 0x00900090;
58 V_ULONG (baseaddr + 4 + ((ulong) 0x0555 << 3)) = 0x00AA00AA;
59 V_ULONG (baseaddr + 4 + ((ulong) 0x02AA << 3)) = 0x00550055;
60 V_ULONG (baseaddr + 4 + ((ulong) 0x0555 << 3)) = 0x00900090;
62 flashtest_h = V_ULONG (baseaddr); /* manufacturer ID */
63 flashtest_l = V_ULONG (baseaddr + 4);
65 switch ((int) flashtest_h) {
67 info->flash_id = FLASH_MAN_AMD;
70 info->flash_id = FLASH_MAN_FUJ;
73 info->flash_id = FLASH_UNKNOWN;
74 info->sector_count = 0;
76 return (0); /* no or unknown flash */
79 flashtest_h = V_ULONG (baseaddr + 8); /* device ID */
80 flashtest_l = V_ULONG (baseaddr + 12);
81 if (flashtest_h != flashtest_l) {
82 info->flash_id = FLASH_UNKNOWN;
84 switch (flashtest_h) {
86 info->flash_id += FLASH_AM800T;
87 info->sector_count = 19;
88 info->size = 0x00400000;
89 break; /* 4 * 1 MB = 4 MB */
91 info->flash_id += FLASH_AM800B;
92 info->sector_count = 19;
93 info->size = 0x00400000;
94 break; /* 4 * 1 MB = 4 MB */
96 info->flash_id += FLASH_AM160T;
97 info->sector_count = 35;
98 info->size = 0x00800000;
99 break; /* 4 * 2 MB = 8 MB */
101 info->flash_id += FLASH_AM160B;
102 info->sector_count = 35;
103 info->size = 0x00800000;
104 break; /* 4 * 2 MB = 8 MB */
106 info->flash_id += FLASH_AMDL322T;
107 info->sector_count = 71;
108 info->size = 0x01000000;
109 break; /* 4 * 4 MB = 16 MB */
111 info->flash_id += FLASH_AMDL322B;
112 info->sector_count = 71;
113 info->size = 0x01000000;
114 break; /* 4 * 4 MB = 16 MB */
116 info->flash_id += FLASH_AMDL323T;
117 info->sector_count = 71;
118 info->size = 0x01000000;
119 break; /* 4 * 4 MB = 16 MB */
121 info->flash_id += FLASH_AMDL323B;
122 info->sector_count = 71;
123 info->size = 0x01000000;
124 break; /* 4 * 4 MB = 16 MB */
126 info->flash_id += FLASH_AM640U;
127 info->sector_count = 128;
128 info->size = 0x02000000;
129 break; /* 4 * 8 MB = 32 MB */
131 info->flash_id = FLASH_UNKNOWN;
132 return (0); /* no or unknown flash */
136 if (flashtest_h == AMD_ID_LV640U) {
138 /* set up sector start adress table (uniform sector type) */
139 for (i = 0; i < info->sector_count; i++)
140 info->start[i] = baseaddr + (i * 0x00040000);
142 } else if (info->flash_id & FLASH_BTYPE) {
144 /* set up sector start adress table (bottom sector type) */
145 info->start[0] = baseaddr + 0x00000000;
146 info->start[1] = baseaddr + 0x00010000;
147 info->start[2] = baseaddr + 0x00018000;
148 info->start[3] = baseaddr + 0x00020000;
149 for (i = 4; i < info->sector_count; i++) {
150 info->start[i] = baseaddr + (i * 0x00040000) - 0x000C0000;
155 /* set up sector start adress table (top sector type) */
156 i = info->sector_count - 1;
157 info->start[i--] = baseaddr + info->size - 0x00010000;
158 info->start[i--] = baseaddr + info->size - 0x00018000;
159 info->start[i--] = baseaddr + info->size - 0x00020000;
160 for (; i >= 0; i--) {
161 info->start[i] = baseaddr + i * 0x00040000;
165 /* check for protected sectors */
166 for (i = 0; i < info->sector_count; i++) {
167 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
168 if ((V_ULONG (info->start[i] + 16) & 0x00010001) ||
169 (V_ULONG (info->start[i] + 20) & 0x00010001)) {
170 info->protect[i] = 1; /* D0 = 1 if protected */
172 info->protect[i] = 0;
180 /*-----------------------------------------------------------------------
182 unsigned long flash_init (void)
184 unsigned long size_b0 = 0;
187 /* Init: no FLASHes known */
188 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
189 flash_info[i].flash_id = FLASH_UNKNOWN;
192 /* Static FLASH Bank configuration here (only one bank) */
194 size_b0 = flash_get_size (CFG_FLASH0_BASE, &flash_info[0]);
195 if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
196 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
197 size_b0, size_b0 >> 20);
201 * protect monitor and environment sectors
204 #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
205 flash_protect (FLAG_PROTECT_SET,
207 CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
210 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
211 # ifndef CFG_ENV_SIZE
212 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
214 flash_protect (FLAG_PROTECT_SET,
216 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
222 /*-----------------------------------------------------------------------
224 void flash_print_info (flash_info_t * info)
228 if (info->flash_id == FLASH_UNKNOWN) {
229 printf ("missing or unknown FLASH type\n");
233 switch (info->flash_id & FLASH_VENDMASK) {
241 printf ("Unknown Vendor ");
245 switch (info->flash_id & FLASH_TYPEMASK) {
247 printf ("29LV800T (8 M, top sector)\n");
250 printf ("29LV800T (8 M, bottom sector)\n");
253 printf ("29LV160T (16 M, top sector)\n");
256 printf ("29LV160B (16 M, bottom sector)\n");
259 printf ("29DL322T (32 M, top sector)\n");
262 printf ("29DL322B (32 M, bottom sector)\n");
265 printf ("29DL323T (32 M, top sector)\n");
268 printf ("29DL323B (32 M, bottom sector)\n");
271 printf ("29LV640D (64 M, uniform sector)\n");
274 printf ("Unknown Chip Type\n");
278 printf (" Size: %ld MB in %d Sectors\n",
279 info->size >> 20, info->sector_count);
281 printf (" Sector Start Addresses:");
282 for (i = 0; i < info->sector_count; ++i) {
287 info->protect[i] ? " (RO)" : " "
294 /*-----------------------------------------------------------------------
296 int flash_erase (flash_info_t * info, int s_first, int s_last)
298 int flag, prot, sect, l_sect;
299 ulong start, now, last;
301 if ((s_first < 0) || (s_first > s_last)) {
302 if (info->flash_id == FLASH_UNKNOWN) {
303 printf ("- missing\n");
305 printf ("- no sectors to erase\n");
311 for (sect = s_first; sect <= s_last; sect++) {
312 if (info->protect[sect])
317 printf ("- Warning: %d protected sectors will not be erased!\n",
325 /* Disable interrupts which might cause a timeout here */
326 flag = disable_interrupts ();
328 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
329 V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
330 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00800080;
331 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
332 V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
333 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
334 V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
335 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00800080;
336 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
337 V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
340 /* Start erase on unprotected sectors */
341 for (sect = s_first; sect <= s_last; sect++) {
342 if (info->protect[sect] == 0) { /* not protected */
343 V_ULONG (info->start[sect]) = 0x00300030;
344 V_ULONG (info->start[sect] + 4) = 0x00300030;
349 /* re-enable interrupts if necessary */
351 enable_interrupts ();
353 /* wait at least 80us - let's wait 1 ms */
357 * We wait for the last triggered sector
362 start = get_timer (0);
364 while ((V_ULONG (info->start[l_sect]) & 0x00800080) != 0x00800080 ||
365 (V_ULONG (info->start[l_sect] + 4) & 0x00800080) != 0x00800080)
367 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
368 printf ("Timeout\n");
371 /* show that we're waiting */
372 if ((now - last) > 1000) { /* every second */
379 /* reset to read mode */
386 static int write_dword (flash_info_t *, ulong, unsigned char *);
388 /*-----------------------------------------------------------------------
389 * Copy memory to flash, returns:
392 * 2 - Flash not erased
395 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
398 static unsigned char bb[8];
399 int i, l, rc, cc = cnt;
401 dp = (addr & ~7); /* get lower dword aligned address */
404 * handle unaligned start bytes
406 if ((l = addr - dp) != 0) {
407 for (i = 0; i < 8; i++)
408 bb[i] = (i < l || (i - l) >= cc) ? V_BYTE (dp + i) : *src++;
409 if ((rc = write_dword (info, dp, bb)) != 0) {
417 * handle word aligned part
420 if ((rc = write_dword (info, dp, src)) != 0) {
433 * handle unaligned tail bytes
435 for (i = 0; i < 8; i++) {
436 bb[i] = (i < cc) ? *src++ : V_BYTE (dp + i);
438 return (write_dword (info, dp, bb));
441 /*-----------------------------------------------------------------------
442 * Write a dword to Flash, returns:
445 * 2 - Flash not erased
447 static int write_dword (flash_info_t * info, ulong dest, unsigned char *pdata)
452 for (ch = 0, i = 0; i < 4; i++)
453 ch = (ch << 8) + *pdata++; /* high word */
454 for (cl = 0, i = 0; i < 4; i++)
455 cl = (cl << 8) + *pdata++; /* low word */
457 /* Check if Flash is (sufficiently) erased */
458 if ((*((vu_long *) dest) & ch) != ch
459 || (*((vu_long *) (dest + 4)) & cl) != cl) {
463 /* Disable interrupts which might cause a timeout here */
464 flag = disable_interrupts ();
466 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
467 V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
468 V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00A000A0;
470 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
471 V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
472 V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00A000A0;
473 V_ULONG (dest + 4) = cl;
475 /* re-enable interrupts if necessary */
477 enable_interrupts ();
479 /* data polling for D7 */
480 start = get_timer (0);
481 while (((V_ULONG (dest) & 0x00800080) != (ch & 0x00800080)) ||
482 ((V_ULONG (dest + 4) & 0x00800080) != (cl & 0x00800080))) {
483 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {