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;
44 #if CONFIG_NETPHONE_VERSION == 2
49 /* Init: no FLASHes known */
50 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
51 flash_info[i].flash_id = FLASH_UNKNOWN;
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",
60 /* Remap FLASH according to real size */
61 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
62 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
64 /* Re-do sizing to get full correct info */
65 size = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
67 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
69 /* monitor protection ON by default */
70 flash_protect(FLAG_PROTECT_SET,
71 CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
74 flash_protect ( FLAG_PROTECT_SET,
76 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
79 #ifdef CONFIG_ENV_ADDR_REDUND
80 flash_protect ( FLAG_PROTECT_SET,
81 CONFIG_ENV_ADDR_REDUND,
82 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
86 flash_info[0].size = size;
88 #if CONFIG_NETPHONE_VERSION == 2
89 size1 = flash_get_size((vu_long *) FLASH_BASE4_PRELIM, &flash_info[1]);
91 if (flash_info[1].flash_id == FLASH_UNKNOWN)
92 printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n", size1, size1 << 20);
94 /* Remap FLASH according to real size */
95 memctl->memc_or4 = CONFIG_SYS_OR_TIMING_FLASH | (-size1 & 0xFFFF8000);
96 memctl->memc_br4 = (CONFIG_SYS_FLASH_BASE4 & BR_BA_MSK) | (memctl->memc_br4 & ~(BR_BA_MSK));
98 /* Re-do sizing to get full correct info */
99 size1 = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE4, &flash_info[1]);
101 flash_get_offsets(CONFIG_SYS_FLASH_BASE4, &flash_info[1]);
105 memctl->memc_br4 &= ~BR_V;
111 /*-----------------------------------------------------------------------
113 static void flash_get_offsets(ulong base, flash_info_t * info)
117 /* set up sector start address table */
118 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
119 for (i = 0; i < info->sector_count; i++) {
120 info->start[i] = base + (i * 0x00010000);
122 } else if (info->flash_id & FLASH_BTYPE) {
123 /* set sector offsets for bottom boot block type */
124 info->start[0] = base + 0x00000000;
125 info->start[1] = base + 0x00004000;
126 info->start[2] = base + 0x00006000;
127 info->start[3] = base + 0x00008000;
128 for (i = 4; i < info->sector_count; i++) {
129 info->start[i] = base + (i * 0x00010000) - 0x00030000;
132 /* set sector offsets for top boot block type */
133 i = info->sector_count - 1;
134 info->start[i--] = base + info->size - 0x00004000;
135 info->start[i--] = base + info->size - 0x00006000;
136 info->start[i--] = base + info->size - 0x00008000;
137 for (; i >= 0; i--) {
138 info->start[i] = base + i * 0x00010000;
143 /*-----------------------------------------------------------------------
145 void flash_print_info(flash_info_t * info)
149 if (info->flash_id == FLASH_UNKNOWN) {
150 printf("missing or unknown FLASH type\n");
154 switch (info->flash_id & FLASH_VENDMASK) {
165 printf("Unknown Vendor ");
169 switch (info->flash_id & FLASH_TYPEMASK) {
171 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
174 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
177 printf("AM29LV400T (4 Mbit, top boot sector)\n");
180 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
183 printf("AM29LV800T (8 Mbit, top boot sector)\n");
186 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
189 printf("AM29LV160T (16 Mbit, top boot sector)\n");
192 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
195 printf("AM29LV320T (32 Mbit, top boot sector)\n");
198 printf("Unknown Chip Type\n");
202 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
204 printf(" Sector Start Addresses:");
205 for (i = 0; i < info->sector_count; ++i) {
208 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
213 /*-----------------------------------------------------------------------
217 /*-----------------------------------------------------------------------
221 * The following code cannot be run from FLASH!
224 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
229 vu_char *caddr = (vu_char *) addr;
230 ulong base = (ulong) addr;
232 /* Write auto select command: read Manufacturer ID */
233 caddr[0x0555] = 0xAA;
234 caddr[0x02AA] = 0x55;
235 caddr[0x0555] = 0x90;
239 case (AMD_MANUFACT & 0xFF):
240 info->flash_id = FLASH_MAN_AMD;
242 case (FUJ_MANUFACT & 0xFF):
243 info->flash_id = FLASH_MAN_FUJ;
245 case (MX_MANUFACT & 0xFF):
246 info->flash_id = FLASH_MAN_MX;
248 case (STM_MANUFACT & 0xFF):
249 info->flash_id = FLASH_MAN_STM;
252 info->flash_id = FLASH_UNKNOWN;
253 info->sector_count = 0;
255 return (0); /* no or unknown flash */
258 pid = caddr[1]; /* device ID */
260 case (AMD_ID_LV400T & 0xFF):
261 info->flash_id += FLASH_AM400T;
262 info->sector_count = 11;
263 info->size = 0x00080000;
264 break; /* => 512 kB */
266 case (AMD_ID_LV400B & 0xFF):
267 info->flash_id += FLASH_AM400B;
268 info->sector_count = 11;
269 info->size = 0x00080000;
270 break; /* => 512 kB */
272 case (AMD_ID_LV800T & 0xFF):
273 info->flash_id += FLASH_AM800T;
274 info->sector_count = 19;
275 info->size = 0x00100000;
278 case (AMD_ID_LV800B & 0xFF):
279 info->flash_id += FLASH_AM800B;
280 info->sector_count = 19;
281 info->size = 0x00100000;
284 case (AMD_ID_LV160T & 0xFF):
285 info->flash_id += FLASH_AM160T;
286 info->sector_count = 35;
287 info->size = 0x00200000;
290 case (AMD_ID_LV160B & 0xFF):
291 info->flash_id += FLASH_AM160B;
292 info->sector_count = 35;
293 info->size = 0x00200000;
296 case (AMD_ID_LV040B & 0xFF):
297 info->flash_id += FLASH_AM040;
298 info->sector_count = 8;
299 info->size = 0x00080000;
302 case (STM_ID_M29W040B & 0xFF):
303 info->flash_id += FLASH_AM040;
304 info->sector_count = 8;
305 info->size = 0x00080000;
308 #if 0 /* enable when device IDs are available */
309 case (AMD_ID_LV320T & 0xFF):
310 info->flash_id += FLASH_AM320T;
311 info->sector_count = 67;
312 info->size = 0x00400000;
315 case (AMD_ID_LV320B & 0xFF):
316 info->flash_id += FLASH_AM320B;
317 info->sector_count = 67;
318 info->size = 0x00400000;
322 info->flash_id = FLASH_UNKNOWN;
323 return (0); /* => no or unknown flash */
328 /* set up sector start address table */
329 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
330 for (i = 0; i < info->sector_count; i++) {
331 info->start[i] = base + (i * 0x00010000);
333 } else if (info->flash_id & FLASH_BTYPE) {
334 /* set sector offsets for bottom boot block type */
335 info->start[0] = base + 0x00000000;
336 info->start[1] = base + 0x00004000;
337 info->start[2] = base + 0x00006000;
338 info->start[3] = base + 0x00008000;
339 for (i = 4; i < info->sector_count; i++) {
340 info->start[i] = base + (i * 0x00010000) - 0x00030000;
343 /* set sector offsets for top boot block type */
344 i = info->sector_count - 1;
345 info->start[i--] = base + info->size - 0x00004000;
346 info->start[i--] = base + info->size - 0x00006000;
347 info->start[i--] = base + info->size - 0x00008000;
348 for (; i >= 0; i--) {
349 info->start[i] = base + i * 0x00010000;
353 /* check for protected sectors */
354 for (i = 0; i < info->sector_count; i++) {
355 /* read sector protection: D0 = 1 if protected */
356 caddr = (volatile unsigned char *)(info->start[i]);
357 info->protect[i] = caddr[2] & 1;
361 * Prevent writes to uninitialized FLASH.
363 if (info->flash_id != FLASH_UNKNOWN) {
364 caddr = (vu_char *) info->start[0];
366 caddr[0x0555] = 0xAA;
367 caddr[0x02AA] = 0x55;
368 caddr[0x0555] = 0xF0;
377 /*-----------------------------------------------------------------------
380 int flash_erase(flash_info_t * info, int s_first, int s_last)
382 vu_char *addr = (vu_char *) (info->start[0]);
383 int flag, prot, sect, l_sect;
384 ulong start, now, last;
386 if ((s_first < 0) || (s_first > s_last)) {
387 if (info->flash_id == FLASH_UNKNOWN) {
388 printf("- missing\n");
390 printf("- no sectors to erase\n");
395 if ((info->flash_id == FLASH_UNKNOWN) ||
396 (info->flash_id > FLASH_AMD_COMP)) {
397 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
402 for (sect = s_first; sect <= s_last; ++sect) {
403 if (info->protect[sect]) {
409 printf("- Warning: %d protected sectors will not be erased!\n", prot);
416 /* Disable interrupts which might cause a timeout here */
417 flag = disable_interrupts();
425 /* Start erase on unprotected sectors */
426 for (sect = s_first; sect <= s_last; sect++) {
427 if (info->protect[sect] == 0) { /* not protected */
428 addr = (vu_char *) (info->start[sect]);
434 /* re-enable interrupts if necessary */
438 /* wait at least 80us - let's wait 1 ms */
442 * We wait for the last triggered sector
447 start = get_timer(0);
449 addr = (vu_char *) (info->start[l_sect]);
450 while ((addr[0] & 0x80) != 0x80) {
451 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
455 /* show that we're waiting */
456 if ((now - last) > 1000) { /* every second */
463 /* reset to read mode */
464 addr = (vu_char *) info->start[0];
465 addr[0] = 0xF0; /* reset bank */
471 /*-----------------------------------------------------------------------
472 * Copy memory to flash, returns:
475 * 2 - Flash not erased
478 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
483 if ((rc = write_byte(info, addr++, *src++)) != 0) {
492 /*-----------------------------------------------------------------------
493 * Write a word to Flash, returns:
496 * 2 - Flash not erased
498 static int write_byte(flash_info_t * info, ulong dest, uchar data)
500 vu_char *addr = (vu_char *) (info->start[0]);
504 /* Check if Flash is (sufficiently) erased */
505 if ((*((vu_char *) dest) & data) != data) {
508 /* Disable interrupts which might cause a timeout here */
509 flag = disable_interrupts();
515 *((vu_char *) dest) = data;
517 /* re-enable interrupts if necessary */
521 /* data polling for D7 */
522 start = get_timer(0);
523 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
524 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {