3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
5 * (C) Copyright 2001-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/byteorder/swab.h>
15 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
17 /* Board support for 1 or 2 flash devices */
18 #define FLASH_PORT_WIDTH32
19 #undef FLASH_PORT_WIDTH16
21 #ifdef FLASH_PORT_WIDTH16
22 #define FLASH_PORT_WIDTH ushort
23 #define FLASH_PORT_WIDTHV vu_short
26 #define FLASH_PORT_WIDTH ulong
27 #define FLASH_PORT_WIDTHV vu_long
31 /* Intel-compatible flash ID */
32 #define INTEL_COMPAT 0x00890089
33 #define INTEL_ALT 0x00B000B0
35 /* Intel-compatible flash commands */
36 #define INTEL_PROGRAM 0x00100010
37 #define INTEL_ERASE 0x00200020
38 #define INTEL_CLEAR 0x00500050
39 #define INTEL_LOCKBIT 0x00600060
40 #define INTEL_PROTECT 0x00010001
41 #define INTEL_STATUS 0x00700070
42 #define INTEL_READID 0x00900090
43 #define INTEL_CONFIRM 0x00D000D0
44 #define INTEL_RESET 0xFFFFFFFF
46 /* Intel-compatible flash status bits */
47 #define INTEL_FINISHED 0x00800080
48 #define INTEL_OK 0x00800080
50 #define FPW FLASH_PORT_WIDTH
51 #define FPWV FLASH_PORT_WIDTHV
53 #define mb() __asm__ __volatile__ ("" : : : "memory")
55 /*-----------------------------------------------------------------------
58 static ulong
flash_get_size (FPW
*addr
, flash_info_t
*info
);
59 static int write_data (flash_info_t
*info
, ulong dest
, FPW data
);
60 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
61 void inline spin_wheel (void);
62 static void flash_sync_real_protect (flash_info_t
* info
);
63 static unsigned char intel_sector_protected (flash_info_t
*info
, ushort sector
);
65 /*-----------------------------------------------------------------------
68 unsigned long flash_init (void)
72 extern void flash_preinit(void);
73 extern void flash_afterinit(ulong
, ulong
);
74 ulong flashbase
= CONFIG_SYS_FLASH_BASE
;
78 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
81 memset(&flash_info
[i
], 0, sizeof(flash_info_t
));
82 flash_get_size ((FPW
*) flashbase
, &flash_info
[i
]);
83 flash_get_offsets (flash_info
[i
].start
[0], &flash_info
[i
]);
86 panic ("configured to many flash banks!\n");
89 size
+= flash_info
[i
].size
;
91 /* get the h/w and s/w protection status in sync */
92 flash_sync_real_protect(&flash_info
[i
]);
95 /* Protect monitor and environment sectors
97 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
98 #ifndef CONFIG_BOOT_ROM
99 flash_protect ( FLAG_PROTECT_SET
,
100 CONFIG_SYS_MONITOR_BASE
,
101 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
106 #ifdef CONFIG_ENV_IS_IN_FLASH
107 flash_protect ( FLAG_PROTECT_SET
,
109 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1, &flash_info
[0] );
112 flash_afterinit(flash_info
[0].start
[0], flash_info
[0].size
);
117 /*-----------------------------------------------------------------------
119 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
123 if (info
->flash_id
== FLASH_UNKNOWN
) {
127 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
128 for (i
= 0; i
< info
->sector_count
; i
++) {
129 info
->start
[i
] = base
+ (i
* PHYS_FLASH_SECT_SIZE
);
134 /*-----------------------------------------------------------------------
136 void flash_print_info (flash_info_t
*info
)
140 if (info
->flash_id
== FLASH_UNKNOWN
) {
141 printf ("missing or unknown FLASH type\n");
145 switch (info
->flash_id
& FLASH_VENDMASK
) {
146 case FLASH_MAN_INTEL
:
150 printf ("Unknown Vendor ");
154 switch (info
->flash_id
& FLASH_TYPEMASK
) {
155 case FLASH_28F256J3A
:
156 printf ("28F256J3A\n");
159 case FLASH_28F128J3A
:
160 printf ("28F128J3A\n");
163 case FLASH_28F640J3A
:
164 printf ("28F640J3A\n");
167 case FLASH_28F320J3A
:
168 printf ("28F320J3A\n");
172 printf ("Unknown Chip Type\n");
176 printf (" Size: %ld MB in %d Sectors\n",
177 info
->size
>> 20, info
->sector_count
);
179 printf (" Sector Start Addresses:");
180 for (i
= 0; i
< info
->sector_count
; ++i
) {
185 info
->protect
[i
] ? " (RO)" : " ");
192 * The following code cannot be run from FLASH!
194 static ulong
flash_get_size (FPW
*addr
, flash_info_t
*info
)
198 /* Write auto select command: read Manufacturer ID */
199 addr
[0x5555] = (FPW
) 0x00AA00AA;
200 addr
[0x2AAA] = (FPW
) 0x00550055;
201 addr
[0x5555] = (FPW
) 0x00900090;
210 case (FPW
) INTEL_MANUFACT
:
211 info
->flash_id
= FLASH_MAN_INTEL
;
215 info
->flash_id
= FLASH_UNKNOWN
;
216 info
->sector_count
= 0;
218 addr
[0] = (FPW
) 0x00FF00FF; /* restore read mode */
219 return (0); /* no or unknown flash */
223 value
= addr
[1]; /* device ID */
227 case (FPW
) INTEL_ID_28F256J3A
:
228 info
->flash_id
+= FLASH_28F256J3A
;
229 /* In U-Boot we support only 32 MB (no bank-switching) */
230 info
->sector_count
= 256 / 2;
231 info
->size
= 0x04000000 / 2;
232 info
->start
[0] = CONFIG_SYS_FLASH_BASE
+ 0x02000000;
233 break; /* => 32 MB */
235 case (FPW
) INTEL_ID_28F128J3A
:
236 info
->flash_id
+= FLASH_28F128J3A
;
237 info
->sector_count
= 128;
238 info
->size
= 0x02000000;
239 info
->start
[0] = CONFIG_SYS_FLASH_BASE
+ 0x02000000;
240 break; /* => 32 MB */
242 case (FPW
) INTEL_ID_28F640J3A
:
243 info
->flash_id
+= FLASH_28F640J3A
;
244 info
->sector_count
= 64;
245 info
->size
= 0x01000000;
246 info
->start
[0] = CONFIG_SYS_FLASH_BASE
+ 0x03000000;
247 break; /* => 16 MB */
249 case (FPW
) INTEL_ID_28F320J3A
:
250 info
->flash_id
+= FLASH_28F320J3A
;
251 info
->sector_count
= 32;
252 info
->size
= 0x800000;
253 info
->start
[0] = CONFIG_SYS_FLASH_BASE
+ 0x03800000;
257 info
->flash_id
= FLASH_UNKNOWN
;
261 if (info
->sector_count
> CONFIG_SYS_MAX_FLASH_SECT
) {
262 printf ("** ERROR: sector count %d > max (%d) **\n",
263 info
->sector_count
, CONFIG_SYS_MAX_FLASH_SECT
);
264 info
->sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
267 addr
[0] = (FPW
) 0x00FF00FF; /* restore read mode */
274 * This function gets the u-boot flash sector protection status
275 * (flash_info_t.protect[]) in sync with the sector protection
276 * status stored in hardware.
278 static void flash_sync_real_protect (flash_info_t
* info
)
282 switch (info
->flash_id
& FLASH_TYPEMASK
) {
284 case FLASH_28F256J3A
:
285 case FLASH_28F128J3A
:
286 case FLASH_28F640J3A
:
287 case FLASH_28F320J3A
:
288 for (i
= 0; i
< info
->sector_count
; ++i
) {
289 info
->protect
[i
] = intel_sector_protected(info
, i
);
293 /* no h/w protect support */
300 * checks if "sector" in bank "info" is protected. Should work on intel
301 * strata flash chips 28FxxxJ3x in 8-bit mode.
302 * Returns 1 if sector is protected (or timed-out while trying to read
303 * protection status), 0 if it is not.
305 static unsigned char intel_sector_protected (flash_info_t
*info
, ushort sector
)
308 FPWV
*lock_conf_addr
;
313 * first, wait for the WSM to be finished. The rationale for
314 * waiting for the WSM to become idle for at most
315 * CONFIG_SYS_FLASH_ERASE_TOUT is as follows. The WSM can be busy
316 * because of: (1) erase, (2) program or (3) lock bit
317 * configuration. So we just wait for the longest timeout of
318 * the (1)-(3), i.e. the erase timeout.
321 /* wait at least 35ns (W12) before issuing Read Status Register */
323 addr
= (FPWV
*) info
->start
[sector
];
324 *addr
= (FPW
) INTEL_STATUS
;
326 start
= get_timer (0);
327 while ((*addr
& (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
) {
328 if (get_timer (start
) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
329 *addr
= (FPW
) INTEL_RESET
; /* restore read mode */
330 printf("WSM busy too long, can't get prot status\n");
335 /* issue the Read Identifier Codes command */
336 *addr
= (FPW
) INTEL_READID
;
338 /* wait at least 35ns (W12) before reading */
341 /* Intel example code uses offset of 2 for 16 bit flash */
342 lock_conf_addr
= (FPWV
*) info
->start
[sector
] + 2;
343 ret
= (*lock_conf_addr
& (FPW
) INTEL_PROTECT
) ? 1 : 0;
345 /* put flash back in read mode */
346 *addr
= (FPW
) INTEL_RESET
;
351 /*-----------------------------------------------------------------------
354 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
356 int flag
, prot
, sect
;
360 if ((s_first
< 0) || (s_first
> s_last
)) {
361 if (info
->flash_id
== FLASH_UNKNOWN
) {
362 printf ("- missing\n");
364 printf ("- no sectors to erase\n");
369 type
= (info
->flash_id
& FLASH_VENDMASK
);
370 if ((type
!= FLASH_MAN_INTEL
)) {
371 printf ("Can't erase unknown flash type %08lx - aborted\n",
377 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
378 if (info
->protect
[sect
]) {
384 printf ("- Warning: %d protected sectors will not be erased!\n",
390 start
= get_timer (0);
392 /* Disable interrupts which might cause a timeout here */
393 flag
= disable_interrupts ();
395 /* Start erase on unprotected sectors */
396 for (sect
= s_first
; sect
<= s_last
; sect
++) {
397 if (info
->protect
[sect
] == 0) { /* not protected */
398 FPWV
*addr
= (FPWV
*) (info
->start
[sect
]);
401 printf ("Erasing sector %2d ... ", sect
);
403 /* arm simple, non interrupt dependent timer */
404 start
= get_timer(0);
406 *addr
= (FPW
) 0x00500050; /* clear status register */
407 *addr
= (FPW
) 0x00200020; /* erase setup */
408 *addr
= (FPW
) 0x00D000D0; /* erase confirm */
410 while (((status
= *addr
) & (FPW
) 0x00800080) != (FPW
) 0x00800080) {
411 if (get_timer(start
) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
412 printf ("Timeout\n");
413 *addr
= (FPW
) 0x00B000B0; /* suspend erase */
414 *addr
= (FPW
) 0x00FF00FF; /* reset to read mode */
420 *addr
= 0x00500050; /* clear status register cmd. */
421 *addr
= 0x00FF00FF; /* resest to read mode */
433 /*-----------------------------------------------------------------------
434 * Copy memory to flash, returns:
437 * 2 - Flash not erased
438 * 4 - Flash not identified
441 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
445 int count
, i
, l
, rc
, port_width
;
447 if (info
->flash_id
== FLASH_UNKNOWN
) {
450 /* get lower word aligned address */
451 #ifdef FLASH_PORT_WIDTH16
460 * handle unaligned start bytes
462 if ((l
= addr
- wp
) != 0) {
464 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
465 data
= (data
<< 8) | (*(uchar
*) cp
);
467 for (; i
< port_width
&& cnt
> 0; ++i
) {
468 data
= (data
<< 8) | *src
++;
472 for (; cnt
== 0 && i
< port_width
; ++i
, ++cp
) {
473 data
= (data
<< 8) | (*(uchar
*) cp
);
476 if ((rc
= write_data (info
, wp
, SWAP (data
))) != 0) {
483 * handle word aligned part
486 while (cnt
>= port_width
) {
488 for (i
= 0; i
< port_width
; ++i
) {
489 data
= (data
<< 8) | *src
++;
491 if ((rc
= write_data (info
, wp
, SWAP (data
))) != 0) {
496 if (count
++ > 0x800) {
507 * handle unaligned tail bytes
510 for (i
= 0, cp
= wp
; i
< port_width
&& cnt
> 0; ++i
, ++cp
) {
511 data
= (data
<< 8) | *src
++;
514 for (; i
< port_width
; ++i
, ++cp
) {
515 data
= (data
<< 8) | (*(uchar
*) cp
);
518 return (write_data (info
, wp
, SWAP (data
)));
521 /*-----------------------------------------------------------------------
522 * Write a word or halfword to Flash, returns:
525 * 2 - Flash not erased
527 static int write_data (flash_info_t
*info
, ulong dest
, FPW data
)
529 FPWV
*addr
= (FPWV
*) dest
;
535 /* Check if Flash is (sufficiently) erased */
536 if ((*addr
& data
) != data
) {
537 printf ("not erased at %08lx (%lx)\n", (ulong
) addr
, *addr
);
540 /* Disable interrupts which might cause a timeout here */
541 flag
= disable_interrupts ();
543 *addr
= (FPW
) 0x00400040; /* write setup */
546 /* arm simple, non interrupt dependent timer */
547 start
= get_timer(0);
549 /* wait while polling the status register */
550 while (((status
= *addr
) & (FPW
) 0x00800080) != (FPW
) 0x00800080) {
551 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
557 *addr
= (FPW
) 0x00FF00FF; /* restore read mode */
565 void inline spin_wheel (void)
568 static char w
[] = "\\/-";
570 printf ("\010%c", w
[p
]);
571 (++p
== 3) ? (p
= 0) : 0;
574 /*-----------------------------------------------------------------------
575 * Set/Clear sector's lock bit, returns:
577 * 1 - Error (timeout, voltage problems, etc.)
579 int flash_real_protect (flash_info_t
*info
, long sector
, int prot
)
584 vu_long
*addr
= (vu_long
*)(info
->start
[sector
]);
585 int flag
= disable_interrupts();
587 *addr
= INTEL_CLEAR
; /* Clear status register */
588 if (prot
) { /* Set sector lock bit */
589 *addr
= INTEL_LOCKBIT
; /* Sector lock bit */
590 *addr
= INTEL_PROTECT
; /* set */
592 else { /* Clear sector lock bit */
593 *addr
= INTEL_LOCKBIT
; /* All sectors lock bits */
594 *addr
= INTEL_CONFIRM
; /* clear */
597 start
= get_timer(0);
599 while ((*addr
& INTEL_FINISHED
) != INTEL_FINISHED
) {
600 if (get_timer(start
) > CONFIG_SYS_FLASH_UNLOCK_TOUT
) {
601 printf("Flash lock bit operation timed out\n");
607 if (*addr
!= INTEL_OK
) {
608 printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
609 (uint
)addr
, (uint
)*addr
);
614 info
->protect
[sector
] = prot
;
617 * Clear lock bit command clears all sectors lock bits, so
618 * we have to restore lock bits of protected sectors.
619 * WARNING: code below re-locks sectors only for one bank (info).
620 * This causes problems on boards where several banks share
621 * the same chip, as sectors in othere banks will be unlocked
622 * but not re-locked. It works fine on pm520 though, as there
623 * is only one chip and one bank.
627 for (i
= 0; i
< info
->sector_count
; i
++)
629 if (info
->protect
[i
])
631 start
= get_timer(0);
632 addr
= (vu_long
*)(info
->start
[i
]);
633 *addr
= INTEL_LOCKBIT
; /* Sector lock bit */
634 *addr
= INTEL_PROTECT
; /* set */
635 while ((*addr
& INTEL_FINISHED
) != INTEL_FINISHED
)
637 if (get_timer(start
) > CONFIG_SYS_FLASH_UNLOCK_TOUT
)
639 printf("Flash lock bit operation timed out\n");
647 * get the s/w sector protection status in sync with the h/w,
648 * in case something went wrong during the re-locking.
650 flash_sync_real_protect(info
); /* resets flash to read mode */
656 *addr
= INTEL_RESET
; /* Reset to read array mode */