~ubuntu-branches/ubuntu/maverick/u-boot-omap3/maverick

« back to all changes in this revision

Viewing changes to board/prodrive/common/flash.c

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Grawert
  • Date: 2010-03-22 15:06:23 UTC
  • Revision ID: james.westby@ubuntu.com-20100322150623-i21g8rgiyl5dohag
Tags: upstream-2010.3git20100315
ImportĀ upstreamĀ versionĀ 2010.3git20100315

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * (C) Copyright 2006
 
3
 * Stefan Roese, DENX Software Engineering, sr@denx.de.
 
4
 *
 
5
 * See file CREDITS for list of people who contributed to this
 
6
 * project.
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation; either version 2 of
 
11
 * the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 
21
 * MA 02111-1307 USA
 
22
 */
 
23
 
 
24
#include <common.h>
 
25
#include <asm/processor.h>
 
26
 
 
27
flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
 
28
 
 
29
/*
 
30
 * Functions
 
31
 */
 
32
static int write_word(flash_info_t *info, ulong dest, ulong data);
 
33
 
 
34
void flash_print_info(flash_info_t *info)
 
35
{
 
36
        int i;
 
37
        int k;
 
38
        int size;
 
39
        int erased;
 
40
        volatile unsigned long *flash;
 
41
 
 
42
        if (info->flash_id == FLASH_UNKNOWN) {
 
43
                printf ("missing or unknown FLASH type\n");
 
44
                return;
 
45
        }
 
46
 
 
47
        switch (info->flash_id & FLASH_VENDMASK) {
 
48
        case FLASH_MAN_AMD:     printf ("AMD ");                break;
 
49
        case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
 
50
        case FLASH_MAN_SST:     printf ("SST ");                break;
 
51
        case FLASH_MAN_STM:     printf ("ST ");                 break;
 
52
        case FLASH_MAN_EXCEL:   printf ("Excel Semiconductor "); break;
 
53
        default:                printf ("Unknown Vendor ");     break;
 
54
        }
 
55
 
 
56
        switch (info->flash_id & FLASH_TYPEMASK) {
 
57
        case FLASH_AM400B:      printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
 
58
                break;
 
59
        case FLASH_AM400T:      printf ("AM29LV400T (4 Mbit, top boot sector)\n");
 
60
                break;
 
61
        case FLASH_AM800B:      printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
 
62
                break;
 
63
        case FLASH_AM800T:      printf ("AM29LV800T (8 Mbit, top boot sector)\n");
 
64
                break;
 
65
        case FLASH_AM160B:      printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
 
66
                break;
 
67
        case FLASH_AM160T:      printf ("AM29LV160T (16 Mbit, top boot sector)\n");
 
68
                break;
 
69
        case FLASH_AM320T:      printf ("AM29LV320T (32 M, top sector)\n");
 
70
                break;
 
71
        case FLASH_AM320B:      printf ("AM29LV320B (32 M, bottom sector)\n");
 
72
                break;
 
73
        case FLASH_AMDL322T:    printf ("AM29DL322T (32 M, top sector)\n");
 
74
                break;
 
75
        case FLASH_AMDL322B:    printf ("AM29DL322B (32 M, bottom sector)\n");
 
76
                break;
 
77
        case FLASH_AMDL323T:    printf ("AM29DL323T (32 M, top sector)\n");
 
78
                break;
 
79
        case FLASH_AMDL323B:    printf ("AM29DL323B (32 M, bottom sector)\n");
 
80
                break;
 
81
        case FLASH_SST020:      printf ("SST39LF/VF020 (2 Mbit, uniform sector size)\n");
 
82
                break;
 
83
        case FLASH_SST040:      printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
 
84
                break;
 
85
        default:                printf ("Unknown Chip Type\n");
 
86
                break;
 
87
        }
 
88
 
 
89
        printf ("  Size: %ld MB in %d Sectors\n",
 
90
                info->size >> 20, info->sector_count);
 
91
 
 
92
        printf ("  Sector Start Addresses:");
 
93
        for (i=0; i<info->sector_count; ++i) {
 
94
#ifdef CONFIG_SYS_FLASH_EMPTY_INFO
 
95
                /*
 
96
                 * Check if whole sector is erased
 
97
                 */
 
98
                if (i != (info->sector_count-1))
 
99
                        size = info->start[i+1] - info->start[i];
 
100
                else
 
101
                        size = info->start[0] + info->size - info->start[i];
 
102
                erased = 1;
 
103
                flash = (volatile unsigned long *)info->start[i];
 
104
                size = size >> 2;        /* divide by 4 for longword access */
 
105
                for (k=0; k<size; k++) {
 
106
                        if (*flash++ != 0xffffffff) {
 
107
                                erased = 0;
 
108
                                break;
 
109
                        }
 
110
                }
 
111
 
 
112
                if ((i % 5) == 0)
 
113
                        printf ("\n   ");
 
114
                /* print empty and read-only info */
 
115
                printf (" %08lX%s%s",
 
116
                        info->start[i],
 
117
                        erased ? " E" : "  ",
 
118
                        info->protect[i] ? "RO " : "   ");
 
119
#else
 
120
                if ((i % 5) == 0)
 
121
                        printf ("\n   ");
 
122
                printf (" %08lX%s",
 
123
                        info->start[i],
 
124
                        info->protect[i] ? " (RO)" : "     ");
 
125
#endif
 
126
 
 
127
        }
 
128
        printf ("\n");
 
129
        return;
 
130
}
 
