3 * Marius Groeger <mgroeger@sysgo.de>
4 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * Flash Routines for AM290[48]0B devices
11 *--------------------------------------------------------------------
12 * SPDX-License-Identifier: GPL-2.0+
19 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
21 /*-----------------------------------------------------------------------
25 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
26 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
28 /*-----------------------------------------------------------------------
31 unsigned long flash_init (void)
33 unsigned long size
, totsize
;
37 /* Init: no FLASHes known */
38 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
39 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
44 for(i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
45 size
= flash_get_size((vu_long
*)addr
, &flash_info
[i
]);
46 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
53 for(i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
55 size
= flash_get_size((vu_long
*)addr
, &flash_info
[i
]);
56 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
62 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
63 /* monitor protection ON by default */
64 flash_protect(FLAG_PROTECT_SET
,
65 CONFIG_SYS_MONITOR_BASE
,
66 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
73 /*-----------------------------------------------------------------------
75 void flash_print_info (flash_info_t
*info
)
79 if (info
->flash_id
== FLASH_UNKNOWN
) {
80 printf ("missing or unknown FLASH type\n");
84 switch (info
->flash_id
>> 16) {
89 printf ("Unknown Vendor ");
93 switch (info
->flash_id
& FLASH_TYPEMASK
) {
95 printf ("AM29F040B (4 Mbit)\n");
98 printf ("AM29F080B (8 Mbit)\n");
101 printf ("AM29F016D (16 Mbit)\n");
104 printf ("Unknown Chip Type\n");
108 printf (" Size: %ld MB in %d Sectors\n",
109 info
->size
>> 20, info
->sector_count
);
111 printf (" Sector Start Addresses:");
112 for (i
=0; i
<info
->sector_count
; ++i
) {
117 info
->protect
[i
] ? " (RO)" : " "
125 * The following code cannot be run from FLASH!
128 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
132 ulong base
= (ulong
)addr
;
134 /* Write auto select command: read Manufacturer ID */
135 addr
[0x0555] = 0xAAAAAAAA;
136 addr
[0x02AA] = 0x55555555;
137 addr
[0x0555] = 0x90909090;
140 devid
= addr
[1] & 0xff;
142 /* only support AMD */
143 if (vendor
!= 0x01010101) {
150 if (devid
== AMD_ID_F040B
) {
151 info
->flash_id
= vendor
<< 16 | devid
;
152 info
->sector_count
= 8;
153 info
->size
= info
->sector_count
* 0x10000;
155 else if (devid
== AMD_ID_F080B
) {
156 info
->flash_id
= vendor
<< 16 | devid
;
157 info
->sector_count
= 16;
158 info
->size
= 4 * info
->sector_count
* 0x10000;
160 else if (devid
== AMD_ID_F016D
) {
161 info
->flash_id
= vendor
<< 16 | devid
;
162 info
->sector_count
= 32;
163 info
->size
= 4 * info
->sector_count
* 0x10000;
166 printf ("## Unknown Flash Type: %08lx\n", devid
);
170 /* check for protected sectors */
171 for (i
= 0; i
< info
->sector_count
; i
++) {
172 /* sector base address */
173 info
->start
[i
] = base
+ i
* (info
->size
/ info
->sector_count
);
174 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
175 /* D0 = 1 if protected */
176 addr
= (volatile unsigned long *)(info
->start
[i
]);
177 info
->protect
[i
] = addr
[2] & 1;
181 * Prevent writes to uninitialized FLASH.
183 if (info
->flash_id
!= FLASH_UNKNOWN
) {
184 addr
= (vu_long
*)info
->start
[0];
185 addr
[0] = 0xF0; /* reset bank */
192 /*-----------------------------------------------------------------------
195 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
197 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
198 int flag
, prot
, sect
, l_sect
;
199 ulong start
, now
, last
;
201 if ((s_first
< 0) || (s_first
> s_last
)) {
202 if (info
->flash_id
== FLASH_UNKNOWN
) {
203 printf ("- missing\n");
205 printf ("- no sectors to erase\n");
211 for (sect
= s_first
; sect
<= s_last
; sect
++) {
212 if (info
->protect
[sect
]) {
218 printf ("- Warning: %d protected sectors will not be erased!\n",
226 /* Disable interrupts which might cause a timeout here */
227 flag
= disable_interrupts();
229 addr
[0x0555] = 0XAAAAAAAA;
230 addr
[0x02AA] = 0x55555555;
231 addr
[0x0555] = 0x80808080;
232 addr
[0x0555] = 0XAAAAAAAA;
233 addr
[0x02AA] = 0x55555555;
235 /* Start erase on unprotected sectors */
236 for (sect
= s_first
; sect
<=s_last
; sect
++) {
237 if (info
->protect
[sect
] == 0) { /* not protected */
238 addr
= (vu_long
*)(info
->start
[sect
]);
239 addr
[0] = 0x30303030;
244 /* re-enable interrupts if necessary */
248 /* wait at least 80us - let's wait 1 ms */
252 * We wait for the last triggered sector
257 start
= get_timer (0);
259 addr
= (vu_long
*)(info
->start
[l_sect
]);
260 while ((addr
[0] & 0x80808080) != 0x80808080) {
261 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
262 printf ("Timeout\n");
265 /* show that we're waiting */
266 if ((now
- last
) > 1000) { /* every second */
273 /* reset to read mode */
274 addr
= (volatile unsigned long *)info
->start
[0];
275 addr
[0] = 0xF0F0F0F0; /* reset bank */
281 /*-----------------------------------------------------------------------
282 * Copy memory to flash, returns:
285 * 2 - Flash not erased
288 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
293 wp
= (addr
& ~3); /* get lower word aligned address */
296 * handle unaligned start bytes
298 if ((l
= addr
- wp
) != 0) {
300 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
301 data
= (data
<< 8) | (*(uchar
*)cp
);
303 for (; i
<4 && cnt
>0; ++i
) {
304 data
= (data
<< 8) | *src
++;
308 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
309 data
= (data
<< 8) | (*(uchar
*)cp
);
312 if ((rc
= write_word(info
, wp
, data
)) != 0) {
319 * handle word aligned part
323 for (i
=0; i
<4; ++i
) {
324 data
= (data
<< 8) | *src
++;
326 if ((rc
= write_word(info
, wp
, data
)) != 0) {
338 * handle unaligned tail bytes
341 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
342 data
= (data
<< 8) | *src
++;
345 for (; i
<4; ++i
, ++cp
) {
346 data
= (data
<< 8) | (*(uchar
*)cp
);
349 return (write_word(info
, wp
, data
));
352 /*-----------------------------------------------------------------------
353 * Write a word to Flash, returns:
356 * 2 - Flash not erased
358 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
360 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
364 /* Check if Flash is (sufficiently) erased */
365 if ((*((vu_long
*)dest
) & data
) != data
) {
368 /* Disable interrupts which might cause a timeout here */
369 flag
= disable_interrupts();
371 addr
[0x0555] = 0xAAAAAAAA;
372 addr
[0x02AA] = 0x55555555;
373 addr
[0x0555] = 0xA0A0A0A0;
375 *((vu_long
*)dest
) = data
;
377 /* re-enable interrupts if necessary */
381 /* data polling for D7 */
382 start
= get_timer (0);
383 while ((*((vu_long
*)dest
) & 0x80808080) != (data
& 0x80808080)) {
384 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
391 /*-----------------------------------------------------------------------