3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #define FLASH_BANK_SIZE 0x1000000
28 #define MAIN_SECT_SIZE 0x20000
30 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
33 /*-----------------------------------------------------------------------
36 ulong flash_init (void)
41 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
44 flash_info[i].flash_id =
45 (INTEL_MANUFACT & FLASH_VENDMASK) |
46 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
47 flash_info[i].size = FLASH_BANK_SIZE;
48 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
49 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
51 flashbase = PHYS_FLASH_1;
53 panic ("configured too many flash banks!\n");
54 for (j = 0; j < flash_info[i].sector_count; j++) {
55 flash_info[i].start[j] = flashbase + j * MAIN_SECT_SIZE;
57 size += flash_info[i].size;
60 /* Protect monitor and environment sectors
62 flash_protect ( FLAG_PROTECT_SET,
63 CONFIG_SYS_FLASH_BASE,
64 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
67 flash_protect ( FLAG_PROTECT_SET,
69 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
74 /*-----------------------------------------------------------------------
76 void flash_print_info (flash_info_t * info)
80 switch (info->flash_id & FLASH_VENDMASK) {
81 case (INTEL_MANUFACT & FLASH_VENDMASK):
85 printf ("Unknown Vendor ");
89 switch (info->flash_id & FLASH_TYPEMASK) {
90 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
91 printf ("28F128J3 (128Mbit)\n");
94 printf ("Unknown Chip Type\n");
99 printf (" Size: %ld MB in %d Sectors\n",
100 info->size >> 20, info->sector_count);
102 printf (" Sector Start Addresses:");
103 for (i = 0; i < info->sector_count; i++) {
107 printf (" %08lX%s", info->start[i],
108 info->protect[i] ? " (RO)" : " ");
115 /*-----------------------------------------------------------------------
118 int flash_erase (flash_info_t * info, int s_first, int s_last)
120 int flag, prot, sect;
124 if (info->flash_id == FLASH_UNKNOWN)
125 return ERR_UNKNOWN_FLASH_TYPE;
127 if ((s_first < 0) || (s_first > s_last)) {
131 if ((info->flash_id & FLASH_VENDMASK) !=
132 (INTEL_MANUFACT & FLASH_VENDMASK)) {
133 return ERR_UNKNOWN_FLASH_VENDOR;
137 for (sect = s_first; sect <= s_last; ++sect) {
138 if (info->protect[sect]) {
143 return ERR_PROTECTED;
146 * Disable interrupts which might cause a timeout
147 * here. Remember that our exception vectors are
148 * at address 0 in the flash, and we don't want a
149 * (ticker) exception to happen while the flash
150 * chip is in programming mode.
152 flag = disable_interrupts ();
154 /* Start erase on unprotected sectors */
155 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
157 printf ("Erasing sector %2d ... ", sect);
159 /* arm simple, non interrupt dependent timer */
160 start = get_timer(0);
162 if (info->protect[sect] == 0) { /* not protected */
163 vu_short *addr = (vu_short *) (info->start[sect]);
165 *addr = 0x20; /* erase setup */
166 *addr = 0xD0; /* erase confirm */
168 while ((*addr & 0x80) != 0x80) {
169 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
170 *addr = 0xB0; /* suspend erase */
171 *addr = 0xFF; /* reset to read mode */
177 /* clear status register command */
179 /* reset to read mode */
185 printf ("User Interrupt!\n");
189 /* allow flash to settle - wait 10 ms */
190 udelay_masked (10000);
193 enable_interrupts ();
198 /*-----------------------------------------------------------------------
199 * Copy memory to flash
202 static int write_word (flash_info_t * info, ulong dest, ushort data)
204 vu_short *addr = (vu_short *) dest, val;
209 /* Check if Flash is (sufficiently) erased
211 if ((*addr & data) != data)
212 return ERR_NOT_ERASED;
215 * Disable interrupts which might cause a timeout
216 * here. Remember that our exception vectors are
217 * at address 0 in the flash, and we don't want a
218 * (ticker) exception to happen while the flash
219 * chip is in programming mode.
221 flag = disable_interrupts ();
223 /* clear status register command */
226 /* program set-up command */
229 /* latch address/data */
232 /* arm simple, non interrupt dependent timer */
233 start = get_timer(0);
235 /* wait while polling the status register */
236 while (((val = *addr) & 0x80) != 0x80) {
237 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
239 /* suspend program command */
245 if (val & 0x1A) { /* check for error */
246 printf ("\nFlash write error %02x at address %08lx\n",
247 (int) val, (unsigned long) dest);
248 if (val & (1 << 3)) {
249 printf ("Voltage range error.\n");
253 if (val & (1 << 1)) {
254 printf ("Device protect error.\n");
258 if (val & (1 << 4)) {
259 printf ("Programming error.\n");
268 /* read array command */
272 enable_interrupts ();
277 /*-----------------------------------------------------------------------
278 * Copy memory to flash.
281 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
288 wp = (addr & ~1); /* get lower word aligned address */
291 * handle unaligned start bytes
293 if ((l = addr - wp) != 0) {
295 for (i = 0, cp = wp; i < l; ++i, ++cp) {
296 data = (data >> 8) | (*(uchar *) cp << 8);
298 for (; i < 2 && cnt > 0; ++i) {
299 data = (data >> 8) | (*src++ << 8);
303 for (; cnt == 0 && i < 2; ++i, ++cp) {
304 data = (data >> 8) | (*(uchar *) cp << 8);
307 if ((rc = write_word (info, wp, data)) != 0) {
314 * handle word aligned part
317 data = *((vu_short *) src);
318 if ((rc = write_word (info, wp, data)) != 0) {
331 * handle unaligned tail bytes
334 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
335 data = (data >> 8) | (*src++ << 8);
338 for (; i < 2; ++i, ++cp) {
339 data = (data >> 8) | (*(uchar *) cp << 8);
342 return write_word (info, wp, data);