3 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <environment.h>
32 #define FLASH_BANK_SIZE 0x800000 /* 8 MB */
33 /* this varies depending on the sector */
34 #define MAIN_SECT_SIZE 0x20000 /* 2 x 64 kB */
36 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
39 #define CMD_READ_ARRAY 0x00F000F0
40 #define CMD_UNLOCK1 0x00AA00AA
41 #define CMD_UNLOCK2 0x00550055
42 #define CMD_ERASE_SETUP 0x00800080
43 #define CMD_ERASE_CONFIRM 0x00300030
44 #define CMD_PROGRAM 0x00A000A0
45 #define CMD_UNLOCK_BYPASS 0x00200020
47 #define MEM_FLASH_ADDR1 (*(volatile u32 *)(CFG_FLASH_BASE + (0x00000555 << 2)))
48 #define MEM_FLASH_ADDR2 (*(volatile u32 *)(CFG_FLASH_BASE + (0x000002AA << 2)))
50 #define BIT_ERASE_DONE 0x00800080
51 #define BIT_RDY_MASK 0x00800080
52 #define BIT_PROGRAM_ERROR 0x00200020
53 #define BIT_TIMEOUT 0x80000000 /* our flag */
59 /*-----------------------------------------------------------------------
62 ulong flash_init (void)
67 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
70 flash_info[i].flash_id =
71 (AMD_MANUFACT & FLASH_VENDMASK) |
72 (AMD_ID_LV320B & FLASH_TYPEMASK);
73 flash_info[i].size = FLASH_BANK_SIZE;
74 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
75 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
77 flashbase = PHYS_FLASH_1;
79 panic ("configured too many flash banks!\n");
80 for (j = 0; j < flash_info[i].sector_count; j++) {
82 flash_info[i].start[j] = flashbase;
84 /* the first 8 sectors are 8 kB */
85 flashbase += (j < 8) ? 0x4000 : MAIN_SECT_SIZE;
87 size += flash_info[i].size;
91 * Protect monitor and environment sectors
93 flash_protect ( FLAG_PROTECT_SET,
95 CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
98 flash_protect ( FLAG_PROTECT_SET,
100 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
102 #ifdef CFG_ENV_ADDR_REDUND
103 flash_protect ( FLAG_PROTECT_SET,
105 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
112 /*-----------------------------------------------------------------------
114 void flash_print_info (flash_info_t * info)
118 switch (info->flash_id & FLASH_VENDMASK) {
119 case (AMD_MANUFACT & FLASH_VENDMASK):
123 printf ("Unknown Vendor ");
127 switch (info->flash_id & FLASH_TYPEMASK) {
128 case (AMD_ID_LV320B & FLASH_TYPEMASK):
129 printf ("2x Am29LV320DB (32Mbit)\n");
132 printf ("Unknown Chip Type\n");
137 printf (" Size: %ld MB in %d Sectors\n",
138 info->size >> 20, info->sector_count);
140 printf (" Sector Start Addresses:");
141 for (i = 0; i < info->sector_count; i++) {
147 info->protect[i] ? " (RO)" : " ");
154 /*-----------------------------------------------------------------------
157 int flash_erase (flash_info_t * info, int s_first, int s_last)
164 int iflag, prot, sect;
168 debug ("flash_erase: s_first %d s_last %d\n", s_first, s_last);
170 /* first look for protection bits */
172 if (info->flash_id == FLASH_UNKNOWN)
173 return ERR_UNKNOWN_FLASH_TYPE;
175 if ((s_first < 0) || (s_first > s_last)) {
179 if ((info->flash_id & FLASH_VENDMASK) !=
180 (AMD_MANUFACT & FLASH_VENDMASK)) {
181 return ERR_UNKNOWN_FLASH_VENDOR;
185 for (sect = s_first; sect <= s_last; ++sect) {
186 if (info->protect[sect]) {
192 printf ("- Warning: %d protected sectors will not be erased!\n",
199 * Disable interrupts which might cause a timeout
200 * here. Remember that our exception vectors are
201 * at address 0 in the flash, and we don't want a
202 * (ticker) exception to happen while the flash
203 * chip is in programming mode.
206 cflag = icache_status ();
209 iflag = disable_interrupts ();
211 /* Start erase on unprotected sectors */
212 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
214 debug ("Erasing sector %2d @ %08lX... ",
215 sect, info->start[sect]);
217 /* arm simple, non interrupt dependent timer */
218 reset_timer_masked ();
220 if (info->protect[sect] == 0) { /* not protected */
221 vu_long *addr = (vu_long *) (info->start[sect]);
223 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
224 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
225 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
227 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
228 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
229 *addr = CMD_ERASE_CONFIRM;
231 /* wait until flash is ready */
238 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
239 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
244 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
247 if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
250 if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
253 if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
256 } while (!chip1 || !chip2);
258 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
260 if (chip1 == ERR || chip2 == ERR) {
271 } else { /* it was protected */
273 printf ("protected!\n");
279 /* allow flash to settle - wait 10 ms */
280 udelay_masked (10000);
283 enable_interrupts ();
292 /*-----------------------------------------------------------------------
293 * Copy memory to flash
296 volatile static int write_word (flash_info_t * info, ulong dest,
299 vu_long *addr = (vu_long *) dest;
310 * Check if Flash is (sufficiently) erased
313 if ((result & data) != data)
314 return ERR_NOT_ERASED;
317 * Disable interrupts which might cause a timeout
318 * here. Remember that our exception vectors are
319 * at address 0 in the flash, and we don't want a
320 * (ticker) exception to happen while the flash
321 * chip is in programming mode.
324 cflag = icache_status ();
327 iflag = disable_interrupts ();
329 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
330 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
331 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
335 /* arm simple, non interrupt dependent timer */
336 reset_timer_masked ();
338 /* wait until flash is ready */
344 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
348 if (!chip1 && ((result & 0x80) == (data & 0x80)))
351 if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
354 if ((result & 0x80) == (data & 0x80))
360 if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
363 if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR)) {
366 if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
372 } while (!chip1 || !chip2);
374 *addr = CMD_READ_ARRAY;
376 if (chip1 == ERR || chip2 == ERR || *addr != data)
380 enable_interrupts ();
390 /*-----------------------------------------------------------------------
391 * Copy memory to flash.
394 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
400 wp = (addr & ~3); /* get lower word aligned address */
403 * handle unaligned start bytes
405 if ((l = addr - wp) != 0) {
407 for (i = 0, cp = wp; i < l; ++i, ++cp) {
408 data = (data >> 8) | (*(uchar *) cp << 24);
410 for (; i < 4 && cnt > 0; ++i) {
411 data = (data >> 8) | (*src++ << 24);
415 for (; cnt == 0 && i < 4; ++i, ++cp) {
416 data = (data >> 8) | (*(uchar *) cp << 24);
419 if ((rc = write_word (info, wp, data)) != 0) {
426 * handle word aligned part
429 data = *((vu_long *) src);
430 if ((rc = write_word (info, wp, data)) != 0) {
443 * handle unaligned tail bytes
446 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
447 data = (data >> 8) | (*src++ << 24);
450 for (; i < 4; ++i, ++cp) {
451 data = (data >> 8) | (*(uchar *) cp << 24);
454 return write_word (info, wp, data);