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[CFG_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 *)CFG_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size_b0 ;
46 /* Init: no FLASHes known */
47 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
51 /* Static FLASH Bank configuration here - FIXME XXX */
52 size_b0 = flash_get_size((vu_long *)FLASH_BASE_PRELIM, &flash_info[0]);
53 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
54 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
55 size_b0, size_b0<<20);
58 /* Remap FLASH according to real size */
59 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
60 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
62 /* Re-do sizing to get full correct info */
64 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
65 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
67 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
68 /* monitor protection ON by default */
69 flash_protect(FLAG_PROTECT_SET,
71 CFG_MONITOR_BASE+monitor_flash_len-1,
75 #ifdef CFG_ENV_IS_IN_FLASH
76 /* ENV protection ON by default */
77 flash_protect(FLAG_PROTECT_SET,
79 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
83 flash_info[0].size = size_b0;
88 /*-----------------------------------------------------------------------
90 static void flash_get_offsets (ulong base, flash_info_t *info)
94 /* set up sector start address table */
95 if (info->flash_id & FLASH_BTYPE) {
96 /* set sector offsets for bottom boot block type */
97 info->start[0] = base + 0x00000000;
98 info->start[1] = base + 0x00010000;
99 info->start[2] = base + 0x00018000;
100 info->start[3] = base + 0x00020000;
101 for (i = 4; i < info->sector_count; i++) {
102 info->start[i] = base + ((i-3) * 0x00040000) ;
105 /* set sector offsets for top boot block type */
106 i = info->sector_count - 1;
107 info->start[i--] = base + info->size - 0x00010000;
108 info->start[i--] = base + info->size - 0x00018000;
109 info->start[i--] = base + info->size - 0x00020000;
110 for (; i >= 0; i--) {
111 info->start[i] = base + i * 0x00040000;
117 /*-----------------------------------------------------------------------
119 void flash_print_info (flash_info_t *info)
123 if (info->flash_id == FLASH_UNKNOWN) {
124 printf ("missing or unknown FLASH type\n");
128 switch (info->flash_id & FLASH_VENDMASK) {
129 case FLASH_MAN_AMD: printf ("AMD "); break;
130 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
131 default: printf ("Unknown Vendor "); break;
134 switch (info->flash_id & FLASH_TYPEMASK) {
135 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
137 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
139 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
141 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
143 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
145 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
147 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
149 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
151 default: printf ("Unknown Chip Type\n");
155 printf (" Size: %ld MB in %d Sectors\n",
156 info->size >> 20, info->sector_count);
158 printf (" Sector Start Addresses:");
159 for (i=0; i<info->sector_count; ++i) {
164 info->protect[i] ? " (RO)" : " "
171 /*-----------------------------------------------------------------------
175 /*-----------------------------------------------------------------------
179 * The following code cannot be run from FLASH!
182 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
186 ulong base = (ulong)addr;
188 /* Write auto select command: read Manufacturer ID */
189 addr[0xAAA] = 0xAAAAAAAA ;
190 addr[0x555] = 0x55555555 ;
191 addr[0xAAA] = 0x90909090 ;
195 switch (value & 0x00FF00FF) {
197 info->flash_id = FLASH_MAN_AMD;
200 info->flash_id = FLASH_MAN_FUJ;
203 info->flash_id = FLASH_UNKNOWN;
204 info->sector_count = 0;
206 return (0); /* no or unknown flash */
209 value = addr[2] ; /* device ID */
211 switch (value & 0x00FF00FF) {
212 case (AMD_ID_LV400T & 0x00FF00FF):
213 info->flash_id += FLASH_AM400T;
214 info->sector_count = 11;
215 info->size = 0x00100000;
218 case (AMD_ID_LV400B & 0x00FF00FF):
219 info->flash_id += FLASH_AM400B;
220 info->sector_count = 11;
221 info->size = 0x00100000;
224 case (AMD_ID_LV800T & 0x00FF00FF):
225 info->flash_id += FLASH_AM800T;
226 info->sector_count = 19;
227 info->size = 0x00200000;
230 case (AMD_ID_LV800B & 0x00FF00FF):
231 info->flash_id += FLASH_AM800B;
232 info->sector_count = 19;
233 info->size = 0x00400000; /*%%% Size doubled by yooth */
236 case (AMD_ID_LV160T & 0x00FF00FF):
237 info->flash_id += FLASH_AM160T;
238 info->sector_count = 35;
239 info->size = 0x00400000;
242 case (AMD_ID_LV160B & 0x00FF00FF):
243 info->flash_id += FLASH_AM160B;
244 info->sector_count = 35;
245 info->size = 0x00800000;
247 #if 0 /* enable when device IDs are available */
249 info->flash_id += FLASH_AM320T;
250 info->sector_count = 67;
251 info->size = 0x00800000;
255 info->flash_id += FLASH_AM320B;
256 info->sector_count = 67;
257 info->size = 0x01000000;
258 break; /* => 16 MB */
261 info->flash_id = FLASH_UNKNOWN;
262 return (0); /* => no or unknown flash */
265 /*%%% sector start address modified */
266 /* set up sector start address table */
267 if (info->flash_id & FLASH_BTYPE) {
268 /* set sector offsets for bottom boot block type */
269 info->start[0] = base + 0x00000000;
270 info->start[1] = base + 0x00010000;
271 info->start[2] = base + 0x00018000;
272 info->start[3] = base + 0x00020000;
273 for (i = 4; i < info->sector_count; i++) {
274 info->start[i] = base + ((i-3) * 0x00040000) ;
277 /* set sector offsets for top boot block type */
278 i = info->sector_count - 1;
279 info->start[i--] = base + info->size - 0x00010000;
280 info->start[i--] = base + info->size - 0x00018000;
281 info->start[i--] = base + info->size - 0x00020000;
282 for (; i >= 0; i--) {
283 info->start[i] = base + i * 0x00040000;
287 /* check for protected sectors */
288 for (i = 0; i < info->sector_count; i++) {
289 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
290 /* D0 = 1 if protected */
291 addr = (volatile unsigned long *)(info->start[i]);
292 info->protect[i] = addr[4] & 1 ;
296 * Prevent writes to uninitialized FLASH.
298 if (info->flash_id != FLASH_UNKNOWN) {
299 addr = (volatile unsigned long *)info->start[0];
301 *addr = 0xF0F0F0F0; /* reset bank */
308 /*-----------------------------------------------------------------------
311 int flash_erase (flash_info_t *info, int s_first, int s_last)
313 vu_long *addr = (vu_long*)(info->start[0]);
314 int flag, prot, sect, l_sect;
315 ulong start, now, last;
317 if ((s_first < 0) || (s_first > s_last)) {
318 if (info->flash_id == FLASH_UNKNOWN) {
319 printf ("- missing\n");
321 printf ("- no sectors to erase\n");
326 if ((info->flash_id == FLASH_UNKNOWN) ||
327 (info->flash_id > FLASH_AMD_COMP)) {
328 printf ("Can't erase unknown flash type %08lx - aborted\n",
334 for (sect=s_first; sect<=s_last; ++sect) {
335 if (info->protect[sect]) {
341 printf ("- Warning: %d protected sectors will not be erased!\n",
349 /* Disable interrupts which might cause a timeout here */
350 flag = disable_interrupts();
352 addr[0xAAA] = 0xAAAAAAAA;
353 addr[0x555] = 0x55555555;
354 addr[0xAAA] = 0x80808080;
355 addr[0xAAA] = 0xAAAAAAAA;
356 addr[0x555] = 0x55555555;
358 /* Start erase on unprotected sectors */
359 for (sect = s_first; sect<=s_last; sect++) {
360 if (info->protect[sect] == 0) { /* not protected */
361 addr = (vu_long *)(info->start[sect]) ;
362 addr[0] = 0x30303030 ;
367 /* re-enable interrupts if necessary */
371 /* wait at least 80us - let's wait 1 ms */
375 * We wait for the last triggered sector
380 start = get_timer (0);
382 addr = (vu_long *)(info->start[l_sect]);
383 while ((addr[0] & 0x80808080) != 0x80808080) {
384 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
385 printf ("Timeout\n");
388 /* show that we're waiting */
389 if ((now - last) > 1000) { /* every second */
396 /* reset to read mode */
397 addr = (vu_long *)info->start[0];
398 addr[0] = 0xF0F0F0F0; /* reset bank */
404 /*-----------------------------------------------------------------------
405 * Copy memory to flash, returns:
408 * 2 - Flash not erased
411 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
416 wp = (addr & ~3); /* get lower word aligned address */
419 * handle unaligned start bytes
421 if ((l = addr - wp) != 0) {
423 for (i=0, cp=wp; i<l; ++i, ++cp) {
424 data = (data << 8) | (*(uchar *)cp);
426 for (; i<4 && cnt>0; ++i) {
427 data = (data << 8) | *src++;
431 for (; cnt==0 && i<4; ++i, ++cp) {
432 data = (data << 8) | (*(uchar *)cp);
435 if ((rc = write_word(info, wp, data)) != 0) {
442 * handle word aligned part
446 for (i=0; i<4; ++i) {
447 data = (data << 8) | *src++;
449 if ((rc = write_word(info, wp, data)) != 0) {
461 * handle unaligned tail bytes
464 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
465 data = (data << 8) | *src++;
468 for (; i<4; ++i, ++cp) {
469 data = (data << 8) | (*(uchar *)cp);
472 return (write_word(info, wp, data));
475 /*-----------------------------------------------------------------------
476 * Write a word to Flash, returns:
479 * 2 - Flash not erased
481 static int write_word (flash_info_t *info, ulong dest, ulong data)
483 vu_long *addr = (vu_long *)(info->start[0]);
487 /* Check if Flash is (sufficiently) erased */
488 if ((*((vu_long *)dest) & data) != data) {
491 /* Disable interrupts which might cause a timeout here */
492 flag = disable_interrupts();
494 addr[0xAAA] = 0xAAAAAAAA;
495 addr[0x555] = 0x55555555;
496 addr[0xAAA] = 0xA0A0A0A0;
498 *((vu_long *)dest) = data;
500 /* re-enable interrupts if necessary */
504 /* data polling for D7 */
505 start = get_timer (0);
506 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
507 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
514 /*-----------------------------------------------------------------------