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)" : " ");
71 unsigned long flash_init (void)
76 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
79 flash_info[i].flash_id =
80 (AMD_MANUFACT & FLASH_VENDMASK) |
81 (AMD_ID_PL160CB & FLASH_TYPEMASK);
82 flash_info[i].size = FLASH_BANK_SIZE;
83 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
84 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
86 flashbase = PHYS_FLASH_1;
88 panic ("configured to many flash banks!\n");
90 for (j = 0; j < flash_info[i].sector_count; j++) {
93 flash_info[i].start[j] = flashbase;
95 if ((j >= 1) && (j <= 2)) {
96 /* 2nd and 3rd are 8 KiB */
97 flash_info[i].start[j] =
98 flashbase + 0x4000 + 0x2000 * (j - 1);
102 flash_info[i].start[j] = flashbase + 0x8000;
104 if ((j >= 4) && (j <= 10)) {
105 /* rest is 256 KiB */
106 flash_info[i].start[j] =
107 flashbase + 0x40000 + 0x40000 * (j -
111 size += flash_info[i].size;
114 flash_protect (FLAG_PROTECT_SET,
116 CFG_FLASH_BASE + 0x3ffff, &flash_info[0]);
122 #define CMD_READ_ARRAY 0x00F0
123 #define CMD_UNLOCK1 0x00AA
124 #define CMD_UNLOCK2 0x0055
125 #define CMD_ERASE_SETUP 0x0080
126 #define CMD_ERASE_CONFIRM 0x0030
127 #define CMD_PROGRAM 0x00A0
128 #define CMD_UNLOCK_BYPASS 0x0020
130 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555<<1)))
131 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA<<1)))
133 #define BIT_ERASE_DONE 0x0080
134 #define BIT_RDY_MASK 0x0080
135 #define BIT_PROGRAM_ERROR 0x0020
136 #define BIT_TIMEOUT 0x80000000 /* our flag */
143 int flash_erase (flash_info_t * info, int s_first, int s_last)
146 int iflag, cflag, prot, sect;
150 /* first look for protection bits */
152 if (info->flash_id == FLASH_UNKNOWN)
153 return ERR_UNKNOWN_FLASH_TYPE;
155 if ((s_first < 0) || (s_first > s_last)) {
159 if ((info->flash_id & FLASH_VENDMASK) !=
160 (AMD_MANUFACT & FLASH_VENDMASK)) {
161 return ERR_UNKNOWN_FLASH_VENDOR;
165 for (sect = s_first; sect <= s_last; ++sect) {
166 if (info->protect[sect]) {
171 return ERR_PROTECTED;
174 * Disable interrupts which might cause a timeout
175 * here. Remember that our exception vectors are
176 * at address 0 in the flash, and we don't want a
177 * (ticker) exception to happen while the flash
178 * chip is in programming mode.
181 cflag = icache_status ();
183 iflag = disable_interrupts ();
187 /* Start erase on unprotected sectors */
188 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
189 printf ("Erasing sector %2d ... ", sect);
191 /* arm simple, non interrupt dependent timer */
194 if (info->protect[sect] == 0) { /* not protected */
196 (volatile u16 *) (info->start[sect]);
198 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
199 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
200 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
202 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
203 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
204 *addr = CMD_ERASE_CONFIRM;
206 /* wait until flash is ready */
213 if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
214 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
220 && (result & 0xFFFF) & BIT_ERASE_DONE)
225 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
237 } else { /* it was protected */
239 printf ("protected!\n");
244 printf ("User Interrupt!\n");
247 /* allow flash to settle - wait 10 ms */
251 enable_interrupts ();
259 static int write_word (flash_info_t * info, ulong dest, ulong data)
261 volatile u16 *addr = (volatile u16 *) dest;
268 * Check if Flash is (sufficiently) erased
271 if ((result & data) != data)
272 return ERR_NOT_ERASED;
276 * Disable interrupts which might cause a timeout
277 * here. Remember that our exception vectors are
278 * at address 0 in the flash, and we don't want a
279 * (ticker) exception to happen while the flash
280 * chip is in programming mode.
283 cflag = icache_status ();
285 iflag = disable_interrupts ();
287 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
288 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
289 MEM_FLASH_ADDR1 = CMD_PROGRAM;
292 /* arm simple, non interrupt dependent timer */
295 /* wait until flash is ready */
301 if (get_timer (0) > CFG_FLASH_ERASE_TOUT) {
305 if (!chip1 && ((result & 0x80) == (data & 0x80)))
310 *addr = CMD_READ_ARRAY;
312 if (chip1 == ERR || *addr != data)
316 enable_interrupts ();
325 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
331 printf ("unaligned destination not supported\n");
337 printf ("odd transfer sizes not supported\n");
345 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
347 if ((rc = write_word (info, wp - 1, data)) != 0) {
356 data = *((volatile u16 *) src);
357 if ((rc = write_word (info, wp, data)) != 0) {
366 data = (*((volatile u8 *) src) << 8) |
367 *((volatile u8 *) (wp + 1));
368 if ((rc = write_word (info, wp, data)) != 0) {