3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * flash_real_protect() routine based on boards/alaska/flash.c
7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 /* Intel-compatible flash commands */
31 #define INTEL_ERASE 0x20
32 #define INTEL_PROGRAM 0x40
33 #define INTEL_CLEAR 0x50
34 #define INTEL_LOCKBIT 0x60
35 #define INTEL_PROTECT 0x01
36 #define INTEL_STATUS 0x70
37 #define INTEL_READID 0x90
38 #define INTEL_READID 0x90
39 #define INTEL_SUSPEND 0xB0
40 #define INTEL_CONFIRM 0xD0
41 #define INTEL_RESET 0xFF
43 /* Intel-compatible flash status bits */
44 #define INTEL_FINISHED 0x80
47 typedef unsigned char FLASH_PORT_WIDTH
;
48 typedef volatile unsigned char FLASH_PORT_WIDTHV
;
49 #define FPW FLASH_PORT_WIDTH
50 #define FPWV FLASH_PORT_WIDTHV
51 #define FLASH_ID_MASK 0xFF
53 #define ORMASK(size) ((-size) & OR_AM_MSK)
55 #define FLASH_CYCLE1 0x0555
56 #define FLASH_CYCLE2 0x02aa
58 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
60 /*-----------------------------------------------------------------------
63 static ulong
flash_get_size(FPWV
*addr
, flash_info_t
*info
);
64 static void flash_reset(flash_info_t
*info
);
65 static flash_info_t
*flash_get_info(ulong base
);
66 static int write_data (flash_info_t
*info
, FPWV
*dest
, FPW data
); /* O2D */
67 static void flash_sync_real_protect (flash_info_t
* info
);
68 static unsigned char intel_sector_protected (flash_info_t
*info
, ushort sector
);
70 /*-----------------------------------------------------------------------
73 * sets up flash_info and returns size of FLASH (bytes)
75 unsigned long flash_init (void)
77 unsigned long size
= 0;
79 extern void flash_preinit(void);
80 extern void flash_afterinit(ulong
);
84 /* Init: no FLASHes known */
85 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; ++i
) {
86 memset(&flash_info
[i
], 0, sizeof(flash_info_t
));
87 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
90 /* Query flash chip */
92 flash_get_size((FPW
*)CFG_FLASH_BASE
, &flash_info
[0]);
93 size
+= flash_info
[0].size
;
95 /* get the h/w and s/w protection status in sync */
96 flash_sync_real_protect(&flash_info
[0]);
98 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
99 /* monitor protection ON by default */
100 flash_protect(FLAG_PROTECT_SET
,
102 CFG_MONITOR_BASE
+monitor_flash_len
-1,
103 flash_get_info(CFG_MONITOR_BASE
));
106 #ifdef CFG_ENV_IS_IN_FLASH
107 /* ENV protection ON by default */
108 flash_protect(FLAG_PROTECT_SET
,
110 CFG_ENV_ADDR
+CFG_ENV_SIZE
-1,
111 flash_get_info(CFG_ENV_ADDR
));
115 flash_afterinit(size
);
116 return (size
? size
: 1);
119 /*-----------------------------------------------------------------------
121 static void flash_reset(flash_info_t
*info
)
123 FPWV
*base
= (FPWV
*)(info
->start
[0]);
125 /* Put FLASH back in read mode */
126 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
)
127 *base
= (FPW
) INTEL_RESET
; /* Intel Read Mode */
130 /*-----------------------------------------------------------------------
133 static flash_info_t
*flash_get_info(ulong base
)
138 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
139 info
= & flash_info
[i
];
141 info
->start
[0] <= base
&&
142 base
<= info
->start
[0] + info
->size
- 1)
146 return (i
== CFG_MAX_FLASH_BANKS
? 0 : info
);
149 /*-----------------------------------------------------------------------
152 void flash_print_info (flash_info_t
*info
)
158 uchar botbootletter
[] = "B";
159 uchar topbootletter
[] = "T";
160 uchar botboottype
[] = "bottom boot sector";
161 uchar topboottype
[] = "top boot sector";
163 if (info
->flash_id
== FLASH_UNKNOWN
) {
164 printf ("missing or unknown FLASH type\n");
168 switch (info
->flash_id
& FLASH_VENDMASK
) {
169 case FLASH_MAN_INTEL
:
173 printf ("Unknown Vendor ");
177 /* check for top or bottom boot, if it applies */
178 if (info
->flash_id
& FLASH_BTYPE
) {
179 boottype
= botboottype
;
180 bootletter
= botbootletter
;
182 boottype
= topboottype
;
183 bootletter
= topbootletter
;
186 switch (info
->flash_id
& FLASH_TYPEMASK
) {
187 case FLASH_28F128J3A
:
188 fmt
= "28F128J3 (128 Mbit, uniform sectors)\n";
191 fmt
= "Unknown Chip Type\n";
195 printf (fmt
, bootletter
, boottype
);
196 printf (" Size: %ld MB in %d Sectors\n",
200 printf (" Sector Start Addresses:");
201 for (i
=0; i
<info
->sector_count
; ++i
) {
205 printf (" %08lX%s", info
->start
[i
],
206 info
->protect
[i
] ? " (RO)" : " ");
211 /*-----------------------------------------------------------------------
215 * The following code cannot be run from FLASH!
217 ulong
flash_get_size (FPWV
*addr
, flash_info_t
*info
)
221 /* Write auto select command: read Manufacturer ID */
222 /* Write auto select command sequence and test FLASH answer */
223 addr
[FLASH_CYCLE1
] = (FPW
) INTEL_READID
; /* selects Intel or AMD */
225 /* The manufacturer codes are only 1 byte, so just use 1 byte.
226 * This works for any bus width and any FLASH device width.
229 switch (addr
[0] & 0xff) {
230 case (uchar
)INTEL_MANUFACT
:
231 info
->flash_id
= FLASH_MAN_INTEL
;
234 info
->flash_id
= FLASH_UNKNOWN
;
235 info
->sector_count
= 0;
240 /* Strataflash is configurable to 8/16bit Bus,
241 * but the Query-Structure is Word-orientated */
242 if (info
->flash_id
!= FLASH_UNKNOWN
) {
243 switch ((FPW
)addr
[2]) {
244 case (FPW
)INTEL_ID_28F128J3
:
245 info
->flash_id
+= FLASH_28F128J3A
;
246 info
->sector_count
= 128;
247 info
->size
= 0x01000000;
248 for( i
= 0; i
< info
->sector_count
; i
++ )
249 info
->start
[i
] = (ulong
)addr
+ (i
* 0x20000);
250 break; /* => Intel Strataflash 16MB */
252 printf("Flash_id != %xd\n", (FPW
)addr
[2]);
253 info
->flash_id
= FLASH_UNKNOWN
;
254 info
->sector_count
= 0;
256 return (0); /* => no or unknown flash */
260 /* Put FLASH back in read mode */
266 /*-----------------------------------------------------------------------
269 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
272 int flag
, prot
, sect
;
273 ulong start
, now
, last
;
276 if ((s_first
< 0) || (s_first
> s_last
)) {
277 if (info
->flash_id
== FLASH_UNKNOWN
) {
278 printf ("- missing\n");
280 printf ("- no sectors to erase\n");
285 switch (info
->flash_id
& FLASH_TYPEMASK
) {
286 case FLASH_28F128J3A
:
290 printf ("Can't erase unknown flash type %08lx - aborted\n",
296 for (sect
= s_first
; sect
<= s_last
; ++sect
)
297 if (info
->protect
[sect
])
301 printf ("- Warning: %d protected sectors will not be erased!",
307 /* Start erase on unprotected sectors */
308 for (sect
= s_first
; sect
<= s_last
&& rcode
== 0; sect
++) {
310 if (info
->protect
[sect
] != 0) /* protected, skip it */
313 /* Disable interrupts which might cause a timeout here */
314 flag
= disable_interrupts();
316 addr
= (FPWV
*)(info
->start
[sect
]);
317 *addr
= (FPW
) INTEL_CLEAR
; /* clear status register */
318 *addr
= (FPW
) INTEL_ERASE
; /* erase setup */
319 *addr
= (FPW
) INTEL_CONFIRM
; /* erase confirm */
321 /* re-enable interrupts if necessary */
325 start
= get_timer(0);
327 /* wait at least 80us for Intel - let's wait 1 ms */
330 while ((*addr
& (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
) {
331 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
332 printf ("Timeout\n");
333 *addr
= (FPW
) INTEL_SUSPEND
;/* suspend erase */
334 flash_reset(info
); /* reset to read mode */
335 rcode
= 1; /* failed */
339 /* show that we're waiting */
340 if ((get_timer(last
)) > CFG_HZ
) { /* every second */
346 flash_reset(info
); /* reset to read mode */
353 /*-----------------------------------------------------------------------
354 * Copy memory to flash, returns:
357 * 2 - Flash not erased
359 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
361 FPW data
= 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
362 int bytes
; /* number of bytes to program in current word */
363 int left
; /* number of bytes left to program */
366 for (left
= cnt
, res
= 0;
367 left
> 0 && res
== 0;
368 addr
+= sizeof(data
), left
-= sizeof(data
) - bytes
) {
370 bytes
= addr
& (sizeof(data
) - 1);
371 addr
&= ~(sizeof(data
) - 1);
373 /* combine source and destination data so can program
374 * an entire word of 16 or 32 bits */
375 for (i
= 0; i
< sizeof(data
); i
++) {
377 if (i
< bytes
|| i
- bytes
>= left
)
378 data
+= *((uchar
*)addr
+ i
);
383 /* write one word to the flash */
384 switch (info
->flash_id
& FLASH_VENDMASK
) {
385 case FLASH_MAN_INTEL
:
386 res
= write_data(info
, (FPWV
*)addr
, data
);
389 /* unknown flash type, error! */
390 printf ("missing or unknown FLASH type\n");
391 res
= 1; /* not really a timeout, but gives error */
399 /*-----------------------------------------------------------------------
400 * Write a word or halfword to Flash, returns:
403 * 2 - Flash not erased
405 static int write_data (flash_info_t
*info
, FPWV
*dest
, FPW data
)
412 /* Check if Flash is (sufficiently) erased */
413 if ((*addr
& data
) != data
) {
414 printf ("not erased at %08lx (%lx)\n", (ulong
) addr
, *addr
);
417 /* Disable interrupts which might cause a timeout here */
418 flag
= disable_interrupts ();
420 *addr
= (FPW
) INTEL_PROGRAM
; /* write setup */
423 /* arm simple, non interrupt dependent timer */
424 start
= get_timer(0);
426 /* wait while polling the status register */
427 while (((status
= *addr
) & (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
) {
428 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
429 *addr
= (FPW
) INTEL_RESET
; /* restore read mode */
434 *addr
= (FPW
) INTEL_RESET
; /* restore read mode */
442 /*-----------------------------------------------------------------------
443 * Set/Clear sector's lock bit, returns:
445 * 1 - Error (timeout, voltage problems, etc.)
447 int flash_real_protect (flash_info_t
* info
, long sector
, int prot
)
452 FPWV
*addr
= (FPWV
*) (info
->start
[sector
]);
453 int flag
= disable_interrupts ();
455 *addr
= INTEL_CLEAR
; /* Clear status register */
456 if (prot
) { /* Set sector lock bit */
457 *addr
= INTEL_LOCKBIT
; /* Sector lock bit */
458 *addr
= INTEL_PROTECT
; /* set */
459 } else { /* Clear sector lock bit */
460 *addr
= INTEL_LOCKBIT
; /* All sectors lock bits */
461 *addr
= INTEL_CONFIRM
; /* clear */
464 start
= get_timer (0);
466 while ((*addr
& INTEL_FINISHED
) != INTEL_FINISHED
) {
467 if (get_timer (start
) > CFG_FLASH_UNLOCK_TOUT
) {
468 printf ("Flash lock bit operation timed out\n");
474 if (*addr
!= INTEL_OK
) {
475 printf ("Flash lock bit operation failed at %08X, CSR=%08X\n",
476 (uint
) addr
, (uint
) * addr
);
481 info
->protect
[sector
] = prot
;
484 * Clear lock bit command clears all sectors lock bits, so
485 * we have to restore lock bits of protected sectors.
488 for (i
= 0; i
< info
->sector_count
; i
++) {
489 if (info
->protect
[i
]) {
490 start
= get_timer (0);
491 addr
= (FPWV
*) (info
->start
[i
]);
492 *addr
= INTEL_LOCKBIT
; /* Sector lock bit */
493 *addr
= INTEL_PROTECT
; /* set */
494 while ((*addr
& INTEL_FINISHED
) !=
496 if (get_timer (start
) >
497 CFG_FLASH_UNLOCK_TOUT
) {
498 printf ("Flash lock bit operation timed out\n");
508 enable_interrupts ();
510 *addr
= INTEL_RESET
; /* Reset to read array mode */
517 * This function gets the u-boot flash sector protection status
518 * (flash_info_t.protect[]) in sync with the sector protection
519 * status stored in hardware.
521 static void flash_sync_real_protect (flash_info_t
* info
)
524 switch (info
->flash_id
& FLASH_TYPEMASK
) {
525 case FLASH_28F128J3A
:
526 for (i
= 0; i
< info
->sector_count
; ++i
) {
527 info
->protect
[i
] = intel_sector_protected(info
, i
);
531 /* no h/w protect support */
538 * checks if "sector" in bank "info" is protected. Should work on intel
539 * strata flash chips 28FxxxJ3x in 8-bit mode.
540 * Returns 1 if sector is protected (or timed-out while trying to read
541 * protection status), 0 if it is not.
543 static unsigned char intel_sector_protected (flash_info_t
*info
, ushort sector
)
546 FPWV
*lock_conf_addr
;
551 * first, wait for the WSM to be finished. The rationale for
552 * waiting for the WSM to become idle for at most
553 * CFG_FLASH_ERASE_TOUT is as follows. The WSM can be busy
554 * because of: (1) erase, (2) program or (3) lock bit
555 * configuration. So we just wait for the longest timeout of
556 * the (1)-(3), i.e. the erase timeout.
559 /* wait at least 35ns (W12) before issuing Read Status Register */
561 addr
= (FPWV
*) info
->start
[sector
];
562 *addr
= (FPW
) INTEL_STATUS
;
564 start
= get_timer (0);
565 while ((*addr
& (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
) {
566 if (get_timer (start
) > CFG_FLASH_ERASE_TOUT
) {
567 *addr
= (FPW
) INTEL_RESET
; /* restore read mode */
568 printf("WSM busy too long, can't get prot status\n");
573 /* issue the Read Identifier Codes command */
574 *addr
= (FPW
) INTEL_READID
;
576 /* wait at least 35ns (W12) before reading */
579 /* Intel example code uses offset of 4 for 8-bit flash */
580 lock_conf_addr
= (FPWV
*) info
->start
[sector
] + 4;
581 ret
= (*lock_conf_addr
& (FPW
) INTEL_PROTECT
) ? 1 : 0;
583 /* put flash back in read mode */
584 *addr
= (FPW
) INTEL_RESET
;