131
 
 
132
/*
 
133
 * The following code cannot be run from FLASH!
 
134
 */
 
135
static ulong flash_get_size(vu_long *addr, flash_info_t *info)
 
136
{
 
137
        short i;
 
138
        short n;
 
139
        CONFIG_SYS_FLASH_WORD_SIZE value;
 
140
        ulong base = (ulong)addr;
 
141
        volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
 
142
 
 
143
        /* Write auto select command: read Manufacturer ID */
 
144
        addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
 
145
        addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
 
146
        addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
 
147
 
 
148
        value = addr2[CONFIG_SYS_FLASH_READ0];
 
149
 
 
150
        switch (value) {
 
151
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
 
152
                info->flash_id = FLASH_MAN_AMD;
 
153
                break;
 
154
        case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
 
155
                info->flash_id = FLASH_MAN_FUJ;
 
156
                break;
 
157
        case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
 
158
                info->flash_id = FLASH_MAN_SST;
 
159
                break;
 
160
        case (CONFIG_SYS_FLASH_WORD_SIZE)STM_MANUFACT:
 
161
                info->flash_id = FLASH_MAN_STM;
 
162
                break;
 
163
        case (CONFIG_SYS_FLASH_WORD_SIZE)EXCEL_MANUFACT:
 
164
                info->flash_id = FLASH_MAN_EXCEL;
 
165
                break;
 
166
        default:
 
167
                info->flash_id = FLASH_UNKNOWN;
 
168
                info->sector_count = 0;
 
169
                info->size = 0;
 
170
                return (0);                     /* no or unknown flash  */
 
171
        }
 
172
 
 
173
        value = addr2[CONFIG_SYS_FLASH_READ1];          /* device ID            */
 
174
 
 
175
        switch (value) {
 
176
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
 
177
                info->flash_id += FLASH_AM400T;
 
178
                info->sector_count = 11;
 
179
                info->size = 0x00080000;
 
180
                break;                          /* => 0.5 MB            */
 
181
 
 
182
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
 
183
                info->flash_id += FLASH_AM400B;
 
184
                info->sector_count = 11;
 
185
                info->size = 0x00080000;
 
186
                break;                          /* => 0.5 MB            */
 
187
 
 
188
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
 
189
                info->flash_id += FLASH_AM800T;
 
190
                info->sector_count = 19;
 
191
                info->size = 0x00100000;
 
192
                break;                          /* => 1 MB              */
 
193
 
 
194
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
 
195
                info->flash_id += FLASH_AM800B;
 
196
                info->sector_count = 19;
 
197
                info->size = 0x00100000;
 
198
                break;                          /* => 1 MB              */
 
199
 
 
200
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
 
201
                info->flash_id += FLASH_AM160T;
 
202
                info->sector_count = 35;
 
203
                info->size = 0x00200000;
 
204
                break;                          /* => 2 MB              */
 
205
 
 
206
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
 
207
                info->flash_id += FLASH_AM160B;
 
208
                info->sector_count = 35;
 
209
                info->size = 0x00200000;
 
210
                break;                          /* => 2 MB              */
 
211
 
 
212
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
 
213
                info->flash_id += FLASH_AM320T;
 
214
                info->sector_count = 71;
 
215
                info->size = 0x00400000;  break;        /* => 4 MB      */
 
216
 
 
217
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
 
218
                info->flash_id += FLASH_AM320B;
 
219
                info->sector_count = 71;
 
220
                info->size = 0x00400000;  break;        /* => 4 MB      */
 
221
 
 
222
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
 
223
                info->flash_id += FLASH_AMDL322T;
 
224
                info->sector_count = 71;
 
225
                info->size = 0x00400000;  break;        /* => 4 MB      */
 
226
 
 
227
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
 
228
                info->flash_id += FLASH_AMDL322B;
 
229
                info->sector_count = 71;
 
230
                info->size = 0x00400000;  break;        /* => 4 MB      */
 
231
 
 
232
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
 
233
                info->flash_id += FLASH_AMDL323T;
 
234
                info->sector_count = 71;
 
235
                info->size = 0x00400000;  break;        /* => 4 MB      */
 
236
 
 
237
        case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
 
238
                info->flash_id += FLASH_AMDL323B;
 
239
                info->sector_count = 71;
 
240
                info->size = 0x00400000;  break;        /* => 4 MB      */
 
241
 
 
242
        case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF020:
 
243
                info->flash_id += FLASH_SST020;
 
244
                info->sector_count = 64;
 
245
                info->size = 0x00040000;
 
246
                break;                          /* => 256 kB            */
 
247
 
 
248
        case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF040:
 
249
                info->flash_id += FLASH_SST040;
 
250
                info->sector_count = 128;
 
251
                info->size = 0x00080000;
 
252
                break;                          /* => 512 kB            */
 
253
 
 
254
        default:
 
255
                info->flash_id = FLASH_UNKNOWN;
 
256
                return (0);                     /* => no or unknown flash */
 
257
 
 
258
        }
 
259
 
 
260
        /* set up sector start address table */
 
261
        if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
 
262
                for (i = 0; i < info->sector_count; i++)
 
263
                        info->start[i] = base + (i * 0x00001000);
 
264
        } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
 
