3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
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 #define FLASH_BANK_SIZE 0x400000
31 #define MAIN_SECT_SIZE 0x20000
33 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
36 /*-----------------------------------------------------------------------
39 ulong
flash_init (void)
44 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
47 flash_info
[i
].flash_id
=
48 (INTEL_MANUFACT
& FLASH_VENDMASK
) |
49 (INTEL_ID_28F128J3
& FLASH_TYPEMASK
);
50 flash_info
[i
].size
= FLASH_BANK_SIZE
;
51 flash_info
[i
].sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
52 memset (flash_info
[i
].protect
, 0, CONFIG_SYS_MAX_FLASH_SECT
);
55 flashbase
= PHYS_FLASH_1
;
58 flashbase
= PHYS_FLASH_2
;
61 panic ("configured too many flash banks!\n");
64 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
65 flash_info
[i
].start
[j
] =
66 flashbase
+ j
* MAIN_SECT_SIZE
;
68 size
+= flash_info
[i
].size
;
71 /* Protect monitor and environment sectors
73 flash_protect (FLAG_PROTECT_SET
,
74 CONFIG_SYS_FLASH_BASE
,
75 CONFIG_SYS_FLASH_BASE
+ monitor_flash_len
- 1,
78 flash_protect (FLAG_PROTECT_SET
,
80 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1, &flash_info
[0]);
85 /*-----------------------------------------------------------------------
87 void flash_print_info (flash_info_t
* info
)
91 for (j
= 0; j
< CONFIG_SYS_MAX_FLASH_BANKS
; j
++) {
92 switch (info
->flash_id
& FLASH_VENDMASK
) {
93 case (INTEL_MANUFACT
& FLASH_VENDMASK
):
97 printf ("Unknown Vendor ");
101 switch (info
->flash_id
& FLASH_TYPEMASK
) {
102 case (INTEL_ID_28F320J3A
& FLASH_TYPEMASK
):
103 printf ("28F320J3A (32Mbit)\n");
105 case (INTEL_ID_28F128J3
& FLASH_TYPEMASK
):
106 printf ("28F128J3 (128Mbit)\n");
109 printf ("Unknown Chip Type\n");
114 printf (" Size: %ld MB in %d Sectors\n",
115 info
->size
>> 20, info
->sector_count
);
117 printf (" Sector Start Addresses:");
118 for (i
= 0; i
< info
->sector_count
; i
++) {
122 printf (" %08lX%s", info
->start
[i
],
123 info
->protect
[i
] ? " (RO)" : " ");
132 /*-----------------------------------------------------------------------
135 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
137 int flag
, prot
, sect
;
140 if (info
->flash_id
== FLASH_UNKNOWN
)
141 return ERR_UNKNOWN_FLASH_TYPE
;
143 if ((s_first
< 0) || (s_first
> s_last
)) {
147 if ((info
->flash_id
& FLASH_VENDMASK
) !=
148 (INTEL_MANUFACT
& FLASH_VENDMASK
)) {
149 return ERR_UNKNOWN_FLASH_VENDOR
;
153 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
154 if (info
->protect
[sect
]) {
159 return ERR_PROTECTED
;
162 * Disable interrupts which might cause a timeout
163 * here. Remember that our exception vectors are
164 * at address 0 in the flash, and we don't want a
165 * (ticker) exception to happen while the flash
166 * chip is in programming mode.
168 flag
= disable_interrupts ();
170 /* Start erase on unprotected sectors */
171 for (sect
= s_first
; sect
<= s_last
&& !ctrlc (); sect
++) {
173 printf ("Erasing sector %2d ... ", sect
);
175 /* arm simple, non interrupt dependent timer */
176 reset_timer_masked ();
178 if (info
->protect
[sect
] == 0) { /* not protected */
179 vu_short
*addr
= (vu_short
*) (info
->start
[sect
]);
181 *addr
= 0x20; /* erase setup */
182 *addr
= 0xD0; /* erase confirm */
184 while ((*addr
& 0x80) != 0x80) {
185 if (get_timer_masked () >
186 CONFIG_SYS_FLASH_ERASE_TOUT
) {
187 *addr
= 0xB0; /* suspend erase */
188 *addr
= 0xFF; /* reset to read mode */
194 /* clear status register command */
196 /* reset to read mode */
202 printf ("User Interrupt!\n");
206 /* allow flash to settle - wait 10 ms */
207 udelay_masked (10000);
210 enable_interrupts ();
215 /*-----------------------------------------------------------------------
216 * Copy memory to flash
219 static int write_word (flash_info_t
* info
, ulong dest
, ushort data
)
221 vu_short
*addr
= (vu_short
*) dest
, val
;
225 /* Check if Flash is (sufficiently) erased
227 if ((*addr
& data
) != data
)
228 return ERR_NOT_ERASED
;
231 * Disable interrupts which might cause a timeout
232 * here. Remember that our exception vectors are
233 * at address 0 in the flash, and we don't want a
234 * (ticker) exception to happen while the flash
235 * chip is in programming mode.
237 flag
= disable_interrupts ();
239 /* clear status register command */
242 /* program set-up command */
245 /* latch address/data */
248 /* arm simple, non interrupt dependent timer */
249 reset_timer_masked ();
251 /* wait while polling the status register */
252 while (((val
= *addr
) & 0x80) != 0x80) {
253 if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT
) {
255 /* suspend program command */
261 if (val
& 0x1A) { /* check for error */
262 printf ("\nFlash write error %02x at address %08lx\n",
263 (int) val
, (unsigned long) dest
);
264 if (val
& (1 << 3)) {
265 printf ("Voltage range error.\n");
269 if (val
& (1 << 1)) {
270 printf ("Device protect error.\n");
274 if (val
& (1 << 4)) {
275 printf ("Programming error.\n");
284 /* read array command */
288 enable_interrupts ();
293 /*-----------------------------------------------------------------------
294 * Copy memory to flash.
297 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
304 wp
= (addr
& ~1); /* get lower word aligned address */
307 * handle unaligned start bytes
309 if ((l
= addr
- wp
) != 0) {
311 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
312 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
314 for (; i
< 2 && cnt
> 0; ++i
) {
315 data
= (data
>> 8) | (*src
++ << 8);
319 for (; cnt
== 0 && i
< 2; ++i
, ++cp
) {
320 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
323 if ((rc
= write_word (info
, wp
, data
)) != 0) {
330 * handle word aligned part
333 data
= *((vu_short
*) src
);
334 if ((rc
= write_word (info
, wp
, data
)) != 0) {
347 * handle unaligned tail bytes
350 for (i
= 0, cp
= wp
; i
< 2 && cnt
> 0; ++i
, ++cp
) {
351 data
= (data
>> 8) | (*src
++ << 8);
354 for (; i
< 2; ++i
, ++cp
) {
355 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
358 return write_word (info
, wp
, data
);