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