~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/u-boot/board/kup/common/flash.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * (C) Copyright 2000-2004
 
3
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 
4
 *
 
5
 * SPDX-License-Identifier:     GPL-2.0+
 
6
 */
 
7
 
 
8
#include <common.h>
 
9
#include <mpc8xx.h>
 
10
 
 
11
#ifndef CONFIG_ENV_ADDR
 
12
#define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
 
13
#endif
 
14
 
 
15
#define CONFIG_FLASH_16BIT
 
16
 
 
17
flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
 
18
 
 
19
/*-----------------------------------------------------------------------
 
20
 * Functions
 
21
 */
 
22
static ulong flash_get_size (vu_long *addr, flash_info_t *info);
 
23
static int write_word (flash_info_t *info, ulong dest, ulong data);
 
24
 
 
25
/*-----------------------------------------------------------------------
 
26
 */
 
27
 
 
28
unsigned long flash_init (void)
 
29
{
 
30
        volatile immap_t     *immap  = (immap_t *)CONFIG_SYS_IMMR;
 
31
        volatile memctl8xx_t *memctl = &immap->im_memctl;
 
32
        unsigned long size_b0;
 
33
        int i;
 
34
 
 
35
        /* Init: no FLASHes known */
 
36
        for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
 
37
                flash_info[i].flash_id = FLASH_UNKNOWN;
 
38
        }
 
39
 
 
40
        /* Static FLASH Bank configuration here - FIXME XXX */
 
41
 
 
42
        size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
 
43
 
 
44
        if (flash_info[0].flash_id == FLASH_UNKNOWN) {
 
45
                printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
 
46
                        size_b0, size_b0<<20);
 
47
        }
 
48
 
 
49
 
 
50
        /* Remap FLASH according to real size */
 
51
        memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
 
52
        memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16;
 
53
 
 
54
        /* Re-do sizing to get full correct info */
 
55
        size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
 
56
 
 
57
#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
 
58
        /* monitor protection ON by default */
 
59
        flash_protect(FLAG_PROTECT_SET,
 
60
                      CONFIG_SYS_MONITOR_BASE,
 
61
                      CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
 
62
                      &flash_info[0]);
 
63
#endif
 
64
 
 
65
#ifdef  CONFIG_ENV_IS_IN_FLASH
 
66
        /* ENV protection ON by default */
 
67
        flash_protect(FLAG_PROTECT_SET,
 
68
                      CONFIG_ENV_ADDR,
 
69
                      CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
 
70
                      &flash_info[0]);
 
71
#endif
 
72
 
 
73
        flash_info[0].size = size_b0;
 
74
 
 
75
        return (size_b0);
 
76
}
 
77
 
 
78
/*-----------------------------------------------------------------------
 
79
 */
 
80
void flash_print_info  (flash_info_t *info)
 
81
{
 
82
        int i;
 
83
 
 
84
        if (info->flash_id == FLASH_UNKNOWN) {
 
85
                printf ("missing or unknown FLASH type\n");
 
86
                return;
 
87
        }
 
88
 
 
89
        switch (info->flash_id & FLASH_VENDMASK) {
 
90
        case FLASH_MAN_AMD:     printf ("AMD ");                break;
 
91
        case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
 
92
        default:                printf ("Unknown Vendor ");     break;
 
93
        }
 
94
 
 
95
        switch (info->flash_id & FLASH_TYPEMASK) {
 
96
        case FLASH_AM400B:      printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
 
97
                                break;
 
98
        case FLASH_AM400T:      printf ("AM29LV400T (4 Mbit, top boot sector)\n");
 
99
                                break;
 
100
        case FLASH_AM800B:      printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
 
101
                                break;
 
102
        case FLASH_AM800T:      printf ("AM29LV800T (8 Mbit, top boot sector)\n");
 
103
                                break;
 
104
        case FLASH_AM160B:      printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
 
105
                                break;
 
106
        case FLASH_AM160T:      printf ("AM29LV160T (16 Mbit, top boot sector)\n");
 
107
                                break;
 
108
        case FLASH_AM320B:      printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
 
109
                                break;
 
110
        case FLASH_AM320T:      printf ("AM29LV320T (32 Mbit, top boot sector)\n");
 
111
                                break;
 
112
        default:                printf ("Unknown Chip Type\n");
 
113
                                break;
 
114
        }
 
115
 
 
116
        printf ("  Size: %ld MB in %d Sectors\n",
 
117
                info->size >> 20, info->sector_count);
 
118
 
 
119
        printf ("  Sector Start Addresses:");
 
120
        for (i=0; i<info->sector_count; ++i) {
 
121
                if ((i % 5) == 0)
 
122
                        printf ("\n   ");
 
123
                printf (" %08lX%s",
 
124
                        info->start[i],
 
125
                        info->protect[i] ? " (RO)" : "     "
 
126
                );
 
127
        }
 
128
        printf ("\n");
 
129
        return;
 
130
}
 
