]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/idmr/flash.c
ColdFire: Cleanup lds files for multiple defined symbols
[karo-tx-uboot.git] / board / idmr / flash.c
1 /*
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
26 #define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
27 #define FLASH_BANK_SIZE 0x800000
28 #define EN29LV640 0x227e227e
29
30 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
31
32 void 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
73 unsigned long flash_init (void)
74 {
75         int i, j;
76         ulong size = 0;
77
78         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
79                 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;
85                 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
86                 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
87                 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,
99                        CONFIG_SYS_FLASH_BASE,
100                        CONFIG_SYS_FLASH_BASE + 0x2ffff, &flash_info[0]);
101
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
114 #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)))
116
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
127 int 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         ulong start;
134
135         /* first look for protection bits */
136
137         if (info->flash_id == FLASH_UNKNOWN)
138                 return ERR_UNKNOWN_FLASH_TYPE;
139
140         if ((s_first < 0) || (s_first > s_last)) {
141                 return ERR_INVAL;
142         }
143
144         if ((info->flash_id & FLASH_VENDMASK) !=
145             (AMD_MANUFACT & FLASH_VENDMASK)) {
146                 return ERR_UNKNOWN_FLASH_VENDOR;
147         }
148
149         prot = 0;
150         for (sect = s_first; sect <= s_last; ++sect) {
151                 if (info->protect[sect]) {
152                         prot++;
153                 }
154         }
155         if (prot)
156                 return ERR_PROTECTED;
157
158         /*
159          * Disable interrupts which might cause a timeout
160          * here. Remember that our exception vectors are
161          * at address 0 in the flash, and we don't want a
162          * (ticker) exception to happen while the flash
163          * chip is in programming mode.
164          */
165         iflag = disable_interrupts ();
166
167         printf ("\n");
168
169         /* Start erase on unprotected sectors */
170         for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
171                 printf ("Erasing sector %2d ... ", sect);
172
173                 /* arm simple, non interrupt dependent timer */
174                 start = get_timer(0);
175
176                 if (info->protect[sect] == 0) { /* not protected */
177                         volatile u16 *addr =
178                                 (volatile u16 *) (info->start[sect]);
179
180                         MEM_FLASH_ADDR1 = CMD_UNLOCK1;
181                         MEM_FLASH_ADDR2 = CMD_UNLOCK2;
182                         MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
183
184                         MEM_FLASH_ADDR1 = CMD_UNLOCK1;
185                         MEM_FLASH_ADDR2 = CMD_UNLOCK2;
186                         *addr = CMD_ERASE_CONFIRM;
187
188                         /* wait until flash is ready */
189                         chip1 = 0;
190
191                         do {
192                                 result = *addr;
193
194                                 /* check timeout */
195                                 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
196                                         MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
197                                         chip1 = TMO;
198                                         break;
199                                 }
200
201                                 if (!chip1
202                                     && (result & 0xFFFF) & BIT_ERASE_DONE)
203                                         chip1 = READY;
204
205                         } while (!chip1);
206
207                         MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
208
209                         if (chip1 == ERR) {
210                                 rc = ERR_PROG_ERROR;
211                                 goto outahere;
212                         }
213                         if (chip1 == TMO) {
214                                 rc = ERR_TIMOUT;
215                                 goto outahere;
216                         }
217
218                         printf ("ok.\n");
219                 } else {        /* it was protected */
220
221                         printf ("protected!\n");
222                 }
223         }
224
225         if (ctrlc ())
226                 printf ("User Interrupt!\n");
227
228       outahere:
229         /* allow flash to settle - wait 10 ms */
230         printf("Waiting 10 ms...");
231          udelay (10000);
232
233 /*      for (i = 0; i < 10 * 1000 * 1000; ++i)
234                 asm(" nop");
235 */
236
237         printf("done\n");
238         if (iflag)
239                 enable_interrupts ();
240
241
242         return rc;
243 }
244
245 static int write_word (flash_info_t * info, ulong dest, ulong data)
246 {
247         volatile u16 *addr = (volatile u16 *) dest;
248         ulong result;
249         int rc = ERR_OK;
250         int iflag;
251         int chip1;
252         ulong start;
253
254         /*
255          * Check if Flash is (sufficiently) erased
256          */
257         result = *addr;
258         if ((result & data) != data)
259                 return ERR_NOT_ERASED;
260
261
262         /*
263          * Disable interrupts which might cause a timeout
264          * here. Remember that our exception vectors are
265          * at address 0 in the flash, and we don't want a
266          * (ticker) exception to happen while the flash
267          * chip is in programming mode.
268          */
269         iflag = disable_interrupts ();
270
271         MEM_FLASH_ADDR1 = CMD_UNLOCK1;
272         MEM_FLASH_ADDR2 = CMD_UNLOCK2;
273         MEM_FLASH_ADDR1 = CMD_PROGRAM;
274         *addr = data;
275
276         /* arm simple, non interrupt dependent timer */
277         start = get_timer(0);
278
279         /* wait until flash is ready */
280         chip1 = 0;
281         do {
282                 result = *addr;
283
284                 /* check timeout */
285                 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT * CONFIG_SYS_HZ / 1000) {
286                         chip1 = ERR | TMO;
287                         break;
288                 }
289                 if (!chip1 && ((result & 0x80) == (data & 0x80)))
290                         chip1 = READY;
291
292         } while (!chip1);
293
294         *addr = CMD_READ_ARRAY;
295
296         if (chip1 == ERR || *addr != data)
297                 rc = ERR_PROG_ERROR;
298
299         if (iflag)
300                 enable_interrupts ();
301
302         return rc;
303 }
304
305
306 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
307 {
308         ulong wp, data;
309         int rc;
310
311         if (addr & 1) {
312                 printf ("unaligned destination not supported\n");
313                 return ERR_ALIGN;
314         }
315
316 #if 0
317         if (cnt & 1) {
318                 printf ("odd transfer sizes not supported\n");
319                 return ERR_ALIGN;
320         }
321 #endif
322
323         wp = addr;
324
325         if (addr & 1) {
326                 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
327                                                           src);
328                 if ((rc = write_word (info, wp - 1, data)) != 0) {
329                         return (rc);
330                 }
331                 src += 1;
332                 wp += 1;
333                 cnt -= 1;
334         }
335
336         while (cnt >= 2) {
337                 data = *((volatile u16 *) src);
338                 if ((rc = write_word (info, wp, data)) != 0) {
339                         return (rc);
340                 }
341                 src += 2;
342                 wp += 2;
343                 cnt -= 2;
344         }
345
346         if (cnt == 1) {
347                 data = (*((volatile u8 *) src) << 8) |
348                         *((volatile u8 *) (wp + 1));
349                 if ((rc = write_word (info, wp, data)) != 0) {
350                         return (rc);
351                 }
352                 src += 1;
353                 wp += 1;
354                 cnt -= 1;
355         }
356
357         return ERR_OK;
358 }