265
                   ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
 
266
                   ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
 
267
                   ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
 
268
                /* set sector offsets for bottom boot block type        */
 
269
                for (i=0; i<8; ++i) {           /*  8 x 8k boot sectors */
 
270
                        info->start[i] = base;
 
271
                        base += 8 << 10;
 
272
                }
 
273
                while (i < info->sector_count) {        /* 64k regular sectors  */
 
274
                        info->start[i] = base;
 
275
                        base += 64 << 10;
 
276
                        ++i;
 
277
                }
 
278
        } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
 
279
                   ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
 
280
                   ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
 
281
                   ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
 
282
                /* set sector offsets for top boot block type           */
 
283
                base += info->size;
 
284
                i = info->sector_count;
 
285
                for (n=0; n<8; ++n) {           /*  8 x 8k boot sectors */
 
286
                        base -= 8 << 10;
 
287
                        --i;
 
288
                        info->start[i] = base;
 
289
                }
 
290
                while (i > 0) {                 /* 64k regular sectors  */
 
291
                        base -= 64 << 10;
 
292
                        --i;
 
293
                        info->start[i] = base;
 
294
                }
 
295
        } else {
 
296
                if (info->flash_id & FLASH_BTYPE) {
 
297
                        /* set sector offsets for bottom boot block type        */
 
298
                        info->start[0] = base + 0x00000000;
 
299
                        info->start[1] = base + 0x00004000;
 
300
                        info->start[2] = base + 0x00006000;
 
301
                        info->start[3] = base + 0x00008000;
 
302
                        for (i = 4; i < info->sector_count; i++) {
 
303
                                info->start[i] = base + (i * 0x00010000) - 0x00030000;
 
304
                        }
 
305
                } else {
 
306
                        /* set sector offsets for top boot block type           */
 
307
                        i = info->sector_count - 1;
 
308
                        info->start[i--] = base + info->size - 0x00004000;
 
309
                        info->start[i--] = base + info->size - 0x00006000;
 
310
                        info->start[i--] = base + info->size - 0x00008000;
 
311
                        for (; i >= 0; i--) {
 
312
                                info->start[i] = base + i * 0x00010000;
 
313
                        }
 
314
                }
 
315
        }
 
