]>
Commit | Line | Data |
---|---|---|
9dd41a7b WD |
1 | /* |
2 | * (C) Copyright 2005 | |
3 | * Heiko Schocher, DENX Software Engineering, <hs@denx.de> | |
4 | * | |
5 | * (C) Copyright 2001 | |
6 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net | |
7 | * | |
8 | * (C) Copyright 2001-2005 | |
9 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
10 | * | |
11 | * See file CREDITS for list of people who contributed to this | |
12 | * project. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public License as | |
16 | * published by the Free Software Foundation; either version 2 of | |
17 | * the License, or (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
27 | * MA 02111-1307 USA | |
28 | */ | |
29 | ||
30 | #undef DEBUG | |
31 | ||
32 | #include <common.h> | |
33 | ||
6d0f6bcf | 34 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
9dd41a7b | 35 | |
5a1aceb0 | 36 | #if defined(CONFIG_ENV_IS_IN_FLASH) |
0e8d1586 | 37 | # ifndef CONFIG_ENV_ADDR |
6d0f6bcf | 38 | # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET) |
9dd41a7b | 39 | # endif |
0e8d1586 JCPV |
40 | # ifndef CONFIG_ENV_SIZE |
41 | # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE | |
9dd41a7b | 42 | # endif |
0e8d1586 JCPV |
43 | # ifndef CONFIG_ENV_SECT_SIZE |
44 | # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE | |
9dd41a7b WD |
45 | # endif |
46 | #endif | |
47 | ||
48 | /*----------------------------------------------------------------------- | |
49 | * Protection Flags: | |
50 | */ | |
51 | #define FLAG_PROTECT_SET 0x01 | |
52 | #define FLAG_PROTECT_CLEAR 0x02 | |
53 | ||
54 | /* Board support for 1 or 2 flash devices */ | |
55 | #undef FLASH_PORT_WIDTH32 | |
56 | #undef FLASH_PORT_WIDTH16 | |
57 | #define FLASH_PORT_WIDTH8 | |
58 | ||
59 | #ifdef FLASH_PORT_WIDTH16 | |
60 | #define FLASH_PORT_WIDTH ushort | |
61 | #define FLASH_PORT_WIDTHV vu_short | |
62 | #elif FLASH_PORT_WIDTH32 | |
63 | #define FLASH_PORT_WIDTH ulong | |
64 | #define FLASH_PORT_WIDTHV vu_long | |
65 | #else /* FLASH_PORT_WIDTH8 */ | |
66 | #define FLASH_PORT_WIDTH uchar | |
67 | #define FLASH_PORT_WIDTHV vu_char | |
68 | #endif | |
69 | ||
70 | #define FPW FLASH_PORT_WIDTH | |
71 | #define FPWV FLASH_PORT_WIDTHV | |
72 | ||
73 | /*----------------------------------------------------------------------- | |
74 | * Functions | |
75 | */ | |
76 | static ulong flash_get_size (FPWV * addr, flash_info_t * info); | |
77 | static int write_data (flash_info_t * info, ulong dest, FPW data); | |
78 | static void flash_get_offsets (ulong base, flash_info_t * info); | |
79 | ||
80 | /*----------------------------------------------------------------------- | |
81 | */ | |
82 | ||
83 | unsigned long flash_init (void) | |
84 | { | |
85 | unsigned long size_b0; | |
86 | int i; | |
6d0f6bcf | 87 | volatile immap_t * immr = (immap_t *)CONFIG_SYS_IMMR; |
9dd41a7b WD |
88 | volatile memctl8260_t *memctl = &immr->im_memctl; |
89 | ||
90 | /* Init: no FLASHes known */ | |
6d0f6bcf | 91 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
9dd41a7b WD |
92 | flash_info[i].flash_id = FLASH_UNKNOWN; |
93 | } | |
94 | ||
95 | /* Static FLASH Bank configuration here - FIXME XXX */ | |
6d0f6bcf | 96 | size_b0 = flash_get_size ((FPW *) CONFIG_SYS_FLASH0_BASE, &flash_info[0]); |
9dd41a7b WD |
97 | |
98 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { | |
99 | printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", | |
100 | size_b0, size_b0 << 20); | |
101 | } | |
102 | ||
103 | memctl->memc_or0 = 0xff800060; | |
104 | memctl->memc_br0 = 0xff800801; | |
105 | ||
106 | flash_get_offsets (0xff800000, &flash_info[0]); | |
107 | ||
6d0f6bcf | 108 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
9dd41a7b WD |
109 | /* monitor protection ON by default */ |
110 | (void) flash_protect (FLAG_PROTECT_SET, | |
6d0f6bcf JCPV |
111 | CONFIG_SYS_MONITOR_BASE, |
112 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, | |
9dd41a7b WD |
113 | &flash_info[0]); |
114 | #endif | |
115 | ||
5a1aceb0 | 116 | #ifdef CONFIG_ENV_IS_IN_FLASH |
9dd41a7b WD |
117 | /* ENV protection ON by default */ |
118 | flash_protect (FLAG_PROTECT_SET, | |
0e8d1586 JCPV |
119 | CONFIG_ENV_ADDR, |
120 | CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, | |
9dd41a7b WD |
121 | &flash_info[0]); |
122 | #endif | |
123 | ||
124 | flash_info[0].size = size_b0; | |
125 | ||
126 | return (size_b0); | |
127 | } | |
128 | ||
129 | /*----------------------------------------------------------------------- | |
130 | */ | |
131 | static void flash_get_offsets (ulong base, flash_info_t * info) | |
132 | { | |
133 | int i; | |
134 | ||
135 | if (info->flash_id == FLASH_UNKNOWN) { | |
136 | return; | |
137 | } | |
138 | ||
139 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { | |
140 | for (i = 0; i < info->sector_count; i++) { | |
141 | info->start[i] = base + (i * 0x00020000); | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | /*----------------------------------------------------------------------- | |
147 | */ | |
148 | void flash_print_info (flash_info_t * info) | |
149 | { | |
150 | int i; | |
151 | ||
152 | if (info->flash_id == FLASH_UNKNOWN) { | |
153 | printf ("missing or unknown FLASH type\n"); | |
154 | return; | |
155 | } | |
156 | ||
157 | switch (info->flash_id & FLASH_VENDMASK) { | |
158 | case FLASH_MAN_INTEL: | |
159 | printf ("INTEL "); | |
160 | break; | |
161 | default: | |
162 | printf ("Unknown Vendor "); | |
163 | break; | |
164 | } | |
165 | ||
166 | switch (info->flash_id & FLASH_TYPEMASK) { | |
167 | case FLASH_28F320J3A: | |
168 | printf ("28F320J3A\n"); | |
169 | break; | |
170 | case FLASH_28F640J3A: | |
171 | printf ("28F640J3A\n"); | |
172 | break; | |
173 | case FLASH_28F128J3A: | |
174 | printf ("28F128J3A\n"); | |
175 | break; | |
176 | default: | |
177 | printf ("Unknown Chip Type\n"); | |
178 | break; | |
179 | } | |
180 | ||
181 | printf (" Size: %ld MB in %d Sectors\n", | |
182 | info->size >> 20, info->sector_count); | |
183 | ||
184 | printf (" Sector Start Addresses:"); | |
185 | for (i = 0; i < info->sector_count; ++i) { | |
186 | if ((i % 5) == 0) | |
187 | printf ("\n "); | |
188 | printf (" %08lX%s", | |
189 | info->start[i], | |
190 | info->protect[i] ? " (RO)" : " "); | |
191 | } | |
192 | printf ("\n"); | |
193 | return; | |
194 | } | |
195 | ||
196 | /*----------------------------------------------------------------------- | |
197 | */ | |
198 | ||
199 | ||
200 | /*----------------------------------------------------------------------- | |
201 | */ | |
202 | ||
203 | /* | |
204 | * The following code cannot be run from FLASH! | |
205 | */ | |
206 | ||
207 | static ulong flash_get_size (FPWV * addr, flash_info_t * info) | |
208 | { | |
209 | FPW value; | |
210 | ||
211 | addr[0] = (FPW) 0x00900090; | |
212 | ||
213 | value = addr[0]; | |
214 | ||
215 | debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value); | |
216 | ||
217 | switch (value) { | |
218 | case (FPW) INTEL_MANUFACT: | |
219 | info->flash_id = FLASH_MAN_INTEL; | |
220 | break; | |
221 | default: | |
222 | info->flash_id = FLASH_UNKNOWN; | |
223 | info->sector_count = 0; | |
224 | info->size = 0; | |
225 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ | |
226 | return (0); /* no or unknown flash */ | |
227 | } | |
228 | ||
229 | #ifdef FLASH_PORT_WIDTH8 | |
230 | value = addr[2]; /* device ID */ | |
231 | #else | |
232 | value = addr[1]; /* device ID */ | |
233 | #endif | |
234 | ||
235 | debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value); | |
236 | ||
237 | switch (value) { | |
238 | case (FPW) INTEL_ID_28F320J3A: | |
239 | info->flash_id += FLASH_28F320J3A; | |
240 | info->sector_count = 32; | |
241 | info->size = 0x00400000; | |
242 | break; /* => 4 MB */ | |
243 | ||
244 | case (FPW) INTEL_ID_28F640J3A: | |
245 | info->flash_id += FLASH_28F640J3A; | |
246 | info->sector_count = 64; | |
247 | info->size = 0x00800000; | |
248 | break; /* => 8 MB */ | |
249 | ||
250 | case (FPW) INTEL_ID_28F128J3A: | |
251 | info->flash_id += FLASH_28F128J3A; | |
252 | info->sector_count = 128; | |
253 | info->size = 0x01000000; | |
254 | break; /* => 16 MB */ | |
255 | ||
256 | default: | |
257 | info->flash_id = FLASH_UNKNOWN; | |
258 | break; | |
259 | } | |
260 | ||
6d0f6bcf | 261 | if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) { |
9dd41a7b | 262 | printf ("** ERROR: sector count %d > max (%d) **\n", |
6d0f6bcf JCPV |
263 | info->sector_count, CONFIG_SYS_MAX_FLASH_SECT); |
264 | info->sector_count = CONFIG_SYS_MAX_FLASH_SECT; | |
9dd41a7b WD |
265 | } |
266 | ||
267 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ | |
268 | ||
269 | return (info->size); | |
270 | } | |
271 | ||
272 | ||
273 | /*----------------------------------------------------------------------- | |
274 | */ | |
275 | ||
276 | int flash_erase (flash_info_t * info, int s_first, int s_last) | |
277 | { | |
278 | int flag, prot, sect; | |
279 | ulong type, start, now, last; | |
280 | int rcode = 0; | |
281 | ||
282 | if ((s_first < 0) || (s_first > s_last)) { | |
283 | if (info->flash_id == FLASH_UNKNOWN) { | |
284 | printf ("- missing\n"); | |
285 | } else { | |
286 | printf ("- no sectors to erase\n"); | |
287 | } | |
288 | return 1; | |
289 | } | |
290 | ||
291 | type = (info->flash_id & FLASH_VENDMASK); | |
292 | if ((type != FLASH_MAN_INTEL)) { | |
293 | printf ("Can't erase unknown flash type %08lx - aborted\n", | |
294 | info->flash_id); | |
295 | return 1; | |
296 | } | |
297 | ||
298 | prot = 0; | |
299 | for (sect = s_first; sect <= s_last; ++sect) { | |
300 | if (info->protect[sect]) { | |
301 | prot++; | |
302 | } | |
303 | } | |
304 | ||
305 | if (prot) { | |
306 | printf ("- Warning: %d protected sectors will not be erased!\n", | |
307 | prot); | |
308 | } else { | |
309 | printf ("\n"); | |
310 | } | |
311 | ||
312 | start = get_timer (0); | |
313 | last = start; | |
314 | /* Start erase on unprotected sectors */ | |
315 | for (sect = s_first; sect <= s_last; sect++) { | |
316 | if (info->protect[sect] == 0) { /* not protected */ | |
317 | FPWV *addr = (FPWV *) (info->start[sect]); | |
318 | FPW status; | |
319 | ||
320 | /* Disable interrupts which might cause a timeout here */ | |
321 | flag = disable_interrupts (); | |
322 | ||
323 | *addr = (FPW) 0x00500050; /* clear status register */ | |
324 | *addr = (FPW) 0x00200020; /* erase setup */ | |
325 | *addr = (FPW) 0x00D000D0; /* erase confirm */ | |
326 | ||
327 | /* re-enable interrupts if necessary */ | |
328 | if (flag) | |
329 | enable_interrupts (); | |
330 | ||
331 | /* wait at least 80us - let's wait 1 ms */ | |
332 | udelay (1000); | |
333 | ||
334 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { | |
6d0f6bcf | 335 | if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
9dd41a7b WD |
336 | printf ("Timeout\n"); |
337 | *addr = (FPW) 0x00B000B0; /* suspend erase */ | |
338 | *addr = (FPW) 0x00FF00FF; /* reset to read mode */ | |
339 | rcode = 1; | |
340 | break; | |
341 | } | |
342 | ||
343 | /* show that we're waiting */ | |
344 | if ((now - last) > 1000) { /* every second */ | |
345 | putc ('.'); | |
346 | last = now; | |
347 | } | |
348 | } | |
349 | ||
350 | *addr = (FPW) 0x00FF00FF; /* reset to read mode */ | |
351 | } | |
352 | } | |
353 | printf (" done\n"); | |
354 | return rcode; | |
355 | } | |
356 | ||
357 | /*----------------------------------------------------------------------- | |
358 | * Copy memory to flash, returns: | |
359 | * 0 - OK | |
360 | * 1 - write timeout | |
361 | * 2 - Flash not erased | |
362 | * 4 - Flash not identified | |
363 | */ | |
364 | ||
365 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) | |
366 | { | |
367 | ulong cp, wp; | |
368 | FPW data; | |
369 | ||
370 | int i, l, rc, port_width; | |
371 | ||
372 | if (info->flash_id == FLASH_UNKNOWN) { | |
373 | return 4; | |
374 | } | |
375 | /* get lower word aligned address */ | |
376 | #ifdef FLASH_PORT_WIDTH16 | |
377 | wp = (addr & ~1); | |
378 | port_width = 2; | |
379 | #elif defined(FLASH_PORT_WIDTH32) | |
380 | wp = (addr & ~3); | |
381 | port_width = 4; | |
382 | #else | |
383 | wp = addr; | |
384 | port_width = 1; | |
385 | #endif | |
386 | ||
387 | /* | |
388 | * handle unaligned start bytes | |
389 | */ | |
390 | if ((l = addr - wp) != 0) { | |
391 | data = 0; | |
392 | for (i = 0, cp = wp; i < l; ++i, ++cp) { | |
393 | data = (data << 8) | (*(uchar *) cp); | |
394 | } | |
395 | for (; i < port_width && cnt > 0; ++i) { | |
396 | data = (data << 8) | *src++; | |
397 | --cnt; | |
398 | ++cp; | |
399 | } | |
400 | for (; cnt == 0 && i < port_width; ++i, ++cp) { | |
401 | data = (data << 8) | (*(uchar *) cp); | |
402 | } | |
403 | ||
404 | if ((rc = write_data (info, wp, data)) != 0) { | |
405 | return (rc); | |
406 | } | |
407 | wp += port_width; | |
408 | } | |
409 | ||
410 | /* | |
411 | * handle word aligned part | |
412 | */ | |
413 | while (cnt >= port_width) { | |
414 | data = 0; | |
415 | for (i = 0; i < port_width; ++i) { | |
416 | data = (data << 8) | *src++; | |
417 | } | |
418 | if ((rc = write_data (info, wp, data)) != 0) { | |
419 | return (rc); | |
420 | } | |
421 | wp += port_width; | |
422 | cnt -= port_width; | |
423 | } | |
424 | ||
425 | if (cnt == 0) { | |
426 | return (0); | |
427 | } | |
428 | ||
429 | /* | |
430 | * handle unaligned tail bytes | |
431 | */ | |
432 | data = 0; | |
433 | for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) { | |
434 | data = (data << 8) | *src++; | |
435 | --cnt; | |
436 | } | |
437 | for (; i < port_width; ++i, ++cp) { | |
438 | data = (data << 8) | (*(uchar *) cp); | |
439 | } | |
440 | ||
441 | return (write_data (info, wp, data)); | |
442 | } | |
443 | ||
444 | /*----------------------------------------------------------------------- | |
445 | * Write a word or halfword to Flash, returns: | |
446 | * 0 - OK | |
447 | * 1 - write timeout | |
448 | * 2 - Flash not erased | |
449 | */ | |
450 | static int write_data (flash_info_t * info, ulong dest, FPW data) | |
451 | { | |
452 | FPWV *addr = (FPWV *) dest; | |
453 | ulong status; | |
454 | ulong start; | |
455 | int flag; | |
456 | ||
457 | /* Check if Flash is (sufficiently) erased */ | |
458 | if ((*addr & data) != data) { | |
459 | printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr); | |
460 | return (2); | |
461 | } | |
462 | /* Disable interrupts which might cause a timeout here */ | |
463 | flag = disable_interrupts (); | |
464 | ||
465 | *addr = (FPW) 0x00400040; /* write setup */ | |
466 | *addr = data; | |
467 | ||
468 | /* re-enable interrupts if necessary */ | |
469 | if (flag) | |
470 | enable_interrupts (); | |
471 | ||
472 | start = get_timer (0); | |
473 | ||
474 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { | |
6d0f6bcf | 475 | if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
9dd41a7b WD |
476 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ |
477 | return (1); | |
478 | } | |
479 | } | |
480 | ||
481 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ | |
482 | ||
483 | return (0); | |
484 | } |