3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 #if defined(CONFIG_ENV_IS_IN_FLASH)
14 # ifndef CONFIG_ENV_ADDR
15 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
17 # ifndef CONFIG_ENV_SIZE
18 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
20 # ifndef CONFIG_ENV_SECT_SIZE
21 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
25 #define QUAD_ID(id) ((((ulong)(id) & 0xFF) << 24) | \
26 (((ulong)(id) & 0xFF) << 16) | \
27 (((ulong)(id) & 0xFF) << 8) | \
28 (((ulong)(id) & 0xFF) << 0) \
31 /*-----------------------------------------------------------------------
34 static ulong flash_get_size (vu_long * addr, flash_info_t * info);
35 static int write_word (flash_info_t * info, ulong dest, ulong data);
37 /*-----------------------------------------------------------------------
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 vu_long *bcsr = (vu_long *)BCSR_ADDR;
44 unsigned long pd_size, total_size, bsize, or_am;
47 /* Init: no FLASHes known */
48 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
49 flash_info[i].flash_id = FLASH_UNKNOWN;
50 flash_info[i].size = 0;
51 flash_info[i].sector_count = 0;
52 flash_info[i].start[0] = 0xFFFFFFFF; /* For TFTP */
55 switch ((bcsr[2] & BCSR2_FLASH_PD_MASK) >> BCSR2_FLASH_PD_SHIFT) {
77 printf("## Unsupported flash detected by BCSR: 0x%08lX\n", bcsr[2]);
81 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS && total_size < pd_size; ++i) {
82 bsize = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE + total_size),
85 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
86 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
87 i, bsize, bsize >> 20);
93 if (total_size != pd_size) {
94 printf("## Detected flash size %lu conflicts with PD data %lu\n",
98 /* Remap FLASH according to real size */
99 memctl->memc_or0 = or_am | CONFIG_SYS_OR_TIMING_FLASH;
101 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS && flash_info[i].size != 0; ++i) {
102 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
103 /* monitor protection ON by default */
104 if (CONFIG_SYS_MONITOR_BASE >= flash_info[i].start[0])
105 flash_protect (FLAG_PROTECT_SET,
106 CONFIG_SYS_MONITOR_BASE,
107 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
111 #ifdef CONFIG_ENV_IS_IN_FLASH
112 /* ENV protection ON by default */
113 if (CONFIG_ENV_ADDR >= flash_info[i].start[0])
114 flash_protect (FLAG_PROTECT_SET,
116 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
124 /*-----------------------------------------------------------------------
126 void flash_print_info (flash_info_t * info)
130 if (info->flash_id == FLASH_UNKNOWN) {
131 printf ("missing or unknown FLASH type\n");
135 switch (info->flash_id & FLASH_VENDMASK) {
143 printf ("BRIGHT MICRO ");
146 printf ("Unknown Vendor ");
150 switch (info->flash_id & FLASH_TYPEMASK) {
152 printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
155 printf ("29F080 or 29LV080 (8 Mbit, uniform sectors)\n");
158 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
161 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
164 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
167 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
170 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
173 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
176 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
179 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
182 printf ("Unknown Chip Type\n");
186 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20,
189 printf (" Sector Start Addresses:");
191 for (i = 0; i < info->sector_count; ++i) {
197 info->start[i], info->protect[i] ? " (RO)" : " ");
203 /*-----------------------------------------------------------------------
204 * The following code can not run from flash!
206 static ulong flash_get_size (vu_long * addr, flash_info_t * info)
210 /* Write auto select command: read Manufacturer ID */
211 addr[0x0555] = 0xAAAAAAAA;
212 addr[0x02AA] = 0x55555555;
213 addr[0x0555] = 0x90909090;
216 case QUAD_ID(AMD_MANUFACT):
217 info->flash_id = FLASH_MAN_AMD;
220 case QUAD_ID(FUJ_MANUFACT):
221 info->flash_id = FLASH_MAN_FUJ;
225 info->flash_id = FLASH_UNKNOWN;
226 info->sector_count = 0;
231 switch (addr[1]) { /* device ID */
232 case QUAD_ID(AMD_ID_F040B):
233 case QUAD_ID(AMD_ID_LV040B):
234 info->flash_id += FLASH_AM040;
235 info->sector_count = 8;
236 info->size = 0x00200000;
239 case QUAD_ID(AMD_ID_F080B):
240 info->flash_id += FLASH_AM080;
241 info->sector_count = 16;
242 info->size = 0x00400000;
246 info->flash_id += FLASH_AM400T;
247 info->sector_count = 11;
248 info->size = 0x00100000;
252 info->flash_id += FLASH_AM400B;
253 info->sector_count = 11;
254 info->size = 0x00100000;
258 info->flash_id += FLASH_AM800T;
259 info->sector_count = 19;
260 info->size = 0x00200000;
264 info->flash_id += FLASH_AM800B;
265 info->sector_count = 19;
266 info->size = 0x00200000;
270 info->flash_id += FLASH_AM160T;
271 info->sector_count = 35;
272 info->size = 0x00400000;
276 info->flash_id += FLASH_AM160B;
277 info->sector_count = 35;
278 info->size = 0x00400000;
282 info->flash_id += FLASH_AM320T;
283 info->sector_count = 67;
284 info->size = 0x00800000;
288 info->flash_id += FLASH_AM320B;
289 info->sector_count = 67;
290 info->size = 0x00800000;
294 info->flash_id = FLASH_UNKNOWN;
295 return (0); /* => no or unknown flash */
299 /* set up sector start address table */
300 if (info->flash_id & FLASH_BTYPE) {
301 /* set sector offsets for bottom boot block type */
302 info->start[0] = base + 0x00000000;
303 info->start[1] = base + 0x00008000;
304 info->start[2] = base + 0x0000C000;
305 info->start[3] = base + 0x00010000;
306 for (i = 4; i < info->sector_count; i++) {
307 info->start[i] = base + (i * 0x00020000) - 0x00060000;
310 /* set sector offsets for top boot block type */
311 i = info->sector_count - 1;
312 info->start[i--] = base + info->size - 0x00008000;
313 info->start[i--] = base + info->size - 0x0000C000;
314 info->start[i--] = base + info->size - 0x00010000;
315 for (; i >= 0; i--) {
316 info->start[i] = base + i * 0x00020000;
320 /* set sector offsets for uniform sector type */
321 for (i = 0; i < info->sector_count; i++)
322 info->start[i] = (ulong)addr + (i * 0x00040000);
325 /* check for protected sectors */
326 for (i = 0; i < info->sector_count; i++) {
327 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
328 /* D0 = 1 if protected */
329 addr = (volatile unsigned long *) (info->start[i]);
330 info->protect[i] = addr[2] & 1;
333 if (info->flash_id != FLASH_UNKNOWN) {
334 addr = (volatile unsigned long *) info->start[0];
335 *addr = 0xF0F0F0F0; /* reset bank */
341 /*-----------------------------------------------------------------------
343 int flash_erase (flash_info_t * info, int s_first, int s_last)
345 vu_long *addr = (vu_long *) (info->start[0]);
346 int flag, prot, sect, l_sect;
347 ulong start, now, last;
349 if ((s_first < 0) || (s_first > s_last)) {
350 if (info->flash_id == FLASH_UNKNOWN) {
351 printf ("- missing\n");
353 printf ("- no sectors to erase\n");
358 if ((info->flash_id == FLASH_UNKNOWN) ||
359 (info->flash_id > FLASH_AMD_COMP)) {
360 printf ("Can't erase unknown flash type - aborted\n");
361 return ERR_UNKNOWN_FLASH_TYPE;
365 for (sect = s_first; sect <= s_last; ++sect) {
366 if (info->protect[sect]) {
372 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
379 /* Disable interrupts which might cause a timeout here */
380 flag = disable_interrupts ();
382 addr[0x0555] = 0xAAAAAAAA;
383 addr[0x02AA] = 0x55555555;
384 addr[0x0555] = 0x80808080;
385 addr[0x0555] = 0xAAAAAAAA;
386 addr[0x02AA] = 0x55555555;
388 /* Start erase on unprotected sectors */
389 for (sect = s_first; sect <= s_last; sect++) {
390 if (info->protect[sect] == 0) { /* not protected */
391 addr = (vu_long *) (info->start[sect]);
392 addr[0] = 0x30303030;
397 /* re-enable interrupts if necessary */
399 enable_interrupts ();
401 /* wait at least 80us - let's wait 1 ms */
405 * We wait for the last triggered sector
410 start = get_timer (0);
412 addr = (vu_long *) (info->start[l_sect]);
413 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
415 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
416 printf ("Timeout\n");
419 /* show that we're waiting */
420 if ((now - last) > 1000) { /* every second */
427 /* reset to read mode */
428 addr = (volatile unsigned long *) info->start[0];
429 addr[0] = 0xF0F0F0F0; /* reset bank */
436 /*-----------------------------------------------------------------------
437 * Copy memory to flash, returns:
440 * 2 - Flash not erased
442 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
447 wp = (addr & ~3); /* get lower word aligned address */
450 * handle unaligned start bytes
452 if ((l = addr - wp) != 0) {
454 for (i = 0, cp = wp; i < l; ++i, ++cp) {
455 data = (data << 8) | (*(uchar *) cp);
457 for (; i < 4 && cnt > 0; ++i) {
458 data = (data << 8) | *src++;
462 for (; cnt == 0 && i < 4; ++i, ++cp) {
463 data = (data << 8) | (*(uchar *) cp);
466 if ((rc = write_word (info, wp, data)) != 0) {
473 * handle word aligned part
477 for (i = 0; i < 4; ++i) {
478 data = (data << 8) | *src++;
480 if ((rc = write_word (info, wp, data)) != 0) {
492 * handle unaligned tail bytes
495 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
496 data = (data << 8) | *src++;
499 for (; i < 4; ++i, ++cp) {
500 data = (data << 8) | (*(uchar *) cp);
503 return (write_word (info, wp, data));
506 /*-----------------------------------------------------------------------
507 * Write a word to Flash, returns:
510 * 2 - Flash not erased
512 static int write_word (flash_info_t * info, ulong dest, ulong data)
514 vu_long *addr = (vu_long *) (info->start[0]);
518 /* Check if Flash is (sufficiently) erased */
519 if ((*((vu_long *) dest) & data) != data) {
520 return ERR_NOT_ERASED;
522 /* Disable interrupts which might cause a timeout here */
523 flag = disable_interrupts ();
525 addr[0x0555] = 0xAAAAAAAA;
526 addr[0x02AA] = 0x55555555;
527 addr[0x0555] = 0xA0A0A0A0;
529 *((vu_long *) dest) = data;
531 /* re-enable interrupts if necessary */
533 enable_interrupts ();
535 /* data polling for D7 */
536 start = get_timer (0);
537 while ((*((vu_long *) dest) & 0x80808080) != (data & 0x80808080))
539 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {