blob: 3e380e5f498afb96d277aa6e967e4ba157d11826 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
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.
13 *
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.
18 *
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,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26
27#define FLASH_BANK_SIZE 0x800000
28#define MAIN_SECT_SIZE 0x20000
29#define PARAM_SECT_SIZE 0x4000
30
31flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
32
33
34/*-----------------------------------------------------------------------
35 */
36
37ulong flash_init(void)
38{
39 int i, j;
40 ulong size = 0;
41
42 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
43 {
44 ulong flashbase = 0;
45 flash_info[i].flash_id =
46 (INTEL_MANUFACT & FLASH_VENDMASK) |
47 (INTEL_ID_28F320B3T & FLASH_TYPEMASK);
48 flash_info[i].size = FLASH_BANK_SIZE;
49 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
50 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
51 if (i == 0)
52 flashbase = PHYS_FLASH_1;
53 else if (i == 1)
54 flashbase = PHYS_FLASH_2;
55 else
wdenk5f535fe2003-09-18 09:21:33 +000056 panic("configured too many flash banks!\n");
wdenkc6097192002-11-03 00:24:07 +000057 for (j = 0; j < flash_info[i].sector_count; j++)
58 {
59 if (j <= 7)
60 {
61 flash_info[i].start[j] = flashbase + j * PARAM_SECT_SIZE;
62 }
63 else
64 {
65 flash_info[i].start[j] = flashbase + (j - 7)*MAIN_SECT_SIZE;
66 }
67 }
68 size += flash_info[i].size;
69 }
70
71 /* Protect monitor and environment sectors
72 */
73 flash_protect(FLAG_PROTECT_SET,
74 CFG_FLASH_BASE,
wdenk3b57fe02003-05-30 12:48:29 +000075 CFG_FLASH_BASE + monitor_flash_len - 1,
wdenkc6097192002-11-03 00:24:07 +000076 &flash_info[0]);
77
78 flash_protect(FLAG_PROTECT_SET,
79 CFG_ENV_ADDR,
80 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
81 &flash_info[0]);
82
83 return size;
84}
85
86/*-----------------------------------------------------------------------
87 */
88void flash_print_info (flash_info_t *info)
89{
90 int i;
91
92 switch (info->flash_id & FLASH_VENDMASK)
93 {
94 case (INTEL_MANUFACT & FLASH_VENDMASK):
95 printf("Intel: ");
96 break;
97 default:
98 printf("Unknown Vendor ");
99 break;
100 }
101
102 switch (info->flash_id & FLASH_TYPEMASK)
103 {
104 case (INTEL_ID_28F320B3T & FLASH_TYPEMASK):
105 printf("28F320F3B (16Mbit)\n");
106 break;
107 default:
108 printf("Unknown Chip Type\n");
109 goto Done;
110 break;
111 }
112
113 printf(" Size: %ld MB in %d Sectors\n",
114 info->size >> 20, info->sector_count);
115
116 printf(" Sector Start Addresses:");
117 for (i = 0; i < info->sector_count; i++)
118 {
119 if ((i % 5) == 0)
120 {
121 printf ("\n ");
122 }
123 printf (" %08lX%s", info->start[i],
124 info->protect[i] ? " (RO)" : " ");
125 }
126 printf ("\n");
127
128Done:
129}
130
131/*-----------------------------------------------------------------------
132 */
133
134int flash_erase (flash_info_t *info, int s_first, int s_last)
135{
136 int flag, prot, sect;
137 int rc = ERR_OK;
138
139 if (info->flash_id == FLASH_UNKNOWN)
140 return ERR_UNKNOWN_FLASH_TYPE;
141
142 if ((s_first < 0) || (s_first > s_last)) {
143 return ERR_INVAL;
144 }
145
146 if ((info->flash_id & FLASH_VENDMASK) !=
147 (INTEL_MANUFACT & FLASH_VENDMASK)) {
148 return ERR_UNKNOWN_FLASH_VENDOR;
149 }
150
151 prot = 0;
152 for (sect=s_first; sect<=s_last; ++sect) {
153 if (info->protect[sect]) {
154 prot++;
155 }
156 }
157 if (prot)
158 return ERR_PROTECTED;
159
160 /*
161 * Disable interrupts which might cause a timeout
162 * here. Remember that our exception vectors are
163 * at address 0 in the flash, and we don't want a
164 * (ticker) exception to happen while the flash
165 * chip is in programming mode.
166 */
167 flag = disable_interrupts();
168
169 /* Start erase on unprotected sectors */
170 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
171
172 printf("Erasing sector %2d ... ", sect);
173
174 /* arm simple, non interrupt dependent timer */
175 reset_timer_masked();
176
177 if (info->protect[sect] == 0) { /* not protected */
178 vu_long *addr = (vu_long *)(info->start[sect]);
179
180 *addr = 0x00200020; /* erase setup */
181 *addr = 0x00D000D0; /* erase confirm */
182
183 while ((*addr & 0x00800080) != 0x00800080) {
184 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
185 *addr = 0x00B000B0; /* suspend erase */
186 *addr = 0x00FF00FF; /* reset to read mode */
187 rc = ERR_TIMOUT;
188 goto outahere;
189 }
190 }
191
192 *addr = 0x00FF00FF; /* reset to read mode */
193 }
194 printf("ok.\n");
195 }
196 if (ctrlc())
197 printf("User Interrupt!\n");
198
199outahere:
200
201 /* allow flash to settle - wait 10 ms */
202 udelay_masked(10000);
203
204 if (flag)
205 enable_interrupts();
206
207 return rc;
208}
209
210/*-----------------------------------------------------------------------
211 * Copy memory to flash
212 */
213
214static int write_word (flash_info_t *info, ulong dest, ulong data)
215{
216 vu_long *addr = (vu_long *)dest;
217 ulong barf;
218 int rc = ERR_OK;
219 int flag;
220
221 /* Check if Flash is (sufficiently) erased
222 */
223 if ((*addr & data) != data)
wdenk8bde7f72003-06-27 21:31:46 +0000224 return ERR_NOT_ERASED;
wdenkc6097192002-11-03 00:24:07 +0000225
226 /*
227 * Disable interrupts which might cause a timeout
228 * here. Remember that our exception vectors are
229 * at address 0 in the flash, and we don't want a
230 * (ticker) exception to happen while the flash
231 * chip is in programming mode.
232 */
233 flag = disable_interrupts();
234
235 /* clear status register command */
236 *addr = 0x00500050;
237
238 /* program set-up command */
239 *addr = 0x00400040;
240
241 /* latch address/data */
242 *addr = data;
243
244 /* arm simple, non interrupt dependent timer */
245 reset_timer_masked();
246
247 /* read status register command */
248 *addr = 0x00700070;
249
250 /* wait while polling the status register */
251 while((*addr & 0x00800080) != 0x00800080)
252 {
253 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
254 rc = ERR_TIMOUT;
255 /* suspend program command */
256 *addr = 0x00B000B0;
257 goto outahere;
258 }
259
260 if( *addr & 0x003A003A) { /* check for error */
261 barf = *addr;
262 if( barf & 0x003A0000) {
263 barf >>=16;
264 } else {
265 barf &= 0x0000003A;
266 }
267 printf("\nFlash write error %02lx at address %08lx\n",
268 barf, (unsigned long)dest);
269 if(barf & 0x0002) {
270 printf("Block locked, not erased.\n");
271 rc = ERR_NOT_ERASED;
272 goto outahere;
273 }
274 if(barf & 0x0010) {
275 printf("Programming error.\n");
276 rc = ERR_PROG_ERROR;
277 goto outahere;
278 }
279 if(barf & 0x0008) {
280 printf("Vpp Low error.\n");
281 rc = ERR_PROG_ERROR;
282 goto outahere;
283 }
284 rc = ERR_PROG_ERROR;
285 goto outahere;
286 }
287 }
288
289
290outahere:
291 /* read array command */
292 *addr = 0x00FF00FF;
293
294 if (flag)
295 enable_interrupts();
296
297 return rc;
298}
299
300/*-----------------------------------------------------------------------
301 * Copy memory to flash.
302 */
303
304int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
305{
306 ulong cp, wp, data;
307 int l;
308 int i, rc;
309
310 wp = (addr & ~3); /* get lower word aligned address */
311
312 /*
313 * handle unaligned start bytes
314 */
315 if ((l = addr - wp) != 0) {
316 data = 0;
317 for (i=0, cp=wp; i<l; ++i, ++cp) {
318 data = (data >> 8) | (*(uchar *)cp << 24);
319 }
320 for (; i<4 && cnt>0; ++i) {
321 data = (data >> 8) | (*src++ << 24);
322 --cnt;
323 ++cp;
324 }
325 for (; cnt==0 && i<4; ++i, ++cp) {
326 data = (data >> 8) | (*(uchar *)cp << 24);
327 }
328
329 if ((rc = write_word(info, wp, data)) != 0) {
330 return (rc);
331 }
332 wp += 4;
333 }
334
335 /*
336 * handle word aligned part
337 */
338 while (cnt >= 4) {
339 data = *((vu_long*)src);
340 if ((rc = write_word(info, wp, data)) != 0) {
341 return (rc);
342 }
343 src += 4;
344 wp += 4;
345 cnt -= 4;
346 }
347
348 if (cnt == 0) {
349 return ERR_OK;
350 }
351
352 /*
353 * handle unaligned tail bytes
354 */
355 data = 0;
356 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
357 data = (data >> 8) | (*src++ << 24);
358 --cnt;
359 }
360 for (; i<4; ++i, ++cp) {
361 data = (data >> 8) | (*(uchar *)cp << 24);
362 }
363
364 return write_word(info, wp, data);
365}