2 * (C) Copyright 2000-2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
6 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
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,
29 #include <asm/immap.h>
31 #ifndef CONFIG_SYS_FLASH_CFI
32 typedef unsigned short FLASH_PORT_WIDTH
;
33 typedef volatile unsigned short FLASH_PORT_WIDTHV
;
35 #define FPW FLASH_PORT_WIDTH
36 #define FPWV FLASH_PORT_WIDTHV
38 #define FLASH_CYCLE1 0x5555
39 #define FLASH_CYCLE2 0x2aaa
41 #define SYNC __asm__("nop")
43 /*-----------------------------------------------------------------------
47 ulong
flash_get_size(FPWV
* addr
, flash_info_t
* info
);
48 int flash_get_offsets(ulong base
, flash_info_t
* info
);
49 int write_word(flash_info_t
* info
, FPWV
* dest
, u16 data
);
50 void inline spin_wheel(void);
52 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
54 ulong
flash_init(void)
59 fbase
= (ulong
) CONFIG_SYS_FLASH_BASE
;
60 flash_get_size((FPWV
*) fbase
, &flash_info
[0]);
61 flash_get_offsets((ulong
) fbase
, &flash_info
[0]);
62 fbase
+= flash_info
[0].size
;
63 size
+= flash_info
[0].size
;
65 /* Protect monitor and environment sectors */
66 flash_protect(FLAG_PROTECT_SET
,
67 CONFIG_SYS_MONITOR_BASE
,
68 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1, &flash_info
[0]);
73 int flash_get_offsets(ulong base
, flash_info_t
* info
)
77 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) {
79 info
->start
[0] = base
;
80 for (k
= 0, j
= 0; j
< CONFIG_SYS_SST_SECT
; j
++, k
++) {
81 info
->start
[k
+ 1] = info
->start
[k
] + CONFIG_SYS_SST_SECTSZ
;
89 void flash_print_info(flash_info_t
* info
)
93 switch (info
->flash_id
& FLASH_VENDMASK
) {
98 printf("Unknown Vendor ");
102 switch (info
->flash_id
& FLASH_TYPEMASK
) {
104 printf("SST39VF6401B\n");
107 printf("Unknown Chip Type\n");
111 if (info
->size
> 0x100000) {
114 printf(" Size: %ld", info
->size
>> 20);
116 remainder
= (info
->size
% 0x100000);
119 remainder
= (int)((float)
120 (((float)remainder
/ (float)1024) *
122 printf(".%d ", remainder
);
125 printf("MB in %d Sectors\n", info
->sector_count
);
127 printf(" Size: %ld KB in %d Sectors\n",
128 info
->size
>> 10, info
->sector_count
);
130 printf(" Sector Start Addresses:");
131 for (i
= 0; i
< info
->sector_count
; ++i
) {
135 info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
141 * The following code cannot be run from FLASH!
143 ulong
flash_get_size(FPWV
* addr
, flash_info_t
* info
)
147 addr
[FLASH_CYCLE1
] = (FPWV
) 0x00AA00AA; /* for Atmel, Intel ignores this */
148 addr
[FLASH_CYCLE2
] = (FPWV
) 0x00550055; /* for Atmel, Intel ignores this */
149 addr
[FLASH_CYCLE1
] = (FPWV
) 0x00900090; /* selects Intel or Atmel */
151 switch (addr
[0] & 0xffff) {
152 case (u8
) SST_MANUFACT
:
153 info
->flash_id
= FLASH_MAN_SST
;
157 printf("Unknown Flash\n");
158 info
->flash_id
= FLASH_UNKNOWN
;
159 info
->sector_count
= 0;
162 *addr
= (FPW
) 0x00F000F0;
163 return (0); /* no or unknown flash */
167 case (u16
) SST_ID_xF6401B
:
168 info
->flash_id
+= FLASH_SST6401B
;
171 info
->flash_id
= FLASH_UNKNOWN
;
175 info
->sector_count
= 0;
177 info
->sector_count
= CONFIG_SYS_SST_SECT
;
178 info
->size
= CONFIG_SYS_SST_SECT
* CONFIG_SYS_SST_SECTSZ
;
181 *addr
= (FPWV
) 0x00F000F0;
183 if (info
->sector_count
> CONFIG_SYS_MAX_FLASH_SECT
) {
184 printf("** ERROR: sector count %d > max (%d) **\n",
185 info
->sector_count
, CONFIG_SYS_MAX_FLASH_SECT
);
186 info
->sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
192 int flash_erase(flash_info_t
* info
, int s_first
, int s_last
)
195 int flag
, prot
, sect
, count
;
196 ulong type
, start
, last
;
197 int rcode
= 0, flashtype
= 0;
199 if ((s_first
< 0) || (s_first
> s_last
)) {
200 if (info
->flash_id
== FLASH_UNKNOWN
)
201 printf("- missing\n");
203 printf("- no sectors to erase\n");
207 type
= (info
->flash_id
& FLASH_VENDMASK
);
214 type
= (info
->flash_id
& FLASH_VENDMASK
);
215 printf("Can't erase unknown flash type %08lx - aborted\n",
221 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
222 if (info
->protect
[sect
]) {
228 printf("- Warning: %d protected sectors will not be erased!\n",
233 flag
= disable_interrupts();
235 start
= get_timer(0);
238 if ((s_last
- s_first
) == (CONFIG_SYS_SST_SECT
- 1)) {
240 addr
= (FPWV
*) info
->start
[0];
242 addr
[FLASH_CYCLE1
] = 0x00AA; /* unlock */
243 addr
[FLASH_CYCLE2
] = 0x0055; /* unlock */
244 addr
[FLASH_CYCLE1
] = 0x0080; /* erase mode */
245 addr
[FLASH_CYCLE1
] = 0x00AA; /* unlock */
246 addr
[FLASH_CYCLE2
] = 0x0055; /* unlock */
247 *addr
= 0x0030; /* erase chip */
250 start
= get_timer(0);
252 while ((*addr
& 0x0080) != 0x0080) {
253 if (count
++ > 0x10000) {
258 if (get_timer(start
) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
260 *addr
= 0x00F0; /* reset to read mode */
266 *addr
= 0x00F0; /* reset to read mode */
268 printf("\b. done\n");
274 } else if (prot
== CONFIG_SYS_SST_SECT
) {
279 /* Start erase on unprotected sectors */
280 for (sect
= s_first
; sect
<= s_last
; sect
++) {
281 if (info
->protect
[sect
] == 0) { /* not protected */
283 addr
= (FPWV
*) (info
->start
[sect
]);
287 /* arm simple, non interrupt dependent timer */
288 start
= get_timer(0);
293 FPWV
*base
; /* first address in bank */
295 flag
= disable_interrupts();
297 base
= (FPWV
*) (CONFIG_SYS_FLASH_BASE
); /* First sector */
299 base
[FLASH_CYCLE1
] = 0x00AA; /* unlock */
300 base
[FLASH_CYCLE2
] = 0x0055; /* unlock */
301 base
[FLASH_CYCLE1
] = 0x0080; /* erase mode */
302 base
[FLASH_CYCLE1
] = 0x00AA; /* unlock */
303 base
[FLASH_CYCLE2
] = 0x0055; /* unlock */
304 *addr
= 0x0050; /* erase sector */
309 while ((*addr
& 0x0080) != 0x0080) {
310 if (get_timer(start
) >
311 CONFIG_SYS_FLASH_ERASE_TOUT
) {
313 *addr
= 0x00F0; /* reset to read mode */
320 *addr
= 0x00F0; /* reset to read mode */
323 } /* switch (flashtype) */
334 int write_buff(flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
340 if (info
->flash_id
== FLASH_UNKNOWN
)
343 /* get lower word aligned address */
345 port_width
= sizeof(FPW
);
347 /* handle unaligned start bytes */
349 data
= *((FPWV
*) wp
);
350 data
= (data
<< 8) | *src
;
352 if ((rc
= write_word(info
, (FPWV
*) wp
, data
)) != 0)
362 * handle word aligned part
365 data
= *((FPWV
*) src
);
367 if ((rc
= write_word(info
, (FPWV
*) wp
, data
)) != 0)
374 if (count
++ > 0x800) {
379 /* handle word aligned part */
381 /* handle word aligned part */
383 data
= *((FPWV
*) wp
);
385 data
= (data
& 0x00FF) | (*src
<< 8);
387 if ((rc
= write_word(info
, (FPWV
*) wp
, data
)) != 0)
393 if (count
++ > 0x800) {
405 /*-----------------------------------------------------------------------
406 * Write a word to Flash
407 * A word is 16 bits, whichever the bus width of the flash bank
408 * (not an individual chip) is.
413 * 2 - Flash not erased
415 int write_word(flash_info_t
* info
, FPWV
* dest
, u16 data
)
419 int res
= 0; /* result, assume success */
420 FPWV
*base
; /* first address in flash bank */
422 /* Check if Flash is (sufficiently) erased */
423 if ((*dest
& (u8
) data
) != (u8
) data
) {
427 base
= (FPWV
*) (CONFIG_SYS_FLASH_BASE
);
429 /* Disable interrupts which might cause a timeout here */
430 flag
= disable_interrupts();
432 base
[FLASH_CYCLE1
] = (u8
) 0x00AA00AA; /* unlock */
433 base
[FLASH_CYCLE2
] = (u8
) 0x00550055; /* unlock */
434 base
[FLASH_CYCLE1
] = (u8
) 0x00A000A0; /* selects program mode */
436 *dest
= data
; /* start programming the data */
438 /* re-enable interrupts if necessary */
442 start
= get_timer(0);
444 /* data polling for D7 */
446 && (*dest
& (u8
) 0x00800080) != (data
& (u8
) 0x00800080)) {
447 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
448 *dest
= (u8
) 0x00F000F0; /* reset bank */
453 *dest
++ = (u8
) 0x00F000F0; /* reset bank */
458 void inline spin_wheel(void)
461 static char w
[] = "\\/-";
463 printf("\010%c", w
[p
]);
464 (++p
== 3) ? (p
= 0) : 0;