3 * Martin Winistoerfer, martinwinistoerfer@gmx.ch.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 * Discription: This Driver is for 28F320J3A, 28F640J3A and
28 * 28F128J3A Intel flashs working in 16 Bit mode.
29 * They are single bank flashs.
31 * Most of this code is taken from existing u-boot
39 #if defined(CONFIG_ENV_IS_IN_FLASH)
40 # ifndef CONFIG_ENV_ADDR
41 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
43 # ifndef CONFIG_ENV_SIZE
44 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
46 # ifndef CONFIG_ENV_SECT_SIZE
47 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
51 #define FLASH_ID_MASK 0xFFFF
52 #define FLASH_BLOCK_SIZE 0x00010000
53 #define FLASH_CMD_READ_ID 0x0090
54 #define FLASH_CMD_RESET 0x00ff
55 #define FLASH_CMD_BLOCK_ERASE 0x0020
56 #define FLASH_CMD_ERASE_CONFIRM 0x00D0
57 #define FLASH_CMD_CLEAR_STATUS 0x0050
58 #define FLASH_CMD_SUSPEND_ERASE 0x00B0
59 #define FLASH_CMD_WRITE 0x0040
60 #define FLASH_CMD_PROTECT 0x0060
61 #define FLASH_CMD_PROTECT_SET 0x0001
62 #define FLASH_CMD_PROTECT_CLEAR 0x00D0
63 #define FLASH_STATUS_DONE 0x0080
65 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
68 * Local function prototypes
70 static ulong
flash_get_size (vu_short
*addr
, flash_info_t
*info
);
71 static int write_short (flash_info_t
*info
, ulong dest
, ushort data
);
72 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
78 unsigned long flash_init (void)
80 unsigned long size_b0
;
83 /* Init: no FLASHes known */
84 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
85 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
88 /* Static FLASH Bank configuration here - FIXME XXX */
90 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM
);
92 size_b0
= flash_get_size((vu_short
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
94 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
95 printf ("## Unknown FLASH on Bank 0: "
96 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
97 flash_info
[0].flash_id
,
98 size_b0
, size_b0
<<20);
101 flash_get_offsets (FLASH_BASE0_PRELIM
, &flash_info
[0]);
103 flash_info
[0].size
= size_b0
;
105 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
106 /* monitor protection ON by default */
107 flash_protect(FLAG_PROTECT_SET
,
108 CONFIG_SYS_MONITOR_BASE
,
109 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
113 #ifdef CONFIG_ENV_IS_IN_FLASH
114 /* ENV protection ON by default */
115 flash_protect(FLAG_PROTECT_SET
,
117 CONFIG_ENV_ADDR
+CONFIG_ENV_SECT_SIZE
-1,
125 * Compute start adress of each sector (block)
128 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
132 if (info
->flash_id
== FLASH_UNKNOWN
) {
136 switch (info
->flash_id
& FLASH_VENDMASK
) {
137 case FLASH_MAN_INTEL
:
138 for (i
= 0; i
< info
->sector_count
; i
++) {
139 info
->start
[i
] = base
+ i
* FLASH_BLOCK_SIZE
;
144 printf ("Don't know sector offsets for flash type 0x%lx\n",
151 * Print flash information
153 void flash_print_info (flash_info_t
*info
)
157 if (info
->flash_id
== FLASH_UNKNOWN
) {
158 printf ("missing or unknown FLASH type\n");
162 switch (info
->flash_id
& FLASH_VENDMASK
) {
163 case FLASH_MAN_AMD
: printf ("AMD "); break;
164 case FLASH_MAN_FUJ
: printf ("Fujitsu "); break;
165 case FLASH_MAN_SST
: printf ("SST "); break;
166 case FLASH_MAN_STM
: printf ("STM "); break;
167 case FLASH_MAN_INTEL
: printf ("Intel "); break;
168 case FLASH_MAN_MT
: printf ("MT "); break;
169 default: printf ("Unknown Vendor "); break;
172 switch (info
->flash_id
& FLASH_TYPEMASK
) {
173 case FLASH_28F320J3A
: printf ("28F320J3A (32Mbit) 16-Bit\n");
175 case FLASH_28F640J3A
: printf ("28F640J3A (64Mbit) 16-Bit\n");
177 case FLASH_28F128J3A
: printf ("28F128J3A (128Mbit) 16-Bit\n");
179 default: printf ("Unknown Chip Type\n");
183 if (info
->size
>= (1 << 20)) {
188 printf (" Size: %ld %cB in %d Sectors\n",
190 (i
== 20) ? 'M' : 'k',
193 printf (" Sector Start Addresses:");
194 for (i
=0; i
<info
->sector_count
; ++i
) {
199 info
->protect
[i
] ? " (RO)" : " "
207 * Get size of flash in bytes.
208 * The following code cannot be run from FLASH!
211 static ulong
flash_get_size (vu_short
*addr
, flash_info_t
*info
)
215 /* Read Manufacturer ID */
216 addr
[0] = FLASH_CMD_READ_ID
;
220 case (AMD_MANUFACT
& FLASH_ID_MASK
):
221 info
->flash_id
= FLASH_MAN_AMD
;
223 case (FUJ_MANUFACT
& FLASH_ID_MASK
):
224 info
->flash_id
= FLASH_MAN_FUJ
;
226 case (SST_MANUFACT
& FLASH_ID_MASK
):
227 info
->flash_id
= FLASH_MAN_SST
;
229 case (STM_MANUFACT
& FLASH_ID_MASK
):
230 info
->flash_id
= FLASH_MAN_STM
;
232 case (INTEL_MANUFACT
& FLASH_ID_MASK
):
233 info
->flash_id
= FLASH_MAN_INTEL
;
236 info
->flash_id
= FLASH_UNKNOWN
;
237 info
->sector_count
= 0;
239 addr
[0] = FLASH_CMD_RESET
; /* restore read mode */
240 return (0); /* no or unknown flash */
243 value
= addr
[1]; /* device ID */
246 case (INTEL_ID_28F320J3A
& FLASH_ID_MASK
):
247 info
->flash_id
+= FLASH_28F320J3A
;
248 info
->sector_count
= 32;
249 info
->size
= 0x00400000;
250 break; /* => 32 MBit */
252 case (INTEL_ID_28F640J3A
& FLASH_ID_MASK
):
253 info
->flash_id
+= FLASH_28F640J3A
;
254 info
->sector_count
= 64;
255 info
->size
= 0x00800000;
256 break; /* => 64 MBit */
258 case (INTEL_ID_28F128J3A
& FLASH_ID_MASK
):
259 info
->flash_id
+= FLASH_28F128J3A
;
260 info
->sector_count
= 128;
261 info
->size
= 0x01000000;
262 break; /* => 128 MBit */
265 info
->flash_id
= FLASH_UNKNOWN
;
266 addr
[0] = FLASH_CMD_RESET
; /* restore read mode */
267 return (0); /* => no or unknown flash */
271 if (info
->sector_count
> CONFIG_SYS_MAX_FLASH_SECT
) {
272 printf ("** ERROR: sector count %d > max (%d) **\n",
273 info
->sector_count
, CONFIG_SYS_MAX_FLASH_SECT
);
274 info
->sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
277 addr
[0] = FLASH_CMD_RESET
; /* restore read mode */
284 * Erase unprotected sectors
287 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
289 int flag
, prot
, sect
;
290 ulong start
, now
, last
;
292 if ((s_first
< 0) || (s_first
> s_last
)) {
293 if (info
->flash_id
== FLASH_UNKNOWN
) {
294 printf ("- missing\n");
296 printf ("- no sectors to erase\n");
301 if ((info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_INTEL
) {
302 printf ("Can erase only Intel flash types - aborted\n");
307 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
308 if (info
->protect
[sect
]) {
314 printf ("- Warning: %d protected sectors will not be erased!\n",
320 start
= get_timer (0);
323 /* Start erase on unprotected sectors */
324 for (sect
= s_first
; sect
<=s_last
; sect
++) {
325 if (info
->protect
[sect
] == 0) { /* not protected */
326 vu_short
*addr
= (vu_short
*)(info
->start
[sect
]);
327 unsigned long status
;
329 /* Disable interrupts which might cause a timeout here */
330 flag
= disable_interrupts();
333 printf("Erase sector %d at start addr 0x%08X", sect
, (unsigned int)info
->start
[sect
]);
336 *addr
= FLASH_CMD_CLEAR_STATUS
;
337 *addr
= FLASH_CMD_BLOCK_ERASE
;
338 *addr
= FLASH_CMD_ERASE_CONFIRM
;
340 /* re-enable interrupts if necessary */
344 /* wait at least 80us - let's wait 1 ms */
347 while (((status
= *addr
) & FLASH_STATUS_DONE
) != FLASH_STATUS_DONE
) {
348 if ((now
=get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
349 printf("Flash erase timeout at address %lx\n", info
->start
[sect
]);
350 *addr
= FLASH_CMD_SUSPEND_ERASE
;
351 *addr
= FLASH_CMD_RESET
;
355 /* show that we're waiting */
356 if ((now
- last
) > 1000) { /* every second */
361 *addr
= FLASH_CMD_RESET
;
369 * Copy memory to flash, returns:
372 * 2 - Flash not erased
373 * 4 - Flash not identified
376 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
382 if (info
->flash_id
== FLASH_UNKNOWN
) {
386 wp
= (addr
& ~1); /* get lower word aligned address */
389 * handle unaligned start byte
394 data
= (data
<< 8) | *src
++;
396 if ((rc
= write_short(info
, wp
, data
)) != 0) {
403 * handle word aligned part
408 for (i
=0; i
<2; ++i
) {
409 data
= (data
<< 8) | *src
++;
412 if ((rc
= write_short(info
, wp
, data
)) != 0) {
424 * handle unaligned tail bytes
428 for (i
=0, cp
=wp
; i
<2 && cnt
>0; ++i
, ++cp
) {
429 data
= (data
<< 8) | *src
++;
432 for (; i
<2; ++i
, ++cp
) {
433 data
= (data
<< 8) | (*(uchar
*)cp
);
436 return (write_short(info
, wp
, data
));
441 * Write 16 bit (short) to flash
444 static int write_short (flash_info_t
*info
, ulong dest
, ushort data
)
446 vu_short
*addr
= (vu_short
*)(info
->start
[0]);
450 /* Check if Flash is (sufficiently) erased */
451 if ((*((vu_short
*)dest
) & data
) != data
) {
455 /* Disable interrupts which might cause a timeout here */
456 flag
= disable_interrupts();
458 if (!(info
->flash_id
& FLASH_VENDMASK
)) {
461 *addr
= FLASH_CMD_ERASE_CONFIRM
;
462 *addr
= FLASH_CMD_WRITE
;
464 *((vu_short
*)dest
) = data
;
466 /* re-enable interrupts if necessary */
471 /* data polling for D7 */
472 start
= get_timer (0);
474 /* wait for error or finish */
475 while(!(addr
[0] & FLASH_STATUS_DONE
)){
476 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
477 addr
[0] = FLASH_CMD_RESET
;
482 *addr
= FLASH_CMD_RESET
;
487 * Protects a flash sector
490 int flash_real_protect(flash_info_t
*info
, long sector
, int prot
)
492 vu_short
*addr
= (vu_short
*)(info
->start
[sector
]);
495 *addr
= FLASH_CMD_CLEAR_STATUS
;
496 *addr
= FLASH_CMD_PROTECT
;
499 *addr
= FLASH_CMD_PROTECT_SET
;
501 *addr
= FLASH_CMD_PROTECT_CLEAR
;
504 /* wait for error or finish */
505 start
= get_timer (0);
506 while(!(addr
[0] & FLASH_STATUS_DONE
)){
507 if (get_timer(start
) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
508 printf("Flash protect timeout at address %lx\n", info
->start
[sector
]);
509 addr
[0] = FLASH_CMD_RESET
;
513 /* Set software protect flag */
514 info
->protect
[sector
] = prot
;
515 *addr
= FLASH_CMD_RESET
;