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 * 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];
38 #define RD_SWP32(x) in_le32((volatile u32*)x)
40 /*-----------------------------------------------------------------------
44 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
45 static int write_word (flash_info_t *info, ulong dest, ulong data);
47 /*-----------------------------------------------------------------------
50 unsigned long flash_init (void)
55 /* Init: no FLASHes known */
56 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
57 flash_info[i].flash_id = FLASH_UNKNOWN;
60 /* for now, only support the 4 MB Flash SIMM */
61 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH0_BASE, &flash_info[0]);
64 * protect monitor and environment sectors
67 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
68 flash_protect(FLAG_PROTECT_SET,
69 CONFIG_SYS_MONITOR_BASE,
70 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
74 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
75 # ifndef CONFIG_ENV_SIZE
76 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
78 flash_protect(FLAG_PROTECT_SET,
80 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
84 return /*size*/ (CONFIG_SYS_FLASH0_SIZE * 1024 * 1024);
87 /*-----------------------------------------------------------------------
89 void flash_print_info (flash_info_t *info)
93 if (info->flash_id == FLASH_UNKNOWN) {
94 printf ("missing or unknown FLASH type\n");
98 switch (info->flash_id & FLASH_VENDMASK) {
99 case (AMD_MANUFACT & FLASH_VENDMASK):
102 case (FUJ_MANUFACT & FLASH_VENDMASK):
105 case (SST_MANUFACT & FLASH_VENDMASK):
109 printf ("Unknown Vendor ");
113 switch (info->flash_id & FLASH_TYPEMASK) {
114 case (AMD_ID_DL323B & FLASH_TYPEMASK):
115 printf("AM29DL323B (32 MBit)\n");
118 printf ("Unknown Chip Type\n");
122 printf (" Size: %ld MB in %d Sectors\n",
123 info->size >> 20, info->sector_count);
125 printf (" Sector Start Addresses:");
126 for (i = 0; i < info->sector_count; ++i) {
127 if ((i % 5) == 0) printf ("\n ");
130 info->protect[i] ? " (RO)" : " "
138 * The following code cannot be run from FLASH!
141 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
144 vu_long vendor[2], devid[2];
145 ulong base = (ulong)addr;
147 /* Reset and Write auto select command: read Manufacturer ID */
148 addr[0] = 0xf0f0f0f0;
149 addr[2 * 0x0555] = 0xAAAAAAAA;
150 addr[2 * 0x02AA] = 0x55555555;
151 addr[2 * 0x0555] = 0x90909090;
152 addr[1] = 0xf0f0f0f0;
153 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
154 addr[2 * 0x02AA + 1] = 0x55555555;
155 addr[2 * 0x0555 + 1] = 0x90909090;
158 vendor[0] = RD_SWP32(&addr[0]);
159 vendor[1] = RD_SWP32(&addr[1]);
160 if (vendor[0] != vendor[1] || vendor[0] != AMD_MANUFACT) {
165 devid[0] = RD_SWP32(&addr[2]);
166 devid[1] = RD_SWP32(&addr[3]);
168 if (devid[0] == AMD_ID_DL323B) {
171 * Bank 1 (23 Sectors): 0-7=8kbyte, 8-22=64kbyte
172 * Bank 2 (48 Sectors): 23-70=64kbyte
174 info->flash_id = (AMD_MANUFACT & FLASH_VENDMASK) |
175 (AMD_ID_DL323B & FLASH_TYPEMASK);
176 info->sector_count = 71;
177 info->size = 4 * (8 * 8 + 63 * 64) * 1024;
184 /* set up sector start address table */
185 for (i = 0; i < 8; i++) {
186 info->start[i] = base + (i * 0x8000);
188 for (i = 8; i < info->sector_count; i++) {
189 info->start[i] = base + (i * 0x40000) + 8 * 0x8000 - 8 * 0x40000;
192 /* check for protected sectors */
193 for (i = 0; i < info->sector_count; i++) {
194 /* read sector protection at sector address */
195 addr = (volatile unsigned long *)(info->start[i]);
196 addr[2 * 0x0555] = 0xAAAAAAAA;
197 addr[2 * 0x02AA] = 0x55555555;
198 addr[2 * 0x0555] = 0x90909090;
199 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
200 addr[2 * 0x02AA + 1] = 0x55555555;
201 addr[2 * 0x0555 + 1] = 0x90909090;
203 base = RD_SWP32(&addr[4]);
204 base |= RD_SWP32(&addr[5]);
205 info->protect[i] = base & 0x00010001 ? 1 : 0;
207 addr = (vu_long*)info->start[0];
211 addr[0] = 0xf0f0f0f0;
212 addr[1] = 0xf0f0f0f0;
218 /*-----------------------------------------------------------------------
221 int flash_erase (flash_info_t *info, int s_first, int s_last)
223 vu_long *addr = (vu_long*)(info->start[0]);
224 int flag, prot, sect, l_sect;
225 ulong start, now, last;
227 if ((s_first < 0) || (s_first > s_last)) {
228 if (info->flash_id == FLASH_UNKNOWN) {
229 printf ("- missing\n");
231 printf ("- no sectors to erase\n");
237 for (sect = s_first; sect <= s_last; sect++) {
238 if (info->protect[sect]) {
244 printf ("- Warning: %d protected sectors will not be erased!\n",
252 /* Disable interrupts which might cause a timeout here */
253 flag = disable_interrupts();
255 addr[2 * 0x0555] = 0xAAAAAAAA;
256 addr[2 * 0x02AA] = 0x55555555;
257 addr[2 * 0x0555] = 0x80808080;
258 addr[2 * 0x0555] = 0xAAAAAAAA;
259 addr[2 * 0x02AA] = 0x55555555;
260 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
261 addr[2 * 0x02AA + 1] = 0x55555555;
262 addr[2 * 0x0555 + 1] = 0x80808080;
263 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
264 addr[2 * 0x02AA + 1] = 0x55555555;
267 /* Start erase on unprotected sectors */
268 for (sect = s_first; sect<=s_last; sect++) {
269 if (info->protect[sect] == 0) { /* not protected */
270 addr = (vu_long*)(info->start[sect]);
271 addr[0] = 0x30303030;
272 addr[1] = 0x30303030;
277 /* re-enable interrupts if necessary */
281 /* wait at least 80us - let's wait 1 ms */
285 * We wait for the last triggered sector
290 start = get_timer (0);
292 addr = (vu_long*)(info->start[l_sect]);
293 while ( (addr[0] & 0x80808080) != 0x80808080 ||
294 (addr[1] & 0x80808080) != 0x80808080) {
295 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
296 printf ("Timeout\n");
299 /* show that we're waiting */
300 if ((now - last) > 1000) { /* every second */
307 /* reset to read mode */
308 addr = (volatile unsigned long *)info->start[0];
309 addr[0] = 0xF0F0F0F0; /* reset bank */
310 addr[1] = 0xF0F0F0F0; /* reset bank */
316 /*-----------------------------------------------------------------------
317 * Copy memory to flash, returns:
320 * 2 - Flash not erased
323 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
328 wp = (addr & ~3); /* get lower word aligned address */
331 * handle unaligned start bytes
333 if ((l = addr - wp) != 0) {
335 for (i=0, cp=wp; i<l; ++i, ++cp) {
336 data = (data << 8) | (*(uchar *)cp);
338 for (; i<4 && cnt>0; ++i) {
339 data = (data << 8) | *src++;
343 for (; cnt==0 && i<4; ++i, ++cp) {
344 data = (data << 8) | (*(uchar *)cp);
347 if ((rc = write_word(info, wp, data)) != 0) {
354 * handle word aligned part
358 for (i=0; i<4; ++i) {
359 data = (data << 8) | *src++;
361 if ((rc = write_word(info, wp, data)) != 0) {
373 * handle unaligned tail bytes
376 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
377 data = (data << 8) | *src++;
380 for (; i<4; ++i, ++cp) {
381 data = (data << 8) | (*(uchar *)cp);
384 return (write_word(info, wp, data));
387 /*-----------------------------------------------------------------------
388 * Write a word to Flash, returns:
391 * 2 - Flash not erased
393 static int write_word (flash_info_t *info, ulong dest, ulong data)
395 vu_long *addr = (vu_long*)(info->start[0]);
399 /* Check if Flash is (sufficiently) erased */
400 if ((*((vu_long *)dest) & data) != data) {
403 /* Disable interrupts which might cause a timeout here */
404 flag = disable_interrupts();
406 if ((dest & 0x00000004) == 0) {
407 addr[2 * 0x0555] = 0xAAAAAAAA;
408 addr[2 * 0x02AA] = 0x55555555;
409 addr[2 * 0x0555] = 0xA0A0A0A0;
412 addr[2 * 0x0555 + 1] = 0xAAAAAAAA;
413 addr[2 * 0x02AA + 1] = 0x55555555;
414 addr[2 * 0x0555 + 1] = 0xA0A0A0A0;
417 *((vu_long *)dest) = data;
419 /* re-enable interrupts if necessary */
423 /* data polling for D7 */
424 start = get_timer (0);
425 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
426 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
433 /*-----------------------------------------------------------------------