3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Gregory E. Allen, gallen@arlut.utexas.edu
7 * Matthew E. Karger, karger@arlut.utexas.edu
8 * Applied Research Laboratories, The University of Texas at Austin
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <asm/processor.h>
33 #define ROM_CS0_START 0xFF800000
34 #define ROM_CS1_START 0xFF000000
36 #if defined(CFG_ENV_IS_IN_FLASH)
38 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
41 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
43 # ifndef CFG_ENV_SECT_SIZE
44 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
48 #define FLASH_BANK_SIZE ((uint)(16 * 1024 * 1024)) /* max 16Mbyte */
49 #define MAIN_SECT_SIZE 0x10000
50 #define SECT_SIZE_32KB 0x8000
51 #define SECT_SIZE_8KB 0x2000
53 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
55 static int write_word (flash_info_t * info, ulong dest, ulong data);
57 static void write_via_fpu (vu_long * addr, ulong * data);
59 static __inline__ unsigned long get_msr (void);
60 static __inline__ void set_msr (unsigned long msr);
62 /*flash command address offsets*/
67 #define FLASH_WORD_SIZE unsigned char
69 /*---------------------------------------------------------------------*/
70 /*#define DEBUG_FLASH 1 */
72 /*---------------------------------------------------------------------*/
74 unsigned long flash_init (void)
76 int i; /* flash bank counter */
77 int j; /* flash device sector counter */
78 int k; /* flash size calculation loop counter */
79 int N; /* pow(2,N) is flash size, but we don't have <math.h> */
80 ulong total_size = 0, device_size = 1;
81 unsigned char manuf_id, device_id;
83 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
84 vu_char *addr = (vu_char *) (CFG_FLASH_BASE + i * FLASH_BANK_SIZE);
86 addr[0x555] = 0xAA; /* get manuf/device info command */
87 addr[0x2AA] = 0x55; /* 3-cycle command */
90 manuf_id = addr[0]; /* read back manuf/device info */
93 addr[0x55] = 0x98; /* CFI command */
94 N = addr[0x27]; /* read back device_size = pow(2,N) */
96 for (k = 0; k < N; k++) /* calculate device_size = pow(2,N) */
99 flash_info[i].size = device_size;
100 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
102 #if defined DEBUG_FLASH
103 printf ("manuf_id = %x, device_id = %x\n", manuf_id, device_id);
105 /* find out what kind of flash we are using */
106 if ((manuf_id == (uchar) (AMD_MANUFACT))
107 && (device_id == AMD_ID_LV033C)) {
108 flash_info[i].flash_id =
109 ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) |
110 (FLASH_AM033C & FLASH_TYPEMASK);
112 /* set individual sector start addresses */
113 for (j = 0; j < flash_info[i].sector_count; j++) {
114 flash_info[i].start[j] =
115 (CFG_FLASH_BASE + i * FLASH_BANK_SIZE +
120 else if ((manuf_id == (uchar) (AMD_MANUFACT)) &&
121 (device_id == AMD_ID_LV116DT)) {
122 flash_info[i].flash_id =
123 ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) |
124 (FLASH_AM160T & FLASH_TYPEMASK);
126 /* set individual sector start addresses */
127 for (j = 0; j < flash_info[i].sector_count; j++) {
128 flash_info[i].start[j] =
129 (CFG_FLASH_BASE + i * FLASH_BANK_SIZE +
132 if (j < (CFG_MAX_FLASH_SECT - 3)) {
133 flash_info[i].start[j] =
134 (CFG_FLASH_BASE + i * FLASH_BANK_SIZE +
136 } else if (j == (CFG_MAX_FLASH_SECT - 3)) {
137 flash_info[i].start[j] =
138 (flash_info[i].start[j - 1] + SECT_SIZE_32KB);
141 flash_info[i].start[j] =
142 (flash_info[i].start[j - 1] + SECT_SIZE_8KB);
148 flash_info[i].flash_id = FLASH_UNKNOWN;
153 #if defined DEBUG_FLASH
154 printf ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
159 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
161 total_size += flash_info[i].size;
164 /* Protect monitor and environment sectors
166 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
167 flash_protect (FLAG_PROTECT_SET, CFG_MONITOR_BASE,
168 CFG_MONITOR_BASE + monitor_flash_len - 1,
172 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
173 flash_protect (FLAG_PROTECT_SET, CFG_ENV_ADDR,
174 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
181 /*-----------------------------------------------------------------------
183 void flash_print_info (flash_info_t * info)
185 static const char unk[] = "Unknown";
186 const char *mfct = unk, *type = unk;
189 if (info->flash_id != FLASH_UNKNOWN) {
190 switch (info->flash_id & FLASH_VENDMASK) {
204 mfct = "Bright Microelectonics";
206 case FLASH_MAN_INTEL:
211 switch (info->flash_id & FLASH_TYPEMASK) {
213 type = "AM29LV033C (32 Mbit, uniform sector size)";
216 type = "AM29LV160T (16 Mbit, top boot sector)";
219 type = "AM29F040B (512K * 8, uniform sector size)";
222 type = "AM29LV400B (4 Mbit, bottom boot sect)";
225 type = "AM29LV400T (4 Mbit, top boot sector)";
228 type = "AM29LV800B (8 Mbit, bottom boot sect)";
231 type = "AM29LV800T (8 Mbit, top boot sector)";
234 type = "AM29LV320B (32 Mbit, bottom boot sect)";
237 type = "AM29LV320T (32 Mbit, top boot sector)";
240 type = "M29W800AB (8 Mbit, bottom boot sect)";
243 type = "SST39LF/VF800 (8 Mbit, uniform sector size)";
246 type = "SST39LF/VF160 (16 Mbit, uniform sector size)";
251 printf ("\n Brand: %s Type: %s\n"
252 " Size: %lu KB in %d Sectors\n",
253 mfct, type, info->size >> 10, info->sector_count);
255 printf (" Sector Start Addresses:");
257 for (i = 0; i < info->sector_count; i++) {
260 unsigned long *flash = (unsigned long *) info->start[i];
263 * Check if whole sector is erased
265 size = (i != (info->sector_count - 1)) ?
266 (info->start[i + 1] - info->start[i]) >> 2 :
267 (info->start[0] + info->size - info->start[i]) >> 2;
269 for (flash = (unsigned long *) info->start[i], erased = 1;
270 (flash != (unsigned long *) info->start[i] + size) && erased;
272 erased = *flash == ~0x0UL;
274 printf ("%s %08lX %s %s",
275 (i % 5) ? "" : "\n ",
277 erased ? "E" : " ", info->protect[i] ? "RO" : " ");
284 /*-----------------------------------------------------------------------
287 int flash_erase (flash_info_t * info, int s_first, int s_last)
289 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
290 int flag, prot, sect, l_sect;
291 ulong start, now, last;
294 if ((s_first < 0) || (s_first > s_last)) {
295 if (info->flash_id == FLASH_UNKNOWN) {
296 printf ("- missing\n");
298 printf ("- no sectors to erase\n");
303 if ((info->flash_id == FLASH_UNKNOWN) ||
304 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
305 printf ("Can't erase unknown flash type - aborted\n");
310 for (sect = s_first; sect <= s_last; ++sect) {
311 if (info->protect[sect]) {
317 printf ("- Warning: %d protected sectors will not be erased!\n",
325 /* Check the ROM CS */
326 if ((info->start[0] >= ROM_CS1_START)
327 && (info->start[0] < ROM_CS0_START))
332 /* Disable interrupts which might cause a timeout here */
333 flag = disable_interrupts ();
335 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
336 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
337 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
338 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
339 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
341 /* Start erase on unprotected sectors */
342 for (sect = s_first; sect <= s_last; sect++) {
343 if (info->protect[sect] == 0) { /* not protected */
344 addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->
350 if (info->flash_id & FLASH_MAN_SST) {
351 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
352 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
353 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
354 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
355 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
356 addr[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
357 udelay (30000); /* wait 30 ms */
359 addr[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
366 /* re-enable interrupts if necessary */
368 enable_interrupts ();
370 /* wait at least 80us - let's wait 1 ms */
374 * We wait for the last triggered sector
379 start = get_timer (0);
381 addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->start[l_sect] -
384 while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
385 (FLASH_WORD_SIZE) 0x00800080) {
386 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
387 printf ("Timeout\n");
390 /* show that we're waiting */
391 if ((now - last) > 1000) { /* every second */
398 /* reset to read mode */
399 addr = (FLASH_WORD_SIZE *) info->start[0];
400 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
407 /*-----------------------------------------------------------------------
408 * Copy memory to flash, returns:
411 * 2 - Flash not erased
414 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
419 wp = (addr & ~3); /* get lower word aligned address */
422 * handle unaligned start bytes
424 if ((l = addr - wp) != 0) {
426 for (i = 0, cp = wp; i < l; ++i, ++cp) {
427 data = (data << 8) | (*(uchar *) cp);
429 for (; i < 4 && cnt > 0; ++i) {
430 data = (data << 8) | *src++;
434 for (; cnt == 0 && i < 4; ++i, ++cp) {
435 data = (data << 8) | (*(uchar *) cp);
438 if ((rc = write_word (info, wp, data)) != 0) {
445 * handle word aligned part
449 for (i = 0; i < 4; ++i) {
450 data = (data << 8) | *src++;
452 if ((rc = write_word (info, wp, data)) != 0) {
464 * handle unaligned tail bytes
467 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
468 data = (data << 8) | *src++;
471 for (; i < 4; ++i, ++cp) {
472 data = (data << 8) | (*(uchar *) cp);
475 return (write_word (info, wp, data));
479 /*-----------------------------------------------------------------------
480 * Write a word to Flash, returns:
483 * 2 - Flash not erased
485 static int write_word (flash_info_t * info, ulong dest, ulong data)
487 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) info->start[0];
488 volatile FLASH_WORD_SIZE *dest2;
489 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
495 /* Check the ROM CS */
496 if ((info->start[0] >= ROM_CS1_START)
497 && (info->start[0] < ROM_CS0_START))
502 dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) +
505 /* Check if Flash is (sufficiently) erased */
506 if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
509 /* Disable interrupts which might cause a timeout here */
510 flag = disable_interrupts ();
512 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
513 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
514 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
515 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00A000A0;
517 dest2[i << sh8b] = data2[i];
519 /* re-enable interrupts if necessary */
521 enable_interrupts ();
523 /* data polling for D7 */
524 start = get_timer (0);
525 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE) 0x00800080) !=
526 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
527 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
536 /*-----------------------------------------------------------------------
539 static void write_via_fpu (vu_long * addr, ulong * data)
541 __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
542 __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
546 /*-----------------------------------------------------------------------
548 static __inline__ unsigned long get_msr (void)
552 __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
557 static __inline__ void set_msr (unsigned long msr)
559 __asm__ __volatile__ ("mtmsr %0"::"r" (msr));