131
 
 
132
/*-----------------------------------------------------------------------
 
133
 */
 
134
 
 
135
 
 
136
/*-----------------------------------------------------------------------
 
137
 */
 
138
 
 
139
/*
 
140
 * The following code cannot be run from FLASH!
 
141
 */
 
142
 
 
143
static ulong flash_get_size (vu_long *addr, flash_info_t *info)
 
144
{
 
145
        short i;
 
146
        ulong value;
 
147
        ulong base = (ulong)addr;
 
148
 
 
149
        /* Write auto select command: read Manufacturer ID */
 
150
        vu_short *s_addr=(vu_short*)addr;
 
151
        s_addr[0x5555] = 0x00AA;
 
152
        s_addr[0x2AAA] = 0x0055;
 
153
        s_addr[0x5555] = 0x0090;
 
154
 
 
155
        value = s_addr[0];
 
156
        value = value|(value<<16);
 
157
 
 
158
        switch (value) {
 
159
        case AMD_MANUFACT:
 
160
                info->flash_id = FLASH_MAN_AMD;
 
161
                break;
 
162
        case FUJ_MANUFACT:
 
163
                info->flash_id = FLASH_MAN_FUJ;
 
164
                break;
 
165
        default:
 
166
                info->flash_id = FLASH_UNKNOWN;
 
167
                info->sector_count = 0;
 
168
                info->size = 0;
 
169
                return (0);                     /* no or unknown flash  */
 
170
        }
 
171
 
 
172
        value = s_addr[1];
 
173
        value = value|(value<<16);
 
174
 
 
175
                switch (value) {
 
176
        case FUJI_ID_29F800BA:
 
177
                info->flash_id += FLASH_AM400T;
 
178
                info->sector_count = 19;
 
179
                info->size = 0x00100000;
 
180
                break;                          /* => 1 MB              */
 
181
        case AMD_ID_LV800T:
 
182
                info->flash_id += FLASH_AM800T;
 
183
                info->sector_count = 19;
 
184
                info->size = 0x00100000;
 
185
                break;                          /* => 1 MB              */
 
186
        case AMD_ID_LV800B:
 
187
                info->flash_id += FLASH_AM800B;
 
188
                info->sector_count = 19;
 
189
                info->size = 0x00100000;
 
190
                break;                          /* => 1 MB              */
 
191
        default:
 
192
                info->flash_id = FLASH_UNKNOWN;
 
193
                return (0);                     /* => no or unknown flash */
 
194
        }
 
195
 
 
196
        /* set up sector start address table */
 
197
        /* set sector offsets for bottom boot block type        */
 
198
        info->start[0] = base + 0x00000000;
 
199
        info->start[1] = base + 0x00004000;
 
200
        info->start[2] = base + 0x00006000;
 
201
        info->start[3] = base + 0x00008000;
 
202
        for (i = 4; i < info->sector_count; i++) {
 
203
                info->start[i] = base + (i * 0x00010000) - 0x00030000;
 
204
        }
 
205
 
 
206
 
 
207
        /* check for protected sectors */
 
208
        for (i = 0; i < info->sector_count; i++) {
 
209
                /* read sector protection at sector address, (A7 .. A0) = 0x02 */
 
210
                /* D0 = 1 if protected */
 
211
                s_addr = (volatile unsigned short *)(info->start[i]);
 
212
                info->protect[i] = s_addr[2] & 1;
 
213
        }
 
214
 
 
215
        /*
 
216
         * Prevent writes to uninitialized FLASH.
 
217
         */
 
218
        if (info->flash_id != FLASH_UNKNOWN) {
 
219
                s_addr = (volatile unsigned short *)info->start[0];
 
220
                *s_addr = 0x00F0;       /* reset bank */
 
221
        }
 
222
        return (info->size);
 
223
}
 
224
 
 
225
 
 
226
/*-----------------------------------------------------------------------
 
227
 */
 
228
 
 
229
 
 
230
int     flash_erase (flash_info_t *info, int s_first, int s_last)
 
