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)
15 #include <asm/immap.h>
17 #ifndef CONFIG_SYS_FLASH_CFI
18 typedef unsigned short FLASH_PORT_WIDTH;
19 typedef volatile unsigned short FLASH_PORT_WIDTHV;
21 #define FPW FLASH_PORT_WIDTH
22 #define FPWV FLASH_PORT_WIDTHV
24 #define FLASH_CYCLE1 0x5555
25 #define FLASH_CYCLE2 0x2aaa
27 #define SYNC __asm__("nop")
29 /*-----------------------------------------------------------------------
33 ulong flash_get_size(FPWV * addr, flash_info_t * info);
34 int flash_get_offsets(ulong base, flash_info_t * info);
35 int write_word(flash_info_t * info, FPWV * dest, u16 data);
36 static inline void spin_wheel(void);
38 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
40 ulong flash_init(void)
45 fbase = (ulong) CONFIG_SYS_FLASH_BASE;
46 flash_get_size((FPWV *) fbase, &flash_info[0]);
47 flash_get_offsets((ulong) fbase, &flash_info[0]);
48 fbase += flash_info[0].size;
49 size += flash_info[0].size;
51 /* Protect monitor and environment sectors */
52 flash_protect(FLAG_PROTECT_SET,
53 CONFIG_SYS_MONITOR_BASE,
54 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
59 int flash_get_offsets(ulong base, flash_info_t * info)
63 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
65 info->start[0] = base;
67 for (i = 1; i < CONFIG_SYS_SST_SECT; i++) {
68 info->start[i] = info->start[i - 1]
69 + CONFIG_SYS_SST_SECTSZ;
77 void flash_print_info(flash_info_t * info)
81 switch (info->flash_id & FLASH_VENDMASK) {
86 printf("Unknown Vendor ");
90 switch (info->flash_id & FLASH_TYPEMASK) {
92 printf("SST39VF6401B\n");
95 printf("Unknown Chip Type\n");
99 if (info->size > 0x100000) {
102 printf(" Size: %ld", info->size >> 20);
104 remainder = (info->size % 0x100000);
107 remainder = (int)((float)
108 (((float)remainder / (float)1024) *
110 printf(".%d ", remainder);
113 printf("MB in %d Sectors\n", info->sector_count);
115 printf(" Size: %ld KB in %d Sectors\n",
116 info->size >> 10, info->sector_count);
118 printf(" Sector Start Addresses:");
119 for (i = 0; i < info->sector_count; ++i) {
123 info->start[i], info->protect[i] ? " (RO)" : " ");
129 * The following code cannot be run from FLASH!
131 ulong flash_get_size(FPWV * addr, flash_info_t * info)
135 addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
136 addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
137 addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
139 switch (addr[0] & 0xffff) {
140 case (u8) SST_MANUFACT:
141 info->flash_id = FLASH_MAN_SST;
145 printf("Unknown Flash\n");
146 info->flash_id = FLASH_UNKNOWN;
147 info->sector_count = 0;
150 *addr = (FPW) 0x00F000F0;
151 return (0); /* no or unknown flash */
155 case (u16) SST_ID_xF6401B:
156 info->flash_id += FLASH_SST6401B;
159 info->flash_id = FLASH_UNKNOWN;
163 info->sector_count = 0;
165 info->sector_count = CONFIG_SYS_SST_SECT;
166 info->size = CONFIG_SYS_SST_SECT * CONFIG_SYS_SST_SECTSZ;
169 *addr = (FPWV) 0x00F000F0;
171 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
172 printf("** ERROR: sector count %d > max (%d) **\n",
173 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
174 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
180 int flash_erase(flash_info_t * info, int s_first, int s_last)
183 int flag, prot, sect, count;
185 int rcode = 0, flashtype = 0;
187 if ((s_first < 0) || (s_first > s_last)) {
188 if (info->flash_id == FLASH_UNKNOWN)
189 printf("- missing\n");
191 printf("- no sectors to erase\n");
195 type = (info->flash_id & FLASH_VENDMASK);
202 type = (info->flash_id & FLASH_VENDMASK);
203 printf("Can't erase unknown flash type %08lx - aborted\n",
209 for (sect = s_first; sect <= s_last; ++sect) {
210 if (info->protect[sect]) {
216 printf("- Warning: %d protected sectors will not be erased!\n",
221 flag = disable_interrupts();
223 start = get_timer(0);
225 if ((s_last - s_first) == (CONFIG_SYS_SST_SECT - 1)) {
227 addr = (FPWV *) info->start[0];
229 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
230 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
231 addr[FLASH_CYCLE1] = 0x0080; /* erase mode */
232 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
233 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
234 *addr = 0x0030; /* erase chip */
237 start = get_timer(0);
239 while ((*addr & 0x0080) != 0x0080) {
240 if (count++ > 0x10000) {
245 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
247 *addr = 0x00F0; /* reset to read mode */
253 *addr = 0x00F0; /* reset to read mode */
255 printf("\b. done\n");
261 } else if (prot == CONFIG_SYS_SST_SECT) {
266 /* Start erase on unprotected sectors */
267 for (sect = s_first; sect <= s_last; sect++) {
268 if (info->protect[sect] == 0) { /* not protected */
270 addr = (FPWV *) (info->start[sect]);
274 /* arm simple, non interrupt dependent timer */
275 start = get_timer(0);
280 FPWV *base; /* first address in bank */
282 flag = disable_interrupts();
284 base = (FPWV *) (CONFIG_SYS_FLASH_BASE); /* First sector */
286 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
287 base[FLASH_CYCLE2] = 0x0055; /* unlock */
288 base[FLASH_CYCLE1] = 0x0080; /* erase mode */
289 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
290 base[FLASH_CYCLE2] = 0x0055; /* unlock */
291 *addr = 0x0050; /* erase sector */
296 while ((*addr & 0x0080) != 0x0080) {
297 if (get_timer(start) >
298 CONFIG_SYS_FLASH_ERASE_TOUT) {
300 *addr = 0x00F0; /* reset to read mode */
307 *addr = 0x00F0; /* reset to read mode */
310 } /* switch (flashtype) */
321 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
327 if (info->flash_id == FLASH_UNKNOWN)
330 /* get lower word aligned address */
333 /* handle unaligned start bytes */
335 data = *((FPWV *) wp);
336 data = (data << 8) | *src;
338 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
348 * handle word aligned part
351 data = *((FPWV *) src);
353 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
360 if (count++ > 0x800) {
365 /* handle word aligned part */
367 /* handle word aligned part */
369 data = *((FPWV *) wp);
371 data = (data & 0x00FF) | (*src << 8);
373 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
379 if (count++ > 0x800) {
391 /*-----------------------------------------------------------------------
392 * Write a word to Flash
393 * A word is 16 bits, whichever the bus width of the flash bank
394 * (not an individual chip) is.
399 * 2 - Flash not erased
401 int write_word(flash_info_t * info, FPWV * dest, u16 data)
405 int res = 0; /* result, assume success */
406 FPWV *base; /* first address in flash bank */
408 /* Check if Flash is (sufficiently) erased */
409 if ((*dest & (u8) data) != (u8) data) {
413 base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
415 /* Disable interrupts which might cause a timeout here */
416 flag = disable_interrupts();
418 base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
419 base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
420 base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
422 *dest = data; /* start programming the data */
424 /* re-enable interrupts if necessary */
428 start = get_timer(0);
430 /* data polling for D7 */
432 && (*dest & (u8) 0x00800080) != (data & (u8) 0x00800080)) {
433 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
434 *dest = (u8) 0x00F000F0; /* reset bank */
439 *dest++ = (u8) 0x00F000F0; /* reset bank */
444 static inline void spin_wheel(void)
447 static char w[] = "\\/-";
449 printf("\010%c", w[p]);
450 (++p == 3) ? (p = 0) : 0;