3 * Marius Groeger <mgroeger@sysgo.de>
4 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * Flash Routines for AMD 29F080B devices
10 * Added support for 64bit and AMD 29DL323B
12 *--------------------------------------------------------------------
13 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
38 #define RD_SWP32(x) in_le32((volatile u32*)x)
40 /*-----------------------------------------------------------------------
44 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
45 static int write_word (flash_info_t *info, ulong dest, ulong data);
47 /*-----------------------------------------------------------------------
50 unsigned long flash_init(void)
54 /* Init: no FLASHes known */
55 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
56 flash_info[i].flash_id = FLASH_UNKNOWN;
58 /* for now, only support the 4 MB Flash SIMM */
59 (void)flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
63 * protect monitor and environment sectors
66 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
67 flash_protect(FLAG_PROTECT_SET,
68 CONFIG_SYS_MONITOR_BASE,
69 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
73 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
74 #ifndef CONFIG_ENV_SIZE
75 #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
77 flash_protect(FLAG_PROTECT_SET,
79 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
82 return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024;
85 /*-----------------------------------------------------------------------
87 void flash_print_info (flash_info_t *info)
91 if (info->flash_id == FLASH_UNKNOWN) {
92 printf ("missing or unknown FLASH type\n");
96 switch (info->flash_id & FLASH_VENDMASK) {
97 case (AMD_MANUFACT & FLASH_VENDMASK):
100 case (FUJ_MANUFACT & FLASH_VENDMASK):
103 case (SST_MANUFACT & FLASH_VENDMASK):
107 printf ("Unknown Vendor ");
111 switch (info->flash_id & FLASH_TYPEMASK) {
112 case (AMD_ID_DL323B & FLASH_TYPEMASK):
113 printf("AM29DL323B (32 MBit)\n");
116 printf ("Unknown Chip Type\n");
120 printf (" Size: %ld MB in %d Sectors\n",
121 info->size >> 20, info->sector_count);
123 printf (" Sector Start Addresses:");
124 for (i = 0; i < info->sector_count; ++i) {
125 if ((i % 5) == 0) printf ("\n ");
128 info->protect[i] ? " (RO)" : " "
136 * The following code cannot be run from FLASH!
139 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
142 vu_long vendor[2], devid[2];
143 ulong base = (ulong)addr;
145 /* Reset and Write auto select command: read Manufacturer ID */
146 addr[0] = 0xf0f0f0f0;
147 addr[2 * 0x0555] = 0xAAAAAAAA;
148 addr[2 * 0x02AA] = 0x55555555;
149 addr[2 * 0x0555] = 0x90909090;
150 addr[1] = 0xf0f0f0f0;
151 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
152 addr[2 * 0x02AA + 1] = 0x55555555;
153 addr[2 * 0x0555 + 1] = 0x90909090;
156 vendor[0] = RD_SWP32(&addr[0]);
157 vendor[1] = RD_SWP32(&addr[1]);
158 if (vendor[0] != vendor[1] || vendor[0] != AMD_MANUFACT) {
163 devid[0] = RD_SWP32(&addr[2]);
164 devid[1] = RD_SWP32(&addr[3]);
166 if (devid[0] == AMD_ID_DL323B) {
169 * Bank 1 (23 Sectors): 0-7=8kbyte, 8-22=64kbyte
170 * Bank 2 (48 Sectors): 23-70=64kbyte
172 info->flash_id = (AMD_MANUFACT & FLASH_VENDMASK) |
173 (AMD_ID_DL323B & FLASH_TYPEMASK);
174 info->sector_count = 71;
175 info->size = 4 * (8 * 8 + 63 * 64) * 1024;
182 /* set up sector start address table */
183 for (i = 0; i < 8; i++) {
184 info->start[i] = base + (i * 0x8000);
186 for (i = 8; i < info->sector_count; i++) {
187 info->start[i] = base + (i * 0x40000) + 8 * 0x8000 - 8 * 0x40000;
190 /* check for protected sectors */
191 for (i = 0; i < info->sector_count; i++) {
192 /* read sector protection at sector address */
193 addr = (volatile unsigned long *)(info->start[i]);
194 addr[2 * 0x0555] = 0xAAAAAAAA;
195 addr[2 * 0x02AA] = 0x55555555;
196 addr[2 * 0x0555] = 0x90909090;
197 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
198 addr[2 * 0x02AA + 1] = 0x55555555;
199 addr[2 * 0x0555 + 1] = 0x90909090;
201 base = RD_SWP32(&addr[4]);
202 base |= RD_SWP32(&addr[5]);
203 info->protect[i] = base & 0x00010001 ? 1 : 0;
205 addr = (vu_long*)info->start[0];
209 addr[0] = 0xf0f0f0f0;
210 addr[1] = 0xf0f0f0f0;
216 /*-----------------------------------------------------------------------
219 int flash_erase (flash_info_t *info, int s_first, int s_last)
221 vu_long *addr = (vu_long*)(info->start[0]);
222 int flag, prot, sect, l_sect;
223 ulong start, now, last;
225 if ((s_first < 0) || (s_first > s_last)) {
226 if (info->flash_id == FLASH_UNKNOWN) {
227 printf ("- missing\n");
229 printf ("- no sectors to erase\n");
235 for (sect = s_first; sect <= s_last; sect++) {
236 if (info->protect[sect]) {
242 printf ("- Warning: %d protected sectors will not be erased!\n",
250 /* Disable interrupts which might cause a timeout here */
251 flag = disable_interrupts();
253 addr[2 * 0x0555] = 0xAAAAAAAA;
254 addr[2 * 0x02AA] = 0x55555555;
255 addr[2 * 0x0555] = 0x80808080;
256 addr[2 * 0x0555] = 0xAAAAAAAA;
257 addr[2 * 0x02AA] = 0x55555555;
258 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
259 addr[2 * 0x02AA + 1] = 0x55555555;
260 addr[2 * 0x0555 + 1] = 0x80808080;
261 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
262 addr[2 * 0x02AA + 1] = 0x55555555;
265 /* Start erase on unprotected sectors */
266 for (sect = s_first; sect<=s_last; sect++) {
267 if (info->protect[sect] == 0) { /* not protected */
268 addr = (vu_long*)(info->start[sect]);
269 addr[0] = 0x30303030;
270 addr[1] = 0x30303030;
275 /* re-enable interrupts if necessary */
279 /* wait at least 80us - let's wait 1 ms */
283 * We wait for the last triggered sector
288 start = get_timer (0);
290 addr = (vu_long*)(info->start[l_sect]);
291 while ( (addr[0] & 0x80808080) != 0x80808080 ||
292 (addr[1] & 0x80808080) != 0x80808080) {
293 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
294 printf ("Timeout\n");
297 /* show that we're waiting */
298 if ((now - last) > 1000) { /* every second */
305 /* reset to read mode */
306 addr = (volatile unsigned long *)info->start[0];
307 addr[0] = 0xF0F0F0F0; /* reset bank */
308 addr[1] = 0xF0F0F0F0; /* reset bank */
314 /*-----------------------------------------------------------------------
315 * Copy memory to flash, returns:
318 * 2 - Flash not erased
321 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
326 wp = (addr & ~3); /* get lower word aligned address */
329 * handle unaligned start bytes
331 if ((l = addr - wp) != 0) {
333 for (i=0, cp=wp; i<l; ++i, ++cp) {
334 data = (data << 8) | (*(uchar *)cp);
336 for (; i<4 && cnt>0; ++i) {
337 data = (data << 8) | *src++;
341 for (; cnt==0 && i<4; ++i, ++cp) {
342 data = (data << 8) | (*(uchar *)cp);
345 if ((rc = write_word(info, wp, data)) != 0) {
352 * handle word aligned part
356 for (i=0; i<4; ++i) {
357 data = (data << 8) | *src++;
359 if ((rc = write_word(info, wp, data)) != 0) {
371 * handle unaligned tail bytes
374 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
375 data = (data << 8) | *src++;
378 for (; i<4; ++i, ++cp) {
379 data = (data << 8) | (*(uchar *)cp);
382 return (write_word(info, wp, data));
385 /*-----------------------------------------------------------------------
386 * Write a word to Flash, returns:
389 * 2 - Flash not erased
391 static int write_word (flash_info_t *info, ulong dest, ulong data)
393 vu_long *addr = (vu_long*)(info->start[0]);
397 /* Check if Flash is (sufficiently) erased */
398 if ((*((vu_long *)dest) & data) != data) {
401 /* Disable interrupts which might cause a timeout here */
402 flag = disable_interrupts();
404 if ((dest & 0x00000004) == 0) {
405 addr[2 * 0x0555] = 0xAAAAAAAA;
406 addr[2 * 0x02AA] = 0x55555555;
407 addr[2 * 0x0555] = 0xA0A0A0A0;
410 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
411 addr[2 * 0x02AA + 1] = 0x55555555;
412 addr[2 * 0x0555 + 1] = 0xA0A0A0A0;
415 *((vu_long *)dest) = data;
417 /* re-enable interrupts if necessary */
421 /* data polling for D7 */
422 start = get_timer (0);
423 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
424 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
431 /*-----------------------------------------------------------------------