blob: 4c8922f35f5c69a5203ab0c67487db2a7ab93034 [file] [log] [blame]
Marian Balakowiczcfae4892006-10-03 20:28:38 +02001/*
2 * (C) Copyright 2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <mpc5xxx.h>
26#include <asm/processor.h>
27
Marian Balakowicz4b7e4562007-01-10 00:26:15 +010028#if defined(CONFIG_CAM5200) && defined(CONFIG_CAM5200_NIOSFLASH)
Marian Balakowiczcfae4892006-10-03 20:28:38 +020029
30#if 0
31#define DEBUGF(x...) printf(x)
32#else
33#define DEBUGF(x...)
34#endif
35
36#define swap16(x) __swab16(x)
37
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020038flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
Marian Balakowiczcfae4892006-10-03 20:28:38 +020039
40/*
41 * CAM5200 is a TQM5200B based board. Additionally it also features
42 * a NIOS cpu. The NIOS CPU peripherals are accessible through MPC5xxx
43 * Local Bus on CS5. This includes 32 bit wide RAM and SRAM as well as
44 * 16 bit wide flash device. Big Endian order on a 32 bit CS5 makes
45 * access to flash chip slightly more complicated as additional byte
46 * swapping is necessary within each 16 bit wide flash 'word'.
47 *
48 * This driver's task is to handle both flash devices: 32 bit TQM5200B
Wolfgang Denk4df0da52006-10-09 00:42:01 +020049 * flash chip and 16 bit NIOS cpu flash chip. In the below
Marian Balakowiczcfae4892006-10-03 20:28:38 +020050 * flash_addr_table table we use least significant address bit to mark
51 * 16 bit flash bank and two sets of routines *_32 and *_16 to handle
52 * specifics of both flashes.
53 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054static unsigned long flash_addr_table[][CONFIG_SYS_MAX_FLASH_BANKS] = {
55 {CONFIG_SYS_BOOTCS_START, CONFIG_SYS_CS5_START | 1}
Marian Balakowiczcfae4892006-10-03 20:28:38 +020056};
57
58/*-----------------------------------------------------------------------
59 * Functions
60 */
61static int write_word(flash_info_t * info, ulong dest, ulong data);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020062#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Marian Balakowiczcfae4892006-10-03 20:28:38 +020063static int write_word_32(flash_info_t * info, ulong dest, ulong data);
64static int write_word_16(flash_info_t * info, ulong dest, ulong data);
65static int flash_erase_32(flash_info_t * info, int s_first, int s_last);
66static int flash_erase_16(flash_info_t * info, int s_first, int s_last);
67static ulong flash_get_size_32(vu_long * addr, flash_info_t * info);
68static ulong flash_get_size_16(vu_long * addr, flash_info_t * info);
69#endif
70
71void flash_print_info(flash_info_t * info)
72{
73 int i, k;
74 int size, erased;
75 volatile unsigned long *flash;
76
77 if (info->flash_id == FLASH_UNKNOWN) {
78 printf("missing or unknown FLASH type\n");
79 return;
80 }
81
82 switch (info->flash_id & FLASH_VENDMASK) {
83 case FLASH_MAN_AMD:
84 printf("AMD ");
85 break;
86 case FLASH_MAN_FUJ:
87 printf("FUJITSU ");
88 break;
89 default:
90 printf("Unknown Vendor ");
91 break;
92 }
93
94 switch (info->flash_id & FLASH_TYPEMASK) {
95 case FLASH_S29GL128N:
96 printf ("S29GL128N (256 Mbit, uniform sector size)\n");
97 break;
98 case FLASH_AM320B:
99 printf ("29LV320B (32 Mbit, bottom boot sect)\n");
100 break;
101 case FLASH_AM320T:
102 printf ("29LV320T (32 Mbit, top boot sect)\n");
103 break;
104 default:
105 printf("Unknown Chip Type\n");
106 break;
107 }
108
109 printf(" Size: %ld KB in %d Sectors\n",
110 info->size >> 10, info->sector_count);
111
112 printf(" Sector Start Addresses:");
113 for (i = 0; i < info->sector_count; ++i) {
114 /*
115 * Check if whole sector is erased
116 */
117 if (i != (info->sector_count - 1))
118 size = info->start[i + 1] - info->start[i];
119 else
120 size = info->start[0] + info->size - info->start[i];
121
122 erased = 1;
123 flash = (volatile unsigned long *)info->start[i];
124 size = size >> 2; /* divide by 4 for longword access */
125
126 for (k = 0; k < size; k++) {
127 if (*flash++ != 0xffffffff) {
128 erased = 0;
129 break;
130 }
131 }
132
133 if ((i % 5) == 0)
134 printf("\n ");
135
136 printf(" %08lX%s%s", info->start[i],
137 erased ? " E" : " ",
138 info->protect[i] ? "RO " : " ");
139 }
140 printf("\n");
141 return;
142}
143
144
145/*
146 * The following code cannot be run from FLASH!
147 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200148#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200149static ulong flash_get_size(vu_long * addr, flash_info_t * info)
150{
151
152 DEBUGF("get_size: FLASH ADDR %08lx\n", addr);
153
154 /* bit 0 used for big flash marking */
155 if ((ulong)addr & 0x1)
156 return flash_get_size_16((vu_long *)((ulong)addr & 0xfffffffe), info);
157 else
158 return flash_get_size_32(addr, info);
159}
160
161static ulong flash_get_size_32(vu_long * addr, flash_info_t * info)
162#else
163static ulong flash_get_size(vu_long * addr, flash_info_t * info)
164#endif
165{
166 short i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200167 CONFIG_SYS_FLASH_WORD_SIZE value;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200168 ulong base = (ulong) addr;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200169 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200170
171 DEBUGF("get_size32: FLASH ADDR: %08x\n", (unsigned)addr);
172
173 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200174 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
175 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
176 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200177 udelay(1000);
178
179 value = addr2[0];
180 DEBUGF("FLASH MANUFACT: %x\n", value);
181
182 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200183 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200184 info->flash_id = FLASH_MAN_AMD;
185 break;
186 default:
187 info->flash_id = FLASH_UNKNOWN;
188 info->sector_count = 0;
189 info->size = 0;
190 return (0); /* no or unknown flash */
191 }
192
193 value = addr2[1]; /* device ID */
194 DEBUGF("\nFLASH DEVICEID: %x\n", value);
195
196 switch (value) {
197 case AMD_ID_MIRROR:
198 DEBUGF("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
199 addr[14], addr[15]);
200 switch(addr[14]) {
201 case AMD_ID_GL128N_2:
202 if (addr[15] != AMD_ID_GL128N_3) {
203 DEBUGF("Chip: S29GL128N -> unknown\n");
204 info->flash_id = FLASH_UNKNOWN;
205 } else {
206 DEBUGF("Chip: S29GL128N\n");
207 info->flash_id += FLASH_S29GL128N;
208 info->sector_count = 128;
209 info->size = 0x02000000;
210 }
211 break;
212 default:
213 info->flash_id = FLASH_UNKNOWN;
214 return(0);
215 }
216 break;
217
218 default:
219 info->flash_id = FLASH_UNKNOWN;
220 return (0); /* => no or unknown flash */
221 }
222
223 /* set up sector start address table */
224 for (i = 0; i < info->sector_count; i++)
225 info->start[i] = base + (i * 0x00040000);
226
227 /* check for protected sectors */
228 for (i = 0; i < info->sector_count; i++) {
229 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
230 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200231 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200232
233 info->protect[i] = addr2[2] & 1;
234 }
235
236 /* issue bank reset to return to read mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200237 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200238
239 return (info->size);
240}
241
242static int wait_for_DQ7_32(flash_info_t * info, int sect)
243{
244 ulong start, now, last;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200245 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
246 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200247
248 start = get_timer(0);
249 last = start;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200250 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
251 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
252 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200253 printf("Timeout\n");
254 return -1;
255 }
256 /* show that we're waiting */
257 if ((now - last) > 1000) { /* every second */
258 putc('.');
259 last = now;
260 }
261 }
262 return 0;
263}
264
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200265#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200266int flash_erase(flash_info_t * info, int s_first, int s_last)
267{
268 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) {
269 return flash_erase_16(info, s_first, s_last);
270 } else {
271 return flash_erase_32(info, s_first, s_last);
272 }
273}
274
275static int flash_erase_32(flash_info_t * info, int s_first, int s_last)
276#else
277int flash_erase(flash_info_t * info, int s_first, int s_last)
278#endif
279{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200280 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
281 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200282 int flag, prot, sect, l_sect;
283
284 if ((s_first < 0) || (s_first > s_last)) {
285 if (info->flash_id == FLASH_UNKNOWN)
286 printf("- missing\n");
287 else
288 printf("- no sectors to erase\n");
289 return 1;
290 }
291
292 if (info->flash_id == FLASH_UNKNOWN) {
293 printf("Can't erase unknown flash type - aborted\n");
294 return 1;
295 }
296
297 prot = 0;
298 for (sect = s_first; sect <= s_last; ++sect) {
299 if (info->protect[sect])
300 prot++;
301 }
302
303 if (prot)
304 printf("- Warning: %d protected sectors will not be erased!", prot);
305
306 printf("\n");
307
308 l_sect = -1;
309
310 /* Disable interrupts which might cause a timeout here */
311 flag = disable_interrupts();
312
313 /* Start erase on unprotected sectors */
314 for (sect = s_first; sect <= s_last; sect++) {
315 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200316 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200317
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200318 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
319 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
320 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
321 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
322 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
323 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200324
325 l_sect = sect;
326 /*
327 * Wait for each sector to complete, it's more
328 * reliable. According to AMD Spec, you must
329 * issue all erase commands within a specified
330 * timeout. This has been seen to fail, especially
331 * if printf()s are included (for debug)!!
332 */
333 wait_for_DQ7_32(info, sect);
334 }
335 }
336
337 /* re-enable interrupts if necessary */
338 if (flag)
339 enable_interrupts();
340
341 /* wait at least 80us - let's wait 1 ms */
342 udelay(1000);
343
344 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200345 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
346 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200347
348 printf(" done\n");
349 return 0;
350}
351
352/*-----------------------------------------------------------------------
353 * Copy memory to flash, returns:
354 * 0 - OK
355 * 1 - write timeout
356 * 2 - Flash not erased
357 */
358int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
359{
360 ulong cp, wp, data;
361 int i, l, rc;
362
363 wp = (addr & ~3); /* get lower word aligned address */
364
365 /*
366 * handle unaligned start bytes
367 */
368 if ((l = addr - wp) != 0) {
369 data = 0;
370 for (i = 0, cp = wp; i < l; ++i, ++cp)
371 data = (data << 8) | (*(uchar *) cp);
372
373 for (; i < 4 && cnt > 0; ++i) {
374 data = (data << 8) | *src++;
375 --cnt;
376 ++cp;
377 }
378
379 for (; cnt == 0 && i < 4; ++i, ++cp)
380 data = (data << 8) | (*(uchar *) cp);
381
382 if ((rc = write_word(info, wp, data)) != 0)
383 return (rc);
384
385 wp += 4;
386 }
387
388 /*
389 * handle word aligned part
390 */
391 while (cnt >= 4) {
392 data = 0;
393 for (i = 0; i < 4; ++i)
394 data = (data << 8) | *src++;
395
396 if ((rc = write_word(info, wp, data)) != 0)
397 return (rc);
398
399 wp += 4;
400 cnt -= 4;
401 }
402
403 if (cnt == 0)
404 return (0);
405
406 /*
407 * handle unaligned tail bytes
408 */
409 data = 0;
410 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
411 data = (data << 8) | *src++;
412 --cnt;
413 }
414 for (; i < 4; ++i, ++cp)
415 data = (data << 8) | (*(uchar *) cp);
416
417 return (write_word(info, wp, data));
418}
419
420/*-----------------------------------------------------------------------
421 * Copy memory to flash, returns:
422 * 0 - OK
423 * 1 - write timeout
424 * 2 - Flash not erased
425 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200426#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200427static int write_word(flash_info_t * info, ulong dest, ulong data)
428{
429 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) {
430 return write_word_16(info, dest, data);
431 } else {
432 return write_word_32(info, dest, data);
433 }
434}
435
436static int write_word_32(flash_info_t * info, ulong dest, ulong data)
437#else
438static int write_word(flash_info_t * info, ulong dest, ulong data)
439#endif
440{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200441 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
442 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
Wolfgang Denk3d00e232010-11-24 22:27:08 +0100443 ulong *datap = &data;
444 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 =
445 (volatile CONFIG_SYS_FLASH_WORD_SIZE *)datap;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200446 ulong start;
447 int i, flag;
448
449 /* Check if Flash is (sufficiently) erased */
450 if ((*((vu_long *)dest) & data) != data)
451 return (2);
452
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200453 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200454 /* Disable interrupts which might cause a timeout here */
455 flag = disable_interrupts();
456
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200457 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
458 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
459 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200460
461 dest2[i] = data2[i];
462
463 /* re-enable interrupts if necessary */
464 if (flag)
465 enable_interrupts();
466
467 /* data polling for D7 */
468 start = get_timer(0);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200469 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
470 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200471
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200472 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200473 return (1);
474 }
475 }
476
477 return (0);
478}
479
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200480#ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200481
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200482#undef CONFIG_SYS_FLASH_WORD_SIZE
483#define CONFIG_SYS_FLASH_WORD_SIZE unsigned short
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200484
485/*
486 * The following code cannot be run from FLASH!
487 */
488static ulong flash_get_size_16(vu_long * addr, flash_info_t * info)
489{
490 short i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200491 CONFIG_SYS_FLASH_WORD_SIZE value;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200492 ulong base = (ulong) addr;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200493 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200494
495 DEBUGF("get_size16: FLASH ADDR: %08x\n", (unsigned)addr);
496
497 /* issue bank reset to return to read mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200498 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF000F000;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200499
500 /* Write auto select command: read Manufacturer ID */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200501 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAA00AA00;
502 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55005500;
503 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90009000;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200504 udelay(1000);
505
506 value = swap16(addr2[0]);
507 DEBUGF("FLASH MANUFACT: %x\n", value);
508
509 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200510 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200511 info->flash_id = FLASH_MAN_AMD;
512 break;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200513 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200514 info->flash_id = FLASH_MAN_FUJ;
515 break;
516 default:
517 info->flash_id = FLASH_UNKNOWN;
518 info->sector_count = 0;
519 info->size = 0;
520 return (0); /* no or unknown flash */
521 }
522
523 value = swap16(addr2[1]); /* device ID */
524 DEBUGF("\nFLASH DEVICEID: %x\n", value);
525
526 switch (value) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200527 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200528 info->flash_id += FLASH_AM320B;
529 info->sector_count = 71;
530 info->size = 0x00400000;
531 break; /* => 4 MB */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200532 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200533 info->flash_id += FLASH_AM320T;
534 info->sector_count = 71;
535 info->size = 0x00400000;
536 break; /* => 4 MB */
537 default:
538 info->flash_id = FLASH_UNKNOWN;
539 return (0); /* => no or unknown flash */
540 }
541
542 if (info->flash_id & FLASH_BTYPE) {
543 /* set sector offsets for bottom boot block type */
544 info->start[0] = base + 0x00000000;
545 info->start[1] = base + 0x00002000;
546 info->start[2] = base + 0x00004000;
547 info->start[3] = base + 0x00006000;
548 info->start[4] = base + 0x00008000;
549 info->start[5] = base + 0x0000a000;
550 info->start[6] = base + 0x0000c000;
551 info->start[7] = base + 0x0000e000;
552
553 for (i = 8; i < info->sector_count; i++)
554 info->start[i] = base + (i * 0x00010000) - 0x00070000;
555 } else {
556 /* set sector offsets for top boot block type */
557 i = info->sector_count - 1;
558 info->start[i--] = base + info->size - 0x00002000;
559 info->start[i--] = base + info->size - 0x00004000;
560 info->start[i--] = base + info->size - 0x00006000;
561 info->start[i--] = base + info->size - 0x00008000;
562 info->start[i--] = base + info->size - 0x0000a000;
563 info->start[i--] = base + info->size - 0x0000c000;
564 info->start[i--] = base + info->size - 0x0000e000;
565
566 for (; i >= 0; i--)
567 info->start[i] = base + i * 0x00010000;
568 }
569
570 /* check for protected sectors */
571 for (i = 0; i < info->sector_count; i++) {
572 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
573 /* D0 = 1 if protected */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200574 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200575
576 info->protect[i] = addr2[2] & 1;
577 }
578
579 /* issue bank reset to return to read mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200580 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF000F000;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200581
582 return (info->size);
583}
584
585static int wait_for_DQ7_16(flash_info_t * info, int sect)
586{
587 ulong start, now, last;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200588 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
589 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200590
591 start = get_timer(0);
592 last = start;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200593 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80008000) !=
594 (CONFIG_SYS_FLASH_WORD_SIZE) 0x80008000) {
595 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200596 printf("Timeout\n");
597 return -1;
598 }
599 /* show that we're waiting */
600 if ((now - last) > 1000) { /* every second */
601 putc('.');
602 last = now;
603 }
604 }
605 return 0;
606}
607
608static int flash_erase_16(flash_info_t * info, int s_first, int s_last)
609{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200610 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
611 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200612 int flag, prot, sect, l_sect;
613
614 if ((s_first < 0) || (s_first > s_last)) {
615 if (info->flash_id == FLASH_UNKNOWN)
616 printf("- missing\n");
617 else
618 printf("- no sectors to erase\n");
619 return 1;
620 }
621
622 if (info->flash_id == FLASH_UNKNOWN) {
623 printf("Can't erase unknown flash type - aborted\n");
624 return 1;
625 }
626
627 prot = 0;
628 for (sect = s_first; sect <= s_last; ++sect) {
629 if (info->protect[sect])
630 prot++;
631 }
632
633 if (prot)
634 printf("- Warning: %d protected sectors will not be erased!", prot);
635
636 printf("\n");
637
638 l_sect = -1;
639
640 /* Disable interrupts which might cause a timeout here */
641 flag = disable_interrupts();
642
643 /* Start erase on unprotected sectors */
644 for (sect = s_first; sect <= s_last; sect++) {
645 if (info->protect[sect] == 0) { /* not protected */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200646 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200647
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200648 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAA00AA00;
649 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55005500;
650 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x80008000;
651 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAA00AA00;
652 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55005500;
653 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x30003000; /* sector erase */
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200654
655 l_sect = sect;
656 /*
657 * Wait for each sector to complete, it's more
658 * reliable. According to AMD Spec, you must
659 * issue all erase commands within a specified
660 * timeout. This has been seen to fail, especially
661 * if printf()s are included (for debug)!!
662 */
663 wait_for_DQ7_16(info, sect);
664 }
665 }
666
667 /* re-enable interrupts if necessary */
668 if (flag)
669 enable_interrupts();
670
671 /* wait at least 80us - let's wait 1 ms */
672 udelay(1000);
673
674 /* reset to read mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200675 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
676 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF000F000; /* reset bank */
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200677
678 printf(" done\n");
679 return 0;
680}
681
682static int write_word_16(flash_info_t * info, ulong dest, ulong data)
683{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200684 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
685 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
Wolfgang Denk3d00e232010-11-24 22:27:08 +0100686 ulong *datap = &data;
687 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 =
688 (volatile CONFIG_SYS_FLASH_WORD_SIZE *)datap;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200689 ulong start;
690 int i;
691
692 /* Check if Flash is (sufficiently) erased */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200693 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200694 if ((dest2[i] & swap16(data2[i])) != swap16(data2[i]))
695 return (2);
696 }
697
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200698 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200699 int flag;
700
701 /* Disable interrupts which might cause a timeout here */
702 flag = disable_interrupts();
703
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200704 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAA00AA00;
705 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55005500;
706 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xA000A000;
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200707
708 dest2[i] = swap16(data2[i]);
709
710 /* re-enable interrupts if necessary */
711 if (flag)
712 enable_interrupts();
713
714 /* data polling for D7 */
715 start = get_timer(0);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200716 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80008000) !=
717 (swap16(data2[i]) & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80008000)) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200718
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200719 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200720 return (1);
721 }
722 }
723 }
724
725 return (0);
726}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200727#endif /* CONFIG_SYS_FLASH_2ND_16BIT_DEV */
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200728
729/*-----------------------------------------------------------------------
730 * Functions
731 */
732static ulong flash_get_size(vu_long * addr, flash_info_t * info);
733static int write_word(flash_info_t * info, ulong dest, ulong data);
734
735/*-----------------------------------------------------------------------
736 */
737
738unsigned long flash_init(void)
739{
740 unsigned long total_b = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200741 unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200742 unsigned short index = 0;
743 int i;
744
745 DEBUGF("\n");
746 DEBUGF("FLASH: Index: %d\n", index);
747
748 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200749 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200750 flash_info[i].flash_id = FLASH_UNKNOWN;
751 flash_info[i].sector_count = -1;
752 flash_info[i].size = 0;
753
754 /* check whether the address is 0 */
755 if (flash_addr_table[index][i] == 0)
756 continue;
757
758 /* call flash_get_size() to initialize sector address */
759 size_b[i] = flash_get_size((vu_long *) flash_addr_table[index][i],
760 &flash_info[i]);
761
762 flash_info[i].size = size_b[i];
763
764 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
765 printf("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
Wolfgang Denk0129dcd2006-11-23 22:58:58 +0100766 i+1, size_b[i], size_b[i] << 20);
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200767 flash_info[i].sector_count = -1;
768 flash_info[i].size = 0;
769 }
770
771 /* Monitor protection ON by default */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200772 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_MONITOR_BASE,
773 CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200774 &flash_info[i]);
Jean-Christophe PLAGNIOL-VILLARD53db4cd2008-09-10 22:48:04 +0200775#if defined(CONFIG_ENV_IS_IN_FLASH)
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200776 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
777 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200778 &flash_info[i]);
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200779#if defined(CONFIG_ENV_ADDR_REDUND)
780 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
781 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
Marian Balakowiczcfae4892006-10-03 20:28:38 +0200782 &flash_info[i]);
783#endif
784#endif
785 total_b += flash_info[i].size;
786 }
787
788 return total_b;
789}
Marian Balakowicz4b7e4562007-01-10 00:26:15 +0100790#endif /* if defined(CONFIG_CAM5200) && defined(CONFIG_CAM5200_NIOSFLASH) */