blob: 56f981c47b4de9a840f5c1b887b5ca517df14ca2 [file] [log] [blame]
wdenka56bd922004-06-06 23:13: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 * (C) Copyright 2003
9 * Texas Instruments, <www.ti.com>
10 * Kshitij Gupta <Kshitij@ti.com>
11 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020012 * SPDX-License-Identifier: GPL-2.0+
wdenka56bd922004-06-06 23:13:55 +000013 */
14
15#include <common.h>
16#include <linux/byteorder/swab.h>
17
18#define PHYS_FLASH_SECT_SIZE 0x00020000 /* 256 KB sectors (x2) */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020019flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenka56bd922004-06-06 23:13:55 +000020
21/* Board support for 1 or 2 flash devices */
22#undef FLASH_PORT_WIDTH32
23#define FLASH_PORT_WIDTH16
24
25#ifdef FLASH_PORT_WIDTH16
26#define FLASH_PORT_WIDTH ushort
27#define FLASH_PORT_WIDTHV vu_short
28#define SWAP(x) __swab16(x)
29#else
30#define FLASH_PORT_WIDTH ulong
31#define FLASH_PORT_WIDTHV vu_long
32#define SWAP(x) __swab32(x)
33#endif
34
35#define FPW FLASH_PORT_WIDTH
36#define FPWV FLASH_PORT_WIDTHV
37
38#define mb() __asm__ __volatile__ ("" : : : "memory")
39
40
41/* Flash Organization Structure */
42typedef struct OrgDef {
43 unsigned int sector_number;
44 unsigned int sector_size;
45} OrgDef;
46
47
48/* Flash Organizations */
49OrgDef OrgIntel_28F256L18T[] = {
50 {4, 32 * 1024}, /* 4 * 32kBytes sectors */
51 {255, 128 * 1024}, /* 255 * 128kBytes sectors */
52};
53
54
55/*-----------------------------------------------------------------------
56 * Functions
57 */
58unsigned long flash_init (void);
59static ulong flash_get_size (FPW * addr, flash_info_t * info);
60static int write_data (flash_info_t * info, ulong dest, FPW data);
61static void flash_get_offsets (ulong base, flash_info_t * info);
62void inline spin_wheel (void);
63void flash_print_info (flash_info_t * info);
64void flash_unprotect_sectors (FPWV * addr);
65int flash_erase (flash_info_t * info, int s_first, int s_last);
66int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
67
68/*-----------------------------------------------------------------------
69 */
70
71unsigned long flash_init (void)
72{
73 int i;
74 ulong size = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020075 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenka56bd922004-06-06 23:13:55 +000076 switch (i) {
77 case 0:
78 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
79 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
80 break;
81 default:
82 panic ("configured too many flash banks!\n");
83 break;
84 }
85 size += flash_info[i].size;
86 }
87
88 /* Protect monitor and environment sectors
89 */
90 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020091 CONFIG_SYS_FLASH_BASE,
92 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
wdenka56bd922004-06-06 23:13:55 +000093
94 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020095 CONFIG_ENV_ADDR,
96 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
wdenka56bd922004-06-06 23:13:55 +000097
98 return size;
99}
100
101/*-----------------------------------------------------------------------
102 */
103static void flash_get_offsets (ulong base, flash_info_t * info)
104{
105 int i;
wdenka56bd922004-06-06 23:13:55 +0000106
wdenka56bd922004-06-06 23:13:55 +0000107 if (info->flash_id == FLASH_UNKNOWN) {
108 return;
109 }
110
111 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
112 for (i = 0; i < info->sector_count; i++) {
113 if (i > 255) {
114 info->start[i] = base + (i * 0x8000);
115 info->protect[i] = 0;
116 } else {
117 info->start[i] = base +
118 (i * PHYS_FLASH_SECT_SIZE);
119 info->protect[i] = 0;
120 }
121 }
122 }
123}
124
125/*-----------------------------------------------------------------------
126 */
127void flash_print_info (flash_info_t * info)
128{
129 int i;
130
131 if (info->flash_id == FLASH_UNKNOWN) {
132 printf ("missing or unknown FLASH type\n");
133 return;
134 }
135
136 switch (info->flash_id & FLASH_VENDMASK) {
137 case FLASH_MAN_INTEL:
138 printf ("INTEL ");
139 break;
140 default:
141 printf ("Unknown Vendor ");
142 break;
143 }
144
145 switch (info->flash_id & FLASH_TYPEMASK) {
146 case FLASH_28F256L18T:
147 printf ("FLASH 28F256L18T\n");
148 break;
149 default:
150 printf ("Unknown Chip Type\n");
151 break;
152 }
153
154 printf (" Size: %ld MB in %d Sectors\n",
155 info->size >> 20, info->sector_count);
156
157 printf (" Sector Start Addresses:");
158 for (i = 0; i < info->sector_count; ++i) {
159 if ((i % 5) == 0)
160 printf ("\n ");
161 printf (" %08lX%s",
162 info->start[i], info->protect[i] ? " (RO)" : " ");
163 }
164 printf ("\n");
165 return;
166}
167
168/*
169 * The following code cannot be run from FLASH!
170 */
171static ulong flash_get_size (FPW * addr, flash_info_t * info)
172{
173 volatile FPW value;
174
175 /* Write auto select command: read Manufacturer ID */
176 addr[0x5555] = (FPW) 0x00AA00AA;
177 addr[0x2AAA] = (FPW) 0x00550055;
178 addr[0x5555] = (FPW) 0x00900090;
179
180 mb ();
181 value = addr[0];
182
183 switch (value) {
184
185 case (FPW) INTEL_MANUFACT:
186 info->flash_id = FLASH_MAN_INTEL;
187 break;
188
189 default:
190 info->flash_id = FLASH_UNKNOWN;
191 info->sector_count = 0;
192 info->size = 0;
193 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
194 return (0); /* no or unknown flash */
195 }
196
197 mb ();
198 value = addr[1]; /* device ID */
199 switch (value) {
200
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 */
206
207 default:
208 info->flash_id = FLASH_UNKNOWN;
209 break;
210 }
211
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200212 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
wdenka56bd922004-06-06 23:13:55 +0000213 printf ("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200214 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
215 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenka56bd922004-06-06 23:13:55 +0000216 }
217
218 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
219
220 return (info->size);
221}
222
223
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...
227 */
228void flash_unprotect_sectors (FPWV * addr)
229{
230#define PD_FINTEL_WSMS_READY_MASK 0x0080
231
232 *addr = (FPW) 0x00500050; /* clear status register */
233
234 /* this sends the clear lock bit command */
235 *addr = (FPW) 0x00600060;
236 *addr = (FPW) 0x00D000D0;
237}
238
239
240/*-----------------------------------------------------------------------
241 */
242
243int flash_erase (flash_info_t * info, int s_first, int s_last)
244{
245 int flag, prot, sect;
Graeme Russa60d1e52011-07-15 23:31:37 +0000246 ulong type, start;
wdenka56bd922004-06-06 23:13:55 +0000247 int rcode = 0;
248
249 if ((s_first < 0) || (s_first > s_last)) {
250 if (info->flash_id == FLASH_UNKNOWN) {
251 printf ("- missing\n");
252 } else {
253 printf ("- no sectors to erase\n");
254 }
255 return 1;
256 }
257
258 type = (info->flash_id & FLASH_VENDMASK);
259 if ((type != FLASH_MAN_INTEL)) {
260 printf ("Can't erase unknown flash type %08lx - aborted\n",
261 info->flash_id);
262 return 1;
263 }
264
265 prot = 0;
266 for (sect = s_first; sect <= s_last; ++sect) {
267 if (info->protect[sect]) {
268 prot++;
269 }
270 }
271
272 if (prot) {
273 printf ("- Warning: %d protected sectors will not be erased!\n",
274 prot);
275 } else {
276 printf ("\n");
277 }
278
wdenka56bd922004-06-06 23:13:55 +0000279 /* Disable interrupts which might cause a timeout here */
280 flag = disable_interrupts ();
281
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]);
286 FPW status;
287
288 printf ("Erasing sector %2d ... ", sect);
289
290 flash_unprotect_sectors (addr);
291
292 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000293 start = get_timer(0);
wdenka56bd922004-06-06 23:13:55 +0000294
295 *addr = (FPW) 0x00500050;/* clear status register */
296 *addr = (FPW) 0x00200020;/* erase setup */
297 *addr = (FPW) 0x00D000D0;/* erase confirm */
298
299 while (((status =
300 *addr) & (FPW) 0x00800080) !=
301 (FPW) 0x00800080) {
Graeme Russa60d1e52011-07-15 23:31:37 +0000302 if (get_timer(start) >
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200303 CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenka56bd922004-06-06 23:13:55 +0000304 printf ("Timeout\n");
305 /* suspend erase */
306 *addr = (FPW) 0x00B000B0;
307 /* reset to read mode */
308 *addr = (FPW) 0x00FF00FF;
309 rcode = 1;
310 break;
311 }
312 }
313
314 /* clear status register cmd. */
315 *addr = (FPW) 0x00500050;
316 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
317 printf (" done\n");
318 }
319 }
Wolfgang Denkf23b7ea2011-12-09 12:14:31 +0100320
321 if (flag)
322 enable_interrupts();
323
wdenka56bd922004-06-06 23:13:55 +0000324 return rcode;
325}
326
327/*-----------------------------------------------------------------------
328 * Copy memory to flash, returns:
329 * 0 - OK
330 * 1 - write timeout
331 * 2 - Flash not erased
332 * 4 - Flash not identified
333 */
334
335int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
336{
337 ulong cp, wp;
338 FPW data;
339 int count, i, l, rc, port_width;
340
341 if (info->flash_id == FLASH_UNKNOWN) {
342 return 4;
343 }
344/* get lower word aligned address */
345#ifdef FLASH_PORT_WIDTH16
346 wp = (addr & ~1);
347 port_width = 2;
348#else
349 wp = (addr & ~3);
350 port_width = 4;
351#endif
352
353 /*
354 * handle unaligned start bytes
355 */
356 if ((l = addr - wp) != 0) {
357 data = 0;
358 for (i = 0, cp = wp; i < l; ++i, ++cp) {
359 data = (data << 8) | (*(uchar *) cp);
360 }
361 for (; i < port_width && cnt > 0; ++i) {
362 data = (data << 8) | *src++;
363 --cnt;
364 ++cp;
365 }
366 for (; cnt == 0 && i < port_width; ++i, ++cp) {
367 data = (data << 8) | (*(uchar *) cp);
368 }
369
370 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
371 return (rc);
372 }
373 wp += port_width;
374 }
375
376 /*
377 * handle word aligned part
378 */
379 count = 0;
380 while (cnt >= port_width) {
381 data = 0;
382 for (i = 0; i < port_width; ++i) {
383 data = (data << 8) | *src++;
384 }
385 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
386 return (rc);
387 }
388 wp += port_width;
389 cnt -= port_width;
390 if (count++ > 0x800) {
391 spin_wheel ();
392 count = 0;
393 }
394 }
395
396 if (cnt == 0) {
397 return (0);
398 }
399
400 /*
401 * handle unaligned tail bytes
402 */
403 data = 0;
404 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
405 data = (data << 8) | *src++;
406 --cnt;
407 }
408 for (; i < port_width; ++i, ++cp) {
409 data = (data << 8) | (*(uchar *) cp);
410 }
411
412 return (write_data (info, wp, SWAP (data)));
413}
414
415/*-----------------------------------------------------------------------
416 * Write a word or halfword to Flash, returns:
417 * 0 - OK
418 * 1 - write timeout
419 * 2 - Flash not erased
420 */
421static int write_data (flash_info_t * info, ulong dest, FPW data)
422{
423 FPWV *addr = (FPWV *) dest;
424 ulong status;
Wolfgang Denkf23b7ea2011-12-09 12:14:31 +0100425 int flag, rc = 0;
Graeme Russa60d1e52011-07-15 23:31:37 +0000426 ulong start;
wdenka56bd922004-06-06 23:13:55 +0000427
428 /* Check if Flash is (sufficiently) erased */
429 if ((*addr & data) != data) {
430 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
431 return (2);
432 }
433 flash_unprotect_sectors (addr);
434 /* Disable interrupts which might cause a timeout here */
435 flag = disable_interrupts ();
436 *addr = (FPW) 0x00400040; /* write setup */
437 *addr = data;
438
439 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000440 start = get_timer(0);
wdenka56bd922004-06-06 23:13:55 +0000441
442 /* wait while polling the status register */
443 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
Graeme Russa60d1e52011-07-15 23:31:37 +0000444 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Wolfgang Denkf23b7ea2011-12-09 12:14:31 +0100445 rc = 1;
446 goto done;
wdenka56bd922004-06-06 23:13:55 +0000447 }
448 }
Wolfgang Denkf23b7ea2011-12-09 12:14:31 +0100449done:
450 *addr = (FPW)0x00FF00FF; /* restore read mode */
451 if (flag)
452 enable_interrupts();
453 return rc;
wdenka56bd922004-06-06 23:13:55 +0000454}
455
456void inline spin_wheel (void)
457{
458 static int p = 0;
459 static char w[] = "\\/-";
460
461 printf ("\010%c", w[p]);
462 (++p == 3) ? (p = 0) : 0;
463}