2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@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,
26 #define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
27 #define FLASH_BANK_SIZE 0x800000
28 #define EN29LV640 0x227e227e
30 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
32 void flash_print_info (flash_info_t * info)
36 switch (info->flash_id & FLASH_VENDMASK) {
37 case (AMD_MANUFACT & FLASH_VENDMASK):
41 printf ("Unknown Vendor ");
45 switch (info->flash_id & FLASH_TYPEMASK) {
46 case (EN29LV640 & FLASH_TYPEMASK):
47 printf ("EN29LV640 (16Mbit)\n");
50 printf ("Unknown Chip Type\n");
55 printf (" Size: %ld MB in %d Sectors\n",
56 info->size >> 20, info->sector_count);
58 printf (" Sector Start Addresses:");
59 for (i = 0; i < info->sector_count; i++) {
63 printf (" %08lX%s", info->start[i],
64 info->protect[i] ? " (RO)" : " ");
73 unsigned long flash_init (void)
78 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
81 flash_info[i].flash_id =
82 (AMD_MANUFACT & FLASH_VENDMASK) |
83 (EN29LV640 & FLASH_TYPEMASK);
84 flash_info[i].size = FLASH_BANK_SIZE;
85 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
86 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
88 flashbase = PHYS_FLASH_1;
90 panic ("configured to many flash banks!\n");
92 for (j = 0; j < flash_info[i].sector_count; j++) {
93 flash_info[i].start[j] = flashbase + 0x10000 * j;
95 size += flash_info[i].size;
98 flash_protect (FLAG_PROTECT_SET,
99 CONFIG_SYS_FLASH_BASE,
100 CONFIG_SYS_FLASH_BASE + 0x2ffff, &flash_info[0]);
106 #define CMD_READ_ARRAY 0x00F0
107 #define CMD_UNLOCK1 0x00AA
108 #define CMD_UNLOCK2 0x0055
109 #define CMD_ERASE_SETUP 0x0080
110 #define CMD_ERASE_CONFIRM 0x0030
111 #define CMD_PROGRAM 0x00A0
112 #define CMD_UNLOCK_BYPASS 0x0020
114 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00000555<<1)))
115 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x000002AA<<1)))
117 #define BIT_ERASE_DONE 0x0080
118 #define BIT_RDY_MASK 0x0080
119 #define BIT_PROGRAM_ERROR 0x0020
120 #define BIT_TIMEOUT 0x80000000 /* our flag */
127 int flash_erase (flash_info_t * info, int s_first, int s_last)
130 int iflag, prot, sect;
134 /* first look for protection bits */
136 if (info->flash_id == FLASH_UNKNOWN)
137 return ERR_UNKNOWN_FLASH_TYPE;
139 if ((s_first < 0) || (s_first > s_last)) {
143 if ((info->flash_id & FLASH_VENDMASK) !=
144 (AMD_MANUFACT & FLASH_VENDMASK)) {
145 return ERR_UNKNOWN_FLASH_VENDOR;
149 for (sect = s_first; sect <= s_last; ++sect) {
150 if (info->protect[sect]) {
155 return ERR_PROTECTED;
158 * Disable interrupts which might cause a timeout
159 * here. Remember that our exception vectors are
160 * at address 0 in the flash, and we don't want a
161 * (ticker) exception to happen while the flash
162 * chip is in programming mode.
164 iflag = disable_interrupts ();
168 /* Start erase on unprotected sectors */
169 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
170 printf ("Erasing sector %2d ... ", sect);
172 /* arm simple, non interrupt dependent timer */
175 if (info->protect[sect] == 0) { /* not protected */
177 (volatile u16 *) (info->start[sect]);
179 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
180 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
181 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
183 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
184 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
185 *addr = CMD_ERASE_CONFIRM;
187 /* wait until flash is ready */
194 if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
195 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
201 && (result & 0xFFFF) & BIT_ERASE_DONE)
206 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
218 } else { /* it was protected */
220 printf ("protected!\n");
225 printf ("User Interrupt!\n");
228 /* allow flash to settle - wait 10 ms */
229 printf("Waiting 10 ms...");
232 /* for (i = 0; i < 10 * 1000 * 1000; ++i)
238 enable_interrupts ();
244 static int write_word (flash_info_t * info, ulong dest, ulong data)
246 volatile u16 *addr = (volatile u16 *) dest;
253 * Check if Flash is (sufficiently) erased
256 if ((result & data) != data)
257 return ERR_NOT_ERASED;
261 * Disable interrupts which might cause a timeout
262 * here. Remember that our exception vectors are
263 * at address 0 in the flash, and we don't want a
264 * (ticker) exception to happen while the flash
265 * chip is in programming mode.
267 iflag = disable_interrupts ();
269 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
270 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
271 MEM_FLASH_ADDR1 = CMD_PROGRAM;
274 /* arm simple, non interrupt dependent timer */
277 /* wait until flash is ready */
283 if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
287 if (!chip1 && ((result & 0x80) == (data & 0x80)))
292 *addr = CMD_READ_ARRAY;
294 if (chip1 == ERR || *addr != data)
298 enable_interrupts ();
304 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
310 printf ("unaligned destination not supported\n");
316 printf ("odd transfer sizes not supported\n");
324 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
326 if ((rc = write_word (info, wp - 1, data)) != 0) {
335 data = *((volatile u16 *) src);
336 if ((rc = write_word (info, wp, data)) != 0) {
345 data = (*((volatile u8 *) src) << 8) |
346 *((volatile u8 *) (wp + 1));
347 if ((rc = write_word (info, wp, data)) != 0) {