3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Yoo. Jonghoon, IPone, yooth@ipone.co.kr
10 * U-Boot port on RPXlite board
12 * Some of flash control words are modified. (from 2x16bit device
14 * RPXLite board I tested has only 4 AM29LV800BB devices. Other devices
17 * (?) Does an RPXLite board which
18 * does not use AM29LV800 flash memory exist ?
25 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
27 /*-----------------------------------------------------------------------
30 static ulong flash_get_size ( short manu, short dev_id, flash_info_t *info);
31 static int write_word (flash_info_t *info, ulong dest, ulong data);
32 static void flash_get_offsets (ulong base, flash_info_t *info, int two_chips);
33 static void flash_get_id_word( void *ptr, short *ptr_manuf, short *ptr_dev_id);
34 static void flash_get_id_long( void *ptr, short *ptr_manuf, short *ptr_dev_id);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init (void)
41 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size_b0, size_b1;
47 /* Init: no FLASHes known */
48 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
49 flash_info[i].flash_id = FLASH_UNKNOWN;
52 /* Do sizing to get full correct info */
54 flash_get_id_word((void*)CONFIG_SYS_FLASH_BASE0,&manu,&dev_id);
56 size_b0 = flash_get_size(manu, dev_id, &flash_info[0]);
58 flash_get_offsets (CONFIG_SYS_FLASH_BASE0, &flash_info[0],0);
60 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (0 - size_b0);
62 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE0
63 /* monitor protection ON by default */
64 flash_protect(FLAG_PROTECT_SET,
65 CONFIG_SYS_MONITOR_BASE,
66 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
70 flash_get_id_long((void*)CONFIG_SYS_FLASH_BASE1,&manu,&dev_id);
72 size_b1 = 2 * flash_get_size(manu, dev_id, &flash_info[1]);
74 flash_get_offsets(CONFIG_SYS_FLASH_BASE1, &flash_info[1],1);
76 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (0 - size_b1);
78 flash_info[0].size = size_b0;
79 flash_info[1].size = size_b1;
81 return (size_b0+size_b1);
84 /*-----------------------------------------------------------------------
86 static void flash_get_offsets (ulong base, flash_info_t *info, int two_chips)
89 vu_short *addr = (vu_short*)base;
91 addr[0x555] = 0x00AA ;
92 addr[0xAAA] = 0x0055 ;
93 addr[0x555] = 0x0090 ;
95 addr_shift = (two_chips ? 2 : 1 );
97 /* set up sector start address table */
98 if (info->flash_id & FLASH_BTYPE) {
99 /* set sector offsets for bottom boot block type */
100 info->start[0] = base + (0x00000000<<addr_shift);
101 info->start[1] = base + (0x00002000<<addr_shift);
102 info->start[2] = base + (0x00003000<<addr_shift);
103 info->start[3] = base + (0x00004000<<addr_shift);
104 for (i = 4; i < info->sector_count; i++) {
105 info->start[i] = base + ((i-3) * (0x00008000<<addr_shift)) ;
108 /* set sector offsets for top boot block type */
109 i = info->sector_count - 1;
110 info->start[i--] = base + info->size - (0x00002000<<addr_shift);
111 info->start[i--] = base + info->size - (0x00003000<<addr_shift);
112 info->start[i--] = base + info->size - (0x00004000<<addr_shift);
113 for (; i >= 0; i--) {
114 info->start[i] = base + i * (0x00008000<<addr_shift);
118 /* check for protected sectors */
119 for (i = 0; i < info->sector_count; i++) {
120 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
121 /* D0 = 1 if protected */
122 addr = (vu_short *)(info->start[i]);
123 info->protect[i] = addr[1<<addr_shift] & 1 ;
126 addr = (vu_short *)info->start[0];
127 *addr = 0xF0F0; /* reset bank */
130 /*-----------------------------------------------------------------------
132 void flash_print_info (flash_info_t *info)
136 if (info->flash_id == FLASH_UNKNOWN) {
137 printf ("missing or unknown FLASH type\n");
141 switch (info->flash_id & FLASH_VENDMASK) {
142 case FLASH_MAN_AMD: printf ("AMD "); break;
143 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
144 case FLASH_MAN_TOSH: printf ("TOSHIBA "); break;
145 default: printf ("Unknown Vendor "); break;
148 switch (info->flash_id & FLASH_TYPEMASK) {
149 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
151 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
153 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
155 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
157 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
159 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
161 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
163 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
165 default: printf ("Unknown Chip Type\n");
169 printf (" Size: %ld MB in %d Sectors\n",
170 info->size >> 20, info->sector_count);
172 printf (" Sector Start Addresses:");
173 for (i=0; i<info->sector_count; ++i) {
178 info->protect[i] ? " (RO)" : " "
184 /*-----------------------------------------------------------------------
188 /*-----------------------------------------------------------------------
192 * The following code cannot be run from FLASH!
195 static void flash_get_id_word( void *ptr, short *ptr_manuf, short *ptr_dev_id)
197 vu_short *addr = (vu_short*)ptr;
199 addr[0x555] = 0x00AA ;
200 addr[0xAAA] = 0x0055 ;
201 addr[0x555] = 0x0090 ;
203 *ptr_manuf = addr[0];
204 *ptr_dev_id = addr[1];
206 addr[0] = 0xf0f0; /* return to normal */
209 static void flash_get_id_long( void *ptr, short *ptr_manuf, short *ptr_dev_id)
211 vu_short *addr = (vu_short*)ptr;
212 vu_short *addr1, *addr2, *addr3;
214 addr1 = (vu_short*) ( ((int)ptr) + (0x5555<<2) );
215 addr2 = (vu_short*) ( ((int)ptr) + (0x2AAA<<2) );
216 addr3 = (vu_short*) ( ((int)ptr) + (0x5555<<2) );
222 *ptr_manuf = addr[0];
223 *ptr_dev_id = addr[2];
225 addr[0] = 0xf0f0; /* return to normal */
228 static ulong flash_get_size ( short manu, short dev_id, flash_info_t *info)
231 case ((short)AMD_MANUFACT):
232 info->flash_id = FLASH_MAN_AMD;
234 case ((short)FUJ_MANUFACT):
235 info->flash_id = FLASH_MAN_FUJ;
237 case ((short)TOSH_MANUFACT):
238 info->flash_id = FLASH_MAN_TOSH;
241 info->flash_id = FLASH_UNKNOWN;
242 info->sector_count = 0;
244 return (0); /* no or unknown flash */
249 case ((short)TOSH_ID_FVT160):
250 info->flash_id += FLASH_AM160T;
251 info->sector_count = 35;
252 info->size = 0x00200000;
255 case ((short)TOSH_ID_FVB160):
256 info->flash_id += FLASH_AM160B;
257 info->sector_count = 35;
258 info->size = 0x00200000;
261 case ((short)AMD_ID_LV400T):
262 info->flash_id += FLASH_AM400T;
263 info->sector_count = 11;
264 info->size = 0x00100000;
267 case ((short)AMD_ID_LV400B):
268 info->flash_id += FLASH_AM400B;
269 info->sector_count = 11;
270 info->size = 0x00100000;
273 case ((short)AMD_ID_LV800T):
274 info->flash_id += FLASH_AM800T;
275 info->sector_count = 19;
276 info->size = 0x00200000;
279 case ((short)AMD_ID_LV800B):
280 info->flash_id += FLASH_AM800B;
281 info->sector_count = 19;
282 info->size = 0x00400000; /*%%% Size doubled by yooth */
285 case ((short)AMD_ID_LV160T):
286 info->flash_id += FLASH_AM160T;
287 info->sector_count = 35;
288 info->size = 0x00200000;
291 case ((short)AMD_ID_LV160B):
292 info->flash_id += FLASH_AM160B;
293 info->sector_count = 35;
294 info->size = 0x00200000;
297 info->flash_id = FLASH_UNKNOWN;
298 return (0); /* => no or unknown flash */
306 /*-----------------------------------------------------------------------
309 int flash_erase (flash_info_t *info, int s_first, int s_last)
311 vu_short *addr = (vu_short*)(info->start[0]);
312 int flag, prot, sect, l_sect;
313 ulong start, now, last;
315 if ((s_first < 0) || (s_first > s_last)) {
316 if (info->flash_id == FLASH_UNKNOWN) {
317 printf ("- missing\n");
319 printf ("- no sectors to erase\n");
324 if ((info->flash_id == FLASH_UNKNOWN) ||
325 (info->flash_id > FLASH_AMD_COMP)) {
326 printf ("Can't erase unknown flash type %08lx - aborted\n",
332 for (sect=s_first; sect<=s_last; ++sect) {
333 if (info->protect[sect]) {
339 printf ("- Warning: %d protected sectors will not be erased!\n",
347 /* Disable interrupts which might cause a timeout here */
348 flag = disable_interrupts();
350 addr[0x555] = (vu_short)0xAAAAAAAA;
351 addr[0xAAA] = (vu_short)0x55555555;
352 addr[0x555] = (vu_short)0x80808080;
353 addr[0x555] = (vu_short)0xAAAAAAAA;
354 addr[0xAAA] = (vu_short)0x55555555;
356 /* Start erase on unprotected sectors */
357 for (sect = s_first; sect<=s_last; sect++) {
358 if (info->protect[sect] == 0) { /* not protected */
359 addr = (vu_short *)(info->start[sect]) ;
360 addr[0] = (vu_short)0x30303030 ;
365 /* re-enable interrupts if necessary */
369 /* wait at least 80us - let's wait 1 ms */
373 * We wait for the last triggered sector
378 start = get_timer (0);
380 addr = (vu_short *)(info->start[l_sect]);
381 while ((addr[0] & 0x8080) != 0x8080) {
382 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
383 printf ("Timeout\n");
386 /* show that we're waiting */
387 if ((now - last) > 1000) { /* every second */
394 /* reset to read mode */
395 addr = (vu_short *)info->start[0];
396 addr[0] = (vu_short)0xF0F0F0F0; /* reset bank */
402 /*-----------------------------------------------------------------------
403 * Copy memory to flash, returns:
406 * 2 - Flash not erased
409 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
414 wp = (addr & ~3); /* get lower word aligned address */
417 * handle unaligned start bytes
419 if ((l = addr - wp) != 0) {
421 for (i=0, cp=wp; i<l; ++i, ++cp) {
422 data = (data << 8) | (*(uchar *)cp);
424 for (; i<4 && cnt>0; ++i) {
425 data = (data << 8) | *src++;
429 for (; cnt==0 && i<4; ++i, ++cp) {
430 data = (data << 8) | (*(uchar *)cp);
433 if ((rc = write_word(info, wp, data)) != 0) {
440 * handle word aligned part
444 for (i=0; i<4; ++i) {
445 data = (data << 8) | *src++;
447 if ((rc = write_word(info, wp, data)) != 0) {
459 * handle unaligned tail bytes
462 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
463 data = (data << 8) | *src++;
466 for (; i<4; ++i, ++cp) {
467 data = (data << 8) | (*(uchar *)cp);
470 return (write_word(info, wp, data));
473 /*-----------------------------------------------------------------------
474 * Write a word to Flash, returns:
477 * 2 - Flash not erased
479 static int write_word (flash_info_t *info, ulong dest, ulong data)
481 vu_short *addr = (vu_short *)(info->start[0]);
487 /* Check if Flash is (sufficiently) erased */
488 if ((*((vu_long *)dest) & data) != data) {
492 /* First write upper 16 bits */
493 sdata = (short)(data>>16);
495 /* Disable interrupts which might cause a timeout here */
496 flag = disable_interrupts();
498 addr[0x555] = 0xAAAA;
499 addr[0xAAA] = 0x5555;
500 addr[0x555] = 0xA0A0;
502 *((vu_short *)dest) = sdata;
504 /* re-enable interrupts if necessary */
508 /* data polling for D7 */
509 start = get_timer (0);
510 while ((*((vu_short *)dest) & 0x8080) != (sdata & 0x8080)) {
511 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
516 /* Now write lower 16 bits */
517 sdata = (short)(data&0xffff);
519 /* Disable interrupts which might cause a timeout here */
520 flag = disable_interrupts();
522 addr[0x555] = 0xAAAA;
523 addr[0xAAA] = 0x5555;
524 addr[0x555] = 0xA0A0;
526 *((vu_short *)dest + 1) = sdata;
528 /* re-enable interrupts if necessary */
532 /* data polling for D7 */
533 start = get_timer (0);
534 while ((*((vu_short *)dest + 1) & 0x8080) != (sdata & 0x8080)) {
535 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
542 /*-----------------------------------------------------------------------