3 * Greg Ungerer, OpenGear Inc, greg.ungerer@opengear.com
6 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/byteorder/swab.h>
16 #include <asm/sections.h>
19 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
21 #define mb() __asm__ __volatile__ ("" : : : "memory")
23 /*-----------------------------------------------------------------------
26 static ulong flash_get_size (unsigned char * addr, flash_info_t * info);
27 static int write_data (flash_info_t * info, ulong dest, unsigned char data);
28 static void flash_get_offsets (ulong base, flash_info_t * info);
29 void inline spin_wheel (void);
31 /*-----------------------------------------------------------------------
34 unsigned long flash_init (void)
39 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
42 flash_get_size ((unsigned char *) PHYS_FLASH_1, &flash_info[i]);
43 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
47 flash_info[i].flash_id = FLASH_UNKNOWN;
50 panic ("configured too many flash banks!\n");
53 size += flash_info[i].size;
56 /* Protect monitor and environment sectors
58 flash_protect (FLAG_PROTECT_SET,
59 CONFIG_SYS_FLASH_BASE,
60 CONFIG_SYS_FLASH_BASE + (__bss_end - __bss_start),
66 /*-----------------------------------------------------------------------
68 static void flash_get_offsets (ulong base, flash_info_t * info)
72 if (info->flash_id == FLASH_UNKNOWN)
75 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
76 for (i = 0; i < info->sector_count; i++) {
77 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
83 /*-----------------------------------------------------------------------
85 void flash_print_info (flash_info_t * info)
89 if (info->flash_id == FLASH_UNKNOWN) {
90 printf ("missing or unknown FLASH type\n");
94 switch (info->flash_id & FLASH_VENDMASK) {
99 printf ("Unknown Vendor ");
103 switch (info->flash_id & FLASH_TYPEMASK) {
104 case FLASH_28F128J3A:
105 printf ("28F128J3A\n");
108 printf ("Unknown Chip Type\n");
112 printf (" Size: %ld MB in %d Sectors\n",
113 info->size >> 20, info->sector_count);
115 printf (" Sector Start Addresses:");
116 for (i = 0; i < info->sector_count; ++i) {
120 info->start[i], info->protect[i] ? " (RO)" : " ");
127 * The following code cannot be run from FLASH!
129 static ulong flash_get_size (unsigned char * addr, flash_info_t * info)
131 volatile unsigned char value;
133 /* Write auto select command: read Manufacturer ID */
143 case (unsigned char)INTEL_MANUFACT:
144 info->flash_id = FLASH_MAN_INTEL;
148 info->flash_id = FLASH_UNKNOWN;
149 info->sector_count = 0;
151 addr[0] = 0xFF; /* restore read mode */
152 return (0); /* no or unknown flash */
156 value = addr[2]; /* device ID */
160 case (unsigned char)INTEL_ID_28F640J3A:
161 info->flash_id += FLASH_28F640J3A;
162 info->sector_count = 64;
163 info->size = 0x00800000;
166 case (unsigned char)INTEL_ID_28F128J3A:
167 info->flash_id += FLASH_28F128J3A;
168 info->sector_count = 128;
169 info->size = 0x01000000;
170 break; /* => 16 MB */
173 info->flash_id = FLASH_UNKNOWN;
177 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
178 printf ("** ERROR: sector count %d > max (%d) **\n",
179 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
180 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
183 addr[0] = 0xFF; /* restore read mode */
189 /*-----------------------------------------------------------------------
192 int flash_erase (flash_info_t * info, int s_first, int s_last)
199 if ((s_first < 0) || (s_first > s_last)) {
200 if (info->flash_id == FLASH_UNKNOWN) {
201 printf ("- missing\n");
203 printf ("- no sectors to erase\n");
208 type = (info->flash_id & FLASH_VENDMASK);
209 if ((type != FLASH_MAN_INTEL)) {
210 printf ("Can't erase unknown flash type %08lx - aborted\n",
216 for (sect = s_first; sect <= s_last; ++sect) {
217 if (info->protect[sect]) {
223 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
227 /* Disable interrupts which might cause a timeout here */
228 disable_interrupts();
230 /* Start erase on unprotected sectors */
231 for (sect = s_first; sect <= s_last; sect++) {
232 if (info->protect[sect] == 0) { /* not protected */
233 volatile unsigned char *addr;
234 unsigned char status;
236 printf ("Erasing sector %2d ... ", sect);
238 /* arm simple, non interrupt dependent timer */
239 start = get_timer(0);
241 addr = (volatile unsigned char *) (info->start[sect]);
242 *addr = 0x50; /* clear status register */
243 *addr = 0x20; /* erase setup */
244 *addr = 0xD0; /* erase confirm */
246 while (((status = *addr) & 0x80) != 0x80) {
247 if (get_timer(start) >
248 CONFIG_SYS_FLASH_ERASE_TOUT) {
249 printf ("Timeout\n");
250 *addr = 0xB0; /* suspend erase */
251 *addr = 0xFF; /* reset to read mode */
257 *addr = 0x50; /* clear status register cmd */
258 *addr = 0xFF; /* resest to read mode */
266 /*-----------------------------------------------------------------------
267 * Copy memory to flash, returns:
270 * 2 - Flash not erased
271 * 4 - Flash not identified
274 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
278 int count, i, l, rc, port_width;
280 if (info->flash_id == FLASH_UNKNOWN)
287 * handle unaligned start bytes
289 if ((l = addr - wp) != 0) {
291 for (i = 0, cp = wp; i < l; ++i, ++cp) {
292 data = (data << 8) | (*(uchar *) cp);
294 for (; i < port_width && cnt > 0; ++i) {
295 data = (data << 8) | *src++;
299 for (; cnt == 0 && i < port_width; ++i, ++cp) {
300 data = (data << 8) | (*(uchar *) cp);
303 if ((rc = write_data (info, wp, data)) != 0) {
310 * handle word aligned part
313 while (cnt >= port_width) {
315 for (i = 0; i < port_width; ++i) {
316 data = (data << 8) | *src++;
318 if ((rc = write_data (info, wp, data)) != 0) {
323 if (count++ > 0x800) {
334 * handle unaligned tail bytes
337 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
338 data = (data << 8) | *src++;
341 for (; i < port_width; ++i, ++cp) {
342 data = (data << 8) | (*(uchar *) cp);
345 return (write_data (info, wp, data));
348 /*-----------------------------------------------------------------------
349 * Write a word or halfword to Flash, returns:
352 * 2 - Flash not erased
354 static int write_data (flash_info_t * info, ulong dest, unsigned char data)
356 volatile unsigned char *addr = (volatile unsigned char *) dest;
360 /* Check if Flash is (sufficiently) erased */
361 if ((*addr & data) != data) {
362 printf ("not erased at %08lx (%lx)\n", (ulong) addr,
366 /* Disable interrupts which might cause a timeout here */
367 disable_interrupts();
369 *addr = 0x40; /* write setup */
372 /* arm simple, non interrupt dependent timer */
373 start = get_timer(0);
375 /* wait while polling the status register */
376 while (((status = *addr) & 0x80) != 0x80) {
377 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
378 *addr = 0xFF; /* restore read mode */
383 *addr = 0xFF; /* restore read mode */
388 void inline spin_wheel (void)
391 static char w[] = "\\/-";
393 printf ("\010%c", w[p]);
394 (++p == 3) ? (p = 0) : 0;