3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Alex Zuepke <azu@sysgo.de>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #define FLASH_BANK_SIZE PHYS_FLASH_SIZE
31 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
36 #define CMD_READ_ARRAY 0x000000F0
37 #define CMD_UNLOCK1 0x000000AA
38 #define CMD_UNLOCK2 0x00000055
39 #define CMD_ERASE_SETUP 0x00000080
40 #define CMD_ERASE_CONFIRM 0x00000030
41 #define CMD_PROGRAM 0x000000A0
42 #define CMD_UNLOCK_BYPASS 0x00000020
44 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
45 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
47 #define BIT_ERASE_DONE 0x00000080
48 #define BIT_RDY_MASK 0x00000080
49 #define BIT_PROGRAM_ERROR 0x00000020
50 #define BIT_TIMEOUT 0x80000000 /* our flag */
56 /*-----------------------------------------------------------------------
59 ulong flash_init (void)
64 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
67 flash_info[i].flash_id =
68 #if defined(CONFIG_AMD_LV400)
69 (AMD_MANUFACT & FLASH_VENDMASK) |
70 (AMD_ID_LV400B & FLASH_TYPEMASK);
71 #elif defined(CONFIG_AMD_LV800)
72 (AMD_MANUFACT & FLASH_VENDMASK) |
73 (AMD_ID_LV800B & FLASH_TYPEMASK);
75 #error "Unknown flash configured"
77 flash_info[i].size = FLASH_BANK_SIZE;
78 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
79 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
81 flashbase = PHYS_FLASH_1;
83 panic ("configured too many flash banks!\n");
84 for (j = 0; j < flash_info[i].sector_count; j++) {
86 /* 1st one is 16 KB */
88 flash_info[i].start[j] =
92 /* 2nd and 3rd are both 8 KB */
93 if ((j == 1) || (j == 2)) {
94 flash_info[i].start[j] =
95 flashbase + 0x4000 + (j -
102 flash_info[i].start[j] =
106 flash_info[i].start[j] =
107 flashbase + (j - 3) * MAIN_SECT_SIZE;
110 size += flash_info[i].size;
113 flash_protect (FLAG_PROTECT_SET,
115 CFG_FLASH_BASE + monitor_flash_len - 1,
118 flash_protect (FLAG_PROTECT_SET,
120 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
125 /*-----------------------------------------------------------------------
127 void flash_print_info (flash_info_t * info)
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case (AMD_MANUFACT & FLASH_VENDMASK):
136 printf ("Unknown Vendor ");
140 switch (info->flash_id & FLASH_TYPEMASK) {
141 case (AMD_ID_LV400B & FLASH_TYPEMASK):
142 printf ("1x Amd29LV400BB (4Mbit)\n");
144 case (AMD_ID_LV800B & FLASH_TYPEMASK):
145 printf ("1x Amd29LV800BB (8Mbit)\n");
148 printf ("Unknown Chip Type\n");
153 printf (" Size: %ld MB in %d Sectors\n",
154 info->size >> 20, info->sector_count);
156 printf (" Sector Start Addresses:");
157 for (i = 0; i < info->sector_count; i++) {
161 printf (" %08lX%s", info->start[i],
162 info->protect[i] ? " (RO)" : " ");
169 /*-----------------------------------------------------------------------
172 int flash_erase (flash_info_t * info, int s_first, int s_last)
175 int iflag, cflag, prot, sect;
179 /* first look for protection bits */
181 if (info->flash_id == FLASH_UNKNOWN)
182 return ERR_UNKNOWN_FLASH_TYPE;
184 if ((s_first < 0) || (s_first > s_last)) {
188 if ((info->flash_id & FLASH_VENDMASK) !=
189 (AMD_MANUFACT & FLASH_VENDMASK)) {
190 return ERR_UNKNOWN_FLASH_VENDOR;
194 for (sect = s_first; sect <= s_last; ++sect) {
195 if (info->protect[sect]) {
200 return ERR_PROTECTED;
203 * Disable interrupts which might cause a timeout
204 * here. Remember that our exception vectors are
205 * at address 0 in the flash, and we don't want a
206 * (ticker) exception to happen while the flash
207 * chip is in programming mode.
209 cflag = icache_status ();
211 iflag = disable_interrupts ();
213 /* Start erase on unprotected sectors */
214 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
215 printf ("Erasing sector %2d ... ", sect);
217 /* arm simple, non interrupt dependent timer */
218 reset_timer_masked ();
220 if (info->protect[sect] == 0) { /* not protected */
221 vu_short *addr = (vu_short *) (info->start[sect]);
223 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
224 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
225 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
227 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
228 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
229 *addr = CMD_ERASE_CONFIRM;
231 /* wait until flash is ready */
238 if (get_timer_masked () >
239 CFG_FLASH_ERASE_TOUT) {
240 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
246 && (result & 0xFFFF) & BIT_ERASE_DONE)
250 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
255 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
267 } else { /* it was protected */
269 printf ("protected!\n");
274 printf ("User Interrupt!\n");
277 /* allow flash to settle - wait 10 ms */
278 udelay_masked (10000);
281 enable_interrupts ();
289 /*-----------------------------------------------------------------------
290 * Copy memory to flash
293 volatile static int write_hword (flash_info_t * info, ulong dest, ushort data)
295 vu_short *addr = (vu_short *) dest;
302 * Check if Flash is (sufficiently) erased
305 if ((result & data) != data)
306 return ERR_NOT_ERASED;
310 * Disable interrupts which might cause a timeout
311 * here. Remember that our exception vectors are
312 * at address 0 in the flash, and we don't want a
313 * (ticker) exception to happen while the flash
314 * chip is in programming mode.
316 cflag = icache_status ();
318 iflag = disable_interrupts ();
320 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
321 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
322 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
326 /* arm simple, non interrupt dependent timer */
327 reset_timer_masked ();
329 /* wait until flash is ready */
335 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
339 if (!chip && ((result & 0x80) == (data & 0x80)))
342 if (!chip && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
345 if ((result & 0x80) == (data & 0x80))
353 *addr = CMD_READ_ARRAY;
355 if (chip == ERR || *addr != data)
359 enable_interrupts ();
367 /*-----------------------------------------------------------------------
368 * Copy memory to flash.
371 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
378 wp = (addr & ~1); /* get lower word aligned address */
381 * handle unaligned start bytes
383 if ((l = addr - wp) != 0) {
385 for (i = 0, cp = wp; i < l; ++i, ++cp) {
386 data = (data >> 8) | (*(uchar *) cp << 8);
388 for (; i < 2 && cnt > 0; ++i) {
389 data = (data >> 8) | (*src++ << 8);
393 for (; cnt == 0 && i < 2; ++i, ++cp) {
394 data = (data >> 8) | (*(uchar *) cp << 8);
397 if ((rc = write_hword (info, wp, data)) != 0) {
404 * handle word aligned part
407 data = *((vu_short *) src);
408 if ((rc = write_hword (info, wp, data)) != 0) {
421 * handle unaligned tail bytes
424 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
425 data = (data >> 8) | (*src++ << 8);
428 for (; i < 2; ++i, ++cp) {
429 data = (data >> 8) | (*(uchar *) cp << 8);
432 return write_hword (info, wp, data);