316
 
 
317
        /* check for protected sectors */
 
318
        for (i = 0; i < info->sector_count; i++) {
 
319
                /* read sector protection at sector address, (A7 .. A0) = 0x02 */
 
320
                /* D0 = 1 if protected */
 
321
                addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
 
322
                if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
 
323
                        info->protect[i] = 0;
 
324
                else
 
325
                        info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
 
326
        }
 
327
 
 
328
        /*
 
329
         * Prevent writes to uninitialized FLASH.
 
330
         */
 
331
        if (info->flash_id != FLASH_UNKNOWN) {
 
332
                addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
 
333
                *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0;        /* reset bank */
 
334
        }
 
335
 
 
336
        return (info->size);
 
337
}
 
338
 
 
339
 
 
340
int flash_erase(flash_info_t *info, int s_first, int s_last)
 
341
{
 
342
        volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
 
343
        volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
 
344
        int flag, prot, sect, l_sect;
 
345
        ulong start, now, last;
 
346
 
 
347
        if ((s_first < 0) || (s_first > s_last)) {
 
348
                if (info->flash_id == FLASH_UNKNOWN)
 
349
                        printf ("- missing\n");
 
350
                else
 
351
                        printf ("- no sectors to erase\n");
 
352
                return 1;
 
353
        }
 
354
 
 
355
        if (info->flash_id == FLASH_UNKNOWN) {
 
356
                printf ("Can't erase unknown flash type - aborted\n");
 
357
                return 1;
 
358
        }
 
359
 
 
360
        prot = 0;
 
361
        for (sect=s_first; sect<=s_last; ++sect)
 
362
                if (info->protect[sect])
 
363
                        prot++;
 
364
 
 
365
        if (prot)
 
366
                printf ("- Warning: %d protected sectors will not be erased!\n", prot);
 
367
        else
 
368
                printf ("\n");
 
369
 
 
370
        l_sect = -1;
 
371
 
 
372
        /* Disable interrupts which might cause a timeout here */
 
373
        flag = disable_interrupts();
 
374
 
 
375
        /* Start erase on unprotected sectors */
 
376
        for (sect = s_first; sect<=s_last; sect++) {
 
377
                if (info->protect[sect] == 0) { /* not protected */
 
378
                        addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
 
379
                        if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
 
380
                                addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
 
381
                                addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
 
382
                                addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
 
383
                                addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
 
384
                                addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
 
385
                                addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030;  /* sector erase */
 
386
 
 
387
                                /* re-enable interrupts if necessary */
 
388
                                if (flag) {
 
389
                                        enable_interrupts();
 
390
                                        flag = 0;
 
391
                                }
 
392
 
 
393
                                /* data polling for D7 */
 
394
                                start = get_timer (0);
 
395
                                while ((addr2[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
 
396
                                       (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
 
397
                                        if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
 
398
                                                return (1);
 
399
                                }
 
400
                        } else {
 
401
                                if (sect == s_first) {
 
402
                                        addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
 
403
                                        addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
 
404
                                        addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
 
405
                                        addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
 
406
                                        addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
 
407
                                }
 
408
                                addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030;  /* sector erase */
 
409
                        }
 
410
                        l_sect = sect;
 
411
                }
 
412
        }
 
413
 
 
414
        /* re-enable interrupts if necessary */
 
415
        if (flag)
 
416
                enable_interrupts();
 
417
 
 
418
        /* wait at least 80us - let's wait 1 ms */
 
419
        udelay (1000);
 
420
 
 
421
        /*
 
422
         * We wait for the last triggered sector
 
423
         */
 
424
        if (l_sect < 0)
 
425
                goto DONE;
 
426
 
 
427
        start = get_timer (0);
 
428
        last  = start;
 
429
        addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
 
430
        while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
 
431
                if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
 
432
                        printf ("Timeout\n");
 
433
                        return 1;
 
434
                }
 
435
                /* show that we're waiting */
 
436
                if ((now - last) > 1000) {      /* every second */
 
437
                        putc ('.');
 
438
                        last = now;
 
439
                }
 
440
        }
 
441
 
 
442
DONE:
 
443
        /* reset to read mode */
 
444
        addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
 
445
        addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0;       /* reset bank */
 
446
 
 
447
        printf (" done\n");
 
448
        return 0;
 
449
}
 
