3 * Stäubli Faverges - <www.staubli.com>
4 * Pierre AUBERT p.aubert@staubli.com
5 * U-Boot port on RPXClassic LF (CLLF_BW31) board
7 * RPXClassic uses Am29DL323B flash memory with 2 banks
11 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
38 /*-----------------------------------------------------------------------
41 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
42 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
43 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
45 /*-----------------------------------------------------------------------
48 unsigned long flash_init (void)
50 unsigned long size_b0
;
53 /* Init: no FLASHes known */
54 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
55 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
58 size_b0
= flash_get_size((vu_long
*)CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
61 flash_get_offsets (CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
63 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
64 /* monitor protection ON by default */
65 flash_protect(FLAG_PROTECT_SET
,
66 CONFIG_SYS_MONITOR_BASE
,
67 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
71 flash_info
[0].size
= size_b0
;
76 /*-----------------------------------------------------------------------
78 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
82 if (info
->flash_id
& FLASH_BTYPE
) {
83 /* set sector offsets for bottom boot block type */
84 info
->start
[0] = base
+ 0x00000000;
85 info
->start
[1] = base
+ 0x00008000;
86 info
->start
[2] = base
+ 0x00010000;
87 info
->start
[3] = base
+ 0x00018000;
88 info
->start
[4] = base
+ 0x00020000;
89 info
->start
[5] = base
+ 0x00028000;
90 info
->start
[6] = base
+ 0x00030000;
91 info
->start
[7] = base
+ 0x00038000;
92 for (i
= 8; i
< info
->sector_count
; i
++) {
93 info
->start
[i
] = base
+ ((i
-7) * 0x00040000) ;
98 /*-----------------------------------------------------------------------
100 void flash_print_info (flash_info_t
*info
)
104 if (info
->flash_id
== FLASH_UNKNOWN
) {
105 printf ("missing or unknown FLASH type\n");
109 switch (info
->flash_id
& FLASH_VENDMASK
) {
110 case FLASH_MAN_AMD
: printf ("AMD "); break;
111 default: printf ("Unknown Vendor "); break;
114 switch (info
->flash_id
& FLASH_TYPEMASK
) {
116 printf ("AMDL323DB (16 Mbytes, bottom boot sect)\n");
119 printf ("Unknown Chip Type\n");
123 printf (" Size: %ld MB in %d Sectors\n",
124 info
->size
>> 20, info
->sector_count
);
126 printf (" Sector Start Addresses:");
127 for (i
=0; i
<info
->sector_count
; ++i
) {
132 info
->protect
[i
] ? " (RO)" : " "
138 /*-----------------------------------------------------------------------
142 /*-----------------------------------------------------------------------
146 * The following code cannot be run from FLASH!
149 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
153 ulong base
= (ulong
)addr
;
155 /* Reset flash componeny */
156 addr
[0] = 0xf0f0f0f0;
158 /* Write auto select command: read Manufacturer ID */
159 addr
[0xAAA] = 0xAAAAAAAA ;
160 addr
[0x555] = 0x55555555 ;
161 addr
[0xAAA] = 0x90909090 ;
165 switch (value
& 0x00FF00FF) {
167 info
->flash_id
= FLASH_MAN_AMD
;
170 info
->flash_id
= FLASH_UNKNOWN
;
171 info
->sector_count
= 0;
173 return (0); /* no or unknown flash */
176 value
= addr
[2] ; /* device ID */
178 switch (value
& 0x00FF00FF) {
179 case (AMD_ID_DL323B
& 0x00FF00FF):
180 info
->flash_id
+= FLASH_AMDL323B
;
181 info
->sector_count
= 71;
182 info
->size
= 0x01000000; /* 16 Mb */
186 info
->flash_id
= FLASH_UNKNOWN
;
187 return (0); /* => no or unknown flash */
190 /* set up sector start address table */
191 /* set sector offsets for bottom boot block type */
192 info
->start
[0] = base
+ 0x00000000;
193 info
->start
[1] = base
+ 0x00008000;
194 info
->start
[2] = base
+ 0x00010000;
195 info
->start
[3] = base
+ 0x00018000;
196 info
->start
[4] = base
+ 0x00020000;
197 info
->start
[5] = base
+ 0x00028000;
198 info
->start
[6] = base
+ 0x00030000;
199 info
->start
[7] = base
+ 0x00038000;
200 for (i
= 8; i
< info
->sector_count
; i
++) {
201 info
->start
[i
] = base
+ ((i
-7) * 0x00040000) ;
204 /* check for protected sectors */
205 for (i
= 0; i
< 23; i
++) {
206 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
207 /* D0 = 1 if protected */
208 addr
= (volatile unsigned long *)(info
->start
[i
]);
209 info
->protect
[i
] = addr
[4] & 1 ;
211 /* Check for protected sectors in the 2nd bank */
212 addr
[0x100AAA] = 0xAAAAAAAA ;
213 addr
[0x100555] = 0x55555555 ;
214 addr
[0x100AAA] = 0x90909090 ;
216 for (i
= 23; i
< info
->sector_count
; i
++) {
217 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
218 /* D0 = 1 if protected */
219 addr
= (volatile unsigned long *)(info
->start
[i
]);
220 info
->protect
[i
] = addr
[4] & 1 ;
224 * Prevent writes to uninitialized FLASH.
226 if (info
->flash_id
!= FLASH_UNKNOWN
) {
227 addr
= (volatile unsigned long *)info
->start
[0];
229 *addr
= 0xF0F0F0F0; /* reset bank 1 */
230 addr
= (volatile unsigned long *)info
->start
[23];
232 *addr
= 0xF0F0F0F0; /* reset bank 2 */
240 /*-----------------------------------------------------------------------
243 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
245 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
246 int flag
, prot
, sect
, l_sect
;
247 ulong start
, now
, last
;
249 if ((s_first
< 0) || (s_first
> s_last
)) {
250 if (info
->flash_id
== FLASH_UNKNOWN
) {
251 printf ("- missing\n");
253 printf ("- no sectors to erase\n");
258 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
259 (info
->flash_id
> FLASH_AMD_COMP
)) {
260 printf ("Can't erase unknown flash type %08lx - aborted\n",
266 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
267 if (info
->protect
[sect
]) {
273 printf ("- Warning: %d protected sectors will not be erased!\n",
281 /* Disable interrupts which might cause a timeout here */
282 flag
= disable_interrupts();
284 addr
[0xAAA] = 0xAAAAAAAA;
285 addr
[0x555] = 0x55555555;
286 addr
[0xAAA] = 0x80808080;
287 addr
[0xAAA] = 0xAAAAAAAA;
288 addr
[0x555] = 0x55555555;
290 /* Start erase on unprotected sectors */
291 for (sect
= s_first
; sect
<=s_last
; sect
++) {
292 if (info
->protect
[sect
] == 0) { /* not protected */
293 addr
= (vu_long
*)(info
->start
[sect
]) ;
294 addr
[0] = 0x30303030 ;
299 /* re-enable interrupts if necessary */
303 /* wait at least 80us - let's wait 1 ms */
307 * We wait for the last triggered sector
312 start
= get_timer (0);
314 addr
= (vu_long
*)(info
->start
[l_sect
]);
315 while ((addr
[0] & 0x80808080) != 0x80808080) {
316 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
317 printf ("Timeout\n");
320 /* show that we're waiting */
321 if ((now
- last
) > 1000) { /* every second */
328 /* reset to read mode */
329 addr
= (vu_long
*)info
->start
[0];
330 addr
[0] = 0xF0F0F0F0; /* reset bank */
336 /*-----------------------------------------------------------------------
337 * Copy memory to flash, returns:
340 * 2 - Flash not erased
343 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
348 wp
= (addr
& ~3); /* get lower word aligned address */
351 * handle unaligned start bytes
353 if ((l
= addr
- wp
) != 0) {
355 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
356 data
= (data
<< 8) | (*(uchar
*)cp
);
358 for (; i
<4 && cnt
>0; ++i
) {
359 data
= (data
<< 8) | *src
++;
363 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
364 data
= (data
<< 8) | (*(uchar
*)cp
);
367 if ((rc
= write_word(info
, wp
, data
)) != 0) {
374 * handle word aligned part
378 for (i
=0; i
<4; ++i
) {
379 data
= (data
<< 8) | *src
++;
381 if ((rc
= write_word(info
, wp
, data
)) != 0) {
393 * handle unaligned tail bytes
396 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
397 data
= (data
<< 8) | *src
++;
400 for (; i
<4; ++i
, ++cp
) {
401 data
= (data
<< 8) | (*(uchar
*)cp
);
404 return (write_word(info
, wp
, data
));
407 /*-----------------------------------------------------------------------
408 * Write a word to Flash, returns:
411 * 2 - Flash not erased
413 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
415 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
419 /* Check if Flash is (sufficiently) erased */
420 if ((*((vu_long
*)dest
) & data
) != data
) {
423 /* Disable interrupts which might cause a timeout here */
424 flag
= disable_interrupts();
426 addr
[0xAAA] = 0xAAAAAAAA;
427 addr
[0x555] = 0x55555555;
428 addr
[0xAAA] = 0xA0A0A0A0;
430 *((vu_long
*)dest
) = data
;
432 /* re-enable interrupts if necessary */
436 /* data polling for D7 */
437 start
= get_timer (0);
438 while ((*((vu_long
*)dest
) & 0x80808080) != (data
& 0x80808080)) {
439 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
446 /*-----------------------------------------------------------------------