2
* (C) Copyright 2003-2004
3
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6
* Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
8
* Modified for the CMC PU2 by (C) Copyright 2004 Gary Jennejohn
11
* See file CREDITS for list of people who contributed to this
14
* This program is free software; you can redistribute it and/or
15
* modify it under the terms of the GNU General Public License as
16
* published by the Free Software Foundation; either version 2 of
17
* the License, or (at your option) any later version.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32
#ifndef CONFIG_ENV_ADDR
33
#define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
36
flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
38
#define FLASH_CYCLE1 0x0555
39
#define FLASH_CYCLE2 0x02AA
41
/*-----------------------------------------------------------------------
44
static ulong flash_get_size(vu_short *addr, flash_info_t *info);
45
static void flash_reset(flash_info_t *info);
46
static int write_word_amd(flash_info_t *info, vu_short *dest, ushort data);
47
static flash_info_t *flash_get_info(ulong base);
49
/*-----------------------------------------------------------------------
52
* sets up flash_info and returns size of FLASH (bytes)
54
unsigned long flash_init (void)
56
unsigned long size = 0;
57
ulong flashbase = CONFIG_SYS_FLASH_BASE;
59
/* Init: no FLASHes known */
60
memset(&flash_info[0], 0, sizeof(flash_info_t));
62
flash_info[0].size = flash_get_size((vu_short *)flashbase, &flash_info[0]);
64
size = flash_info[0].size;
66
#if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
67
/* monitor protection ON by default */
68
flash_protect(FLAG_PROTECT_SET,
69
CONFIG_SYS_MONITOR_BASE,
70
CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
71
flash_get_info(CONFIG_SYS_MONITOR_BASE));
74
#ifdef CONFIG_ENV_IS_IN_FLASH
75
/* ENV protection ON by default */
76
flash_protect(FLAG_PROTECT_SET,
78
CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
79
flash_get_info(CONFIG_ENV_ADDR));
82
return size ? size : 1;
85
/*-----------------------------------------------------------------------
87
static void flash_reset(flash_info_t *info)
89
vu_short *base = (vu_short *)(info->start[0]);
91
/* Put FLASH back in read mode */
92
if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
93
*base = 0x00FF; /* Intel Read Mode */
94
else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
95
*base = 0x00F0; /* AMD Read Mode */
98
/*-----------------------------------------------------------------------
101
static flash_info_t *flash_get_info(ulong base)
107
for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i ++) {
108
info = & flash_info[i];
109
if (info->size && info->start[0] <= base &&
110
base <= info->start[0] + info->size - 1)
114
return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
117
/*-----------------------------------------------------------------------
120
void flash_print_info (flash_info_t *info)
124
if (info->flash_id == FLASH_UNKNOWN) {
125
printf ("missing or unknown FLASH type\n");
129
switch (info->flash_id & FLASH_VENDMASK) {
130
case FLASH_MAN_AMD: printf ("AMD "); break;
131
case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
132
case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
133
case FLASH_MAN_SST: printf ("SST "); break;
134
case FLASH_MAN_STM: printf ("STM "); break;
135
case FLASH_MAN_INTEL: printf ("INTEL "); break;
136
default: printf ("Unknown Vendor "); break;
139
switch (info->flash_id & FLASH_TYPEMASK) {
140
case FLASH_S29GL064M:
141
printf ("S29GL064M-R6 (64Mbit, uniform sector size)\n");
144
printf ("Unknown Chip Type\n");
148
printf (" Size: %ld MB in %d Sectors\n",
152
printf (" Sector Start Addresses:");
154
for (i=0; i<info->sector_count; ++i) {
160
info->protect[i] ? " (RO)" : " ");
166
/*-----------------------------------------------------------------------
170
* The following code cannot be run from FLASH!
173
ulong flash_get_size (vu_short *addr, flash_info_t *info)
177
ulong base = (ulong)addr;
179
/* Write auto select command sequence */
180
addr[FLASH_CYCLE1] = 0x00AA; /* for AMD, Intel ignores this */
181
addr[FLASH_CYCLE2] = 0x0055; /* for AMD, Intel ignores this */
182
addr[FLASH_CYCLE1] = 0x0090; /* selects Intel or AMD */
184
/* read Manufacturer ID */
187
debug ("Manufacturer ID: %04X\n", value);
191
case (AMD_MANUFACT & 0xFFFF):
192
debug ("Manufacturer: AMD (Spansion)\n");
193
info->flash_id = FLASH_MAN_AMD;
196
case (INTEL_MANUFACT & 0xFFFF):
197
debug ("Manufacturer: Intel (not supported yet)\n");
198
info->flash_id = FLASH_MAN_INTEL;
202
printf ("Unknown Manufacturer ID: %04X\n", value);
203
info->flash_id = FLASH_UNKNOWN;
204
info->sector_count = 0;
210
debug ("Device ID: %04X\n", value);
214
case (AMD_ID_MIRROR & 0xFFFF):
215
debug ("Mirror Bit flash: addr[14] = %08X addr[15] = %08X\n",
219
case (AMD_ID_GL064M_2 & 0xFFFF):
220
if (addr[15] != (AMD_ID_GL064M_3 & 0xffff)) {
221
printf ("Chip: S29GLxxxM -> unknown\n");
222
info->flash_id = FLASH_UNKNOWN;
223
info->sector_count = 0;
226
debug ("Chip: S29GL064M-R6\n");
227
info->flash_id += FLASH_S29GL064M;
228
info->sector_count = 128;
229
info->size = 0x00800000;
230
for (i = 0; i < info->sector_count; i++) {
231
info->start[i] = base;
235
break; /* => 16 MB */
237
printf ("Chip: *** unknown ***\n");
238
info->flash_id = FLASH_UNKNOWN;
239
info->sector_count = 0;
246
printf ("Unknown Device ID: %04X\n", value);
247
info->flash_id = FLASH_UNKNOWN;
248
info->sector_count = 0;
254
/* Put FLASH back in read mode */
260
/*-----------------------------------------------------------------------
263
int flash_erase (flash_info_t *info, int s_first, int s_last)
265
vu_short *addr = (vu_short *)(info->start[0]);
266
int flag, prot, sect, ssect, l_sect;
269
debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
271
if ((s_first < 0) || (s_first > s_last)) {
272
if (info->flash_id == FLASH_UNKNOWN) {
273
printf ("- missing\n");
275
printf ("- no sectors to erase\n");
280
if ((info->flash_id == FLASH_UNKNOWN) ||
281
(info->flash_id > FLASH_AMD_COMP)) {
282
printf ("Can't erase unknown flash type %08lx - aborted\n",
288
for (sect=s_first; sect<=s_last; ++sect) {
289
if (info->protect[sect]) {
295
printf ("- Warning: %d protected sectors will not be erased!\n",
301
/* Disable interrupts which might cause a timeout here */
302
flag = disable_interrupts();
305
* Start erase on unprotected sectors.
306
* Since the flash can erase multiple sectors with one command
307
* we take advantage of that by doing the erase in chunks of
310
for (sect = s_first; sect <= s_last; ) {
313
addr[FLASH_CYCLE1] = 0x00AA;
314
addr[FLASH_CYCLE2] = 0x0055;
315
addr[FLASH_CYCLE1] = 0x0080;
316
addr[FLASH_CYCLE1] = 0x00AA;
317
addr[FLASH_CYCLE2] = 0x0055;
319
/* do the erase in chunks of at most 3 sectors */
320
for (ssect = 0; ssect < 3; ssect++) {
321
if ((sect + ssect) > s_last)
323
if (info->protect[sect + ssect] == 0) { /* not protected */
324
addr = (vu_short *)(info->start[sect + ssect]);
326
l_sect = sect + ssect;
329
/* wait at least 80us - let's wait 1 ms */
333
* We wait for the last triggered sector
338
reset_timer_masked ();
340
addr = (vu_short *)(info->start[l_sect]);
341
while ((addr[0] & 0x0080) != 0x0080) {
342
if ((now = get_timer_masked ()) > CONFIG_SYS_FLASH_ERASE_TOUT) {
343
printf ("Timeout\n");
346
/* show that we're waiting */
347
if ((now - last) > 1000) { /* every second */
352
addr = (vu_short *)info->start[0];
353
addr[0] = 0x00F0; /* reset bank */
357
/* re-enable interrupts if necessary */
362
/* reset to read mode */
363
addr = (vu_short *)info->start[0];
364
addr[0] = 0x00F0; /* reset bank */
370
/*-----------------------------------------------------------------------
371
* Copy memory to flash, returns:
374
* 2 - Flash not erased
377
int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
383
printf ("unaligned destination not supported\n");
388
printf ("unaligned source not supported\n");
395
data = *((vu_short *)src);
396
if ((rc = write_word_amd(info, (vu_short *)wp, data)) != 0) {
397
printf ("write_buff 1: write_word_amd() rc=%d\n", rc);
410
data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1)) << 8);
411
if ((rc = write_word_amd(info, (vu_short *)wp, data)) != 0) {
412
printf ("write_buff 1: write_word_amd() rc=%d\n", rc);
423
/*-----------------------------------------------------------------------
424
* Write a word to Flash for AMD FLASH
425
* A word is 16 or 32 bits, whichever the bus width of the flash bank
426
* (not an individual chip) is.
431
* 2 - Flash not erased
433
static int write_word_amd (flash_info_t *info, vu_short *dest, ushort data)
436
vu_short *base; /* first address in flash bank */
438
/* Check if Flash is (sufficiently) erased */
439
if ((*dest & data) != data) {
443
base = (vu_short *)(info->start[0]);
445
/* Disable interrupts which might cause a timeout here */
446
flag = disable_interrupts();
448
base[FLASH_CYCLE1] = 0x00AA; /* unlock */
449
base[FLASH_CYCLE2] = 0x0055; /* unlock */
450
base[FLASH_CYCLE1] = 0x00A0; /* selects program mode */
452
*dest = data; /* start programming the data */
454
/* re-enable interrupts if necessary */
458
reset_timer_masked ();
460
/* data polling for D7 */
461
while ((*dest & 0x0080) != (data & 0x0080)) {
462
if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT) {
463
*dest = 0x00F0; /* reset bank */