450
 
 
451
/*
 
452
 * Copy memory to flash, returns:
 
453
 * 0 - OK
 
454
 * 1 - write timeout
 
455
 * 2 - Flash not erased
 
456
 */
 
457
int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 
458
{
 
459
        ulong cp, wp, data;
 
460
        int i, l, rc;
 
461
 
 
462
        wp = (addr & ~3);       /* get lower word aligned address */
 
463
 
 
464
        /*
 
465
         * handle unaligned start bytes
 
466
         */
 
467
        if ((l = addr - wp) != 0) {
 
468
                data = 0;
 
469
                for (i=0, cp=wp; i<l; ++i, ++cp) {
 
470
                        data = (data << 8) | (*(uchar *)cp);
 
471
                }
 
472
                for (; i<4 && cnt>0; ++i) {
 
473
                        data = (data << 8) | *src++;
 
474
                        --cnt;
 
475
                        ++cp;
 
476
                }
 
477
                for (; cnt==0 && i<4; ++i, ++cp) {
 
478
                        data = (data << 8) | (*(uchar *)cp);
 
479
                }
 
480
 
 
481
                if ((rc = write_word(info, wp, data)) != 0) {
 
482
                        return (rc);
 
483
                }
 
484
                wp += 4;
 
485
        }
 
486
 
 
487
        /*
 
488
         * handle word aligned part
 
489
         */
 
490
        while (cnt >= 4) {
 
491
                data = 0;
 
492
                for (i=0; i<4; ++i)
 
493
                        data = (data << 8) | *src++;
 
494
                if ((rc = write_word(info, wp, data)) != 0)
 
495
                        return (rc);
 
496
                wp  += 4;
 
497
                cnt -= 4;
 
498
        }
 
499
 
 
500
        if (cnt == 0)
 
501
                return (0);
 
502
 
 
503
        /*
 
504
         * handle unaligned tail bytes
 
505
         */
 
506
        data = 0;
 
507
        for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
 
508
                data = (data << 8) | *src++;
 
509
                --cnt;
 
510
        }
 
511
        for (; i<4; ++i, ++cp)
 
512
                data = (data << 8) | (*(uchar *)cp);
 
513
 
 
514
        return (write_word(info, wp, data));
 
515
}
 
516
 
 
517
/*
 
518
 * Write a word to Flash, returns:
 
519
 * 0 - OK
 
520
 * 1 - write timeout
 
521
 * 2 - Flash not erased
 
522
 */
 
523
static int write_word(flash_info_t *info, ulong dest, ulong data)
 
524
{
 
525
        volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
 
526
        volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
 
527
        volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)&data;
 
528
        ulong start;
 
529
        int flag;
 
530
        int i;
 
531
 
 
532
        /* Check if Flash is (sufficiently) erased */
 
533
        if ((*((vu_long *)dest) & data) != data)
 
534
                return (2);
 
535
 
 
536
        /* Disable interrupts which might cause a timeout here */
 
537
        flag = disable_interrupts();
 
538
 
 
539
        for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
 
540
                addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
 
541
                addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
 
542
                addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
 
543
 
 
544
                dest2[i] = data2[i];
 
545
 
 
546
                /* re-enable interrupts if necessary */
 
547
                if (flag)
 
548
                        enable_interrupts();
 
549
 
 
550
                /* data polling for D7 */
 
551
                start = get_timer (0);
 
552
                while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
 
553
                       (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
 
554
                        if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
 
555
                                return (1);
 
556
                }
 
557
        }
 
558
 
 
559
        return (0);
 
560
}