2 * (C) Copyright 2000, 2001, 2002
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,
27 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
32 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33 static int write_word (flash_info_t *info, ulong dest, ulong data);
34 static void flash_get_offsets (ulong base, flash_info_t *info);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init (void)
41 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
46 /* Init: no FLASHes known */
47 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
51 /* Static FLASH Bank configuration here - FIXME XXX */
53 size = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
55 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
56 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
61 /* Remap FLASH according to real size */
62 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
63 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
65 /* Re-do sizing to get full correct info */
66 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
68 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
70 flash_info[0].size = size;
75 /*-----------------------------------------------------------------------
77 static void flash_get_offsets (ulong base, flash_info_t *info)
81 /* set up sector start address table */
82 if (info->flash_id & FLASH_BTYPE) {
83 /* set sector offsets for bottom boot block type */
84 info->start[0] = base + 0x00000000;
85 info->start[1] = base + 0x00008000;
86 info->start[2] = base + 0x0000C000;
87 info->start[3] = base + 0x00010000;
88 for (i = 4; i < info->sector_count; i++) {
89 info->start[i] = base + (i * 0x00020000) - 0x00060000;
92 /* set sector offsets for top boot block type */
93 i = info->sector_count - 1;
94 info->start[i--] = base + info->size - 0x00008000;
95 info->start[i--] = base + info->size - 0x0000C000;
96 info->start[i--] = base + info->size - 0x00010000;
98 info->start[i] = base + i * 0x00020000;
104 /*-----------------------------------------------------------------------
106 void flash_print_info (flash_info_t *info)
110 if (info->flash_id == FLASH_UNKNOWN) {
111 printf ("missing or unknown FLASH type\n");
115 switch (info->flash_id & FLASH_VENDMASK) {
116 case FLASH_MAN_AMD: printf ("AMD "); break;
117 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
118 default: printf ("Unknown Vendor "); break;
121 switch (info->flash_id & FLASH_TYPEMASK) {
122 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
124 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
126 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
128 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
130 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
132 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
134 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
136 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
138 default: printf ("Unknown Chip Type\n");
142 printf (" Size: %ld MB in %d Sectors\n",
143 info->size >> 20, info->sector_count);
145 printf (" Sector Start Addresses:");
146 for (i=0; i<info->sector_count; ++i) {
151 info->protect[i] ? " (RO)" : " "
157 /*-----------------------------------------------------------------------
161 /*-----------------------------------------------------------------------
165 * The following code cannot be run from FLASH!
168 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
172 ulong base = (ulong)addr;
175 /* Write auto select command: read Manufacturer ID */
176 addr[0x0555] = 0x00AA00AA;
177 addr[0x02AA] = 0x00550055;
178 addr[0x0555] = 0x00900090;
184 info->flash_id = FLASH_MAN_AMD;
187 info->flash_id = FLASH_MAN_FUJ;
190 info->flash_id = FLASH_UNKNOWN;
191 info->sector_count = 0;
193 return (0); /* no or unknown flash */
196 value = addr[1]; /* device ID */
200 info->flash_id += FLASH_AM400T;
201 info->sector_count = 11;
202 info->size = 0x00100000;
206 info->flash_id += FLASH_AM400B;
207 info->sector_count = 11;
208 info->size = 0x00100000;
212 info->flash_id += FLASH_AM800T;
213 info->sector_count = 19;
214 info->size = 0x00200000;
218 info->flash_id += FLASH_AM800B;
219 info->sector_count = 19;
220 info->size = 0x00200000;
224 info->flash_id += FLASH_AM160T;
225 info->sector_count = 35;
226 info->size = 0x00400000;
230 info->flash_id += FLASH_AM160B;
231 info->sector_count = 35;
232 info->size = 0x00400000;
234 #if 0 /* enable when device IDs are available */
236 info->flash_id += FLASH_AM320T;
237 info->sector_count = 67;
238 info->size = 0x00800000;
242 info->flash_id += FLASH_AM320B;
243 info->sector_count = 67;
244 info->size = 0x00800000;
248 info->flash_id = FLASH_UNKNOWN;
249 return (0); /* => no or unknown flash */
253 /* set up sector start address table */
254 if (info->flash_id & FLASH_BTYPE) {
255 /* set sector offsets for bottom boot block type */
256 info->start[0] = base + 0x00000000;
257 info->start[1] = base + 0x00008000;
258 info->start[2] = base + 0x0000C000;
259 info->start[3] = base + 0x00010000;
260 for (i = 4; i < info->sector_count; i++) {
261 info->start[i] = base + (i * 0x00020000) - 0x00060000;
264 /* set sector offsets for top boot block type */
265 i = info->sector_count - 1;
266 info->start[i--] = base + info->size - 0x00008000;
267 info->start[i--] = base + info->size - 0x0000C000;
268 info->start[i--] = base + info->size - 0x00010000;
269 for (; i >= 0; i--) {
270 info->start[i] = base + i * 0x00020000;
274 /* check for protected sectors */
275 for (i = 0; i < info->sector_count; i++) {
276 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
277 /* D0 = 1 if protected */
278 addr = (volatile unsigned long *)(info->start[i]);
279 info->protect[i] = addr[2] & 1;
283 * Prevent writes to uninitialized FLASH.
285 if (info->flash_id != FLASH_UNKNOWN) {
286 addr = (volatile unsigned long *)info->start[0];
288 *addr = 0x00F000F0; /* reset bank */
295 /*-----------------------------------------------------------------------
298 int flash_erase (flash_info_t *info, int s_first, int s_last)
300 vu_long *addr = (vu_long*)(info->start[0]);
301 int flag, prot, sect, l_sect;
302 ulong start, now, last;
304 if ((s_first < 0) || (s_first > s_last)) {
305 if (info->flash_id == FLASH_UNKNOWN) {
306 printf ("- missing\n");
308 printf ("- no sectors to erase\n");
313 if ((info->flash_id == FLASH_UNKNOWN) ||
314 (info->flash_id > FLASH_AMD_COMP)) {
315 printf ("Can't erase unknown flash type %08lx - aborted\n",
321 for (sect=s_first; sect<=s_last; ++sect) {
322 if (info->protect[sect]) {
328 printf ("- Warning: %d protected sectors will not be erased!\n",
336 /* Disable interrupts which might cause a timeout here */
337 flag = disable_interrupts();
339 addr[0x0555] = 0x00AA00AA;
340 addr[0x02AA] = 0x00550055;
341 addr[0x0555] = 0x00800080;
342 addr[0x0555] = 0x00AA00AA;
343 addr[0x02AA] = 0x00550055;
345 /* Start erase on unprotected sectors */
346 for (sect = s_first; sect<=s_last; sect++) {
347 if (info->protect[sect] == 0) { /* not protected */
348 addr = (vu_long*)(info->start[sect]);
349 addr[0] = 0x00300030;
354 /* re-enable interrupts if necessary */
358 /* wait at least 80us - let's wait 1 ms */
362 * We wait for the last triggered sector
367 start = get_timer (0);
369 addr = (vu_long*)(info->start[l_sect]);
370 while ((addr[0] & 0x00800080) != 0x00800080) {
371 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
372 printf ("Timeout\n");
375 /* show that we're waiting */
376 if ((now - last) > 1000) { /* every second */
383 /* reset to read mode */
384 addr = (volatile unsigned long *)info->start[0];
385 addr[0] = 0x00F000F0; /* reset bank */
391 /*-----------------------------------------------------------------------
392 * Copy memory to flash, returns:
395 * 2 - Flash not erased
398 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
403 wp = (addr & ~3); /* get lower word aligned address */
406 * handle unaligned start bytes
408 if ((l = addr - wp) != 0) {
410 for (i=0, cp=wp; i<l; ++i, ++cp) {
411 data = (data << 8) | (*(uchar *)cp);
413 for (; i<4 && cnt>0; ++i) {
414 data = (data << 8) | *src++;
418 for (; cnt==0 && i<4; ++i, ++cp) {
419 data = (data << 8) | (*(uchar *)cp);
422 if ((rc = write_word(info, wp, data)) != 0) {
429 * handle word aligned part
433 for (i=0; i<4; ++i) {
434 data = (data << 8) | *src++;
436 if ((rc = write_word(info, wp, data)) != 0) {
448 * handle unaligned tail bytes
451 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
452 data = (data << 8) | *src++;
455 for (; i<4; ++i, ++cp) {
456 data = (data << 8) | (*(uchar *)cp);
459 return (write_word(info, wp, data));
462 /*-----------------------------------------------------------------------
463 * Write a word to Flash, returns:
466 * 2 - Flash not erased
468 static int write_word (flash_info_t *info, ulong dest, ulong data)
470 vu_long *addr = (vu_long*)(info->start[0]);
474 /* Check if Flash is (sufficiently) erased */
475 if ((*((vu_long *)dest) & data) != data) {
478 /* Disable interrupts which might cause a timeout here */
479 flag = disable_interrupts();
481 addr[0x0555] = 0x00AA00AA;
482 addr[0x02AA] = 0x00550055;
483 addr[0x0555] = 0x00A000A0;
485 *((vu_long *)dest) = data;
487 /* re-enable interrupts if necessary */
491 /* data polling for D7 */
492 start = get_timer (0);
493 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
494 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
501 /*-----------------------------------------------------------------------