2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2004, Li-Pro.Net <www.li-pro.net>
6 * Stephan Linz <linz@li-pro.net>
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,
32 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
34 /*--------------------------------------------------------------------*/
35 void flash_print_info (flash_info_t * info)
40 volatile unsigned char *flash;
42 printf (" Size: %ld KB in %d Sectors\n",
43 info->size >> 10, info->sector_count);
44 printf (" Sector Start Addresses:");
45 for (i = 0; i < info->sector_count; ++i) {
47 /* Check if whole sector is erased */
48 if (i != (info->sector_count - 1))
49 size = info->start[i + 1] - info->start[i];
51 size = info->start[0] + info->size - info->start[i];
53 flash = (volatile unsigned char *) info->start[i];
54 for (k = 0; k < size; k++) {
55 if (*flash++ != 0xff) {
64 printf (" %08lX%s%s", info->start[i], erased ? " E" : " ",
65 info->protect[i] ? "RO " : " ");
70 /*-------------------------------------------------------------------*/
73 int flash_erase (flash_info_t * info, int s_first, int s_last)
75 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
76 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
81 /* Some sanity checking */
82 if ((s_first < 0) || (s_first > s_last)) {
83 printf ("- no sectors to erase\n");
88 for (sect = s_first; sect <= s_last; ++sect) {
89 if (info->protect[sect]) {
94 printf ("- Warning: %d protected sectors will not be erased!\n",
101 for (sect = s_first; sect <= s_last; sect++) {
102 printf("- Erase: Sect: %i @ 0x%08x\n", sect, info->start[sect]);
106 /* NOTE: disabling interrupts on Nios can be very bad since it
107 * also disables the LO_LIMIT exception. It's better here to
108 * set the interrupt priority to 3 & restore it when we're done.
112 /* It's ok to erase multiple sectors provided we don't delay more
113 * than 50 usec between cmds ... at which point the erase time-out
114 * occurs. So don't go and put printf() calls in the loop ... it
115 * won't be very helpful ;-)
117 for (sect = s_first; sect <= s_last; sect++) {
118 if (info->protect[sect] == 0) { /* not protected */
119 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
121 *(addr+0xAAA/2) = 0xaa;
122 *(addr+0x554/2) = 0x55;
123 *(addr+0xAAA/2) = 0x80;
124 *(addr+0xAAA/2) = 0xaa;
125 *(addr+0x554/2) = 0x55;
127 /* Now just wait for 0xffff & provide some user
128 * feedback while we wait. Here we have to grant
129 * timer interrupts. Otherwise get_timer() can't
132 start = get_timer (0);
133 while (*addr2 != 0xffff) {
134 for (wait = 8; wait; wait--) {
138 if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
139 printf ("timeout\n");
143 oldpri = ipri (3); /* disallow non important irqs again */
150 /* Restore interrupt priority */
156 /*-----------------------------------------------------------------------
157 * Copy memory to flash, returns:
160 * 2 - Flash not erased
163 int write_buff (flash_info_t * info, uchar * srcbuffer, ulong addr, ulong cnt)
166 volatile CONFIG_SYS_FLASH_WORD_SIZE *cmd = (vu_short *) info->start[0];
167 volatile CONFIG_SYS_FLASH_WORD_SIZE *dst = (vu_short *) addr;
168 CONFIG_SYS_FLASH_WORD_SIZE *src = (void *) srcbuffer;
169 CONFIG_SYS_FLASH_WORD_SIZE b;
173 cnt /= sizeof(CONFIG_SYS_FLASH_WORD_SIZE);
175 /* Check for sufficient erase */
177 if ((*dst & b) != b) {
178 printf ("%02x : %02x\n", *dst, b);
182 /* Disable interrupts other than window underflow
183 * (interrupt priority 2)
186 *(cmd+0xAAA/2) = 0xaa;
187 *(cmd+0x554/2) = 0x55;
188 *(cmd+0xAAA/2) = 0xa0;
193 start = get_timer (0);
195 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {