3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * Texas Instruments, <www.ti.com>
10 * Kshitij Gupta <Kshitij@ti.com>
12 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/byteorder/swab.h>
18 #define PHYS_FLASH_SECT_SIZE 0x00020000 /* 256 KB sectors (x2) */
19 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
21 /* Board support for 1 or 2 flash devices */
22 #undef FLASH_PORT_WIDTH32
23 #define FLASH_PORT_WIDTH16
25 #ifdef FLASH_PORT_WIDTH16
26 #define FLASH_PORT_WIDTH ushort
27 #define FLASH_PORT_WIDTHV vu_short
28 #define SWAP(x) __swab16(x)
30 #define FLASH_PORT_WIDTH ulong
31 #define FLASH_PORT_WIDTHV vu_long
32 #define SWAP(x) __swab32(x)
35 #define FPW FLASH_PORT_WIDTH
36 #define FPWV FLASH_PORT_WIDTHV
38 #define mb() __asm__ __volatile__ ("" : : : "memory")
41 /* Flash Organization Structure */
42 typedef struct OrgDef {
43 unsigned int sector_number;
44 unsigned int sector_size;
48 /* Flash Organizations */
49 OrgDef OrgIntel_28F256L18T[] = {
50 {4, 32 * 1024}, /* 4 * 32kBytes sectors */
51 {255, 128 * 1024}, /* 255 * 128kBytes sectors */
55 /*-----------------------------------------------------------------------
58 unsigned long flash_init (void);
59 static ulong flash_get_size (FPW * addr, flash_info_t * info);
60 static int write_data (flash_info_t * info, ulong dest, FPW data);
61 static void flash_get_offsets (ulong base, flash_info_t * info);
62 void inline spin_wheel (void);
63 void flash_print_info (flash_info_t * info);
64 void flash_unprotect_sectors (FPWV * addr);
65 int flash_erase (flash_info_t * info, int s_first, int s_last);
66 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
68 /*-----------------------------------------------------------------------
71 unsigned long flash_init (void)
75 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
78 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
79 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
82 panic ("configured too many flash banks!\n");
85 size += flash_info[i].size;
88 /* Protect monitor and environment sectors
90 flash_protect (FLAG_PROTECT_SET,
91 CONFIG_SYS_FLASH_BASE,
92 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
94 flash_protect (FLAG_PROTECT_SET,
96 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
101 /*-----------------------------------------------------------------------
103 static void flash_get_offsets (ulong base, flash_info_t * info)
107 if (info->flash_id == FLASH_UNKNOWN) {
111 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
112 for (i = 0; i < info->sector_count; i++) {
114 info->start[i] = base + (i * 0x8000);
115 info->protect[i] = 0;
117 info->start[i] = base +
118 (i * PHYS_FLASH_SECT_SIZE);
119 info->protect[i] = 0;
125 /*-----------------------------------------------------------------------
127 void flash_print_info (flash_info_t * info)
131 if (info->flash_id == FLASH_UNKNOWN) {
132 printf ("missing or unknown FLASH type\n");
136 switch (info->flash_id & FLASH_VENDMASK) {
137 case FLASH_MAN_INTEL:
141 printf ("Unknown Vendor ");
145 switch (info->flash_id & FLASH_TYPEMASK) {
146 case FLASH_28F256L18T:
147 printf ("FLASH 28F256L18T\n");
150 printf ("Unknown Chip Type\n");
154 printf (" Size: %ld MB in %d Sectors\n",
155 info->size >> 20, info->sector_count);
157 printf (" Sector Start Addresses:");
158 for (i = 0; i < info->sector_count; ++i) {
162 info->start[i], info->protect[i] ? " (RO)" : " ");
169 * The following code cannot be run from FLASH!
171 static ulong flash_get_size (FPW * addr, flash_info_t * info)
175 /* Write auto select command: read Manufacturer ID */
176 addr[0x5555] = (FPW) 0x00AA00AA;
177 addr[0x2AAA] = (FPW) 0x00550055;
178 addr[0x5555] = (FPW) 0x00900090;
185 case (FPW) INTEL_MANUFACT:
186 info->flash_id = FLASH_MAN_INTEL;
190 info->flash_id = FLASH_UNKNOWN;
191 info->sector_count = 0;
193 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
194 return (0); /* no or unknown flash */
198 value = addr[1]; /* device ID */
201 case (FPW) (INTEL_ID_28F256L18T):
202 info->flash_id += FLASH_28F256L18T;
203 info->sector_count = 259;
204 info->size = 0x02000000;
205 break; /* => 32 MB */
208 info->flash_id = FLASH_UNKNOWN;
212 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
213 printf ("** ERROR: sector count %d > max (%d) **\n",
214 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
215 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
218 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
224 /* unprotects a sector for write and erase
225 * on some intel parts, this unprotects the entire chip, but it
226 * wont hurt to call this additional times per sector...
228 void flash_unprotect_sectors (FPWV * addr)
230 #define PD_FINTEL_WSMS_READY_MASK 0x0080
232 *addr = (FPW) 0x00500050; /* clear status register */
234 /* this sends the clear lock bit command */
235 *addr = (FPW) 0x00600060;
236 *addr = (FPW) 0x00D000D0;
240 /*-----------------------------------------------------------------------
243 int flash_erase (flash_info_t * info, int s_first, int s_last)
245 int flag, prot, sect;
249 if ((s_first < 0) || (s_first > s_last)) {
250 if (info->flash_id == FLASH_UNKNOWN) {
251 printf ("- missing\n");
253 printf ("- no sectors to erase\n");
258 type = (info->flash_id & FLASH_VENDMASK);
259 if ((type != FLASH_MAN_INTEL)) {
260 printf ("Can't erase unknown flash type %08lx - aborted\n",
266 for (sect = s_first; sect <= s_last; ++sect) {
267 if (info->protect[sect]) {
273 printf ("- Warning: %d protected sectors will not be erased!\n",
279 /* Disable interrupts which might cause a timeout here */
280 flag = disable_interrupts ();
282 /* Start erase on unprotected sectors */
283 for (sect = s_first; sect <= s_last; sect++) {
284 if (info->protect[sect] == 0) { /* not protected */
285 FPWV *addr = (FPWV *) (info->start[sect]);
288 printf ("Erasing sector %2d ... ", sect);
290 flash_unprotect_sectors (addr);
292 /* arm simple, non interrupt dependent timer */
293 start = get_timer(0);
295 *addr = (FPW) 0x00500050;/* clear status register */
296 *addr = (FPW) 0x00200020;/* erase setup */
297 *addr = (FPW) 0x00D000D0;/* erase confirm */
300 *addr) & (FPW) 0x00800080) !=
302 if (get_timer(start) >
303 CONFIG_SYS_FLASH_ERASE_TOUT) {
304 printf ("Timeout\n");
306 *addr = (FPW) 0x00B000B0;
307 /* reset to read mode */
308 *addr = (FPW) 0x00FF00FF;
314 /* clear status register cmd. */
315 *addr = (FPW) 0x00500050;
316 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
327 /*-----------------------------------------------------------------------
328 * Copy memory to flash, returns:
331 * 2 - Flash not erased
332 * 4 - Flash not identified
335 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
339 int count, i, l, rc, port_width;
341 if (info->flash_id == FLASH_UNKNOWN) {
344 /* get lower word aligned address */
345 #ifdef FLASH_PORT_WIDTH16
354 * handle unaligned start bytes
356 if ((l = addr - wp) != 0) {
358 for (i = 0, cp = wp; i < l; ++i, ++cp) {
359 data = (data << 8) | (*(uchar *) cp);
361 for (; i < port_width && cnt > 0; ++i) {
362 data = (data << 8) | *src++;
366 for (; cnt == 0 && i < port_width; ++i, ++cp) {
367 data = (data << 8) | (*(uchar *) cp);
370 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
377 * handle word aligned part
380 while (cnt >= port_width) {
382 for (i = 0; i < port_width; ++i) {
383 data = (data << 8) | *src++;
385 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
390 if (count++ > 0x800) {
401 * handle unaligned tail bytes
404 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
405 data = (data << 8) | *src++;
408 for (; i < port_width; ++i, ++cp) {
409 data = (data << 8) | (*(uchar *) cp);
412 return (write_data (info, wp, SWAP (data)));
415 /*-----------------------------------------------------------------------
416 * Write a word or halfword to Flash, returns:
419 * 2 - Flash not erased
421 static int write_data (flash_info_t * info, ulong dest, FPW data)
423 FPWV *addr = (FPWV *) dest;
428 /* Check if Flash is (sufficiently) erased */
429 if ((*addr & data) != data) {
430 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
433 flash_unprotect_sectors (addr);
434 /* Disable interrupts which might cause a timeout here */
435 flag = disable_interrupts ();
436 *addr = (FPW) 0x00400040; /* write setup */
439 /* arm simple, non interrupt dependent timer */
440 start = get_timer(0);
442 /* wait while polling the status register */
443 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
444 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
450 *addr = (FPW)0x00FF00FF; /* restore read mode */
456 void inline spin_wheel (void)
459 static char w[] = "\\/-";
461 printf ("\010%c", w[p]);
462 (++p == 3) ? (p = 0) : 0;