1 | /* |
---|
2 | * (C) Copyright 2000-2004 |
---|
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 | |
---|
27 | #ifndef CONFIG_ENV_ADDR |
---|
28 | #define CONFIG_ENV_ADDR (CFG_FLASH_BASE + CONFIG_ENV_OFFSET) |
---|
29 | #endif |
---|
30 | |
---|
31 | #define CONFIG_FLASH_16BIT |
---|
32 | |
---|
33 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
---|
34 | |
---|
35 | /*----------------------------------------------------------------------- |
---|
36 | * Functions |
---|
37 | */ |
---|
38 | static ulong flash_get_size (vu_long *addr, flash_info_t *info); |
---|
39 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
---|
40 | |
---|
41 | /*----------------------------------------------------------------------- |
---|
42 | */ |
---|
43 | |
---|
44 | unsigned long flash_init (void) |
---|
45 | { |
---|
46 | volatile immap_t *immap = (immap_t *)CFG_IMMR; |
---|
47 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
---|
48 | unsigned long size_b0; |
---|
49 | int i; |
---|
50 | |
---|
51 | /* Init: no FLASHes known */ |
---|
52 | for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) { |
---|
53 | 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 | |
---|
66 | /* Remap FLASH according to real size */ |
---|
67 | memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK); |
---|
68 | memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16; |
---|
69 | |
---|
70 | /* Re-do sizing to get full correct info */ |
---|
71 | size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]); |
---|
72 | |
---|
73 | #if CFG_MONITOR_BASE >= CFG_FLASH_BASE |
---|
74 | /* monitor protection ON by default */ |
---|
75 | flash_protect(FLAG_PROTECT_SET, |
---|
76 | CFG_MONITOR_BASE, |
---|
77 | CFG_MONITOR_BASE+monitor_flash_len-1, |
---|
78 | &flash_info[0]); |
---|
79 | #endif |
---|
80 | |
---|
81 | #ifdef CONFIG_ENV_IS_IN_FLASH |
---|
82 | /* ENV protection ON by default */ |
---|
83 | flash_protect(FLAG_PROTECT_SET, |
---|
84 | CONFIG_ENV_ADDR, |
---|
85 | CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1, |
---|
86 | &flash_info[0]); |
---|
87 | #endif |
---|
88 | |
---|
89 | flash_info[0].size = size_b0; |
---|
90 | |
---|
91 | return (size_b0); |
---|
92 | } |
---|
93 | |
---|
94 | /*----------------------------------------------------------------------- |
---|
95 | */ |
---|
96 | void flash_print_info (flash_info_t *info) |
---|
97 | { |
---|
98 | int i; |
---|
99 | |
---|
100 | if (info->flash_id == FLASH_UNKNOWN) { |
---|
101 | printf ("missing or unknown FLASH type\n"); |
---|
102 | return; |
---|
103 | } |
---|
104 | |
---|
105 | switch (info->flash_id & FLASH_VENDMASK) { |
---|
106 | case FLASH_MAN_AMD: printf ("AMD "); break; |
---|
107 | case FLASH_MAN_FUJ: printf ("FUJITSU "); break; |
---|
108 | default: printf ("Unknown Vendor "); break; |
---|
109 | } |
---|
110 | |
---|
111 | switch (info->flash_id & FLASH_TYPEMASK) { |
---|
112 | case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
---|
113 | break; |
---|
114 | case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n"); |
---|
115 | break; |
---|
116 | case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
---|
117 | break; |
---|
118 | case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n"); |
---|
119 | break; |
---|
120 | case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
---|
121 | break; |
---|
122 | case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n"); |
---|
123 | break; |
---|
124 | case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
---|
125 | break; |
---|
126 | case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n"); |
---|
127 | break; |
---|
128 | default: printf ("Unknown Chip Type\n"); |
---|
129 | break; |
---|
130 | } |
---|
131 | |
---|
132 | printf (" Size: %ld MB in %d Sectors\n", |
---|
133 | info->size >> 20, info->sector_count); |
---|
134 | |
---|
135 | printf (" Sector Start Addresses:"); |
---|
136 | for (i=0; i<info->sector_count; ++i) { |
---|
137 | if ((i % 5) == 0) |
---|
138 | printf ("\n "); |
---|
139 | printf (" %08lX%s", |
---|
140 | info->start[i], |
---|
141 | info->protect[i] ? " (RO)" : " " |
---|
142 | ); |
---|
143 | } |
---|
144 | printf ("\n"); |
---|
145 | return; |
---|
146 | } |
---|
147 | |
---|
148 | /*----------------------------------------------------------------------- |
---|
149 | */ |
---|
150 | |
---|
151 | |
---|
152 | /*----------------------------------------------------------------------- |
---|
153 | */ |
---|
154 | |
---|
155 | /* |
---|
156 | * The following code cannot be run from FLASH! |
---|
157 | */ |
---|
158 | |
---|
159 | static ulong flash_get_size (vu_long *addr, flash_info_t *info) |
---|
160 | { |
---|
161 | short i; |
---|
162 | ulong value; |
---|
163 | ulong base = (ulong)addr; |
---|
164 | |
---|
165 | /* Write auto select command: read Manufacturer ID */ |
---|
166 | vu_short *s_addr=(vu_short*)addr; |
---|
167 | s_addr[0x5555] = 0x00AA; |
---|
168 | s_addr[0x2AAA] = 0x0055; |
---|
169 | s_addr[0x5555] = 0x0090; |
---|
170 | |
---|
171 | value = s_addr[0]; |
---|
172 | value = value|(value<<16); |
---|
173 | |
---|
174 | switch (value) { |
---|
175 | case AMD_MANUFACT: |
---|
176 | info->flash_id = FLASH_MAN_AMD; |
---|
177 | break; |
---|
178 | case FUJ_MANUFACT: |
---|
179 | info->flash_id = FLASH_MAN_FUJ; |
---|
180 | break; |
---|
181 | default: |
---|
182 | info->flash_id = FLASH_UNKNOWN; |
---|
183 | info->sector_count = 0; |
---|
184 | info->size = 0; |
---|
185 | return (0); /* no or unknown flash */ |
---|
186 | } |
---|
187 | |
---|
188 | value = s_addr[1]; |
---|
189 | value = value|(value<<16); |
---|
190 | |
---|
191 | switch (value) { |
---|
192 | case FUJI_ID_29F800BA: |
---|
193 | info->flash_id += FLASH_AM400T; |
---|
194 | info->sector_count = 19; |
---|
195 | info->size = 0x00100000; |
---|
196 | break; /* => 1 MB */ |
---|
197 | case AMD_ID_LV800T: |
---|
198 | info->flash_id += FLASH_AM800T; |
---|
199 | info->sector_count = 19; |
---|
200 | info->size = 0x00100000; |
---|
201 | break; /* => 1 MB */ |
---|
202 | case AMD_ID_LV800B: |
---|
203 | info->flash_id += FLASH_AM800B; |
---|
204 | info->sector_count = 19; |
---|
205 | info->size = 0x00100000; |
---|
206 | break; /* => 1 MB */ |
---|
207 | default: |
---|
208 | info->flash_id = FLASH_UNKNOWN; |
---|
209 | return (0); /* => no or unknown flash */ |
---|
210 | } |
---|
211 | |
---|
212 | /* set up sector start address table */ |
---|
213 | /* set sector offsets for bottom boot block type */ |
---|
214 | info->start[0] = base + 0x00000000; |
---|
215 | info->start[1] = base + 0x00004000; |
---|
216 | info->start[2] = base + 0x00006000; |
---|
217 | info->start[3] = base + 0x00008000; |
---|
218 | for (i = 4; i < info->sector_count; i++) { |
---|
219 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | /* check for protected sectors */ |
---|
224 | for (i = 0; i < info->sector_count; i++) { |
---|
225 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
---|
226 | /* D0 = 1 if protected */ |
---|
227 | s_addr = (volatile unsigned short *)(info->start[i]); |
---|
228 | info->protect[i] = s_addr[2] & 1; |
---|
229 | } |
---|
230 | |
---|
231 | /* |
---|
232 | * Prevent writes to uninitialized FLASH. |
---|
233 | */ |
---|
234 | if (info->flash_id != FLASH_UNKNOWN) { |
---|
235 | s_addr = (volatile unsigned short *)info->start[0]; |
---|
236 | *s_addr = 0x00F0; /* reset bank */ |
---|
237 | } |
---|
238 | return (info->size); |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | /*----------------------------------------------------------------------- |
---|
243 | */ |
---|
244 | |
---|
245 | |
---|
246 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
---|
247 | { |
---|
248 | vu_long *addr = (vu_long*)(info->start[0]); |
---|
249 | int flag, prot, sect; |
---|
250 | ulong start, now, last; |
---|
251 | #ifdef CONFIG_FLASH_16BIT |
---|
252 | vu_short *s_addr = (vu_short*)addr; |
---|
253 | #endif |
---|
254 | |
---|
255 | if ((s_first < 0) || (s_first > s_last)) { |
---|
256 | if (info->flash_id == FLASH_UNKNOWN) { |
---|
257 | printf ("- missing\n"); |
---|
258 | } else { |
---|
259 | printf ("- no sectors to erase\n"); |
---|
260 | } |
---|
261 | return 1; |
---|
262 | } |
---|
263 | /*#ifndef CONFIG_FLASH_16BIT |
---|
264 | ulong type; |
---|
265 | type = (info->flash_id & FLASH_VENDMASK); |
---|
266 | if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) { |
---|
267 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
---|
268 | info->flash_id); |
---|
269 | return; |
---|
270 | } |
---|
271 | #endif*/ |
---|
272 | prot = 0; |
---|
273 | for (sect=s_first; sect<=s_last; ++sect) { |
---|
274 | if (info->protect[sect]) { |
---|
275 | prot++; |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | if (prot) { |
---|
280 | printf ("- Warning: %d protected sectors will not be erased!\n", |
---|
281 | prot); |
---|
282 | } else { |
---|
283 | printf ("\n"); |
---|
284 | } |
---|
285 | |
---|
286 | start = get_timer (0); |
---|
287 | last = start; |
---|
288 | /* Start erase on unprotected sectors */ |
---|
289 | for (sect = s_first; sect<=s_last; sect++) { |
---|
290 | if (info->protect[sect] == 0) { /* not protected */ |
---|
291 | #ifdef CONFIG_FLASH_16BIT |
---|
292 | vu_short *s_sect_addr = (vu_short*)(info->start[sect]); |
---|
293 | #else |
---|
294 | vu_long *sect_addr = (vu_long*)(info->start[sect]); |
---|
295 | #endif |
---|
296 | /* Disable interrupts which might cause a timeout here */ |
---|
297 | flag = disable_interrupts(); |
---|
298 | |
---|
299 | #ifdef CONFIG_FLASH_16BIT |
---|
300 | |
---|
301 | /*printf("\ns_sect_addr=%x",s_sect_addr);*/ |
---|
302 | s_addr[0x5555] = 0x00AA; |
---|
303 | s_addr[0x2AAA] = 0x0055; |
---|
304 | s_addr[0x5555] = 0x0080; |
---|
305 | s_addr[0x5555] = 0x00AA; |
---|
306 | s_addr[0x2AAA] = 0x0055; |
---|
307 | s_sect_addr[0] = 0x0030; |
---|
308 | #else |
---|
309 | addr[0x5555] = 0x00AA00AA; |
---|
310 | addr[0x2AAA] = 0x00550055; |
---|
311 | addr[0x5555] = 0x00800080; |
---|
312 | addr[0x5555] = 0x00AA00AA; |
---|
313 | addr[0x2AAA] = 0x00550055; |
---|
314 | sect_addr[0] = 0x00300030; |
---|
315 | #endif |
---|
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 | #ifdef CONFIG_FLASH_16BIT |
---|
324 | while ((s_sect_addr[0] & 0x0080) != 0x0080) { |
---|
325 | #else |
---|
326 | while ((sect_addr[0] & 0x00800080) != 0x00800080) { |
---|
327 | #endif |
---|
328 | if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) { |
---|
329 | printf ("Timeout\n"); |
---|
330 | return 1; |
---|
331 | } |
---|
332 | /* show that we're waiting */ |
---|
333 | if ((now - last) > 1000) { /* every second */ |
---|
334 | putc ('.'); |
---|
335 | last = now; |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | /* reset to read mode */ |
---|
342 | addr = (volatile unsigned long *)info->start[0]; |
---|
343 | #ifdef CONFIG_FLASH_16BIT |
---|
344 | s_addr[0] = 0x00F0; /* reset bank */ |
---|
345 | #else |
---|
346 | addr[0] = 0x00F000F0; /* reset bank */ |
---|
347 | #endif |
---|
348 | |
---|
349 | printf (" done\n"); |
---|
350 | return 0; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | /*----------------------------------------------------------------------- |
---|
355 | * Copy memory to flash, returns: |
---|
356 | * 0 - OK |
---|
357 | * 1 - write timeout |
---|
358 | * 2 - Flash not erased |
---|
359 | */ |
---|
360 | |
---|
361 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
---|
362 | { |
---|
363 | ulong cp, wp, data; |
---|
364 | int i, l, rc; |
---|
365 | |
---|
366 | wp = (addr & ~3); /* get lower word aligned address */ |
---|
367 | |
---|
368 | /* |
---|
369 | * handle unaligned start bytes |
---|
370 | */ |
---|
371 | if ((l = addr - wp) != 0) { |
---|
372 | data = 0; |
---|
373 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
---|
374 | data = (data << 8) | (*(uchar *)cp); |
---|
375 | } |
---|
376 | for (; i<4 && cnt>0; ++i) { |
---|
377 | data = (data << 8) | *src++; |
---|
378 | --cnt; |
---|
379 | ++cp; |
---|
380 | } |
---|
381 | for (; cnt==0 && i<4; ++i, ++cp) { |
---|
382 | data = (data << 8) | (*(uchar *)cp); |
---|
383 | } |
---|
384 | |
---|
385 | if ((rc = write_word(info, wp, data)) != 0) { |
---|
386 | return (rc); |
---|
387 | } |
---|
388 | wp += 4; |
---|
389 | } |
---|
390 | |
---|
391 | /* |
---|
392 | * handle word aligned part |
---|
393 | */ |
---|
394 | while (cnt >= 4) { |
---|
395 | data = 0; |
---|
396 | for (i=0; i<4; ++i) { |
---|
397 | data = (data << 8) | *src++; |
---|
398 | } |
---|
399 | if ((rc = write_word(info, wp, data)) != 0) { |
---|
400 | return (rc); |
---|
401 | } |
---|
402 | wp += 4; |
---|
403 | cnt -= 4; |
---|
404 | } |
---|
405 | |
---|
406 | if (cnt == 0) { |
---|
407 | return (0); |
---|
408 | } |
---|
409 | |
---|
410 | /* |
---|
411 | * handle unaligned tail bytes |
---|
412 | */ |
---|
413 | data = 0; |
---|
414 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) { |
---|
415 | data = (data << 8) | *src++; |
---|
416 | --cnt; |
---|
417 | } |
---|
418 | for (; i<4; ++i, ++cp) { |
---|
419 | data = (data << 8) | (*(uchar *)cp); |
---|
420 | } |
---|
421 | |
---|
422 | return (write_word(info, wp, data)); |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | /*----------------------------------------------------------------------- |
---|
427 | * Write a word to Flash, returns: |
---|
428 | * 0 - OK |
---|
429 | * 1 - write timeout |
---|
430 | * 2 - Flash not erased |
---|
431 | */ |
---|
432 | static int write_word (flash_info_t *info, ulong dest, ulong data) |
---|
433 | { |
---|
434 | vu_long *addr = (vu_long*)(info->start[0]); |
---|
435 | |
---|
436 | #ifdef CONFIG_FLASH_16BIT |
---|
437 | vu_short high_data; |
---|
438 | vu_short low_data; |
---|
439 | vu_short *s_addr = (vu_short*)addr; |
---|
440 | #endif |
---|
441 | ulong start; |
---|
442 | int flag; |
---|
443 | |
---|
444 | /* Check if Flash is (sufficiently) erased */ |
---|
445 | if ((*((vu_long *)dest) & data) != data) { |
---|
446 | return (2); |
---|
447 | } |
---|
448 | |
---|
449 | #ifdef CONFIG_FLASH_16BIT |
---|
450 | /* Write the 16 higher-bits */ |
---|
451 | /* Disable interrupts which might cause a timeout here */ |
---|
452 | flag = disable_interrupts(); |
---|
453 | |
---|
454 | high_data = ((data>>16) & 0x0000ffff); |
---|
455 | |
---|
456 | s_addr[0x5555] = 0x00AA; |
---|
457 | s_addr[0x2AAA] = 0x0055; |
---|
458 | s_addr[0x5555] = 0x00A0; |
---|
459 | |
---|
460 | *((vu_short *)dest) = high_data; |
---|
461 | |
---|
462 | |
---|
463 | /* re-enable interrupts if necessary */ |
---|
464 | if (flag) |
---|
465 | enable_interrupts(); |
---|
466 | |
---|
467 | /* data polling for D7 */ |
---|
468 | start = get_timer (0); |
---|
469 | while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) { |
---|
470 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) { |
---|
471 | return (1); |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | |
---|
476 | /* Write the 16 lower-bits */ |
---|
477 | #endif |
---|
478 | |
---|
479 | /* Disable interrupts which might cause a timeout here */ |
---|
480 | flag = disable_interrupts(); |
---|
481 | #ifdef CONFIG_FLASH_16BIT |
---|
482 | dest += 0x2; |
---|
483 | low_data = (data & 0x0000ffff); |
---|
484 | |
---|
485 | s_addr[0x5555] = 0x00AA; |
---|
486 | s_addr[0x2AAA] = 0x0055; |
---|
487 | s_addr[0x5555] = 0x00A0; |
---|
488 | *((vu_short *)dest) = low_data; |
---|
489 | |
---|
490 | #else |
---|
491 | addr[0x5555] = 0x00AA00AA; |
---|
492 | addr[0x2AAA] = 0x00550055; |
---|
493 | addr[0x5555] = 0x00A000A0; |
---|
494 | *((vu_long *)dest) = data; |
---|
495 | #endif |
---|
496 | |
---|
497 | /* re-enable interrupts if necessary */ |
---|
498 | if (flag) |
---|
499 | enable_interrupts(); |
---|
500 | |
---|
501 | /* data polling for D7 */ |
---|
502 | start = get_timer (0); |
---|
503 | |
---|
504 | #ifdef CONFIG_FLASH_16BIT |
---|
505 | while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) { |
---|
506 | #else |
---|
507 | while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) { |
---|
508 | #endif |
---|
509 | |
---|
510 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) { |
---|
511 | return (1); |
---|
512 | } |
---|
513 | } |
---|
514 | return (0); |
---|
515 | } |
---|