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

« back to all changes in this revision

Viewing changes to roms/u-boot/board/xaeniax/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 2001
 
3
 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
 
4
 *
 
5
 * (C) Copyright 2001
 
6
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 
7
 *
 
8
 * SPDX-License-Identifier:     GPL-2.0+
 
9
 */
 
10
 
 
11
#include <common.h>
 
12
#include <linux/byteorder/swab.h>
 
13
 
 
14
 
 
15
flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];    /* info for FLASH chips    */
 
16
 
 
17
/* Board support for 1 or 2 flash devices */
 
18
#define FLASH_PORT_WIDTH32
 
19
#undef FLASH_PORT_WIDTH16
 
20
 
 
21
#ifdef FLASH_PORT_WIDTH16
 
22
#define FLASH_PORT_WIDTH                ushort
 
23
#define FLASH_PORT_WIDTHV               vu_short
 
24
#define SWAP(x)               __swab16(x)
 
25
#else
 
26
#define FLASH_PORT_WIDTH                ulong
 
27
#define FLASH_PORT_WIDTHV               vu_long
 
28
#define SWAP(x)               __swab32(x)
 
29
#endif
 
30
 
 
31
#define FPW    FLASH_PORT_WIDTH
 
32
#define FPWV   FLASH_PORT_WIDTHV
 
33
 
 
34
#define mb() __asm__ __volatile__ ("" : : : "memory")
 
35
 
 
36
/*-----------------------------------------------------------------------
 
37
 * Functions
 
38
 */
 
39
static ulong flash_get_size (FPW *addr, flash_info_t *info);
 
40
static int write_data (flash_info_t *info, ulong dest, FPW data);
 
41
static void flash_get_offsets (ulong base, flash_info_t *info);
 
42
void inline spin_wheel (void);
 
43
 
 
44
/*-----------------------------------------------------------------------
 
45
 */
 
46
 
 
47
unsigned long flash_init (void)
 
48
{
 
49
        int i;
 
50
        ulong size = 0;
 
51
 
 
52
        for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
 
53
                switch (i) {
 
54
                case 0:
 
55
                        flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
 
56
                        flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
 
57
                        break;
 
58
                case 1:
 
59
                        flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
 
60
                        flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
 
61
                        break;
 
62
                default:
 
63
                        panic ("configured too many flash banks!\n");
 
64
                        break;
 
65
                }
 
66
                size += flash_info[i].size;
 
67
        }
 
68
 
 
69
        /* Protect monitor and environment sectors
 
70
         */
 
71
        flash_protect ( FLAG_PROTECT_SET,
 
72
                        CONFIG_SYS_FLASH_BASE,
 
73
                        CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
 
74
                        &flash_info[0] );
 
75
 
 
76
        flash_protect ( FLAG_PROTECT_SET,
 
77
                        CONFIG_ENV_ADDR,
 
78
                        CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
 
79
 
 
80
        return size;
 
81
}
 
82
 
 
83
/*-----------------------------------------------------------------------
 
84
 */
 
85
static void flash_get_offsets (ulong base, flash_info_t *info)
 
86
{
 
87
        int i;
 
88
 
 
89
        if (info->flash_id == FLASH_UNKNOWN) {
 
90
                return;
 
91
        }
 
92
 
 
93
        if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
 
94
                for (i = 0; i < info->sector_count; i++) {
 
95
                        info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
 
96
                        info->protect[i] = 0;
 
97
                }
 
98
        }
 
99
}
 
100
 
 
101
/*-----------------------------------------------------------------------
 
102
 */
 
103
void flash_print_info (flash_info_t *info)
 
104
{
 
105
        int i;
 
106
 
 
107
        if (info->flash_id == FLASH_UNKNOWN) {
 
108
                printf ("missing or unknown FLASH type\n");
 
109
                return;
 
110
        }
 
111
 
 
112
        switch (info->flash_id & FLASH_VENDMASK) {
 
113
        case FLASH_MAN_INTEL:
 
114
                printf ("INTEL ");
 
115
                break;
 
116
        default:
 
117
                printf ("Unknown Vendor ");
 
118
                break;
 
119
        }
 
120
 
 
121
        switch (info->flash_id & FLASH_TYPEMASK) {
 
122
        case FLASH_28F128J3A:
 
123
                printf ("28F128J3A\n");
 
124
                break;
 
125
        default:
 
126
                printf ("Unknown Chip Type\n");
 
127
                break;
 
128
        }
 
129
 
 
130
        printf ("  Size: %ld MB in %d Sectors\n",
 
131
                        info->size >> 20, info->sector_count);
 
132
 
 
133
        printf ("  Sector Start Addresses:");
 
134
        for (i = 0; i < info->sector_count; ++i) {
 
135
                if ((i % 5) == 0)
 
136
                        printf ("\n   ");
 
137
                printf (" %08lX%s",
 
138
                        info->start[i],
 
139
                        info->protect[i] ? " (RO)" : "     ");
 
140
        }
 
141
        printf ("\n");
 
142
        return;
 
143
}
 
