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
30
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 *)CONFIG_SYS_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size;
44 int i;
45
46
47 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
49 }
50
51
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
61 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
62 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
63 (memctl->memc_br0 & ~(BR_BA_MSK));
64
65
66 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
67
68 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
69
70#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
71
72 flash_protect(FLAG_PROTECT_SET,
73 CONFIG_SYS_MONITOR_BASE,
74 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
75 &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
90 if (info->flash_id & FLASH_BTYPE) {
91
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
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
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
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);
203 }
204
205 value = caddr[2];
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;
213
214 case (AMD_ID_LV400B & 0xFF):
215 info->flash_id += FLASH_AM400B;
216 info->sector_count = 11;
217 info->size = 0x00080000;
218 break;
219
220 case (AMD_ID_LV800T & 0xFF):
221 info->flash_id += FLASH_AM800T;
222 info->sector_count = 19;
223 info->size = 0x00100000;
224 break;
225
226 case (AMD_ID_LV800B & 0xFF):
227 info->flash_id += FLASH_AM800B;
228 info->sector_count = 19;
229 info->size = 0x00100000;
230 break;
231
232 case (AMD_ID_LV160T & 0xFF):
233 info->flash_id += FLASH_AM160T;
234 info->sector_count = 35;
235 info->size = 0x00200000;
236 break;
237
238 case (AMD_ID_LV160B & 0xFF):
239 info->flash_id += FLASH_AM160B;
240 info->sector_count = 35;
241 info->size = 0x00200000;
242 break;
243#if 0
244 case (AMD_ID_LV320T & 0xFF):
245 info->flash_id += FLASH_AM320T;
246 info->sector_count = 67;
247 info->size = 0x00400000;
248 break;
249
250 case (AMD_ID_LV320B & 0xFF):
251 info->flash_id += FLASH_AM320B;
252 info->sector_count = 67;
253 info->size = 0x00400000;
254 break;
255#endif
256 default:
257 info->flash_id = FLASH_UNKNOWN;
258 return (0);
259
260 }
261
262
263 if (info->flash_id & FLASH_BTYPE) {
264
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
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
284 for (i = 0; i < info->sector_count; i++) {
285
286 caddr = (volatile unsigned char *)(info->start[i]);
287 info->protect[i] = caddr[4] & 1;
288 }
289
290
291
292
293 if (info->flash_id != FLASH_UNKNOWN) {
294 caddr = (vu_char *)info->start[0];
295
296 *caddr = 0xF0;
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
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
354 for (sect = s_first; sect<=s_last; sect++) {
355 if (info->protect[sect] == 0) {
356 addr = (vu_char*)(info->start[sect]);
357 addr[0] = 0x30;
358 l_sect = sect;
359 }
360 }
361
362
363 if (flag)
364 enable_interrupts();
365
366
367 udelay (1000);
368
369
370
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)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
380 printf ("Timeout\n");
381 return 1;
382 }
383
384 if ((now - last) > 1000) {
385 putc ('.');
386 last = now;
387 }
388 }
389
390DONE:
391
392 addr = (vu_char *)info->start[0];
393 addr[0] = 0xF0;
394
395 printf (" done\n");
396 return 0;
397}
398
399
400
401
402
403
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
422
423
424
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
433 if ((*((vu_char *)dest) & data) != data) {
434 return (2);
435 }
436
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
446 if (flag)
447 enable_interrupts();
448
449
450 start = get_timer (0);
451 while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
452 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
453 return (1);
454 }
455 }
456 return (0);
457}
458
459
460
461