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 AMD 29F080B devices
10 * Added support for 64bit and AMD 29DL323B
12 *--------------------------------------------------------------------
13 * SPDX-License-Identifier: GPL-2.0+
20 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
22 #define RD_SWP32(x) in_le32((volatile u32*)x)
24 /*-----------------------------------------------------------------------
28 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
29 static int write_word (flash_info_t *info, ulong dest, ulong data);
31 /*-----------------------------------------------------------------------
34 unsigned long flash_init(void)
38 /* Init: no FLASHes known */
39 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
40 flash_info[i].flash_id = FLASH_UNKNOWN;
42 /* for now, only support the 4 MB Flash SIMM */
43 (void)flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
47 * protect monitor and environment sectors
50 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
51 flash_protect(FLAG_PROTECT_SET,
52 CONFIG_SYS_MONITOR_BASE,
53 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
57 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
58 #ifndef CONFIG_ENV_SIZE
59 #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
61 flash_protect(FLAG_PROTECT_SET,
63 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
66 return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024;
69 /*-----------------------------------------------------------------------
71 void flash_print_info (flash_info_t *info)
75 if (info->flash_id == FLASH_UNKNOWN) {
76 printf ("missing or unknown FLASH type\n");
80 switch (info->flash_id & FLASH_VENDMASK) {
81 case (AMD_MANUFACT & FLASH_VENDMASK):
84 case (FUJ_MANUFACT & FLASH_VENDMASK):
87 case (SST_MANUFACT & FLASH_VENDMASK):
91 printf ("Unknown Vendor ");
95 switch (info->flash_id & FLASH_TYPEMASK) {
96 case (AMD_ID_DL323B & FLASH_TYPEMASK):
97 printf("AM29DL323B (32 MBit)\n");
100 printf ("Unknown Chip Type\n");
104 printf (" Size: %ld MB in %d Sectors\n",
105 info->size >> 20, info->sector_count);
107 printf (" Sector Start Addresses:");
108 for (i = 0; i < info->sector_count; ++i) {
109 if ((i % 5) == 0) printf ("\n ");
112 info->protect[i] ? " (RO)" : " "
120 * The following code cannot be run from FLASH!
123 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
126 vu_long vendor[2], devid[2];
127 ulong base = (ulong)addr;
129 /* Reset and Write auto select command: read Manufacturer ID */
130 addr[0] = 0xf0f0f0f0;
131 addr[2 * 0x0555] = 0xAAAAAAAA;
132 addr[2 * 0x02AA] = 0x55555555;
133 addr[2 * 0x0555] = 0x90909090;
134 addr[1] = 0xf0f0f0f0;
135 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
136 addr[2 * 0x02AA + 1] = 0x55555555;
137 addr[2 * 0x0555 + 1] = 0x90909090;
140 vendor[0] = RD_SWP32(&addr[0]);
141 vendor[1] = RD_SWP32(&addr[1]);
142 if (vendor[0] != vendor[1] || vendor[0] != AMD_MANUFACT) {
147 devid[0] = RD_SWP32(&addr[2]);
148 devid[1] = RD_SWP32(&addr[3]);
150 if (devid[0] == AMD_ID_DL323B) {
153 * Bank 1 (23 Sectors): 0-7=8kbyte, 8-22=64kbyte
154 * Bank 2 (48 Sectors): 23-70=64kbyte
156 info->flash_id = (AMD_MANUFACT & FLASH_VENDMASK) |
157 (AMD_ID_DL323B & FLASH_TYPEMASK);
158 info->sector_count = 71;
159 info->size = 4 * (8 * 8 + 63 * 64) * 1024;
166 /* set up sector start address table */
167 for (i = 0; i < 8; i++) {
168 info->start[i] = base + (i * 0x8000);
170 for (i = 8; i < info->sector_count; i++) {
171 info->start[i] = base + (i * 0x40000) + 8 * 0x8000 - 8 * 0x40000;
174 /* check for protected sectors */
175 for (i = 0; i < info->sector_count; i++) {
176 /* read sector protection at sector address */
177 addr = (volatile unsigned long *)(info->start[i]);
178 addr[2 * 0x0555] = 0xAAAAAAAA;
179 addr[2 * 0x02AA] = 0x55555555;
180 addr[2 * 0x0555] = 0x90909090;
181 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
182 addr[2 * 0x02AA + 1] = 0x55555555;
183 addr[2 * 0x0555 + 1] = 0x90909090;
185 base = RD_SWP32(&addr[4]);
186 base |= RD_SWP32(&addr[5]);
187 info->protect[i] = base & 0x00010001 ? 1 : 0;
189 addr = (vu_long*)info->start[0];
193 addr[0] = 0xf0f0f0f0;
194 addr[1] = 0xf0f0f0f0;
200 /*-----------------------------------------------------------------------
203 int flash_erase (flash_info_t *info, int s_first, int s_last)
205 vu_long *addr = (vu_long*)(info->start[0]);
206 int flag, prot, sect, l_sect;
207 ulong start, now, last;
209 if ((s_first < 0) || (s_first > s_last)) {
210 if (info->flash_id == FLASH_UNKNOWN) {
211 printf ("- missing\n");
213 printf ("- no sectors to erase\n");
219 for (sect = s_first; sect <= s_last; sect++) {
220 if (info->protect[sect]) {
226 printf ("- Warning: %d protected sectors will not be erased!\n",
234 /* Disable interrupts which might cause a timeout here */
235 flag = disable_interrupts();
237 addr[2 * 0x0555] = 0xAAAAAAAA;
238 addr[2 * 0x02AA] = 0x55555555;
239 addr[2 * 0x0555] = 0x80808080;
240 addr[2 * 0x0555] = 0xAAAAAAAA;
241 addr[2 * 0x02AA] = 0x55555555;
242 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
243 addr[2 * 0x02AA + 1] = 0x55555555;
244 addr[2 * 0x0555 + 1] = 0x80808080;
245 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
246 addr[2 * 0x02AA + 1] = 0x55555555;
249 /* Start erase on unprotected sectors */
250 for (sect = s_first; sect<=s_last; sect++) {
251 if (info->protect[sect] == 0) { /* not protected */
252 addr = (vu_long*)(info->start[sect]);
253 addr[0] = 0x30303030;
254 addr[1] = 0x30303030;
259 /* re-enable interrupts if necessary */
263 /* wait at least 80us - let's wait 1 ms */
267 * We wait for the last triggered sector
272 start = get_timer (0);
274 addr = (vu_long*)(info->start[l_sect]);
275 while ( (addr[0] & 0x80808080) != 0x80808080 ||
276 (addr[1] & 0x80808080) != 0x80808080) {
277 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
278 printf ("Timeout\n");
281 /* show that we're waiting */
282 if ((now - last) > 1000) { /* every second */
289 /* reset to read mode */
290 addr = (volatile unsigned long *)info->start[0];
291 addr[0] = 0xF0F0F0F0; /* reset bank */
292 addr[1] = 0xF0F0F0F0; /* reset bank */
298 /*-----------------------------------------------------------------------
299 * Copy memory to flash, returns:
302 * 2 - Flash not erased
305 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
310 wp = (addr & ~3); /* get lower word aligned address */
313 * handle unaligned start bytes
315 if ((l = addr - wp) != 0) {
317 for (i=0, cp=wp; i<l; ++i, ++cp) {
318 data = (data << 8) | (*(uchar *)cp);
320 for (; i<4 && cnt>0; ++i) {
321 data = (data << 8) | *src++;
325 for (; cnt==0 && i<4; ++i, ++cp) {
326 data = (data << 8) | (*(uchar *)cp);
329 if ((rc = write_word(info, wp, data)) != 0) {
336 * handle word aligned part
340 for (i=0; i<4; ++i) {
341 data = (data << 8) | *src++;
343 if ((rc = write_word(info, wp, data)) != 0) {
355 * handle unaligned tail bytes
358 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
359 data = (data << 8) | *src++;
362 for (; i<4; ++i, ++cp) {
363 data = (data << 8) | (*(uchar *)cp);
366 return (write_word(info, wp, data));
369 /*-----------------------------------------------------------------------
370 * Write a word to Flash, returns:
373 * 2 - Flash not erased
375 static int write_word (flash_info_t *info, ulong dest, ulong data)
377 vu_long *addr = (vu_long*)(info->start[0]);
381 /* Check if Flash is (sufficiently) erased */
382 if ((*((vu_long *)dest) & data) != data) {
385 /* Disable interrupts which might cause a timeout here */
386 flag = disable_interrupts();
388 if ((dest & 0x00000004) == 0) {
389 addr[2 * 0x0555] = 0xAAAAAAAA;
390 addr[2 * 0x02AA] = 0x55555555;
391 addr[2 * 0x0555] = 0xA0A0A0A0;
394 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
395 addr[2 * 0x02AA + 1] = 0x55555555;
396 addr[2 * 0x0555 + 1] = 0xA0A0A0A0;
399 *((vu_long *)dest) = data;
401 /* re-enable interrupts if necessary */
405 /* data polling for D7 */
406 start = get_timer (0);
407 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
408 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
415 /*-----------------------------------------------------------------------