3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #define FLASH_BANK_SIZE 0x1000000
28 #define MAIN_SECT_SIZE 0x20000
30 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
33 /*-----------------------------------------------------------------------
36 ulong
flash_init (void)
41 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
44 flash_info
[i
].flash_id
=
45 (INTEL_MANUFACT
& FLASH_VENDMASK
) |
46 (INTEL_ID_28F128J3
& FLASH_TYPEMASK
);
47 flash_info
[i
].size
= FLASH_BANK_SIZE
;
48 flash_info
[i
].sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
49 memset (flash_info
[i
].protect
, 0, CONFIG_SYS_MAX_FLASH_SECT
);
51 flashbase
= PHYS_FLASH_1
;
53 panic ("configured too many flash banks!\n");
54 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
55 flash_info
[i
].start
[j
] = flashbase
+ j
* MAIN_SECT_SIZE
;
57 size
+= flash_info
[i
].size
;
60 /* Protect monitor and environment sectors
62 flash_protect ( FLAG_PROTECT_SET
,
63 CONFIG_SYS_FLASH_BASE
,
64 CONFIG_SYS_FLASH_BASE
+ monitor_flash_len
- 1,
67 flash_protect ( FLAG_PROTECT_SET
,
69 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1, &flash_info
[0]);
74 /*-----------------------------------------------------------------------
76 void flash_print_info (flash_info_t
* info
)
80 switch (info
->flash_id
& FLASH_VENDMASK
) {
81 case (INTEL_MANUFACT
& FLASH_VENDMASK
):
85 printf ("Unknown Vendor ");
89 switch (info
->flash_id
& FLASH_TYPEMASK
) {
90 case (INTEL_ID_28F128J3
& FLASH_TYPEMASK
):
91 printf ("28F128J3 (128Mbit)\n");
94 printf ("Unknown Chip Type\n");
99 printf (" Size: %ld MB in %d Sectors\n",
100 info
->size
>> 20, info
->sector_count
);
102 printf (" Sector Start Addresses:");
103 for (i
= 0; i
< info
->sector_count
; i
++) {
107 printf (" %08lX%s", info
->start
[i
],
108 info
->protect
[i
] ? " (RO)" : " ");
115 /*-----------------------------------------------------------------------
118 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
120 int flag
, prot
, sect
;
123 if (info
->flash_id
== FLASH_UNKNOWN
)
124 return ERR_UNKNOWN_FLASH_TYPE
;
126 if ((s_first
< 0) || (s_first
> s_last
)) {
130 if ((info
->flash_id
& FLASH_VENDMASK
) !=
131 (INTEL_MANUFACT
& FLASH_VENDMASK
)) {
132 return ERR_UNKNOWN_FLASH_VENDOR
;
136 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
137 if (info
->protect
[sect
]) {
142 return ERR_PROTECTED
;
145 * Disable interrupts which might cause a timeout
146 * here. Remember that our exception vectors are
147 * at address 0 in the flash, and we don't want a
148 * (ticker) exception to happen while the flash
149 * chip is in programming mode.
151 flag
= disable_interrupts ();
153 /* Start erase on unprotected sectors */
154 for (sect
= s_first
; sect
<= s_last
&& !ctrlc (); sect
++) {
156 printf ("Erasing sector %2d ... ", sect
);
158 /* arm simple, non interrupt dependent timer */
159 reset_timer_masked ();
161 if (info
->protect
[sect
] == 0) { /* not protected */
162 vu_short
*addr
= (vu_short
*) (info
->start
[sect
]);
164 *addr
= 0x20; /* erase setup */
165 *addr
= 0xD0; /* erase confirm */
167 while ((*addr
& 0x80) != 0x80) {
168 if (get_timer_masked () > CONFIG_SYS_FLASH_ERASE_TOUT
) {
169 *addr
= 0xB0; /* suspend erase */
170 *addr
= 0xFF; /* reset to read mode */
176 /* clear status register command */
178 /* reset to read mode */
184 printf ("User Interrupt!\n");
188 /* allow flash to settle - wait 10 ms */
189 udelay_masked (10000);
192 enable_interrupts ();
197 /*-----------------------------------------------------------------------
198 * Copy memory to flash
201 static int write_word (flash_info_t
* info
, ulong dest
, ushort data
)
203 vu_short
*addr
= (vu_short
*) dest
, val
;
207 /* Check if Flash is (sufficiently) erased
209 if ((*addr
& data
) != data
)
210 return ERR_NOT_ERASED
;
213 * Disable interrupts which might cause a timeout
214 * here. Remember that our exception vectors are
215 * at address 0 in the flash, and we don't want a
216 * (ticker) exception to happen while the flash
217 * chip is in programming mode.
219 flag
= disable_interrupts ();
221 /* clear status register command */
224 /* program set-up command */
227 /* latch address/data */
230 /* arm simple, non interrupt dependent timer */
231 reset_timer_masked ();
233 /* wait while polling the status register */
234 while (((val
= *addr
) & 0x80) != 0x80) {
235 if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT
) {
237 /* suspend program command */
243 if (val
& 0x1A) { /* check for error */
244 printf ("\nFlash write error %02x at address %08lx\n",
245 (int) val
, (unsigned long) dest
);
246 if (val
& (1 << 3)) {
247 printf ("Voltage range error.\n");
251 if (val
& (1 << 1)) {
252 printf ("Device protect error.\n");
256 if (val
& (1 << 4)) {
257 printf ("Programming error.\n");
266 /* read array command */
270 enable_interrupts ();
275 /*-----------------------------------------------------------------------
276 * Copy memory to flash.
279 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
286 wp
= (addr
& ~1); /* get lower word aligned address */
289 * handle unaligned start bytes
291 if ((l
= addr
- wp
) != 0) {
293 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
294 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
296 for (; i
< 2 && cnt
> 0; ++i
) {
297 data
= (data
>> 8) | (*src
++ << 8);
301 for (; cnt
== 0 && i
< 2; ++i
, ++cp
) {
302 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
305 if ((rc
= write_word (info
, wp
, data
)) != 0) {
312 * handle word aligned part
315 data
= *((vu_short
*) src
);
316 if ((rc
= write_word (info
, wp
, data
)) != 0) {
329 * handle unaligned tail bytes
332 for (i
= 0, cp
= wp
; i
< 2 && cnt
> 0; ++i
, ++cp
) {
333 data
= (data
>> 8) | (*src
++ << 8);
336 for (; i
< 2; ++i
, ++cp
) {
337 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
340 return write_word (info
, wp
, data
);