231
{
 
232
        vu_long *addr = (vu_long*)(info->start[0]);
 
233
        int flag, prot, sect;
 
234
        ulong start, now, last;
 
235
#ifdef CONFIG_FLASH_16BIT
 
236
        vu_short *s_addr = (vu_short*)addr;
 
237
#endif
 
238
 
 
239
        if ((s_first < 0) || (s_first > s_last)) {
 
240
                if (info->flash_id == FLASH_UNKNOWN) {
 
241
                        printf ("- missing\n");
 
242
                } else {
 
243
                        printf ("- no sectors to erase\n");
 
244
                }
 
245
                return 1;
 
246
        }
 
247
/*#ifndef CONFIG_FLASH_16BIT
 
248
        ulong type;
 
249
        type = (info->flash_id & FLASH_VENDMASK);
 
250
        if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
 
251
                printf ("Can't erase unknown flash type %08lx - aborted\n",
 
252
                        info->flash_id);
 
253
                return;
 
254
        }
 
255
#endif*/
 
256
        prot = 0;
 
257
        for (sect=s_first; sect<=s_last; ++sect) {
 
258
                if (info->protect[sect]) {
 
259
                        prot++;
 
260
                }
 
261
        }
 
262
 
 
263
        if (prot) {
 
264
                printf ("- Warning: %d protected sectors will not be erased!\n",
 
265
                        prot);
 
266
        } else {
 
267
                printf ("\n");
 
268
        }
 
269
 
 
270
        start = get_timer (0);
 
271
        last  = start;
 
272
        /* Start erase on unprotected sectors */
 
273
        for (sect = s_first; sect<=s_last; sect++) {
 
274
                if (info->protect[sect] == 0) { /* not protected */
 
275
#ifdef CONFIG_FLASH_16BIT
 
276
                        vu_short *s_sect_addr = (vu_short*)(info->start[sect]);
 
277
#else
 
278
                        vu_long *sect_addr = (vu_long*)(info->start[sect]);
 
279
#endif
 
280
                        /* Disable interrupts which might cause a timeout here */
 
281
                        flag = disable_interrupts();
 
282
 
 
283
#ifdef CONFIG_FLASH_16BIT
 
284
 
 
285
                        /*printf("\ns_sect_addr=%x",s_sect_addr);*/
 
286
                        s_addr[0x5555] = 0x00AA;
 
287
                        s_addr[0x2AAA] = 0x0055;
 
288
                        s_addr[0x5555] = 0x0080;
 
289
                        s_addr[0x5555] = 0x00AA;
 
290
                        s_addr[0x2AAA] = 0x0055;
 
291
                        s_sect_addr[0] = 0x0030;
 
292
#else
 
293
                        addr[0x5555] = 0x00AA00AA;
 
294
                        addr[0x2AAA] = 0x00550055;
 
295
                        addr[0x5555] = 0x00800080;
 
296
                        addr[0x5555] = 0x00AA00AA;
 
297
                        addr[0x2AAA] = 0x00550055;
 
298
                        sect_addr[0] = 0x00300030;
 
299
#endif
 
300
                        /* re-enable interrupts if necessary */
 
301
                        if (flag)
 
302
                                enable_interrupts();
 
303
 
 
304
                        /* wait at least 80us - let's wait 1 ms */
 
305
                        udelay (1000);
 
306
 
 
307
#ifdef CONFIG_FLASH_16BIT
 
308
                        while ((s_sect_addr[0] & 0x0080) != 0x0080) {
 
309
#else
 
310
                        while ((sect_addr[0] & 0x00800080) != 0x00800080) {
 
311
#endif
 
312
                                if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
 
313
                                        printf ("Timeout\n");
 
314
                                        return 1;
 
315
                                }
 
316
                                /* show that we're waiting */
 
317
                                if ((now - last) > 1000) {      /* every second */
 
318
                                        putc ('.');
 
319
                                        last = now;
 
320
                                }
 
321
                        }
 
322
                }
 
323
        }
 
324
 
 
325
        /* reset to read mode */
 
326
        addr = (volatile unsigned long *)info->start[0];
 
327
#ifdef CONFIG_FLASH_16BIT
 
328
        s_addr[0] = 0x00F0;     /* reset bank */
 
329
#else
 
330
        addr[0] = 0x00F000F0;   /* reset bank */
 
331
#endif
 
332
 
 
333
        printf (" done\n");
 
334
        return 0;
 
335
}
 
336
 
 
337
 
 
338
/*-----------------------------------------------------------------------
 
339
 * Copy memory to flash, returns:
 
340
 * 0 - OK
 
341
 * 1 - write timeout
 
342
 * 2 - Flash not erased
 
343
 */
 
