2 * (C) Copyright 2003-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
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,
29 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
32 * CPU to flash interface is 8-bit, so make declaration accordingly
34 typedef unsigned char FLASH_PORT_WIDTH;
35 typedef volatile unsigned char FLASH_PORT_WIDTHV;
37 #define FPW FLASH_PORT_WIDTH
38 #define FPWV FLASH_PORT_WIDTHV
40 #define FLASH_CYCLE1 0x0555
41 #define FLASH_CYCLE2 0x02aa
43 /*-----------------------------------------------------------------------
46 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
47 static void flash_reset(flash_info_t *info);
48 static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
49 static flash_info_t *flash_get_info(ulong base);
51 /*-----------------------------------------------------------------------
54 * sets up flash_info and returns size of FLASH (bytes)
56 unsigned long flash_init (void)
58 unsigned long size = 0;
59 extern void flash_preinit(void);
60 ulong flashbase = CFG_FLASH_BASE;
64 /* Init: no FLASHes known */
65 memset(&flash_info[0], 0, sizeof(flash_info_t));
68 flash_get_size((FPW *)flashbase, &flash_info[0]);
70 size = flash_info[0].size;
72 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
73 /* monitor protection ON by default */
74 flash_protect(FLAG_PROTECT_SET,
76 CFG_MONITOR_BASE+monitor_flash_len-1,
77 flash_get_info(CFG_MONITOR_BASE));
80 #ifdef CFG_ENV_IS_IN_FLASH
81 /* ENV protection ON by default */
82 flash_protect(FLAG_PROTECT_SET,
84 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
85 flash_get_info(CFG_ENV_ADDR));
88 return size ? size : 1;
91 /*-----------------------------------------------------------------------
93 static void flash_reset(flash_info_t *info)
95 FPWV *base = (FPWV *)(info->start[0]);
97 /* Put FLASH back in read mode */
98 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
99 *base = (FPW)0x00FF00FF; /* Intel Read Mode */
100 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
101 *base = (FPW)0x00F000F0; /* AMD Read Mode */
104 /*-----------------------------------------------------------------------
107 static flash_info_t *flash_get_info(ulong base)
112 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
113 info = & flash_info[i];
114 if (info->size && info->start[0] <= base &&
115 base <= info->start[0] + info->size - 1)
119 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
122 /*-----------------------------------------------------------------------
125 void flash_print_info (flash_info_t *info)
129 if (info->flash_id == FLASH_UNKNOWN) {
130 printf ("missing or unknown FLASH type\n");
134 switch (info->flash_id & FLASH_VENDMASK) {
135 case FLASH_MAN_AMD: printf ("AMD "); break;
136 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
137 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
138 case FLASH_MAN_SST: printf ("SST "); break;
139 case FLASH_MAN_STM: printf ("STM "); break;
140 case FLASH_MAN_INTEL: printf ("INTEL "); break;
141 default: printf ("Unknown Vendor "); break;
144 switch (info->flash_id & FLASH_TYPEMASK) {
146 printf ("AM29LV116DB (16Mbit, bottom boot sect)\n");
149 printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
152 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
155 printf ("Unknown Chip Type\n");
159 printf (" Size: %ld MB in %d Sectors\n",
163 printf (" Sector Start Addresses:");
165 for (i=0; i<info->sector_count; ++i) {
171 info->protect[i] ? " (RO)" : " ");
177 /*-----------------------------------------------------------------------
181 * The following code cannot be run from FLASH!
184 ulong flash_get_size (FPWV *addr, flash_info_t *info)
187 ulong base = (ulong)addr;
189 /* Write auto select command: read Manufacturer ID */
190 /* Write auto select command sequence and test FLASH answer */
191 addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
192 addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
193 addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
195 /* The manufacturer codes are only 1 byte, so just use 1 byte.
196 * This works for any bus width and any FLASH device width.
199 switch (addr[0] & 0xff) {
201 case (uchar)AMD_MANUFACT:
202 debug ("Manufacturer: AMD (Spansion)\n");
203 info->flash_id = FLASH_MAN_AMD;
206 case (uchar)INTEL_MANUFACT:
207 debug ("Manufacturer: Intel (not supported yet)\n");
208 info->flash_id = FLASH_MAN_INTEL;
212 info->flash_id = FLASH_UNKNOWN;
213 info->sector_count = 0;
218 /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
219 if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[1]) {
221 case (uchar)AMD_ID_LV116DB:
222 debug ("Chip: AM29LV116DB\n");
223 info->flash_id += FLASH_AM116DB;
224 info->sector_count = 35;
225 info->size = 0x00200000;
227 * The first 4 sectors are 16 kB, 8 kB, 8 kB and 32 kB, all
228 * the other ones are 64 kB
230 info->start[0] = base + 0x00000000;
231 info->start[1] = base + 0x00004000;
232 info->start[2] = base + 0x00006000;
233 info->start[3] = base + 0x00008000;
234 for( i = 4; i < info->sector_count; i++ )
236 base + (i * (64 << 10)) - 0x00030000;
239 case (FPW)AMD_ID_LV160B:
240 debug ("Chip: AM29LV160MB\n");
241 info->flash_id += FLASH_AM160B;
242 info->sector_count = 35;
243 info->size = 0x00400000;
245 * The first 4 sectors are 16 kB, 8 kB, 8 kB and 32 kB, all
246 * the other ones are 64 kB
248 info->start[0] = base + 0x00000000;
249 info->start[1] = base + 0x00008000;
250 info->start[2] = base + 0x0000C000;
251 info->start[3] = base + 0x00010000;
252 for( i = 4; i < info->sector_count; i++ )
254 base + (i * 2 * (64 << 10)) - 0x00060000;
258 info->flash_id = FLASH_UNKNOWN;
259 info->sector_count = 0;
263 /* Put FLASH back in read mode */
269 /*-----------------------------------------------------------------------
272 int flash_erase (flash_info_t *info, int s_first, int s_last)
274 FPWV *addr = (FPWV*)(info->start[0]);
275 int flag, prot, sect, l_sect;
276 ulong start, now, last;
278 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
280 if ((s_first < 0) || (s_first > s_last)) {
281 if (info->flash_id == FLASH_UNKNOWN) {
282 printf ("- missing\n");
284 printf ("- no sectors to erase\n");
289 if ((info->flash_id == FLASH_UNKNOWN) ||
290 (info->flash_id > FLASH_AMD_COMP)) {
291 printf ("Can't erase unknown flash type %08lx - aborted\n",
297 for (sect=s_first; sect<=s_last; ++sect) {
298 if (info->protect[sect]) {
304 printf ("- Warning: %d protected sectors will not be erased!\n",
312 /* Disable interrupts which might cause a timeout here */
313 flag = disable_interrupts();
315 addr[0x0555] = (FPW)0x00AA00AA;
316 addr[0x02AA] = (FPW)0x00550055;
317 addr[0x0555] = (FPW)0x00800080;
318 addr[0x0555] = (FPW)0x00AA00AA;
319 addr[0x02AA] = (FPW)0x00550055;
321 /* Start erase on unprotected sectors */
322 for (sect = s_first; sect<=s_last; sect++) {
323 if (info->protect[sect] == 0) { /* not protected */
324 addr = (FPWV*)(info->start[sect]);
325 addr[0] = (FPW)0x00300030;
330 /* re-enable interrupts if necessary */
334 /* wait at least 80us - let's wait 1 ms */
338 * We wait for the last triggered sector
343 start = get_timer (0);
345 addr = (FPWV*)(info->start[l_sect]);
346 while ((addr[0] & (FPW)0x00800080) != (FPW)0x00800080) {
347 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
348 printf ("Timeout\n");
351 /* show that we're waiting */
352 if ((now - last) > 1000) { /* every second */
359 /* reset to read mode */
360 addr = (FPWV*)info->start[0];
361 addr[0] = (FPW)0x00F000F0; /* reset bank */
367 /*-----------------------------------------------------------------------
368 * Copy memory to flash, returns:
371 * 2 - Flash not erased
374 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
378 for (i = 0; i < cnt; i++)
379 if ((rc = write_word_amd(info, (FPW *)(addr+i), src[i])) != 0) {
386 /*-----------------------------------------------------------------------
387 * Write a word to Flash for AMD FLASH
388 * A word is 16 or 32 bits, whichever the bus width of the flash bank
389 * (not an individual chip) is.
394 * 2 - Flash not erased
396 static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
400 FPWV *base; /* first address in flash bank */
402 /* Check if Flash is (sufficiently) erased */
403 if ((*dest & data) != data) {
407 base = (FPWV *)(info->start[0]);
409 /* Disable interrupts which might cause a timeout here */
410 flag = disable_interrupts();
412 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
413 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
414 base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
416 *dest = data; /* start programming the data */
418 /* re-enable interrupts if necessary */
422 start = get_timer (0);
424 /* data polling for D7 */
425 while ((*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
426 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
427 *dest = (FPW)0x00F000F0; /* reset bank */