1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <common.h>
32#include <linux/byteorder/swab.h>
33
34#define PHYS_FLASH_SECT_SIZE 0x00020000
35flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
36
37
38#undef FLASH_PORT_WIDTH32
39#define FLASH_PORT_WIDTH16
40
41#ifdef FLASH_PORT_WIDTH16
42#define FLASH_PORT_WIDTH ushort
43#define FLASH_PORT_WIDTHV vu_short
44#define SWAP(x) __swab16(x)
45#else
46#define FLASH_PORT_WIDTH ulong
47#define FLASH_PORT_WIDTHV vu_long
48#define SWAP(x) __swab32(x)
49#endif
50
51#define FPW FLASH_PORT_WIDTH
52#define FPWV FLASH_PORT_WIDTHV
53
54#define mb() __asm__ __volatile__ ("" : : : "memory")
55
56
57
58typedef struct OrgDef {
59 unsigned int sector_number;
60 unsigned int sector_size;
61} OrgDef;
62
63
64
65OrgDef OrgIntel_28F256L18T[] = {
66 {4, 32 * 1024},
67 {255, 128 * 1024},
68};
69
70
71
72
73
74unsigned long flash_init (void);
75static ulong flash_get_size (FPW * addr, flash_info_t * info);
76static int write_data (flash_info_t * info, ulong dest, FPW data);
77static void flash_get_offsets (ulong base, flash_info_t * info);
78void inline spin_wheel (void);
79void flash_print_info (flash_info_t * info);
80void flash_unprotect_sectors (FPWV * addr);
81int flash_erase (flash_info_t * info, int s_first, int s_last);
82int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
83void flash_unlock(flash_info_t * info);
84
85
86
87
88unsigned long flash_init (void)
89{
90 int i;
91 ulong size = 0;
92
93 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
94 switch (i) {
95 case 0:
96 flash_get_size ((FPW *) CONFIG_SYS_FLASH_BASE, &flash_info[i]);
97 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[i]);
98
99 flash_unlock(&flash_info[i]);
100 break;
101 default:
102 panic ("configured too many flash banks!\n");
103 break;
104 }
105 size += flash_info[i].size;
106 }
107
108
109
110 flash_protect (FLAG_PROTECT_SET,
111 CONFIG_SYS_FLASH_BASE,
112 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
113
114 flash_protect (FLAG_PROTECT_SET,
115 CONFIG_ENV_ADDR,
116 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
117
118 return size;
119}
120
121
122
123void flash_unlock(flash_info_t * info)
124{
125 int j;
126 for (j=2;j<CONFIG_SYS_MAX_FLASH_SECT;j++){
127 FPWV *addr = (FPWV *) (info->start[j]);
128 flash_unprotect_sectors (addr);
129 *addr = (FPW) 0x00500050;
130 *addr = (FPW) 0x00FF00FF;
131 }
132}
133
134
135
136static void flash_get_offsets (ulong base, flash_info_t * info)
137{
138 int i;
139 OrgDef *pOrgDef;
140
141 pOrgDef = OrgIntel_28F256L18T;
142 if (info->flash_id == FLASH_UNKNOWN) {
143 return;
144 }
145
146 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
147 for (i = 0; i < info->sector_count; i++) {
148 if (i > 255) {
149 info->start[i] = base + (i * 0x8000);
150 info->protect[i] = 0;
151 } else {
152 info->start[i] = base +
153 (i * PHYS_FLASH_SECT_SIZE);
154 info->protect[i] = 0;
155 }
156 }
157 }
158}
159
160
161
162void flash_print_info (flash_info_t * info)
163{
164 int i;
165
166 if (info->flash_id == FLASH_UNKNOWN) {
167 printf ("missing or unknown FLASH type\n");
168 return;
169 }
170
171 switch (info->flash_id & FLASH_VENDMASK) {
172 case FLASH_MAN_INTEL:
173 printf ("INTEL ");
174 break;
175 default:
176 printf ("Unknown Vendor ");
177 break;
178 }
179
180 switch (info->flash_id & FLASH_TYPEMASK) {
181 case FLASH_28F256L18T:
182 printf ("FLASH 28F256L18T\n");
183 break;
184 default:
185 printf ("Unknown Chip Type\n");
186 break;
187 }
188
189 printf (" Size: %ld MB in %d Sectors\n",
190 info->size >> 20, info->sector_count);
191
192 printf (" Sector Start Addresses:");
193 for (i = 0; i < info->sector_count; ++i) {
194 if ((i % 5) == 0)
195 printf ("\n ");
196 printf (" %08lX%s",
197 info->start[i], info->protect[i] ? " (RO)" : " ");
198 }
199 printf ("\n");
200 return;
201}
202
203
204
205
206static ulong flash_get_size (FPW * addr, flash_info_t * info)
207{
208 volatile FPW value;
209
210
211 addr[0x5555] = (FPW) 0x00AA00AA;
212 addr[0x2AAA] = (FPW) 0x00550055;
213 addr[0x5555] = (FPW) 0x00900090;
214
215 mb ();
216 value = addr[0];
217
218 switch (value) {
219
220 case (FPW) INTEL_MANUFACT:
221 info->flash_id = FLASH_MAN_INTEL;
222 break;
223
224 default:
225 info->flash_id = FLASH_UNKNOWN;
226 info->sector_count = 0;
227 info->size = 0;
228 addr[0] = (FPW) 0x00FF00FF;
229 return (0);
230 }
231
232 mb ();
233 value = addr[1];
234 switch (value) {
235
236 case (FPW) (INTEL_ID_28F256L18T):
237 info->flash_id += FLASH_28F256L18T;
238 info->sector_count = 259;
239 info->size = 0x02000000;
240 break;
241
242 default:
243 info->flash_id = FLASH_UNKNOWN;
244 break;
245 }
246
247 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
248 printf ("** ERROR: sector count %d > max (%d) **\n",
249 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
250 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
251 }
252
253 addr[0] = (FPW) 0x00FF00FF;
254
255 return (info->size);
256}
257
258
259
260
261
262
263void flash_unprotect_sectors (FPWV * addr)
264{
265#define PD_FINTEL_WSMS_READY_MASK 0x0080
266
267 *addr = (FPW) 0x00500050;
268
269
270 *addr = (FPW) 0x00600060;
271 *addr = (FPW) 0x00D000D0;
272}
273
274
275
276
277
278int flash_erase (flash_info_t * info, int s_first, int s_last)
279{
280 int flag, prot, sect;
281 ulong type, start, last;
282 int rcode = 0;
283
284 if ((s_first < 0) || (s_first > s_last)) {
285 if (info->flash_id == FLASH_UNKNOWN) {
286 printf ("- missing\n");
287 } else {
288 printf ("- no sectors to erase\n");
289 }
290 return 1;
291 }
292
293 type = (info->flash_id & FLASH_VENDMASK);
294 if ((type != FLASH_MAN_INTEL)) {
295 printf ("Can't erase unknown flash type %08lx - aborted\n",
296 info->flash_id);
297 return 1;
298 }
299
300 prot = 0;
301 for (sect = s_first; sect <= s_last; ++sect) {
302 if (info->protect[sect]) {
303 prot++;
304 }
305 }
306
307 if (prot) {
308 printf ("- Warning: %d protected sectors will not be erased!\n",
309 prot);
310 } else {
311 printf ("\n");
312 }
313
314
315 start = get_timer (0);
316 last = start;
317
318
319 flag = disable_interrupts ();
320
321
322 for (sect = s_first; sect <= s_last; sect++) {
323 if (info->protect[sect] == 0) {
324 FPWV *addr = (FPWV *) (info->start[sect]);
325 FPW status;
326
327 printf ("Erasing sector %2d ... ", sect);
328
329 flash_unprotect_sectors (addr);
330
331
332 reset_timer_masked ();
333
334 *addr = (FPW) 0x00500050;
335 *addr = (FPW) 0x00200020;
336 *addr = (FPW) 0x00D000D0;
337
338 while (((status =
339 *addr) & (FPW) 0x00800080) !=
340 (FPW) 0x00800080) {
341 if (get_timer_masked () >
342 CONFIG_SYS_FLASH_ERASE_TOUT) {
343 printf ("Timeout\n");
344
345 *addr = (FPW) 0x00B000B0;
346
347 *addr = (FPW) 0x00FF00FF;
348 rcode = 1;
349 break;
350 }
351 }
352
353
354 *addr = (FPW) 0x00500050;
355 *addr = (FPW) 0x00FF00FF;
356 printf (" done\n");
357 }
358 }
359 return rcode;
360}
361
362
363
364
365
366
367
368
369
370int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
371{
372 ulong cp, wp;
373 FPW data;
374 int count, i, l, rc, port_width;
375
376 if (info->flash_id == FLASH_UNKNOWN) {
377 return 4;
378 }
379
380#ifdef FLASH_PORT_WIDTH16
381 wp = (addr & ~1);
382 port_width = 2;
383#else
384 wp = (addr & ~3);
385 port_width = 4;
386#endif
387
388
389
390
391 if ((l = addr - wp) != 0) {
392 data = 0;
393 for (i = 0, cp = wp; i < l; ++i, ++cp) {
394 data = (data << 8) | (*(uchar *) cp);
395 }
396 for (; i < port_width && cnt > 0; ++i) {
397 data = (data << 8) | *src++;
398 --cnt;
399 ++cp;
400 }
401 for (; cnt == 0 && i < port_width; ++i, ++cp) {
402 data = (data << 8) | (*(uchar *) cp);
403 }
404
405 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
406 return (rc);
407 }
408 wp += port_width;
409 }
410
411
412
413
414 count = 0;
415 while (cnt >= port_width) {
416 data = 0;
417 for (i = 0; i < port_width; ++i) {
418 data = (data << 8) | *src++;
419 }
420 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
421 return (rc);
422 }
423 wp += port_width;
424 cnt -= port_width;
425 if (count++ > 0x800) {
426 spin_wheel ();
427 count = 0;
428 }
429 }
430
431 if (cnt == 0) {
432 return (0);
433 }
434
435
436
437
438 data = 0;
439 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
440 data = (data << 8) | *src++;
441 --cnt;
442 }
443 for (; i < port_width; ++i, ++cp) {
444 data = (data << 8) | (*(uchar *) cp);
445 }
446
447 return (write_data (info, wp, SWAP (data)));
448}
449
450
451
452
453
454
455
456static int write_data (flash_info_t * info, ulong dest, FPW data)
457{
458 FPWV *addr = (FPWV *) dest;
459 ulong status;
460 int flag;
461
462
463 if ((*addr & data) != data) {
464 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
465 return (2);
466 }
467
468 flag = disable_interrupts ();
469 *addr = (FPW) 0x00400040;
470 *addr = data;
471
472
473 reset_timer_masked ();
474
475
476 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
477 if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT) {
478 *addr = (FPW) 0x00FF00FF;
479 return (1);
480 }
481 }
482 *addr = (FPW) 0x00FF00FF;
483 return (0);
484}
485
486void inline spin_wheel (void)
487{
488 static int p = 0;
489 static char w[] = "\\/-";
490
491 printf ("\010%c", w[p]);
492 (++p == 3) ? (p = 0) : 0;
493}
494