blob: f6bb22c2e472ba561ba55be7cb478ba223d8e203 [file] [log] [blame]
wdenkea8015b2002-10-26 16:43:06 +00001/*
wdenkdb2f721f2003-03-06 00:58:30 +00002 * (C) Copyright 2001
wdenkea8015b2002-10-26 16:43:06 +00003 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
wdenkdb2f721f2003-03-06 00:58:30 +00005 * (C) Copyright 2001
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkea8015b2002-10-26 16:43:06 +00007 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkea8015b2002-10-26 16:43:06 +00009 */
10
11#include <common.h>
wdenkdb2f721f2003-03-06 00:58:30 +000012#include <linux/byteorder/swab.h>
wdenkea8015b2002-10-26 16:43:06 +000013
wdenkea8015b2002-10-26 16:43:06 +000014
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020015flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkea8015b2002-10-26 16:43:06 +000016
wdenkdb2f721f2003-03-06 00:58:30 +000017/* Board support for 1 or 2 flash devices */
18#define FLASH_PORT_WIDTH32
19#undef FLASH_PORT_WIDTH16
20
21#ifdef FLASH_PORT_WIDTH16
22#define FLASH_PORT_WIDTH ushort
23#define FLASH_PORT_WIDTHV vu_short
24#define SWAP(x) __swab16(x)
25#else
26#define FLASH_PORT_WIDTH ulong
27#define FLASH_PORT_WIDTHV vu_long
28#define SWAP(x) __swab32(x)
29#endif
30
31#define FPW FLASH_PORT_WIDTH
32#define FPWV FLASH_PORT_WIDTHV
33
34#define mb() __asm__ __volatile__ ("" : : : "memory")
35
36/*-----------------------------------------------------------------------
37 * Functions
38 */
39static ulong flash_get_size (FPW *addr, flash_info_t *info);
wdenk06d01db2003-03-14 20:47:52 +000040static int write_data (flash_info_t *info, ulong dest, FPW data);
41static void flash_get_offsets (ulong base, flash_info_t *info);
42void inline spin_wheel (void);
wdenkea8015b2002-10-26 16:43:06 +000043
44/*-----------------------------------------------------------------------
45 */
46
wdenkdb2f721f2003-03-06 00:58:30 +000047unsigned long flash_init (void)
wdenkea8015b2002-10-26 16:43:06 +000048{
wdenk06d01db2003-03-14 20:47:52 +000049 int i;
50 ulong size = 0;
wdenkea8015b2002-10-26 16:43:06 +000051
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenk06d01db2003-03-14 20:47:52 +000053 switch (i) {
54 case 0:
55 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
56 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
57 break;
58 case 1:
59 flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
60 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
61 break;
62 default:
wdenk5f535fe2003-09-18 09:21:33 +000063 panic ("configured too many flash banks!\n");
wdenk06d01db2003-03-14 20:47:52 +000064 break;
65 }
66 size += flash_info[i].size;
67 }
wdenkea8015b2002-10-26 16:43:06 +000068
wdenk06d01db2003-03-14 20:47:52 +000069 /* Protect monitor and environment sectors
70 */
71 flash_protect ( FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020072 CONFIG_SYS_FLASH_BASE,
73 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
wdenk06d01db2003-03-14 20:47:52 +000074 &flash_info[0] );
wdenkea8015b2002-10-26 16:43:06 +000075
wdenk06d01db2003-03-14 20:47:52 +000076 flash_protect ( FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020077 CONFIG_ENV_ADDR,
78 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
wdenkea8015b2002-10-26 16:43:06 +000079
wdenk06d01db2003-03-14 20:47:52 +000080 return size;
wdenkea8015b2002-10-26 16:43:06 +000081}
82
83/*-----------------------------------------------------------------------
84 */
wdenkdb2f721f2003-03-06 00:58:30 +000085static void flash_get_offsets (ulong base, flash_info_t *info)
86{
87 int i;
88
89 if (info->flash_id == FLASH_UNKNOWN) {
90 return;
91 }
92
93 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
94 for (i = 0; i < info->sector_count; i++) {
95 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
96 info->protect[i] = 0;
97 }
98 }
99}
100
101/*-----------------------------------------------------------------------
102 */
wdenk06d01db2003-03-14 20:47:52 +0000103void flash_print_info (flash_info_t *info)
wdenkea8015b2002-10-26 16:43:06 +0000104{
wdenkdb2f721f2003-03-06 00:58:30 +0000105 int i;
wdenkea8015b2002-10-26 16:43:06 +0000106
wdenkdb2f721f2003-03-06 00:58:30 +0000107 if (info->flash_id == FLASH_UNKNOWN) {
108 printf ("missing or unknown FLASH type\n");
109 return;
wdenk06d01db2003-03-14 20:47:52 +0000110 }
wdenkea8015b2002-10-26 16:43:06 +0000111
wdenkdb2f721f2003-03-06 00:58:30 +0000112 switch (info->flash_id & FLASH_VENDMASK) {
wdenk06d01db2003-03-14 20:47:52 +0000113 case FLASH_MAN_INTEL:
114 printf ("INTEL ");
115 break;
116 default:
117 printf ("Unknown Vendor ");
118 break;
wdenkdb2f721f2003-03-06 00:58:30 +0000119 }
120
121 switch (info->flash_id & FLASH_TYPEMASK) {
wdenk06d01db2003-03-14 20:47:52 +0000122 case FLASH_28F128J3A:
123 printf ("28F128J3A\n");
124 break;
125 default:
126 printf ("Unknown Chip Type\n");
127 break;
128 }
wdenkea8015b2002-10-26 16:43:06 +0000129
wdenkdb2f721f2003-03-06 00:58:30 +0000130 printf (" Size: %ld MB in %d Sectors\n",
wdenk06d01db2003-03-14 20:47:52 +0000131 info->size >> 20, info->sector_count);
wdenkea8015b2002-10-26 16:43:06 +0000132
wdenkdb2f721f2003-03-06 00:58:30 +0000133 printf (" Sector Start Addresses:");
wdenk06d01db2003-03-14 20:47:52 +0000134 for (i = 0; i < info->sector_count; ++i) {
135 if ((i % 5) == 0)
136 printf ("\n ");
wdenkdb2f721f2003-03-06 00:58:30 +0000137 printf (" %08lX%s",
138 info->start[i],
wdenk06d01db2003-03-14 20:47:52 +0000139 info->protect[i] ? " (RO)" : " ");
140 }
141 printf ("\n");
wdenkdb2f721f2003-03-06 00:58:30 +0000142 return;
143}
144
145/*
146 * The following code cannot be run from FLASH!
147 */
148static ulong flash_get_size (FPW *addr, flash_info_t *info)
149{
150 volatile FPW value;
151
152 /* Write auto select command: read Manufacturer ID */
wdenk06d01db2003-03-14 20:47:52 +0000153 addr[0x5555] = (FPW) 0x00AA00AA;
154 addr[0x2AAA] = (FPW) 0x00550055;
155 addr[0x5555] = (FPW) 0x00900090;
wdenkdb2f721f2003-03-06 00:58:30 +0000156
wdenk06d01db2003-03-14 20:47:52 +0000157 mb ();
wdenkdb2f721f2003-03-06 00:58:30 +0000158 value = addr[0];
159
wdenk06d01db2003-03-14 20:47:52 +0000160 switch (value) {
wdenkdb2f721f2003-03-06 00:58:30 +0000161
wdenk06d01db2003-03-14 20:47:52 +0000162 case (FPW) INTEL_MANUFACT:
163 info->flash_id = FLASH_MAN_INTEL;
164 break;
wdenkdb2f721f2003-03-06 00:58:30 +0000165
166 default:
167 info->flash_id = FLASH_UNKNOWN;
168 info->sector_count = 0;
169 info->size = 0;
wdenk06d01db2003-03-14 20:47:52 +0000170 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
171 return (0); /* no or unknown flash */
wdenkdb2f721f2003-03-06 00:58:30 +0000172 }
173
wdenk06d01db2003-03-14 20:47:52 +0000174 mb ();
175 value = addr[1]; /* device ID */
wdenkdb2f721f2003-03-06 00:58:30 +0000176
wdenk06d01db2003-03-14 20:47:52 +0000177 switch (value) {
wdenkdb2f721f2003-03-06 00:58:30 +0000178
wdenk06d01db2003-03-14 20:47:52 +0000179 case (FPW) INTEL_ID_28F128J3A:
180 info->flash_id += FLASH_28F128J3A;
181 info->sector_count = 128;
182 info->size = 0x02000000;
183 break; /* => 16 MB */
wdenkdb2f721f2003-03-06 00:58:30 +0000184
185 default:
186 info->flash_id = FLASH_UNKNOWN;
187 break;
188 }
189
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200190 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
wdenkdb2f721f2003-03-06 00:58:30 +0000191 printf ("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200192 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
193 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenk06d01db2003-03-14 20:47:52 +0000194 }
wdenkea8015b2002-10-26 16:43:06 +0000195
wdenk06d01db2003-03-14 20:47:52 +0000196 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
wdenkdb2f721f2003-03-06 00:58:30 +0000197
198 return (info->size);
wdenkea8015b2002-10-26 16:43:06 +0000199}
200
wdenkdb2f721f2003-03-06 00:58:30 +0000201
wdenkea8015b2002-10-26 16:43:06 +0000202/*-----------------------------------------------------------------------
203 */
204
wdenk06d01db2003-03-14 20:47:52 +0000205int flash_erase (flash_info_t *info, int s_first, int s_last)
wdenkea8015b2002-10-26 16:43:06 +0000206{
Anatolij Gustschin90729c02011-11-19 13:12:15 +0000207 int prot, sect;
Graeme Russa60d1e52011-07-15 23:31:37 +0000208 ulong type, start;
wdenkdb2f721f2003-03-06 00:58:30 +0000209 int rcode = 0;
wdenkea8015b2002-10-26 16:43:06 +0000210
wdenk06d01db2003-03-14 20:47:52 +0000211 if ((s_first < 0) || (s_first > s_last)) {
wdenkdb2f721f2003-03-06 00:58:30 +0000212 if (info->flash_id == FLASH_UNKNOWN) {
213 printf ("- missing\n");
214 } else {
215 printf ("- no sectors to erase\n");
216 }
217 return 1;
wdenk06d01db2003-03-14 20:47:52 +0000218 }
wdenkea8015b2002-10-26 16:43:06 +0000219
wdenkdb2f721f2003-03-06 00:58:30 +0000220 type = (info->flash_id & FLASH_VENDMASK);
221 if ((type != FLASH_MAN_INTEL)) {
222 printf ("Can't erase unknown flash type %08lx - aborted\n",
223 info->flash_id);
224 return 1;
wdenkea8015b2002-10-26 16:43:06 +0000225 }
wdenk06d01db2003-03-14 20:47:52 +0000226
227 prot = 0;
228 for (sect = s_first; sect <= s_last; ++sect) {
229 if (info->protect[sect]) {
230 prot++;
231 }
232 }
wdenkea8015b2002-10-26 16:43:06 +0000233
wdenkdb2f721f2003-03-06 00:58:30 +0000234 if (prot) {
235 printf ("- Warning: %d protected sectors will not be erased!\n",
236 prot);
237 } else {
238 printf ("\n");
239 }
240
wdenk06d01db2003-03-14 20:47:52 +0000241 /* Disable interrupts which might cause a timeout here */
Anatolij Gustschin90729c02011-11-19 13:12:15 +0000242 disable_interrupts();
wdenkea8015b2002-10-26 16:43:06 +0000243
wdenk06d01db2003-03-14 20:47:52 +0000244 /* Start erase on unprotected sectors */
245 for (sect = s_first; sect <= s_last; sect++) {
wdenkdb2f721f2003-03-06 00:58:30 +0000246 if (info->protect[sect] == 0) { /* not protected */
wdenk06d01db2003-03-14 20:47:52 +0000247 FPWV *addr = (FPWV *) (info->start[sect]);
wdenkdb2f721f2003-03-06 00:58:30 +0000248 FPW status;
wdenkea8015b2002-10-26 16:43:06 +0000249
wdenk06d01db2003-03-14 20:47:52 +0000250 printf ("Erasing sector %2d ... ", sect);
wdenkea8015b2002-10-26 16:43:06 +0000251
wdenk06d01db2003-03-14 20:47:52 +0000252 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000253 start = get_timer(0);
wdenkea8015b2002-10-26 16:43:06 +0000254
wdenk06d01db2003-03-14 20:47:52 +0000255 *addr = (FPW) 0x00500050; /* clear status register */
256 *addr = (FPW) 0x00200020; /* erase setup */
257 *addr = (FPW) 0x00D000D0; /* erase confirm */
wdenkea8015b2002-10-26 16:43:06 +0000258
wdenk06d01db2003-03-14 20:47:52 +0000259 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
Graeme Russa60d1e52011-07-15 23:31:37 +0000260 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenkdb2f721f2003-03-06 00:58:30 +0000261 printf ("Timeout\n");
wdenk06d01db2003-03-14 20:47:52 +0000262 *addr = (FPW) 0x00B000B0; /* suspend erase */
263 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
wdenkdb2f721f2003-03-06 00:58:30 +0000264 rcode = 1;
265 break;
wdenk06d01db2003-03-14 20:47:52 +0000266 }
267 }
wdenkea8015b2002-10-26 16:43:06 +0000268
wdenk06d01db2003-03-14 20:47:52 +0000269 *addr = 0x00500050; /* clear status register cmd. */
270 *addr = 0x00FF00FF; /* resest to read mode */
wdenkea8015b2002-10-26 16:43:06 +0000271
wdenkdb2f721f2003-03-06 00:58:30 +0000272 printf (" done\n");
wdenk06d01db2003-03-14 20:47:52 +0000273 }
274 }
wdenkdb2f721f2003-03-06 00:58:30 +0000275 return rcode;
wdenkea8015b2002-10-26 16:43:06 +0000276}
277
278/*-----------------------------------------------------------------------
wdenkdb2f721f2003-03-06 00:58:30 +0000279 * Copy memory to flash, returns:
280 * 0 - OK
281 * 1 - write timeout
282 * 2 - Flash not erased
283 * 4 - Flash not identified
wdenkea8015b2002-10-26 16:43:06 +0000284 */
285
286int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
287{
wdenk06d01db2003-03-14 20:47:52 +0000288 ulong cp, wp;
wdenkdb2f721f2003-03-06 00:58:30 +0000289 FPW data;
290 int count, i, l, rc, port_width;
wdenkea8015b2002-10-26 16:43:06 +0000291
wdenkdb2f721f2003-03-06 00:58:30 +0000292 if (info->flash_id == FLASH_UNKNOWN) {
293 return 4;
294 }
295/* get lower word aligned address */
296#ifdef FLASH_PORT_WIDTH16
297 wp = (addr & ~1);
298 port_width = 2;
299#else
300 wp = (addr & ~3);
301 port_width = 4;
302#endif
wdenkea8015b2002-10-26 16:43:06 +0000303
wdenk06d01db2003-03-14 20:47:52 +0000304 /*
305 * handle unaligned start bytes
306 */
307 if ((l = addr - wp) != 0) {
308 data = 0;
309 for (i = 0, cp = wp; i < l; ++i, ++cp) {
310 data = (data << 8) | (*(uchar *) cp);
311 }
312 for (; i < port_width && cnt > 0; ++i) {
wdenkdb2f721f2003-03-06 00:58:30 +0000313 data = (data << 8) | *src++;
wdenk06d01db2003-03-14 20:47:52 +0000314 --cnt;
315 ++cp;
316 }
317 for (; cnt == 0 && i < port_width; ++i, ++cp) {
318 data = (data << 8) | (*(uchar *) cp);
319 }
wdenkea8015b2002-10-26 16:43:06 +0000320
wdenk06d01db2003-03-14 20:47:52 +0000321 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
322 return (rc);
323 }
wdenkdb2f721f2003-03-06 00:58:30 +0000324 wp += port_width;
wdenk06d01db2003-03-14 20:47:52 +0000325 }
wdenkea8015b2002-10-26 16:43:06 +0000326
wdenk06d01db2003-03-14 20:47:52 +0000327 /*
328 * handle word aligned part
329 */
wdenkdb2f721f2003-03-06 00:58:30 +0000330 count = 0;
331 while (cnt >= port_width) {
332 data = 0;
wdenk06d01db2003-03-14 20:47:52 +0000333 for (i = 0; i < port_width; ++i) {
wdenkdb2f721f2003-03-06 00:58:30 +0000334 data = (data << 8) | *src++;
335 }
wdenk06d01db2003-03-14 20:47:52 +0000336 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
337 return (rc);
338 }
339 wp += port_width;
wdenkdb2f721f2003-03-06 00:58:30 +0000340 cnt -= port_width;
wdenk06d01db2003-03-14 20:47:52 +0000341 if (count++ > 0x800) {
342 spin_wheel ();
wdenkdb2f721f2003-03-06 00:58:30 +0000343 count = 0;
344 }
wdenkdb2f721f2003-03-06 00:58:30 +0000345 }
346
wdenk06d01db2003-03-14 20:47:52 +0000347 if (cnt == 0) {
348 return (0);
349 }
350
351 /*
352 * handle unaligned tail bytes
353 */
354 data = 0;
355 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
356 data = (data << 8) | *src++;
357 --cnt;
358 }
359 for (; i < port_width; ++i, ++cp) {
360 data = (data << 8) | (*(uchar *) cp);
361 }
362
363 return (write_data (info, wp, SWAP (data)));
wdenkdb2f721f2003-03-06 00:58:30 +0000364}
365
366/*-----------------------------------------------------------------------
367 * Write a word or halfword to Flash, returns:
368 * 0 - OK
369 * 1 - write timeout
370 * 2 - Flash not erased
371 */
372static int write_data (flash_info_t *info, ulong dest, FPW data)
373{
wdenk06d01db2003-03-14 20:47:52 +0000374 FPWV *addr = (FPWV *) dest;
wdenkdb2f721f2003-03-06 00:58:30 +0000375 ulong status;
Graeme Russa60d1e52011-07-15 23:31:37 +0000376 ulong start;
wdenkdb2f721f2003-03-06 00:58:30 +0000377
378 /* Check if Flash is (sufficiently) erased */
379 if ((*addr & data) != data) {
wdenk06d01db2003-03-14 20:47:52 +0000380 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
wdenkdb2f721f2003-03-06 00:58:30 +0000381 return (2);
382 }
383 /* Disable interrupts which might cause a timeout here */
Anatolij Gustschin90729c02011-11-19 13:12:15 +0000384 disable_interrupts();
wdenkdb2f721f2003-03-06 00:58:30 +0000385
wdenk06d01db2003-03-14 20:47:52 +0000386 *addr = (FPW) 0x00400040; /* write setup */
wdenkdb2f721f2003-03-06 00:58:30 +0000387 *addr = data;
388
389 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000390 start = get_timer(0);
wdenkdb2f721f2003-03-06 00:58:30 +0000391
392 /* wait while polling the status register */
wdenk06d01db2003-03-14 20:47:52 +0000393 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
Marek Vasutc4f4c762011-08-20 14:24:49 +0200394 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk06d01db2003-03-14 20:47:52 +0000395 *addr = (FPW) 0x00FF00FF; /* restore read mode */
wdenkdb2f721f2003-03-06 00:58:30 +0000396 return (1);
397 }
wdenk06d01db2003-03-14 20:47:52 +0000398 }
wdenkea8015b2002-10-26 16:43:06 +0000399
wdenk06d01db2003-03-14 20:47:52 +0000400 *addr = (FPW) 0x00FF00FF; /* restore read mode */
wdenkdb2f721f2003-03-06 00:58:30 +0000401
402 return (0);
wdenkea8015b2002-10-26 16:43:06 +0000403}
wdenkdb2f721f2003-03-06 00:58:30 +0000404
wdenk06d01db2003-03-14 20:47:52 +0000405void inline spin_wheel (void)
wdenkdb2f721f2003-03-06 00:58:30 +0000406{
wdenk06d01db2003-03-14 20:47:52 +0000407 static int p = 0;
408 static char w[] = "\\/-";
wdenkdb2f721f2003-03-06 00:58:30 +0000409
wdenk06d01db2003-03-14 20:47:52 +0000410 printf ("\010%c", w[p]);
411 (++p == 3) ? (p = 0) : 0;
wdenkdb2f721f2003-03-06 00:58:30 +0000412}