3 * BuS Elektronik GmbH & Co.KG <esw@bus-elektonik.de>
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include "cfm_flash.h"
30 #define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
31 #define FLASH_BANK_SIZE 0x200000
33 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
35 void flash_print_info (flash_info_t * info)
39 switch (info->flash_id & FLASH_VENDMASK) {
40 case (AMD_MANUFACT & FLASH_VENDMASK):
42 switch (info->flash_id & FLASH_TYPEMASK) {
43 case (AMD_ID_LV160B & FLASH_TYPEMASK):
44 printf ("AM29LV160B (16Bit)\n");
47 printf ("Unknown Chip Type\n");
51 case FREESCALE_MANUFACT & FLASH_VENDMASK:
52 cfm_flash_print_info (info);
55 printf ("Unknown Vendor ");
60 if ((info->size >> 20) > 0)
62 printf ("%ld MiB",info->size >> 20);
66 printf ("%ld KiB",info->size >> 10);
68 printf (" in %d Sectors\n", info->sector_count);
70 printf (" Sector Start Addresses:");
71 for (i = 0; i < info->sector_count; i++) {
75 printf ("%02d: %08lX%s ", i,info->start[i],
76 info->protect[i] ? " P" : " ");
81 unsigned long flash_init (void)
86 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
92 flash_info[i].flash_id =
93 (AMD_MANUFACT & FLASH_VENDMASK) |
94 (AMD_ID_LV160B & FLASH_TYPEMASK);
95 flash_info[i].size = FLASH_BANK_SIZE;
96 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
97 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
98 flashbase = PHYS_FLASH_1;
99 for (j = 0; j < flash_info[i].sector_count; j++) {
102 flash_info[i].start[j] = flashbase;
104 if ((j >= 1) && (j <= 2)) {
105 /* 2nd and 3rd are 8 KiB */
106 flash_info[i].start[j] =
107 flashbase + 0x4000 + 0x2000 * (j - 1);
111 flash_info[i].start[j] = flashbase + 0x8000;
113 if ((j >= 4) && (j <= 34)) {
114 /* rest is 256 KiB */
115 flash_info[i].start[j] =
116 flashbase + 0x10000 + 0x10000 * (j - 4);
121 cfm_flash_init (&flash_info[i]);
124 panic ("configured to many flash banks!\n");
127 size += flash_info[i].size;
130 flash_protect (FLAG_PROTECT_SET,
131 CONFIG_SYS_FLASH_BASE,
132 CONFIG_SYS_FLASH_BASE + 0xffff, &flash_info[0]);
137 #define CMD_READ_ARRAY 0x00F0
138 #define CMD_UNLOCK1 0x00AA
139 #define CMD_UNLOCK2 0x0055
140 #define CMD_ERASE_SETUP 0x0080
141 #define CMD_ERASE_CONFIRM 0x0030
142 #define CMD_PROGRAM 0x00A0
143 #define CMD_UNLOCK_BYPASS 0x0020
145 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(info->start[0] + (0x00000555<<1)))
146 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(info->start[0] + (0x000002AA<<1)))
149 #define BIT_ERASE_DONE 0x0080
150 #define BIT_RDY_MASK 0x0080
151 #define BIT_PROGRAM_ERROR 0x0020
152 #define BIT_TIMEOUT 0x80000000 /* our flag */
156 int amd_flash_erase_sector(flash_info_t * info, int sector)
163 (volatile u16 *) (info->start[sector]);
165 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
166 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
167 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
169 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
170 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
171 *addr = CMD_ERASE_CONFIRM;
173 /* wait until flash is ready */
175 start = get_timer(0);
181 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
182 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
186 if (!state && (result & 0xFFFF) & BIT_ERASE_DONE)
190 if (state == ERR_READY)
193 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
198 int flash_erase (flash_info_t * info, int s_first, int s_last)
206 if (info->flash_id == FLASH_UNKNOWN)
208 rc = ERR_UNKNOWN_FLASH_TYPE;
209 } /* (info->flash_id == FLASH_UNKNOWN) */
211 if ((s_first < 0) || (s_first > s_last) || s_last >= info->sector_count)
216 cflag = icache_status ();
218 iflag = disable_interrupts ();
220 for (sector = s_first; (sector <= s_last) && (rc == ERR_OK); sector++) {
222 if (info->protect[sector])
224 putc('P'); /* protected sector will not erase */
228 /* erase on unprotected sector */
230 switch (info->flash_id & FLASH_VENDMASK)
232 case (AMD_MANUFACT & FLASH_VENDMASK):
233 rc = amd_flash_erase_sector(info,sector);
235 case (FREESCALE_MANUFACT & FLASH_VENDMASK):
236 rc = cfm_flash_erase_sector(info,sector);
239 return ERR_UNKNOWN_FLASH_VENDOR;
254 udelay (10000); /* allow flash to settle - wait 10 ms */
257 enable_interrupts ();
265 volatile static int amd_write_word (flash_info_t * info, ulong dest, u16 data)
274 * Check if Flash is (sufficiently) erased
276 addr = (volatile u16 *) dest;
279 if ((result & data) != data)
280 return ERR_NOT_ERASED;
283 * Disable interrupts which might cause a timeout
284 * here. Remember that our exception vectors are
285 * at address 0 in the flash, and we don't want a
286 * (ticker) exception to happen while the flash
287 * chip is in programming mode.
290 cflag = icache_status ();
292 iflag = disable_interrupts ();
294 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
295 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
296 MEM_FLASH_ADDR1 = CMD_PROGRAM;
299 /* arm simple, non interrupt dependent timer */
300 start = get_timer(0);
302 /* wait until flash is ready */
308 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
311 if (!state && ((result & BIT_RDY_MASK) == (data & BIT_RDY_MASK)))
316 *addr = CMD_READ_ARRAY;
318 if (state == ERR_READY)
320 if ((*addr != data) && (state != ERR_TIMOUT))
321 state = ERR_PROG_ERROR;
324 enable_interrupts ();
332 int amd_flash_write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
341 debug ("Byte alignment not supported\n");
346 debug ("Byte transfer not supported\n");
351 while ((cnt>=2) && (rc == ERR_OK))
353 data = *((volatile u16 *) src);
354 rc=amd_write_word (info,dest,data);
362 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
366 switch (info->flash_id & FLASH_VENDMASK)
368 case (AMD_MANUFACT & FLASH_VENDMASK):
369 rc = amd_flash_write_buff(info,src,addr,cnt);
371 case (FREESCALE_MANUFACT & FLASH_VENDMASK):
372 rc = cfm_flash_write_buff(info,src,addr,cnt);
375 rc = ERR_UNKNOWN_FLASH_VENDOR;
380 int amd_flash_protect(flash_info_t * info,long sector,int prot)
386 info->protect[sector]=1;
390 info->protect[sector]=0;
395 #ifdef CONFIG_SYS_FLASH_PROTECTION
397 int flash_real_protect(flash_info_t * info,long sector,int prot)
401 switch (info->flash_id & FLASH_VENDMASK)
403 case (AMD_MANUFACT & FLASH_VENDMASK):
404 rc = amd_flash_protect(info,sector,prot);
406 case (FREESCALE_MANUFACT & FLASH_VENDMASK):
407 rc = cfm_flash_protect(info,sector,prot);
410 rc = ERR_UNKNOWN_FLASH_VENDOR;