144
 
 
145
/*
 
146
 * The following code cannot be run from FLASH!
 
147
 */
 
148
static ulong flash_get_size (FPW *addr, flash_info_t *info)
 
149
{
 
150
        volatile FPW value;
 
151
 
 
152
        /* Write auto select command: read Manufacturer ID */
 
153
        addr[0x5555] = (FPW) 0x00AA00AA;
 
154
        addr[0x2AAA] = (FPW) 0x00550055;
 
155
        addr[0x5555] = (FPW) 0x00900090;
 
156
 
 
157
        mb ();
 
158
        value = addr[0];
 
159
 
 
160
        switch (value) {
 
161
 
 
162
        case (FPW) INTEL_MANUFACT:
 
163
                info->flash_id = FLASH_MAN_INTEL;
 
164
                break;
 
165
 
 
166
        default:
 
167
                info->flash_id = FLASH_UNKNOWN;
 
168
                info->sector_count = 0;
 
169
                info->size = 0;
 
170
                addr[0] = (FPW) 0x00FF00FF;     /* restore read mode */
 
171
                return (0);                     /* no or unknown flash  */
 
172
        }
 
173
 
 
174
        mb ();
 
175
        value = addr[1];                        /* device ID        */
 
176
 
 
177
        switch (value) {
 
178
 
 
179
        case (FPW) INTEL_ID_28F128J3A:
 
180
                info->flash_id += FLASH_28F128J3A;
 
181
                info->sector_count = 128;
 
182
                info->size = 0x02000000;
 
183
                break;                          /* => 16 MB     */
 
184
 
 
185
        default:
 
186
                info->flash_id = FLASH_UNKNOWN;
 
187
                break;
 
188
        }
 
189
 
 
190
        if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
 
191
                printf ("** ERROR: sector count %d > max (%d) **\n",
 
192
                        info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
 
193
                info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
 
194
        }
 
195
 
 
196
        addr[0] = (FPW) 0x00FF00FF;             /* restore read mode */
 
197
 
 
198
        return (info->size);
 
199
}
 
200
 
 
201
 
 
202
/*-----------------------------------------------------------------------
 
203
 */
 
204
 
 
205
int flash_erase (flash_info_t *info, int s_first, int s_last)
 
206
{
 
207
        int prot, sect;
 
208
        ulong type, start;
 
209
        int rcode = 0;
 
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
        type = (info->flash_id & FLASH_VENDMASK);
 
221
        if ((type != FLASH_MAN_INTEL)) {
 
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
        /* Disable interrupts which might cause a timeout here */
 
242
        disable_interrupts();
 
243
 
 
244
        /* Start erase on unprotected sectors */
 
245
        for (sect = s_first; sect <= s_last; sect++) {
 
246
                if (info->protect[sect] == 0) { /* not protected */
 
247
                        FPWV *addr = (FPWV *) (info->start[sect]);
 
248
                        FPW status;
 
249
 
 
250
                        printf ("Erasing sector %2d ... ", sect);
 
251
 
 
252
                        /* arm simple, non interrupt dependent timer */
 
253
                        start = get_timer(0);
 
254
 
 
255
                        *addr = (FPW) 0x00500050;       /* clear status register */
 
256
                        *addr = (FPW) 0x00200020;       /* erase setup */
 
257
                        *addr = (FPW) 0x00D000D0;       /* erase confirm */
 
258
 
 
259
                        while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
 
260
                                if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
 
261
                                        printf ("Timeout\n");
 
262
                                        *addr = (FPW) 0x00B000B0;       /* suspend erase     */
 
263
                                        *addr = (FPW) 0x00FF00FF;       /* reset to read mode */
 
264
                                        rcode = 1;
 
265
                                        break;
 
266
                                }
 
267
                        }
 
268
 
 
269
                        *addr = 0x00500050;     /* clear status register cmd.   */
 
270
                        *addr = 0x00FF00FF;     /* resest to read mode          */
 
271
 
 
272
                        printf (" done\n");
 
273
                }
 
274
        }
 
275
        return rcode;
 
276
}
 
