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 0x800000
28 #define MAIN_SECT_SIZE 0x20000
29 #define PARAM_SECT_SIZE 0x4000
31 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
34 /*-----------------------------------------------------------------------
37 ulong flash_init (void)
42 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
45 flash_info[i].flash_id =
46 (INTEL_MANUFACT & FLASH_VENDMASK) |
47 (INTEL_ID_28F320B3T & FLASH_TYPEMASK);
48 flash_info[i].size = FLASH_BANK_SIZE;
49 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
50 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
52 flashbase = PHYS_FLASH_1;
54 flashbase = PHYS_FLASH_2;
56 panic ("configured too many flash banks!\n");
57 for (j = 0; j < flash_info[i].sector_count; j++) {
59 flash_info[i].start[j] =
60 flashbase + j * PARAM_SECT_SIZE;
62 flash_info[i].start[j] =
63 flashbase + (j - 7) * MAIN_SECT_SIZE;
66 size += flash_info[i].size;
69 /* Protect monitor and environment sectors
71 flash_protect (FLAG_PROTECT_SET,
72 CONFIG_SYS_FLASH_BASE,
73 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
76 flash_protect (FLAG_PROTECT_SET,
78 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
83 /*-----------------------------------------------------------------------
85 void flash_print_info (flash_info_t * info)
89 switch (info->flash_id & FLASH_VENDMASK) {
90 case (INTEL_MANUFACT & FLASH_VENDMASK):
94 printf ("Unknown Vendor ");
98 switch (info->flash_id & FLASH_TYPEMASK) {
99 case (INTEL_ID_28F320B3T & FLASH_TYPEMASK):
100 printf ("28F320F3B (16Mbit)\n");
103 printf ("Unknown Chip Type\n");
108 printf (" Size: %ld MB in %d Sectors\n",
109 info->size >> 20, info->sector_count);
111 printf (" Sector Start Addresses:");
112 for (i = 0; i < info->sector_count; i++) {
116 printf (" %08lX%s", info->start[i],
117 info->protect[i] ? " (RO)" : " ");
124 /*-----------------------------------------------------------------------
127 int flash_erase (flash_info_t * info, int s_first, int s_last)
129 int flag, prot, sect;
132 if (info->flash_id == FLASH_UNKNOWN)
133 return ERR_UNKNOWN_FLASH_TYPE;
135 if ((s_first < 0) || (s_first > s_last)) {
139 if ((info->flash_id & FLASH_VENDMASK) !=
140 (INTEL_MANUFACT & FLASH_VENDMASK)) {
141 return ERR_UNKNOWN_FLASH_VENDOR;
145 for (sect = s_first; sect <= s_last; ++sect) {
146 if (info->protect[sect]) {
151 return ERR_PROTECTED;
154 * Disable interrupts which might cause a timeout
155 * here. Remember that our exception vectors are
156 * at address 0 in the flash, and we don't want a
157 * (ticker) exception to happen while the flash
158 * chip is in programming mode.
160 flag = disable_interrupts ();
162 /* Start erase on unprotected sectors */
163 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
165 printf ("Erasing sector %2d ... ", sect);
167 /* arm simple, non interrupt dependent timer */
168 reset_timer_masked ();
170 if (info->protect[sect] == 0) { /* not protected */
171 vu_long *addr = (vu_long *) (info->start[sect]);
173 *addr = 0x00200020; /* erase setup */
174 *addr = 0x00D000D0; /* erase confirm */
176 while ((*addr & 0x00800080) != 0x00800080) {
177 if (get_timer_masked () >
178 CONFIG_SYS_FLASH_ERASE_TOUT) {
179 *addr = 0x00B000B0; /* suspend erase */
180 *addr = 0x00FF00FF; /* reset to read mode */
186 *addr = 0x00FF00FF; /* reset to read mode */
191 printf ("User Interrupt!\n");
195 /* allow flash to settle - wait 10 ms */
196 udelay_masked (10000);
199 enable_interrupts ();
204 /*-----------------------------------------------------------------------
205 * Copy memory to flash
208 static int write_word (flash_info_t * info, ulong dest, ulong data)
210 vu_long *addr = (vu_long *) dest;
215 /* Check if Flash is (sufficiently) erased
217 if ((*addr & data) != data)
218 return ERR_NOT_ERASED;
221 * Disable interrupts which might cause a timeout
222 * here. Remember that our exception vectors are
223 * at address 0 in the flash, and we don't want a
224 * (ticker) exception to happen while the flash
225 * chip is in programming mode.
227 flag = disable_interrupts ();
229 /* clear status register command */
232 /* program set-up command */
235 /* latch address/data */
238 /* arm simple, non interrupt dependent timer */
239 reset_timer_masked ();
241 /* read status register command */
244 /* wait while polling the status register */
245 while ((*addr & 0x00800080) != 0x00800080) {
246 if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT) {
248 /* suspend program command */
253 if (*addr & 0x003A003A) { /* check for error */
255 if (barf & 0x003A0000) {
260 printf ("\nFlash write error %02lx at address %08lx\n", barf, (unsigned long) dest);
262 printf ("Block locked, not erased.\n");
267 printf ("Programming error.\n");
272 printf ("Vpp Low error.\n");
283 /* read array command */
287 enable_interrupts ();
292 /*-----------------------------------------------------------------------
293 * Copy memory to flash.
296 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
302 wp = (addr & ~3); /* get lower word aligned address */
305 * handle unaligned start bytes
307 if ((l = addr - wp) != 0) {
309 for (i = 0, cp = wp; i < l; ++i, ++cp) {
310 data = (data >> 8) | (*(uchar *) cp << 24);
312 for (; i < 4 && cnt > 0; ++i) {
313 data = (data >> 8) | (*src++ << 24);
317 for (; cnt == 0 && i < 4; ++i, ++cp) {
318 data = (data >> 8) | (*(uchar *) cp << 24);
321 if ((rc = write_word (info, wp, data)) != 0) {
328 * handle word aligned part
331 data = *((vu_long *) src);
332 if ((rc = write_word (info, wp, data)) != 0) {
345 * handle unaligned tail bytes
348 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
349 data = (data >> 8) | (*src++ << 24);
352 for (; i < 4; ++i, ++cp) {
353 data = (data >> 8) | (*(uchar *) cp << 24);
356 return write_word (info, wp, data);