blob: 14888f84bae13159882ca9e520f72738e447b500 [file] [log] [blame]
wdenked247f42002-10-07 21:58:02 +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+
wdenked247f42002-10-07 21:58:02 +00006 */
7
8#include <common.h>
9#include <mpc8xx.h>
10
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020011flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenked247f42002-10-07 21:58:02 +000012
13/*-----------------------------------------------------------------------
14 * Functions
15 */
16static ulong flash_get_size(vu_long * addr, flash_info_t * info);
17static int write_byte(flash_info_t * info, ulong dest, uchar data);
18static void flash_get_offsets(ulong base, flash_info_t * info);
19
20/*-----------------------------------------------------------------------
21 */
22
23unsigned long flash_init(void)
24{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020025 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenked247f42002-10-07 21:58:02 +000026 volatile memctl8xx_t *memctl = &immap->im_memctl;
27 unsigned long size;
28 int i;
29
30 /* Init: no FLASHes known */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020031 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
wdenked247f42002-10-07 21:58:02 +000032 flash_info[i].flash_id = FLASH_UNKNOWN;
wdenked247f42002-10-07 21:58:02 +000033
34 size = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
35
36 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
37 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size, size << 20);
38 }
39
40 /* Remap FLASH according to real size */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020041 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
42 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
wdenked247f42002-10-07 21:58:02 +000043
44 /* Re-do sizing to get full correct info */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020045 size = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenked247f42002-10-07 21:58:02 +000046
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
wdenked247f42002-10-07 21:58:02 +000048
49 /* monitor protection ON by default */
wdenk993cad92003-06-26 22:04:09 +000050 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051 CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
wdenk993cad92003-06-26 22:04:09 +000052 &flash_info[0]);
53
54 flash_protect ( FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020055 CONFIG_ENV_ADDR,
Wolfgang Denkdfcd7f22009-05-15 00:16:03 +020056 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
wdenk993cad92003-06-26 22:04:09 +000057 &flash_info[0]);
58
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020059#ifdef CONFIG_ENV_ADDR_REDUND
wdenk993cad92003-06-26 22:04:09 +000060 flash_protect ( FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020061 CONFIG_ENV_ADDR_REDUND,
Wolfgang Denkdfcd7f22009-05-15 00:16:03 +020062 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
wdenk993cad92003-06-26 22:04:09 +000063 &flash_info[0]);
64#endif
65
wdenked247f42002-10-07 21:58:02 +000066
67 flash_info[0].size = size;
68
69 return (size);
70}
71
72/*-----------------------------------------------------------------------
73 */
74static void flash_get_offsets(ulong base, flash_info_t * info)
75{
76 int i;
77
78 /* set up sector start address table */
79 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
80 for (i = 0; i < info->sector_count; i++) {
81 info->start[i] = base + (i * 0x00010000);
82 }
83 } else if (info->flash_id & FLASH_BTYPE) {
84 /* set sector offsets for bottom boot block type */
85 info->start[0] = base + 0x00000000;
86 info->start[1] = base + 0x00004000;
87 info->start[2] = base + 0x00006000;
88 info->start[3] = base + 0x00008000;
89 for (i = 4; i < info->sector_count; i++) {
90 info->start[i] = base + (i * 0x00010000) - 0x00030000;
91 }
92 } else {
93 /* set sector offsets for top boot block type */
94 i = info->sector_count - 1;
95 info->start[i--] = base + info->size - 0x00004000;
96 info->start[i--] = base + info->size - 0x00006000;
97 info->start[i--] = base + info->size - 0x00008000;
98 for (; i >= 0; i--) {
99 info->start[i] = base + i * 0x00010000;
100 }
101 }
102
103}
104
105/*-----------------------------------------------------------------------
106 */
107void flash_print_info(flash_info_t * info)
108{
109 int i;
110
111 if (info->flash_id == FLASH_UNKNOWN) {
112 printf("missing or unknown FLASH type\n");
113 return;
114 }
115
116 switch (info->flash_id & FLASH_VENDMASK) {
117 case FLASH_MAN_AMD:
118 printf("AMD ");
119 break;
120 case FLASH_MAN_FUJ:
121 printf("FUJITSU ");
122 break;
123 case FLASH_MAN_MX:
124 printf("MXIC ");
125 break;
126 default:
127 printf("Unknown Vendor ");
128 break;
129 }
130
131 switch (info->flash_id & FLASH_TYPEMASK) {
132 case FLASH_AM040:
133 printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
134 break;
135 case FLASH_AM400B:
136 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
137 break;
138 case FLASH_AM400T:
139 printf("AM29LV400T (4 Mbit, top boot sector)\n");
140 break;
141 case FLASH_AM800B:
142 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
143 break;
144 case FLASH_AM800T:
145 printf("AM29LV800T (8 Mbit, top boot sector)\n");
146 break;
147 case FLASH_AM160B:
148 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
149 break;
150 case FLASH_AM160T:
151 printf("AM29LV160T (16 Mbit, top boot sector)\n");
152 break;
153 case FLASH_AM320B:
154 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
155 break;
156 case FLASH_AM320T:
157 printf("AM29LV320T (32 Mbit, top boot sector)\n");
158 break;
159 default:
160 printf("Unknown Chip Type\n");
161 break;
162 }
163
164 printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
165
166 printf(" Sector Start Addresses:");
167 for (i = 0; i < info->sector_count; ++i) {
168 if ((i % 5) == 0)
169 printf("\n ");
170 printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
171 }
172 printf("\n");
173}
174
175/*-----------------------------------------------------------------------
176 */
177
178
179/*-----------------------------------------------------------------------
180 */
181
182/*
183 * The following code cannot be run from FLASH!
184 */
185
186static ulong flash_get_size(vu_long * addr, flash_info_t * info)
187{
188 short i;
189 uchar mid;
190 uchar pid;
191 vu_char *caddr = (vu_char *) addr;
192 ulong base = (ulong) addr;
193
194
195 /* Write auto select command: read Manufacturer ID */
196 caddr[0x0555] = 0xAA;
197 caddr[0x02AA] = 0x55;
198 caddr[0x0555] = 0x90;
199
200 mid = caddr[0];
201 switch (mid) {
202 case (AMD_MANUFACT & 0xFF):
203 info->flash_id = FLASH_MAN_AMD;
204 break;
205 case (FUJ_MANUFACT & 0xFF):
206 info->flash_id = FLASH_MAN_FUJ;
207 break;
208 case (MX_MANUFACT & 0xFF):
209 info->flash_id = FLASH_MAN_MX;
210 break;
211 case (STM_MANUFACT & 0xFF):
212 info->flash_id = FLASH_MAN_STM;
213 break;
214 default:
215 info->flash_id = FLASH_UNKNOWN;
216 info->sector_count = 0;
217 info->size = 0;
218 return (0); /* no or unknown flash */
219 }
220
221 pid = caddr[1]; /* device ID */
222 switch (pid) {
223 case (AMD_ID_LV400T & 0xFF):
224 info->flash_id += FLASH_AM400T;
225 info->sector_count = 11;
226 info->size = 0x00080000;
227 break; /* => 512 kB */
228
229 case (AMD_ID_LV400B & 0xFF):
230 info->flash_id += FLASH_AM400B;
231 info->sector_count = 11;
232 info->size = 0x00080000;
233 break; /* => 512 kB */
234
235 case (AMD_ID_LV800T & 0xFF):
236 info->flash_id += FLASH_AM800T;
237 info->sector_count = 19;
238 info->size = 0x00100000;
239 break; /* => 1 MB */
240
241 case (AMD_ID_LV800B & 0xFF):
242 info->flash_id += FLASH_AM800B;
243 info->sector_count = 19;
244 info->size = 0x00100000;
245 break; /* => 1 MB */
246
247 case (AMD_ID_LV160T & 0xFF):
248 info->flash_id += FLASH_AM160T;
249 info->sector_count = 35;
250 info->size = 0x00200000;
251 break; /* => 2 MB */
252
253 case (AMD_ID_LV160B & 0xFF):
254 info->flash_id += FLASH_AM160B;
255 info->sector_count = 35;
256 info->size = 0x00200000;
257 break; /* => 2 MB */
258
259 case (AMD_ID_LV040B & 0xFF):
260 info->flash_id += FLASH_AM040;
261 info->sector_count = 8;
262 info->size = 0x00080000;
263 break;
264
265 case (STM_ID_M29W040B & 0xFF):
266 info->flash_id += FLASH_AM040;
267 info->sector_count = 8;
268 info->size = 0x00080000;
269 break;
270
271#if 0 /* enable when device IDs are available */
272 case (AMD_ID_LV320T & 0xFF):
273 info->flash_id += FLASH_AM320T;
274 info->sector_count = 67;
275 info->size = 0x00400000;
276 break; /* => 4 MB */
277
278 case (AMD_ID_LV320B & 0xFF):
279 info->flash_id += FLASH_AM320B;
280 info->sector_count = 67;
281 info->size = 0x00400000;
282 break; /* => 4 MB */
283#endif
284 default:
285 info->flash_id = FLASH_UNKNOWN;
286 return (0); /* => no or unknown flash */
287
288 }
289
290 printf(" ");
291 /* set up sector start address table */
292 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
293 for (i = 0; i < info->sector_count; i++) {
294 info->start[i] = base + (i * 0x00010000);
295 }
296 } else if (info->flash_id & FLASH_BTYPE) {
297 /* set sector offsets for bottom boot block type */
298 info->start[0] = base + 0x00000000;
299 info->start[1] = base + 0x00004000;
300 info->start[2] = base + 0x00006000;
301 info->start[3] = base + 0x00008000;
302 for (i = 4; i < info->sector_count; i++) {
303 info->start[i] = base + (i * 0x00010000) - 0x00030000;
304 }
305 } else {
306 /* set sector offsets for top boot block type */
307 i = info->sector_count - 1;
308 info->start[i--] = base + info->size - 0x00004000;
309 info->start[i--] = base + info->size - 0x00006000;
310 info->start[i--] = base + info->size - 0x00008000;
311 for (; i >= 0; i--) {
312 info->start[i] = base + i * 0x00010000;
313 }
314 }
315
316 /* check for protected sectors */
317 for (i = 0; i < info->sector_count; i++) {
318 /* read sector protection: D0 = 1 if protected */
319 caddr = (volatile unsigned char *)(info->start[i]);
320 info->protect[i] = caddr[2] & 1;
321 }
322
323 /*
324 * Prevent writes to uninitialized FLASH.
325 */
326 if (info->flash_id != FLASH_UNKNOWN) {
327 caddr = (vu_char *) info->start[0];
328
329 caddr[0x0555] = 0xAA;
330 caddr[0x02AA] = 0x55;
331 caddr[0x0555] = 0xF0;
332
333 udelay(20000);
334 }
335
336 return (info->size);
337}
338
339
340/*-----------------------------------------------------------------------
341 */
342
343int flash_erase(flash_info_t * info, int s_first, int s_last)
344{
345 vu_char *addr = (vu_char *) (info->start[0]);
346 int flag, prot, sect, l_sect;
347 ulong start, now, last;
348
349 if ((s_first < 0) || (s_first > s_last)) {
350 if (info->flash_id == FLASH_UNKNOWN) {
351 printf("- missing\n");
352 } else {
353 printf("- no sectors to erase\n");
354 }
355 return 1;
356 }
357
358 if ((info->flash_id == FLASH_UNKNOWN) ||
359 (info->flash_id > FLASH_AMD_COMP)) {
360 printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
361 return 1;
362 }
363
364 prot = 0;
365 for (sect = s_first; sect <= s_last; ++sect) {
366 if (info->protect[sect]) {
367 prot++;
368 }
369 }
370
371 if (prot) {
372 printf("- Warning: %d protected sectors will not be erased!\n", prot);
373 } else {
374 printf("\n");
375 }
376
377 l_sect = -1;
378
379 /* Disable interrupts which might cause a timeout here */
380 flag = disable_interrupts();
381
382 addr[0x0555] = 0xAA;
383 addr[0x02AA] = 0x55;
384 addr[0x0555] = 0x80;
385 addr[0x0555] = 0xAA;
386 addr[0x02AA] = 0x55;
387
388 /* Start erase on unprotected sectors */
389 for (sect = s_first; sect <= s_last; sect++) {
390 if (info->protect[sect] == 0) { /* not protected */
391 addr = (vu_char *) (info->start[sect]);
392 addr[0] = 0x30;
393 l_sect = sect;
394 }
395 }
396
397 /* re-enable interrupts if necessary */
398 if (flag)
399 enable_interrupts();
400
401 /* wait at least 80us - let's wait 1 ms */
402 udelay(1000);
403
404 /*
405 * We wait for the last triggered sector
406 */
407 if (l_sect < 0)
408 goto DONE;
409
410 start = get_timer(0);
411 last = start;
412 addr = (vu_char *) (info->start[l_sect]);
413 while ((addr[0] & 0x80) != 0x80) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200414 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenked247f42002-10-07 21:58:02 +0000415 printf("Timeout\n");
416 return 1;
417 }
418 /* show that we're waiting */
419 if ((now - last) > 1000) { /* every second */
420 putc('.');
421 last = now;
422 }
423 }
424
425 DONE:
426 /* reset to read mode */
427 addr = (vu_char *) info->start[0];
428 addr[0] = 0xF0; /* reset bank */
429
430 printf(" done\n");
431 return 0;
432}
433
434/*-----------------------------------------------------------------------
435 * Copy memory to flash, returns:
436 * 0 - OK
437 * 1 - write timeout
438 * 2 - Flash not erased
439 */
440
441int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
442{
443 int rc;
444
445 while (cnt > 0) {
446 if ((rc = write_byte(info, addr++, *src++)) != 0) {
447 return (rc);
448 }
449 --cnt;
450 }
451
452 return (0);
453}
454
455/*-----------------------------------------------------------------------
456 * Write a word to Flash, returns:
457 * 0 - OK
458 * 1 - write timeout
459 * 2 - Flash not erased
460 */
461static int write_byte(flash_info_t * info, ulong dest, uchar data)
462{
463 vu_char *addr = (vu_char *) (info->start[0]);
464 ulong start;
465 int flag;
466
467 /* Check if Flash is (sufficiently) erased */
468 if ((*((vu_char *) dest) & data) != data) {
469 return (2);
470 }
471 /* Disable interrupts which might cause a timeout here */
472 flag = disable_interrupts();
473
474 addr[0x0555] = 0xAA;
475 addr[0x02AA] = 0x55;
476 addr[0x0555] = 0xA0;
477
478 *((vu_char *) dest) = data;
479
480 /* re-enable interrupts if necessary */
481 if (flag)
482 enable_interrupts();
483
484 /* data polling for D7 */
485 start = get_timer(0);
486 while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200487 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenked247f42002-10-07 21:58:02 +0000488 return (1);
489 }
490 }
491 return (0);
492}
493
494/*-----------------------------------------------------------------------
495 */