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,
25 #include <board/cogent/flash.h>
26 #include <linux/compiler.h>
28 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
30 #if defined(CONFIG_ENV_IS_IN_FLASH)
31 # ifndef CONFIG_ENV_ADDR
32 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
34 # ifndef CONFIG_ENV_SIZE
35 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
37 # ifndef CONFIG_ENV_SECT_SIZE
38 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
42 /*-----------------------------------------------------------------------
45 static int write_word (flash_info_t *info, ulong dest, ulong data);
47 /*-----------------------------------------------------------------------
51 #if defined(CONFIG_CMA302)
54 * probe for the existence of flash at address "addr"
55 * 0 = yes, 1 = bad Manufacturer's Id, 2 = bad Device Id
58 c302f_probe_word(c302f_addr_t addr)
61 *addr = C302F_BNK_CMD_RST;
63 /* check the manufacturer id */
64 *addr = C302F_BNK_CMD_RD_ID;
65 if (*C302F_BNK_ADDR_MAN(addr) != C302F_BNK_RD_ID_MAN)
68 /* check the device id */
69 *addr = C302F_BNK_CMD_RD_ID;
70 if (*C302F_BNK_ADDR_DEV(addr) != C302F_BNK_RD_ID_DEV)
77 printf("\nMaster Lock Config = 0x%08lx\n",
78 *C302F_BNK_ADDR_CFGM(addr));
79 for (i = 0; i < C302F_BNK_NBLOCKS; i++)
80 printf("Block %2d Lock Config = 0x%08lx\n",
81 i, *C302F_BNK_ADDR_CFG(i, addr));
85 /* reset the flash again */
86 *addr = C302F_BNK_CMD_RST;
92 * probe for Cogent CMA302 flash module at address "base" and store
93 * info for any found into flash_info entry "fip". Must find at least
97 c302f_probe(flash_info_t *fip, c302f_addr_t base)
99 c302f_addr_t addr, eaddr;
103 fip->sector_count = 0;
106 eaddr = C302F_BNK_ADDR_BASE(addr, C302F_MAX_BANKS);
109 while (addr < eaddr) {
110 c302f_addr_t addrw, eaddrw, addrb;
114 eaddrw = C302F_BNK_ADDR_NEXT_WORD(addrw);
116 while (addrw < eaddrw)
117 if (c302f_probe_word(addrw++) != 0)
120 /* bank exists - append info for this bank to *fip */
121 fip->flash_id = FLASH_MAN_INTEL|FLASH_28F008S5;
122 fip->size += C302F_BNK_SIZE;
123 osc = fip->sector_count;
124 fip->sector_count += C302F_BNK_NBLOCKS;
125 if ((nsc = fip->sector_count) >= CONFIG_SYS_MAX_FLASH_SECT)
126 panic("Too many sectors in flash at address 0x%08lx\n",
127 (unsigned long)base);
130 for (i = osc; i < nsc; i++) {
131 fip->start[i] = (ulong)addrb;
133 addrb = C302F_BNK_ADDR_NEXT_BLK(addrb);
136 addr = C302F_BNK_ADDR_NEXT_BNK(addr);
142 panic("ERROR: no flash found at address 0x%08lx\n",
143 (unsigned long)base);
147 c302f_reset(flash_info_t *info, int sect)
149 c302f_addr_t addrw, eaddrw;
151 addrw = (c302f_addr_t)info->start[sect];
152 eaddrw = C302F_BNK_ADDR_NEXT_WORD(addrw);
154 while (addrw < eaddrw) {
156 printf(" writing reset cmd to addr 0x%08lx\n",
157 (unsigned long)addrw);
159 *addrw = C302F_BNK_CMD_RST;
165 c302f_erase_init(flash_info_t *info, int sect)
167 c302f_addr_t addrw, saddrw, eaddrw;
171 printf("0x%08lx C302F_BNK_CMD_PROG\n", C302F_BNK_CMD_PROG);
172 printf("0x%08lx C302F_BNK_CMD_ERASE1\n", C302F_BNK_CMD_ERASE1);
173 printf("0x%08lx C302F_BNK_CMD_ERASE2\n", C302F_BNK_CMD_ERASE2);
174 printf("0x%08lx C302F_BNK_CMD_CLR_STAT\n", C302F_BNK_CMD_CLR_STAT);
175 printf("0x%08lx C302F_BNK_CMD_RST\n", C302F_BNK_CMD_RST);
176 printf("0x%08lx C302F_BNK_STAT_RDY\n", C302F_BNK_STAT_RDY);
177 printf("0x%08lx C302F_BNK_STAT_ERR\n", C302F_BNK_STAT_ERR);
180 saddrw = (c302f_addr_t)info->start[sect];
181 eaddrw = C302F_BNK_ADDR_NEXT_WORD(saddrw);
184 printf("erasing sector %d, start addr = 0x%08lx "
185 "(bank next word addr = 0x%08lx)\n", sect,
186 (unsigned long)saddrw, (unsigned long)eaddrw);
189 /* Disable intrs which might cause a timeout here */
190 flag = disable_interrupts();
192 for (addrw = saddrw; addrw < eaddrw; addrw++) {
194 printf(" writing erase cmd to addr 0x%08lx\n",
195 (unsigned long)addrw);
197 *addrw = C302F_BNK_CMD_ERASE1;
198 *addrw = C302F_BNK_CMD_ERASE2;
201 /* re-enable interrupts if necessary */
207 c302f_erase_poll(flash_info_t *info, int sect)
209 c302f_addr_t addrw, saddrw, eaddrw;
210 int sectdone, haderr;
212 saddrw = (c302f_addr_t)info->start[sect];
213 eaddrw = C302F_BNK_ADDR_NEXT_WORD(saddrw);
218 for (addrw = saddrw; addrw < eaddrw; addrw++) {
219 c302f_word_t stat = *addrw;
222 printf(" checking status at addr "
223 "0x%08lx [0x%08lx]\n",
224 (unsigned long)addrw, stat);
226 if ((stat & C302F_BNK_STAT_RDY) != C302F_BNK_STAT_RDY)
228 else if ((stat & C302F_BNK_STAT_ERR) != 0) {
229 printf(" failed on sector %d "
230 "(stat = 0x%08lx) at "
233 (unsigned long)addrw);
234 *addrw = C302F_BNK_CMD_CLR_STAT;
246 c302f_write_word(c302f_addr_t addr, c302f_word_t value)
252 /* Disable interrupts which might cause a timeout here */
253 flag = disable_interrupts();
255 *addr = C302F_BNK_CMD_PROG;
259 /* re-enable interrupts if necessary */
265 /* data polling for D7 */
266 start = get_timer (0);
268 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
273 } while ((stat & C302F_BNK_STAT_RDY) != C302F_BNK_STAT_RDY);
275 if ((stat & C302F_BNK_STAT_ERR) != 0) {
276 printf("flash program failed (stat = 0x%08lx) "
277 "at address 0x%08lx\n", (ulong)stat, (ulong)addr);
278 *addr = C302F_BNK_CMD_CLR_STAT;
283 /* reset to read mode */
284 *addr = C302F_BNK_CMD_RST;
289 #endif /* CONFIG_CMA302 */
296 __maybe_unused flash_info_t *fip;
298 /* Init: no FLASHes known */
299 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
300 flash_info[i].flash_id = FLASH_UNKNOWN;
303 fip = &flash_info[0];
306 #if defined(CONFIG_CMA302)
307 c302f_probe(fip, (c302f_addr_t)CONFIG_SYS_FLASH_BASE);
312 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
314 cmbf_probe(fip, (cmbf_addr_t)CMA_MB_FLASH_BASE);
321 * protect monitor and environment sectors
324 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
325 flash_protect(FLAG_PROTECT_SET,
326 CONFIG_SYS_MONITOR_BASE,
327 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
331 #ifdef CONFIG_ENV_IS_IN_FLASH
332 /* ENV protection ON by default */
333 flash_protect(FLAG_PROTECT_SET,
335 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
341 /*-----------------------------------------------------------------------
344 flash_print_info(flash_info_t *info)
348 if (info->flash_id == FLASH_UNKNOWN) {
349 printf ("missing or unknown FLASH type\n");
353 switch (info->flash_id & FLASH_VENDMASK) {
354 case FLASH_MAN_INTEL: printf ("INTEL "); break;
355 default: printf ("Unknown Vendor "); break;
358 switch (info->flash_id & FLASH_TYPEMASK) {
359 case FLASH_28F008S5: printf ("28F008S5\n");
361 default: printf ("Unknown Chip Type\n");
365 printf (" Size: %ld MB in %d Sectors\n",
366 info->size >> 20, info->sector_count);
368 printf (" Sector Start Addresses:");
369 for (i=0; i<info->sector_count; ++i) {
372 printf (" %2d - %08lX%s", i,
374 info->protect[i] ? " (RO)" : " "
381 /*-----------------------------------------------------------------------
385 /*-----------------------------------------------------------------------
389 * The following code cannot be run from FLASH!
393 flash_erase(flash_info_t *info, int s_first, int s_last)
395 int prot, sect, haderr;
396 ulong start, now, last;
397 void (*erase_init)(flash_info_t *, int);
398 int (*erase_poll)(flash_info_t *, int);
399 void (*reset)(flash_info_t *, int);
403 printf("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
404 " Bank # %d: ", s_last - s_first + 1, s_first, s_last,
405 (info - flash_info) + 1);
406 flash_print_info(info);
409 if ((s_first < 0) || (s_first > s_last)) {
410 if (info->flash_id == FLASH_UNKNOWN) {
411 printf ("- missing\n");
413 printf ("- no sectors to erase\n");
418 switch (info->flash_id) {
420 #if defined(CONFIG_CMA302)
421 case FLASH_MAN_INTEL|FLASH_28F008S5:
422 erase_init = c302f_erase_init;
423 erase_poll = c302f_erase_poll;
428 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
429 case FLASH_MAN_INTEL|FLASH_28F800_B:
430 case FLASH_MAN_AMD|FLASH_AM29F800B:
432 erase_init = cmbf_erase_init;
433 erase_poll = cmbf_erase_poll;
440 printf ("Flash type %08lx not supported - aborted\n",
446 for (sect=s_first; sect<=s_last; ++sect) {
447 if (info->protect[sect]) {
453 printf("- Warning: %d protected sector%s will not be erased!\n",
454 prot, (prot > 1 ? "s" : ""));
457 start = get_timer (0);
461 for (sect = s_first; sect <= s_last; sect++) {
462 if (info->protect[sect] == 0) { /* not protected */
466 (*erase_init)(info, sect);
468 /* wait at least 80us - let's wait 1 ms */
471 estart = get_timer(start);
474 now = get_timer(start);
476 if (now - estart > CONFIG_SYS_FLASH_ERASE_TOUT) {
477 printf ("Timeout (sect %d)\n", sect);
483 /* show that we're waiting */
484 if ((now - last) > 1000) { /* every second */
490 sectdone = (*erase_poll)(info, sect);
505 printf (" failed\n");
511 /* reset to read mode */
512 for (sect = s_first; sect <= s_last; sect++) {
513 if (info->protect[sect] == 0) { /* not protected */
514 (*reset)(info, sect);
520 /*-----------------------------------------------------------------------
521 * Copy memory to flash, returns:
524 * 2 - Flash not erased
529 write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
533 ulong start, now, last;
535 wp = (addr & ~3); /* get lower word aligned address */
538 * handle unaligned start bytes
540 if ((l = addr - wp) != 0) {
542 for (i=0, cp=wp; i<l; ++i, ++cp) {
543 data = (data << 8) | (*(uchar *)cp);
545 for (; i<4 && cnt>0; ++i) {
546 data = (data << 8) | *src++;
550 for (; cnt==0 && i<4; ++i, ++cp) {
551 data = (data << 8) | (*(uchar *)cp);
554 if ((rc = write_word(info, wp, data)) != 0) {
561 * handle word aligned part
563 start = get_timer (0);
567 for (i=0; i<4; ++i) {
568 data = (data << 8) | *src++;
570 if ((rc = write_word(info, wp, data)) != 0) {
576 /* show that we're waiting */
577 now = get_timer(start);
578 if ((now - last) > 1000) { /* every second */
589 * handle unaligned tail bytes
592 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
593 data = (data << 8) | *src++;
596 for (; i<4; ++i, ++cp) {
597 data = (data << 8) | (*(uchar *)cp);
600 return (write_word(info, wp, data));
603 /*-----------------------------------------------------------------------
604 * Write a word to Flash, returns:
607 * 2 - Flash not erased
611 write_word(flash_info_t *info, ulong dest, ulong data)
615 /* Check if Flash is (sufficiently) erased */
616 if ((*(ulong *)dest & data) != data) {
620 switch (info->flash_id) {
622 #if defined(CONFIG_CMA302)
623 case FLASH_MAN_INTEL|FLASH_28F008S5:
624 retval = c302f_write_word((c302f_addr_t)dest, (c302f_word_t)data);
628 #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
629 case FLASH_MAN_INTEL|FLASH_28F800_B:
630 case FLASH_MAN_AMD|FLASH_AM29F800B:
632 retval = cmbf_write_word((cmbf_addr_t)dest, (cmbf_word_t)data);
639 printf ("Flash type %08lx not supported - aborted\n",
648 /*-----------------------------------------------------------------------