2 * (C) Copyright 2000-2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
6 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/immap.h>
15 #ifndef CONFIG_SYS_FLASH_CFI
16 typedef unsigned short FLASH_PORT_WIDTH;
17 typedef volatile unsigned short FLASH_PORT_WIDTHV;
19 #define FPW FLASH_PORT_WIDTH
20 #define FPWV FLASH_PORT_WIDTHV
22 #define FLASH_CYCLE1 0x5555
23 #define FLASH_CYCLE2 0x2aaa
25 #define SYNC __asm__("nop")
27 /*-----------------------------------------------------------------------
31 ulong flash_get_size(FPWV * addr, flash_info_t * info);
32 int flash_get_offsets(ulong base, flash_info_t * info);
33 int write_word(flash_info_t * info, FPWV * dest, u16 data);
34 void inline spin_wheel(void);
36 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
38 ulong flash_init(void)
43 fbase = (ulong) CONFIG_SYS_FLASH_BASE;
44 flash_get_size((FPWV *) fbase, &flash_info[0]);
45 flash_get_offsets((ulong) fbase, &flash_info[0]);
46 fbase += flash_info[0].size;
47 size += flash_info[0].size;
49 /* Protect monitor and environment sectors */
50 flash_protect(FLAG_PROTECT_SET,
51 CONFIG_SYS_MONITOR_BASE,
52 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
57 int flash_get_offsets(ulong base, flash_info_t * info)
61 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
63 info->start[0] = base;
64 for (k = 0, j = 0; j < CONFIG_SYS_SST_SECT; j++, k++) {
65 info->start[k + 1] = info->start[k] + CONFIG_SYS_SST_SECTSZ;
73 void flash_print_info(flash_info_t * info)
77 switch (info->flash_id & FLASH_VENDMASK) {
82 printf("Unknown Vendor ");
86 switch (info->flash_id & FLASH_TYPEMASK) {
88 printf("SST39VF6401B\n");
91 printf("Unknown Chip Type\n");
95 if (info->size > 0x100000) {
98 printf(" Size: %ld", info->size >> 20);
100 remainder = (info->size % 0x100000);
103 remainder = (int)((float)
104 (((float)remainder / (float)1024) *
106 printf(".%d ", remainder);
109 printf("MB in %d Sectors\n", info->sector_count);
111 printf(" Size: %ld KB in %d Sectors\n",
112 info->size >> 10, info->sector_count);
114 printf(" Sector Start Addresses:");
115 for (i = 0; i < info->sector_count; ++i) {
119 info->start[i], info->protect[i] ? " (RO)" : " ");
125 * The following code cannot be run from FLASH!
127 ulong flash_get_size(FPWV * addr, flash_info_t * info)
131 addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
132 addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
133 addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
135 switch (addr[0] & 0xffff) {
136 case (u8) SST_MANUFACT:
137 info->flash_id = FLASH_MAN_SST;
141 printf("Unknown Flash\n");
142 info->flash_id = FLASH_UNKNOWN;
143 info->sector_count = 0;
146 *addr = (FPW) 0x00F000F0;
147 return (0); /* no or unknown flash */
151 case (u16) SST_ID_xF6401B:
152 info->flash_id += FLASH_SST6401B;
155 info->flash_id = FLASH_UNKNOWN;
159 info->sector_count = 0;
161 info->sector_count = CONFIG_SYS_SST_SECT;
162 info->size = CONFIG_SYS_SST_SECT * CONFIG_SYS_SST_SECTSZ;
165 *addr = (FPWV) 0x00F000F0;
167 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
168 printf("** ERROR: sector count %d > max (%d) **\n",
169 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
170 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
176 int flash_erase(flash_info_t * info, int s_first, int s_last)
179 int flag, prot, sect, count;
180 ulong type, start, last;
181 int rcode = 0, flashtype = 0;
183 if ((s_first < 0) || (s_first > s_last)) {
184 if (info->flash_id == FLASH_UNKNOWN)
185 printf("- missing\n");
187 printf("- no sectors to erase\n");
191 type = (info->flash_id & FLASH_VENDMASK);
198 type = (info->flash_id & FLASH_VENDMASK);
199 printf("Can't erase unknown flash type %08lx - aborted\n",
205 for (sect = s_first; sect <= s_last; ++sect) {
206 if (info->protect[sect]) {
212 printf("- Warning: %d protected sectors will not be erased!\n",
217 flag = disable_interrupts();
219 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 */
329 port_width = sizeof(FPW);
331 /* handle unaligned start bytes */
333 data = *((FPWV *) wp);
334 data = (data << 8) | *src;
336 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
346 * handle word aligned part
349 data = *((FPWV *) src);
351 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
358 if (count++ > 0x800) {
363 /* handle word aligned part */
365 /* handle word aligned part */
367 data = *((FPWV *) wp);
369 data = (data & 0x00FF) | (*src << 8);
371 if ((rc = write_word(info, (FPWV *) wp, data)) != 0)
377 if (count++ > 0x800) {
389 /*-----------------------------------------------------------------------
390 * Write a word to Flash
391 * A word is 16 bits, whichever the bus width of the flash bank
392 * (not an individual chip) is.
397 * 2 - Flash not erased
399 int write_word(flash_info_t * info, FPWV * dest, u16 data)
403 int res = 0; /* result, assume success */
404 FPWV *base; /* first address in flash bank */
406 /* Check if Flash is (sufficiently) erased */
407 if ((*dest & (u8) data) != (u8) data) {
411 base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
413 /* Disable interrupts which might cause a timeout here */
414 flag = disable_interrupts();
416 base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
417 base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
418 base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
420 *dest = data; /* start programming the data */
422 /* re-enable interrupts if necessary */
426 start = get_timer(0);
428 /* data polling for D7 */
430 && (*dest & (u8) 0x00800080) != (data & (u8) 0x00800080)) {
431 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
432 *dest = (u8) 0x00F000F0; /* reset bank */
437 *dest++ = (u8) 0x00F000F0; /* reset bank */
442 void inline spin_wheel(void)
445 static char w[] = "\\/-";
447 printf("\010%c", w[p]);
448 (++p == 3) ? (p = 0) : 0;