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 /* check timeout, 1000ms */
246 if (get_timer(start) > 1000) {
248 *addr = 0x00F0; /* reset to read mode */
254 *addr = 0x00F0; /* reset to read mode */
256 printf("\b. done\n");
262 } else if (prot == CONFIG_SYS_SST_SECT) {
267 /* Start erase on unprotected sectors */
268 for (sect = s_first; sect <= s_last; sect++) {
269 if (info->protect[sect] == 0) { /* not protected */
271 addr = (FPWV *) (info->start[sect]);
275 /* arm simple, non interrupt dependent timer */
276 start = get_timer(0);
281 FPWV *base; /* first address in bank */
283 flag = disable_interrupts();
285 base = (FPWV *) (CONFIG_SYS_FLASH_BASE); /* First sector */
287 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
288 base[FLASH_CYCLE2] = 0x0055; /* unlock */
289 base[FLASH_CYCLE1] = 0x0080; /* erase mode */
290 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
291 base[FLASH_CYCLE2] = 0x0055; /* unlock */
292 *addr = 0x0050; /* erase sector */
297 while ((*addr & 0x0080) != 0x0080) {
298 /* check timeout, 1000ms */
299 if (get_timer(start) > 1000) {
301 *addr = 0x00F0; /* reset to read mode */
308 *addr = 0x00F0; /* reset to read mode */
311 } /* switch (flashtype) */
322 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
328 if (info->flash_id == FLASH_UNKNOWN)
331 /* get lower word aligned address */
334 /* handle unaligned start bytes */
336 data = *((FPWV *) wp);
337 data = (data << 8) | *src;
339 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
349 * handle word aligned part
352 data = *((FPWV *) src);
354 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
361 if (count++ > 0x800) {
366 /* handle word aligned part */
368 /* handle word aligned part */
370 data = *((FPWV *) wp);
372 data = (data & 0x00FF) | (*src << 8);
374 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
380 if (count++ > 0x800) {
392 /*-----------------------------------------------------------------------
393 * Write a word to Flash
394 * A word is 16 bits, whichever the bus width of the flash bank
395 * (not an individual chip) is.
400 * 2 - Flash not erased
402 int write_word(flash_info_t * info, FPWV * dest, u16 data)
406 int res = 0; /* result, assume success */
407 FPWV *base; /* first address in flash bank */
409 /* Check if Flash is (sufficiently) erased */
410 if ((*dest & (u8) data) != (u8) data) {
414 base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
416 /* Disable interrupts which might cause a timeout here */
417 flag = disable_interrupts();
419 base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
420 base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
421 base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
423 *dest = data; /* start programming the data */
425 /* re-enable interrupts if necessary */
429 start = get_timer(0);
431 /* data polling for D7 */
433 && (*dest & (u8) 0x00800080) != (data & (u8) 0x00800080)) {
434 /* check timeout, 500ms */
435 if (get_timer(start) > 500) {
436 *dest = (u8) 0x00F000F0; /* reset bank */
441 *dest++ = (u8) 0x00F000F0; /* reset bank */
446 static inline void spin_wheel(void)
449 static char w[] = "\\/-";
451 printf("\010%c", w[p]);
452 (++p == 3) ? (p = 0) : 0;