blob: 799fe83f3e6c46139bbddf3cc1daae8c31ec327c [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <mpc8xx.h>
26
27flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
28
29/*-----------------------------------------------------------------------
30 * Functions
31 */
32static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33static int write_byte (flash_info_t *info, ulong dest, uchar data);
34static void flash_get_offsets (ulong base, flash_info_t *info);
35
36/*-----------------------------------------------------------------------
37 */
38
39unsigned long flash_init (void)
40{
41 volatile immap_t *immap = (immap_t *)CFG_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size;
44 int i;
45
46 /* Init: no FLASHes known */
47 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
49 }
50
51 /* Static FLASH Bank configuration here - FIXME XXX */
52
53 size = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
54
55 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
56 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
57 size, size<<20);
58 }
59
60 /* Remap FLASH according to real size */
61 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
62 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) |
63 (memctl->memc_br0 & ~(BR_BA_MSK));
64
65 /* Re-do sizing to get full correct info */
66 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
67
68 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
69
70#if CFG_MONITOR_BASE >= CFG_FLASH_BASE
71 /* monitor protection ON by default */
72 flash_protect(FLAG_PROTECT_SET,
73 CFG_MONITOR_BASE,
wdenk3b57fe02003-05-30 12:48:29 +000074 CFG_MONITOR_BASE+monitor_flash_len-1,
wdenkaffae2b2002-08-17 09:36:01 +000075 &flash_info[0]);
76#endif
77
78 flash_info[0].size = size;
79
80 return (size);
81}
82
83/*-----------------------------------------------------------------------
84 */
85static void flash_get_offsets (ulong base, flash_info_t *info)
86{
87 int i;
88
89 /* set up sector start address table */
90 if (info->flash_id & FLASH_BTYPE) {
91 /* set sector offsets for bottom boot block type */
92 info->start[0] = base + 0x00000000;
93 info->start[1] = base + 0x00004000;
94 info->start[2] = base + 0x00006000;
95 info->start[3] = base + 0x00008000;
96 for (i = 4; i < info->sector_count; i++) {
97 info->start[i] = base + (i * 0x00010000) - 0x00030000;
98 }
99 } else {
100 /* set sector offsets for top boot block type */
101 i = info->sector_count - 1;
102 info->start[i--] = base + info->size - 0x00004000;
103 info->start[i--] = base + info->size - 0x00006000;
104 info->start[i--] = base + info->size - 0x00008000;
105 for (; i >= 0; i--) {
106 info->start[i] = base + i * 0x00010000;
107 }
108 }
109
110}
111
112/*-----------------------------------------------------------------------
113 */
114void flash_print_info (flash_info_t *info)
115{
116 int i;
117
118 if (info->flash_id == FLASH_UNKNOWN) {
119 printf ("missing or unknown FLASH type\n");
120 return;
121 }
122
123 switch (info->flash_id & FLASH_VENDMASK) {
124 case FLASH_MAN_AMD: printf ("AMD "); break;
125 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
126 default: printf ("Unknown Vendor "); break;
127 }
128
129 switch (info->flash_id & FLASH_TYPEMASK) {
130 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
131 break;
132 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
133 break;
134 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
135 break;
136 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
137 break;
138 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
139 break;
140 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
141 break;
142 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
143 break;
144 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
145 break;
146 default: printf ("Unknown Chip Type\n");
147 break;
148 }
149
150 printf (" Size: %ld MB in %d Sectors\n",
151 info->size >> 20, info->sector_count);
152
153 printf (" Sector Start Addresses:");
154 for (i=0; i<info->sector_count; ++i) {
155 if ((i % 5) == 0)
156 printf ("\n ");
157 printf (" %08lX%s",
158 info->start[i],
159 info->protect[i] ? " (RO)" : " "
160 );
161 }
162 printf ("\n");
163 return;
164}
165
166/*-----------------------------------------------------------------------
167 */
168
169
170/*-----------------------------------------------------------------------
171 */
172
173/*
174 * The following code cannot be run from FLASH!
175 */
176
177static ulong flash_get_size (vu_long *addr, flash_info_t *info)
178{
179 short i;
180 uchar value;
181 vu_char *caddr = (vu_char *)addr;
182 ulong base = (ulong)addr;
183
184
185 /* Write auto select command: read Manufacturer ID */
186 caddr[0x0AAA] = 0xAA;
187 caddr[0x0555] = 0x55;
188 caddr[0x0AAA] = 0x90;
189
190 value = caddr[0];
191 switch (value) {
192 case (AMD_MANUFACT & 0xFF):
193 info->flash_id = FLASH_MAN_AMD;
194 break;
195 case (FUJ_MANUFACT & 0xFF):
196 info->flash_id = FLASH_MAN_FUJ;
197 break;
198 default:
199 info->flash_id = FLASH_UNKNOWN;
200 info->sector_count = 0;
201 info->size = 0;
202 return (0); /* no or unknown flash */
203 }
204
205 value = caddr[2]; /* device ID */
206
207 switch (value) {
208 case (AMD_ID_LV400T & 0xFF):
209 info->flash_id += FLASH_AM400T;
210 info->sector_count = 11;
211 info->size = 0x00080000;
212 break; /* => 512 kB */
213
214 case (AMD_ID_LV400B & 0xFF):
215 info->flash_id += FLASH_AM400B;
216 info->sector_count = 11;
217 info->size = 0x00080000;
218 break; /* => 512 kB */
219
220 case (AMD_ID_LV800T & 0xFF):
221 info->flash_id += FLASH_AM800T;
222 info->sector_count = 19;
223 info->size = 0x00100000;
224 break; /* => 1 MB */
225
226 case (AMD_ID_LV800B & 0xFF):
227 info->flash_id += FLASH_AM800B;
228 info->sector_count = 19;
229 info->size = 0x00100000;
230 break; /* => 1 MB */
231
232 case (AMD_ID_LV160T & 0xFF):
233 info->flash_id += FLASH_AM160T;
234 info->sector_count = 35;
235 info->size = 0x00200000;
236 break; /* => 2 MB */
237
238 case (AMD_ID_LV160B & 0xFF):
239 info->flash_id += FLASH_AM160B;
240 info->sector_count = 35;
241 info->size = 0x00200000;
242 break; /* => 2 MB */
243#if 0 /* enable when device IDs are available */
244 case (AMD_ID_LV320T & 0xFF):
245 info->flash_id += FLASH_AM320T;
246 info->sector_count = 67;
247 info->size = 0x00400000;
248 break; /* => 4 MB */
249
250 case (AMD_ID_LV320B & 0xFF):
251 info->flash_id += FLASH_AM320B;
252 info->sector_count = 67;
253 info->size = 0x00400000;
254 break; /* => 4 MB */
255#endif
256 default:
257 info->flash_id = FLASH_UNKNOWN;
258 return (0); /* => no or unknown flash */
259
260 }
261
262 /* set up sector start address table */
263 if (info->flash_id & FLASH_BTYPE) {
264 /* set sector offsets for bottom boot block type */
265 info->start[0] = base + 0x00000000;
266 info->start[1] = base + 0x00004000;
267 info->start[2] = base + 0x00006000;
268 info->start[3] = base + 0x00008000;
269 for (i = 4; i < info->sector_count; i++) {
270 info->start[i] = base + (i * 0x00010000) - 0x00030000;
271 }
272 } else {
273 /* set sector offsets for top boot block type */
274 i = info->sector_count - 1;
275 info->start[i--] = base + info->size - 0x00004000;
276 info->start[i--] = base + info->size - 0x00006000;
277 info->start[i--] = base + info->size - 0x00008000;
278 for (; i >= 0; i--) {
279 info->start[i] = base + i * 0x00010000;
280 }
281 }
282
283 /* check for protected sectors */
284 for (i = 0; i < info->sector_count; i++) {
285 /* read sector protection: D0 = 1 if protected */
286 caddr = (volatile unsigned char *)(info->start[i]);
287 info->protect[i] = caddr[4] & 1;
288 }
289
290 /*
291 * Prevent writes to uninitialized FLASH.
292 */
293 if (info->flash_id != FLASH_UNKNOWN) {
294 caddr = (vu_char *)info->start[0];
295
296 *caddr = 0xF0; /* reset bank */
297 }
298
299 return (info->size);
300}
301
302
303/*-----------------------------------------------------------------------
304 */
305
306int flash_erase (flash_info_t *info, int s_first, int s_last)
307{
308 vu_char *addr = (vu_char*)(info->start[0]);
309 int flag, prot, sect, l_sect;
310 ulong start, now, last;
311
312 if ((s_first < 0) || (s_first > s_last)) {
313 if (info->flash_id == FLASH_UNKNOWN) {
314 printf ("- missing\n");
315 } else {
316 printf ("- no sectors to erase\n");
317 }
318 return 1;
319 }
320
321 if ((info->flash_id == FLASH_UNKNOWN) ||
322 (info->flash_id > FLASH_AMD_COMP)) {
323 printf ("Can't erase unknown flash type %08lx - aborted\n",
324 info->flash_id);
325 return 1;
326 }
327
328 prot = 0;
329 for (sect=s_first; sect<=s_last; ++sect) {
330 if (info->protect[sect]) {
331 prot++;
332 }
333 }
334
335 if (prot) {
336 printf ("- Warning: %d protected sectors will not be erased!\n",
337 prot);
338 } else {
339 printf ("\n");
340 }
341
342 l_sect = -1;
343
344 /* Disable interrupts which might cause a timeout here */
345 flag = disable_interrupts();
346
347 addr[0x0AAA] = 0xAA;
348 addr[0x0555] = 0x55;
349 addr[0x0AAA] = 0x80;
350 addr[0x0AAA] = 0xAA;
351 addr[0x0555] = 0x55;
352
353 /* Start erase on unprotected sectors */
354 for (sect = s_first; sect<=s_last; sect++) {
355 if (info->protect[sect] == 0) { /* not protected */
356 addr = (vu_char*)(info->start[sect]);
357 addr[0] = 0x30;
358 l_sect = sect;
359 }
360 }
361
362 /* re-enable interrupts if necessary */
363 if (flag)
364 enable_interrupts();
365
366 /* wait at least 80us - let's wait 1 ms */
367 udelay (1000);
368
369 /*
370 * We wait for the last triggered sector
371 */
372 if (l_sect < 0)
373 goto DONE;
374
375 start = get_timer (0);
376 last = start;
377 addr = (vu_char*)(info->start[l_sect]);
378 while ((addr[0] & 0x80) != 0x80) {
379 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
380 printf ("Timeout\n");
381 return 1;
382 }
383 /* show that we're waiting */
384 if ((now - last) > 1000) { /* every second */
385 putc ('.');
386 last = now;
387 }
388 }
389
390DONE:
391 /* reset to read mode */
392 addr = (vu_char *)info->start[0];
393 addr[0] = 0xF0; /* reset bank */
394
395 printf (" done\n");
396 return 0;
397}
398
399/*-----------------------------------------------------------------------
400 * Copy memory to flash, returns:
401 * 0 - OK
402 * 1 - write timeout
403 * 2 - Flash not erased
404 */
405
406int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
407{
408 int rc;
409
410 while (cnt > 0) {
411 if ((rc = write_byte(info, addr++, *src++)) != 0) {
412 return (rc);
413 }
414 --cnt;
415 }
416
417 return (0);
418}
419
420/*-----------------------------------------------------------------------
421 * Write a word to Flash, returns:
422 * 0 - OK
423 * 1 - write timeout
424 * 2 - Flash not erased
425 */
426static int write_byte (flash_info_t *info, ulong dest, uchar data)
427{
428 vu_char *addr = (vu_char*)(info->start[0]);
429 ulong start;
430 int flag;
431
432 /* Check if Flash is (sufficiently) erased */
433 if ((*((vu_char *)dest) & data) != data) {
434 return (2);
435 }
436 /* Disable interrupts which might cause a timeout here */
437 flag = disable_interrupts();
438
439 addr[0x0AAA] = 0xAA;
440 addr[0x0555] = 0x55;
441 addr[0x0AAA] = 0xA0;
442
443 *((vu_char *)dest) = data;
444
445 /* re-enable interrupts if necessary */
446 if (flag)
447 enable_interrupts();
448
449 /* data polling for D7 */
450 start = get_timer (0);
451 while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
452 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
453 return (1);
454 }
455 }
456 return (0);
457}
458
459/*-----------------------------------------------------------------------
460 */