3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
34 #if defined(CFG_ENV_IS_IN_FLASH)
36 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
39 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
41 # ifndef CFG_ENV_SECT_SIZE
42 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
46 /*-----------------------------------------------------------------------
49 #define FLAG_PROTECT_SET 0x01
50 #define FLAG_PROTECT_CLEAR 0x02
52 /* Board support for 1 or 2 flash devices */
53 #undef FLASH_PORT_WIDTH32
54 #define FLASH_PORT_WIDTH16
56 #ifdef FLASH_PORT_WIDTH16
57 #define FLASH_PORT_WIDTH ushort
58 #define FLASH_PORT_WIDTHV vu_short
60 #define FLASH_PORT_WIDTH ulong
61 #define FLASH_PORT_WIDTHV vu_long
64 #define FPW FLASH_PORT_WIDTH
65 #define FPWV FLASH_PORT_WIDTHV
67 /*-----------------------------------------------------------------------
70 static ulong
flash_get_size (FPW
* addr
, flash_info_t
* info
);
71 static int write_data (flash_info_t
* info
, ulong dest
, FPW data
);
72 static void flash_get_offsets (ulong base
, flash_info_t
* info
);
74 /*-----------------------------------------------------------------------
77 unsigned long flash_init (void)
79 volatile immap_t
*immap
= (immap_t
*) CFG_IMMR
;
80 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
81 unsigned long size_b0
;
84 /* Init: no FLASHes known */
85 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; ++i
) {
86 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
89 /* Static FLASH Bank configuration here - FIXME XXX */
90 size_b0
= flash_get_size ((FPW
*) FLASH_BASE0_PRELIM
, &flash_info
[0]);
92 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
93 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
94 size_b0
, size_b0
<< 20);
97 /* Remap FLASH according to real size */
98 memctl
->memc_or0
= CFG_OR_TIMING_FLASH
| (-size_b0
& 0xFFFF8000);
99 memctl
->memc_br0
= (CFG_FLASH_BASE
& BR_BA_MSK
) | BR_PS_16
| BR_MS_GPCM
| BR_V
;
101 /* Re-do sizing to get full correct info */
102 size_b0
= flash_get_size ((FPW
*) CFG_FLASH_BASE
, &flash_info
[0]);
104 flash_get_offsets (CFG_FLASH_BASE
, &flash_info
[0]);
106 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
107 /* monitor protection ON by default */
108 (void) flash_protect (FLAG_PROTECT_SET
,
110 CFG_FLASH_BASE
+ monitor_flash_len
- 1,
114 #ifdef CFG_ENV_IS_IN_FLASH
115 /* ENV protection ON by default */
116 flash_protect (FLAG_PROTECT_SET
,
118 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1,
122 flash_info
[0].size
= size_b0
;
127 /*-----------------------------------------------------------------------
129 static void flash_get_offsets (ulong base
, flash_info_t
* info
)
133 if (info
->flash_id
== FLASH_UNKNOWN
) {
137 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
138 for (i
= 0; i
< info
->sector_count
; i
++) {
139 info
->start
[i
] = base
+ (i
* 0x00020000);
144 /*-----------------------------------------------------------------------
146 void flash_print_info (flash_info_t
* info
)
150 if (info
->flash_id
== FLASH_UNKNOWN
) {
151 printf ("missing or unknown FLASH type\n");
155 switch (info
->flash_id
& FLASH_VENDMASK
) {
156 case FLASH_MAN_INTEL
:
160 printf ("Unknown Vendor ");
164 switch (info
->flash_id
& FLASH_TYPEMASK
) {
165 case FLASH_28F320J3A
:
166 printf ("28F320J3A\n");
168 case FLASH_28F640J3A
:
169 printf ("28F640J3A\n");
171 case FLASH_28F128J3A
:
172 printf ("28F128J3A\n");
175 printf ("Unknown Chip Type\n");
179 printf (" Size: %ld MB in %d Sectors\n",
180 info
->size
>> 20, info
->sector_count
);
182 printf (" Sector Start Addresses:");
183 for (i
= 0; i
< info
->sector_count
; ++i
) {
188 info
->protect
[i
] ? " (RO)" : " ");
194 /*-----------------------------------------------------------------------
198 /*-----------------------------------------------------------------------
202 * The following code cannot be run from FLASH!
205 static ulong
flash_get_size (FPW
* addr
, flash_info_t
* info
)
209 /* Make sure Block Lock Bits get cleared */
210 addr
[0] = (FPW
) 0x00FF00FF;
211 addr
[0] = (FPW
) 0x00600060;
212 addr
[0] = (FPW
) 0x00D000D0;
213 addr
[0] = (FPW
) 0x00FF00FF;
215 /* Write auto select command: read Manufacturer ID */
216 addr
[0x5555] = (FPW
) 0x00AA00AA;
217 addr
[0x2AAA] = (FPW
) 0x00550055;
218 addr
[0x5555] = (FPW
) 0x00900090;
222 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong
)addr
, value
);
225 case (FPW
) INTEL_MANUFACT
:
226 info
->flash_id
= FLASH_MAN_INTEL
;
229 info
->flash_id
= FLASH_UNKNOWN
;
230 info
->sector_count
= 0;
232 addr
[0] = (FPW
) 0x00FF00FF; /* restore read mode */
233 return (0); /* no or unknown flash */
236 value
= addr
[1]; /* device ID */
238 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong
)(&addr
[1]), value
);
241 case (FPW
) INTEL_ID_28F320J3A
:
242 info
->flash_id
+= FLASH_28F320J3A
;
243 info
->sector_count
= 32;
244 info
->size
= 0x00400000;
247 case (FPW
) INTEL_ID_28F640J3A
:
248 info
->flash_id
+= FLASH_28F640J3A
;
249 info
->sector_count
= 64;
250 info
->size
= 0x00800000;
253 case (FPW
) INTEL_ID_28F128J3A
:
254 info
->flash_id
+= FLASH_28F128J3A
;
255 info
->sector_count
= 128;
256 info
->size
= 0x01000000;
257 break; /* => 16 MB */
260 info
->flash_id
= FLASH_UNKNOWN
;
264 if (info
->sector_count
> CFG_MAX_FLASH_SECT
) {
265 printf ("** ERROR: sector count %d > max (%d) **\n",
266 info
->sector_count
, CFG_MAX_FLASH_SECT
);
267 info
->sector_count
= CFG_MAX_FLASH_SECT
;
270 addr
[0] = (FPW
) 0x00FF00FF; /* restore read mode */
276 /*-----------------------------------------------------------------------
279 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
281 int flag
, prot
, sect
;
282 ulong type
, start
, now
, last
;
285 if ((s_first
< 0) || (s_first
> s_last
)) {
286 if (info
->flash_id
== FLASH_UNKNOWN
) {
287 printf ("- missing\n");
289 printf ("- no sectors to erase\n");
294 type
= (info
->flash_id
& FLASH_VENDMASK
);
295 if ((type
!= FLASH_MAN_INTEL
)) {
296 printf ("Can't erase unknown flash type %08lx - aborted\n",
302 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
303 if (info
->protect
[sect
]) {
309 printf ("- Warning: %d protected sectors will not be erased!\n",
315 start
= get_timer (0);
317 /* Start erase on unprotected sectors */
318 for (sect
= s_first
; sect
<= s_last
; sect
++) {
319 if (info
->protect
[sect
] == 0) { /* not protected */
320 FPWV
*addr
= (FPWV
*) (info
->start
[sect
]);
323 /* Disable interrupts which might cause a timeout here */
324 flag
= disable_interrupts ();
326 *addr
= (FPW
) 0x00500050; /* clear status register */
327 *addr
= (FPW
) 0x00200020; /* erase setup */
328 *addr
= (FPW
) 0x00D000D0; /* erase confirm */
330 /* re-enable interrupts if necessary */
332 enable_interrupts ();
334 /* wait at least 80us - let's wait 1 ms */
337 while (((status
= *addr
) & (FPW
) 0x00800080) != (FPW
) 0x00800080) {
338 if ((now
= get_timer (start
)) > CFG_FLASH_ERASE_TOUT
) {
339 printf ("Timeout\n");
340 *addr
= (FPW
) 0x00B000B0; /* suspend erase */
341 *addr
= (FPW
) 0x00FF00FF; /* reset to read mode */
346 /* show that we're waiting */
347 if ((now
- last
) > 1000) { /* every second */
353 *addr
= (FPW
) 0x00FF00FF; /* reset to read mode */
360 /*-----------------------------------------------------------------------
361 * Copy memory to flash, returns:
364 * 2 - Flash not erased
365 * 4 - Flash not identified
368 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
373 int i
, l
, rc
, port_width
;
375 if (info
->flash_id
== FLASH_UNKNOWN
) {
378 /* get lower word aligned address */
379 #ifdef FLASH_PORT_WIDTH16
388 * handle unaligned start bytes
390 if ((l
= addr
- wp
) != 0) {
392 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
393 data
= (data
<< 8) | (*(uchar
*) cp
);
395 for (; i
< port_width
&& cnt
> 0; ++i
) {
396 data
= (data
<< 8) | *src
++;
400 for (; cnt
== 0 && i
< port_width
; ++i
, ++cp
) {
401 data
= (data
<< 8) | (*(uchar
*) cp
);
404 if ((rc
= write_data (info
, wp
, data
)) != 0) {
411 * handle word aligned part
413 while (cnt
>= port_width
) {
415 for (i
= 0; i
< port_width
; ++i
) {
416 data
= (data
<< 8) | *src
++;
418 if ((rc
= write_data (info
, wp
, data
)) != 0) {
430 * handle unaligned tail bytes
433 for (i
= 0, cp
= wp
; i
< port_width
&& cnt
> 0; ++i
, ++cp
) {
434 data
= (data
<< 8) | *src
++;
437 for (; i
< port_width
; ++i
, ++cp
) {
438 data
= (data
<< 8) | (*(uchar
*) cp
);
441 return (write_data (info
, wp
, data
));
444 /*-----------------------------------------------------------------------
445 * Write a word or halfword to Flash, returns:
448 * 2 - Flash not erased
450 static int write_data (flash_info_t
* info
, ulong dest
, FPW data
)
452 FPWV
*addr
= (FPWV
*) dest
;
457 /* Check if Flash is (sufficiently) erased */
458 if ((*addr
& data
) != data
) {
459 printf ("not erased at %08lx (%x)\n", (ulong
) addr
, *addr
);
462 /* Disable interrupts which might cause a timeout here */
463 flag
= disable_interrupts ();
465 *addr
= (FPW
) 0x00400040; /* write setup */
468 /* re-enable interrupts if necessary */
470 enable_interrupts ();
472 start
= get_timer (0);
474 while (((status
= *addr
) & (FPW
) 0x00800080) != (FPW
) 0x00800080) {
475 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
) {
476 *addr
= (FPW
) 0x00FF00FF; /* restore read mode */
481 *addr
= (FPW
) 0x00FF00FF; /* restore read mode */