1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
7 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
12 #include <asm/immap.h>
14 #ifndef CONFIG_SYS_FLASH_CFI
15 typedef unsigned short FLASH_PORT_WIDTH;
16 typedef volatile unsigned short FLASH_PORT_WIDTHV;
18 #define FPW FLASH_PORT_WIDTH
19 #define FPWV FLASH_PORT_WIDTHV
21 #define FLASH_CYCLE1 0x5555
22 #define FLASH_CYCLE2 0x2aaa
24 #define SYNC __asm__("nop")
26 /*-----------------------------------------------------------------------
30 ulong flash_get_size(FPWV * addr, flash_info_t * info);
31 int flash_get_offsets(ulong base, flash_info_t * info);
32 int write_word(flash_info_t * info, FPWV * dest, u16 data);
33 static inline void spin_wheel(void);
35 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
37 ulong flash_init(void)
42 fbase = (ulong) CONFIG_SYS_FLASH_BASE;
43 flash_get_size((FPWV *) fbase, &flash_info[0]);
44 flash_get_offsets((ulong) fbase, &flash_info[0]);
45 fbase += flash_info[0].size;
46 size += flash_info[0].size;
48 /* Protect monitor and environment sectors */
49 flash_protect(FLAG_PROTECT_SET,
50 CONFIG_SYS_MONITOR_BASE,
51 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
56 int flash_get_offsets(ulong base, flash_info_t * info)
60 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
62 info->start[0] = base;
64 for (i = 1; i < CONFIG_SYS_SST_SECT; i++) {
65 info->start[i] = info->start[i - 1]
66 + CONFIG_SYS_SST_SECTSZ;
74 void flash_print_info(flash_info_t * info)
78 switch (info->flash_id & FLASH_VENDMASK) {
83 printf("Unknown Vendor ");
87 switch (info->flash_id & FLASH_TYPEMASK) {
89 printf("SST39VF6401B\n");
92 printf("Unknown Chip Type\n");
96 if (info->size > 0x100000) {
99 printf(" Size: %ld", info->size >> 20);
101 remainder = (info->size % 0x100000);
104 remainder = (int)((float)
105 (((float)remainder / (float)1024) *
107 printf(".%d ", remainder);
110 printf("MB in %d Sectors\n", info->sector_count);
112 printf(" Size: %ld KB in %d Sectors\n",
113 info->size >> 10, info->sector_count);
115 printf(" Sector Start Addresses:");
116 for (i = 0; i < info->sector_count; ++i) {
120 info->start[i], info->protect[i] ? " (RO)" : " ");
126 * The following code cannot be run from FLASH!
128 ulong flash_get_size(FPWV * addr, flash_info_t * info)
132 addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
133 addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
134 addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
136 switch (addr[0] & 0xffff) {
137 case (u8) SST_MANUFACT:
138 info->flash_id = FLASH_MAN_SST;
142 printf("Unknown Flash\n");
143 info->flash_id = FLASH_UNKNOWN;
144 info->sector_count = 0;
147 *addr = (FPW) 0x00F000F0;
148 return (0); /* no or unknown flash */
152 case (u16) SST_ID_xF6401B:
153 info->flash_id += FLASH_SST6401B;
156 info->flash_id = FLASH_UNKNOWN;
160 info->sector_count = 0;
162 info->sector_count = CONFIG_SYS_SST_SECT;
163 info->size = CONFIG_SYS_SST_SECT * CONFIG_SYS_SST_SECTSZ;
166 *addr = (FPWV) 0x00F000F0;
168 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
169 printf("** ERROR: sector count %d > max (%d) **\n",
170 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
171 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
177 int flash_erase(flash_info_t * info, int s_first, int s_last)
180 int flag, prot, sect, count;
182 int rcode = 0, flashtype = 0;
184 if ((s_first < 0) || (s_first > s_last)) {
185 if (info->flash_id == FLASH_UNKNOWN)
186 printf("- missing\n");
188 printf("- no sectors to erase\n");
192 type = (info->flash_id & FLASH_VENDMASK);
199 type = (info->flash_id & FLASH_VENDMASK);
200 printf("Can't erase unknown flash type %08lx - aborted\n",
206 for (sect = s_first; sect <= s_last; ++sect) {
207 if (info->protect[sect]) {
213 printf("- Warning: %d protected sectors will not be erased!\n",
218 flag = disable_interrupts();
220 start = get_timer(0);
222 if ((s_last - s_first) == (CONFIG_SYS_SST_SECT - 1)) {
224 addr = (FPWV *) info->start[0];
226 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
227 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
228 addr[FLASH_CYCLE1] = 0x0080; /* erase mode */
229 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
230 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
231 *addr = 0x0030; /* erase chip */
234 start = get_timer(0);
236 while ((*addr & 0x0080) != 0x0080) {
237 if (count++ > 0x10000) {
242 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
244 *addr = 0x00F0; /* reset to read mode */
250 *addr = 0x00F0; /* reset to read mode */
252 printf("\b. done\n");
258 } else if (prot == CONFIG_SYS_SST_SECT) {
263 /* Start erase on unprotected sectors */
264 for (sect = s_first; sect <= s_last; sect++) {
265 if (info->protect[sect] == 0) { /* not protected */
267 addr = (FPWV *) (info->start[sect]);
271 /* arm simple, non interrupt dependent timer */
272 start = get_timer(0);
277 FPWV *base; /* first address in bank */
279 flag = disable_interrupts();
281 base = (FPWV *) (CONFIG_SYS_FLASH_BASE); /* First sector */
283 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
284 base[FLASH_CYCLE2] = 0x0055; /* unlock */
285 base[FLASH_CYCLE1] = 0x0080; /* erase mode */
286 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
287 base[FLASH_CYCLE2] = 0x0055; /* unlock */
288 *addr = 0x0050; /* erase sector */
293 while ((*addr & 0x0080) != 0x0080) {
294 if (get_timer(start) >
295 CONFIG_SYS_FLASH_ERASE_TOUT) {
297 *addr = 0x00F0; /* reset to read mode */
304 *addr = 0x00F0; /* reset to read mode */
307 } /* switch (flashtype) */
318 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
324 if (info->flash_id == FLASH_UNKNOWN)
327 /* get lower word aligned address */
330 /* handle unaligned start bytes */
332 data = *((FPWV *) wp);
333 data = (data << 8) | *src;
335 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
345 * handle word aligned part
348 data = *((FPWV *) src);
350 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
357 if (count++ > 0x800) {
362 /* handle word aligned part */
364 /* handle word aligned part */
366 data = *((FPWV *) wp);
368 data = (data & 0x00FF) | (*src << 8);
370 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
376 if (count++ > 0x800) {
388 /*-----------------------------------------------------------------------
389 * Write a word to Flash
390 * A word is 16 bits, whichever the bus width of the flash bank
391 * (not an individual chip) is.
396 * 2 - Flash not erased
398 int write_word(flash_info_t * info, FPWV * dest, u16 data)
402 int res = 0; /* result, assume success */
403 FPWV *base; /* first address in flash bank */
405 /* Check if Flash is (sufficiently) erased */
406 if ((*dest & (u8) data) != (u8) data) {
410 base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
412 /* Disable interrupts which might cause a timeout here */
413 flag = disable_interrupts();
415 base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
416 base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
417 base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
419 *dest = data; /* start programming the data */
421 /* re-enable interrupts if necessary */
425 start = get_timer(0);
427 /* data polling for D7 */
429 && (*dest & (u8) 0x00800080) != (data & (u8) 0x00800080)) {
430 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
431 *dest = (u8) 0x00F000F0; /* reset bank */
436 *dest++ = (u8) 0x00F000F0; /* reset bank */
441 static inline void spin_wheel(void)
444 static char w[] = "\\/-";
446 printf("\010%c", w[p]);
447 (++p == 3) ? (p = 0) : 0;