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#include <config.h>
30#include <common.h>
31#include <flash.h>
32#include <asm/io.h>
33
34flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
35
36extern int hwc_flash_size(void);
37static ulong flash_get_size (u32 addr, flash_info_t *info);
38static int flash_get_offsets (u32 base, flash_info_t *info);
39static int write_word (flash_info_t *info, ulong dest, ulong data);
40static void flash_reset (u32 addr);
41
42#define out8(a,v) *(volatile unsigned char*)(a) = v
43#define in8(a) *(volatile unsigned char*)(a)
44#define in32(a) *(volatile unsigned long*)(a)
45#define iobarrier_rw() eieio()
46
47unsigned long flash_init (void)
48{
49 unsigned int i;
50 unsigned long flash_size = 0;
51 unsigned long bank_size;
52 unsigned int bank = 0;
53
54
55 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
56 flash_info[i].flash_id = FLASH_UNKNOWN;
57 flash_info[i].sector_count = 0;
58 flash_info[i].size = 0;
59 }
60
61
62 if (bank == CONFIG_SYS_MAX_FLASH_BANKS) {
63 puts ("Warning: not all Flashes are initialised !");
64 return flash_size;
65 }
66
67 bank_size = flash_get_size (CONFIG_SYS_FLASH_BASE, flash_info + bank);
68 if (bank_size) {
69#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE && \
70 CONFIG_SYS_MONITOR_BASE < CONFIG_SYS_FLASH_BASE + CONFIG_SYS_MAX_FLASH_SIZE
71
72 flash_protect(FLAG_PROTECT_SET,
73 CONFIG_SYS_MONITOR_BASE,
74 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
75 flash_info + bank);
76#endif
77
78#ifdef CONFIG_ENV_IS_IN_FLASH
79
80 flash_protect(FLAG_PROTECT_SET,
81 CONFIG_ENV_ADDR,
82 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
83 flash_info + bank);
84#endif
85
86
87 flash_protect(FLAG_PROTECT_SET,
88 CONFIG_SYS_FLASH_BASE,
89 CONFIG_SYS_FLASH_BASE + 0x10000 - 1,
90 flash_info + bank);
91
92 flash_size += bank_size;
93 bank++;
94 } else {
95 puts ("Warning: the BOOT Flash is not initialised !");
96 }
97
98 return flash_size;
99}
100
101
102
103
104static ulong flash_get_size (u32 addr, flash_info_t *info)
105{
106 volatile uchar value;
107#if 0
108 int i;
109#endif
110
111
112 out8(addr + 0x0555, 0xAA);
113 iobarrier_rw();
114 udelay(10);
115 out8(addr + 0x02AA, 0x55);
116 iobarrier_rw();
117 udelay(10);
118 out8(addr + 0x0555, 0x90);
119 iobarrier_rw();
120 udelay(10);
121
122 value = in8(addr);
123 iobarrier_rw();
124 udelay(10);
125 switch (value | (value << 16)) {
126 case AMD_MANUFACT:
127 info->flash_id = FLASH_MAN_AMD;
128 break;
129
130 case FUJ_MANUFACT:
131 info->flash_id = FLASH_MAN_FUJ;
132 break;
133
134 default:
135 info->flash_id = FLASH_UNKNOWN;
136 flash_reset (addr);
137 return 0;
138 }
139
140 value = in8(addr + 1);
141 iobarrier_rw();
142
143 switch (value) {
144 case AMD_ID_LV033C:
145 info->flash_id += FLASH_AM033C;
146 info->size = hwc_flash_size();
147 if (info->size > CONFIG_SYS_MAX_FLASH_SIZE) {
148 printf("U-Boot supports only %d MB\n",
149 CONFIG_SYS_MAX_FLASH_SIZE);
150 info->size = CONFIG_SYS_MAX_FLASH_SIZE;
151 }
152 info->sector_count = info->size / 0x10000;
153 break;
154
155 default:
156 info->flash_id = FLASH_UNKNOWN;
157 flash_reset (addr);
158 return (0);
159
160 }
161
162 if (!flash_get_offsets (addr, info)) {
163 flash_reset (addr);
164 return 0;
165 }
166
167#if 0
168
169 for (i = 0; i < info->sector_count; i++) {
170
171
172 value = in8(info->start[i] + 2);
173 iobarrier_rw();
174 info->protect[i] = (value & 1) != 0;
175 }
176#endif
177
178
179
180
181 flash_reset (addr);
182
183 return (info->size);
184}
185
186static int flash_get_offsets (u32 base, flash_info_t *info)
187{
188 unsigned int i, size;
189
190 switch (info->flash_id & FLASH_TYPEMASK) {
191 case FLASH_AM033C:
192
193 size = info->size / info->sector_count;
194 for (i = 0; i < info->sector_count; i++) {
195 info->start[i] = base + i * size;
196 }
197 break;
198 default:
199 return 0;
200 }
201
202 return 1;
203}
204
205int flash_erase (flash_info_t *info, int s_first, int s_last)
206{
207 volatile u32 addr = info->start[0];
208 int flag, prot, sect, l_sect;
209 ulong start, now, last;
210
211 if (s_first < 0 || s_first > s_last) {
212 if (info->flash_id == FLASH_UNKNOWN) {
213 printf ("- missing\n");
214 } else {
215 printf ("- no sectors to erase\n");
216 }
217 return 1;
218 }
219
220 if (info->flash_id == FLASH_UNKNOWN ||
221 info->flash_id > FLASH_AMD_COMP) {
222 printf ("Can't erase unknown flash type %08lx - aborted\n",
223 info->flash_id);
224 return 1;
225 }
226
227 prot = 0;
228 for (sect=s_first; sect<=s_last; ++sect) {
229 if (info->protect[sect]) {
230 prot++;
231 }
232 }
233
234 if (prot) {
235 printf ("- Warning: %d protected sectors will not be erased!\n",
236 prot);
237 } else {
238 printf ("\n");
239 }
240
241 l_sect = -1;
242
243
244 flag = disable_interrupts();
245
246 out8(addr + 0x555, 0xAA);
247 iobarrier_rw();
248 out8(addr + 0x2AA, 0x55);
249 iobarrier_rw();
250 out8(addr + 0x555, 0x80);
251 iobarrier_rw();
252 out8(addr + 0x555, 0xAA);
253 iobarrier_rw();
254 out8(addr + 0x2AA, 0x55);
255 iobarrier_rw();
256
257
258 for (sect = s_first; sect<=s_last; sect++) {
259 if (info->protect[sect] == 0) {
260 addr = info->start[sect];
261 out8(addr, 0x30);
262 iobarrier_rw();
263 l_sect = sect;
264 }
265 }
266
267
268 if (flag)
269 enable_interrupts();
270
271
272 udelay (1000);
273
274
275
276
277 if (l_sect < 0)
278 goto DONE;
279
280 start = get_timer (0);
281 last = start;
282 addr = info->start[l_sect];
283 while ((in8(addr) & 0x80) != 0x80) {
284 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
285 printf ("Timeout\n");
286 return 1;
287 }
288
289 if ((now - last) > 1000) {
290 putc ('.');
291 last = now;
292 }
293 iobarrier_rw();
294 }
295
296DONE:
297
298 flash_reset (info->start[0]);
299
300 printf (" done\n");
301 return 0;
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, data;
313 int i, l, rc;
314
315 wp = (addr & ~3);
316
317
318
319
320 if ((l = addr - wp) != 0) {
321 data = 0;
322 for (i=0, cp=wp; i<l; ++i, ++cp) {
323 data = (data << 8) | (*(uchar *)cp);
324 }
325 for (; i<4 && cnt>0; ++i) {
326 data = (data << 8) | *src++;
327 --cnt;
328 ++cp;
329 }
330 for (; cnt==0 && i<4; ++i, ++cp) {
331 data = (data << 8) | (*(uchar *)cp);
332 }
333
334 if ((rc = write_word(info, wp, data)) != 0) {
335 return (rc);
336 }
337 wp += 4;
338 }
339
340
341
342
343 while (cnt >= 4) {
344 data = 0;
345 for (i=0; i<4; ++i) {
346 data = (data << 8) | *src++;
347 }
348 if ((rc = write_word(info, wp, data)) != 0) {
349 return (rc);
350 }
351 wp += 4;
352 cnt -= 4;
353 }
354
355 if (cnt == 0) {
356 return (0);
357 }
358
359
360
361
362 data = 0;
363 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
364 data = (data << 8) | *src++;
365 --cnt;
366 }
367 for (; i<4; ++i, ++cp) {
368 data = (data << 8) | (*(uchar *)cp);
369 }
370
371 return (write_word(info, wp, data));
372}
373
374
375
376
377
378
379
380static int write_word (flash_info_t *info, ulong dest, ulong data)
381{
382 volatile u32 addr = info->start[0];
383 ulong start;
384 int flag, i;
385
386
387 if ((in32(dest) & data) != data) {
388 return (2);
389 }
390
391 flag = disable_interrupts();
392
393
394 out8(addr + 0x555, 0xAA);
395 iobarrier_rw();
396 out8(addr + 0x2AA, 0x55);
397 iobarrier_rw();
398 out8(addr + 0x555, 0x20);
399 iobarrier_rw();
400
401
402 for (i = 0; i < 4; i++) {
403 char *data_ch = (char *)&data;
404 out8(addr, 0xA0);
405 iobarrier_rw();
406 out8(dest+i, data_ch[i]);
407 iobarrier_rw();
408 udelay(10);
409 }
410
411
412 out8(addr, 0x90);
413 iobarrier_rw();
414 out8(addr, 0x00);
415 iobarrier_rw();
416
417
418 if (flag)
419 enable_interrupts();
420
421
422 start = get_timer (0);
423 while ((in32(dest) & 0x80808080) != (data & 0x80808080)) {
424 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
425 return (1);
426 }
427 iobarrier_rw();
428 }
429
430 flash_reset (addr);
431
432 return (0);
433}
434
435
436
437
438static void flash_reset (u32 addr)
439{
440 out8(addr, 0xF0);
441 iobarrier_rw();
442}
443
444void flash_print_info (flash_info_t *info)
445{
446 int i;
447
448 if (info->flash_id == FLASH_UNKNOWN) {
449 printf ("missing or unknown FLASH type\n");
450 return;
451 }
452
453 switch (info->flash_id & FLASH_VENDMASK) {
454 case FLASH_MAN_AMD: printf ("AMD "); break;
455 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
456 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
457 default: printf ("Unknown Vendor "); break;
458 }
459
460 switch (info->flash_id & FLASH_TYPEMASK) {
461 case FLASH_AM033C: printf ("AM29LV033C (32 Mbit, uniform sectors)\n");
462 break;
463 default: printf ("Unknown Chip Type\n");
464 break;
465 }
466
467 if (info->size % 0x100000 == 0) {
468 printf (" Size: %ld MB in %d Sectors\n",
469 info->size / 0x100000, info->sector_count);
470 }
471 else if (info->size % 0x400 == 0) {
472 printf (" Size: %ld KB in %d Sectors\n",
473 info->size / 0x400, info->sector_count);
474 }
475 else {
476 printf (" Size: %ld B in %d Sectors\n",
477 info->size, info->sector_count);
478 }
479
480 printf (" Sector Start Addresses:");
481 for (i=0; i<info->sector_count; ++i) {
482 if ((i % 5) == 0)
483 printf ("\n ");
484 printf (" %08lX%s",
485 info->start[i],
486 info->protect[i] ? " (RO)" : " "
487 );
488 }
489 printf ("\n");
490}
491