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 * 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,
35 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
37 /*-----------------------------------------------------------------------
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);
44 /*-----------------------------------------------------------------------
47 unsigned long flash_init (void)
49 unsigned long size, totsize;
53 /* Init: no FLASHes known */
54 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
55 flash_info[i].flash_id = FLASH_UNKNOWN;
60 for(i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
61 size = flash_get_size((vu_long *)addr, &flash_info[i]);
62 if (flash_info[i].flash_id == FLASH_UNKNOWN)
69 for(i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
71 size = flash_get_size((vu_long *)addr, &flash_info[i]);
72 if (flash_info[i].flash_id == FLASH_UNKNOWN)
78 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
79 /* monitor protection ON by default */
80 flash_protect(FLAG_PROTECT_SET,
81 CONFIG_SYS_MONITOR_BASE,
82 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
89 /*-----------------------------------------------------------------------
91 void flash_print_info (flash_info_t *info)
95 if (info->flash_id == FLASH_UNKNOWN) {
96 printf ("missing or unknown FLASH type\n");
100 switch (info->flash_id >> 16) {
105 printf ("Unknown Vendor ");
109 switch (info->flash_id & FLASH_TYPEMASK) {
111 printf ("AM29F040B (4 Mbit)\n");
114 printf ("AM29F080B (8 Mbit)\n");
117 printf ("AM29F016D (16 Mbit)\n");
120 printf ("Unknown Chip Type\n");
124 printf (" Size: %ld MB in %d Sectors\n",
125 info->size >> 20, info->sector_count);
127 printf (" Sector Start Addresses:");
128 for (i=0; i<info->sector_count; ++i) {
133 info->protect[i] ? " (RO)" : " "
141 * The following code cannot be run from FLASH!
144 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
148 ulong base = (ulong)addr;
150 /* Write auto select command: read Manufacturer ID */
151 addr[0x0555] = 0xAAAAAAAA;
152 addr[0x02AA] = 0x55555555;
153 addr[0x0555] = 0x90909090;
156 devid = addr[1] & 0xff;
158 /* only support AMD */
159 if (vendor != 0x01010101) {
166 if (devid == AMD_ID_F040B) {
167 info->flash_id = vendor << 16 | devid;
168 info->sector_count = 8;
169 info->size = info->sector_count * 0x10000;
171 else if (devid == AMD_ID_F080B) {
172 info->flash_id = vendor << 16 | devid;
173 info->sector_count = 16;
174 info->size = 4 * info->sector_count * 0x10000;
176 else if (devid == AMD_ID_F016D) {
177 info->flash_id = vendor << 16 | devid;
178 info->sector_count = 32;
179 info->size = 4 * info->sector_count * 0x10000;
182 printf ("## Unknown Flash Type: %08lx\n", devid);
186 /* check for protected sectors */
187 for (i = 0; i < info->sector_count; i++) {
188 /* sector base address */
189 info->start[i] = base + i * (info->size / info->sector_count);
190 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
191 /* D0 = 1 if protected */
192 addr = (volatile unsigned long *)(info->start[i]);
193 info->protect[i] = addr[2] & 1;
197 * Prevent writes to uninitialized FLASH.
199 if (info->flash_id != FLASH_UNKNOWN) {
200 addr = (vu_long *)info->start[0];
201 addr[0] = 0xF0; /* reset bank */
208 /*-----------------------------------------------------------------------
211 int flash_erase (flash_info_t *info, int s_first, int s_last)
213 vu_long *addr = (vu_long*)(info->start[0]);
214 int flag, prot, sect, l_sect;
215 ulong start, now, last;
217 if ((s_first < 0) || (s_first > s_last)) {
218 if (info->flash_id == FLASH_UNKNOWN) {
219 printf ("- missing\n");
221 printf ("- no sectors to erase\n");
227 for (sect = s_first; sect <= s_last; sect++) {
228 if (info->protect[sect]) {
234 printf ("- Warning: %d protected sectors will not be erased!\n",
242 /* Disable interrupts which might cause a timeout here */
243 flag = disable_interrupts();
245 addr[0x0555] = 0XAAAAAAAA;
246 addr[0x02AA] = 0x55555555;
247 addr[0x0555] = 0x80808080;
248 addr[0x0555] = 0XAAAAAAAA;
249 addr[0x02AA] = 0x55555555;
251 /* Start erase on unprotected sectors */
252 for (sect = s_first; sect<=s_last; sect++) {
253 if (info->protect[sect] == 0) { /* not protected */
254 addr = (vu_long*)(info->start[sect]);
255 addr[0] = 0x30303030;
260 /* re-enable interrupts if necessary */
264 /* wait at least 80us - let's wait 1 ms */
268 * We wait for the last triggered sector
273 start = get_timer (0);
275 addr = (vu_long*)(info->start[l_sect]);
276 while ((addr[0] & 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 */
297 /*-----------------------------------------------------------------------
298 * Copy memory to flash, returns:
301 * 2 - Flash not erased
304 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
309 wp = (addr & ~3); /* get lower word aligned address */
312 * handle unaligned start bytes
314 if ((l = addr - wp) != 0) {
316 for (i=0, cp=wp; i<l; ++i, ++cp) {
317 data = (data << 8) | (*(uchar *)cp);
319 for (; i<4 && cnt>0; ++i) {
320 data = (data << 8) | *src++;
324 for (; cnt==0 && i<4; ++i, ++cp) {
325 data = (data << 8) | (*(uchar *)cp);
328 if ((rc = write_word(info, wp, data)) != 0) {
335 * handle word aligned part
339 for (i=0; i<4; ++i) {
340 data = (data << 8) | *src++;
342 if ((rc = write_word(info, wp, data)) != 0) {
354 * handle unaligned tail bytes
357 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
358 data = (data << 8) | *src++;
361 for (; i<4; ++i, ++cp) {
362 data = (data << 8) | (*(uchar *)cp);
365 return (write_word(info, wp, data));
368 /*-----------------------------------------------------------------------
369 * Write a word to Flash, returns:
372 * 2 - Flash not erased
374 static int write_word (flash_info_t *info, ulong dest, ulong data)
376 vu_long *addr = (vu_long*)(info->start[0]);
380 /* Check if Flash is (sufficiently) erased */
381 if ((*((vu_long *)dest) & data) != data) {
384 /* Disable interrupts which might cause a timeout here */
385 flag = disable_interrupts();
387 addr[0x0555] = 0xAAAAAAAA;
388 addr[0x02AA] = 0x55555555;
389 addr[0x0555] = 0xA0A0A0A0;
391 *((vu_long *)dest) = data;
393 /* re-enable interrupts if necessary */
397 /* data polling for D7 */
398 start = get_timer (0);
399 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
400 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
407 /*-----------------------------------------------------------------------