3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
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,
31 #include <linux/byteorder/swab.h>
33 #define CFG_MAX_FLASH_BANKS 1
34 #define PHYS_FLASH_SECT_SIZE 0x00020000 /* 128 KB sectors (x1) */
36 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
38 #define FLASH_PORT_WIDTH ushort
39 #define FLASH_PORT_WIDTHV vu_short
40 #define SWAP(x) __swab16(x)
42 #define FPW FLASH_PORT_WIDTH
43 #define FPWV FLASH_PORT_WIDTHV
45 #define mb() __asm__ __volatile__ ("" : : : "memory")
47 /* Intel-compatible flash commands */
48 #define INTEL_PROGRAM 0x00100010
49 #define INTEL_ERASE 0x00200020
50 #define INTEL_PROG 0x00400040
51 #define INTEL_CLEAR 0x00500050
52 #define INTEL_LOCKBIT 0x00600060
53 #define INTEL_PROTECT 0x00010001
54 #define INTEL_STATUS 0x00700070
55 #define INTEL_READID 0x00900090
56 #define INTEL_SUSPEND 0x00B000B0
57 #define INTEL_CONFIRM 0x00D000D0
58 #define INTEL_RESET 0xFFFFFFFF
60 /* Intel-compatible flash status bits */
61 #define INTEL_FINISHED 0x00800080
62 #define INTEL_OK 0x00800080
64 /*-----------------------------------------------------------------------
67 static ulong
flash_get_size (FPW
*addr
, flash_info_t
*info
);
68 static int write_data (flash_info_t
*info
, ulong dest
, FPW data
);
69 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
70 void inline spin_wheel (void);
72 /*-----------------------------------------------------------------------
75 unsigned long flash_init (void)
80 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
83 flash_get_size ((FPW
*) PHYS_FLASH_1
, &flash_info
[i
]);
84 flash_get_offsets (PHYS_FLASH_1
, &flash_info
[i
]);
87 panic ("configured too many flash banks!\n");
90 size
+= flash_info
[i
].size
;
93 /* Protect monitor and environment sectors
95 flash_protect ( FLAG_PROTECT_SET
,
97 CFG_FLASH_BASE
+ monitor_flash_len
- 1,
100 flash_protect ( FLAG_PROTECT_SET
,
102 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1, &flash_info
[0] );
107 /*-----------------------------------------------------------------------
109 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
113 if (info
->flash_id
== FLASH_UNKNOWN
) {
117 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
118 for (i
= 0; i
< info
->sector_count
; i
++) {
119 info
->start
[i
] = base
+ (i
* PHYS_FLASH_SECT_SIZE
);
120 info
->protect
[i
] = 0;
125 /*-----------------------------------------------------------------------
127 void flash_print_info (flash_info_t
*info
)
131 if (info
->flash_id
== FLASH_UNKNOWN
) {
132 printf ("missing or unknown FLASH type\n");
136 switch (info
->flash_id
& FLASH_VENDMASK
) {
137 case FLASH_MAN_INTEL
:
141 printf ("Unknown Vendor ");
145 switch (info
->flash_id
& FLASH_TYPEMASK
) {
146 case FLASH_28F640J3A
:
147 printf ("28F640J3A\n");
149 case FLASH_28F128J3A
:
150 printf ("28F128J3A\n");
153 printf ("Unknown Chip Type\n");
157 printf (" Size: %ld MB in %d Sectors\n",
158 info
->size
>> 20, info
->sector_count
);
160 printf (" Sector Start Addresses:");
161 for (i
= 0; i
< info
->sector_count
; ++i
) {
166 info
->protect
[i
] ? " (RO)" : " ");
173 * The following code cannot be run from FLASH!
175 static ulong
flash_get_size (FPW
*addr
, flash_info_t
*info
)
179 /* Write auto select command: read Manufacturer ID */
180 addr
[0x5555] = (FPW
) 0x00AA00AA;
181 addr
[0x2AAA] = (FPW
) 0x00550055;
182 addr
[0x5555] = (FPW
) 0x00900090;
189 case (FPW
) INTEL_MANUFACT
:
190 info
->flash_id
= FLASH_MAN_INTEL
;
194 info
->flash_id
= FLASH_UNKNOWN
;
195 info
->sector_count
= 0;
197 addr
[0] = (FPW
) INTEL_RESET
; /* restore read mode */
198 return (0); /* no or unknown flash */
202 value
= addr
[1]; /* device ID */
206 case (FPW
) INTEL_ID_28F640J3A
:
207 info
->flash_id
+= FLASH_28F640J3A
;
208 info
->sector_count
= 64;
209 info
->size
= 0x00800000;
212 case (FPW
) INTEL_ID_28F128J3A
:
213 info
->flash_id
+= FLASH_28F128J3A
;
214 info
->sector_count
= 128;
215 info
->size
= 0x01000000;
216 break; /* => 16 MB */
219 info
->flash_id
= FLASH_UNKNOWN
;
223 if (info
->sector_count
> CFG_MAX_FLASH_SECT
) {
224 printf ("** ERROR: sector count %d > max (%d) **\n",
225 info
->sector_count
, CFG_MAX_FLASH_SECT
);
226 info
->sector_count
= CFG_MAX_FLASH_SECT
;
229 addr
[0] = (FPW
) INTEL_RESET
; /* restore read mode */
235 /*-----------------------------------------------------------------------
238 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
241 ulong type
, start
, last
;
245 if ((s_first
< 0) || (s_first
> s_last
)) {
246 if (info
->flash_id
== FLASH_UNKNOWN
) {
247 printf ("- missing\n");
249 printf ("- no sectors to erase\n");
254 type
= (info
->flash_id
& FLASH_VENDMASK
);
255 if ((type
!= FLASH_MAN_INTEL
)) {
256 printf ("Can't erase unknown flash type %08lx - aborted\n",
262 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
263 if (info
->protect
[sect
]) {
269 printf ("- Warning: %d protected sectors will not be erased!\n",
275 start
= get_timer (0);
279 * Disable interrupts which might cause a timeout
280 * here. Remember that our exception vectors are
281 * at address 0 in the flash, and we don't want a
282 * (ticker) exception to happen while the flash
283 * chip is in programming mode.
285 cflag
= icache_status ();
287 /* Disable interrupts which might cause a timeout here */
288 iflag
= disable_interrupts ();
290 /* Start erase on unprotected sectors */
291 for (sect
= s_first
; sect
<= s_last
; sect
++) {
292 if (info
->protect
[sect
] == 0) { /* not protected */
293 FPWV
*addr
= (FPWV
*) (info
->start
[sect
]);
296 printf ("Erasing sector %2d ... ", sect
);
298 /* arm simple, non interrupt dependent timer */
299 reset_timer_masked ();
301 *addr
= (FPW
) INTEL_CLEAR
; /* clear status register */
302 *addr
= (FPW
) INTEL_ERASE
; /* erase setup */
303 *addr
= (FPW
) INTEL_CONFIRM
; /* erase confirm */
305 while (((status
= *addr
) & (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
) {
306 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT
) {
307 printf ("Timeout\n");
308 *addr
= (FPW
) INTEL_SUSPEND
; /* suspend erase */
309 *addr
= (FPW
) INTEL_RESET
; /* reset to read mode */
315 *addr
= (FPWV
)INTEL_CLEAR
; /* clear status register cmd. */
316 *addr
= (FPWV
)INTEL_RESET
; /* resest to read mode */
323 enable_interrupts ();
331 /*-----------------------------------------------------------------------
332 * Copy memory to flash, returns:
335 * 2 - Flash not erased
336 * 4 - Flash not identified
339 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
343 int count
, i
, l
, rc
, port_width
;
345 if (info
->flash_id
== FLASH_UNKNOWN
) {
349 /* get lower word aligned address */
354 * handle unaligned start bytes
356 if ((l
= addr
- wp
) != 0) {
358 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
359 data
= (data
<< 8) | (*(uchar
*) cp
);
361 for (; i
< port_width
&& cnt
> 0; ++i
) {
362 data
= (data
<< 8) | *src
++;
366 for (; cnt
== 0 && i
< port_width
; ++i
, ++cp
) {
367 data
= (data
<< 8) | (*(uchar
*) cp
);
370 if ((rc
= write_data (info
, wp
, SWAP (data
))) != 0) {
377 * handle word aligned part
380 while (cnt
>= port_width
) {
382 for (i
= 0; i
< port_width
; ++i
) {
383 data
= (data
<< 8) | *src
++;
385 if ((rc
= write_data (info
, wp
, SWAP (data
))) != 0) {
390 if (count
++ > 0x800) {
401 * handle unaligned tail bytes
404 for (i
= 0, cp
= wp
; i
< port_width
&& cnt
> 0; ++i
, ++cp
) {
405 data
= (data
<< 8) | *src
++;
408 for (; i
< port_width
; ++i
, ++cp
) {
409 data
= (data
<< 8) | (*(uchar
*) cp
);
412 return (write_data (info
, wp
, SWAP (data
)));
415 /*-----------------------------------------------------------------------
416 * Write a word or halfword to Flash, returns:
419 * 2 - Flash not erased
421 static int write_data (flash_info_t
*info
, ulong dest
, FPW data
)
423 FPWV
*addr
= (FPWV
*) dest
;
427 /* Check if Flash is (sufficiently) erased */
428 if ((*addr
& data
) != data
) {
429 printf ("not erased at %08lx (%lx)\n", (ulong
) addr
, *addr
);
433 * Disable interrupts which might cause a timeout
434 * here. Remember that our exception vectors are
435 * at address 0 in the flash, and we don't want a
436 * (ticker) exception to happen while the flash
437 * chip is in programming mode.
439 cflag
= icache_status ();
441 /* Disable interrupts which might cause a timeout here */
442 iflag
= disable_interrupts ();
444 *addr
= (FPW
) INTEL_PROG
; /* write setup */
447 /* arm simple, non interrupt dependent timer */
448 reset_timer_masked ();
450 /* wait while polling the status register */
451 while (((status
= *addr
) & (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
) {
452 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT
) {
453 *addr
= (FPW
) INTEL_RESET
; /* restore read mode */
458 *addr
= (FPW
) INTEL_RESET
; /* restore read mode */
461 enable_interrupts ();
469 void inline spin_wheel (void)
472 static char w
[] = "\\/-";
474 printf ("\010%c", w
[p
]);
475 (++p
== 3) ? (p
= 0) : 0;
478 /*-----------------------------------------------------------------------
479 * Set/Clear sector's lock bit, returns:
481 * 1 - Error (timeout, voltage problems, etc.)
483 int flash_real_protect(flash_info_t
*info
, long sector
, int prot
)
487 FPWV
*addr
= (FPWV
*)(info
->start
[sector
]);
488 int flag
= disable_interrupts();
490 *addr
= (FPW
) INTEL_CLEAR
; /* Clear status register */
491 if (prot
) { /* Set sector lock bit */
492 *addr
= (FPW
) INTEL_LOCKBIT
; /* Sector lock bit */
493 *addr
= (FPW
) INTEL_PROTECT
; /* set */
495 else { /* Clear sector lock bit */
496 *addr
= (FPW
) INTEL_LOCKBIT
; /* All sectors lock bits */
497 *addr
= (FPW
) INTEL_CONFIRM
; /* clear */
500 reset_timer_masked ();
502 while ((*addr
& (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
) {
503 if (get_timer_masked () > CFG_FLASH_UNLOCK_TOUT
) {
504 printf("Flash lock bit operation timed out\n");
510 if (*addr
!= (FPW
) INTEL_OK
) {
511 printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
512 (uint
)addr
, (uint
)*addr
);
517 info
->protect
[sector
] = prot
;
520 * Clear lock bit command clears all sectors lock bits, so
521 * we have to restore lock bits of protected sectors.
525 for (i
= 0; i
< info
->sector_count
; i
++)
527 if (info
->protect
[i
])
529 reset_timer_masked ();
530 addr
= (FPWV
*) (info
->start
[i
]);
531 *addr
= (FPW
) INTEL_LOCKBIT
; /* Sector lock bit */
532 *addr
= (FPW
) INTEL_PROTECT
; /* set */
533 while ((*addr
& (FPW
) INTEL_FINISHED
) != (FPW
) INTEL_FINISHED
)
535 if (get_timer_masked () > CFG_FLASH_UNLOCK_TOUT
)
537 printf("Flash lock bit operation timed out\n");
549 *addr
= (FPW
) INTEL_RESET
; /* Reset to read array mode */