2 * (C) Copyright 2000-2003
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 CFG_FLASH_BASE
27 #define FLASH_BANK_SIZE 0x200000
29 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
31 void flash_print_info (flash_info_t * info)
35 switch (info->flash_id & FLASH_VENDMASK) {
36 case (AMD_MANUFACT & FLASH_VENDMASK):
40 printf ("Unknown Vendor ");
44 switch (info->flash_id & FLASH_TYPEMASK) {
45 case (AMD_ID_PL160CB & FLASH_TYPEMASK):
46 printf ("AM29PL160CB (16Mbit)\n");
49 printf ("Unknown Chip Type\n");
54 printf (" Size: %ld MB in %d Sectors\n",
55 info->size >> 20, info->sector_count);
57 printf (" Sector Start Addresses:");
58 for (i = 0; i < info->sector_count; i++) {
62 printf (" %08lX%s", info->start[i],
63 info->protect[i] ? " (RO)" : " ");
72 unsigned long flash_init (void)
77 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
80 flash_info[i].flash_id =
81 (AMD_MANUFACT & FLASH_VENDMASK) |
82 (AMD_ID_PL160CB & FLASH_TYPEMASK);
83 flash_info[i].size = FLASH_BANK_SIZE;
84 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
85 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
87 flashbase = PHYS_FLASH_1;
89 panic ("configured to many flash banks!\n");
91 for (j = 0; j < flash_info[i].sector_count; j++) {
94 flash_info[i].start[j] = flashbase;
96 if ((j >= 1) && (j <= 2)) {
97 /* 2nd and 3rd are 8 KiB */
98 flash_info[i].start[j] =
99 flashbase + 0x4000 + 0x2000 * (j - 1);
103 flash_info[i].start[j] = flashbase + 0x8000;
105 if ((j >= 4) && (j <= 10)) {
106 /* rest is 256 KiB */
107 flash_info[i].start[j] =
108 flashbase + 0x40000 + 0x40000 * (j -
112 size += flash_info[i].size;
115 flash_protect (FLAG_PROTECT_SET,
117 CFG_FLASH_BASE + 0x3ffff, &flash_info[0]);
123 #define CMD_READ_ARRAY 0x00F0
124 #define CMD_UNLOCK1 0x00AA
125 #define CMD_UNLOCK2 0x0055
126 #define CMD_ERASE_SETUP 0x0080
127 #define CMD_ERASE_CONFIRM 0x0030
128 #define CMD_PROGRAM 0x00A0
129 #define CMD_UNLOCK_BYPASS 0x0020
131 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555<<1)))
132 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA<<1)))
134 #define BIT_ERASE_DONE 0x0080
135 #define BIT_RDY_MASK 0x0080
136 #define BIT_PROGRAM_ERROR 0x0020
137 #define BIT_TIMEOUT 0x80000000 /* our flag */
144 int flash_erase (flash_info_t * info, int s_first, int s_last)
147 int iflag, cflag, prot, sect;
151 /* first look for protection bits */
153 if (info->flash_id == FLASH_UNKNOWN)
154 return ERR_UNKNOWN_FLASH_TYPE;
156 if ((s_first < 0) || (s_first > s_last)) {
160 if ((info->flash_id & FLASH_VENDMASK) !=
161 (AMD_MANUFACT & FLASH_VENDMASK)) {
162 return ERR_UNKNOWN_FLASH_VENDOR;
166 for (sect = s_first; sect <= s_last; ++sect) {
167 if (info->protect[sect]) {
172 return ERR_PROTECTED;
175 * Disable interrupts which might cause a timeout
176 * here. Remember that our exception vectors are
177 * at address 0 in the flash, and we don't want a
178 * (ticker) exception to happen while the flash
179 * chip is in programming mode.
182 cflag = icache_status ();
184 iflag = disable_interrupts ();
188 /* Start erase on unprotected sectors */
189 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
190 printf ("Erasing sector %2d ... ", sect);
192 /* arm simple, non interrupt dependent timer */
195 if (info->protect[sect] == 0) { /* not protected */
197 (volatile u16 *) (info->start[sect]);
199 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
200 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
201 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
203 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
204 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
205 *addr = CMD_ERASE_CONFIRM;
207 /* wait until flash is ready */
214 if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
215 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
221 && (result & 0xFFFF) & BIT_ERASE_DONE)
226 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
238 } else { /* it was protected */
240 printf ("protected!\n");
245 printf ("User Interrupt!\n");
248 /* allow flash to settle - wait 10 ms */
252 enable_interrupts ();
260 static int write_word (flash_info_t * info, ulong dest, ulong data)
262 volatile u16 *addr = (volatile u16 *) dest;
269 * Check if Flash is (sufficiently) erased
272 if ((result & data) != data)
273 return ERR_NOT_ERASED;
277 * Disable interrupts which might cause a timeout
278 * here. Remember that our exception vectors are
279 * at address 0 in the flash, and we don't want a
280 * (ticker) exception to happen while the flash
281 * chip is in programming mode.
284 cflag = icache_status ();
286 iflag = disable_interrupts ();
288 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
289 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
290 MEM_FLASH_ADDR1 = CMD_PROGRAM;
293 /* arm simple, non interrupt dependent timer */
296 /* wait until flash is ready */
302 if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
306 if (!chip1 && ((result & 0x80) == (data & 0x80)))
311 *addr = CMD_READ_ARRAY;
313 if (chip1 == ERR || *addr != data)
317 enable_interrupts ();
326 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
332 printf ("unaligned destination not supported\n");
338 printf ("odd transfer sizes not supported\n");
346 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
348 if ((rc = write_word (info, wp - 1, data)) != 0) {
357 data = *((volatile u16 *) src);
358 if ((rc = write_word (info, wp, data)) != 0) {
367 data = (*((volatile u8 *) src) << 8) |
368 *((volatile u8 *) (wp + 1));
369 if ((rc = write_word (info, wp, data)) != 0) {