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;
65 for (i = 1; i < CONFIG_SYS_SST_SECT; i++) {
66 info->start[i] = info->start[i - 1]
67 + CONFIG_SYS_SST_SECTSZ;
75 void flash_print_info(flash_info_t * info)
79 switch (info->flash_id & FLASH_VENDMASK) {
84 printf("Unknown Vendor ");
88 switch (info->flash_id & FLASH_TYPEMASK) {
90 printf("SST39VF6401B\n");
93 printf("Unknown Chip Type\n");
97 if (info->size > 0x100000) {
100 printf(" Size: %ld", info->size >> 20);
102 remainder = (info->size % 0x100000);
105 remainder = (int)((float)
106 (((float)remainder / (float)1024) *
108 printf(".%d ", remainder);
111 printf("MB in %d Sectors\n", info->sector_count);
113 printf(" Size: %ld KB in %d Sectors\n",
114 info->size >> 10, info->sector_count);
116 printf(" Sector Start Addresses:");
117 for (i = 0; i < info->sector_count; ++i) {
121 info->start[i], info->protect[i] ? " (RO)" : " ");
127 * The following code cannot be run from FLASH!
129 ulong flash_get_size(FPWV * addr, flash_info_t * info)
133 addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
134 addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
135 addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
137 switch (addr[0] & 0xffff) {
138 case (u8) SST_MANUFACT:
139 info->flash_id = FLASH_MAN_SST;
143 printf("Unknown Flash\n");
144 info->flash_id = FLASH_UNKNOWN;
145 info->sector_count = 0;
148 *addr = (FPW) 0x00F000F0;
149 return (0); /* no or unknown flash */
153 case (u16) SST_ID_xF6401B:
154 info->flash_id += FLASH_SST6401B;
157 info->flash_id = FLASH_UNKNOWN;
161 info->sector_count = 0;
163 info->sector_count = CONFIG_SYS_SST_SECT;
164 info->size = CONFIG_SYS_SST_SECT * CONFIG_SYS_SST_SECTSZ;
167 *addr = (FPWV) 0x00F000F0;
169 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
170 printf("** ERROR: sector count %d > max (%d) **\n",
171 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
172 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
178 int flash_erase(flash_info_t * info, int s_first, int s_last)
181 int flag, prot, sect, count;
183 int rcode = 0, flashtype = 0;
185 if ((s_first < 0) || (s_first > s_last)) {
186 if (info->flash_id == FLASH_UNKNOWN)
187 printf("- missing\n");
189 printf("- no sectors to erase\n");
193 type = (info->flash_id & FLASH_VENDMASK);
200 type = (info->flash_id & FLASH_VENDMASK);
201 printf("Can't erase unknown flash type %08lx - aborted\n",
207 for (sect = s_first; sect <= s_last; ++sect) {
208 if (info->protect[sect]) {
214 printf("- Warning: %d protected sectors will not be erased!\n",
219 flag = disable_interrupts();
221 start = get_timer(0);
223 if ((s_last - s_first) == (CONFIG_SYS_SST_SECT - 1)) {
225 addr = (FPWV *) info->start[0];
227 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
228 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
229 addr[FLASH_CYCLE1] = 0x0080; /* erase mode */
230 addr[FLASH_CYCLE1] = 0x00AA; /* unlock */
231 addr[FLASH_CYCLE2] = 0x0055; /* unlock */
232 *addr = 0x0030; /* erase chip */
235 start = get_timer(0);
237 while ((*addr & 0x0080) != 0x0080) {
238 if (count++ > 0x10000) {
243 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
245 *addr = 0x00F0; /* reset to read mode */
251 *addr = 0x00F0; /* reset to read mode */
253 printf("\b. done\n");
259 } else if (prot == CONFIG_SYS_SST_SECT) {
264 /* Start erase on unprotected sectors */
265 for (sect = s_first; sect <= s_last; sect++) {
266 if (info->protect[sect] == 0) { /* not protected */
268 addr = (FPWV *) (info->start[sect]);
272 /* arm simple, non interrupt dependent timer */
273 start = get_timer(0);
278 FPWV *base; /* first address in bank */
280 flag = disable_interrupts();
282 base = (FPWV *) (CONFIG_SYS_FLASH_BASE); /* First sector */
284 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
285 base[FLASH_CYCLE2] = 0x0055; /* unlock */
286 base[FLASH_CYCLE1] = 0x0080; /* erase mode */
287 base[FLASH_CYCLE1] = 0x00AA; /* unlock */
288 base[FLASH_CYCLE2] = 0x0055; /* unlock */
289 *addr = 0x0050; /* erase sector */
294 while ((*addr & 0x0080) != 0x0080) {
295 if (get_timer(start) >
296 CONFIG_SYS_FLASH_ERASE_TOUT) {
298 *addr = 0x00F0; /* reset to read mode */
305 *addr = 0x00F0; /* reset to read mode */
308 } /* switch (flashtype) */
319 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
325 if (info->flash_id == FLASH_UNKNOWN)
328 /* get lower word aligned address */
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;