blob: 8a2265263c8e280a535521f89b1f3375996b6601 [file] [log] [blame]
wdenk682011f2003-06-03 23:54:09 +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+
wdenk682011f2003-06-03 23:54:09 +00006 */
7
8#include <common.h>
9#include <mpc8xx.h>
10
Wolfgang Denk69158402011-11-04 15:55:46 +000011flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk682011f2003-06-03 23:54:09 +000012
Wolfgang Denk69158402011-11-04 15:55:46 +000013/*
wdenk682011f2003-06-03 23:54:09 +000014 * Functions
15 */
Wolfgang Denk69158402011-11-04 15:55:46 +000016static ulong flash_get_size(vu_long *addr, flash_info_t *info);
17static int write_word(flash_info_t *info, ulong dest, ulong data);
18static void flash_get_offsets(ulong base, flash_info_t *info);
wdenk682011f2003-06-03 23:54:09 +000019
Wolfgang Denk69158402011-11-04 15:55:46 +000020unsigned long flash_init(void)
wdenk682011f2003-06-03 23:54:09 +000021{
Wolfgang Denke0ead842011-11-04 15:55:47 +000022 unsigned long size_b0;
wdenk682011f2003-06-03 23:54:09 +000023 int i;
24
25 /* Init: no FLASHes known */
Wolfgang Denk69158402011-11-04 15:55:46 +000026 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
27 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenk682011f2003-06-03 23:54:09 +000028
29 /* Detect size */
Wolfgang Denk69158402011-11-04 15:55:46 +000030 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
31 &flash_info[0]);
wdenk682011f2003-06-03 23:54:09 +000032
33 /* Setup offsets */
Wolfgang Denk69158402011-11-04 15:55:46 +000034 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenk682011f2003-06-03 23:54:09 +000035
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020036#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
wdenk682011f2003-06-03 23:54:09 +000037 /* Monitor protection ON by default */
38 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020039 CONFIG_SYS_MONITOR_BASE,
40 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
wdenk682011f2003-06-03 23:54:09 +000041 &flash_info[0]);
42#endif
43
wdenk682011f2003-06-03 23:54:09 +000044 flash_info[0].size = size_b0;
wdenk682011f2003-06-03 23:54:09 +000045
Wolfgang Denke0ead842011-11-04 15:55:47 +000046 return size_b0;
wdenk682011f2003-06-03 23:54:09 +000047}
48
49/*-----------------------------------------------------------------------
50 * Fix this to support variable sector sizes
51*/
Wolfgang Denk69158402011-11-04 15:55:46 +000052static void flash_get_offsets(ulong base, flash_info_t *info)
wdenk682011f2003-06-03 23:54:09 +000053{
54 int i;
55
56 /* set up sector start address table */
57 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
58 /* set sector offsets for bottom boot block type */
59 for (i = 0; i < info->sector_count; i++)
60 info->start[i] = base + (i * 0x00010000);
61 }
62}
63
64/*-----------------------------------------------------------------------
65 */
Wolfgang Denk69158402011-11-04 15:55:46 +000066void flash_print_info(flash_info_t *info)
wdenk682011f2003-06-03 23:54:09 +000067{
68 int i;
69
Wolfgang Denk69158402011-11-04 15:55:46 +000070 if (info->flash_id == FLASH_UNKNOWN) {
71 puts("missing or unknown FLASH type\n");
wdenk682011f2003-06-03 23:54:09 +000072 return;
73 }
74
Wolfgang Denk69158402011-11-04 15:55:46 +000075 switch (info->flash_id & FLASH_VENDMASK) {
76 case FLASH_MAN_AMD:
77 printf("AMD ");
78 break;
79 case FLASH_MAN_FUJ:
80 printf("FUJITSU ");
81 break;
82 case FLASH_MAN_BM:
83 printf("BRIGHT MICRO ");
84 break;
85 default:
86 printf("Unknown Vendor ");
87 break;
wdenk682011f2003-06-03 23:54:09 +000088 }
89
Wolfgang Denk69158402011-11-04 15:55:46 +000090 switch (info->flash_id & FLASH_TYPEMASK) {
91 case FLASH_AM040:
92 printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
93 break;
94 case FLASH_AM400B:
95 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
96 break;
97 case FLASH_AM400T:
98 printf("AM29LV400T (4 Mbit, top boot sector)\n");
99 break;
100 case FLASH_AM800B:
101 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
102 break;
103 case FLASH_AM800T:
104 printf("AM29LV800T (8 Mbit, top boot sector)\n");
105 break;
106 case FLASH_AM160B:
107 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
108 break;
109 case FLASH_AM160T:
110 printf("AM29LV160T (16 Mbit, top boot sector)\n");
111 break;
112 case FLASH_AM320B:
113 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
114 break;
115 case FLASH_AM320T:
116 printf("AM29LV320T (32 Mbit, top boot sector)\n");
117 break;
118 default:
119 printf("Unknown Chip Type\n");
120 break;
wdenk682011f2003-06-03 23:54:09 +0000121 }
122
123 if (info->size >> 20) {
Wolfgang Denk69158402011-11-04 15:55:46 +0000124 printf(" Size: %ld MB in %d Sectors\n",
125 info->size >> 20,
126 info->sector_count);
wdenk682011f2003-06-03 23:54:09 +0000127 } else {
Wolfgang Denk69158402011-11-04 15:55:46 +0000128 printf(" Size: %ld KB in %d Sectors\n",
129 info->size >> 10,
130 info->sector_count);
wdenk682011f2003-06-03 23:54:09 +0000131 }
132
Wolfgang Denk69158402011-11-04 15:55:46 +0000133 puts(" Sector Start Addresses:");
wdenk682011f2003-06-03 23:54:09 +0000134
Wolfgang Denk69158402011-11-04 15:55:46 +0000135 for (i = 0; i < info->sector_count; ++i) {
wdenk682011f2003-06-03 23:54:09 +0000136 if ((i % 5) == 0)
Wolfgang Denk69158402011-11-04 15:55:46 +0000137 puts("\n ");
wdenk682011f2003-06-03 23:54:09 +0000138
Wolfgang Denk69158402011-11-04 15:55:46 +0000139 printf(" %08lX%s",
wdenk682011f2003-06-03 23:54:09 +0000140 info->start[i],
141 info->protect[i] ? " (RO)" : " ");
142 }
143
Wolfgang Denk69158402011-11-04 15:55:46 +0000144 putc('\n');
wdenk682011f2003-06-03 23:54:09 +0000145 return;
146}
wdenk682011f2003-06-03 23:54:09 +0000147
148/*
149 * The following code cannot be run from FLASH!
150 */
151
Wolfgang Denk69158402011-11-04 15:55:46 +0000152static ulong flash_get_size(vu_long *addr, flash_info_t *info)
wdenk682011f2003-06-03 23:54:09 +0000153{
154 short i;
155 volatile unsigned char *caddr;
156 char value;
157
158 caddr = (volatile unsigned char *)addr ;
159
160 /* Write auto select command: read Manufacturer ID */
161
Wolfgang Denke0ead842011-11-04 15:55:47 +0000162 debug("Base address is: %8p\n", caddr);
wdenk682011f2003-06-03 23:54:09 +0000163
164 caddr[0x0555] = 0xAA;
165 caddr[0x02AA] = 0x55;
166 caddr[0x0555] = 0x90;
167
168 value = caddr[0];
169
Wolfgang Denk69158402011-11-04 15:55:46 +0000170 debug("Manufact ID: %02x\n", value);
171
172 switch (value) {
173 case 0x01: /*AMD_MANUFACT*/
174 info->flash_id = FLASH_MAN_AMD;
wdenk682011f2003-06-03 23:54:09 +0000175 break;
176
Wolfgang Denk69158402011-11-04 15:55:46 +0000177 case 0x04: /*FUJ_MANUFACT*/
178 info->flash_id = FLASH_MAN_FUJ;
wdenk682011f2003-06-03 23:54:09 +0000179 break;
180
Wolfgang Denk69158402011-11-04 15:55:46 +0000181 default:
182 info->flash_id = FLASH_UNKNOWN;
183 info->sector_count = 0;
184 info->size = 0;
185 break;
wdenk682011f2003-06-03 23:54:09 +0000186 }
187
188 value = caddr[1]; /* device ID */
wdenk682011f2003-06-03 23:54:09 +0000189
Wolfgang Denk69158402011-11-04 15:55:46 +0000190 debug("Device ID: %02x\n", value);
wdenk682011f2003-06-03 23:54:09 +0000191
Wolfgang Denk69158402011-11-04 15:55:46 +0000192 switch (value) {
193 case AMD_ID_LV040B:
194 info->flash_id += FLASH_AM040;
195 info->sector_count = 8;
196 info->size = 0x00080000;
197 break; /* => 512Kb */
198
199 default:
200 info->flash_id = FLASH_UNKNOWN;
201 return 0; /* => no or unknown flash */
wdenk682011f2003-06-03 23:54:09 +0000202 }
203
Wolfgang Denk69158402011-11-04 15:55:46 +0000204 flash_get_offsets((ulong)addr, &flash_info[0]);
wdenk682011f2003-06-03 23:54:09 +0000205
206 /* check for protected sectors */
Wolfgang Denk69158402011-11-04 15:55:46 +0000207 for (i = 0; i < info->sector_count; i++) {
208 /*
209 * read sector protection at sector address,
210 * (A7 .. A0) = 0x02
211 * D0 = 1 if protected
212 */
wdenk682011f2003-06-03 23:54:09 +0000213 caddr = (volatile unsigned char *)(info->start[i]);
214 info->protect[i] = caddr[2] & 1;
215 }
216
217 /*
218 * Prevent writes to uninitialized FLASH.
219 */
Wolfgang Denk69158402011-11-04 15:55:46 +0000220 if (info->flash_id != FLASH_UNKNOWN) {
wdenk682011f2003-06-03 23:54:09 +0000221 caddr = (volatile unsigned char *)info->start[0];
222 *caddr = 0xF0; /* reset bank */
223 }
224
Wolfgang Denk69158402011-11-04 15:55:46 +0000225 return info->size;
wdenk682011f2003-06-03 23:54:09 +0000226}
227
228
Wolfgang Denk69158402011-11-04 15:55:46 +0000229int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenk682011f2003-06-03 23:54:09 +0000230{
Wolfgang Denk69158402011-11-04 15:55:46 +0000231 volatile unsigned char *addr =
232 (volatile unsigned char *)(info->start[0]);
wdenk682011f2003-06-03 23:54:09 +0000233 int flag, prot, sect, l_sect;
234 ulong start, now, last;
235
236 if ((s_first < 0) || (s_first > s_last)) {
Wolfgang Denk69158402011-11-04 15:55:46 +0000237 if (info->flash_id == FLASH_UNKNOWN)
238 printf("- missing\n");
239 else
240 printf("- no sectors to erase\n");
241
wdenk682011f2003-06-03 23:54:09 +0000242 return 1;
243 }
244
245 if ((info->flash_id == FLASH_UNKNOWN) ||
246 (info->flash_id > FLASH_AMD_COMP)) {
Wolfgang Denk69158402011-11-04 15:55:46 +0000247 printf("Can't erase unknown flash type - aborted\n");
wdenk682011f2003-06-03 23:54:09 +0000248 return 1;
249 }
250
251 prot = 0;
Wolfgang Denk69158402011-11-04 15:55:46 +0000252 for (sect = s_first; sect <= s_last; ++sect) {
253 if (info->protect[sect])
wdenk682011f2003-06-03 23:54:09 +0000254 prot++;
wdenk682011f2003-06-03 23:54:09 +0000255 }
256
257 if (prot) {
Wolfgang Denk69158402011-11-04 15:55:46 +0000258 printf("- Warning: %d protected sectors will not be erased!\n",
wdenk682011f2003-06-03 23:54:09 +0000259 prot);
260 } else {
Wolfgang Denk69158402011-11-04 15:55:46 +0000261 printf("\n");
wdenk682011f2003-06-03 23:54:09 +0000262 }
263
264 l_sect = -1;
265
266 /* Disable interrupts which might cause a timeout here */
267 flag = disable_interrupts();
268
269 addr[0x0555] = 0xAA;
270 addr[0x02AA] = 0x55;
271 addr[0x0555] = 0x80;
272 addr[0x0555] = 0xAA;
273 addr[0x02AA] = 0x55;
274
275 /* Start erase on unprotected sectors */
Wolfgang Denk69158402011-11-04 15:55:46 +0000276 for (sect = s_first; sect <= s_last; sect++) {
wdenk682011f2003-06-03 23:54:09 +0000277 if (info->protect[sect] == 0) { /* not protected */
278 addr = (volatile unsigned char *)(info->start[sect]);
279 addr[0] = 0x30;
280 l_sect = sect;
281 }
282 }
283
284 /* re-enable interrupts if necessary */
285 if (flag)
286 enable_interrupts();
287
288 /* wait at least 80us - let's wait 1 ms */
Wolfgang Denk69158402011-11-04 15:55:46 +0000289 udelay(1000);
wdenk682011f2003-06-03 23:54:09 +0000290
291 /*
292 * We wait for the last triggered sector
293 */
294 if (l_sect < 0)
295 goto DONE;
296
Wolfgang Denk69158402011-11-04 15:55:46 +0000297 start = get_timer(0);
wdenk682011f2003-06-03 23:54:09 +0000298 last = start;
299 addr = (volatile unsigned char *)(info->start[l_sect]);
300
Wolfgang Denk69158402011-11-04 15:55:46 +0000301 while ((addr[0] & 0xFF) != 0xFF) {
302 now = get_timer(start);
303 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
304 printf("Timeout\n");
wdenk682011f2003-06-03 23:54:09 +0000305 return 1;
306 }
307 /* show that we're waiting */
308 if ((now - last) > 1000) { /* every second */
Wolfgang Denk69158402011-11-04 15:55:46 +0000309 putc('.');
wdenk682011f2003-06-03 23:54:09 +0000310 last = now;
311 }
312 }
313
314DONE:
315 /* reset to read mode */
316 addr = (volatile unsigned char *)info->start[0];
317
318 addr[0] = 0xF0; /* reset bank */
319
Wolfgang Denk69158402011-11-04 15:55:46 +0000320 printf(" done\n");
wdenk682011f2003-06-03 23:54:09 +0000321 return 0;
322}
323
Wolfgang Denk69158402011-11-04 15:55:46 +0000324/*
wdenk682011f2003-06-03 23:54:09 +0000325 * Copy memory to flash, returns:
326 * 0 - OK
327 * 1 - write timeout
328 * 2 - Flash not erased
329 */
330
Wolfgang Denk69158402011-11-04 15:55:46 +0000331int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenk682011f2003-06-03 23:54:09 +0000332{
333 ulong cp, wp, data;
334 int i, l, rc;
335
336 wp = (addr & ~3); /* get lower word aligned address */
337
338 /*
339 * handle unaligned start bytes
340 */
Wolfgang Denk69158402011-11-04 15:55:46 +0000341 l = addr - wp;
342
343 if (l != 0) {
wdenk682011f2003-06-03 23:54:09 +0000344 data = 0;
Wolfgang Denk69158402011-11-04 15:55:46 +0000345 for (i = 0, cp = wp; i < l; ++i, ++cp)
wdenk682011f2003-06-03 23:54:09 +0000346 data = (data << 8) | (*(uchar *)cp);
Wolfgang Denk69158402011-11-04 15:55:46 +0000347
348 for (; i < 4 && cnt > 0; ++i) {
wdenk682011f2003-06-03 23:54:09 +0000349 data = (data << 8) | *src++;
350 --cnt;
351 ++cp;
352 }
wdenk682011f2003-06-03 23:54:09 +0000353
Wolfgang Denk69158402011-11-04 15:55:46 +0000354 for (; cnt == 0 && i < 4; ++i, ++cp)
355 data = (data << 8) | (*(uchar *)cp);
356
357 rc = write_word(info, wp, data);
358
359 if (rc != 0)
360 return rc;
361
wdenk682011f2003-06-03 23:54:09 +0000362 wp += 4;
363 }
364
365 /*
366 * handle word aligned part
367 */
368 while (cnt >= 4) {
369 data = 0;
Wolfgang Denk69158402011-11-04 15:55:46 +0000370 for (i = 0; i < 4; ++i)
wdenk682011f2003-06-03 23:54:09 +0000371 data = (data << 8) | *src++;
Wolfgang Denk69158402011-11-04 15:55:46 +0000372
373 rc = write_word(info, wp, data);
374
375 if (rc != 0)
376 return rc;
377
wdenk682011f2003-06-03 23:54:09 +0000378 wp += 4;
379 cnt -= 4;
380 }
381
Wolfgang Denk69158402011-11-04 15:55:46 +0000382 if (cnt == 0)
383 return 0;
wdenk682011f2003-06-03 23:54:09 +0000384
385 /*
386 * handle unaligned tail bytes
387 */
388 data = 0;
Wolfgang Denk69158402011-11-04 15:55:46 +0000389 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
wdenk682011f2003-06-03 23:54:09 +0000390 data = (data << 8) | *src++;
391 --cnt;
392 }
Wolfgang Denk69158402011-11-04 15:55:46 +0000393 for (; i < 4; ++i, ++cp)
wdenk682011f2003-06-03 23:54:09 +0000394 data = (data << 8) | (*(uchar *)cp);
wdenk682011f2003-06-03 23:54:09 +0000395
Wolfgang Denk69158402011-11-04 15:55:46 +0000396 return write_word(info, wp, data);
wdenk682011f2003-06-03 23:54:09 +0000397}
398
Wolfgang Denk69158402011-11-04 15:55:46 +0000399/*
wdenk682011f2003-06-03 23:54:09 +0000400 * Write a word to Flash, returns:
401 * 0 - OK
402 * 1 - write timeout
403 * 2 - Flash not erased
404 */
Wolfgang Denk69158402011-11-04 15:55:46 +0000405static int write_word(flash_info_t *info, ulong dest, ulong data)
wdenk682011f2003-06-03 23:54:09 +0000406{
Wolfgang Denk69158402011-11-04 15:55:46 +0000407 volatile unsigned char *cdest, *cdata;
408 volatile unsigned char *addr =
409 (volatile unsigned char *)(info->start[0]);
wdenk682011f2003-06-03 23:54:09 +0000410 ulong start;
411 int flag, count = 4 ;
412
413 cdest = (volatile unsigned char *)dest ;
414 cdata = (volatile unsigned char *)&data ;
415
416 /* Check if Flash is (sufficiently) erased */
Wolfgang Denk69158402011-11-04 15:55:46 +0000417 if ((*((vu_long *)dest)&data) != data)
418 return 2;
wdenk682011f2003-06-03 23:54:09 +0000419
Wolfgang Denk69158402011-11-04 15:55:46 +0000420 while (count--) {
421 /* Disable interrupts which might cause a timeout here */
422 flag = disable_interrupts();
wdenk682011f2003-06-03 23:54:09 +0000423
Wolfgang Denk69158402011-11-04 15:55:46 +0000424 addr[0x0555] = 0xAA;
425 addr[0x02AA] = 0x55;
426 addr[0x0555] = 0xA0;
wdenk682011f2003-06-03 23:54:09 +0000427
Wolfgang Denk69158402011-11-04 15:55:46 +0000428 *cdest = *cdata;
wdenk682011f2003-06-03 23:54:09 +0000429
Wolfgang Denk69158402011-11-04 15:55:46 +0000430 /* re-enable interrupts if necessary */
431 if (flag)
432 enable_interrupts();
wdenk682011f2003-06-03 23:54:09 +0000433
Wolfgang Denk69158402011-11-04 15:55:46 +0000434 /* data polling for D7 */
435 start = get_timer(0);
436 while ((*cdest ^ *cdata) & 0x80) {
437 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
438 return 1;
wdenk682011f2003-06-03 23:54:09 +0000439 }
wdenk682011f2003-06-03 23:54:09 +0000440
Wolfgang Denk69158402011-11-04 15:55:46 +0000441 cdata++ ;
442 cdest++ ;
wdenk682011f2003-06-03 23:54:09 +0000443 }
Wolfgang Denk69158402011-11-04 15:55:46 +0000444 return 0;
wdenk682011f2003-06-03 23:54:09 +0000445}