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;
46 /* Init: no FLASHes known */
47 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
51 /* Static FLASH Bank configuration here - FIXME XXX */
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", size, size << 20);
59 /* Remap FLASH according to real size */
60 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
61 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
63 /* Re-do sizing to get full correct info */
64 size = flash_get_size((vu_long *) CFG_FLASH_BASE, &flash_info[0]);
66 flash_get_offsets(CFG_FLASH_BASE, &flash_info[0]);
68 /* monitor protection ON by default */
69 flash_protect(FLAG_PROTECT_SET, CFG_FLASH_BASE, CFG_FLASH_BASE + CFG_MONITOR_LEN - 1, &flash_info[0]);
71 flash_info[0].size = size;
76 /*-----------------------------------------------------------------------
78 static void flash_get_offsets(ulong base, flash_info_t * info)
82 /* set up sector start address table */
83 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
84 for (i = 0; i < info->sector_count; i++) {
85 info->start[i] = base + (i * 0x00010000);
87 } else if (info->flash_id & FLASH_BTYPE) {
88 /* set sector offsets for bottom boot block type */
89 info->start[0] = base + 0x00000000;
90 info->start[1] = base + 0x00004000;
91 info->start[2] = base + 0x00006000;
92 info->start[3] = base + 0x00008000;
93 for (i = 4; i < info->sector_count; i++) {
94 info->start[i] = base + (i * 0x00010000) - 0x00030000;
97 /* set sector offsets for top boot block type */
98 i = info->sector_count - 1;
99 info->start[i--] = base + info->size - 0x00004000;
100 info->start[i--] = base + info->size - 0x00006000;
101 info->start[i--] = base + info->size - 0x00008000;
102 for (; i >= 0; i--) {
103 info->start[i] = base + i * 0x00010000;
109 /*-----------------------------------------------------------------------
111 void flash_print_info(flash_info_t * info)
115 if (info->flash_id == FLASH_UNKNOWN) {
116 printf("missing or unknown FLASH type\n");
120 switch (info->flash_id & FLASH_VENDMASK) {
131 printf("Unknown Vendor ");
135 switch (info->flash_id & FLASH_TYPEMASK) {
137 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
140 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
143 printf("AM29LV400T (4 Mbit, top boot sector)\n");
146 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
149 printf("AM29LV800T (8 Mbit, top boot sector)\n");
152 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
155 printf("AM29LV160T (16 Mbit, top boot sector)\n");
158 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
161 printf("AM29LV320T (32 Mbit, top boot sector)\n");
164 printf("Unknown Chip Type\n");
168 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
170 printf(" Sector Start Addresses:");
171 for (i = 0; i < info->sector_count; ++i) {
174 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
179 /*-----------------------------------------------------------------------
183 /*-----------------------------------------------------------------------
187 * The following code cannot be run from FLASH!
190 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
195 vu_char *caddr = (vu_char *) addr;
196 ulong base = (ulong) addr;
199 /* Write auto select command: read Manufacturer ID */
200 caddr[0x0555] = 0xAA;
201 caddr[0x02AA] = 0x55;
202 caddr[0x0555] = 0x90;
206 case (AMD_MANUFACT & 0xFF):
207 info->flash_id = FLASH_MAN_AMD;
209 case (FUJ_MANUFACT & 0xFF):
210 info->flash_id = FLASH_MAN_FUJ;
212 case (MX_MANUFACT & 0xFF):
213 info->flash_id = FLASH_MAN_MX;
215 case (STM_MANUFACT & 0xFF):
216 info->flash_id = FLASH_MAN_STM;
219 info->flash_id = FLASH_UNKNOWN;
220 info->sector_count = 0;
222 return (0); /* no or unknown flash */
225 pid = caddr[1]; /* device ID */
227 case (AMD_ID_LV400T & 0xFF):
228 info->flash_id += FLASH_AM400T;
229 info->sector_count = 11;
230 info->size = 0x00080000;
231 break; /* => 512 kB */
233 case (AMD_ID_LV400B & 0xFF):
234 info->flash_id += FLASH_AM400B;
235 info->sector_count = 11;
236 info->size = 0x00080000;
237 break; /* => 512 kB */
239 case (AMD_ID_LV800T & 0xFF):
240 info->flash_id += FLASH_AM800T;
241 info->sector_count = 19;
242 info->size = 0x00100000;
245 case (AMD_ID_LV800B & 0xFF):
246 info->flash_id += FLASH_AM800B;
247 info->sector_count = 19;
248 info->size = 0x00100000;
251 case (AMD_ID_LV160T & 0xFF):
252 info->flash_id += FLASH_AM160T;
253 info->sector_count = 35;
254 info->size = 0x00200000;
257 case (AMD_ID_LV160B & 0xFF):
258 info->flash_id += FLASH_AM160B;
259 info->sector_count = 35;
260 info->size = 0x00200000;
263 case (AMD_ID_LV040B & 0xFF):
264 info->flash_id += FLASH_AM040;
265 info->sector_count = 8;
266 info->size = 0x00080000;
269 case (STM_ID_M29W040B & 0xFF):
270 info->flash_id += FLASH_AM040;
271 info->sector_count = 8;
272 info->size = 0x00080000;
275 #if 0 /* enable when device IDs are available */
276 case (AMD_ID_LV320T & 0xFF):
277 info->flash_id += FLASH_AM320T;
278 info->sector_count = 67;
279 info->size = 0x00400000;
282 case (AMD_ID_LV320B & 0xFF):
283 info->flash_id += FLASH_AM320B;
284 info->sector_count = 67;
285 info->size = 0x00400000;
289 info->flash_id = FLASH_UNKNOWN;
290 return (0); /* => no or unknown flash */
295 /* set up sector start address table */
296 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
297 for (i = 0; i < info->sector_count; i++) {
298 info->start[i] = base + (i * 0x00010000);
300 } else if (info->flash_id & FLASH_BTYPE) {
301 /* set sector offsets for bottom boot block type */
302 info->start[0] = base + 0x00000000;
303 info->start[1] = base + 0x00004000;
304 info->start[2] = base + 0x00006000;
305 info->start[3] = base + 0x00008000;
306 for (i = 4; i < info->sector_count; i++) {
307 info->start[i] = base + (i * 0x00010000) - 0x00030000;
310 /* set sector offsets for top boot block type */
311 i = info->sector_count - 1;
312 info->start[i--] = base + info->size - 0x00004000;
313 info->start[i--] = base + info->size - 0x00006000;
314 info->start[i--] = base + info->size - 0x00008000;
315 for (; i >= 0; i--) {
316 info->start[i] = base + i * 0x00010000;
320 /* check for protected sectors */
321 for (i = 0; i < info->sector_count; i++) {
322 /* read sector protection: D0 = 1 if protected */
323 caddr = (volatile unsigned char *)(info->start[i]);
324 info->protect[i] = caddr[2] & 1;
328 * Prevent writes to uninitialized FLASH.
330 if (info->flash_id != FLASH_UNKNOWN) {
331 caddr = (vu_char *) info->start[0];
333 caddr[0x0555] = 0xAA;
334 caddr[0x02AA] = 0x55;
335 caddr[0x0555] = 0xF0;
344 /*-----------------------------------------------------------------------
347 int flash_erase(flash_info_t * info, int s_first, int s_last)
349 vu_char *addr = (vu_char *) (info->start[0]);
350 int flag, prot, sect, l_sect;
351 ulong start, now, last;
353 if ((s_first < 0) || (s_first > s_last)) {
354 if (info->flash_id == FLASH_UNKNOWN) {
355 printf("- missing\n");
357 printf("- no sectors to erase\n");
362 if ((info->flash_id == FLASH_UNKNOWN) ||
363 (info->flash_id > FLASH_AMD_COMP)) {
364 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
369 for (sect = s_first; sect <= s_last; ++sect) {
370 if (info->protect[sect]) {
376 printf("- Warning: %d protected sectors will not be erased!\n", prot);
383 /* Disable interrupts which might cause a timeout here */
384 flag = disable_interrupts();
392 /* Start erase on unprotected sectors */
393 for (sect = s_first; sect <= s_last; sect++) {
394 if (info->protect[sect] == 0) { /* not protected */
395 addr = (vu_char *) (info->start[sect]);
401 /* re-enable interrupts if necessary */
405 /* wait at least 80us - let's wait 1 ms */
409 * We wait for the last triggered sector
414 start = get_timer(0);
416 addr = (vu_char *) (info->start[l_sect]);
417 while ((addr[0] & 0x80) != 0x80) {
418 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
422 /* show that we're waiting */
423 if ((now - last) > 1000) { /* every second */
430 /* reset to read mode */
431 addr = (vu_char *) info->start[0];
432 addr[0] = 0xF0; /* reset bank */
438 /*-----------------------------------------------------------------------
439 * Copy memory to flash, returns:
442 * 2 - Flash not erased
445 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
450 if ((rc = write_byte(info, addr++, *src++)) != 0) {
459 /*-----------------------------------------------------------------------
460 * Write a word to Flash, returns:
463 * 2 - Flash not erased
465 static int write_byte(flash_info_t * info, ulong dest, uchar data)
467 vu_char *addr = (vu_char *) (info->start[0]);
471 /* Check if Flash is (sufficiently) erased */
472 if ((*((vu_char *) dest) & data) != data) {
475 /* Disable interrupts which might cause a timeout here */
476 flag = disable_interrupts();
482 *((vu_char *) dest) = data;
484 /* re-enable interrupts if necessary */
488 /* data polling for D7 */
489 start = get_timer(0);
490 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
491 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
498 /*-----------------------------------------------------------------------