5 * Changes for MATRIX Vision MVsensor (C) Copyright 2001
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #define mvdebug(p) do {} while (0)
38 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
41 #ifdef CONFIG_MVS_16BIT_FLASH
42 #define FLASH_DATA_MASK 0xffff
45 #define FLASH_DATA_MASK 0xffffffff
50 /*-----------------------------------------------------------------------
53 static ulong flash_get_size (vu_long *address, flash_info_t *info);
54 static int write_word (flash_info_t *info, ulong dest, ulong data);
55 static void flash_get_offsets (ulong base, flash_info_t *info);
57 /*-----------------------------------------------------------------------
60 unsigned long flash_init (void)
62 volatile immap_t *immap = (immap_t *)CFG_IMMR;
63 volatile memctl8xx_t *memctl = &immap->im_memctl;
64 unsigned long size_b0, size_b1;
67 /* Init: no FLASHes known */
68 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
69 flash_info[i].flash_id = FLASH_UNKNOWN;
72 /* Static FLASH Bank configuration here - FIXME XXX */
74 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
76 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
77 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
78 size_b0, size_b0<<20);
81 #if defined (FLASH_BASE1_PRELIM)
82 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
84 if (size_b1 > size_b0) {
86 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
90 flash_info[0].flash_id = FLASH_UNKNOWN;
91 flash_info[1].flash_id = FLASH_UNKNOWN;
92 flash_info[0].sector_count = -1;
93 flash_info[1].sector_count = -1;
94 flash_info[0].size = 0;
95 flash_info[1].size = 0;
102 /* Remap FLASH according to real size */
103 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
104 #ifdef CONFIG_MVS_16BIT_FLASH
105 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
107 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_32 | BR_MS_GPCM | BR_V;
110 /* Re-do sizing to get full correct info */
111 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
113 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
115 /* monitor protection ON by default */
116 flash_protect(FLAG_PROTECT_SET,
118 CFG_FLASH_BASE+monitor_flash_len-1,
122 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
123 #ifdef CONFIG_MVS_16BIT_FLASH
124 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
125 BR_PS_16 | BR_MS_GPCM | BR_V;
127 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
128 BR_PS_32 | BR_MS_GPCM | BR_V;
130 /* Re-do sizing to get full correct info */
131 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
134 flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
136 /* monitor protection ON by default */
137 flash_protect(FLAG_PROTECT_SET,
139 CFG_FLASH_BASE+monitor_flash_len-1,
142 memctl->memc_br1 = 0; /* invalidate bank */
144 flash_info[1].flash_id = FLASH_UNKNOWN;
145 flash_info[1].sector_count = -1;
148 flash_info[0].size = size_b0;
149 flash_info[1].size = size_b1;
151 return (size_b0 + size_b1);
154 /*-----------------------------------------------------------------------
156 static void flash_get_offsets (ulong base, flash_info_t *info)
160 /* set up sector start address table */
161 if (info->flash_id & FLASH_BTYPE)
162 { /* bottom boot sector types - these are the useful ones! */
163 /* set sector offsets for bottom boot block type */
164 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B)
165 { /* AMDLV320B has 8 x 8k bottom boot sectors */
166 for (i = 0; i < 8; i++) /* +8k */
167 info->start[i] = base + (i * (0x00002000 << FLASH_SHIFT));
168 for (; i < info->sector_count; i++) /* +64k */
169 info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT)) - (0x00070000 << FLASH_SHIFT);
172 { /* other types have 4 bottom boot sectors (16,8,8,32) */
174 info->start[i++] = base + 0x00000000; /* - */
175 info->start[i++] = base + (0x00004000 << FLASH_SHIFT); /* +16k */
176 info->start[i++] = base + (0x00006000 << FLASH_SHIFT); /* +8k */
177 info->start[i++] = base + (0x00008000 << FLASH_SHIFT); /* +8k */
178 info->start[i++] = base + (0x00010000 << FLASH_SHIFT); /* +32k */
179 for (; i < info->sector_count; i++) /* +64k */
180 info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT)) - (0x00030000 << FLASH_SHIFT);
184 { /* top boot sector types - not so useful */
185 /* set sector offsets for top boot block type */
186 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T)
187 { /* AMDLV320T has 8 x 8k top boot sectors */
188 for (i = 0; i < info->sector_count - 8; i++) /* +64k */
189 info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT));
190 for (; i < info->sector_count; i++) /* +8k */
191 info->start[i] = base + (i * (0x00002000 << FLASH_SHIFT));
194 { /* other types have 4 top boot sectors (32,8,8,16) */
195 for (i = 0; i < info->sector_count - 4; i++) /* +64k */
196 info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT));
198 info->start[i++] = base + info->size - (0x00010000 << FLASH_SHIFT); /* -32k */
199 info->start[i++] = base + info->size - (0x00008000 << FLASH_SHIFT); /* -8k */
200 info->start[i++] = base + info->size - (0x00006000 << FLASH_SHIFT); /* -8k */
201 info->start[i] = base + info->size - (0x00004000 << FLASH_SHIFT); /* -16k */
206 /*-----------------------------------------------------------------------
208 void flash_print_info (flash_info_t *info)
212 if (info->flash_id == FLASH_UNKNOWN) {
213 printf ("missing or unknown FLASH type\n");
217 switch (info->flash_id & FLASH_VENDMASK) {
218 case FLASH_MAN_AMD: printf ("AMD "); break;
219 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
220 case FLASH_MAN_STM: printf ("ST "); break;
221 default: printf ("Unknown Vendor "); break;
224 switch (info->flash_id & FLASH_TYPEMASK) {
225 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
227 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
229 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
231 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
233 case FLASH_STMW320DB: printf ("M29W320B (32 Mbit, bottom boot sect)\n");
235 case FLASH_STMW320DT: printf ("M29W320T (32 Mbit, top boot sector)\n");
237 default: printf ("Unknown Chip Type\n");
241 printf (" Size: %ld MB in %d Sectors\n",
242 info->size >> 20, info->sector_count);
244 printf (" Sector Start Addresses:");
245 for (i=0; i<info->sector_count; ++i) {
250 info->protect[i] ? " (RO)" : " "
256 /*-----------------------------------------------------------------------
260 /*-----------------------------------------------------------------------
264 * The following code cannot be run from FLASH!
267 #define AMD_ID_LV160T_MVS (AMD_ID_LV160T & FLASH_DATA_MASK)
268 #define AMD_ID_LV160B_MVS (AMD_ID_LV160B & FLASH_DATA_MASK)
269 #define AMD_ID_LV320T_MVS (AMD_ID_LV320T & FLASH_DATA_MASK)
270 #define AMD_ID_LV320B_MVS (AMD_ID_LV320B & FLASH_DATA_MASK)
271 #define STM_ID_W320DT_MVS (STM_ID_29W320DT & FLASH_DATA_MASK)
272 #define STM_ID_W320DB_MVS (STM_ID_29W320DB & FLASH_DATA_MASK)
273 #define AMD_MANUFACT_MVS (AMD_MANUFACT & FLASH_DATA_MASK)
274 #define FUJ_MANUFACT_MVS (FUJ_MANUFACT & FLASH_DATA_MASK)
275 #define STM_MANUFACT_MVS (STM_MANUFACT & FLASH_DATA_MASK)
277 #define AUTOSELECT_ADDR1 0x0555
278 #define AUTOSELECT_ADDR2 0x02AA
279 #define AUTOSELECT_ADDR3 AUTOSELECT_ADDR1
281 #define AUTOSELECT_DATA1 (0x00AA00AA & FLASH_DATA_MASK)
282 #define AUTOSELECT_DATA2 (0x00550055 & FLASH_DATA_MASK)
283 #define AUTOSELECT_DATA3 (0x00900090 & FLASH_DATA_MASK)
285 #define RESET_BANK_DATA (0x00F000F0 & FLASH_DATA_MASK)
287 static ulong flash_get_size (vu_long *address, flash_info_t *info)
290 #ifdef CONFIG_MVS_16BIT_FLASH
292 vu_short *addr = (vu_short *)address;
295 vu_long *addr = (vu_long *)address;
297 ulong base = (ulong)address;
299 /* Write auto select command: read Manufacturer ID */
300 addr[AUTOSELECT_ADDR1] = AUTOSELECT_DATA1;
301 addr[AUTOSELECT_ADDR2] = AUTOSELECT_DATA2;
302 addr[AUTOSELECT_ADDR3] = AUTOSELECT_DATA3;
304 value = addr[0]; /* manufacturer ID */
306 case AMD_MANUFACT_MVS:
307 info->flash_id = FLASH_MAN_AMD;
309 case FUJ_MANUFACT_MVS:
310 info->flash_id = FLASH_MAN_FUJ;
312 case STM_MANUFACT_MVS:
313 info->flash_id = FLASH_MAN_STM;
316 info->flash_id = FLASH_UNKNOWN;
317 info->sector_count = 0;
319 return (0); /* no or unknown flash */
322 value = addr[1]; /* device ID */
324 case AMD_ID_LV160T_MVS:
325 info->flash_id += FLASH_AM160T;
326 info->sector_count = 37;
327 info->size = (0x00200000 << FLASH_SHIFT);
328 break; /* => 2 or 4 MB */
330 case AMD_ID_LV160B_MVS:
331 info->flash_id += FLASH_AM160B;
332 info->sector_count = 37;
333 info->size = (0x00200000 << FLASH_SHIFT);
334 break; /* => 2 or 4 MB */
336 case AMD_ID_LV320T_MVS:
337 info->flash_id += FLASH_AM320T;
338 info->sector_count = 71;
339 info->size = (0x00400000 << FLASH_SHIFT);
340 break; /* => 4 or 8 MB */
342 case AMD_ID_LV320B_MVS:
343 info->flash_id += FLASH_AM320B;
344 info->sector_count = 71;
345 info->size = (0x00400000 << FLASH_SHIFT);
346 break; /* => 4 or 8MB */
348 case STM_ID_W320DT_MVS:
349 info->flash_id += FLASH_STMW320DT;
350 info->sector_count = 67;
351 info->size = (0x00400000 << FLASH_SHIFT);
352 break; /* => 4 or 8 MB */
354 case STM_ID_W320DB_MVS:
355 info->flash_id += FLASH_STMW320DB;
356 info->sector_count = 67;
357 info->size = (0x00400000 << FLASH_SHIFT);
358 break; /* => 4 or 8MB */
361 info->flash_id = FLASH_UNKNOWN;
362 return (0); /* => no or unknown flash */
366 /* set up sector start address table */
367 flash_get_offsets (base, info);
369 /* check for protected sectors */
370 for (i = 0; i < info->sector_count; i++) {
371 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
372 /* D0 = 1 if protected */
373 #ifdef CONFIG_MVS_16BIT_FLASH
374 addr = (vu_short *)(info->start[i]);
376 addr = (vu_long *)(info->start[i]);
378 info->protect[i] = addr[2] & 1;
382 * Prevent writes to uninitialized FLASH.
384 if (info->flash_id != FLASH_UNKNOWN) {
385 #ifdef CONFIG_MVS_16BIT_FLASH
386 addr = (vu_short *)info->start[0];
388 addr = (vu_long *)info->start[0];
390 *addr = RESET_BANK_DATA; /* reset bank */
397 /*-----------------------------------------------------------------------
400 #define ERASE_ADDR1 0x0555
401 #define ERASE_ADDR2 0x02AA
402 #define ERASE_ADDR3 ERASE_ADDR1
403 #define ERASE_ADDR4 ERASE_ADDR1
404 #define ERASE_ADDR5 ERASE_ADDR2
406 #define ERASE_DATA1 (0x00AA00AA & FLASH_DATA_MASK)
407 #define ERASE_DATA2 (0x00550055 & FLASH_DATA_MASK)
408 #define ERASE_DATA3 (0x00800080 & FLASH_DATA_MASK)
409 #define ERASE_DATA4 ERASE_DATA1
410 #define ERASE_DATA5 ERASE_DATA2
412 #define ERASE_SECTOR_DATA (0x00300030 & FLASH_DATA_MASK)
413 #define ERASE_CHIP_DATA (0x00100010 & FLASH_DATA_MASK)
414 #define ERASE_CONFIRM_DATA (0x00800080 & FLASH_DATA_MASK)
416 int flash_erase (flash_info_t *info, int s_first, int s_last)
418 #ifdef CONFIG_MVS_16BIT_FLASH
419 vu_short *addr = (vu_short *)(info->start[0]);
421 vu_long *addr = (vu_long *)(info->start[0]);
423 int flag, prot, sect, l_sect;
424 ulong start, now, last;
426 if ((s_first < 0) || (s_first > s_last)) {
427 if (info->flash_id == FLASH_UNKNOWN) {
428 printf ("- missing\n");
430 printf ("- no sectors to erase\n");
435 if ((info->flash_id == FLASH_UNKNOWN) ||
436 (info->flash_id > FLASH_AMD_COMP)) {
437 printf ("Can't erase unknown flash type %08lx - aborted\n",
443 for (sect=s_first; sect<=s_last; ++sect) {
444 if (info->protect[sect]) {
450 printf ("- Warning: %d protected sectors will not be erased!\n",
458 /* Disable interrupts which might cause a timeout here */
459 flag = disable_interrupts();
461 addr[ERASE_ADDR1] = ERASE_DATA1;
462 addr[ERASE_ADDR2] = ERASE_DATA2;
463 addr[ERASE_ADDR3] = ERASE_DATA3;
464 addr[ERASE_ADDR4] = ERASE_DATA4;
465 addr[ERASE_ADDR5] = ERASE_DATA5;
467 /* Start erase on unprotected sectors */
468 for (sect = s_first; sect<=s_last; sect++) {
469 if (info->protect[sect] == 0) { /* not protected */
470 #ifdef CONFIG_MVS_16BIT_FLASH
471 addr = (vu_short *)(info->start[sect]);
473 addr = (vu_long *)(info->start[sect]);
475 addr[0] = ERASE_SECTOR_DATA;
480 /* re-enable interrupts if necessary */
484 /* wait at least 80us - let's wait 1 ms */
488 * We wait for the last triggered sector
493 start = get_timer (0);
495 #ifdef CONFIG_MVS_16BIT_FLASH
496 addr = (vu_short *)(info->start[l_sect]);
498 addr = (vu_long *)(info->start[l_sect]);
500 while ((addr[0] & ERASE_CONFIRM_DATA) != ERASE_CONFIRM_DATA) {
501 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
502 printf ("Timeout\n");
505 /* show that we're waiting */
506 if ((now - last) > 1000) { /* every second */
513 /* reset to read mode */
514 #ifdef CONFIG_MVS_16BIT_FLASH
515 addr = (vu_short *)info->start[0];
517 addr = (vu_long *)info->start[0];
519 addr[0] = RESET_BANK_DATA; /* reset bank */
526 /*-----------------------------------------------------------------------
527 * Copy memory to flash, returns:
530 * 2 - Flash not erased
533 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
539 mvdebug (("+write_buff %p ==> 0x%08lx, count = 0x%08lx\n", src, addr, cnt));
541 wp = (addr & ~3); /* get lower word aligned address */
543 * handle unaligned start bytes
545 if ((l = addr - wp) != 0) {
546 mvdebug ((" handle unaligned start bytes (cnt = 0x%08%lx)\n", cnt));
548 for (i=0, cp=wp; i<l; ++i, ++cp) {
549 data = (data << 8) | (*(uchar *)cp);
551 for (; i<BUFF_INC && cnt>0; ++i) {
552 data = (data << 8) | *src++;
556 for (; cnt==0 && i<BUFF_INC; ++i, ++cp) {
557 data = (data << 8) | (*(uchar *)cp);
560 if ((rc = write_word(info, wp, data)) != 0) {
567 * handle (half)word aligned part
569 mvdebug ((" handle word aligned part (cnt = 0x%08%lx)\n", cnt));
570 while (cnt >= BUFF_INC) {
572 for (i=0; i<BUFF_INC; ++i) {
573 data = (data << 8) | *src++;
575 if ((rc = write_word(info, wp, data)) != 0) {
587 * handle unaligned tail bytes
589 mvdebug ((" handle unaligned tail bytes (cnt = 0x%08%lx)\n", cnt));
591 for (i=0, cp=wp; i<BUFF_INC && cnt>0; ++i, ++cp) {
592 data = (data << 8) | *src++;
595 for (; i<BUFF_INC; ++i, ++cp) {
596 data = (data << 8) | (*(uchar *)cp);
599 return (write_word(info, wp, data));
602 #define WRITE_ADDR1 0x0555
603 #define WRITE_ADDR2 0x02AA
604 #define WRITE_ADDR3 WRITE_ADDR1
606 #define WRITE_DATA1 (0x00AA00AA & FLASH_DATA_MASK)
607 #define WRITE_DATA2 (0x00550055 & FLASH_DATA_MASK)
608 #define WRITE_DATA3 (0x00A000A0 & FLASH_DATA_MASK)
610 #define WRITE_CONFIRM_DATA ERASE_CONFIRM_DATA
612 #ifndef CONFIG_MVS_16BIT_FLASH
613 /*-----------------------------------------------------------------------
614 * Write a word to Flash, returns:
617 * 2 - Flash not erased
619 static int write_word (flash_info_t *info, ulong dest, ulong data)
621 vu_long *addr = (vu_long *)(info->start[0]);
625 mvdebug (("+write_word (to 0x%08lx)\n", dest));
626 /* Check if Flash is (sufficiently) erased */
627 if ((*((vu_long *)dest) & data) != data) {
630 /* Disable interrupts which might cause a timeout here */
631 flag = disable_interrupts();
633 addr[WRITE_ADDR1] = WRITE_DATA1;
634 addr[WRITE_ADDR2] = WRITE_DATA2;
635 addr[WRITE_ADDR3] = WRITE_DATA3;
637 *((vu_long *)dest) = data;
639 /* re-enable interrupts if necessary */
643 /* data polling for D7 */
644 start = get_timer (0);
645 addr = (vu_long *)dest;
646 while ((*addr & WRITE_CONFIRM_DATA) != (data & WRITE_CONFIRM_DATA)) {
647 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
652 mvdebug (("-write_word\n"));
655 #else /* CONFIG_MVS_16BIT_FLASH */
656 /*-----------------------------------------------------------------------
657 * Write a halfword to Flash, returns:
660 * 2 - Flash not erased
662 static int write_halfword (flash_info_t *info, ulong dest, ushort data)
664 vu_short *addr = (vu_short *)(info->start[0]);
668 mvdebug (("+write_halfword (to 0x%08lx)\n", dest));
669 /* Check if Flash is (sufficiently) erased */
670 if ((*((vu_short *)dest) & data) != data) {
673 /* Disable interrupts which might cause a timeout here */
674 flag = disable_interrupts();
676 addr[WRITE_ADDR1] = WRITE_DATA1;
677 addr[WRITE_ADDR2] = WRITE_DATA2;
678 addr[WRITE_ADDR3] = WRITE_DATA3;
680 *((vu_short *)dest) = data;
682 /* re-enable interrupts if necessary */
686 /* data polling for D7 */
687 start = get_timer (0);
688 addr = (vu_short *)dest;
689 while ((*addr & WRITE_CONFIRM_DATA) != (data & WRITE_CONFIRM_DATA)) {
690 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
694 mvdebug (("-write_halfword\n"));
699 /*-----------------------------------------------------------------------
700 * Write a word to Flash, returns:
703 * 2 - Flash not erased
705 static int write_word (flash_info_t *info, ulong dest, ulong data)
709 if (write_halfword (info, dest, (data & ~FLASH_DATA_MASK) >> 16) == 0)
712 data = data & FLASH_DATA_MASK;
713 result = write_halfword (info, dest, data);
718 /*-----------------------------------------------------------------------