to make u-boot work for fat32 filesystem
[jz_uboot.git] / board / netvia / flash.c
blobd31f7707f74595e16b7584ec014c8457f8557e28
1 /*
2 * (C) Copyright 2000
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", size, size << 20);
56 /* Remap FLASH according to real size */
57 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
58 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
60 /* Re-do sizing to get full correct info */
61 size = flash_get_size((vu_long *) CFG_FLASH_BASE, &flash_info[0]);
63 flash_get_offsets(CFG_FLASH_BASE, &flash_info[0]);
65 /* monitor protection ON by default */
66 flash_protect(FLAG_PROTECT_SET,
67 CFG_FLASH_BASE, CFG_FLASH_BASE + monitor_flash_len - 1,
68 &flash_info[0]);
70 flash_protect ( FLAG_PROTECT_SET,
71 CFG_ENV_ADDR,
72 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
73 &flash_info[0]);
75 #ifdef CFG_ENV_ADDR_REDUND
76 flash_protect ( FLAG_PROTECT_SET,
77 CFG_ENV_ADDR_REDUND,
78 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
79 &flash_info[0]);
80 #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;
121 /*-----------------------------------------------------------------------
123 void flash_print_info(flash_info_t * info)
125 int i;
127 if (info->flash_id == FLASH_UNKNOWN) {
128 printf("missing or unknown FLASH type\n");
129 return;
132 switch (info->flash_id & FLASH_VENDMASK) {
133 case FLASH_MAN_AMD:
134 printf("AMD ");
135 break;
136 case FLASH_MAN_FUJ:
137 printf("FUJITSU ");
138 break;
139 case FLASH_MAN_MX:
140 printf("MXIC ");
141 break;
142 default:
143 printf("Unknown Vendor ");
144 break;
147 switch (info->flash_id & FLASH_TYPEMASK) {
148 case FLASH_AM040:
149 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
150 break;
151 case FLASH_AM400B:
152 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
153 break;
154 case FLASH_AM400T:
155 printf("AM29LV400T (4 Mbit, top boot sector)\n");
156 break;
157 case FLASH_AM800B:
158 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
159 break;
160 case FLASH_AM800T:
161 printf("AM29LV800T (8 Mbit, top boot sector)\n");
162 break;
163 case FLASH_AM160B:
164 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
165 break;
166 case FLASH_AM160T:
167 printf("AM29LV160T (16 Mbit, top boot sector)\n");
168 break;
169 case FLASH_AM320B:
170 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
171 break;
172 case FLASH_AM320T:
173 printf("AM29LV320T (32 Mbit, top boot sector)\n");
174 break;
175 default:
176 printf("Unknown Chip Type\n");
177 break;
180 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
182 printf(" Sector Start Addresses:");
183 for (i = 0; i < info->sector_count; ++i) {
184 if ((i % 5) == 0)
185 printf("\n ");
186 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
188 printf("\n");
191 /*-----------------------------------------------------------------------
195 /*-----------------------------------------------------------------------
199 * The following code cannot be run from FLASH!
202 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
204 short i;
205 uchar mid;
206 uchar pid;
207 vu_char *caddr = (vu_char *) addr;
208 ulong base = (ulong) addr;
211 /* Write auto select command: read Manufacturer ID */
212 caddr[0x0555] = 0xAA;
213 caddr[0x02AA] = 0x55;
214 caddr[0x0555] = 0x90;
216 mid = caddr[0];
217 switch (mid) {
218 case (AMD_MANUFACT & 0xFF):
219 info->flash_id = FLASH_MAN_AMD;
220 break;
221 case (FUJ_MANUFACT & 0xFF):
222 info->flash_id = FLASH_MAN_FUJ;
223 break;
224 case (MX_MANUFACT & 0xFF):
225 info->flash_id = FLASH_MAN_MX;
226 break;
227 case (STM_MANUFACT & 0xFF):
228 info->flash_id = FLASH_MAN_STM;
229 break;
230 default:
231 info->flash_id = FLASH_UNKNOWN;
232 info->sector_count = 0;
233 info->size = 0;
234 return (0); /* no or unknown flash */
237 pid = caddr[1]; /* device ID */
238 switch (pid) {
239 case (AMD_ID_LV400T & 0xFF):
240 info->flash_id += FLASH_AM400T;
241 info->sector_count = 11;
242 info->size = 0x00080000;
243 break; /* => 512 kB */
245 case (AMD_ID_LV400B & 0xFF):
246 info->flash_id += FLASH_AM400B;
247 info->sector_count = 11;
248 info->size = 0x00080000;
249 break; /* => 512 kB */
251 case (AMD_ID_LV800T & 0xFF):
252 info->flash_id += FLASH_AM800T;
253 info->sector_count = 19;
254 info->size = 0x00100000;
255 break; /* => 1 MB */
257 case (AMD_ID_LV800B & 0xFF):
258 info->flash_id += FLASH_AM800B;
259 info->sector_count = 19;
260 info->size = 0x00100000;
261 break; /* => 1 MB */
263 case (AMD_ID_LV160T & 0xFF):
264 info->flash_id += FLASH_AM160T;
265 info->sector_count = 35;
266 info->size = 0x00200000;
267 break; /* => 2 MB */
269 case (AMD_ID_LV160B & 0xFF):
270 info->flash_id += FLASH_AM160B;
271 info->sector_count = 35;
272 info->size = 0x00200000;
273 break; /* => 2 MB */
275 case (AMD_ID_LV040B & 0xFF):
276 info->flash_id += FLASH_AM040;
277 info->sector_count = 8;
278 info->size = 0x00080000;
279 break;
281 case (STM_ID_M29W040B & 0xFF):
282 info->flash_id += FLASH_AM040;
283 info->sector_count = 8;
284 info->size = 0x00080000;
285 break;
287 #if 0 /* enable when device IDs are available */
288 case (AMD_ID_LV320T & 0xFF):
289 info->flash_id += FLASH_AM320T;
290 info->sector_count = 67;
291 info->size = 0x00400000;
292 break; /* => 4 MB */
294 case (AMD_ID_LV320B & 0xFF):
295 info->flash_id += FLASH_AM320B;
296 info->sector_count = 67;
297 info->size = 0x00400000;
298 break; /* => 4 MB */
299 #endif
300 default:
301 info->flash_id = FLASH_UNKNOWN;
302 return (0); /* => no or unknown flash */
306 printf(" ");
307 /* set up sector start address table */
308 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
309 for (i = 0; i < info->sector_count; i++) {
310 info->start[i] = base + (i * 0x00010000);
312 } else if (info->flash_id & FLASH_BTYPE) {
313 /* set sector offsets for bottom boot block type */
314 info->start[0] = base + 0x00000000;
315 info->start[1] = base + 0x00004000;
316 info->start[2] = base + 0x00006000;
317 info->start[3] = base + 0x00008000;
318 for (i = 4; i < info->sector_count; i++) {
319 info->start[i] = base + (i * 0x00010000) - 0x00030000;
321 } else {
322 /* set sector offsets for top boot block type */
323 i = info->sector_count - 1;
324 info->start[i--] = base + info->size - 0x00004000;
325 info->start[i--] = base + info->size - 0x00006000;
326 info->start[i--] = base + info->size - 0x00008000;
327 for (; i >= 0; i--) {
328 info->start[i] = base + i * 0x00010000;
332 /* check for protected sectors */
333 for (i = 0; i < info->sector_count; i++) {
334 /* read sector protection: D0 = 1 if protected */
335 caddr = (volatile unsigned char *)(info->start[i]);
336 info->protect[i] = caddr[2] & 1;
340 * Prevent writes to uninitialized FLASH.
342 if (info->flash_id != FLASH_UNKNOWN) {
343 caddr = (vu_char *) info->start[0];
345 caddr[0x0555] = 0xAA;
346 caddr[0x02AA] = 0x55;
347 caddr[0x0555] = 0xF0;
349 udelay(20000);
352 return (info->size);
356 /*-----------------------------------------------------------------------
359 int flash_erase(flash_info_t * info, int s_first, int s_last)
361 vu_char *addr = (vu_char *) (info->start[0]);
362 int flag, prot, sect, l_sect;
363 ulong start, now, last;
365 if ((s_first < 0) || (s_first > s_last)) {
366 if (info->flash_id == FLASH_UNKNOWN) {
367 printf("- missing\n");
368 } else {
369 printf("- no sectors to erase\n");
371 return 1;
374 if ((info->flash_id == FLASH_UNKNOWN) ||
375 (info->flash_id > FLASH_AMD_COMP)) {
376 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
377 return 1;
380 prot = 0;
381 for (sect = s_first; sect <= s_last; ++sect) {
382 if (info->protect[sect]) {
383 prot++;
387 if (prot) {
388 printf("- Warning: %d protected sectors will not be erased!\n", prot);
389 } else {
390 printf("\n");
393 l_sect = -1;
395 /* Disable interrupts which might cause a timeout here */
396 flag = disable_interrupts();
398 addr[0x0555] = 0xAA;
399 addr[0x02AA] = 0x55;
400 addr[0x0555] = 0x80;
401 addr[0x0555] = 0xAA;
402 addr[0x02AA] = 0x55;
404 /* Start erase on unprotected sectors */
405 for (sect = s_first; sect <= s_last; sect++) {
406 if (info->protect[sect] == 0) { /* not protected */
407 addr = (vu_char *) (info->start[sect]);
408 addr[0] = 0x30;
409 l_sect = sect;
413 /* re-enable interrupts if necessary */
414 if (flag)
415 enable_interrupts();
417 /* wait at least 80us - let's wait 1 ms */
418 udelay(1000);
421 * We wait for the last triggered sector
423 if (l_sect < 0)
424 goto DONE;
426 start = get_timer(0);
427 last = start;
428 addr = (vu_char *) (info->start[l_sect]);
429 while ((addr[0] & 0x80) != 0x80) {
430 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
431 printf("Timeout\n");
432 return 1;
434 /* show that we're waiting */
435 if ((now - last) > 1000) { /* every second */
436 putc('.');
437 last = now;
441 DONE:
442 /* reset to read mode */
443 addr = (vu_char *) info->start[0];
444 addr[0] = 0xF0; /* reset bank */
446 printf(" done\n");
447 return 0;
450 /*-----------------------------------------------------------------------
451 * Copy memory to flash, returns:
452 * 0 - OK
453 * 1 - write timeout
454 * 2 - Flash not erased
457 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
459 int rc;
461 while (cnt > 0) {
462 if ((rc = write_byte(info, addr++, *src++)) != 0) {
463 return (rc);
465 --cnt;
468 return (0);
471 /*-----------------------------------------------------------------------
472 * Write a word to Flash, returns:
473 * 0 - OK
474 * 1 - write timeout
475 * 2 - Flash not erased
477 static int write_byte(flash_info_t * info, ulong dest, uchar data)
479 vu_char *addr = (vu_char *) (info->start[0]);
480 ulong start;
481 int flag;
483 /* Check if Flash is (sufficiently) erased */
484 if ((*((vu_char *) dest) & data) != data) {
485 return (2);
487 /* Disable interrupts which might cause a timeout here */
488 flag = disable_interrupts();
490 addr[0x0555] = 0xAA;
491 addr[0x02AA] = 0x55;
492 addr[0x0555] = 0xA0;
494 *((vu_char *) dest) = data;
496 /* re-enable interrupts if necessary */
497 if (flag)
498 enable_interrupts();
500 /* data polling for D7 */
501 start = get_timer(0);
502 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
503 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
504 return (1);
507 return (0);
510 /*-----------------------------------------------------------------------