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,
29 #define FLASH_BANK_SIZE PHYS_FLASH_SIZE
30 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
32 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
34 #define CMD_READ_ARRAY 0x000000F0
35 #define CMD_UNLOCK1 0x000000AA
36 #define CMD_UNLOCK2 0x00000055
37 #define CMD_ERASE_SETUP 0x00000080
38 #define CMD_ERASE_CONFIRM 0x00000030
39 #define CMD_PROGRAM 0x000000A0
40 #define CMD_UNLOCK_BYPASS 0x00000020
42 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00000555 << 1)))
43 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x000002AA << 1)))
45 #define BIT_ERASE_DONE 0x00000080
46 #define BIT_RDY_MASK 0x00000080
47 #define BIT_PROGRAM_ERROR 0x00000020
48 #define BIT_TIMEOUT 0x80000000 /* our flag */
54 /*-----------------------------------------------------------------------
57 ulong flash_init (void)
62 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
65 flash_info[i].flash_id =
66 #if defined(CONFIG_AMD_LV400)
67 (AMD_MANUFACT & FLASH_VENDMASK) |
68 (AMD_ID_LV400B & FLASH_TYPEMASK);
69 #elif defined(CONFIG_AMD_LV800)
70 (AMD_MANUFACT & FLASH_VENDMASK) |
71 (AMD_ID_LV800B & FLASH_TYPEMASK);
73 #error "Unknown flash configured"
75 flash_info[i].size = FLASH_BANK_SIZE;
76 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
77 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
79 flashbase = PHYS_FLASH_1;
81 panic ("configured too many flash banks!\n");
82 for (j = 0; j < flash_info[i].sector_count; j++) {
84 /* 1st one is 16 KB */
86 flash_info[i].start[j] =
90 /* 2nd and 3rd are both 8 KB */
91 if ((j == 1) || (j == 2)) {
92 flash_info[i].start[j] =
93 flashbase + 0x4000 + (j -
100 flash_info[i].start[j] =
104 flash_info[i].start[j] =
105 flashbase + (j - 3) * MAIN_SECT_SIZE;
108 size += flash_info[i].size;
111 flash_protect (FLAG_PROTECT_SET,
112 CONFIG_SYS_FLASH_BASE,
113 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
116 flash_protect (FLAG_PROTECT_SET,
118 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
123 /*-----------------------------------------------------------------------
125 void flash_print_info (flash_info_t * info)
129 switch (info->flash_id & FLASH_VENDMASK) {
130 case (AMD_MANUFACT & FLASH_VENDMASK):
134 printf ("Unknown Vendor ");
138 switch (info->flash_id & FLASH_TYPEMASK) {
139 case (AMD_ID_LV400B & FLASH_TYPEMASK):
140 printf ("1x Amd29LV400BB (4Mbit)\n");
142 case (AMD_ID_LV800B & FLASH_TYPEMASK):
143 printf ("1x Amd29LV800BB (8Mbit)\n");
146 printf ("Unknown Chip Type\n");
151 printf (" Size: %ld MB in %d Sectors\n",
152 info->size >> 20, info->sector_count);
154 printf (" Sector Start Addresses:");
155 for (i = 0; i < info->sector_count; i++) {
159 printf (" %08lX%s", info->start[i],
160 info->protect[i] ? " (RO)" : " ");
167 /*-----------------------------------------------------------------------
170 int flash_erase (flash_info_t * info, int s_first, int s_last)
173 int iflag, cflag, prot, sect;
177 /* first look for protection bits */
179 if (info->flash_id == FLASH_UNKNOWN)
180 return ERR_UNKNOWN_FLASH_TYPE;
182 if ((s_first < 0) || (s_first > s_last)) {
186 if ((info->flash_id & FLASH_VENDMASK) !=
187 (AMD_MANUFACT & FLASH_VENDMASK)) {
188 return ERR_UNKNOWN_FLASH_VENDOR;
192 for (sect = s_first; sect <= s_last; ++sect) {
193 if (info->protect[sect]) {
198 return ERR_PROTECTED;
201 * Disable interrupts which might cause a timeout
202 * here. Remember that our exception vectors are
203 * at address 0 in the flash, and we don't want a
204 * (ticker) exception to happen while the flash
205 * chip is in programming mode.
207 cflag = icache_status ();
209 iflag = disable_interrupts ();
211 /* Start erase on unprotected sectors */
212 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
213 printf ("Erasing sector %2d ... ", sect);
215 /* arm simple, non interrupt dependent timer */
216 reset_timer_masked ();
218 if (info->protect[sect] == 0) { /* not protected */
219 vu_short *addr = (vu_short *) (info->start[sect]);
221 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
222 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
223 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
225 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
226 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
227 *addr = CMD_ERASE_CONFIRM;
229 /* wait until flash is ready */
236 if (get_timer_masked () >
237 CONFIG_SYS_FLASH_ERASE_TOUT) {
238 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
244 && (result & 0xFFFF) & BIT_ERASE_DONE)
248 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
253 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
265 } else { /* it was protected */
267 printf ("protected!\n");
272 printf ("User Interrupt!\n");
275 /* allow flash to settle - wait 10 ms */
276 udelay_masked (10000);
279 enable_interrupts ();
287 /*-----------------------------------------------------------------------
288 * Copy memory to flash
291 static int write_hword (flash_info_t * info, ulong dest, ushort data)
293 vu_short *addr = (vu_short *) dest;
300 * Check if Flash is (sufficiently) erased
303 if ((result & data) != data)
304 return ERR_NOT_ERASED;
308 * Disable interrupts which might cause a timeout
309 * here. Remember that our exception vectors are
310 * at address 0 in the flash, and we don't want a
311 * (ticker) exception to happen while the flash
312 * chip is in programming mode.
314 cflag = icache_status ();
316 iflag = disable_interrupts ();
318 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
319 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
320 MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
324 /* arm simple, non interrupt dependent timer */
325 reset_timer_masked ();
327 /* wait until flash is ready */
333 if (get_timer_masked () > CONFIG_SYS_FLASH_ERASE_TOUT) {
337 if (!chip && ((result & 0x80) == (data & 0x80)))
340 if (!chip && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
343 if ((result & 0x80) == (data & 0x80))
351 *addr = CMD_READ_ARRAY;
353 if (chip == ERR || *addr != data)
357 enable_interrupts ();
365 /*-----------------------------------------------------------------------
366 * Copy memory to flash.
369 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
376 wp = (addr & ~1); /* get lower word aligned address */
379 * handle unaligned start bytes
381 if ((l = addr - wp) != 0) {
383 for (i = 0, cp = wp; i < l; ++i, ++cp) {
384 data = (data >> 8) | (*(uchar *) cp << 8);
386 for (; i < 2 && cnt > 0; ++i) {
387 data = (data >> 8) | (*src++ << 8);
391 for (; cnt == 0 && i < 2; ++i, ++cp) {
392 data = (data >> 8) | (*(uchar *) cp << 8);
395 if ((rc = write_hword (info, wp, data)) != 0) {
402 * handle word aligned part
405 data = *((vu_short *) src);
406 if ((rc = write_hword (info, wp, data)) != 0) {
419 * handle unaligned tail bytes
422 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
423 data = (data >> 8) | (*src++ << 8);
426 for (; i < 2; ++i, ++cp) {
427 data = (data >> 8) | (*(uchar *) cp << 8);
430 return write_hword (info, wp, data);