3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
7 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <environment.h>
33 #define FLASH_BANK_SIZE 0x1000000 /* 2 x 8 MB */
34 #define MAIN_SECT_SIZE 0x40000 /* 2 x 128 kB */
36 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
39 #define CMD_READ_ARRAY 0x00FF00FF
40 #define CMD_IDENTIFY 0x00900090
41 #define CMD_ERASE_SETUP 0x00200020
42 #define CMD_ERASE_CONFIRM 0x00D000D0
43 #define CMD_PROGRAM 0x00400040
44 #define CMD_RESUME 0x00D000D0
45 #define CMD_SUSPEND 0x00B000B0
46 #define CMD_STATUS_READ 0x00700070
47 #define CMD_STATUS_RESET 0x00500050
49 #define BIT_BUSY 0x00800080
50 #define BIT_ERASE_SUSPEND 0x00400040
51 #define BIT_ERASE_ERROR 0x00200020
52 #define BIT_PROGRAM_ERROR 0x00100010
53 #define BIT_VPP_RANGE_ERROR 0x00080008
54 #define BIT_PROGRAM_SUSPEND 0x00040004
55 #define BIT_PROTECT_ERROR 0x00020002
56 #define BIT_UNDEFINED 0x00010001
58 #define BIT_SEQUENCE_ERROR 0x00300030
59 #define BIT_TIMEOUT 0x80000000
61 /*-----------------------------------------------------------------------
64 ulong
flash_init (void)
69 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
72 flash_info
[i
].flash_id
=
73 (INTEL_MANUFACT
& FLASH_VENDMASK
) |
74 (INTEL_ID_28F640J3A
& FLASH_TYPEMASK
);
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
= CONFIG_SYS_FLASH_BASE
;
81 panic ("configured too many flash banks!\n");
82 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
83 flash_info
[i
].start
[j
] = flashbase
;
85 /* uniform sector size */
86 flashbase
+= MAIN_SECT_SIZE
;
88 size
+= flash_info
[i
].size
;
92 * Protect monitor and environment sectors
94 flash_protect ( FLAG_PROTECT_SET
,
95 CONFIG_SYS_FLASH_BASE
,
96 CONFIG_SYS_FLASH_BASE
+ monitor_flash_len
- 1,
99 flash_protect ( FLAG_PROTECT_SET
,
101 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1, &flash_info
[0]);
103 #ifdef CONFIG_ENV_ADDR_REDUND
104 flash_protect ( FLAG_PROTECT_SET
,
105 CONFIG_ENV_ADDR_REDUND
,
106 CONFIG_ENV_ADDR_REDUND
+ CONFIG_ENV_SIZE_REDUND
- 1,
113 /*-----------------------------------------------------------------------
115 void flash_print_info (flash_info_t
* info
)
119 switch (info
->flash_id
& FLASH_VENDMASK
) {
120 case (INTEL_MANUFACT
& FLASH_VENDMASK
):
124 printf ("Unknown Vendor ");
128 switch (info
->flash_id
& FLASH_TYPEMASK
) {
129 case (INTEL_ID_28F640J3A
& FLASH_TYPEMASK
):
130 printf ("2x 28F640J3A (64Mbit)\n");
133 printf ("Unknown Chip Type\n");
138 printf (" Size: %ld MB in %d Sectors\n",
139 info
->size
>> 20, info
->sector_count
);
141 printf (" Sector Start Addresses:");
142 for (i
= 0; i
< info
->sector_count
; i
++) {
148 info
->protect
[i
] ? " (RO)" : " ");
155 /*-----------------------------------------------------------------------
158 int flash_error (ulong code
)
160 /* Check bit patterns */
161 /* SR.7=0 is busy, SR.7=1 is ready */
162 /* all other flags indicate error on 1 */
163 /* SR.0 is undefined */
164 /* Timeout is our faked flag */
166 /* sequence is described in Intel 290644-005 document */
169 if (code
& BIT_TIMEOUT
) {
174 /* check Busy, SR.7 */
175 if (~code
& BIT_BUSY
) {
177 return ERR_PROG_ERROR
;
180 /* check Vpp low, SR.3 */
181 if (code
& BIT_VPP_RANGE_ERROR
) {
182 puts ("Vpp range error\n");
183 return ERR_PROG_ERROR
;
186 /* check Device Protect Error, SR.1 */
187 if (code
& BIT_PROTECT_ERROR
) {
188 puts ("Device protect error\n");
189 return ERR_PROG_ERROR
;
192 /* check Command Seq Error, SR.4 & SR.5 */
193 if (code
& BIT_SEQUENCE_ERROR
) {
194 puts ("Command seqence error\n");
195 return ERR_PROG_ERROR
;
198 /* check Block Erase Error, SR.5 */
199 if (code
& BIT_ERASE_ERROR
) {
200 puts ("Block erase error\n");
201 return ERR_PROG_ERROR
;
204 /* check Program Error, SR.4 */
205 if (code
& BIT_PROGRAM_ERROR
) {
206 puts ("Program error\n");
207 return ERR_PROG_ERROR
;
210 /* check Block Erase Suspended, SR.6 */
211 if (code
& BIT_ERASE_SUSPEND
) {
212 puts ("Block erase suspended\n");
213 return ERR_PROG_ERROR
;
216 /* check Program Suspended, SR.2 */
217 if (code
& BIT_PROGRAM_SUSPEND
) {
218 puts ("Program suspended\n");
219 return ERR_PROG_ERROR
;
226 /*-----------------------------------------------------------------------
229 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
231 ulong result
, result1
;
232 int iflag
, prot
, sect
;
239 debug ("flash_erase: s_first %d s_last %d\n", s_first
, s_last
);
241 /* first look for protection bits */
243 if (info
->flash_id
== FLASH_UNKNOWN
)
244 return ERR_UNKNOWN_FLASH_TYPE
;
246 if ((s_first
< 0) || (s_first
> s_last
)) {
250 if ((info
->flash_id
& FLASH_VENDMASK
) !=
251 (INTEL_MANUFACT
& FLASH_VENDMASK
)) {
252 return ERR_UNKNOWN_FLASH_VENDOR
;
256 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
257 if (info
->protect
[sect
]) {
263 printf ("- Warning: %d protected sectors will not be erased!\n",
270 * Disable interrupts which might cause a timeout
271 * here. Remember that our exception vectors are
272 * at address 0 in the flash, and we don't want a
273 * (ticker) exception to happen while the flash
274 * chip is in programming mode.
277 cflag
= dcache_status ();
280 iflag
= disable_interrupts ();
282 /* Start erase on unprotected sectors */
283 for (sect
= s_first
; sect
<= s_last
&& !ctrlc (); sect
++) {
285 debug ("Erasing sector %2d @ %08lX... ",
286 sect
, info
->start
[sect
]);
288 /* arm simple, non interrupt dependent timer */
289 reset_timer_masked ();
291 if (info
->protect
[sect
] == 0) { /* not protected */
292 vu_long
*addr
= (vu_long
*) (info
->start
[sect
]);
293 ulong bsR7
, bsR7_2
, bsR5
, bsR5_2
;
295 /* *addr = CMD_STATUS_RESET; */
296 *addr
= CMD_ERASE_SETUP
;
297 *addr
= CMD_ERASE_CONFIRM
;
299 /* wait until flash is ready */
302 if (get_timer_masked () > CONFIG_SYS_FLASH_ERASE_TOUT
) {
303 *addr
= CMD_STATUS_RESET
;
304 result
= BIT_TIMEOUT
;
308 *addr
= CMD_STATUS_READ
;
310 bsR7
= result
& (1 << 7);
311 bsR7_2
= result
& (1 << 23);
312 } while (!bsR7
| !bsR7_2
);
314 *addr
= CMD_STATUS_READ
;
316 bsR5
= result1
& (1 << 5);
317 bsR5_2
= result1
& (1 << 21);
318 #ifdef SAMSUNG_FLASH_DEBUG
319 printf ("bsR5 %lx bsR5_2 %lx\n", bsR5
, bsR5_2
);
320 if (bsR5
!= 0 && bsR5_2
!= 0)
321 printf ("bsR5 %lx bsR5_2 %lx\n", bsR5
, bsR5_2
);
324 *addr
= CMD_READ_ARRAY
;
327 if ((rc
= flash_error (result
)) != ERR_OK
)
331 } else { /* it was protected */
333 printf ("protected!\n");
339 /* allow flash to settle - wait 10 ms */
340 udelay_masked (10000);
343 enable_interrupts ();
352 /*-----------------------------------------------------------------------
353 * Copy memory to flash
356 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
358 vu_long
*addr
= (vu_long
*) dest
;
368 * Check if Flash is (sufficiently) erased
371 if ((result
& data
) != data
)
372 return ERR_NOT_ERASED
;
375 * Disable interrupts which might cause a timeout
376 * here. Remember that our exception vectors are
377 * at address 0 in the flash, and we don't want a
378 * (ticker) exception to happen while the flash
379 * chip is in programming mode.
382 cflag
= dcache_status ();
385 iflag
= disable_interrupts ();
387 /* *addr = CMD_STATUS_RESET; */
391 /* arm simple, non interrupt dependent timer */
392 reset_timer_masked ();
394 /* wait until flash is ready */
397 if (get_timer_masked () > CONFIG_SYS_FLASH_ERASE_TOUT
) {
399 result
= BIT_TIMEOUT
;
403 *addr
= CMD_STATUS_READ
;
405 } while (~result
& BIT_BUSY
);
407 /* *addr = CMD_READ_ARRAY; */
408 *addr
= CMD_STATUS_READ
;
411 rc
= flash_error (result
);
414 enable_interrupts ();
420 *addr
= CMD_READ_ARRAY
;
425 /*-----------------------------------------------------------------------
426 * Copy memory to flash.
429 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
435 wp
= (addr
& ~3); /* get lower word aligned address */
438 * handle unaligned start bytes
440 if ((l
= addr
- wp
) != 0) {
442 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
443 data
= (data
>> 8) | (*(uchar
*) cp
<< 24);
445 for (; i
< 4 && cnt
> 0; ++i
) {
446 data
= (data
>> 8) | (*src
++ << 24);
450 for (; cnt
== 0 && i
< 4; ++i
, ++cp
) {
451 data
= (data
>> 8) | (*(uchar
*) cp
<< 24);
454 if ((rc
= write_word (info
, wp
, data
)) != 0) {
461 * handle word aligned part
464 data
= *((vu_long
*) src
);
465 if ((rc
= write_word (info
, wp
, data
)) != 0) {
478 * handle unaligned tail bytes
481 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
482 data
= (data
>> 8) | (*src
++ << 24);
485 for (; i
< 4; ++i
, ++cp
) {
486 data
= (data
>> 8) | (*(uchar
*) cp
<< 24);
489 return write_word (info
, wp
, data
);