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
11 *--------------------------------------------------------------------
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
36 /*-----------------------------------------------------------------------
40 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
41 static int write_word (flash_info_t *info, ulong dest, ulong data);
43 /*-----------------------------------------------------------------------
46 unsigned long flash_init (void)
51 /* Init: no FLASHes known */
52 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
53 flash_info[i].flash_id = FLASH_UNKNOWN;
56 /* for now, only support the 4 MB Flash SIMM */
57 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH0_BASE, &flash_info[0]);
60 * protect monitor and environment sectors
63 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
64 flash_protect(FLAG_PROTECT_SET,
65 CONFIG_SYS_MONITOR_BASE,
66 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
70 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
71 # ifndef CONFIG_ENV_SIZE
72 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
74 flash_protect(FLAG_PROTECT_SET,
76 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
80 return /*size*/ (CONFIG_SYS_FLASH0_SIZE * 1024 * 1024);
83 /*-----------------------------------------------------------------------
85 void flash_print_info (flash_info_t *info)
89 if (info->flash_id == FLASH_UNKNOWN) {
90 printf ("missing or unknown FLASH type\n");
94 switch ((info->flash_id >> 16) & 0xff) {
99 printf ("Unknown Vendor ");
103 switch (info->flash_id & FLASH_TYPEMASK) {
105 printf ("AM29F040B (4 Mbit)\n");
108 printf ("AM29F080B (8 Mbit)\n");
111 printf ("Unknown Chip Type\n");
115 printf (" Size: %ld MB in %d Sectors\n",
116 info->size >> 20, info->sector_count);
118 printf (" Sector Start Addresses:");
119 for (i=0; i<info->sector_count; ++i) {
124 info->protect[i] ? " (RO)" : " "
132 * The following code cannot be run from FLASH!
135 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
138 vu_long vendor, devid;
139 ulong base = (ulong)addr;
141 /* printf("addr = %08lx\n", (unsigned long)addr); */
143 /* Reset and Write auto select command: read Manufacturer ID */
144 addr[0] = 0xf0f0f0f0;
145 addr[0x0555] = 0xAAAAAAAA;
146 addr[0x02AA] = 0x55555555;
147 addr[0x0555] = 0x90909090;
151 /* printf("vendor = %08lx\n", vendor); */
152 if (vendor != 0x01010101) {
158 /* printf("devid = %08lx\n", devid); */
160 if ((devid & 0xff) == AMD_ID_F080B) {
161 info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F080B;
162 /* we have 16 sectors with 64KB each x 4 */
163 info->sector_count = 16;
164 info->size = 4 * info->sector_count * 64*1024;
171 /* check for protected sectors */
172 for (i = 0; i < info->sector_count; i++) {
173 /* sector base address */
174 info->start[i] = base + i * (info->size / info->sector_count);
175 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
176 /* D0 = 1 if protected */
177 addr = (volatile unsigned long *)(info->start[i]);
178 info->protect[i] = addr[2] & 1;
182 addr = (vu_long *)info->start[0];
185 addr[0] = 0xf0f0f0f0;
191 /*-----------------------------------------------------------------------
194 int flash_erase (flash_info_t *info, int s_first, int s_last)
196 vu_long *addr = (vu_long*)(info->start[0]);
197 int flag, prot, sect, l_sect;
198 ulong start, now, last;
200 if ((s_first < 0) || (s_first > s_last)) {
201 if (info->flash_id == FLASH_UNKNOWN) {
202 printf ("- missing\n");
204 printf ("- no sectors to erase\n");
210 for (sect = s_first; sect <= s_last; sect++) {
211 if (info->protect[sect]) {
217 printf ("- Warning: %d protected sectors will not be erased!\n",
225 /* Disable interrupts which might cause a timeout here */
226 flag = disable_interrupts();
228 addr[0x0555] = 0xAAAAAAAA;
229 addr[0x02AA] = 0x55555555;
230 addr[0x0555] = 0x80808080;
231 addr[0x0555] = 0xAAAAAAAA;
232 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 /*-----------------------------------------------------------------------