2 * (C) Copyright 2001, 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Flash Routines for Intel devices
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,
31 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
33 /*-----------------------------------------------------------------------
35 ulong flash_get_size (volatile unsigned long *baseaddr,
39 unsigned long flashtest_h, flashtest_l;
41 info->sector_count = info->size = 0;
42 info->flash_id = FLASH_UNKNOWN;
44 /* Write query command sequence and test FLASH answer
46 baseaddr[0] = 0x00980098;
47 baseaddr[1] = 0x00980098;
49 flashtest_h = baseaddr[0]; /* manufacturer ID */
50 flashtest_l = baseaddr[1];
52 if (flashtest_h != INTEL_MANUFACT || flashtest_l != INTEL_MANUFACT)
53 return (0); /* no or unknown flash */
55 flashtest_h = baseaddr[2]; /* device ID */
56 flashtest_l = baseaddr[3];
58 if (flashtest_h != flashtest_l)
61 switch (flashtest_h) {
62 case INTEL_ID_28F160C3B:
63 info->flash_id = FLASH_28F160C3B;
64 info->sector_count = 39;
65 info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
67 case INTEL_ID_28F160F3B:
68 info->flash_id = FLASH_28F160F3B;
69 info->sector_count = 39;
70 info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
73 return (0); /* no or unknown flash */
76 info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
78 if (info->flash_id & FLASH_BTYPE) {
79 volatile unsigned long *tmp = baseaddr;
81 /* set up sector start adress table (bottom sector type)
82 * AND unlock the sectors (if our chip is 160C3)
84 for (i = 0; i < info->sector_count; i++) {
85 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) {
91 info->start[i] = (uint) tmp;
92 tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith */
96 memset (info->protect, 0, info->sector_count);
98 baseaddr[0] = 0x00FF00FF;
99 baseaddr[1] = 0x00FF00FF;
104 /*-----------------------------------------------------------------------
106 unsigned long flash_init (void)
108 unsigned long size_b0 = 0;
111 /* Init: no FLASHes known
113 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
114 flash_info[i].flash_id = FLASH_UNKNOWN;
117 /* Static FLASH Bank configuration here (only one bank) */
119 size_b0 = flash_get_size ((ulong *) CFG_FLASH0_BASE, &flash_info[0]);
120 if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
121 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
122 size_b0, size_b0 >> 20);
125 /* protect monitor and environment sectors
128 #ifndef CONFIG_BOOT_ROM
129 /* If U-Boot is booted from ROM the CFG_MONITOR_BASE > CFG_FLASH0_BASE
130 * but we shouldn't protect it.
133 # if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
134 flash_protect (FLAG_PROTECT_SET,
136 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1, &flash_info[0]
139 #endif /* CONFIG_BOOT_ROM */
141 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
142 # ifndef CFG_ENV_SIZE
143 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
145 flash_protect (FLAG_PROTECT_SET,
147 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
153 /*-----------------------------------------------------------------------
155 void flash_print_info (flash_info_t * info)
159 if (info->flash_id == FLASH_UNKNOWN) {
160 printf ("missing or unknown FLASH type\n");
164 switch ((info->flash_id >> 16) & 0xff) {
169 printf ("Unknown Vendor ");
173 switch (info->flash_id & FLASH_TYPEMASK) {
174 case FLASH_28F160C3B:
175 printf ("28F160C3B (16 M, bottom sector)\n");
177 case FLASH_28F160F3B:
178 printf ("28F160F3B (16 M, bottom sector)\n");
181 printf ("Unknown Chip Type\n");
185 printf (" Size: %ld MB in %d Sectors\n",
186 info->size >> 20, info->sector_count);
188 printf (" Sector Start Addresses:");
189 for (i = 0; i < info->sector_count; ++i) {
194 info->protect[i] ? " (RO)" : " "
200 /*-----------------------------------------------------------------------
202 int flash_erase (flash_info_t * info, int s_first, int s_last)
204 int flag, prot, sect;
205 ulong start, now, last;
207 if ((s_first < 0) || (s_first > s_last)) {
208 if (info->flash_id == FLASH_UNKNOWN) {
209 printf ("- missing\n");
211 printf ("- no sectors to erase\n");
217 for (sect = s_first; sect <= s_last; sect++) {
218 if (info->protect[sect])
223 printf ("- Warning: %d protected sectors will not be erased!\n",
229 /* Start erase on unprotected sectors
231 for (sect = s_first; sect <= s_last; sect++) {
232 volatile ulong *addr =
233 (volatile unsigned long *) info->start[sect];
235 start = get_timer (0);
237 if (info->protect[sect] == 0) {
238 /* Disable interrupts which might cause a timeout here
240 flag = disable_interrupts ();
244 addr[0] = 0x00200020;
245 addr[1] = 0x00200020;
246 addr[0] = 0x00D000D0;
247 addr[1] = 0x00D000D0;
249 /* re-enable interrupts if necessary
252 enable_interrupts ();
254 /* wait at least 80us - let's wait 1 ms
259 while ((addr[0] & 0x00800080) != 0x00800080 ||
260 (addr[1] & 0x00800080) != 0x00800080) {
261 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
262 printf ("Timeout (erase suspended!)\n");
265 addr[0] = 0x00B000B0;
266 addr[1] = 0x00B000B0;
269 /* show that we're waiting
271 if ((now - last) > 1000) { /* every second */
276 if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
277 printf ("*** ERROR: erase failed!\n");
281 /* Clear status register and reset to read mode
283 addr[0] = 0x00500050;
284 addr[1] = 0x00500050;
285 addr[0] = 0x00FF00FF;
286 addr[1] = 0x00FF00FF;
295 static int write_word (flash_info_t *, volatile unsigned long *, ulong);
297 /*-----------------------------------------------------------------------
298 * Copy memory to flash, returns:
301 * 2 - Flash not erased
303 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
306 int i, l, cc = cnt, res = 0;
309 for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
313 for (i = 0; i < 4; i++) {
314 v = (v << 8) + (i < l || i - l >= cc ?
315 *((unsigned char *) addr + i) : *src++);
318 if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
325 /*-----------------------------------------------------------------------
326 * Write a word to Flash, returns:
329 * 2 - Flash not erased
331 static int write_word (flash_info_t * info, volatile unsigned long *addr,
337 /* Check if Flash is (sufficiently) erased
339 if ((*addr & data) != data)
342 /* Disable interrupts which might cause a timeout here
344 flag = disable_interrupts ();
349 /* re-enable interrupts if necessary
352 enable_interrupts ();
354 start = get_timer (0);
355 while ((*addr & 0x00800080) != 0x00800080) {
356 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
365 if (*addr & 0x00220022) {
366 printf ("*** ERROR: program failed!\n");
371 /* Clear status register and reset to read mode