blob: 57c994863765b2d1fb63389bc58711204c80fe5c [file] [log] [blame]
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +01001/*
2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026#define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +010027#define FLASH_BANK_SIZE 0x800000
28#define EN29LV640 0x227e227e
29
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +010031
32void flash_print_info (flash_info_t * info)
33{
34 int i;
35
36 switch (info->flash_id & FLASH_VENDMASK) {
37 case (AMD_MANUFACT & FLASH_VENDMASK):
38 printf ("AMD: ");
39 break;
40 default:
41 printf ("Unknown Vendor ");
42 break;
43 }
44
45 switch (info->flash_id & FLASH_TYPEMASK) {
46 case (EN29LV640 & FLASH_TYPEMASK):
47 printf ("EN29LV640 (16Mbit)\n");
48 break;
49 default:
50 printf ("Unknown Chip Type\n");
51 goto Done;
52 break;
53 }
54
55 printf (" Size: %ld MB in %d Sectors\n",
56 info->size >> 20, info->sector_count);
57
58 printf (" Sector Start Addresses:");
59 for (i = 0; i < info->sector_count; i++) {
60 if ((i % 5) == 0) {
61 printf ("\n ");
62 }
63 printf (" %08lX%s", info->start[i],
64 info->protect[i] ? " (RO)" : " ");
65 }
66 printf ("\n");
67
68 Done:
69 return;
70}
71
72
73unsigned long flash_init (void)
74{
75 int i, j;
76 ulong size = 0;
77
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +010079 ulong flashbase = 0;
80
81 flash_info[i].flash_id =
82 (AMD_MANUFACT & FLASH_VENDMASK) |
83 (EN29LV640 & FLASH_TYPEMASK);
84 flash_info[i].size = FLASH_BANK_SIZE;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020085 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
86 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +010087 if (i == 0)
88 flashbase = PHYS_FLASH_1;
89 else
90 panic ("configured to many flash banks!\n");
91
92 for (j = 0; j < flash_info[i].sector_count; j++) {
93 flash_info[i].start[j] = flashbase + 0x10000 * j;
94 }
95 size += flash_info[i].size;
96 }
97
98 flash_protect (FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099 CONFIG_SYS_FLASH_BASE,
100 CONFIG_SYS_FLASH_BASE + 0x2ffff, &flash_info[0]);
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +0100101
102 return size;
103}
104
105
106#define CMD_READ_ARRAY 0x00F0
107#define CMD_UNLOCK1 0x00AA
108#define CMD_UNLOCK2 0x0055
109#define CMD_ERASE_SETUP 0x0080
110#define CMD_ERASE_CONFIRM 0x0030
111#define CMD_PROGRAM 0x00A0
112#define CMD_UNLOCK_BYPASS 0x0020
113
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200114#define MEM_FLASH_ADDR1 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x00000555<<1)))
115#define MEM_FLASH_ADDR2 (*(volatile u16 *)(CONFIG_SYS_FLASH_BASE + (0x000002AA<<1)))
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +0100116
117#define BIT_ERASE_DONE 0x0080
118#define BIT_RDY_MASK 0x0080
119#define BIT_PROGRAM_ERROR 0x0020
120#define BIT_TIMEOUT 0x80000000 /* our flag */
121
122#define READY 1
123#define ERR 2
124#define TMO 4
125
126
127int flash_erase (flash_info_t * info, int s_first, int s_last)
128{
129 ulong result;
130 int iflag, prot, sect;
131 int rc = ERR_OK;
132 int chip1;
133
134 /* first look for protection bits */
135
136 if (info->flash_id == FLASH_UNKNOWN)
137 return ERR_UNKNOWN_FLASH_TYPE;
138
139 if ((s_first < 0) || (s_first > s_last)) {
140 return ERR_INVAL;
141 }
142
143 if ((info->flash_id & FLASH_VENDMASK) !=
144 (AMD_MANUFACT & FLASH_VENDMASK)) {
145 return ERR_UNKNOWN_FLASH_VENDOR;
146 }
147
148 prot = 0;
149 for (sect = s_first; sect <= s_last; ++sect) {
150 if (info->protect[sect]) {
151 prot++;
152 }
153 }
154 if (prot)
155 return ERR_PROTECTED;
156
157 /*
158 * Disable interrupts which might cause a timeout
159 * here. Remember that our exception vectors are
160 * at address 0 in the flash, and we don't want a
161 * (ticker) exception to happen while the flash
162 * chip is in programming mode.
163 */
164 iflag = disable_interrupts ();
165
166 printf ("\n");
167
168 /* Start erase on unprotected sectors */
169 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
170 printf ("Erasing sector %2d ... ", sect);
171
172 /* arm simple, non interrupt dependent timer */
173 set_timer (0);
174
175 if (info->protect[sect] == 0) { /* not protected */
176 volatile u16 *addr =
177 (volatile u16 *) (info->start[sect]);
178
179 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
180 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
181 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
182
183 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
184 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
185 *addr = CMD_ERASE_CONFIRM;
186
187 /* wait until flash is ready */
188 chip1 = 0;
189
190 do {
191 result = *addr;
192
193 /* check timeout */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200194 if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +0100195 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
196 chip1 = TMO;
197 break;
198 }
199
200 if (!chip1
201 && (result & 0xFFFF) & BIT_ERASE_DONE)
202 chip1 = READY;
203
204 } while (!chip1);
205
206 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
207
208 if (chip1 == ERR) {
209 rc = ERR_PROG_ERROR;
210 goto outahere;
211 }
212 if (chip1 == TMO) {
213 rc = ERR_TIMOUT;
214 goto outahere;
215 }
216
217 printf ("ok.\n");
218 } else { /* it was protected */
219
220 printf ("protected!\n");
221 }
222 }
223
224 if (ctrlc ())
225 printf ("User Interrupt!\n");
226
227 outahere:
228 /* allow flash to settle - wait 10 ms */
229 printf("Waiting 10 ms...");
230 udelay (10000);
231
232/* for (i = 0; i < 10 * 1000 * 1000; ++i)
233 asm(" nop");
234*/
235
236 printf("done\n");
237 if (iflag)
238 enable_interrupts ();
239
240
241 return rc;
242}
243
244static int write_word (flash_info_t * info, ulong dest, ulong data)
245{
246 volatile u16 *addr = (volatile u16 *) dest;
247 ulong result;
248 int rc = ERR_OK;
249 int iflag;
250 int chip1;
251
252 /*
253 * Check if Flash is (sufficiently) erased
254 */
255 result = *addr;
256 if ((result & data) != data)
257 return ERR_NOT_ERASED;
258
259
260 /*
261 * Disable interrupts which might cause a timeout
262 * here. Remember that our exception vectors are
263 * at address 0 in the flash, and we don't want a
264 * (ticker) exception to happen while the flash
265 * chip is in programming mode.
266 */
267 iflag = disable_interrupts ();
268
269 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
270 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
271 MEM_FLASH_ADDR1 = CMD_PROGRAM;
272 *addr = data;
273
274 /* arm simple, non interrupt dependent timer */
275 set_timer (0);
276
277 /* wait until flash is ready */
278 chip1 = 0;
279 do {
280 result = *addr;
281
282 /* check timeout */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200283 if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
Bartlomiej Siekadaa6e412006-12-20 00:27:32 +0100284 chip1 = ERR | TMO;
285 break;
286 }
287 if (!chip1 && ((result & 0x80) == (data & 0x80)))
288 chip1 = READY;
289
290 } while (!chip1);
291
292 *addr = CMD_READ_ARRAY;
293
294 if (chip1 == ERR || *addr != data)
295 rc = ERR_PROG_ERROR;
296
297 if (iflag)
298 enable_interrupts ();
299
300 return rc;
301}
302
303
304int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
305{
306 ulong wp, data;
307 int rc;
308
309 if (addr & 1) {
310 printf ("unaligned destination not supported\n");
311 return ERR_ALIGN;
312 }
313
314#if 0
315 if (cnt & 1) {
316 printf ("odd transfer sizes not supported\n");
317 return ERR_ALIGN;
318 }
319#endif
320
321 wp = addr;
322
323 if (addr & 1) {
324 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
325 src);
326 if ((rc = write_word (info, wp - 1, data)) != 0) {
327 return (rc);
328 }
329 src += 1;
330 wp += 1;
331 cnt -= 1;
332 }
333
334 while (cnt >= 2) {
335 data = *((volatile u16 *) src);
336 if ((rc = write_word (info, wp, data)) != 0) {
337 return (rc);
338 }
339 src += 2;
340 wp += 2;
341 cnt -= 2;
342 }
343
344 if (cnt == 1) {
345 data = (*((volatile u8 *) src) << 8) |
346 *((volatile u8 *) (wp + 1));
347 if ((rc = write_word (info, wp, data)) != 0) {
348 return (rc);
349 }
350 src += 1;
351 wp += 1;
352 cnt -= 1;
353 }
354
355 return ERR_OK;
356}