blob: 96a1e474a57c38d87e900d4a1b187677a0d915c6 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +00006 */
7
8/*
9 * Modified 4/5/2001
10 * Wait for completion of each sector erase command issued
11 * 4/5/2001
12 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
13 */
14
15/*
16 * Modified July 20, 2001
17 * Strip down to support ONLY the AMD29F032B.
18 * Dave Updegraff - Cray, Inc. dave@cray.com
19 */
20
21#include <common.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020022#include <asm/ppc4xx.h>
wdenkfe8c2802002-11-03 00:38:21 +000023#include <asm/processor.h>
24
25/* The flash chip we use... */
26#define AMD_ID_F032B 0x41 /* 29F032B ID 32 Mbit,64 64Kx8 sectors */
27#define FLASH_AM320B 0x0009
28
29
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenkfe8c2802002-11-03 00:38:21 +000031
32/*-----------------------------------------------------------------------
33 * Functions
34 */
35static ulong flash_get_size (vu_long *addr, flash_info_t *info);
36static int write_word (flash_info_t *info, ulong dest, ulong data);
37static void flash_get_offsets (ulong base, flash_info_t *info);
38
39#define ADDR0 0x5555
40#define ADDR1 0x2aaa
41#define FLASH_WORD_SIZE unsigned char
42
43/*-----------------------------------------------------------------------
44 */
45
46unsigned long flash_init (void)
47{
48 unsigned long size_b0, size_b1;
49 int i;
50
51 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +000053 flash_info[i].flash_id = FLASH_UNKNOWN;
54 }
55
56 /* Static FLASH Bank configuration here - FIXME XXX */
57
58 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
59
60 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
61 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
62 size_b0, size_b0<<20);
63 }
64
65 /* Only one bank */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066 if (CONFIG_SYS_MAX_FLASH_BANKS == 1)
wdenkfe8c2802002-11-03 00:38:21 +000067 {
68 /* Setup offsets */
69 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
70
71#if 0
72 /* Monitor protection ON by default */
73 (void)flash_protect(FLAG_PROTECT_SET,
74 FLASH_BASE0_PRELIM,
wdenk3b57fe02003-05-30 12:48:29 +000075 FLASH_BASE0_PRELIM+monitor_flash_len-1,
wdenkfe8c2802002-11-03 00:38:21 +000076 &flash_info[0]);
77#endif
78 size_b1 = 0 ;
79 flash_info[0].size = size_b0;
80 }
81
82 return (size_b0 + size_b1);
83}
84
85
wdenkfe8c2802002-11-03 00:38:21 +000086/*-----------------------------------------------------------------------
87 */
88static void flash_get_offsets (ulong base, flash_info_t *info)
89{
90 int i;
91
92 /* set up sector start address table */
93 for (i = 0; i < info->sector_count; i++)
94 info->start[i] = base + (i * 0x00010000);
95}
96
97/*-----------------------------------------------------------------------
98 */
99void flash_print_info (flash_info_t *info)
100{
101 int i;
wdenk8bde7f72003-06-27 21:31:46 +0000102 int k;
103 int size;
104 int erased;
105 volatile unsigned long *flash;
wdenkfe8c2802002-11-03 00:38:21 +0000106
107 if (info->flash_id == FLASH_UNKNOWN) {
108 printf ("missing or unknown FLASH type\n");
109 return;
110 }
111
112 switch (info->flash_id & FLASH_VENDMASK) {
113 case FLASH_MAN_AMD: printf ("AMD "); break;
114 default: printf ("Unknown Vendor "); break;
115 }
116
117 switch (info->flash_id & FLASH_TYPEMASK) {
118 case FLASH_AM320B:printf ("AM29F032B (32 Mbit 64x64KB uniform sectors)\n");
119 break;
120 default: printf ("Unknown Chip Type\n");
121 break;
122 }
123
124 printf (" Size: %ld KB in %d Sectors\n",
125 info->size >> 10, info->sector_count);
126
127 printf (" Sector Start Addresses:");
128 for (i=0; i<info->sector_count; ++i) {
wdenk8bde7f72003-06-27 21:31:46 +0000129 /*
130 * Check if whole sector is erased
131 */
132 if (i != (info->sector_count-1))
133 size = info->start[i+1] - info->start[i];
134 else
135 size = info->start[0] + info->size - info->start[i];
136 erased = 1;
137 flash = (volatile unsigned long *)info->start[i];
138 size = size >> 2; /* divide by 4 for longword access */
139 for (k=0; k<size; k++)
140 {
141 if (*flash++ != 0xffffffff)
142 {
143 erased = 0;
144 break;
145 }
146 }
wdenkfe8c2802002-11-03 00:38:21 +0000147
148 if ((i % 5) == 0)
149 printf ("\n ");
150
151 printf (" %08lX%s%s",
152 info->start[i],
153 erased ? " E" : " ",
154 info->protect[i] ? "RO " : " "
155 );
156 }
157 printf ("\n");
158}
159
160/*-----------------------------------------------------------------------
161 */
162
163
164/*-----------------------------------------------------------------------
165 */
166
167/*
168 * The following code cannot be run from FLASH!
169 */
170static ulong flash_get_size (vu_long *addr, flash_info_t *info)
171{
172 short i;
173 FLASH_WORD_SIZE value;
174 ulong base = (ulong)addr;
wdenk8bde7f72003-06-27 21:31:46 +0000175 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
wdenkfe8c2802002-11-03 00:38:21 +0000176
177 /* Write auto select command: read Manufacturer ID */
178 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
179 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
180 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
181
182 value = addr2[0];
183
184 switch (value) {
185 case (FLASH_WORD_SIZE)AMD_MANUFACT:
186 info->flash_id = FLASH_MAN_AMD;
187 break;
188 default:
189 info->flash_id = FLASH_UNKNOWN;
190 info->sector_count = 0;
191 info->size = 0;
192 return (0); /* no or unknown flash */
193 }
194
195 value = addr2[1]; /* device ID */
196
197 switch (value) {
198 case (FLASH_WORD_SIZE)AMD_ID_F032B:
199 info->flash_id += FLASH_AM320B;
200 info->sector_count = 64;
201 info->size = 0x0400000; /* => 4 MB */
202 break;
203 default:
204 info->flash_id = FLASH_UNKNOWN;
205 return (0); /* => no or unknown flash */
206
207 }
208
209 /* set up sector start address table */
210 for (i = 0; i < info->sector_count; i++)
211 info->start[i] = base + (i * 0x00010000);
212
213 /* check for protected sectors */
214 for (i = 0; i < info->sector_count; i++) {
215 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
216 /* D0 = 1 if protected */
217 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
wdenk8bde7f72003-06-27 21:31:46 +0000218 info->protect[i] = addr2[2] & 1;
wdenkfe8c2802002-11-03 00:38:21 +0000219 }
220
221 /*
222 * Prevent writes to uninitialized FLASH.
223 */
224 if (info->flash_id != FLASH_UNKNOWN) {
225 addr2 = (FLASH_WORD_SIZE *)info->start[0];
226 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
227 }
228
229 return (info->size);
230}
231
232int wait_for_DQ7(flash_info_t *info, int sect)
233{
234 ulong start, now, last;
235 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
236
237 start = get_timer (0);
238 last = start;
239 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200240 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk8bde7f72003-06-27 21:31:46 +0000241 printf ("Timeout\n");
242 return -1;
243 }
244 /* show that we're waiting */
245 if ((now - last) > 1000) { /* every second */
246 putc ('.');
247 last = now;
248 }
wdenkfe8c2802002-11-03 00:38:21 +0000249 }
250 return 0;
251}
252
253/*-----------------------------------------------------------------------
254 */
255
256int flash_erase (flash_info_t *info, int s_first, int s_last)
257{
258 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
259 volatile FLASH_WORD_SIZE *addr2;
Wolfgang Denkfc4df322011-11-29 22:17:47 +0000260 int flag, prot, sect;
wdenkfe8c2802002-11-03 00:38:21 +0000261
262 if ((s_first < 0) || (s_first > s_last)) {
263 if (info->flash_id == FLASH_UNKNOWN) {
264 printf ("- missing\n");
265 } else {
266 printf ("- no sectors to erase\n");
267 }
268 return 1;
269 }
270
271 if (info->flash_id == FLASH_UNKNOWN) {
272 printf ("Can't erase unknown flash type - aborted\n");
273 return 1;
274 }
275
276 prot = 0;
277 for (sect=s_first; sect<=s_last; ++sect) {
278 if (info->protect[sect]) {
279 prot++;
280 }
281 }
282
283 if (prot) {
284 printf ("- Warning: %d protected sectors will not be erased!\n",
285 prot);
286 } else {
287 printf ("\n");
288 }
289
wdenkfe8c2802002-11-03 00:38:21 +0000290 /* Disable interrupts which might cause a timeout here */
291 flag = disable_interrupts();
292
293 /* Start erase on unprotected sectors */
294 for (sect = s_first; sect<=s_last; sect++) {
295 if (info->protect[sect] == 0) { /* not protected */
Wolfgang Denkfc4df322011-11-29 22:17:47 +0000296 addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
297 printf("Erasing sector %p\n", addr2);
wdenkfe8c2802002-11-03 00:38:21 +0000298
299 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
300 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
301 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
302 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
303 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
304 addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
Wolfgang Denkfc4df322011-11-29 22:17:47 +0000305 /*
306 * Wait for each sector to complete, it's more
307 * reliable. According to AMD Spec, you must
308 * issue all erase commands within a specified
309 * timeout. This has been seen to fail, especially
310 * if printf()s are included (for debug)!!
311 */
312 wait_for_DQ7(info, sect);
wdenkfe8c2802002-11-03 00:38:21 +0000313 }
314 }
315
316 /* re-enable interrupts if necessary */
317 if (flag)
318 enable_interrupts();
319
320 /* wait at least 80us - let's wait 1 ms */
321 udelay (1000);
322
323 /* reset to read mode */
324 addr = (FLASH_WORD_SIZE *)info->start[0];
325 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
326
327 printf (" done\n");
328 return 0;
329}
330
331/*-----------------------------------------------------------------------
332 * Copy memory to flash, returns:
333 * 0 - OK
334 * 1 - write timeout
335 * 2 - Flash not erased
336 */
337
338int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
339{
340 ulong cp, wp, data;
341 int i, l, rc;
342
343 wp = (addr & ~3); /* get lower word aligned address */
344
345 /*
346 * handle unaligned start bytes
347 */
348 if ((l = addr - wp) != 0) {
349 data = 0;
350 for (i=0, cp=wp; i<l; ++i, ++cp) {
351 data = (data << 8) | (*(uchar *)cp);
352 }
353 for (; i<4 && cnt>0; ++i) {
354 data = (data << 8) | *src++;
355 --cnt;
356 ++cp;
357 }
358 for (; cnt==0 && i<4; ++i, ++cp) {
359 data = (data << 8) | (*(uchar *)cp);
360 }
361
362 if ((rc = write_word(info, wp, data)) != 0) {
363 return (rc);
364 }
365 wp += 4;
366 }
367
368 /*
369 * handle word aligned part
370 */
371 while (cnt >= 4) {
372 data = 0;
373 for (i=0; i<4; ++i) {
374 data = (data << 8) | *src++;
375 }
376 if ((rc = write_word(info, wp, data)) != 0) {
377 return (rc);
378 }
379 wp += 4;
380 cnt -= 4;
381 }
382
383 if (cnt == 0) {
384 return (0);
385 }
386
387 /*
388 * handle unaligned tail bytes
389 */
390 data = 0;
391 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
392 data = (data << 8) | *src++;
393 --cnt;
394 }
395 for (; i<4; ++i, ++cp) {
396 data = (data << 8) | (*(uchar *)cp);
397 }
398
399 return (write_word(info, wp, data));
400}
401
402/*-----------------------------------------------------------------------
403 * Write a word to Flash, returns:
404 * 0 - OK
405 * 1 - write timeout
406 * 2 - Flash not erased
407 */
408static int write_word (flash_info_t *info, ulong dest, ulong data)
409{
wdenk8bde7f72003-06-27 21:31:46 +0000410 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
411 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
412 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
wdenkfe8c2802002-11-03 00:38:21 +0000413 ulong start;
414 int flag;
wdenk8bde7f72003-06-27 21:31:46 +0000415 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000416
417 /* Check if Flash is (sufficiently) erased */
418 if ((*((volatile FLASH_WORD_SIZE *)dest) &
wdenk8bde7f72003-06-27 21:31:46 +0000419 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
wdenkfe8c2802002-11-03 00:38:21 +0000420 return (2);
421 }
422 /* Disable interrupts which might cause a timeout here */
423 flag = disable_interrupts();
424
wdenk8bde7f72003-06-27 21:31:46 +0000425 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
426 {
427 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
428 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
429 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
wdenkfe8c2802002-11-03 00:38:21 +0000430
wdenk8bde7f72003-06-27 21:31:46 +0000431 dest2[i] = data2[i];
wdenkfe8c2802002-11-03 00:38:21 +0000432
wdenk8bde7f72003-06-27 21:31:46 +0000433 /* re-enable interrupts if necessary */
434 if (flag)
435 enable_interrupts();
wdenkfe8c2802002-11-03 00:38:21 +0000436
wdenk8bde7f72003-06-27 21:31:46 +0000437 /* data polling for D7 */
438 start = get_timer (0);
439 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
440 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200441 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk8bde7f72003-06-27 21:31:46 +0000442 return (1);
443 }
444 }
445 }
wdenkfe8c2802002-11-03 00:38:21 +0000446
447 return (0);
448}
449
450/*-----------------------------------------------------------------------
451 */