blob: 818a7c3929cefaf6293b2ea40232a682ea9c65a6 [file] [log] [blame]
Stefan Roese5ff4c3f2005-08-15 12:31:23 +02001/*
2 * (C) Copyright 2002-2005
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
6 * Add support for Am29F016D and dynamic switch setting.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26/*
27 * Ported from Ebony flash support
28 * Travis B. Sawyer
29 * Sandburst Corporation
30 */
31#include <common.h>
Stefan Roese247e9d72010-09-09 19:18:00 +020032#include <asm/ppc4xx.h>
Stefan Roese5ff4c3f2005-08-15 12:31:23 +020033#include <asm/processor.h>
34
35
36#undef DEBUG
37#ifdef DEBUG
38#define DEBUGF(x...) printf(x)
39#else
40#define DEBUGF(x...)
41#endif /* DEBUG */
42
43
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020044flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
Stefan Roese5ff4c3f2005-08-15 12:31:23 +020045
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046static unsigned long flash_addr_table[8][CONFIG_SYS_MAX_FLASH_BANKS] = {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +020047 {0xfff80000} /* Boot Flash */
48};
49
50/*-----------------------------------------------------------------------
51 * Functions
52 */
53static ulong flash_get_size (vu_long *addr, flash_info_t *info);
54static int write_word (flash_info_t *info, ulong dest, ulong data);
55
56
57#define ADDR0 0x5555
58#define ADDR1 0x2aaa
59#define FLASH_WORD_SIZE unsigned char
60
61
62/*-----------------------------------------------------------------------
63 */
64
65unsigned long flash_init (void)
66{
67 unsigned long total_b = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020068 unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
Stefan Roese5ff4c3f2005-08-15 12:31:23 +020069 unsigned short index = 0;
70 int i;
71
72
73 DEBUGF("\n");
74 DEBUGF("FLASH: Index: %d\n", index);
75
76 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +020078 flash_info[i].flash_id = FLASH_UNKNOWN;
79 flash_info[i].sector_count = -1;
80 flash_info[i].size = 0;
81
82 /* check whether the address is 0 */
83 if (flash_addr_table[index][i] == 0) {
84 continue;
85 }
86
87 /* call flash_get_size() to initialize sector address */
88 size_b[i] = flash_get_size(
89 (vu_long *)flash_addr_table[index][i], &flash_info[i]);
90 flash_info[i].size = size_b[i];
91 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
92 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
93 i, size_b[i], size_b[i]<<20);
94 flash_info[i].sector_count = -1;
95 flash_info[i].size = 0;
96 }
97
98 total_b += flash_info[i].size;
99 }
100
101 return total_b;
102}
103
104
105/*-----------------------------------------------------------------------
106 */
107void flash_print_info (flash_info_t *info)
108{
109 int i;
110 int k;
111 int size;
112 int erased;
113 volatile unsigned long *flash;
114
115 if (info->flash_id == FLASH_UNKNOWN) {
116 printf ("missing or unknown FLASH type\n");
117 return;
118 }
119
120 switch (info->flash_id & FLASH_VENDMASK) {
121 case FLASH_MAN_AMD: printf ("AMD "); break;
122 default: printf ("Unknown Vendor "); break;
123 }
124
125 switch (info->flash_id & FLASH_TYPEMASK) {
126 case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
127 break;
128 default: printf ("Unknown Chip Type\n");
129 break;
130 }
131
132 printf (" Size: %ld KB in %d Sectors\n",
133 info->size >> 10, info->sector_count);
134
135 printf (" Sector Start Addresses:");
136 for (i=0; i<info->sector_count; ++i) {
137 /*
138 * Check if whole sector is erased
139 */
140 if (i != (info->sector_count-1))
141 size = info->start[i+1] - info->start[i];
142 else
143 size = info->start[0] + info->size - info->start[i];
144 erased = 1;
145 flash = (volatile unsigned long *)info->start[i];
146 size = size >> 2; /* divide by 4 for longword access */
147 for (k=0; k<size; k++)
148 {
149 if (*flash++ != 0xffffffff)
150 {
151 erased = 0;
152 break;
153 }
154 }
155
156 if ((i % 5) == 0)
157 printf ("\n ");
158 printf (" %08lX%s%s",
159 info->start[i],
160 erased ? " E" : " ",
161 info->protect[i] ? "RO " : " "
162 );
163 }
164 printf ("\n");
165 return;
166 }
167
168/*-----------------------------------------------------------------------
169 */
170
171
172/*-----------------------------------------------------------------------
173 */
174
175/*
176 * The following code cannot be run from FLASH!
177 */
178static ulong flash_get_size (vu_long *addr, flash_info_t *info)
179{
180 short i;
181 FLASH_WORD_SIZE value;
182 ulong base = (ulong)addr;
183 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
184
185 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr );
186
187 /* Write auto select command: read Manufacturer ID */
188 udelay(10000);
189 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
190 udelay(1000);
191 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
192 udelay(1000);
193 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
194 udelay(1000);
195
196 value = addr2[0];
197
198 DEBUGF("FLASH MANUFACT: %x\n", value);
199
200 switch (value) {
201 case (FLASH_WORD_SIZE)AMD_MANUFACT:
202 info->flash_id = FLASH_MAN_AMD;
203 break;
204 default:
205 info->flash_id = FLASH_UNKNOWN;
206 info->sector_count = 0;
207 info->size = 0;
208 return (0); /* no or unknown flash */
209 }
210
211 value = addr2[1]; /* device ID */
212
213 DEBUGF("\nFLASH DEVICEID: %x\n", value);
214
215 switch (value) {
216 case (FLASH_WORD_SIZE)AMD_ID_LV040B:
217 info->flash_id += FLASH_AM040;
218 info->sector_count = 8;
219 info->size = 0x00080000; /* => 512 kb */
220 break;
221
222 default:
223 info->flash_id = FLASH_UNKNOWN;
224 return (0); /* => no or unknown flash */
225
226 }
227
228 /* set up sector start address table */
229 if (info->flash_id == FLASH_AM040) {
230 for (i = 0; i < info->sector_count; i++)
231 info->start[i] = base + (i * 0x00010000);
232 } else {
233 if (info->flash_id & FLASH_BTYPE) {
234 /* set sector offsets for bottom boot block type */
235 info->start[0] = base + 0x00000000;
236 info->start[1] = base + 0x00004000;
237 info->start[2] = base + 0x00006000;
238 info->start[3] = base + 0x00008000;
239 for (i = 4; i < info->sector_count; i++) {
240 info->start[i] = base + (i * 0x00010000) - 0x00030000;
241 }
242 } else {
243 /* set sector offsets for top boot block type */
244 i = info->sector_count - 1;
245 info->start[i--] = base + info->size - 0x00004000;
246 info->start[i--] = base + info->size - 0x00006000;
247 info->start[i--] = base + info->size - 0x00008000;
248 for (; i >= 0; i--) {
249 info->start[i] = base + i * 0x00010000;
250 }
251 }
252 }
253
254 /* check for protected sectors */
255 for (i = 0; i < info->sector_count; i++) {
256 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
257 /* D0 = 1 if protected */
258 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
259 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
260 info->protect[i] = 0;
261 else
262 info->protect[i] = addr2[2] & 1;
263 }
264
265 /* reset to return to reading data */
266 addr2[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
267
268 /*
269 * Prevent writes to uninitialized FLASH.
270 */
271 if (info->flash_id != FLASH_UNKNOWN) {
272 addr2 = (FLASH_WORD_SIZE *)info->start[0];
273 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
274 }
275
276 return (info->size);
277}
278
279int wait_for_DQ7(flash_info_t *info, int sect)
280{
281 ulong start, now, last;
282 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
283
284 start = get_timer (0);
285 last = start;
286 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200287 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200288 printf ("Timeout\n");
289 return -1;
290 }
291 /* show that we're waiting */
292 if ((now - last) > 1000) { /* every second */
293 putc ('.');
294 last = now;
295 }
296 }
297 return 0;
298}
299
300/*-----------------------------------------------------------------------
301 */
302
303int flash_erase (flash_info_t *info, int s_first, int s_last)
304{
305 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
306 volatile FLASH_WORD_SIZE *addr2;
Wolfgang Denk3e1d4932011-11-29 22:17:50 +0000307 int flag, prot, sect;
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200308 int i;
309
310 if ((s_first < 0) || (s_first > s_last)) {
311 if (info->flash_id == FLASH_UNKNOWN) {
312 printf ("- missing\n");
313 } else {
314 printf ("- no sectors to erase\n");
315 }
316 return 1;
317 }
318
319 if (info->flash_id == FLASH_UNKNOWN) {
320 printf ("Can't erase unknown flash type - aborted\n");
321 return 1;
322 }
323
324 prot = 0;
325 for (sect=s_first; sect<=s_last; ++sect) {
326 if (info->protect[sect]) {
327 prot++;
328 }
329 }
330
331 if (prot) {
332 printf ("- Warning: %d protected sectors will not be erased!\n",
333 prot);
334 } else {
335 printf ("\n");
336 }
337
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200338 /* Disable interrupts which might cause a timeout here */
339 flag = disable_interrupts();
340
341 /* Start erase on unprotected sectors */
342 for (sect = s_first; sect<=s_last; sect++) {
343 if (info->protect[sect] == 0) { /* not protected */
344 addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
345 DEBUGF("Erasing sector %p\n", addr2);
346
347 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
348 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
349 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
350 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
351 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
352 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
353 addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
354 for (i=0; i<50; i++)
355 udelay(1000); /* wait 1 ms */
356 } else {
357 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
358 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
359 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
360 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
361 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
362 addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
363 }
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200364 /*
365 * Wait for each sector to complete, it's more
366 * reliable. According to AMD Spec, you must
367 * issue all erase commands within a specified
368 * timeout. This has been seen to fail, especially
369 * if printf()s are included (for debug)!!
370 */
371 wait_for_DQ7(info, sect);
372 }
373 }
374
375 /* re-enable interrupts if necessary */
376 if (flag)
377 enable_interrupts();
378
379 /* wait at least 80us - let's wait 1 ms */
380 udelay (1000);
381
382 /* reset to read mode */
383 addr = (FLASH_WORD_SIZE *)info->start[0];
384 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
385
386 printf (" done\n");
387 return 0;
388}
389
390/*-----------------------------------------------------------------------
391 * Copy memory to flash, returns:
392 * 0 - OK
393 * 1 - write timeout
394 * 2 - Flash not erased
395 */
396
397int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
398{
399 ulong cp, wp, data;
400 int i, l, rc;
401
402 wp = (addr & ~3); /* get lower word aligned address */
403
404 /*
405 * handle unaligned start bytes
406 */
407 if ((l = addr - wp) != 0) {
408 data = 0;
409 for (i=0, cp=wp; i<l; ++i, ++cp) {
410 data = (data << 8) | (*(uchar *)cp);
411 }
412 for (; i<4 && cnt>0; ++i) {
413 data = (data << 8) | *src++;
414 --cnt;
415 ++cp;
416 }
417 for (; cnt==0 && i<4; ++i, ++cp) {
418 data = (data << 8) | (*(uchar *)cp);
419 }
420
421 if ((rc = write_word(info, wp, data)) != 0) {
422 return (rc);
423 }
424 wp += 4;
425 }
426
427 /*
428 * handle word aligned part
429 */
430 while (cnt >= 4) {
431 data = 0;
432 for (i=0; i<4; ++i) {
433 data = (data << 8) | *src++;
434 }
435 if ((rc = write_word(info, wp, data)) != 0) {
436 return (rc);
437 }
438 wp += 4;
439 cnt -= 4;
440 }
441
442 if (cnt == 0) {
443 return (0);
444 }
445
446 /*
447 * handle unaligned tail bytes
448 */
449 data = 0;
450 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
451 data = (data << 8) | *src++;
452 --cnt;
453 }
454 for (; i<4; ++i, ++cp) {
455 data = (data << 8) | (*(uchar *)cp);
456 }
457
458 return (write_word(info, wp, data));
459}
460
461/*-----------------------------------------------------------------------
462 * Write a word to Flash, returns:
463 * 0 - OK
464 * 1 - write timeout
465 * 2 - Flash not erased
466 */
467static int write_word (flash_info_t * info, ulong dest, ulong data)
468{
469 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
470 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
471 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
472 ulong start;
473 int i;
474
475 /* Check if Flash is (sufficiently) erased */
476 if ((*((volatile FLASH_WORD_SIZE *) dest) &
477 (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
478 return (2);
479 }
480
481 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
482 int flag;
483
484 /* Disable interrupts which might cause a timeout here */
485 flag = disable_interrupts ();
486
487 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
488 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
489 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
490
491 dest2[i] = data2[i];
492
493 /* re-enable interrupts if necessary */
494 if (flag)
495 enable_interrupts ();
496
497 /* data polling for D7 */
498 start = get_timer (0);
499 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
500 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
501
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200502 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200503 return (1);
504 }
505 }
506 }
507
508 return (0);
509}