3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #define FLASH_BANK_SIZE 0x2000000
31 #define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
36 /*-----------------------------------------------------------------------
39 ulong flash_init(void)
44 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
47 flash_info[i].flash_id =
48 (INTEL_MANUFACT & FLASH_VENDMASK) |
49 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
50 flash_info[i].size = FLASH_BANK_SIZE;
51 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
52 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
56 flashbase = PHYS_FLASH_1;
59 flashbase = PHYS_FLASH_2;
62 panic("configured to many flash banks!\n");
65 for (j = 0; j < flash_info[i].sector_count; j++)
67 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
69 size += flash_info[i].size;
72 /* Protect monitor and environment sectors
74 flash_protect(FLAG_PROTECT_SET,
76 CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
79 flash_protect(FLAG_PROTECT_SET,
81 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
87 /*-----------------------------------------------------------------------
89 void flash_print_info (flash_info_t *info)
93 for (j=0; j<CFG_MAX_FLASH_BANKS; j++)
95 switch (info->flash_id & FLASH_VENDMASK)
97 case (INTEL_MANUFACT & FLASH_VENDMASK):
101 printf("Unknown Vendor ");
105 switch (info->flash_id & FLASH_TYPEMASK)
107 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
108 printf("28F128J3 (128Mbit)\n");
111 printf("Unknown Chip Type\n");
116 printf(" Size: %ld MB in %d Sectors\n",
117 info->size >> 20, info->sector_count);
119 printf(" Sector Start Addresses:");
120 for (i = 0; i < info->sector_count; i++)
126 printf (" %08lX%s", info->start[i],
127 info->protect[i] ? " (RO)" : " ");
136 /*-----------------------------------------------------------------------
139 int flash_erase (flash_info_t *info, int s_first, int s_last)
141 int flag, prot, sect;
144 if (info->flash_id == FLASH_UNKNOWN)
145 return ERR_UNKNOWN_FLASH_TYPE;
147 if ((s_first < 0) || (s_first > s_last)) {
151 if ((info->flash_id & FLASH_VENDMASK) !=
152 (INTEL_MANUFACT & FLASH_VENDMASK)) {
153 return ERR_UNKNOWN_FLASH_VENDOR;
157 for (sect=s_first; sect<=s_last; ++sect) {
158 if (info->protect[sect]) {
163 return ERR_PROTECTED;
166 * Disable interrupts which might cause a timeout
167 * here. Remember that our exception vectors are
168 * at address 0 in the flash, and we don't want a
169 * (ticker) exception to happen while the flash
170 * chip is in programming mode.
172 flag = disable_interrupts();
174 /* Start erase on unprotected sectors */
175 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
177 printf("Erasing sector %2d ... ", sect);
179 /* arm simple, non interrupt dependent timer */
180 reset_timer_masked();
182 if (info->protect[sect] == 0) { /* not protected */
183 vu_short *addr = (vu_short *)(info->start[sect]);
185 *addr = 0x20; /* erase setup */
186 *addr = 0xD0; /* erase confirm */
188 while ((*addr & 0x80) != 0x80) {
189 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
190 *addr = 0xB0; /* suspend erase */
191 *addr = 0xFF; /* reset to read mode */
197 /* clear status register command */
199 /* reset to read mode */
205 printf("User Interrupt!\n");
209 /* allow flash to settle - wait 10 ms */
210 udelay_masked(10000);
218 /*-----------------------------------------------------------------------
219 * Copy memory to flash
222 static int write_word (flash_info_t *info, ulong dest, ushort data)
224 vu_short *addr = (vu_short *)dest, val;
228 /* Check if Flash is (sufficiently) erased
230 if ((*addr & data) != data)
231 return ERR_NOT_ERASED;
234 * Disable interrupts which might cause a timeout
235 * here. Remember that our exception vectors are
236 * at address 0 in the flash, and we don't want a
237 * (ticker) exception to happen while the flash
238 * chip is in programming mode.
240 flag = disable_interrupts();
242 /* clear status register command */
245 /* program set-up command */
248 /* latch address/data */
251 /* arm simple, non interrupt dependent timer */
252 reset_timer_masked();
254 /* wait while polling the status register */
255 while(((val = *addr) & 0x80) != 0x80)
257 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
259 /* suspend program command */
265 if(val & 0x1A) { /* check for error */
266 printf("\nFlash write error %02x at address %08lx\n",
267 (int)val, (unsigned long)dest);
269 printf("Voltage range error.\n");
274 printf("Device protect error.\n");
279 printf("Programming error.\n");
288 /* read array command */
297 /*-----------------------------------------------------------------------
298 * Copy memory to flash.
301 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
308 wp = (addr & ~1); /* get lower word aligned address */
311 * handle unaligned start bytes
313 if ((l = addr - wp) != 0) {
315 for (i=0, cp=wp; i<l; ++i, ++cp) {
316 data = (data >> 8) | (*(uchar *)cp << 8);
318 for (; i<2 && cnt>0; ++i) {
319 data = (data >> 8) | (*src++ << 8);
323 for (; cnt==0 && i<2; ++i, ++cp) {
324 data = (data >> 8) | (*(uchar *)cp << 8);
327 if ((rc = write_word(info, wp, data)) != 0) {
334 * handle word aligned part
337 data = *((vu_short*)src);
338 if ((rc = write_word(info, wp, data)) != 0) {
351 * handle unaligned tail bytes
354 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
355 data = (data >> 8) | (*src++ << 8);
358 for (; i<2; ++i, ++cp) {
359 data = (data >> 8) | (*(uchar *)cp << 8);
362 return write_word(info, wp, data);