3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Alex Zuepke <azu@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,
30 #define FLASH_BANK_SIZE PHYS_FLASH_SIZE
31 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
36 #define CMD_READ_ARRAY 0x000000F0
37 #define CMD_UNLOCK1 0x000000AA
38 #define CMD_UNLOCK2 0x00000055
39 #define CMD_ERASE_SETUP 0x00000080
40 #define CMD_ERASE_CONFIRM 0x00000030
41 #define CMD_PROGRAM 0x000000A0
42 #define CMD_UNLOCK_BYPASS 0x00000020
44 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
45 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
47 #define BIT_ERASE_DONE 0x00000080
48 #define BIT_RDY_MASK 0x00000080
49 #define BIT_PROGRAM_ERROR 0x00000020
50 #define BIT_TIMEOUT 0x80000000 /* our flag */
56 /*-----------------------------------------------------------------------
59 ulong flash_init(void)
64 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
67 flash_info[i].flash_id =
68 #if defined(CONFIG_AMD_LV400)
69 (AMD_MANUFACT & FLASH_VENDMASK) |
70 (AMD_ID_LV400B & FLASH_TYPEMASK);
71 #elif defined(CONFIG_AMD_LV800)
72 (AMD_MANUFACT & FLASH_VENDMASK) |
73 (AMD_ID_LV800B & FLASH_TYPEMASK);
75 #error "Unknown flash configured"
77 flash_info[i].size = FLASH_BANK_SIZE;
78 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
79 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
81 flashbase = PHYS_FLASH_1;
83 panic("configured too many flash banks!\n");
84 for (j = 0; j < flash_info[i].sector_count; j++)
88 /* 1st one is 16 KB */
91 flash_info[i].start[j] = flashbase + 0;
94 /* 2nd and 3rd are both 8 KB */
95 if ((j == 1) || (j == 2))
97 flash_info[i].start[j] = flashbase + 0x4000 + (j-1)*0x2000;
103 flash_info[i].start[j] = flashbase + 0x8000;
108 flash_info[i].start[j] = flashbase + (j - 3)*MAIN_SECT_SIZE;
111 size += flash_info[i].size;
114 flash_protect(FLAG_PROTECT_SET,
116 CFG_FLASH_BASE + monitor_flash_len - 1,
119 flash_protect(FLAG_PROTECT_SET,
121 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
127 /*-----------------------------------------------------------------------
129 void flash_print_info (flash_info_t *info)
133 switch (info->flash_id & FLASH_VENDMASK)
135 case (AMD_MANUFACT & FLASH_VENDMASK):
139 printf("Unknown Vendor ");
143 switch (info->flash_id & FLASH_TYPEMASK)
145 case (AMD_ID_LV400B & FLASH_TYPEMASK):
146 printf("1x Amd29LV400BB (4Mbit)\n");
148 case (AMD_ID_LV800B & FLASH_TYPEMASK):
149 printf("1x Amd29LV800BB (8Mbit)\n");
152 printf("Unknown Chip Type\n");
157 printf(" Size: %ld MB in %d Sectors\n",
158 info->size >> 20, info->sector_count);
160 printf(" Sector Start Addresses:");
161 for (i = 0; i < info->sector_count; i++)
167 printf (" %08lX%s", info->start[i],
168 info->protect[i] ? " (RO)" : " ");
175 /*-----------------------------------------------------------------------
178 int flash_erase (flash_info_t *info, int s_first, int s_last)
181 int iflag, cflag, prot, sect;
185 /* first look for protection bits */
187 if (info->flash_id == FLASH_UNKNOWN)
188 return ERR_UNKNOWN_FLASH_TYPE;
190 if ((s_first < 0) || (s_first > s_last)) {
194 if ((info->flash_id & FLASH_VENDMASK) !=
195 (AMD_MANUFACT & FLASH_VENDMASK)) {
196 return ERR_UNKNOWN_FLASH_VENDOR;
200 for (sect=s_first; sect<=s_last; ++sect) {
201 if (info->protect[sect]) {
206 return ERR_PROTECTED;
209 * Disable interrupts which might cause a timeout
210 * here. Remember that our exception vectors are
211 * at address 0 in the flash, and we don't want a
212 * (ticker) exception to happen while the flash
213 * chip is in programming mode.
215 cflag = icache_status();
217 iflag = disable_interrupts();
219 /* Start erase on unprotected sectors */
220 for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
222 printf("Erasing sector %2d ... ", sect);
224 /* arm simple, non interrupt dependent timer */
225 reset_timer_masked();
227 if (info->protect[sect] == 0)
228 { /* not protected */
229 vu_short *addr = (vu_short *)(info->start[sect]);
231 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
232 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
233 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
235 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
236 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
237 *addr = CMD_ERASE_CONFIRM;
239 /* wait until flash is ready */
247 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
249 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
254 if (!chip && (result & 0xFFFF) & BIT_ERASE_DONE)
257 if (!chip && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
262 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
277 else /* it was protected */
279 printf("protected!\n");
284 printf("User Interrupt!\n");
287 /* allow flash to settle - wait 10 ms */
288 udelay_masked(10000);
299 /*-----------------------------------------------------------------------
300 * Copy memory to flash
303 volatile static int write_hword (flash_info_t *info, ulong dest, ushort data)
305 vu_short *addr = (vu_short *)dest;
312 * Check if Flash is (sufficiently) erased
315 if ((result & data) != data)
316 return ERR_NOT_ERASED;
320 * Disable interrupts which might cause a timeout
321 * here. Remember that our exception vectors are
322 * at address 0 in the flash, and we don't want a
323 * (ticker) exception to happen while the flash
324 * chip is in programming mode.
326 cflag = icache_status();
328 iflag = disable_interrupts();
330 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
331 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
332 MEM_FLASH_ADDR1 = CMD_PROGRAM;
335 /* arm simple, non interrupt dependent timer */
336 reset_timer_masked();
338 /* wait until flash is ready */
345 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
350 if (!chip && ((result & 0x80) == (data & 0x80)))
353 if (!chip && ((result & 0xFFFF) & BIT_PROGRAM_ERROR))
357 if ((result & 0x80) == (data & 0x80))
365 *addr = CMD_READ_ARRAY;
367 if (chip == ERR || *addr != data)
379 /*-----------------------------------------------------------------------
380 * Copy memory to flash.
383 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
390 wp = (addr & ~1); /* get lower word aligned address */
393 * handle unaligned start bytes
395 if ((l = addr - wp) != 0) {
397 for (i=0, cp=wp; i<l; ++i, ++cp) {
398 data = (data >> 8) | (*(uchar *)cp << 8);
400 for (; i<2 && cnt>0; ++i) {
401 data = (data >> 8) | (*src++ << 8);
405 for (; cnt==0 && i<2; ++i, ++cp) {
406 data = (data >> 8) | (*(uchar *)cp << 8);
409 if ((rc = write_hword(info, wp, data)) != 0) {
416 * handle word aligned part
419 data = *((vu_short*)src);
420 if ((rc = write_hword(info, wp, data)) != 0) {
433 * handle unaligned tail bytes
436 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
437 data = (data >> 8) | (*src++ << 8);
440 for (; i<2; ++i, ++cp) {
441 data = (data >> 8) | (*(uchar *)cp << 8);
444 return write_hword(info, wp, data);