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) CFG_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 < CFG_SYS_SST_SECT; i++) {
68 info->start[i] = info->start[i - 1]
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 printf(" Size: %ld KB in %d Sectors\n",
100 info->size >> 10, info->sector_count);
102 printf(" Sector Start Addresses:");
103 for (i = 0; i < info->sector_count; ++i) {
107 info->start[i], info->protect[i] ? " (RO)" : " ");
113 * The following code cannot be run from FLASH!
115 ulong flash_get_size(FPWV * addr, flash_info_t * info)
119 addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
120 addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
121 addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
123 switch (addr[0] & 0xffff) {
124 case (u8) SST_MANUFACT:
125 info->flash_id = FLASH_MAN_SST;
129 printf("Unknown Flash\n");
130 info->flash_id = FLASH_UNKNOWN;
131 info->sector_count = 0;
134 *addr = (FPW) 0x00F000F0;
135 return (0); /* no or unknown flash */
139 case (u16) SST_ID_xF6401B:
140 info->flash_id += FLASH_SST6401B;
143 info->flash_id = FLASH_UNKNOWN;
147 info->sector_count = 0;
149 info->sector_count = CFG_SYS_SST_SECT;
150 info->size = CFG_SYS_SST_SECT * CFG_SYS_SST_SECTSZ;
153 *addr = (FPWV) 0x00F000F0;
155 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
156 printf("** ERROR: sector count %d > max (%d) **\n",
157 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
158 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
164 int flash_erase(flash_info_t * info, int s_first, int s_last)
167 int flag, prot, sect, count;
169 int rcode = 0, flashtype = 0;
171 if ((s_first < 0) || (s_first > s_last)) {
172 if (info->flash_id == FLASH_UNKNOWN)
173 printf("- missing\n");
175 printf("- no sectors to erase\n");
179 type = (info->flash_id & FLASH_VENDMASK);
186 type = (info->flash_id & FLASH_VENDMASK);
187 printf("Can't erase unknown flash type %08lx - aborted\n",
193 for (sect = s_first; sect <= s_last; ++sect) {
194 if (info->protect[sect]) {
200 printf("- Warning: %d protected sectors will not be erased!\n",
205 flag = disable_interrupts();
207 start = get_timer(0);
209 if ((s_last - s_first) == (CFG_SYS_SST_SECT - 1)) {
211 addr = (FPWV *) info->start[0];
213 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
214 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
215 addr[FLASH_CYCLE1] = 0x0080; /* erase mode */
216 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
217 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
218 *addr = 0x0030; /* erase chip */
221 start = get_timer(0);
223 while ((*addr & 0x0080) != 0x0080) {
224 if (count++ > 0x10000) {
229 /* check timeout, 1000ms */
230 if (get_timer(start) > 1000) {
232 *addr = 0x00F0; /* reset to read mode */
238 *addr = 0x00F0; /* reset to read mode */
240 printf("\b. done\n");
246 } else if (prot == CFG_SYS_SST_SECT) {
251 /* Start erase on unprotected sectors */
252 for (sect = s_first; sect <= s_last; sect++) {
253 if (info->protect[sect] == 0) { /* not protected */
255 addr = (FPWV *) (info->start[sect]);
259 /* arm simple, non interrupt dependent timer */
260 start = get_timer(0);
265 FPWV *base; /* first address in bank */
267 flag = disable_interrupts();
269 base = (FPWV *) (CFG_SYS_FLASH_BASE); /* First sector */
271 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
272 base[FLASH_CYCLE2] = 0x0055; /* unlock */
273 base[FLASH_CYCLE1] = 0x0080; /* erase mode */
274 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
275 base[FLASH_CYCLE2] = 0x0055; /* unlock */
276 *addr = 0x0050; /* erase sector */
281 while ((*addr & 0x0080) != 0x0080) {
282 /* check timeout, 1000ms */
283 if (get_timer(start) > 1000) {
285 *addr = 0x00F0; /* reset to read mode */
292 *addr = 0x00F0; /* reset to read mode */
295 } /* switch (flashtype) */
306 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
312 if (info->flash_id == FLASH_UNKNOWN)
315 /* get lower word aligned address */
318 /* handle unaligned start bytes */
320 data = *((FPWV *) wp);
321 data = (data << 8) | *src;
323 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
333 * handle word aligned part
336 data = *((FPWV *) src);
338 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
345 if (count++ > 0x800) {
350 /* handle word aligned part */
352 /* handle word aligned part */
354 data = *((FPWV *) wp);
356 data = (data & 0x00FF) | (*src << 8);
358 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
364 if (count++ > 0x800) {
376 /*-----------------------------------------------------------------------
377 * Write a word to Flash
378 * A word is 16 bits, whichever the bus width of the flash bank
379 * (not an individual chip) is.
384 * 2 - Flash not erased
386 int write_word(flash_info_t * info, FPWV * dest, u16 data)
390 int res = 0; /* result, assume success */
391 FPWV *base; /* first address in flash bank */
393 /* Check if Flash is (sufficiently) erased */
394 if ((*dest & (u8) data) != (u8) data) {
398 base = (FPWV *) (CFG_SYS_FLASH_BASE);
400 /* Disable interrupts which might cause a timeout here */
401 flag = disable_interrupts();
403 base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
404 base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
405 base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
407 *dest = data; /* start programming the data */
409 /* re-enable interrupts if necessary */
413 start = get_timer(0);
415 /* data polling for D7 */
417 && (*dest & (u8) 0x00800080) != (data & (u8) 0x00800080)) {
418 /* check timeout, 500ms */
419 if (get_timer(start) > 500) {
420 *dest = (u8) 0x00F000F0; /* reset bank */
425 *dest++ = (u8) 0x00F000F0; /* reset bank */
430 static inline void spin_wheel(void)
433 static char w[] = "\\/-";
435 printf("\010%c", w[p]);
436 (++p == 3) ? (p = 0) : 0;