blob: f1d9190eac4e255772852bf29f9e639d1b155156 [file] [log] [blame]
wdenk2d5b5612003-10-14 19:43:55 +00001/*
2 * (C) Copyright 2001
3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
5 * (C) Copyright 2001
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <linux/byteorder/swab.h>
29
30
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020031flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk2d5b5612003-10-14 19:43:55 +000032
33/* Board support for 1 or 2 flash devices */
34#undef FLASH_PORT_WIDTH32
35#define FLASH_PORT_WIDTH16
36
37#ifdef FLASH_PORT_WIDTH16
Wolfgang Denk53677ef2008-05-20 16:00:29 +020038#define FLASH_PORT_WIDTH ushort
39#define FLASH_PORT_WIDTHV vu_short
40#define SWAP(x) x
wdenk2d5b5612003-10-14 19:43:55 +000041#else
Wolfgang Denk53677ef2008-05-20 16:00:29 +020042#define FLASH_PORT_WIDTH ulong
43#define FLASH_PORT_WIDTHV vu_long
44#define SWAP(x) __swab32(x)
wdenk2d5b5612003-10-14 19:43:55 +000045#endif
46
Wolfgang Denk53677ef2008-05-20 16:00:29 +020047#define FPW FLASH_PORT_WIDTH
48#define FPWV FLASH_PORT_WIDTHV
wdenk2d5b5612003-10-14 19:43:55 +000049
Wolfgang Denk53677ef2008-05-20 16:00:29 +020050#define mb() __asm__ __volatile__ ("" : : : "memory")
wdenk2d5b5612003-10-14 19:43:55 +000051
52/*-----------------------------------------------------------------------
53 * Functions
54 */
55static ulong flash_get_size (FPW * addr, flash_info_t * info);
56static int write_data (flash_info_t * info, ulong dest, FPW data);
57static void flash_get_offsets (ulong base, flash_info_t * info);
58void inline spin_wheel (void);
59
60/*-----------------------------------------------------------------------
61 */
62
63unsigned long flash_init (void)
64{
65 int i;
66 ulong size = 0;
67
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenk2d5b5612003-10-14 19:43:55 +000069 switch (i) {
70 case 0:
71 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
72 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
73 break;
74 default:
wdenkf6e20fc2004-02-08 19:38:38 +000075 panic ("configured too many flash banks!\n");
wdenk2d5b5612003-10-14 19:43:55 +000076 break;
77 }
78 size += flash_info[i].size;
79 }
80
81 /* Protect monitor and environment sectors
82 */
83 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020084 CONFIG_SYS_FLASH_BASE,
85 CONFIG_SYS_FLASH_BASE + _bss_start - _armboot_start,
wdenk2d5b5612003-10-14 19:43:55 +000086 &flash_info[0]);
87
88 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020089 CONFIG_ENV_ADDR,
90 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
wdenk2d5b5612003-10-14 19:43:55 +000091
92 return size;
93}
94
95/*-----------------------------------------------------------------------
96 */
97static void flash_get_offsets (ulong base, flash_info_t * info)
98{
99 int i;
100
101 if (info->flash_id == FLASH_UNKNOWN) {
102 return;
103 }
104
105 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
106 for (i = 0; i < info->sector_count; i++) {
107 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
108 info->protect[i] = 0;
109 }
110 }
111}
112
113/*-----------------------------------------------------------------------
114 */
115void flash_print_info (flash_info_t * info)
116{
117 int i;
118
119 if (info->flash_id == FLASH_UNKNOWN) {
120 printf ("missing or unknown FLASH type\n");
121 return;
122 }
123
124 switch (info->flash_id & FLASH_VENDMASK) {
125 case FLASH_MAN_INTEL:
126 printf ("INTEL ");
127 break;
128 default:
129 printf ("Unknown Vendor ");
130 break;
131 }
132
133 switch (info->flash_id & FLASH_TYPEMASK) {
134 case FLASH_28F128J3A:
135 printf ("28F128J3A\n");
136 break;
137 default:
138 printf ("Unknown Chip Type\n");
139 break;
140 }
141
142 printf (" Size: %ld MB in %d Sectors\n",
143 info->size >> 20, info->sector_count);
144
145 printf (" Sector Start Addresses:");
146 for (i = 0; i < info->sector_count; ++i) {
147 if ((i % 5) == 0)
148 printf ("\n ");
149 printf (" %08lX%s",
150 info->start[i], info->protect[i] ? " (RO)" : " ");
151 }
152 printf ("\n");
153 return;
154}
155
156/*
157 * The following code cannot be run from FLASH!
158 */
159static ulong flash_get_size (FPW * addr, flash_info_t * info)
160{
161 volatile FPW value;
162
163 /* Write auto select command: read Manufacturer ID */
164 addr[0x5555] = (FPW) 0x00AA00AA;
165 addr[0x2AAA] = (FPW) 0x00550055;
166 addr[0x5555] = (FPW) 0x00900090;
167
168 mb ();
169 value = addr[0];
170
171 switch (value) {
172
173 case (FPW) INTEL_MANUFACT:
174 info->flash_id = FLASH_MAN_INTEL;
175 break;
176
177 default:
178 info->flash_id = FLASH_UNKNOWN;
179 info->sector_count = 0;
180 info->size = 0;
181 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
182 return (0); /* no or unknown flash */
183 }
184
185 mb ();
186 value = addr[1]; /* device ID */
187
188 switch (value) {
189
190 case (FPW) INTEL_ID_28F128J3A:
191 info->flash_id += FLASH_28F128J3A;
192 info->sector_count = 128;
193 info->size = 0x02000000;
194 break; /* => 16 MB */
195
196 default:
197 info->flash_id = FLASH_UNKNOWN;
198 break;
199 }
200
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200201 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
wdenk2d5b5612003-10-14 19:43:55 +0000202 printf ("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200203 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
204 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenk2d5b5612003-10-14 19:43:55 +0000205 }
206
207 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
208
209 return (info->size);
210}
211
212
213/*-----------------------------------------------------------------------
214 */
215
216int flash_erase (flash_info_t * info, int s_first, int s_last)
217{
218 int flag, prot, sect;
219 ulong type;
220 int rcode = 0;
221
222 if ((s_first < 0) || (s_first > s_last)) {
223 if (info->flash_id == FLASH_UNKNOWN) {
224 printf ("- missing\n");
225 } else {
226 printf ("- no sectors to erase\n");
227 }
228 return 1;
229 }
230
231 type = (info->flash_id & FLASH_VENDMASK);
232 if ((type != FLASH_MAN_INTEL)) {
233 printf ("Can't erase unknown flash type %08lx - aborted\n",
234 info->flash_id);
235 return 1;
236 }
237
238 prot = 0;
239 for (sect = s_first; sect <= s_last; ++sect) {
240 if (info->protect[sect]) {
241 prot++;
242 }
243 }
244
245 if (prot) {
246 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
247 } else {
248 printf ("\n");
249 }
250
251 /* Disable interrupts which might cause a timeout here */
252 flag = disable_interrupts ();
253
254 /* Start erase on unprotected sectors */
255 for (sect = s_first; sect <= s_last; sect++) {
256 if (info->protect[sect] == 0) { /* not protected */
257 FPWV *addr = (FPWV *) (info->start[sect]);
258 FPW status;
259
260 printf ("Erasing sector %2d ... ", sect);
261
262 /* arm simple, non interrupt dependent timer */
263 reset_timer_masked ();
264
265 *addr = (FPW) 0x00500050; /* clear status register */
266 *addr = (FPW) 0x00200020; /* erase setup */
267 *addr = (FPW) 0x00D000D0; /* erase confirm */
268
269 while (((status =
270 *addr) & (FPW) 0x00800080) !=
271 (FPW) 0x00800080) {
272 if (get_timer_masked () >
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200273 CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk2d5b5612003-10-14 19:43:55 +0000274 printf ("Timeout\n");
275 *addr = (FPW) 0x00B000B0; /* suspend erase */
276 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
277 rcode = 1;
278 break;
279 }
280 }
281
282 *addr = (FPW) 0x00500050; /* clear status register cmd. */
283 *addr = (FPW) 0x00FF00FF; /* resest to read mode */
284
285 printf (" done\n");
286 }
287 }
288 return rcode;
289}
290
291/*-----------------------------------------------------------------------
292 * Copy memory to flash, returns:
293 * 0 - OK
294 * 1 - write timeout
295 * 2 - Flash not erased
296 * 4 - Flash not identified
297 */
298
299int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
300{
301 ulong cp, wp;
302 FPW data;
303 int count, i, l, rc, port_width;
304
305 if (info->flash_id == FLASH_UNKNOWN) {
306 return 4;
307 }
308
309/* get lower word aligned address */
310#ifdef FLASH_PORT_WIDTH16
311 wp = (addr & ~1);
312 port_width = 2;
313#else
314 wp = (addr & ~3);
315 port_width = 4;
316#endif
317
318 /*
319 * handle unaligned start bytes
320 */
321 if ((l = addr - wp) != 0) {
322 data = 0;
323 for (i = 0, cp = wp; i < l; ++i, ++cp) {
324 data = (data << 8) | (*(uchar *) cp);
325 }
326 for (; i < port_width && cnt > 0; ++i) {
327 data = (data << 8) | *src++;
328 --cnt;
329 ++cp;
330 }
331 for (; cnt == 0 && i < port_width; ++i, ++cp) {
332 data = (data << 8) | (*(uchar *) cp);
333 }
334
335 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
336 return (rc);
337 }
338 wp += port_width;
339 }
340
341 /*
342 * handle word aligned part
343 */
344 count = 0;
345 while (cnt >= port_width) {
346 data = 0;
347 for (i = 0; i < port_width; ++i) {
348 data = (data << 8) | *src++;
349 }
350 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
351 return (rc);
352 }
353 wp += port_width;
354 cnt -= port_width;
355 if (count++ > 0x800) {
356 spin_wheel ();
357 count = 0;
358 }
359 }
360
361 if (cnt == 0) {
362 return (0);
363 }
364
365 /*
366 * handle unaligned tail bytes
367 */
368 data = 0;
369 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
370 data = (data << 8) | *src++;
371 --cnt;
372 }
373 for (; i < port_width; ++i, ++cp) {
374 data = (data << 8) | (*(uchar *) cp);
375 }
376
377 return (write_data (info, wp, SWAP (data)));
378}
379
380/*-----------------------------------------------------------------------
381 * Write a word or halfword to Flash, returns:
382 * 0 - OK
383 * 1 - write timeout
384 * 2 - Flash not erased
385 */
386static int write_data (flash_info_t * info, ulong dest, FPW data)
387{
388 FPWV *addr = (FPWV *) dest;
389 ulong status;
390 int flag;
391
392 /* Check if Flash is (sufficiently) erased */
393 if ((*addr & data) != data) {
394 printf ("not erased at %08lx (%lx)\n", (ulong) addr,
395 (ulong) * addr);
396 return (2);
397 }
398 /* Disable interrupts which might cause a timeout here */
399 flag = disable_interrupts ();
400
401 *addr = (FPW) 0x00400040; /* write setup */
402 *addr = data;
403
404 /* arm simple, non interrupt dependent timer */
405 reset_timer_masked ();
406
407 /* wait while polling the status register */
408 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200409 if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk2d5b5612003-10-14 19:43:55 +0000410 *addr = (FPW) 0x00FF00FF; /* restore read mode */
411 return (1);
412 }
413 }
414
415 *addr = (FPW) 0x00FF00FF; /* restore read mode */
416
417 return (0);
418}
419
420void inline spin_wheel (void)
421{
422 static int p = 0;
423 static char w[] = "\\/-";
424
425 printf ("\010%c", w[p]);
426 (++p == 3) ? (p = 0) : 0;
427}