2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
11 #define FLASH_BANK_SIZE 0x800000
12 #define EN29LV640 0x227e227e
14 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
16 void flash_print_info (flash_info_t
* info
)
20 switch (info
->flash_id
& FLASH_VENDMASK
) {
21 case (AMD_MANUFACT
& FLASH_VENDMASK
):
25 printf ("Unknown Vendor ");
29 switch (info
->flash_id
& FLASH_TYPEMASK
) {
30 case (EN29LV640
& FLASH_TYPEMASK
):
31 printf ("EN29LV640 (16Mbit)\n");
34 printf ("Unknown Chip Type\n");
39 printf (" Size: %ld MB in %d Sectors\n",
40 info
->size
>> 20, info
->sector_count
);
42 printf (" Sector Start Addresses:");
43 for (i
= 0; i
< info
->sector_count
; i
++) {
47 printf (" %08lX%s", info
->start
[i
],
48 info
->protect
[i
] ? " (RO)" : " ");
57 unsigned long flash_init (void)
62 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
65 flash_info
[i
].flash_id
=
66 (AMD_MANUFACT
& FLASH_VENDMASK
) |
67 (EN29LV640
& FLASH_TYPEMASK
);
68 flash_info
[i
].size
= FLASH_BANK_SIZE
;
69 flash_info
[i
].sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
70 memset (flash_info
[i
].protect
, 0, CONFIG_SYS_MAX_FLASH_SECT
);
72 flashbase
= PHYS_FLASH_1
;
74 panic ("configured to many flash banks!\n");
76 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
77 flash_info
[i
].start
[j
] = flashbase
+ 0x10000 * j
;
79 size
+= flash_info
[i
].size
;
82 flash_protect (FLAG_PROTECT_SET
,
83 CONFIG_SYS_FLASH_BASE
,
84 CONFIG_SYS_FLASH_BASE
+ 0x2ffff, &flash_info
[0]);
90 #define CMD_READ_ARRAY 0x00F0
91 #define CMD_UNLOCK1 0x00AA
92 #define CMD_UNLOCK2 0x0055
93 #define CMD_ERASE_SETUP 0x0080
94 #define CMD_ERASE_CONFIRM 0x0030
95 #define CMD_PROGRAM 0x00A0
96 #define CMD_UNLOCK_BYPASS 0x0020
98 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00000555<<1)))
99 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x000002AA<<1)))
101 #define BIT_ERASE_DONE 0x0080
102 #define BIT_RDY_MASK 0x0080
103 #define BIT_PROGRAM_ERROR 0x0020
104 #define BIT_TIMEOUT 0x80000000 /* our flag */
111 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
114 int iflag
, prot
, sect
;
119 /* first look for protection bits */
121 if (info
->flash_id
== FLASH_UNKNOWN
)
122 return ERR_UNKNOWN_FLASH_TYPE
;
124 if ((s_first
< 0) || (s_first
> s_last
)) {
128 if ((info
->flash_id
& FLASH_VENDMASK
) !=
129 (AMD_MANUFACT
& FLASH_VENDMASK
)) {
130 return ERR_UNKNOWN_FLASH_VENDOR
;
134 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
135 if (info
->protect
[sect
]) {
140 return ERR_PROTECTED
;
143 * Disable interrupts which might cause a timeout
144 * here. Remember that our exception vectors are
145 * at address 0 in the flash, and we don't want a
146 * (ticker) exception to happen while the flash
147 * chip is in programming mode.
149 iflag
= disable_interrupts ();
153 /* Start erase on unprotected sectors */
154 for (sect
= s_first
; sect
<= s_last
&& !ctrlc (); sect
++) {
155 printf ("Erasing sector %2d ... ", sect
);
157 /* arm simple, non interrupt dependent timer */
158 start
= get_timer(0);
160 if (info
->protect
[sect
] == 0) { /* not protected */
162 (volatile u16
*) (info
->start
[sect
]);
164 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
165 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
166 MEM_FLASH_ADDR1
= CMD_ERASE_SETUP
;
168 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
169 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
170 *addr
= CMD_ERASE_CONFIRM
;
172 /* wait until flash is ready */
179 if (get_timer(start
) > CONFIG_SYS_FLASH_ERASE_TOUT
* CONFIG_SYS_HZ
/ 1000) {
180 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
186 && (result
& 0xFFFF) & BIT_ERASE_DONE
)
191 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
203 } else { /* it was protected */
205 printf ("protected!\n");
210 printf ("User Interrupt!\n");
213 /* allow flash to settle - wait 10 ms */
214 printf("Waiting 10 ms...");
217 /* for (i = 0; i < 10 * 1000 * 1000; ++i)
223 enable_interrupts ();
229 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
231 volatile u16
*addr
= (volatile u16
*) dest
;
239 * Check if Flash is (sufficiently) erased
242 if ((result
& data
) != data
)
243 return ERR_NOT_ERASED
;
247 * Disable interrupts which might cause a timeout
248 * here. Remember that our exception vectors are
249 * at address 0 in the flash, and we don't want a
250 * (ticker) exception to happen while the flash
251 * chip is in programming mode.
253 iflag
= disable_interrupts ();
255 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
256 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
257 MEM_FLASH_ADDR1
= CMD_PROGRAM
;
260 /* arm simple, non interrupt dependent timer */
261 start
= get_timer(0);
263 /* wait until flash is ready */
269 if (get_timer(start
) > CONFIG_SYS_FLASH_ERASE_TOUT
* CONFIG_SYS_HZ
/ 1000) {
273 if (!chip1
&& ((result
& 0x80) == (data
& 0x80)))
278 *addr
= CMD_READ_ARRAY
;
280 if (chip1
== ERR
|| *addr
!= data
)
284 enable_interrupts ();
290 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
296 printf ("unaligned destination not supported\n");
302 printf ("odd transfer sizes not supported\n");
310 data
= (*((volatile u8
*) addr
) << 8) | *((volatile u8
*)
312 if ((rc
= write_word (info
, wp
- 1, data
)) != 0) {
321 data
= *((volatile u16
*) src
);
322 if ((rc
= write_word (info
, wp
, data
)) != 0) {
331 data
= (*((volatile u8
*) src
) << 8) |
332 *((volatile u8
*) (wp
+ 1));
333 if ((rc
= write_word (info
, wp
, data
)) != 0) {