3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
28 /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
29 * has nothing to do with the flash chip being 8-bit or 16-bit.
31 #ifdef CONFIG_FLASH_16BIT
32 typedef unsigned short FLASH_PORT_WIDTH;
33 typedef volatile unsigned short FLASH_PORT_WIDTHV;
34 #define FLASH_ID_MASK 0xFFFF
36 typedef unsigned char FLASH_PORT_WIDTH;
37 typedef volatile unsigned char FLASH_PORT_WIDTHV;
38 #define FLASH_ID_MASK 0xFF
41 #define FPW FLASH_PORT_WIDTH
42 #define FPWV FLASH_PORT_WIDTHV
44 #define ORMASK(size) ((-size) & OR_AM_MSK)
46 #define FLASH_CYCLE1 0x0555
47 #define FLASH_CYCLE2 0x02aa
49 /*-----------------------------------------------------------------------
52 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
53 static void flash_reset(flash_info_t *info);
54 static flash_info_t *flash_get_info(ulong base);
55 static int write_data (flash_info_t *info, FPWV *dest, FPW data); /* O2D */
57 /*-----------------------------------------------------------------------
60 * sets up flash_info and returns size of FLASH (bytes)
62 unsigned long flash_init (void)
64 unsigned long size = 0;
66 extern void flash_preinit(void);
67 extern void flash_afterinit(ulong);
71 /* Init: no FLASHes known */
72 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
73 memset(&flash_info[i], 0, sizeof(flash_info_t));
74 flash_info[i].flash_id = FLASH_UNKNOWN;
77 /* Query flash chip */
79 flash_get_size((FPW *)CFG_FLASH_BASE, &flash_info[0]);
80 size += flash_info[0].size;
82 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
83 /* monitor protection ON by default */
84 flash_protect(FLAG_PROTECT_SET,
86 CFG_MONITOR_BASE+monitor_flash_len-1,
87 flash_get_info(CFG_MONITOR_BASE));
90 #ifdef CFG_ENV_IS_IN_FLASH
91 /* ENV protection ON by default */
92 flash_protect(FLAG_PROTECT_SET,
94 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
95 flash_get_info(CFG_ENV_ADDR));
99 flash_afterinit(size);
100 return (size ? size : 1);
103 /*-----------------------------------------------------------------------
105 static void flash_reset(flash_info_t *info)
107 FPWV *base = (FPWV *)(info->start[0]);
109 /* Put FLASH back in read mode */
110 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
111 *base = (FPW)0x00FF00FF; /* Intel Read Mode */
114 /*-----------------------------------------------------------------------
117 static flash_info_t *flash_get_info(ulong base)
122 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
123 info = & flash_info[i];
125 info->start[0] <= base &&
126 base <= info->start[0] + info->size - 1)
130 return (i == CFG_MAX_FLASH_BANKS ? 0 : info);
133 /*-----------------------------------------------------------------------
136 void flash_print_info (flash_info_t *info)
142 uchar botbootletter[] = "B";
143 uchar topbootletter[] = "T";
144 uchar botboottype[] = "bottom boot sector";
145 uchar topboottype[] = "top boot sector";
147 if (info->flash_id == FLASH_UNKNOWN) {
148 printf ("missing or unknown FLASH type\n");
152 switch (info->flash_id & FLASH_VENDMASK) {
153 case FLASH_MAN_INTEL:
157 printf ("Unknown Vendor ");
161 /* check for top or bottom boot, if it applies */
162 if (info->flash_id & FLASH_BTYPE) {
163 boottype = botboottype;
164 bootletter = botbootletter;
166 boottype = topboottype;
167 bootletter = topbootletter;
170 switch (info->flash_id & FLASH_TYPEMASK) {
171 case FLASH_28F128J3A:
172 fmt = "28F128J3 (128 Mbit, uniform sectors)\n";
175 fmt = "Unknown Chip Type\n";
179 printf (fmt, bootletter, boottype);
180 printf (" Size: %ld MB in %d Sectors\n",
184 printf (" Sector Start Addresses:");
185 for (i=0; i<info->sector_count; ++i) {
189 printf (" %08lX%s", info->start[i],
190 info->protect[i] ? " (RO)" : " ");
195 /*-----------------------------------------------------------------------
199 * The following code cannot be run from FLASH!
201 ulong flash_get_size (FPWV *addr, flash_info_t *info)
205 /* Write auto select command: read Manufacturer ID */
206 /* Write auto select command sequence and test FLASH answer */
207 addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
209 /* The manufacturer codes are only 1 byte, so just use 1 byte.
210 * This works for any bus width and any FLASH device width.
213 switch (addr[0] & 0xff) {
214 case (uchar)INTEL_MANUFACT:
215 info->flash_id = FLASH_MAN_INTEL;
218 info->flash_id = FLASH_UNKNOWN;
219 info->sector_count = 0;
224 /* Strataflash is configurable to 8/16bit Bus,
225 * but the Query-Structure is Word-orientated */
226 if (info->flash_id != FLASH_UNKNOWN) {
227 switch ((FPW)addr[2]) {
228 case (FPW)INTEL_ID_28F128J3:
229 info->flash_id += FLASH_28F128J3A;
230 info->sector_count = 128;
231 info->size = 0x01000000;
232 for( i = 0; i < info->sector_count; i++ )
233 info->start[i] = (ulong)addr + (i * 0x20000);
234 break; /* => Intel Strataflash 16MB */
236 printf("Flash_id != %xd\n", (FPW)addr[2]);
237 info->flash_id = FLASH_UNKNOWN;
238 info->sector_count = 0;
240 return (0); /* => no or unknown flash */
244 /* Put FLASH back in read mode */
250 /*-----------------------------------------------------------------------
253 int flash_erase (flash_info_t *info, int s_first, int s_last)
256 int flag, prot, sect;
257 ulong start, now, last;
260 if ((s_first < 0) || (s_first > s_last)) {
261 if (info->flash_id == FLASH_UNKNOWN) {
262 printf ("- missing\n");
264 printf ("- no sectors to erase\n");
269 switch (info->flash_id & FLASH_TYPEMASK) {
270 case FLASH_28F128J3A:
274 printf ("Can't erase unknown flash type %08lx - aborted\n",
280 for (sect = s_first; sect <= s_last; ++sect)
281 if (info->protect[sect])
285 printf ("- Warning: %d protected sectors will not be erased!",
291 /* Start erase on unprotected sectors */
292 for (sect = s_first; sect <= s_last && rcode == 0; sect++) {
294 if (info->protect[sect] != 0) /* protected, skip it */
297 /* Disable interrupts which might cause a timeout here */
298 flag = disable_interrupts();
300 addr = (FPWV *)(info->start[sect]);
301 *addr = (FPW)0x00500050; /* clear status register */
302 *addr = (FPW)0x00200020; /* erase setup */
303 *addr = (FPW)0x00D000D0; /* erase confirm */
305 /* re-enable interrupts if necessary */
309 start = get_timer(0);
311 /* wait at least 80us for Intel - let's wait 1 ms */
314 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
315 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
316 printf ("Timeout\n");
317 *addr = (FPW)0x00B000B0;/* suspend erase */
318 flash_reset(info); /* reset to read mode */
319 rcode = 1; /* failed */
323 /* show that we're waiting */
324 if ((get_timer(last)) > CFG_HZ) { /* every second */
330 flash_reset(info); /* reset to read mode */
337 /*-----------------------------------------------------------------------
338 * Copy memory to flash, returns:
341 * 2 - Flash not erased
343 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
345 FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
346 int bytes; /* number of bytes to program in current word */
347 int left; /* number of bytes left to program */
350 for (left = cnt, res = 0;
351 left > 0 && res == 0;
352 addr += sizeof(data), left -= sizeof(data) - bytes) {
354 bytes = addr & (sizeof(data) - 1);
355 addr &= ~(sizeof(data) - 1);
357 /* combine source and destination data so can program
358 * an entire word of 16 or 32 bits */
359 for (i = 0; i < sizeof(data); i++) {
361 if (i < bytes || i - bytes >= left )
362 data += *((uchar *)addr + i);
367 /* write one word to the flash */
368 switch (info->flash_id & FLASH_VENDMASK) {
369 case FLASH_MAN_INTEL:
370 res = write_data(info, (FPWV *)addr, data);
373 /* unknown flash type, error! */
374 printf ("missing or unknown FLASH type\n");
375 res = 1; /* not really a timeout, but gives error */
383 /*-----------------------------------------------------------------------
384 * Write a word or halfword to Flash, returns:
387 * 2 - Flash not erased
389 static int write_data (flash_info_t *info, FPWV *dest, FPW data)
396 /* Check if Flash is (sufficiently) erased */
397 if ((*addr & data) != data) {
398 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
401 /* Disable interrupts which might cause a timeout here */
402 flag = disable_interrupts ();
404 *addr = (FPW) 0x00400040; /* write setup */
407 /* arm simple, non interrupt dependent timer */
408 start = get_timer(0);
410 /* wait while polling the status register */
411 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
412 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
413 *addr = (FPW) 0x00FF00FF; /* restore read mode */
418 *addr = (FPW) 0x00FF00FF; /* restore read mode */