Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / board / netta2 / flash.c
bloba1c87f513102bdf019b5f153e265d779cc03f562
1 /*
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
6 * project.
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,
21 * MA 02111-1307 USA
24 #include <common.h>
25 #include <mpc8xx.h>
27 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
30 * Functions
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;
43 unsigned long size;
44 int i;
46 /* Init: no FLASHes known */
47 for (i = 0; i < CFG_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",
54 size, size << 20);
57 /* Remap FLASH according to real size */
58 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
59 memctl->memc_br0 = (CFG_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 *) CFG_FLASH_BASE, &flash_info[0]);
64 flash_get_offsets(CFG_FLASH_BASE, &flash_info[0]);
66 /* monitor protection ON by default */
67 flash_protect(FLAG_PROTECT_SET,
68 CFG_FLASH_BASE, CFG_FLASH_BASE + monitor_flash_len - 1,
69 &flash_info[0]);
71 flash_protect ( FLAG_PROTECT_SET,
72 CFG_ENV_ADDR,
73 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
74 &flash_info[0]);
76 #ifdef CFG_ENV_ADDR_REDUND
77 flash_protect ( FLAG_PROTECT_SET,
78 CFG_ENV_ADDR_REDUND,
79 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
80 &flash_info[0]);
81 #endif
83 flash_info[0].size = size;
85 return (size);
88 /*-----------------------------------------------------------------------
90 static void flash_get_offsets(ulong base, flash_info_t * info)
92 int i;
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;
108 } else {
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)
124 int i;
126 if (info->flash_id == FLASH_UNKNOWN) {
127 printf("missing or unknown FLASH type\n");
128 return;
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_AMD:
133 printf("AMD ");
134 break;
135 case FLASH_MAN_FUJ:
136 printf("FUJITSU ");
137 break;
138 case FLASH_MAN_MX:
139 printf("MXIC ");
140 break;
141 default:
142 printf("Unknown Vendor ");
143 break;
146 switch (info->flash_id & FLASH_TYPEMASK) {
147 case FLASH_AM040:
148 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
149 break;
150 case FLASH_AM400B:
151 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
152 break;
153 case FLASH_AM400T:
154 printf("AM29LV400T (4 Mbit, top boot sector)\n");
155 break;
156 case FLASH_AM800B:
157 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
158 break;
159 case FLASH_AM800T:
160 printf("AM29LV800T (8 Mbit, top boot sector)\n");
161 break;
162 case FLASH_AM160B:
163 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
164 break;
165 case FLASH_AM160T:
166 printf("AM29LV160T (16 Mbit, top boot sector)\n");
167 break;
168 case FLASH_AM320B:
169 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
170 break;
171 case FLASH_AM320T:
172 printf("AM29LV320T (32 Mbit, top boot sector)\n");
173 break;
174 default:
175 printf("Unknown Chip Type\n");
176 break;
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) {
183 if ((i % 5) == 0)
184 printf("\n ");
185 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
187 printf("\n");
190 /*-----------------------------------------------------------------------
194 /*-----------------------------------------------------------------------
198 * The following code cannot be run from FLASH!
201 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
203 short i;
204 uchar mid;
205 uchar pid;
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;
214 mid = caddr[0];
215 switch (mid) {
216 case (AMD_MANUFACT & 0xFF):
217 info->flash_id = FLASH_MAN_AMD;
218 break;
219 case (FUJ_MANUFACT & 0xFF):
220 info->flash_id = FLASH_MAN_FUJ;
221 break;
222 case (MX_MANUFACT & 0xFF):
223 info->flash_id = FLASH_MAN_MX;
224 break;
225 case (STM_MANUFACT & 0xFF):
226 info->flash_id = FLASH_MAN_STM;
227 break;
228 default:
229 info->flash_id = FLASH_UNKNOWN;
230 info->sector_count = 0;
231 info->size = 0;
232 return (0); /* no or unknown flash */
235 pid = caddr[1]; /* device ID */
236 switch (pid) {
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;
253 break; /* => 1 MB */
255 case (AMD_ID_LV800B & 0xFF):
256 info->flash_id += FLASH_AM800B;
257 info->sector_count = 19;
258 info->size = 0x00100000;
259 break; /* => 1 MB */
261 case (AMD_ID_LV160T & 0xFF):
262 info->flash_id += FLASH_AM160T;
263 info->sector_count = 35;
264 info->size = 0x00200000;
265 break; /* => 2 MB */
267 case (AMD_ID_LV160B & 0xFF):
268 info->flash_id += FLASH_AM160B;
269 info->sector_count = 35;
270 info->size = 0x00200000;
271 break; /* => 2 MB */
273 case (AMD_ID_LV040B & 0xFF):
274 info->flash_id += FLASH_AM040;
275 info->sector_count = 8;
276 info->size = 0x00080000;
277 break;
279 case (STM_ID_M29W040B & 0xFF):
280 info->flash_id += FLASH_AM040;
281 info->sector_count = 8;
282 info->size = 0x00080000;
283 break;
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;
290 break; /* => 4 MB */
292 case (AMD_ID_LV320B & 0xFF):
293 info->flash_id += FLASH_AM320B;
294 info->sector_count = 67;
295 info->size = 0x00400000;
296 break; /* => 4 MB */
297 #endif
298 default:
299 info->flash_id = FLASH_UNKNOWN;
300 return (0); /* => no or unknown flash */
304 printf(" ");
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;
319 } else {
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;
347 udelay(20000);
350 return (info->size);
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");
366 } else {
367 printf("- no sectors to erase\n");
369 return 1;
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);
375 return 1;
378 prot = 0;
379 for (sect = s_first; sect <= s_last; ++sect) {
380 if (info->protect[sect]) {
381 prot++;
385 if (prot) {
386 printf("- Warning: %d protected sectors will not be erased!\n", prot);
387 } else {
388 printf("\n");
391 l_sect = -1;
393 /* Disable interrupts which might cause a timeout here */
394 flag = disable_interrupts();
396 addr[0x0555] = 0xAA;
397 addr[0x02AA] = 0x55;
398 addr[0x0555] = 0x80;
399 addr[0x0555] = 0xAA;
400 addr[0x02AA] = 0x55;
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]);
406 addr[0] = 0x30;
407 l_sect = sect;
411 /* re-enable interrupts if necessary */
412 if (flag)
413 enable_interrupts();
415 /* wait at least 80us - let's wait 1 ms */
416 udelay(1000);
419 * We wait for the last triggered sector
421 if (l_sect < 0)
422 goto DONE;
424 start = get_timer(0);
425 last = start;
426 addr = (vu_char *) (info->start[l_sect]);
427 while ((addr[0] & 0x80) != 0x80) {
428 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
429 printf("Timeout\n");
430 return 1;
432 /* show that we're waiting */
433 if ((now - last) > 1000) { /* every second */
434 putc('.');
435 last = now;
439 DONE:
440 /* reset to read mode */
441 addr = (vu_char *) info->start[0];
442 addr[0] = 0xF0; /* reset bank */
444 printf(" done\n");
445 return 0;
448 /*-----------------------------------------------------------------------
449 * Copy memory to flash, returns:
450 * 0 - OK
451 * 1 - write timeout
452 * 2 - Flash not erased
455 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
457 int rc;
459 while (cnt > 0) {
460 if ((rc = write_byte(info, addr++, *src++)) != 0) {
461 return (rc);
463 --cnt;
466 return (0);
469 /*-----------------------------------------------------------------------
470 * Write a word to Flash, returns:
471 * 0 - OK
472 * 1 - write timeout
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]);
478 ulong start;
479 int flag;
481 /* Check if Flash is (sufficiently) erased */
482 if ((*((vu_char *) dest) & data) != data) {
483 return (2);
485 /* Disable interrupts which might cause a timeout here */
486 flag = disable_interrupts();
488 addr[0x0555] = 0xAA;
489 addr[0x02AA] = 0x55;
490 addr[0x0555] = 0xA0;
492 *((vu_char *) dest) = data;
494 /* re-enable interrupts if necessary */
495 if (flag)
496 enable_interrupts();
498 /* data polling for D7 */
499 start = get_timer(0);
500 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
501 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
502 return (1);
505 return (0);