277
 
 
278
/*-----------------------------------------------------------------------
 
279
 * Copy memory to flash, returns:
 
280
 * 0 - OK
 
281
 * 1 - write timeout
 
282
 * 2 - Flash not erased
 
283
 * 4 - Flash not identified
 
284
 */
 
285
 
 
286
int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
 
287
{
 
288
        ulong cp, wp;
 
289
        FPW data;
 
290
        int count, i, l, rc, port_width;
 
291
 
 
292
        if (info->flash_id == FLASH_UNKNOWN) {
 
293
                return 4;
 
294
        }
 
295
/* get lower word aligned address */
 
296
#ifdef FLASH_PORT_WIDTH16
 
297
        wp = (addr & ~1);
 
298
        port_width = 2;
 
299
#else
 
300
        wp = (addr & ~3);
 
301
        port_width = 4;
 
302
#endif
 
303
 
 
304
        /*
 
305
         * handle unaligned start bytes
 
306
         */
 
307
        if ((l = addr - wp) != 0) {
 
308
                data = 0;
 
309
                for (i = 0, cp = wp; i < l; ++i, ++cp) {
 
310
                        data = (data << 8) | (*(uchar *) cp);
 
311
                }
 
312
                for (; i < port_width && cnt > 0; ++i) {
 
313
                        data = (data << 8) | *src++;
 
314
                        --cnt;
 
315
                        ++cp;
 
316
                }
 
317
                for (; cnt == 0 && i < port_width; ++i, ++cp) {
 
318
                        data = (data << 8) | (*(uchar *) cp);
 
319
                }
 
320
 
 
321
                if ((rc = write_data (info, wp, SWAP (data))) != 0) {
 
322
                        return (rc);
 
323
                }
 
324
                wp += port_width;
 
325
        }
 
326
 
 
327
        /*
 
328
         * handle word aligned part
 
329
         */
 
330
        count = 0;
 
331
        while (cnt >= port_width) {
 
332
                data = 0;
 
333
                for (i = 0; i < port_width; ++i) {
 
334
                        data = (data << 8) | *src++;
 
335
                }
 
336
                if ((rc = write_data (info, wp, SWAP (data))) != 0) {
 
337
                        return (rc);
 
338
                }
 
339
                wp += port_width;
 
340
                cnt -= port_width;
 
341
                if (count++ > 0x800) {
 
342
                        spin_wheel ();
 
343
                        count = 0;
 
344
                }
 
345
        }
 
346
 
 
347
        if (cnt == 0) {
 
348
                return (0);
 
349
        }
 
350
 
 
351
        /*
 
352
         * handle unaligned tail bytes
 
353
         */
 
354
        data = 0;
 
355
        for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
 
356
                data = (data << 8) | *src++;
 
357
                --cnt;
 
358
        }
 
359
        for (; i < port_width; ++i, ++cp) {
 
360
                data = (data << 8) | (*(uchar *) cp);
 
361
        }
 
362
 
 
363
        return (write_data (info, wp, SWAP (data)));
 
364
}
 
365
 
 
366
/*-----------------------------------------------------------------------
 
367
 * Write a word or halfword to Flash, returns:
 
368
 * 0 - OK
 
369
 * 1 - write timeout
 
370
 * 2 - Flash not erased
 
371
 */
 
372
static int write_data (flash_info_t *info, ulong dest, FPW data)
 
373
{
 
374
        FPWV *addr = (FPWV *) dest;
 
375
        ulong status;
 
376
        ulong start;
 
377
 
 
378
        /* Check if Flash is (sufficiently) erased */
 
379
        if ((*addr & data) != data) {
 
380
                printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
 
381
                return (2);
 
382
        }
 
383
        /* Disable interrupts which might cause a timeout here */
 
384
        disable_interrupts();
 
385
 
 
386
        *addr = (FPW) 0x00400040;       /* write setup */
 
387
        *addr = data;
 
388
 
 
389
        /* arm simple, non interrupt dependent timer */
 
390
        start = get_timer(0);
 
391
 
 
392
        /* wait while polling the status register */
 
393
        while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
 
394
                if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
 
395
                        *addr = (FPW) 0x00FF00FF;       /* restore read mode */
 
396
                        return (1);
 
397
                }
 
398
        }
 
399
 
 
400
        *addr = (FPW) 0x00FF00FF;       /* restore read mode */
 
401
 
 
402
        return (0);
 
403
}
 
404
 
 
405
void inline spin_wheel (void)
 
406
{
 
407
        static int p = 0;
 
408
        static char w[] = "\\/-";
 
409
 
 
410
        printf ("\010%c", w[p]);
 
411
        (++p == 3) ? (p = 0) : 0;
 
412
}