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;
135 /* first look for protection bits */
137 if (info->flash_id == FLASH_UNKNOWN)
138 return ERR_UNKNOWN_FLASH_TYPE;
140 if ((s_first < 0) || (s_first > s_last)) {
144 if ((info->flash_id & FLASH_VENDMASK) !=
145 (AMD_MANUFACT & FLASH_VENDMASK)) {
146 return ERR_UNKNOWN_FLASH_VENDOR;
150 for (sect = s_first; sect <= s_last; ++sect) {
151 if (info->protect[sect]) {
156 return ERR_PROTECTED;
159 * Disable interrupts which might cause a timeout
160 * here. Remember that our exception vectors are
161 * at address 0 in the flash, and we don't want a
162 * (ticker) exception to happen while the flash
163 * chip is in programming mode.
165 iflag = disable_interrupts ();
169 /* Start erase on unprotected sectors */
170 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
171 printf ("Erasing sector %2d ... ", sect);
173 /* arm simple, non interrupt dependent timer */
174 start = get_timer(0);
176 if (info->protect[sect] == 0) { /* not protected */
178 (volatile u16 *) (info->start[sect]);
180 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
181 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
182 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
184 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
185 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
186 *addr = CMD_ERASE_CONFIRM;
188 /* wait until flash is ready */
195 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
196 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
202 && (result & 0xFFFF) & BIT_ERASE_DONE)
207 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
219 } else { /* it was protected */
221 printf ("protected!\n");
226 printf ("User Interrupt!\n");
229 /* allow flash to settle - wait 10 ms */
230 printf("Waiting 10 ms...");
233 /* for (i = 0; i < 10 * 1000 * 1000; ++i)
239 enable_interrupts ();
245 static int write_word (flash_info_t * info, ulong dest, ulong data)
247 volatile u16 *addr = (volatile u16 *) dest;
255 * Check if Flash is (sufficiently) erased
258 if ((result & data) != data)
259 return ERR_NOT_ERASED;
263 * Disable interrupts which might cause a timeout
264 * here. Remember that our exception vectors are
265 * at address 0 in the flash, and we don't want a
266 * (ticker) exception to happen while the flash
267 * chip is in programming mode.
269 iflag = disable_interrupts ();
271 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
272 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
273 MEM_FLASH_ADDR1 = CMD_PROGRAM;
276 /* arm simple, non interrupt dependent timer */
277 start = get_timer(0);
279 /* wait until flash is ready */
285 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
289 if (!chip1 && ((result & 0x80) == (data & 0x80)))
294 *addr = CMD_READ_ARRAY;
296 if (chip1 == ERR || *addr != data)
300 enable_interrupts ();
306 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
312 printf ("unaligned destination not supported\n");
318 printf ("odd transfer sizes not supported\n");
326 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
328 if ((rc = write_word (info, wp - 1, data)) != 0) {
337 data = *((volatile u16 *) src);
338 if ((rc = write_word (info, wp, data)) != 0) {
347 data = (*((volatile u8 *) src) << 8) |
348 *((volatile u8 *) (wp + 1));
349 if ((rc = write_word (info, wp, data)) != 0) {