blob: 0faf1970421a6b2a9633602d43783dc4024d0c9a [file] [log] [blame]
wdenk62cb5cf2005-05-19 22:46:33 +00001/*
2 * (C) Copyright 2005
3 * Greg Ungerer, OpenGear Inc, greg.ungerer@opengear.com
4 *
5 * (C) Copyright 2001
6 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
7 *
8 * (C) Copyright 2001
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 *
11 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 */
29
30#include <common.h>
31#include <linux/byteorder/swab.h>
Simon Glasse0e641e2013-03-05 14:39:38 +000032#include <asm/sections.h>
wdenk62cb5cf2005-05-19 22:46:33 +000033
34
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020035flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk62cb5cf2005-05-19 22:46:33 +000036
37#define mb() __asm__ __volatile__ ("" : : : "memory")
38
39/*-----------------------------------------------------------------------
40 * Functions
41 */
42static ulong flash_get_size (unsigned char * addr, flash_info_t * info);
43static int write_data (flash_info_t * info, ulong dest, unsigned char data);
44static void flash_get_offsets (ulong base, flash_info_t * info);
45void inline spin_wheel (void);
46
47/*-----------------------------------------------------------------------
48 */
49
50unsigned long flash_init (void)
51{
52 int i;
53 ulong size = 0;
54
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020055 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenk62cb5cf2005-05-19 22:46:33 +000056 switch (i) {
57 case 0:
58 flash_get_size ((unsigned char *) PHYS_FLASH_1, &flash_info[i]);
59 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
60 break;
61 case 1:
62 /* ignore for now */
63 flash_info[i].flash_id = FLASH_UNKNOWN;
64 break;
65 default:
66 panic ("configured too many flash banks!\n");
67 break;
68 }
69 size += flash_info[i].size;
70 }
71
72 /* Protect monitor and environment sectors
73 */
74 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075 CONFIG_SYS_FLASH_BASE,
Greg Ungerer48cfffd2011-09-09 22:23:34 +100076 CONFIG_SYS_FLASH_BASE + _bss_start_ofs,
wdenk62cb5cf2005-05-19 22:46:33 +000077 &flash_info[0]);
78
79 return size;
80}
81
82/*-----------------------------------------------------------------------
83 */
84static void flash_get_offsets (ulong base, flash_info_t * info)
85{
86 int i;
87
88 if (info->flash_id == FLASH_UNKNOWN)
89 return;
90
91 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
92 for (i = 0; i < info->sector_count; i++) {
93 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
94 info->protect[i] = 0;
95 }
96 }
97}
98
99/*-----------------------------------------------------------------------
100 */
101void flash_print_info (flash_info_t * info)
102{
103 int i;
104
105 if (info->flash_id == FLASH_UNKNOWN) {
106 printf ("missing or unknown FLASH type\n");
107 return;
108 }
109
110 switch (info->flash_id & FLASH_VENDMASK) {
111 case FLASH_MAN_INTEL:
112 printf ("INTEL ");
113 break;
114 default:
115 printf ("Unknown Vendor ");
116 break;
117 }
118
119 switch (info->flash_id & FLASH_TYPEMASK) {
120 case FLASH_28F128J3A:
121 printf ("28F128J3A\n");
122 break;
123 default:
124 printf ("Unknown Chip Type\n");
125 break;
126 }
127
128 printf (" Size: %ld MB in %d Sectors\n",
129 info->size >> 20, info->sector_count);
130
131 printf (" Sector Start Addresses:");
132 for (i = 0; i < info->sector_count; ++i) {
133 if ((i % 5) == 0)
134 printf ("\n ");
135 printf (" %08lX%s",
136 info->start[i], info->protect[i] ? " (RO)" : " ");
137 }
138 printf ("\n");
139 return;
140}
141
142/*
143 * The following code cannot be run from FLASH!
144 */
145static ulong flash_get_size (unsigned char * addr, flash_info_t * info)
146{
147 volatile unsigned char value;
148
149 /* Write auto select command: read Manufacturer ID */
150 addr[0x5555] = 0xAA;
151 addr[0x2AAA] = 0x55;
152 addr[0x5555] = 0x90;
153
154 mb ();
155 value = addr[0];
156
157 switch (value) {
158
159 case (unsigned char)INTEL_MANUFACT:
160 info->flash_id = FLASH_MAN_INTEL;
161 break;
162
163 default:
164 info->flash_id = FLASH_UNKNOWN;
165 info->sector_count = 0;
166 info->size = 0;
167 addr[0] = 0xFF; /* restore read mode */
168 return (0); /* no or unknown flash */
169 }
170
171 mb ();
172 value = addr[2]; /* device ID */
173
174 switch (value) {
175
176 case (unsigned char)INTEL_ID_28F640J3A:
177 info->flash_id += FLASH_28F640J3A;
178 info->sector_count = 64;
179 info->size = 0x00800000;
180 break; /* => 8 MB */
181
182 case (unsigned char)INTEL_ID_28F128J3A:
183 info->flash_id += FLASH_28F128J3A;
184 info->sector_count = 128;
185 info->size = 0x01000000;
186 break; /* => 16 MB */
187
188 default:
189 info->flash_id = FLASH_UNKNOWN;
190 break;
191 }
192
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200193 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
wdenk62cb5cf2005-05-19 22:46:33 +0000194 printf ("** ERROR: sector count %d > max (%d) **\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200195 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
196 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenk62cb5cf2005-05-19 22:46:33 +0000197 }
198
199 addr[0] = 0xFF; /* restore read mode */
200
201 return (info->size);
202}
203
204
205/*-----------------------------------------------------------------------
206 */
207
208int flash_erase (flash_info_t * info, int s_first, int s_last)
209{
Anatolij Gustschindf7e62d2011-11-15 13:20:56 +0000210 int prot, sect;
wdenk62cb5cf2005-05-19 22:46:33 +0000211 ulong type;
212 int rcode = 0;
Graeme Russf8b82ee2011-07-15 23:31:37 +0000213 ulong start;
wdenk62cb5cf2005-05-19 22:46:33 +0000214
215 if ((s_first < 0) || (s_first > s_last)) {
216 if (info->flash_id == FLASH_UNKNOWN) {
217 printf ("- missing\n");
218 } else {
219 printf ("- no sectors to erase\n");
220 }
221 return 1;
222 }
223
224 type = (info->flash_id & FLASH_VENDMASK);
225 if ((type != FLASH_MAN_INTEL)) {
226 printf ("Can't erase unknown flash type %08lx - aborted\n",
227 info->flash_id);
228 return 1;
229 }
230
231 prot = 0;
232 for (sect = s_first; sect <= s_last; ++sect) {
233 if (info->protect[sect]) {
234 prot++;
235 }
236 }
237
238 if (prot)
239 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
240 else
241 printf ("\n");
242
243 /* Disable interrupts which might cause a timeout here */
Anatolij Gustschindf7e62d2011-11-15 13:20:56 +0000244 disable_interrupts();
wdenk62cb5cf2005-05-19 22:46:33 +0000245
246 /* Start erase on unprotected sectors */
247 for (sect = s_first; sect <= s_last; sect++) {
248 if (info->protect[sect] == 0) { /* not protected */
249 volatile unsigned char *addr;
250 unsigned char status;
251
252 printf ("Erasing sector %2d ... ", sect);
253
254 /* arm simple, non interrupt dependent timer */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000255 start = get_timer(0);
wdenk62cb5cf2005-05-19 22:46:33 +0000256
257 addr = (volatile unsigned char *) (info->start[sect]);
258 *addr = 0x50; /* clear status register */
259 *addr = 0x20; /* erase setup */
260 *addr = 0xD0; /* erase confirm */
261
262 while (((status = *addr) & 0x80) != 0x80) {
Graeme Russf8b82ee2011-07-15 23:31:37 +0000263 if (get_timer(start) >
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200264 CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk62cb5cf2005-05-19 22:46:33 +0000265 printf ("Timeout\n");
266 *addr = 0xB0; /* suspend erase */
267 *addr = 0xFF; /* reset to read mode */
268 rcode = 1;
269 break;
270 }
271 }
272
273 *addr = 0x50; /* clear status register cmd */
274 *addr = 0xFF; /* resest to read mode */
275
276 printf (" done\n");
277 }
278 }
279 return rcode;
280}
281
282/*-----------------------------------------------------------------------
283 * Copy memory to flash, returns:
284 * 0 - OK
285 * 1 - write timeout
286 * 2 - Flash not erased
287 * 4 - Flash not identified
288 */
289
290int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
291{
292 ulong cp, wp;
293 unsigned char data;
294 int count, i, l, rc, port_width;
295
296 if (info->flash_id == FLASH_UNKNOWN)
297 return 4;
298
299 wp = addr;
300 port_width = 1;
301
302 /*
303 * handle unaligned start bytes
304 */
305 if ((l = addr - wp) != 0) {
306 data = 0;
307 for (i = 0, cp = wp; i < l; ++i, ++cp) {
308 data = (data << 8) | (*(uchar *) cp);
309 }
310 for (; i < port_width && cnt > 0; ++i) {
311 data = (data << 8) | *src++;
312 --cnt;
313 ++cp;
314 }
315 for (; cnt == 0 && i < port_width; ++i, ++cp) {
316 data = (data << 8) | (*(uchar *) cp);
317 }
318
319 if ((rc = write_data (info, wp, data)) != 0) {
320 return (rc);
321 }
322 wp += port_width;
323 }
324
325 /*
326 * handle word aligned part
327 */
328 count = 0;
329 while (cnt >= port_width) {
330 data = 0;
331 for (i = 0; i < port_width; ++i) {
332 data = (data << 8) | *src++;
333 }
334 if ((rc = write_data (info, wp, data)) != 0) {
335 return (rc);
336 }
337 wp += port_width;
338 cnt -= port_width;
339 if (count++ > 0x800) {
340 spin_wheel ();
341 count = 0;
342 }
343 }
344
345 if (cnt == 0) {
346 return (0);
347 }
348
349 /*
350 * handle unaligned tail bytes
351 */
352 data = 0;
353 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
354 data = (data << 8) | *src++;
355 --cnt;
356 }
357 for (; i < port_width; ++i, ++cp) {
358 data = (data << 8) | (*(uchar *) cp);
359 }
360
361 return (write_data (info, wp, data));
362}
363
364/*-----------------------------------------------------------------------
365 * Write a word or halfword to Flash, returns:
366 * 0 - OK
367 * 1 - write timeout
368 * 2 - Flash not erased
369 */
370static int write_data (flash_info_t * info, ulong dest, unsigned char data)
371{
372 volatile unsigned char *addr = (volatile unsigned char *) dest;
373 ulong status;
Graeme Russf8b82ee2011-07-15 23:31:37 +0000374 ulong start;
wdenk62cb5cf2005-05-19 22:46:33 +0000375
376 /* Check if Flash is (sufficiently) erased */
377 if ((*addr & data) != data) {
378 printf ("not erased at %08lx (%lx)\n", (ulong) addr,
379 (ulong) * addr);
380 return (2);
381 }
382 /* Disable interrupts which might cause a timeout here */
Anatolij Gustschindf7e62d2011-11-15 13:20:56 +0000383 disable_interrupts();
wdenk62cb5cf2005-05-19 22:46:33 +0000384
385 *addr = 0x40; /* write setup */
386 *addr = data;
387
388 /* arm simple, non interrupt dependent timer */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000389 start = get_timer(0);
wdenk62cb5cf2005-05-19 22:46:33 +0000390
391 /* wait while polling the status register */
392 while (((status = *addr) & 0x80) != 0x80) {
Graeme Russf8b82ee2011-07-15 23:31:37 +0000393 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk62cb5cf2005-05-19 22:46:33 +0000394 *addr = 0xFF; /* restore read mode */
395 return (1);
396 }
397 }
398
399 *addr = 0xFF; /* restore read mode */
400
401 return (0);
402}
403
404void inline spin_wheel (void)
405{
406 static int p = 0;
407 static char w[] = "\\/-";
408
409 printf ("\010%c", w[p]);
410 (++p == 3) ? (p = 0) : 0;
411}