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#include <linux/byteorder/swab.h>
27
28flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
29
30
31
32
33#define FLAG_PROTECT_SET 0x01
34#define FLAG_PROTECT_CLEAR 0x02
35
36
37#undef FLASH_PORT_WIDTH32
38#define FLASH_PORT_WIDTH16
39
40#ifdef FLASH_PORT_WIDTH16
41#define FLASH_PORT_WIDTH ushort
42#define FLASH_PORT_WIDTHV vu_short
43#define SWAP(x) __swab16(x)
44#else
45#define FLASH_PORT_WIDTH ulong
46#define FLASH_PORT_WIDTHV vu_long
47#define SWAP(x) __swab32(x)
48#endif
49
50#define FPW FLASH_PORT_WIDTH
51#define FPWV FLASH_PORT_WIDTHV
52
53
54
55
56static ulong flash_get_size (FPW *addr, flash_info_t *info);
57static int write_data (flash_info_t *info, ulong dest, FPW data);
58static void flash_get_offsets (ulong base, flash_info_t *info);
59
60
61
62
63unsigned long flash_init (void)
64{
65 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
66 volatile memctl8xx_t *memctl = &immap->im_memctl;
67 unsigned long size_b0;
68 int i;
69
70
71 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
72 flash_info[i].flash_id = FLASH_UNKNOWN;
73 }
74
75
76 size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]);
77
78 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
79 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
80 size_b0, size_b0<<20);
81 }
82
83
84 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
85 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
86
87
88 size_b0 = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
89
90 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
91
92
93 (void)flash_protect(FLAG_PROTECT_SET,
94 CONFIG_SYS_FLASH_BASE,
95 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
96 &flash_info[0]);
97
98 flash_info[0].size = size_b0;
99
100 return (size_b0);
101}
102
103
104
105static void flash_get_offsets (ulong base, flash_info_t *info)
106{
107 int i;
108
109 if (info->flash_id == FLASH_UNKNOWN) {
110 return;
111 }
112
113 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
114 for (i = 0; i < info->sector_count; i++) {
115 info->start[i] = base + (i * 0x00020000);
116 }
117 }
118}
119
120
121
122void flash_print_info (flash_info_t *info)
123{
124 int i;
125
126 if (info->flash_id == FLASH_UNKNOWN) {
127 printf ("missing or unknown FLASH type\n");
128 return;
129 }
130
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_INTEL: printf ("INTEL "); break;
133 default: printf ("Unknown Vendor "); break;
134 }
135
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case FLASH_28F640J5 :
138 printf ("28F640J5 \n"); break;
139 default: printf ("Unknown Chip Type=0x%lXh\n",
140 info->flash_id & FLASH_TYPEMASK); break;
141 }
142
143 printf (" Size: %ld MB in %d Sectors\n",
144 info->size >> 20, info->sector_count);
145
146 printf (" Sector Start Addresses:");
147 for (i=0; i<info->sector_count; ++i) {
148 if ((i % 5) == 0)
149 printf ("\n ");
150 printf (" %08lX%s",
151 info->start[i],
152 info->protect[i] ? " (RO)" : " "
153 );
154 }
155 printf ("\n");
156}
157
158
159
160
161
162
163
164
165
166
167
168
169static ulong flash_get_size (FPW *addr, flash_info_t *info)
170{
171 FPW value;
172
173
174 addr[0x5555] = (FPW)0xAA00AA00;
175 addr[0x2AAA] = (FPW)0x55005500;
176 addr[0x5555] = (FPW)0x90009000;
177
178 value = SWAP(addr[0]);
179
180 switch (value) {
181 case (FPW)INTEL_MANUFACT:
182 info->flash_id = FLASH_MAN_INTEL;
183 break;
184 default:
185 info->flash_id = FLASH_UNKNOWN;
186 info->sector_count = 0;
187 info->size = 0;
188 addr[0] = (FPW)0xFF00FF00;
189 return (0);
190 }
191
192 value = SWAP(addr[1]);
193
194 switch (value) {
195 case (FPW)INTEL_ID_28F640J5 :
196 info->flash_id += FLASH_28F640J5 ;
197 info->sector_count = 64;
198 info->size = 0x00800000;
199 break;
200
201 default:
202 info->flash_id = FLASH_UNKNOWN;
203 break;
204 }
205
206 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
207 printf ("** ERROR: sector count %d > max (%d) **\n",
208 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
209 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
210 }
211
212 addr[0] = (FPW)0xFF00FF00;
213
214 return (info->size);
215}
216
217
218
219
220
221int flash_erase (flash_info_t *info, int s_first, int s_last)
222{
223 int flag, prot, sect;
224 ulong type, start, now, last;
225 int rc = 0;
226
227 if ((s_first < 0) || (s_first > s_last)) {
228 if (info->flash_id == FLASH_UNKNOWN) {
229 printf ("- missing\n");
230 } else {
231 printf ("- no sectors to erase\n");
232 }
233 return 1;
234 }
235
236 type = (info->flash_id & FLASH_VENDMASK);
237 if ((type != FLASH_MAN_INTEL)) {
238 printf ("Can't erase unknown flash type %08lx - aborted\n",
239 info->flash_id);
240 return 1;
241 }
242
243 prot = 0;
244 for (sect=s_first; sect<=s_last; ++sect) {
245 if (info->protect[sect]) {
246 prot++;
247 }
248 }
249
250 if (prot) {
251 printf ("- Warning: %d protected sectors will not be erased!\n",
252 prot);
253 } else {
254 printf ("\n");
255 }
256
257 start = get_timer (0);
258 last = start;
259
260 for (sect = s_first; sect<=s_last; sect++) {
261 if (info->protect[sect] == 0) {
262 FPWV *addr = (FPWV *)(info->start[sect]);
263 FPW status;
264
265
266 flag = disable_interrupts();
267
268 *addr = (FPW)0x50005000;
269 *addr = (FPW)0x20002000;
270 *addr = (FPW)0xD000D000;
271
272
273 if (flag)
274 enable_interrupts();
275
276
277 udelay (1000);
278
279 while (((status = SWAP(*addr)) & (FPW)0x00800080) != (FPW)0x00800080) {
280 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
281 printf ("Timeout\n");
282 *addr = (FPW)0xB000B000;
283 *addr = (FPW)0xFF00FF00;
284 rc = 1;
285 break;
286 }
287
288
289 if ((now - last) > 1000) {
290 putc ('.');
291 last = now;
292 }
293 }
294
295 *addr = (FPW)0xFF00FF00;
296 printf (" done\n");
297 }
298 }
299 return rc;
300}
301
302
303
304
305
306
307
308
309
310int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
311{
312 ulong cp, wp;
313 FPW data;
314 int count, i, l, rc, port_width;
315
316 if (info->flash_id == FLASH_UNKNOWN) {
317 return 4;
318 }
319
320#ifdef FLASH_PORT_WIDTH16
321 wp = (addr & ~1);
322 port_width = 2;
323#else
324 wp = (addr & ~3);
325 port_width = 4;
326#endif
327
328
329
330
331 if ((l = addr - wp) != 0) {
332 data = 0;
333 for (i=0, cp=wp; i<l; ++i, ++cp) {
334 data = (data << 8) | (*(uchar *)cp);
335 }
336 for (; i<port_width && cnt>0; ++i) {
337 data = (data << 8) | *src++;
338 --cnt;
339 ++cp;
340 }
341 for (; cnt==0 && i<port_width; ++i, ++cp) {
342 data = (data << 8) | (*(uchar *)cp);
343 }
344
345 if ((rc = write_data(info, wp, data)) != 0) {
346 return (rc);
347 }
348 wp += port_width;
349 }
350
351
352
353
354 count = 0;
355 while (cnt >= port_width) {
356 data = 0;
357 for (i=0; i<port_width; ++i) {
358 data = (data << 8) | *src++;
359 }
360 if ((rc = write_data(info, wp, data)) != 0) {
361 return (rc);
362 }
363 wp += port_width;
364 cnt -= port_width;
365 if ((wp & 0xfff) == 0)
366 {
367 printf("%08lX",wp);
368 printf("\x1b[8D");
369 }
370 }
371
372 if (cnt == 0) {
373 return (0);
374 }
375
376
377
378
379 data = 0;
380 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
381 data = (data << 8) | *src++;
382 --cnt;
383 }
384 for (; i<port_width; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *)cp);
386 }
387
388 return (write_data(info, wp, data));
389}
390
391
392
393
394
395
396
397static int write_data (flash_info_t *info, ulong dest, FPW data)
398{
399 FPWV *addr = (FPWV *)dest;
400 ulong status;
401 ulong start;
402 int flag;
403
404
405 if ((*addr & data) != data) {
406 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
407 return (2);
408 }
409
410 flag = disable_interrupts();
411
412 *addr = (FPW)0x40004000;
413 *addr = data;
414
415
416 if (flag)
417 enable_interrupts();
418
419 start = get_timer (0);
420
421 while (((status = SWAP(*addr)) & (FPW)0x00800080) != (FPW)0x00800080) {
422 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
423 *addr = (FPW)0xFF00FF00;
424 return (1);
425 }
426 }
427
428 *addr = (FPW)0xFF00FF00;
429
430 return (0);
431}
432