boot-from-ram-reloc.patch
[u-boot-openmoko/mini2440.git] / board / idmr / flash.c
blob33512b8946df35dd9d110c732f26947a5cfb1285
1 /*
2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
24 #include <common.h>
26 #define PHYS_FLASH_1 CFG_FLASH_BASE
27 #define FLASH_BANK_SIZE 0x800000
28 #define EN29LV640 0x227e227e
30 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
32 void flash_print_info (flash_info_t * info)
34 int i;
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;
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;
55 printf (" Size: %ld MB in %d Sectors\n",
56 info->size >> 20, info->sector_count);
58 printf (" Sector Start Addresses:");
59 for (i = 0; i < info->sector_count; i++) {
60 if ((i % 5) == 0) {
61 printf ("\n ");
63 printf (" %08lX%s", info->start[i],
64 info->protect[i] ? " (RO)" : " ");
66 printf ("\n");
68 Done:
69 return;
73 unsigned long flash_init (void)
75 int i, j;
76 ulong size = 0;
78 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
79 ulong flashbase = 0;
81 flash_info[i].flash_id =
82 (AMD_MANUFACT & FLASH_VENDMASK) |
83 (EN29LV640 & FLASH_TYPEMASK);
84 flash_info[i].size = FLASH_BANK_SIZE;
85 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
86 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
87 if (i == 0)
88 flashbase = PHYS_FLASH_1;
89 else
90 panic ("configured to many flash banks!\n");
92 for (j = 0; j < flash_info[i].sector_count; j++) {
93 flash_info[i].start[j] = flashbase + 0x10000 * j;
95 size += flash_info[i].size;
98 flash_protect (FLAG_PROTECT_SET,
99 CFG_FLASH_BASE,
100 CFG_FLASH_BASE + 0x2ffff, &flash_info[0]);
102 return size;
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
114 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555<<1)))
115 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA<<1)))
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 */
122 #define READY 1
123 #define ERR 2
124 #define TMO 4
127 int flash_erase (flash_info_t * info, int s_first, int s_last)
129 ulong result;
130 int iflag, prot, sect;
131 int rc = ERR_OK;
132 int chip1;
134 /* first look for protection bits */
136 if (info->flash_id == FLASH_UNKNOWN)
137 return ERR_UNKNOWN_FLASH_TYPE;
139 if ((s_first < 0) || (s_first > s_last)) {
140 return ERR_INVAL;
143 if ((info->flash_id & FLASH_VENDMASK) !=
144 (AMD_MANUFACT & FLASH_VENDMASK)) {
145 return ERR_UNKNOWN_FLASH_VENDOR;
148 prot = 0;
149 for (sect = s_first; sect <= s_last; ++sect) {
150 if (info->protect[sect]) {
151 prot++;
154 if (prot)
155 return ERR_PROTECTED;
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.
164 iflag = disable_interrupts ();
166 printf ("\n");
168 /* Start erase on unprotected sectors */
169 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
170 printf ("Erasing sector %2d ... ", sect);
172 /* arm simple, non interrupt dependent timer */
173 set_timer (0);
175 if (info->protect[sect] == 0) { /* not protected */
176 volatile u16 *addr =
177 (volatile u16 *) (info->start[sect]);
179 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
180 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
181 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
183 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
184 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
185 *addr = CMD_ERASE_CONFIRM;
187 /* wait until flash is ready */
188 chip1 = 0;
190 do {
191 result = *addr;
193 /* check timeout */
194 if (get_timer (0) > CFG_FLASH_ERASE_TOUT * CFG_HZ / 1000) {
195 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
196 chip1 = TMO;
197 break;
200 if (!chip1
201 && (result & 0xFFFF) & BIT_ERASE_DONE)
202 chip1 = READY;
204 } while (!chip1);
206 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
208 if (chip1 == ERR) {
209 rc = ERR_PROG_ERROR;
210 goto outahere;
212 if (chip1 == TMO) {
213 rc = ERR_TIMOUT;
214 goto outahere;
217 printf ("ok.\n");
218 } else { /* it was protected */
220 printf ("protected!\n");
224 if (ctrlc ())
225 printf ("User Interrupt!\n");
227 outahere:
228 /* allow flash to settle - wait 10 ms */
229 printf("Waiting 10 ms...");
230 udelay (10000);
232 /* for (i = 0; i < 10 * 1000 * 1000; ++i)
233 asm(" nop");
236 printf("done\n");
237 if (iflag)
238 enable_interrupts ();
241 return rc;
244 static int write_word (flash_info_t * info, ulong dest, ulong data)
246 volatile u16 *addr = (volatile u16 *) dest;
247 ulong result;
248 int rc = ERR_OK;
249 int iflag;
250 int chip1;
253 * Check if Flash is (sufficiently) erased
255 result = *addr;
256 if ((result & data) != data)
257 return ERR_NOT_ERASED;
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.
267 iflag = disable_interrupts ();
269 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
270 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
271 MEM_FLASH_ADDR1 = CMD_PROGRAM;
272 *addr = data;
274 /* arm simple, non interrupt dependent timer */
275 set_timer (0);
277 /* wait until flash is ready */
278 chip1 = 0;
279 do {
280 result = *addr;
282 /* check timeout */
283 if (get_timer (0) > CFG_FLASH_ERASE_TOUT * CFG_HZ / 1000) {
284 chip1 = ERR | TMO;
285 break;
287 if (!chip1 && ((result & 0x80) == (data & 0x80)))
288 chip1 = READY;
290 } while (!chip1);
292 *addr = CMD_READ_ARRAY;
294 if (chip1 == ERR || *addr != data)
295 rc = ERR_PROG_ERROR;
297 if (iflag)
298 enable_interrupts ();
300 return rc;
304 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
306 ulong wp, data;
307 int rc;
309 if (addr & 1) {
310 printf ("unaligned destination not supported\n");
311 return ERR_ALIGN;
314 #if 0
315 if (cnt & 1) {
316 printf ("odd transfer sizes not supported\n");
317 return ERR_ALIGN;
319 #endif
321 wp = addr;
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);
329 src += 1;
330 wp += 1;
331 cnt -= 1;
334 while (cnt >= 2) {
335 data = *((volatile u16 *) src);
336 if ((rc = write_word (info, wp, data)) != 0) {
337 return (rc);
339 src += 2;
340 wp += 2;
341 cnt -= 2;
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);
350 src += 1;
351 wp += 1;
352 cnt -= 1;
355 return ERR_OK;