1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <mpc8xx.h>
26
27flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
28
29#if defined(CONFIG_ENV_IS_IN_FLASH)
30# ifndef CONFIG_ENV_ADDR
31# define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
32# endif
33# ifndef CONFIG_ENV_SIZE
34# define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
35# endif
36# ifndef CONFIG_ENV_SECT_SIZE
37# define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
38# endif
39#endif
40
41
42
43
44static ulong flash_get_size (vu_long *addr, flash_info_t *info);
45static int write_word (flash_info_t *info, ulong dest, ulong data);
46static void flash_get_offsets (ulong base, flash_info_t *info);
47
48
49
50
51unsigned long flash_init (void)
52{
53 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
54 volatile memctl8xx_t *memctl = &immap->im_memctl;
55 volatile ip860_bcsr_t *bcsr = (ip860_bcsr_t *)BCSR_BASE;
56 unsigned long size;
57 int i;
58
59
60
61
62 bcsr->bd_ctrl |= BD_CTRL_FLWE;
63
64 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
65 flash_info[i].flash_id = FLASH_UNKNOWN;
66 }
67
68
69
70 size = flash_get_size((vu_long *)FLASH_BASE, &flash_info[0]);
71
72 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
73 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
74 size, size<<20);
75 }
76
77
78 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
79 memctl->memc_br1 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
80 (memctl->memc_br1 & ~(BR_BA_MSK));
81
82
83 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
84
85 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
86
87 flash_info[0].size = size;
88
89#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
90
91 flash_protect(FLAG_PROTECT_SET,
92 CONFIG_SYS_MONITOR_BASE,
93 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
94 &flash_info[0]);
95#endif
96
97#ifdef CONFIG_ENV_IS_IN_FLASH
98
99 flash_protect(FLAG_PROTECT_SET,
100 CONFIG_ENV_ADDR,
101 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
102 &flash_info[0]);
103#endif
104 return (size);
105}
106
107
108
109static void flash_get_offsets (ulong base, flash_info_t *info)
110{
111 int i;
112
113
114
115
116
117
118
119
120 for (i = 0; i < info->sector_count; i++) {
121 info->start[i] = base;
122 base += 0x00020000;
123 }
124}
125
126
127
128void flash_print_info (flash_info_t *info)
129{
130 int i;
131
132 if (info->flash_id == FLASH_UNKNOWN) {
133 printf ("missing or unknown FLASH type\n");
134 return;
135 }
136
137 switch (info->flash_id & FLASH_VENDMASK) {
138 case FLASH_MAN_INTEL: printf ("Intel "); break;
139 default: printf ("Unknown Vendor "); break;
140 }
141
142 switch (info->flash_id & FLASH_TYPEMASK) {
143 case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
144 break;
145 case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
146 break;
147 case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
148 break;
149 default: printf ("Unknown Chip Type\n");
150 break;
151 }
152
153 printf (" Size: %ld MB in %d Sectors\n",
154 info->size >> 20, info->sector_count);
155
156 printf (" Sector Start Addresses:");
157 for (i=0; i<info->sector_count; ++i) {
158 if ((i % 5) == 0)
159 printf ("\n ");
160 printf (" %08lX%s",
161 info->start[i],
162 info->protect[i] ? " (RO)" : " "
163 );
164 }
165 printf ("\n");
166 return;
167}
168
169
170
171
172
173
174
175
176
177
178
179
180static ulong flash_get_size (vu_long *addr, flash_info_t *info)
181{
182 short i;
183 ulong value;
184 ulong base = (ulong)addr;
185
186
187 *addr = 0x90909090;
188
189 value = addr[0];
190 switch (value) {
191 case (MT_MANUFACT & 0x00FF00FF):
192 case (INTEL_ALT_MANU & 0x00FF00FF):
193 info->flash_id = FLASH_MAN_INTEL;
194 break;
195 default:
196 info->flash_id = FLASH_UNKNOWN;
197 info->sector_count = 0;
198 info->size = 0;
199 return (0);
200 }
201
202 value = addr[1];
203
204 switch (value) {
205 case (INTEL_ID_28F016S):
206 info->flash_id += FLASH_28F016SV;
207 info->sector_count = 32;
208 info->size = 0x00400000;
209 break;
210
211 case (INTEL_ID_28F160S3):
212 info->flash_id += FLASH_28F160S3;
213 info->sector_count = 32;
214 info->size = 0x00400000;
215 break;
216
217 case (INTEL_ID_28F320S3):
218 info->flash_id += FLASH_28F320S3;
219 info->sector_count = 64;
220 info->size = 0x00800000;
221 break;
222
223 default:
224 info->flash_id = FLASH_UNKNOWN;
225 return (0);
226
227 }
228
229
230 for (i = 0; i < info->sector_count; i++) {
231 info->start[i] = base + (i * 0x00020000);
232
233 info->protect[i] = 0;
234 }
235
236
237
238
239 if (info->flash_id != FLASH_UNKNOWN) {
240 addr = (vu_long *)info->start[0];
241
242 *addr = 0xFFFFFF;
243 }
244
245 return (info->size);
246}
247
248
249
250
251
252int flash_erase (flash_info_t *info, int s_first, int s_last)
253{
254 int flag, prot, sect;
255 ulong start, now, last;
256
257 if ((s_first < 0) || (s_first > s_last)) {
258 if (info->flash_id == FLASH_UNKNOWN) {
259 printf ("- missing\n");
260 } else {
261 printf ("- no sectors to erase\n");
262 }
263 return 1;
264 }
265
266 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
267 printf ("Can't erase unknown flash type %08lx - aborted\n",
268 info->flash_id);
269 return 1;
270 }
271
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
289
290 for (sect = s_first; sect<=s_last; sect++) {
291 if (info->protect[sect] == 0) {
292 vu_long *addr = (vu_long *)(info->start[sect]);
293
294
295 flag = disable_interrupts();
296
297
298 *addr = 0x20202020;
299
300 *addr = 0xD0D0D0D0;
301
302 *addr = 0xD0D0D0D0;
303
304
305 if (flag)
306 enable_interrupts();
307
308
309 udelay (1000);
310
311 while ((*addr & 0x00800080) != 0x00800080) {
312 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
313 printf ("Timeout\n");
314 *addr = 0xFFFFFFFF;
315 return 1;
316 }
317
318 if ((now - last) > 1000) {
319 putc ('.');
320 last = now;
321 }
322 }
323
324
325 *addr = 0xFFFFFFFF;
326 }
327 }
328
329 printf (" done\n");
330 return 0;
331}
332
333
334
335
336
337
338
339
340int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
341{
342 ulong cp, wp, data;
343 int i, l, rc;
344
345 wp = (addr & ~3);
346
347
348
349
350 if ((l = addr - wp) != 0) {
351 data = 0;
352 for (i=0, cp=wp; i<l; ++i, ++cp) {
353 data = (data << 8) | (*(uchar *)cp);
354 }
355 for (; i<4 && cnt>0; ++i) {
356 data = (data << 8) | *src++;
357 --cnt;
358 ++cp;
359 }
360 for (; cnt==0 && i<4; ++i, ++cp) {
361 data = (data << 8) | (*(uchar *)cp);
362 }
363
364 if ((rc = write_word(info, wp, data)) != 0) {
365 return (rc);
366 }
367 wp += 4;
368 }
369
370
371
372
373 while (cnt >= 4) {
374 data = 0;
375 for (i=0; i<4; ++i) {
376 data = (data << 8) | *src++;
377 }
378 if ((rc = write_word(info, wp, data)) != 0) {
379 return (rc);
380 }
381 wp += 4;
382 cnt -= 4;
383 }
384
385 if (cnt == 0) {
386 return (0);
387 }
388
389
390
391
392 data = 0;
393 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
394 data = (data << 8) | *src++;
395 --cnt;
396 }
397 for (; i<4; ++i, ++cp) {
398 data = (data << 8) | (*(uchar *)cp);
399 }
400
401 return (write_word(info, wp, data));
402}
403
404
405
406
407
408
409
410static int write_word (flash_info_t *info, ulong dest, ulong data)
411{
412 vu_long *addr = (vu_long *)dest;
413 ulong start, csr;
414 int flag;
415
416
417 if ((*addr & data) != data) {
418 return (2);
419 }
420
421 flag = disable_interrupts();
422
423
424 *addr = 0x10101010;
425
426
427 *addr = data;
428
429
430 if (flag)
431 enable_interrupts();
432
433
434 start = get_timer (0);
435 flag = 0;
436 while (((csr = *addr) & 0x00800080) != 0x00800080) {
437 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
438 flag = 1;
439 break;
440 }
441 }
442 if (csr & 0x00400040) {
443printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
444 flag = 1;
445 }
446
447
448 *addr = 0x50505050;
449
450 *addr = 0xFFFFFFFF;
451
452 return (flag);
453}
454
455
456
457