3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 * Yoo. Jonghoon, IPone, yooth@ipone.co.kr
26 * U-Boot port on RPXlite board
28 * Some of flash control words are modified. (from 2x16bit device
30 * RPXLite board I tested has only 4 AM29LV800BB devices. Other devices
33 * (?) Does an RPXLite board which
34 * does not use AM29LV800 flash memory exist ?
41 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
43 /*-----------------------------------------------------------------------
46 static ulong flash_get_size ( short manu, short dev_id, flash_info_t *info);
47 static int write_word (flash_info_t *info, ulong dest, ulong data);
48 static void flash_get_offsets (ulong base, flash_info_t *info, int two_chips);
49 static void flash_get_id_word( void *ptr, short *ptr_manuf, short *ptr_dev_id);
50 static void flash_get_id_long( void *ptr, short *ptr_manuf, short *ptr_dev_id);
52 /*-----------------------------------------------------------------------
55 unsigned long flash_init (void)
57 volatile immap_t *immap = (immap_t *)CFG_IMMR;
58 volatile memctl8xx_t *memctl = &immap->im_memctl;
59 unsigned long size_b0, size_b1;
63 /* Init: no FLASHes known */
64 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
65 flash_info[i].flash_id = FLASH_UNKNOWN;
68 /* Do sizing to get full correct info */
70 flash_get_id_word((void*)CFG_FLASH_BASE0,&manu,&dev_id);
72 size_b0 = flash_get_size(manu, dev_id, &flash_info[0]);
74 flash_get_offsets (CFG_FLASH_BASE0, &flash_info[0],0);
76 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (0 - size_b0);
78 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE0
79 /* monitor protection ON by default */
80 flash_protect(FLAG_PROTECT_SET,
82 CFG_MONITOR_BASE+monitor_flash_len-1,
86 flash_get_id_long((void*)CFG_FLASH_BASE1,&manu,&dev_id);
88 size_b1 = 2 * flash_get_size(manu, dev_id, &flash_info[1]);
90 flash_get_offsets(CFG_FLASH_BASE1, &flash_info[1],1);
92 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (0 - size_b1);
94 flash_info[0].size = size_b0;
95 flash_info[1].size = size_b1;
97 return (size_b0+size_b1);
100 /*-----------------------------------------------------------------------
102 static void flash_get_offsets (ulong base, flash_info_t *info, int two_chips)
105 vu_short *addr = (vu_short*)base;
107 addr[0x555] = 0x00AA ;
108 addr[0xAAA] = 0x0055 ;
109 addr[0x555] = 0x0090 ;
111 addr_shift = (two_chips ? 2 : 1 );
113 /* set up sector start address table */
114 if (info->flash_id & FLASH_BTYPE) {
115 /* set sector offsets for bottom boot block type */
116 info->start[0] = base + (0x00000000<<addr_shift);
117 info->start[1] = base + (0x00002000<<addr_shift);
118 info->start[2] = base + (0x00003000<<addr_shift);
119 info->start[3] = base + (0x00004000<<addr_shift);
120 for (i = 4; i < info->sector_count; i++) {
121 info->start[i] = base + ((i-3) * (0x00008000<<addr_shift)) ;
124 /* set sector offsets for top boot block type */
125 i = info->sector_count - 1;
126 info->start[i--] = base + info->size - (0x00002000<<addr_shift);
127 info->start[i--] = base + info->size - (0x00003000<<addr_shift);
128 info->start[i--] = base + info->size - (0x00004000<<addr_shift);
129 for (; i >= 0; i--) {
130 info->start[i] = base + i * (0x00008000<<addr_shift);
134 /* check for protected sectors */
135 for (i = 0; i < info->sector_count; i++) {
136 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
137 /* D0 = 1 if protected */
138 addr = (vu_short *)(info->start[i]);
139 info->protect[i] = addr[1<<addr_shift] & 1 ;
142 addr = (vu_short *)info->start[0];
143 *addr = 0xF0F0; /* reset bank */
146 /*-----------------------------------------------------------------------
148 void flash_print_info (flash_info_t *info)
152 if (info->flash_id == FLASH_UNKNOWN) {
153 printf ("missing or unknown FLASH type\n");
157 switch (info->flash_id & FLASH_VENDMASK) {
158 case FLASH_MAN_AMD: printf ("AMD "); break;
159 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
160 case FLASH_MAN_TOSH: printf ("TOSHIBA "); break;
161 default: printf ("Unknown Vendor "); break;
164 switch (info->flash_id & FLASH_TYPEMASK) {
165 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
167 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
169 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
171 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
173 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
175 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
177 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
179 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
181 default: printf ("Unknown Chip Type\n");
185 printf (" Size: %ld MB in %d Sectors\n",
186 info->size >> 20, info->sector_count);
188 printf (" Sector Start Addresses:");
189 for (i=0; i<info->sector_count; ++i) {
194 info->protect[i] ? " (RO)" : " "
200 /*-----------------------------------------------------------------------
204 /*-----------------------------------------------------------------------
208 * The following code cannot be run from FLASH!
211 static void flash_get_id_word( void *ptr, short *ptr_manuf, short *ptr_dev_id)
213 vu_short *addr = (vu_short*)ptr;
215 addr[0x555] = 0x00AA ;
216 addr[0xAAA] = 0x0055 ;
217 addr[0x555] = 0x0090 ;
219 *ptr_manuf = addr[0];
220 *ptr_dev_id = addr[1];
222 addr[0] = 0xf0f0; /* return to normal */
225 static void flash_get_id_long( void *ptr, short *ptr_manuf, short *ptr_dev_id)
227 vu_short *addr = (vu_short*)ptr;
228 vu_short *addr1, *addr2, *addr3;
230 addr1 = (vu_short*) ( ((int)ptr) + (0x5555<<2) );
231 addr2 = (vu_short*) ( ((int)ptr) + (0x2AAA<<2) );
232 addr3 = (vu_short*) ( ((int)ptr) + (0x5555<<2) );
238 *ptr_manuf = addr[0];
239 *ptr_dev_id = addr[2];
241 addr[0] = 0xf0f0; /* return to normal */
244 static ulong flash_get_size ( short manu, short dev_id, flash_info_t *info)
247 case ((short)AMD_MANUFACT):
248 info->flash_id = FLASH_MAN_AMD;
250 case ((short)FUJ_MANUFACT):
251 info->flash_id = FLASH_MAN_FUJ;
253 case ((short)TOSH_MANUFACT):
254 info->flash_id = FLASH_MAN_TOSH;
257 info->flash_id = FLASH_UNKNOWN;
258 info->sector_count = 0;
260 return (0); /* no or unknown flash */
265 case ((short)TOSH_ID_FVT160):
266 info->flash_id += FLASH_AM160T;
267 info->sector_count = 35;
268 info->size = 0x00200000;
271 case ((short)TOSH_ID_FVB160):
272 info->flash_id += FLASH_AM160B;
273 info->sector_count = 35;
274 info->size = 0x00200000;
277 case ((short)AMD_ID_LV400T):
278 info->flash_id += FLASH_AM400T;
279 info->sector_count = 11;
280 info->size = 0x00100000;
283 case ((short)AMD_ID_LV400B):
284 info->flash_id += FLASH_AM400B;
285 info->sector_count = 11;
286 info->size = 0x00100000;
289 case ((short)AMD_ID_LV800T):
290 info->flash_id += FLASH_AM800T;
291 info->sector_count = 19;
292 info->size = 0x00200000;
295 case ((short)AMD_ID_LV800B):
296 info->flash_id += FLASH_AM800B;
297 info->sector_count = 19;
298 info->size = 0x00400000; /*%%% Size doubled by yooth */
301 case ((short)AMD_ID_LV160T):
302 info->flash_id += FLASH_AM160T;
303 info->sector_count = 35;
304 info->size = 0x00200000;
307 case ((short)AMD_ID_LV160B):
308 info->flash_id += FLASH_AM160B;
309 info->sector_count = 35;
310 info->size = 0x00200000;
313 info->flash_id = FLASH_UNKNOWN;
314 return (0); /* => no or unknown flash */
322 /*-----------------------------------------------------------------------
325 int flash_erase (flash_info_t *info, int s_first, int s_last)
327 vu_short *addr = (vu_short*)(info->start[0]);
328 int flag, prot, sect, l_sect;
329 ulong start, now, last;
331 if ((s_first < 0) || (s_first > s_last)) {
332 if (info->flash_id == FLASH_UNKNOWN) {
333 printf ("- missing\n");
335 printf ("- no sectors to erase\n");
340 if ((info->flash_id == FLASH_UNKNOWN) ||
341 (info->flash_id > FLASH_AMD_COMP)) {
342 printf ("Can't erase unknown flash type %08lx - aborted\n",
348 for (sect=s_first; sect<=s_last; ++sect) {
349 if (info->protect[sect]) {
355 printf ("- Warning: %d protected sectors will not be erased!\n",
363 /* Disable interrupts which might cause a timeout here */
364 flag = disable_interrupts();
366 addr[0x555] = (vu_short)0xAAAAAAAA;
367 addr[0xAAA] = (vu_short)0x55555555;
368 addr[0x555] = (vu_short)0x80808080;
369 addr[0x555] = (vu_short)0xAAAAAAAA;
370 addr[0xAAA] = (vu_short)0x55555555;
372 /* Start erase on unprotected sectors */
373 for (sect = s_first; sect<=s_last; sect++) {
374 if (info->protect[sect] == 0) { /* not protected */
375 addr = (vu_short *)(info->start[sect]) ;
376 addr[0] = (vu_short)0x30303030 ;
381 /* re-enable interrupts if necessary */
385 /* wait at least 80us - let's wait 1 ms */
389 * We wait for the last triggered sector
394 start = get_timer (0);
396 addr = (vu_short *)(info->start[l_sect]);
397 while ((addr[0] & 0x8080) != 0x8080) {
398 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
399 printf ("Timeout\n");
402 /* show that we're waiting */
403 if ((now - last) > 1000) { /* every second */
410 /* reset to read mode */
411 addr = (vu_short *)info->start[0];
412 addr[0] = (vu_short)0xF0F0F0F0; /* reset bank */
418 /*-----------------------------------------------------------------------
419 * Copy memory to flash, returns:
422 * 2 - Flash not erased
425 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
430 wp = (addr & ~3); /* get lower word aligned address */
433 * handle unaligned start bytes
435 if ((l = addr - wp) != 0) {
437 for (i=0, cp=wp; i<l; ++i, ++cp) {
438 data = (data << 8) | (*(uchar *)cp);
440 for (; i<4 && cnt>0; ++i) {
441 data = (data << 8) | *src++;
445 for (; cnt==0 && i<4; ++i, ++cp) {
446 data = (data << 8) | (*(uchar *)cp);
449 if ((rc = write_word(info, wp, data)) != 0) {
456 * handle word aligned part
460 for (i=0; i<4; ++i) {
461 data = (data << 8) | *src++;
463 if ((rc = write_word(info, wp, data)) != 0) {
475 * handle unaligned tail bytes
478 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
479 data = (data << 8) | *src++;
482 for (; i<4; ++i, ++cp) {
483 data = (data << 8) | (*(uchar *)cp);
486 return (write_word(info, wp, data));
489 /*-----------------------------------------------------------------------
490 * Write a word to Flash, returns:
493 * 2 - Flash not erased
495 static int write_word (flash_info_t *info, ulong dest, ulong data)
497 vu_short *addr = (vu_short *)(info->start[0]);
503 /* Check if Flash is (sufficiently) erased */
504 if ((*((vu_long *)dest) & data) != data) {
508 /* First write upper 16 bits */
509 sdata = (short)(data>>16);
511 /* Disable interrupts which might cause a timeout here */
512 flag = disable_interrupts();
514 addr[0x555] = 0xAAAA;
515 addr[0xAAA] = 0x5555;
516 addr[0x555] = 0xA0A0;
518 *((vu_short *)dest) = sdata;
520 /* re-enable interrupts if necessary */
524 /* data polling for D7 */
525 start = get_timer (0);
526 while ((*((vu_short *)dest) & 0x8080) != (sdata & 0x8080)) {
527 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
532 /* Now write lower 16 bits */
533 sdata = (short)(data&0xffff);
535 /* Disable interrupts which might cause a timeout here */
536 flag = disable_interrupts();
538 addr[0x555] = 0xAAAA;
539 addr[0xAAA] = 0x5555;
540 addr[0x555] = 0xA0A0;
542 *((vu_short *)dest + 1) = sdata;
544 /* re-enable interrupts if necessary */
548 /* data polling for D7 */
549 start = get_timer (0);
550 while ((*((vu_short *)dest + 1) & 0x8080) != (sdata & 0x8080)) {
551 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
558 /*-----------------------------------------------------------------------