3 * Lineo, Inc. <www.lineo.com>
4 * Bernhard Kuhn <bkuhn@lineo.com>
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Alex Zuepke <azu@sysgo.de>
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #define FLASH_BANK_SIZE 0x200000 /* 2 MB */
35 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
37 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
40 #define CMD_READ_ARRAY 0x00F0
41 #define CMD_UNLOCK1 0x00AA
42 #define CMD_UNLOCK2 0x0055
43 #define CMD_ERASE_SETUP 0x0080
44 #define CMD_ERASE_CONFIRM 0x0030
45 #define CMD_PROGRAM 0x00A0
46 #define CMD_UNLOCK_BYPASS 0x0020
48 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00005555<<1)))
49 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00002AAA<<1)))
51 #define BIT_ERASE_DONE 0x0080
52 #define BIT_RDY_MASK 0x0080
53 #define BIT_PROGRAM_ERROR 0x0020
54 #define BIT_TIMEOUT 0x80000000 /* our flag */
60 /*-----------------------------------------------------------------------
63 ulong flash_init(void)
68 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
71 flash_info[i].flash_id =
72 (ATM_MANUFACT & FLASH_VENDMASK) |
73 (ATM_ID_BV1614 & FLASH_TYPEMASK);
74 flash_info[i].size = FLASH_BANK_SIZE;
75 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
76 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
78 flashbase = PHYS_FLASH_1;
80 panic("configured to many flash banks!\n");
81 for (j = 0; j < flash_info[i].sector_count; j++)
86 /* 1st to 8th are 8 KB */
89 flash_info[i].start[j] = flashbase + j*0x2000;
92 /* 9th and 10th are both 32 KB */
93 if ((j == 8) || (j == 9))
95 flash_info[i].start[j] = flashbase + 0x10000 + (j-8)*0x8000;
100 flash_info[i].start[j] = flashbase + (j-8)*MAIN_SECT_SIZE;
103 size += flash_info[i].size;
106 flash_protect(FLAG_PROTECT_SET,
111 flash_protect(FLAG_PROTECT_SET,
113 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
119 /*-----------------------------------------------------------------------
121 void flash_print_info (flash_info_t *info)
125 switch (info->flash_id & FLASH_VENDMASK)
127 case (ATM_MANUFACT & FLASH_VENDMASK):
131 printf("Unknown Vendor ");
135 switch (info->flash_id & FLASH_TYPEMASK)
137 case (ATM_ID_BV1614 & FLASH_TYPEMASK):
138 printf("AT49BV1614 (16Mbit)\n");
141 printf("Unknown Chip Type\n");
146 printf(" Size: %ld MB in %d Sectors\n",
147 info->size >> 20, info->sector_count);
149 printf(" Sector Start Addresses:");
150 for (i = 0; i < info->sector_count; i++)
156 printf (" %08lX%s", info->start[i],
157 info->protect[i] ? " (RO)" : " ");
164 /*-----------------------------------------------------------------------
167 int flash_erase (flash_info_t *info, int s_first, int s_last)
170 int iflag, cflag, prot, sect;
174 /* first look for protection bits */
176 if (info->flash_id == FLASH_UNKNOWN)
177 return ERR_UNKNOWN_FLASH_TYPE;
179 if ((s_first < 0) || (s_first > s_last)) {
183 if ((info->flash_id & FLASH_VENDMASK) !=
184 (ATM_MANUFACT & FLASH_VENDMASK)) {
185 return ERR_UNKNOWN_FLASH_VENDOR;
189 for (sect=s_first; sect<=s_last; ++sect) {
190 if (info->protect[sect]) {
195 return ERR_PROTECTED;
198 * Disable interrupts which might cause a timeout
199 * here. Remember that our exception vectors are
200 * at address 0 in the flash, and we don't want a
201 * (ticker) exception to happen while the flash
202 * chip is in programming mode.
204 cflag = icache_status();
206 iflag = disable_interrupts();
208 /* Start erase on unprotected sectors */
209 for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
211 printf("Erasing sector %2d ... ", sect);
213 /* arm simple, non interrupt dependent timer */
214 reset_timer_masked();
216 if (info->protect[sect] == 0)
217 { /* not protected */
218 volatile u16 *addr = (volatile u16 *)(info->start[sect]);
220 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
221 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
222 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
224 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
225 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
226 *addr = CMD_ERASE_CONFIRM;
228 /* wait until flash is ready */
236 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
238 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
243 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
248 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
263 else /* it was protected */
265 printf("protected!\n");
270 printf("User Interrupt!\n");
273 /* allow flash to settle - wait 10 ms */
274 udelay_masked(10000);
285 /*-----------------------------------------------------------------------
286 * Copy memory to flash
289 volatile static int write_word (flash_info_t *info, ulong dest, ulong data)
291 volatile u16 *addr = (volatile u16 *)dest;
298 * Check if Flash is (sufficiently) erased
301 if ((result & data) != data)
302 return ERR_NOT_ERASED;
306 * Disable interrupts which might cause a timeout
307 * here. Remember that our exception vectors are
308 * at address 0 in the flash, and we don't want a
309 * (ticker) exception to happen while the flash
310 * chip is in programming mode.
312 cflag = icache_status();
314 iflag = disable_interrupts();
316 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
317 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
318 MEM_FLASH_ADDR1 = CMD_PROGRAM;
321 /* arm simple, non interrupt dependent timer */
322 reset_timer_masked();
324 /* wait until flash is ready */
331 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
336 if (!chip1 && ((result & 0x80) == (data & 0x80)))
341 *addr = CMD_READ_ARRAY;
343 if (chip1 == ERR || *addr != data)
355 /*-----------------------------------------------------------------------
356 * Copy memory to flash.
359 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
365 printf("unaligned destination not supported\n");
370 printf("unaligned source not supported\n");
377 data = *((volatile u16*)src);
378 if ((rc = write_word(info, wp, data)) != 0) {
387 data = (*((volatile u8*)src)) | (*((volatile u8*)(wp+1)) << 8);
388 if ((rc = write_word(info, wp, data)) != 0) {