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#include <common.h>
28#include <linux/byteorder/swab.h>
29
30
31flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
32
33
34#define FLASH_PORT_WIDTH32
35#undef FLASH_PORT_WIDTH16
36
37#ifdef FLASH_PORT_WIDTH16
38#define FLASH_PORT_WIDTH ushort
39#define FLASH_PORT_WIDTHV vu_short
40#define SWAP(x) __swab16(x)
41#else
42#define FLASH_PORT_WIDTH ulong
43#define FLASH_PORT_WIDTHV vu_long
44#define SWAP(x) __swab32(x)
45#endif
46
47#define FPW FLASH_PORT_WIDTH
48#define FPWV FLASH_PORT_WIDTHV
49
50#define mb() __asm__ __volatile__ ("" : : : "memory")
51
52
53
54
55static ulong flash_get_size (FPW *addr, flash_info_t *info);
56static int write_data (flash_info_t *info, ulong dest, FPW data);
57static void flash_get_offsets (ulong base, flash_info_t *info);
58void inline spin_wheel (void);
59
60
61
62
63unsigned long flash_init (void)
64{
65 int i;
66 ulong size = 0;
67
68 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
69 switch (i) {
70 case 0:
71 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
72 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
73 break;
74 case 1:
75 flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
76 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
77 break;
78 default:
79 panic ("configured too many flash banks!\n");
80 break;
81 }
82 size += flash_info[i].size;
83 }
84
85
86
87 flash_protect ( FLAG_PROTECT_SET,
88 CONFIG_SYS_FLASH_BASE,
89 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
90 &flash_info[0] );
91
92 flash_protect ( FLAG_PROTECT_SET,
93 CONFIG_ENV_ADDR,
94 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
95
96 return size;
97}
98
99
100
101static void flash_get_offsets (ulong base, flash_info_t *info)
102{
103 int i;
104
105 if (info->flash_id == FLASH_UNKNOWN) {
106 return;
107 }
108
109 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
110 for (i = 0; i < info->sector_count; i++) {
111 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
112 info->protect[i] = 0;
113 }
114 }
115}
116
117
118
119void flash_print_info (flash_info_t *info)
120{
121 int i;
122
123 if (info->flash_id == FLASH_UNKNOWN) {
124 printf ("missing or unknown FLASH type\n");
125 return;
126 }
127
128 switch (info->flash_id & FLASH_VENDMASK) {
129 case FLASH_MAN_INTEL:
130 printf ("INTEL ");
131 break;
132 default:
133 printf ("Unknown Vendor ");
134 break;
135 }
136
137 switch (info->flash_id & FLASH_TYPEMASK) {
138 case FLASH_28F128J3A:
139 printf ("28F128J3A\n");
140 break;
141 default:
142 printf ("Unknown Chip Type\n");
143 break;
144 }
145
146 printf (" Size: %ld MB in %d Sectors\n",
147 info->size >> 20, info->sector_count);
148
149 printf (" Sector Start Addresses:");
150 for (i = 0; i < info->sector_count; ++i) {
151 if ((i % 5) == 0)
152 printf ("\n ");
153 printf (" %08lX%s",
154 info->start[i],
155 info->protect[i] ? " (RO)" : " ");
156 }
157 printf ("\n");
158 return;
159}
160
161
162
163
164static ulong flash_get_size (FPW *addr, flash_info_t *info)
165{
166 volatile FPW value;
167
168
169 addr[0x5555] = (FPW) 0x00AA00AA;
170 addr[0x2AAA] = (FPW) 0x00550055;
171 addr[0x5555] = (FPW) 0x00900090;
172
173 mb ();
174 value = addr[0];
175
176 switch (value) {
177
178 case (FPW) INTEL_MANUFACT:
179 info->flash_id = FLASH_MAN_INTEL;
180 break;
181
182 default:
183 info->flash_id = FLASH_UNKNOWN;
184 info->sector_count = 0;
185 info->size = 0;
186 addr[0] = (FPW) 0x00FF00FF;
187 return (0);
188 }
189
190 mb ();
191 value = addr[1];
192
193 switch (value) {
194
195 case (FPW) INTEL_ID_28F128J3A:
196 info->flash_id += FLASH_28F128J3A;
197 info->sector_count = 128;
198 info->size = 0x02000000;
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) 0x00FF00FF;
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 prot, sect;
224 ulong type, start;
225 int rcode = 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
258 disable_interrupts();
259
260
261 for (sect = s_first; sect <= s_last; sect++) {
262 if (info->protect[sect] == 0) {
263 FPWV *addr = (FPWV *) (info->start[sect]);
264 FPW status;
265
266 printf ("Erasing sector %2d ... ", sect);
267
268
269 start = get_timer(0);
270
271 *addr = (FPW) 0x00500050;
272 *addr = (FPW) 0x00200020;
273 *addr = (FPW) 0x00D000D0;
274
275 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
276 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
277 printf ("Timeout\n");
278 *addr = (FPW) 0x00B000B0;
279 *addr = (FPW) 0x00FF00FF;
280 rcode = 1;
281 break;
282 }
283 }
284
285 *addr = 0x00500050;
286 *addr = 0x00FF00FF;
287
288 printf (" done\n");
289 }
290 }
291 return rcode;
292}
293
294
295
296
297
298
299
300
301
302int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
303{
304 ulong cp, wp;
305 FPW data;
306 int count, i, l, rc, port_width;
307
308 if (info->flash_id == FLASH_UNKNOWN) {
309 return 4;
310 }
311
312#ifdef FLASH_PORT_WIDTH16
313 wp = (addr & ~1);
314 port_width = 2;
315#else
316 wp = (addr & ~3);
317 port_width = 4;
318#endif
319
320
321
322
323 if ((l = addr - wp) != 0) {
324 data = 0;
325 for (i = 0, cp = wp; i < l; ++i, ++cp) {
326 data = (data << 8) | (*(uchar *) cp);
327 }
328 for (; i < port_width && cnt > 0; ++i) {
329 data = (data << 8) | *src++;
330 --cnt;
331 ++cp;
332 }
333 for (; cnt == 0 && i < port_width; ++i, ++cp) {
334 data = (data << 8) | (*(uchar *) cp);
335 }
336
337 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
338 return (rc);
339 }
340 wp += port_width;
341 }
342
343
344
345
346 count = 0;
347 while (cnt >= port_width) {
348 data = 0;
349 for (i = 0; i < port_width; ++i) {
350 data = (data << 8) | *src++;
351 }
352 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
353 return (rc);
354 }
355 wp += port_width;
356 cnt -= port_width;
357 if (count++ > 0x800) {
358 spin_wheel ();
359 count = 0;
360 }
361 }
362
363 if (cnt == 0) {
364 return (0);
365 }
366
367
368
369
370 data = 0;
371 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
372 data = (data << 8) | *src++;
373 --cnt;
374 }
375 for (; i < port_width; ++i, ++cp) {
376 data = (data << 8) | (*(uchar *) cp);
377 }
378
379 return (write_data (info, wp, SWAP (data)));
380}
381
382
383
384
385
386
387
388static int write_data (flash_info_t *info, ulong dest, FPW data)
389{
390 FPWV *addr = (FPWV *) dest;
391 ulong status;
392 ulong start;
393
394
395 if ((*addr & data) != data) {
396 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
397 return (2);
398 }
399
400 disable_interrupts();
401
402 *addr = (FPW) 0x00400040;
403 *addr = data;
404
405
406 start = get_timer(0);
407
408
409 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
410 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
411 *addr = (FPW) 0x00FF00FF;
412 return (1);
413 }
414 }
415
416 *addr = (FPW) 0x00FF00FF;
417
418 return (0);
419}
420
421void inline spin_wheel (void)
422{
423 static int p = 0;
424 static char w[] = "\\/-";
425
426 printf ("\010%c", w[p]);
427 (++p == 3) ? (p = 0) : 0;
428}
429