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[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_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 *) CFG_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 < CFG_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 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
62 memctl->memc_br0 = (CFG_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 *) CFG_FLASH_BASE, &flash_info[0]);
67 flash_get_offsets(CFG_FLASH_BASE, &flash_info[0]);
69 /* monitor protection ON by default */
70 flash_protect(FLAG_PROTECT_SET,
71 CFG_FLASH_BASE, CFG_FLASH_BASE + monitor_flash_len - 1,
74 flash_protect ( FLAG_PROTECT_SET,
76 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
79 #ifdef CFG_ENV_ADDR_REDUND
80 flash_protect ( FLAG_PROTECT_SET,
82 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 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 && size1 > 0) {
92 printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n", size1, size1 << 20);
95 /* Remap FLASH according to real size */
96 memctl->memc_or4 = CFG_OR_TIMING_FLASH | (-size1 & 0xFFFF8000);
97 memctl->memc_br4 = (CFG_FLASH_BASE4 & BR_BA_MSK) | (memctl->memc_br4 & ~(BR_BA_MSK));
99 /* Re-do sizing to get full correct info */
100 size1 = flash_get_size((vu_long *) CFG_FLASH_BASE4, &flash_info[1]);
102 flash_get_offsets(CFG_FLASH_BASE4, &flash_info[1]);
110 /*-----------------------------------------------------------------------
112 static void flash_get_offsets(ulong base, flash_info_t * info)
116 /* set up sector start address table */
117 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
118 for (i = 0; i < info->sector_count; i++) {
119 info->start[i] = base + (i * 0x00010000);
121 } else if (info->flash_id & FLASH_BTYPE) {
122 /* set sector offsets for bottom boot block type */
123 info->start[0] = base + 0x00000000;
124 info->start[1] = base + 0x00004000;
125 info->start[2] = base + 0x00006000;
126 info->start[3] = base + 0x00008000;
127 for (i = 4; i < info->sector_count; i++) {
128 info->start[i] = base + (i * 0x00010000) - 0x00030000;
131 /* set sector offsets for top boot block type */
132 i = info->sector_count - 1;
133 info->start[i--] = base + info->size - 0x00004000;
134 info->start[i--] = base + info->size - 0x00006000;
135 info->start[i--] = base + info->size - 0x00008000;
136 for (; i >= 0; i--) {
137 info->start[i] = base + i * 0x00010000;
142 /*-----------------------------------------------------------------------
144 void flash_print_info(flash_info_t * info)
148 if (info->flash_id == FLASH_UNKNOWN) {
149 printf("missing or unknown FLASH type\n");
153 switch (info->flash_id & FLASH_VENDMASK) {
164 printf("Unknown Vendor ");
168 switch (info->flash_id & FLASH_TYPEMASK) {
170 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
173 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
176 printf("AM29LV400T (4 Mbit, top boot sector)\n");
179 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
182 printf("AM29LV800T (8 Mbit, top boot sector)\n");
185 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
188 printf("AM29LV160T (16 Mbit, top boot sector)\n");
191 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
194 printf("AM29LV320T (32 Mbit, top boot sector)\n");
197 printf("Unknown Chip Type\n");
201 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
203 printf(" Sector Start Addresses:");
204 for (i = 0; i < info->sector_count; ++i) {
207 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
212 /*-----------------------------------------------------------------------
216 /*-----------------------------------------------------------------------
220 * The following code cannot be run from FLASH!
223 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
228 vu_char *caddr = (vu_char *) addr;
229 ulong base = (ulong) addr;
231 /* Write auto select command: read Manufacturer ID */
232 caddr[0x0555] = 0xAA;
233 caddr[0x02AA] = 0x55;
234 caddr[0x0555] = 0x90;
238 case (AMD_MANUFACT & 0xFF):
239 info->flash_id = FLASH_MAN_AMD;
241 case (FUJ_MANUFACT & 0xFF):
242 info->flash_id = FLASH_MAN_FUJ;
244 case (MX_MANUFACT & 0xFF):
245 info->flash_id = FLASH_MAN_MX;
247 case (STM_MANUFACT & 0xFF):
248 info->flash_id = FLASH_MAN_STM;
251 info->flash_id = FLASH_UNKNOWN;
252 info->sector_count = 0;
254 return (0); /* no or unknown flash */
257 pid = caddr[1]; /* device ID */
259 case (AMD_ID_LV400T & 0xFF):
260 info->flash_id += FLASH_AM400T;
261 info->sector_count = 11;
262 info->size = 0x00080000;
263 break; /* => 512 kB */
265 case (AMD_ID_LV400B & 0xFF):
266 info->flash_id += FLASH_AM400B;
267 info->sector_count = 11;
268 info->size = 0x00080000;
269 break; /* => 512 kB */
271 case (AMD_ID_LV800T & 0xFF):
272 info->flash_id += FLASH_AM800T;
273 info->sector_count = 19;
274 info->size = 0x00100000;
277 case (AMD_ID_LV800B & 0xFF):
278 info->flash_id += FLASH_AM800B;
279 info->sector_count = 19;
280 info->size = 0x00100000;
283 case (AMD_ID_LV160T & 0xFF):
284 info->flash_id += FLASH_AM160T;
285 info->sector_count = 35;
286 info->size = 0x00200000;
289 case (AMD_ID_LV160B & 0xFF):
290 info->flash_id += FLASH_AM160B;
291 info->sector_count = 35;
292 info->size = 0x00200000;
295 case (AMD_ID_LV040B & 0xFF):
296 info->flash_id += FLASH_AM040;
297 info->sector_count = 8;
298 info->size = 0x00080000;
301 case (STM_ID_M29W040B & 0xFF):
302 info->flash_id += FLASH_AM040;
303 info->sector_count = 8;
304 info->size = 0x00080000;
307 #if 0 /* enable when device IDs are available */
308 case (AMD_ID_LV320T & 0xFF):
309 info->flash_id += FLASH_AM320T;
310 info->sector_count = 67;
311 info->size = 0x00400000;
314 case (AMD_ID_LV320B & 0xFF):
315 info->flash_id += FLASH_AM320B;
316 info->sector_count = 67;
317 info->size = 0x00400000;
321 info->flash_id = FLASH_UNKNOWN;
322 return (0); /* => no or unknown flash */
327 /* set up sector start address table */
328 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
329 for (i = 0; i < info->sector_count; i++) {
330 info->start[i] = base + (i * 0x00010000);
332 } else if (info->flash_id & FLASH_BTYPE) {
333 /* set sector offsets for bottom boot block type */
334 info->start[0] = base + 0x00000000;
335 info->start[1] = base + 0x00004000;
336 info->start[2] = base + 0x00006000;
337 info->start[3] = base + 0x00008000;
338 for (i = 4; i < info->sector_count; i++) {
339 info->start[i] = base + (i * 0x00010000) - 0x00030000;
342 /* set sector offsets for top boot block type */
343 i = info->sector_count - 1;
344 info->start[i--] = base + info->size - 0x00004000;
345 info->start[i--] = base + info->size - 0x00006000;
346 info->start[i--] = base + info->size - 0x00008000;
347 for (; i >= 0; i--) {
348 info->start[i] = base + i * 0x00010000;
352 /* check for protected sectors */
353 for (i = 0; i < info->sector_count; i++) {
354 /* read sector protection: D0 = 1 if protected */
355 caddr = (volatile unsigned char *)(info->start[i]);
356 info->protect[i] = caddr[2] & 1;
360 * Prevent writes to uninitialized FLASH.
362 if (info->flash_id != FLASH_UNKNOWN) {
363 caddr = (vu_char *) info->start[0];
365 caddr[0x0555] = 0xAA;
366 caddr[0x02AA] = 0x55;
367 caddr[0x0555] = 0xF0;
376 /*-----------------------------------------------------------------------
379 int flash_erase(flash_info_t * info, int s_first, int s_last)
381 vu_char *addr = (vu_char *) (info->start[0]);
382 int flag, prot, sect, l_sect;
383 ulong start, now, last;
385 if ((s_first < 0) || (s_first > s_last)) {
386 if (info->flash_id == FLASH_UNKNOWN) {
387 printf("- missing\n");
389 printf("- no sectors to erase\n");
394 if ((info->flash_id == FLASH_UNKNOWN) ||
395 (info->flash_id > FLASH_AMD_COMP)) {
396 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
401 for (sect = s_first; sect <= s_last; ++sect) {
402 if (info->protect[sect]) {
408 printf("- Warning: %d protected sectors will not be erased!\n", prot);
415 /* Disable interrupts which might cause a timeout here */
416 flag = disable_interrupts();
424 /* Start erase on unprotected sectors */
425 for (sect = s_first; sect <= s_last; sect++) {
426 if (info->protect[sect] == 0) { /* not protected */
427 addr = (vu_char *) (info->start[sect]);
433 /* re-enable interrupts if necessary */
437 /* wait at least 80us - let's wait 1 ms */
441 * We wait for the last triggered sector
446 start = get_timer(0);
448 addr = (vu_char *) (info->start[l_sect]);
449 while ((addr[0] & 0x80) != 0x80) {
450 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
454 /* show that we're waiting */
455 if ((now - last) > 1000) { /* every second */
462 /* reset to read mode */
463 addr = (vu_char *) info->start[0];
464 addr[0] = 0xF0; /* reset bank */
470 /*-----------------------------------------------------------------------
471 * Copy memory to flash, returns:
474 * 2 - Flash not erased
477 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
482 if ((rc = write_byte(info, addr++, *src++)) != 0) {
491 /*-----------------------------------------------------------------------
492 * Write a word to Flash, returns:
495 * 2 - Flash not erased
497 static int write_byte(flash_info_t * info, ulong dest, uchar data)
499 vu_char *addr = (vu_char *) (info->start[0]);
503 /* Check if Flash is (sufficiently) erased */
504 if ((*((vu_char *) dest) & data) != data) {
507 /* Disable interrupts which might cause a timeout here */
508 flag = disable_interrupts();
514 *((vu_char *) dest) = data;
516 /* re-enable interrupts if necessary */
520 /* data polling for D7 */
521 start = get_timer(0);
522 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
523 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {