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 (vu_long *addr, 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);
34 /*-----------------------------------------------------------------------
37 unsigned long flash_init (void)
39 /* volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR; */
40 /* volatile memctl8xx_t *memctl = &immap->im_memctl; */
41 unsigned long size_b0 ;
44 /* Init: no FLASHes known */
45 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
46 flash_info[i].flash_id = FLASH_UNKNOWN;
49 /* Static FLASH Bank configuration here - FIXME XXX */
51 size_b0 = flash_get_size((vu_long *)FLASH_BASE_DEBUG, &flash_info[0]);
53 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
54 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
55 size_b0, size_b0<<20);
58 /* Remap FLASH according to real size */
60 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
61 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
63 /* Re-do sizing to get full correct info */
65 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
66 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
68 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
69 /* monitor protection ON by default */
70 flash_protect(FLAG_PROTECT_SET,
71 CONFIG_SYS_MONITOR_BASE,
72 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
76 flash_info[0].size = size_b0;
81 /*-----------------------------------------------------------------------
83 static void flash_get_offsets (ulong base, flash_info_t *info)
87 /* set up sector start address table */
88 if (info->flash_id & FLASH_BTYPE) {
89 /* set sector offsets for bottom boot block type */
90 info->start[0] = base + 0x00000000;
91 info->start[1] = base + 0x00010000;
92 info->start[2] = base + 0x00018000;
93 info->start[3] = base + 0x00020000;
94 for (i = 4; i < info->sector_count; i++) {
95 info->start[i] = base + ((i-3) * 0x00040000) ;
98 /* set sector offsets for top boot block type */
99 i = info->sector_count - 1;
100 info->start[i--] = base + info->size - 0x00010000;
101 info->start[i--] = base + info->size - 0x00018000;
102 info->start[i--] = base + info->size - 0x00020000;
103 for (; i >= 0; i--) {
104 info->start[i] = base + i * 0x00040000;
110 /*-----------------------------------------------------------------------
112 void flash_print_info (flash_info_t *info)
116 if (info->flash_id == FLASH_UNKNOWN) {
117 printf ("missing or unknown FLASH type\n");
121 switch (info->flash_id & FLASH_VENDMASK) {
122 case FLASH_MAN_AMD: printf ("AMD "); break;
123 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
124 default: printf ("Unknown Vendor "); break;
127 switch (info->flash_id & FLASH_TYPEMASK) {
128 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
130 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
132 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
134 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
136 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
138 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
140 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
142 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
144 default: printf ("Unknown Chip Type\n");
148 printf (" Size: %ld MB in %d Sectors\n",
149 info->size >> 20, info->sector_count);
151 printf (" Sector Start Addresses:");
152 for (i=0; i<info->sector_count; ++i) {
157 info->protect[i] ? " (RO)" : " "
164 /*-----------------------------------------------------------------------
168 /*-----------------------------------------------------------------------
172 * The following code cannot be run from FLASH!
175 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
179 ulong base = (ulong)addr;
181 /* Write auto select command: read Manufacturer ID */
182 addr[0xAAA] = 0x00AA00AA ;
183 addr[0x555] = 0x00550055 ;
184 addr[0xAAA] = 0x00900090 ;
188 switch (value & 0x00FF00FF) {
190 info->flash_id = FLASH_MAN_AMD;
193 info->flash_id = FLASH_MAN_FUJ;
196 info->flash_id = FLASH_UNKNOWN;
197 info->sector_count = 0;
199 return (0); /* no or unknown flash */
202 value = addr[2] ; /* device ID */
204 switch (value & 0x00FF00FF) {
205 case (AMD_ID_LV400T & 0x00FF00FF):
206 info->flash_id += FLASH_AM400T;
207 info->sector_count = 11;
208 info->size = 0x00100000;
211 case (AMD_ID_LV400B & 0x00FF00FF):
212 info->flash_id += FLASH_AM400B;
213 info->sector_count = 11;
214 info->size = 0x00100000;
217 case (AMD_ID_LV800T & 0x00FF00FF):
218 info->flash_id += FLASH_AM800T;
219 info->sector_count = 19;
220 info->size = 0x00200000;
223 case (AMD_ID_LV800B & 0x00FF00FF):
224 info->flash_id += FLASH_AM800B;
225 info->sector_count = 19;
226 info->size = 0x00400000; /*%%% Size doubled by yooth */
229 case (AMD_ID_LV160T & 0x00FF00FF):
230 info->flash_id += FLASH_AM160T;
231 info->sector_count = 35;
232 info->size = 0x00400000;
235 case (AMD_ID_LV160B & 0x00FF00FF):
236 info->flash_id += FLASH_AM160B;
237 info->sector_count = 35;
238 info->size = 0x00400000;
240 #if 0 /* enable when device IDs are available */
242 info->flash_id += FLASH_AM320T;
243 info->sector_count = 67;
244 info->size = 0x00800000;
248 info->flash_id += FLASH_AM320B;
249 info->sector_count = 67;
250 info->size = 0x00800000;
254 info->flash_id = FLASH_UNKNOWN;
255 return (0); /* => no or unknown flash */
258 /*%%% sector start address modified */
259 /* set up sector start address table */
260 if (info->flash_id & FLASH_BTYPE) {
261 /* set sector offsets for bottom boot block type */
262 info->start[0] = base + 0x00000000;
263 info->start[1] = base + 0x00010000;
264 info->start[2] = base + 0x00018000;
265 info->start[3] = base + 0x00020000;
266 for (i = 4; i < info->sector_count; i++) {
267 info->start[i] = base + ((i-3) * 0x00040000) ;
270 /* set sector offsets for top boot block type */
271 i = info->sector_count - 1;
272 info->start[i--] = base + info->size - 0x00010000;
273 info->start[i--] = base + info->size - 0x00018000;
274 info->start[i--] = base + info->size - 0x00020000;
275 for (; i >= 0; i--) {
276 info->start[i] = base + i * 0x00040000;
280 /* check for protected sectors */
281 for (i = 0; i < info->sector_count; i++) {
282 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
283 /* D0 = 1 if protected */
284 addr = (volatile unsigned long *)(info->start[i]);
285 info->protect[i] = addr[4] & 1 ;
289 * Prevent writes to uninitialized FLASH.
291 if (info->flash_id != FLASH_UNKNOWN) {
292 addr = (volatile unsigned long *)info->start[0];
294 *addr = 0xF0F0F0F0; /* reset bank */
301 /*-----------------------------------------------------------------------
304 int flash_erase (flash_info_t *info, int s_first, int s_last)
306 vu_long *addr = (vu_long*)(info->start[0]);
307 int flag, prot, sect, l_sect;
308 ulong start, now, last;
310 if ((s_first < 0) || (s_first > s_last)) {
311 if (info->flash_id == FLASH_UNKNOWN) {
312 printf ("- missing\n");
314 printf ("- no sectors to erase\n");
319 if ((info->flash_id == FLASH_UNKNOWN) ||
320 (info->flash_id > FLASH_AMD_COMP)) {
321 printf ("Can't erase unknown flash type %08lx - aborted\n",
327 for (sect=s_first; sect<=s_last; ++sect) {
328 if (info->protect[sect]) {
334 printf ("- Warning: %d protected sectors will not be erased!\n",
342 /* Disable interrupts which might cause a timeout here */
343 flag = disable_interrupts();
345 addr[0xAAA] = 0xAAAAAAAA;
346 addr[0x555] = 0x55555555;
347 addr[0xAAA] = 0x80808080;
348 addr[0xAAA] = 0xAAAAAAAA;
349 addr[0x555] = 0x55555555;
351 /* Start erase on unprotected sectors */
352 for (sect = s_first; sect<=s_last; sect++) {
353 if (info->protect[sect] == 0) { /* not protected */
354 addr = (vu_long *)(info->start[sect]) ;
355 addr[0] = 0x30303030 ;
360 /* re-enable interrupts if necessary */
364 /* wait at least 80us - let's wait 1 ms */
368 * We wait for the last triggered sector
373 start = get_timer (0);
375 addr = (vu_long *)(info->start[l_sect]);
376 while ((addr[0] & 0x80808080) != 0x80808080) {
377 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
378 printf ("Timeout\n");
381 /* show that we're waiting */
382 if ((now - last) > 1000) { /* every second */
389 /* reset to read mode */
390 addr = (vu_long *)info->start[0];
391 addr[0] = 0xF0F0F0F0; /* reset bank */
397 /*-----------------------------------------------------------------------
398 * Copy memory to flash, returns:
401 * 2 - Flash not erased
404 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
409 wp = (addr & ~3); /* get lower word aligned address */
412 * handle unaligned start bytes
414 if ((l = addr - wp) != 0) {
416 for (i=0, cp=wp; i<l; ++i, ++cp) {
417 data = (data << 8) | (*(uchar *)cp);
419 for (; i<4 && cnt>0; ++i) {
420 data = (data << 8) | *src++;
424 for (; cnt==0 && i<4; ++i, ++cp) {
425 data = (data << 8) | (*(uchar *)cp);
428 if ((rc = write_word(info, wp, data)) != 0) {
435 * handle word aligned part
439 for (i=0; i<4; ++i) {
440 data = (data << 8) | *src++;
442 if ((rc = write_word(info, wp, data)) != 0) {
454 * handle unaligned tail bytes
457 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
458 data = (data << 8) | *src++;
461 for (; i<4; ++i, ++cp) {
462 data = (data << 8) | (*(uchar *)cp);
465 return (write_word(info, wp, data));
468 /*-----------------------------------------------------------------------
469 * Write a word to Flash, returns:
472 * 2 - Flash not erased
474 static int write_word (flash_info_t *info, ulong dest, ulong data)
476 vu_long *addr = (vu_long *)(info->start[0]);
480 /* Check if Flash is (sufficiently) erased */
481 if ((*((vu_long *)dest) & data) != data) {
484 /* Disable interrupts which might cause a timeout here */
485 flag = disable_interrupts();
487 addr[0xAAA] = 0xAAAAAAAA;
488 addr[0x555] = 0x55555555;
489 addr[0xAAA] = 0xA0A0A0A0;
491 *((vu_long *)dest) = data;
493 /* re-enable interrupts if necessary */
497 /* data polling for D7 */
498 start = get_timer (0);
499 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
500 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
507 /*-----------------------------------------------------------------------