344
 
 
345
int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 
346
{
 
347
        ulong cp, wp, data;
 
348
        int i, l, rc;
 
349
 
 
350
        wp = (addr & ~3);       /* get lower word aligned address */
 
351
 
 
352
        /*
 
353
         * handle unaligned start bytes
 
354
         */
 
355
        if ((l = addr - wp) != 0) {
 
356
                data = 0;
 
357
                for (i=0, cp=wp; i<l; ++i, ++cp) {
 
358
                        data = (data << 8) | (*(uchar *)cp);
 
359
                }
 
360
                for (; i<4 && cnt>0; ++i) {
 
361
                        data = (data << 8) | *src++;
 
362
                        --cnt;
 
363
                        ++cp;
 
364
                }
 
365
                for (; cnt==0 && i<4; ++i, ++cp) {
 
366
                        data = (data << 8) | (*(uchar *)cp);
 
367
                }
 
368
 
 
369
                if ((rc = write_word(info, wp, data)) != 0) {
 
370
                        return (rc);
 
371
                }
 
372
                wp += 4;
 
373
        }
 
374
 
 
375
        /*
 
376
         * handle word aligned part
 
377
         */
 
378
        while (cnt >= 4) {
 
379
                data = 0;
 
380
                for (i=0; i<4; ++i) {
 
381
                        data = (data << 8) | *src++;
 
382
                }
 
383
                if ((rc = write_word(info, wp, data)) != 0) {
 
384
                        return (rc);
 
385
                }
 
386
                wp  += 4;
 
387
                cnt -= 4;
 
388
        }
 
389
 
 
390
        if (cnt == 0) {
 
391
                return (0);
 
392
        }
 
393
 
 
394
        /*
 
395
         * handle unaligned tail bytes
 
396
         */
 
397
        data = 0;
 
398
        for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
 
399
                data = (data << 8) | *src++;
 
400
                --cnt;
 
401
        }
 
402
        for (; i<4; ++i, ++cp) {
 
403
                data = (data << 8) | (*(uchar *)cp);
 
404
        }
 
405
 
 
406
        return (write_word(info, wp, data));
 
407
}
 
408
 
 
409
 
 
410
/*-----------------------------------------------------------------------
 
411
 * Write a word to Flash, returns:
 
412
 * 0 - OK
 
413
 * 1 - write timeout
 
414
 * 2 - Flash not erased
 
415
 */
 
416
static int write_word (flash_info_t *info, ulong dest, ulong data)
 
417
{
 
418
        vu_long *addr = (vu_long*)(info->start[0]);
 
419
 
 
420
#ifdef CONFIG_FLASH_16BIT
 
421
        vu_short high_data;
 
422
        vu_short low_data;
 
423
        vu_short *s_addr = (vu_short*)addr;
 
424
#endif
 
425
        ulong start;
 
426
        int flag;
 
427
 
 
428
        /* Check if Flash is (sufficiently) erased */
 
429
        if ((*((vu_long *)dest) & data) != data) {
 
430
                return (2);
 
431
        }
 
432
 
 
433
#ifdef CONFIG_FLASH_16BIT
 
434
        /* Write the 16 higher-bits */
 
435
        /* Disable interrupts which might cause a timeout here */
 
436
        flag = disable_interrupts();
 
437
 
 
438
        high_data = ((data>>16) & 0x0000ffff);
 
439
 
 
440
        s_addr[0x5555] = 0x00AA;
 
441
        s_addr[0x2AAA] = 0x0055;
 
442
        s_addr[0x5555] = 0x00A0;
 
443
 
 
444
        *((vu_short *)dest) = high_data;
 
445
 
 
446
 
 
447
        /* re-enable interrupts if necessary */
 
448
        if (flag)
 
449
                enable_interrupts();
 
450
 
 
451
        /* data polling for D7 */
 
452
        start = get_timer (0);
 
453
        while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
 
454
                if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
 
455
                        return (1);
 
456
                }
 
457
        }
 
458
 
 
459
 
 
460
        /* Write the 16 lower-bits */
 
461
#endif
 
462
 
 
463
        /* Disable interrupts which might cause a timeout here */
 
464
        flag = disable_interrupts();
 
465
#ifdef CONFIG_FLASH_16BIT
 
466
        dest += 0x2;
 
467
        low_data = (data & 0x0000ffff);
 
468
 
 
469
        s_addr[0x5555] = 0x00AA;
 
470
        s_addr[0x2AAA] = 0x0055;
 
471
        s_addr[0x5555] = 0x00A0;
 
472
        *((vu_short *)dest) = low_data;
 
473
 
 
474
#else
 
475
        addr[0x5555] = 0x00AA00AA;
 
476
        addr[0x2AAA] = 0x00550055;
 
477
        addr[0x5555] = 0x00A000A0;
 
478
        *((vu_long *)dest) = data;
 
479
#endif
 
480
 
 
481
        /* re-enable interrupts if necessary */
 
482
        if (flag)
 
483
                enable_interrupts();
 
484
 
 
485
        /* data polling for D7 */
 
486
        start = get_timer (0);
 
487
 
 
488
#ifdef CONFIG_FLASH_16BIT
 
489
        while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
 
490
#else
 
491
        while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
 
492
#endif
 
493
 
 
494
                if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
 
495
                        return (1);
 
496
                }
 
497
        }
 
498
        return (0);
 
499
}