2 * (C) Copyright 2000-2004
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_byte(flash_info_t * info, ulong dest, uchar 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;
50 size = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
52 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
53 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
57 /* Remap FLASH according to real size */
58 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
59 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
61 /* Re-do sizing to get full correct info */
62 size = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
64 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
66 /* monitor protection ON by default */
67 flash_protect(FLAG_PROTECT_SET,
68 CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
71 flash_protect ( FLAG_PROTECT_SET,
73 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
76 #ifdef CONFIG_ENV_ADDR_REDUND
77 flash_protect ( FLAG_PROTECT_SET,
78 CONFIG_ENV_ADDR_REDUND,
79 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
83 flash_info[0].size = size;
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_TYPEMASK) == FLASH_AM040) {
96 for (i = 0; i < info->sector_count; i++) {
97 info->start[i] = base + (i * 0x00010000);
99 } else if (info->flash_id & FLASH_BTYPE) {
100 /* set sector offsets for bottom boot block type */
101 info->start[0] = base + 0x00000000;
102 info->start[1] = base + 0x00004000;
103 info->start[2] = base + 0x00006000;
104 info->start[3] = base + 0x00008000;
105 for (i = 4; i < info->sector_count; i++) {
106 info->start[i] = base + (i * 0x00010000) - 0x00030000;
109 /* set sector offsets for top boot block type */
110 i = info->sector_count - 1;
111 info->start[i--] = base + info->size - 0x00004000;
112 info->start[i--] = base + info->size - 0x00006000;
113 info->start[i--] = base + info->size - 0x00008000;
114 for (; i >= 0; i--) {
115 info->start[i] = base + i * 0x00010000;
120 /*-----------------------------------------------------------------------
122 void flash_print_info(flash_info_t * info)
126 if (info->flash_id == FLASH_UNKNOWN) {
127 printf("missing or unknown FLASH type\n");
131 switch (info->flash_id & FLASH_VENDMASK) {
142 printf("Unknown Vendor ");
146 switch (info->flash_id & FLASH_TYPEMASK) {
148 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
151 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
154 printf("AM29LV400T (4 Mbit, top boot sector)\n");
157 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
160 printf("AM29LV800T (8 Mbit, top boot sector)\n");
163 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
166 printf("AM29LV160T (16 Mbit, top boot sector)\n");
169 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
172 printf("AM29LV320T (32 Mbit, top boot sector)\n");
175 printf("Unknown Chip Type\n");
179 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
181 printf(" Sector Start Addresses:");
182 for (i = 0; i < info->sector_count; ++i) {
185 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
190 /*-----------------------------------------------------------------------
194 /*-----------------------------------------------------------------------
198 * The following code cannot be run from FLASH!
201 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
206 vu_char *caddr = (vu_char *) addr;
207 ulong base = (ulong) addr;
209 /* Write auto select command: read Manufacturer ID */
210 caddr[0x0555] = 0xAA;
211 caddr[0x02AA] = 0x55;
212 caddr[0x0555] = 0x90;
216 case (AMD_MANUFACT & 0xFF):
217 info->flash_id = FLASH_MAN_AMD;
219 case (FUJ_MANUFACT & 0xFF):
220 info->flash_id = FLASH_MAN_FUJ;
222 case (MX_MANUFACT & 0xFF):
223 info->flash_id = FLASH_MAN_MX;
225 case (STM_MANUFACT & 0xFF):
226 info->flash_id = FLASH_MAN_STM;
229 info->flash_id = FLASH_UNKNOWN;
230 info->sector_count = 0;
232 return (0); /* no or unknown flash */
235 pid = caddr[1]; /* device ID */
237 case (AMD_ID_LV400T & 0xFF):
238 info->flash_id += FLASH_AM400T;
239 info->sector_count = 11;
240 info->size = 0x00080000;
241 break; /* => 512 kB */
243 case (AMD_ID_LV400B & 0xFF):
244 info->flash_id += FLASH_AM400B;
245 info->sector_count = 11;
246 info->size = 0x00080000;
247 break; /* => 512 kB */
249 case (AMD_ID_LV800T & 0xFF):
250 info->flash_id += FLASH_AM800T;
251 info->sector_count = 19;
252 info->size = 0x00100000;
255 case (AMD_ID_LV800B & 0xFF):
256 info->flash_id += FLASH_AM800B;
257 info->sector_count = 19;
258 info->size = 0x00100000;
261 case (AMD_ID_LV160T & 0xFF):
262 info->flash_id += FLASH_AM160T;
263 info->sector_count = 35;
264 info->size = 0x00200000;
267 case (AMD_ID_LV160B & 0xFF):
268 info->flash_id += FLASH_AM160B;
269 info->sector_count = 35;
270 info->size = 0x00200000;
273 case (AMD_ID_LV040B & 0xFF):
274 info->flash_id += FLASH_AM040;
275 info->sector_count = 8;
276 info->size = 0x00080000;
279 case (STM_ID_M29W040B & 0xFF):
280 info->flash_id += FLASH_AM040;
281 info->sector_count = 8;
282 info->size = 0x00080000;
285 #if 0 /* enable when device IDs are available */
286 case (AMD_ID_LV320T & 0xFF):
287 info->flash_id += FLASH_AM320T;
288 info->sector_count = 67;
289 info->size = 0x00400000;
292 case (AMD_ID_LV320B & 0xFF):
293 info->flash_id += FLASH_AM320B;
294 info->sector_count = 67;
295 info->size = 0x00400000;
299 info->flash_id = FLASH_UNKNOWN;
300 return (0); /* => no or unknown flash */
305 /* set up sector start address table */
306 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
307 for (i = 0; i < info->sector_count; i++) {
308 info->start[i] = base + (i * 0x00010000);
310 } else if (info->flash_id & FLASH_BTYPE) {
311 /* set sector offsets for bottom boot block type */
312 info->start[0] = base + 0x00000000;
313 info->start[1] = base + 0x00004000;
314 info->start[2] = base + 0x00006000;
315 info->start[3] = base + 0x00008000;
316 for (i = 4; i < info->sector_count; i++) {
317 info->start[i] = base + (i * 0x00010000) - 0x00030000;
320 /* set sector offsets for top boot block type */
321 i = info->sector_count - 1;
322 info->start[i--] = base + info->size - 0x00004000;
323 info->start[i--] = base + info->size - 0x00006000;
324 info->start[i--] = base + info->size - 0x00008000;
325 for (; i >= 0; i--) {
326 info->start[i] = base + i * 0x00010000;
330 /* check for protected sectors */
331 for (i = 0; i < info->sector_count; i++) {
332 /* read sector protection: D0 = 1 if protected */
333 caddr = (volatile unsigned char *)(info->start[i]);
334 info->protect[i] = caddr[2] & 1;
338 * Prevent writes to uninitialized FLASH.
340 if (info->flash_id != FLASH_UNKNOWN) {
341 caddr = (vu_char *) info->start[0];
343 caddr[0x0555] = 0xAA;
344 caddr[0x02AA] = 0x55;
345 caddr[0x0555] = 0xF0;
354 /*-----------------------------------------------------------------------
357 int flash_erase(flash_info_t * info, int s_first, int s_last)
359 vu_char *addr = (vu_char *) (info->start[0]);
360 int flag, prot, sect, l_sect;
361 ulong start, now, last;
363 if ((s_first < 0) || (s_first > s_last)) {
364 if (info->flash_id == FLASH_UNKNOWN) {
365 printf("- missing\n");
367 printf("- no sectors to erase\n");
372 if ((info->flash_id == FLASH_UNKNOWN) ||
373 (info->flash_id > FLASH_AMD_COMP)) {
374 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
379 for (sect = s_first; sect <= s_last; ++sect) {
380 if (info->protect[sect]) {
386 printf("- Warning: %d protected sectors will not be erased!\n", prot);
393 /* Disable interrupts which might cause a timeout here */
394 flag = disable_interrupts();
402 /* Start erase on unprotected sectors */
403 for (sect = s_first; sect <= s_last; sect++) {
404 if (info->protect[sect] == 0) { /* not protected */
405 addr = (vu_char *) (info->start[sect]);
411 /* re-enable interrupts if necessary */
415 /* wait at least 80us - let's wait 1 ms */
419 * We wait for the last triggered sector
424 start = get_timer(0);
426 addr = (vu_char *) (info->start[l_sect]);
427 while ((addr[0] & 0x80) != 0x80) {
428 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
432 /* show that we're waiting */
433 if ((now - last) > 1000) { /* every second */
440 /* reset to read mode */
441 addr = (vu_char *) info->start[0];
442 addr[0] = 0xF0; /* reset bank */
448 /*-----------------------------------------------------------------------
449 * Copy memory to flash, returns:
452 * 2 - Flash not erased
455 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
460 if ((rc = write_byte(info, addr++, *src++)) != 0) {
469 /*-----------------------------------------------------------------------
470 * Write a word to Flash, returns:
473 * 2 - Flash not erased
475 static int write_byte(flash_info_t * info, ulong dest, uchar data)
477 vu_char *addr = (vu_char *) (info->start[0]);
481 /* Check if Flash is (sufficiently) erased */
482 if ((*((vu_char *) dest) & data) != data) {
485 /* Disable interrupts which might cause a timeout here */
486 flag = disable_interrupts();
492 *((vu_char *) dest) = data;
494 /* re-enable interrupts if necessary */
498 /* data polling for D7 */
499 start = get_timer(0);
500 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
501 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {