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 0x400000
31 #define MAIN_SECT_SIZE 0x20000
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_28F320J3A & FLASH_TYPEMASK):
108 printf("28F320J3A (32Mbit)\n");
110 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
111 printf("28F128J3 (128Mbit)\n");
114 printf("Unknown Chip Type\n");
119 printf(" Size: %ld MB in %d Sectors\n",
120 info->size >> 20, info->sector_count);
122 printf(" Sector Start Addresses:");
123 for (i = 0; i < info->sector_count; i++)
129 printf (" %08lX%s", info->start[i],
130 info->protect[i] ? " (RO)" : " ");
139 /*-----------------------------------------------------------------------
142 int flash_erase (flash_info_t *info, int s_first, int s_last)
144 int flag, prot, sect;
147 if (info->flash_id == FLASH_UNKNOWN)
148 return ERR_UNKNOWN_FLASH_TYPE;
150 if ((s_first < 0) || (s_first > s_last)) {
154 if ((info->flash_id & FLASH_VENDMASK) !=
155 (INTEL_MANUFACT & FLASH_VENDMASK)) {
156 return ERR_UNKNOWN_FLASH_VENDOR;
160 for (sect=s_first; sect<=s_last; ++sect) {
161 if (info->protect[sect]) {
166 return ERR_PROTECTED;
169 * Disable interrupts which might cause a timeout
170 * here. Remember that our exception vectors are
171 * at address 0 in the flash, and we don't want a
172 * (ticker) exception to happen while the flash
173 * chip is in programming mode.
175 flag = disable_interrupts();
177 /* Start erase on unprotected sectors */
178 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
180 printf("Erasing sector %2d ... ", sect);
182 /* arm simple, non interrupt dependent timer */
183 reset_timer_masked();
185 if (info->protect[sect] == 0) { /* not protected */
186 vu_short *addr = (vu_short *)(info->start[sect]);
188 *addr = 0x20; /* erase setup */
189 *addr = 0xD0; /* erase confirm */
191 while ((*addr & 0x80) != 0x80) {
192 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
193 *addr = 0xB0; /* suspend erase */
194 *addr = 0xFF; /* reset to read mode */
200 /* clear status register command */
202 /* reset to read mode */
208 printf("User Interrupt!\n");
212 /* allow flash to settle - wait 10 ms */
213 udelay_masked(10000);
221 /*-----------------------------------------------------------------------
222 * Copy memory to flash
225 static int write_word (flash_info_t *info, ulong dest, ushort data)
227 vu_short *addr = (vu_short *)dest, val;
231 /* Check if Flash is (sufficiently) erased
233 if ((*addr & data) != data)
234 return ERR_NOT_ERASED;
237 * Disable interrupts which might cause a timeout
238 * here. Remember that our exception vectors are
239 * at address 0 in the flash, and we don't want a
240 * (ticker) exception to happen while the flash
241 * chip is in programming mode.
243 flag = disable_interrupts();
245 /* clear status register command */
248 /* program set-up command */
251 /* latch address/data */
254 /* arm simple, non interrupt dependent timer */
255 reset_timer_masked();
257 /* wait while polling the status register */
258 while(((val = *addr) & 0x80) != 0x80)
260 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
262 /* suspend program command */
268 if(val & 0x1A) { /* check for error */
269 printf("\nFlash write error %02x at address %08lx\n",
270 (int)val, (unsigned long)dest);
272 printf("Voltage range error.\n");
277 printf("Device protect error.\n");
282 printf("Programming error.\n");
291 /* read array command */
300 /*-----------------------------------------------------------------------
301 * Copy memory to flash.
304 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
311 wp = (addr & ~1); /* get lower word aligned address */
314 * handle unaligned start bytes
316 if ((l = addr - wp) != 0)
319 for (i=0, cp=wp; i<l; ++i, ++cp) {
320 data = (data >> 8) | (*(uchar *)cp << 8);
322 for (; i<2 && cnt>0; ++i) {
323 data = (data >> 8) | (*src++ << 8);
327 for (; cnt==0 && i<2; ++i, ++cp) {
328 data = (data >> 8) | (*(uchar *)cp << 8);
331 if ((rc = write_word(info, wp, data)) != 0) {
338 * handle word aligned part
341 data = *((vu_short*)src);
342 if ((rc = write_word(info, wp, data)) != 0) {
355 * handle unaligned tail bytes
358 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
359 data = (data >> 8) | (*src++ << 8);
362 for (; i<2; ++i, ++cp) {
363 data = (data >> 8) | (*(uchar *)cp << 8);
366 return write_word(info, wp, data);