]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/trab/flash.c
Initial revision
[karo-tx-uboot.git] / board / trab / flash.c
1 /*
2  * (C) Copyright 2002
3  * Gary Jennejohn, DENX Software Engineering, <gj@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 /* #define DEBUG */
25
26 #include <common.h>
27 #include <environment.h>
28
29 ulong myflush (void);
30
31
32 #define FLASH_BANK_SIZE 0x800000        /* 8 MB */
33 /* this varies depending on the sector */
34 #define MAIN_SECT_SIZE  0x20000         /* 2 x 64 kB */
35
36 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
37
38
39 #define CMD_READ_ARRAY          0x00F000F0
40 #define CMD_UNLOCK1             0x00AA00AA
41 #define CMD_UNLOCK2             0x00550055
42 #define CMD_ERASE_SETUP         0x00800080
43 #define CMD_ERASE_CONFIRM       0x00300030
44 #define CMD_PROGRAM             0x00A000A0
45 #define CMD_UNLOCK_BYPASS       0x00200020
46
47 #define MEM_FLASH_ADDR1         (*(volatile u32 *)(CFG_FLASH_BASE + (0x00000555 << 2)))
48 #define MEM_FLASH_ADDR2         (*(volatile u32 *)(CFG_FLASH_BASE + (0x000002AA << 2)))
49
50 #define BIT_ERASE_DONE          0x00800080
51 #define BIT_RDY_MASK            0x00800080
52 #define BIT_PROGRAM_ERROR       0x00200020
53 #define BIT_TIMEOUT             0x80000000      /* our flag */
54
55 #define READY 1
56 #define ERR   2
57 #define TMO   4
58
59 /*-----------------------------------------------------------------------
60  */
61
62 ulong flash_init (void)
63 {
64         int i, j;
65         ulong size = 0;
66
67         for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
68                 ulong flashbase = 0;
69
70                 flash_info[i].flash_id =
71                         (AMD_MANUFACT  & FLASH_VENDMASK) |
72                         (AMD_ID_LV320B & FLASH_TYPEMASK);
73                 flash_info[i].size = FLASH_BANK_SIZE;
74                 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
75                 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
76                 if (i == 0)
77                         flashbase = PHYS_FLASH_1;
78                 else
79                         panic ("configured too many flash banks!\n");
80                 for (j = 0; j < flash_info[i].sector_count; j++) {
81
82                         flash_info[i].start[j] = flashbase;
83
84                         /* the first 8 sectors are 8 kB */
85                         flashbase += (j < 8) ? 0x4000 : MAIN_SECT_SIZE;
86                 }
87                 size += flash_info[i].size;
88         }
89
90         /*
91          * Protect monitor and environment sectors
92          */
93         flash_protect ( FLAG_PROTECT_SET,
94                         CFG_FLASH_BASE,
95                         CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
96                         &flash_info[0]);
97
98         flash_protect ( FLAG_PROTECT_SET,
99                         CFG_ENV_ADDR,
100                         CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
101
102 #ifdef CFG_ENV_ADDR_REDUND
103         flash_protect ( FLAG_PROTECT_SET,
104                         CFG_ENV_ADDR_REDUND,
105                         CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
106                         &flash_info[0]);
107 #endif
108
109         return size;
110 }
111
112 /*-----------------------------------------------------------------------
113  */
114 void flash_print_info (flash_info_t * info)
115 {
116         int i;
117
118         switch (info->flash_id & FLASH_VENDMASK) {
119         case (AMD_MANUFACT & FLASH_VENDMASK):
120                 printf ("AMD: ");
121                 break;
122         default:
123                 printf ("Unknown Vendor ");
124                 break;
125         }
126
127         switch (info->flash_id & FLASH_TYPEMASK) {
128         case (AMD_ID_LV320B & FLASH_TYPEMASK):
129                 printf ("2x Am29LV320DB (32Mbit)\n");
130                 break;
131         default:
132                 printf ("Unknown Chip Type\n");
133                 goto Done;
134                 break;
135         }
136
137         printf ("  Size: %ld MB in %d Sectors\n",
138                         info->size >> 20, info->sector_count);
139
140         printf ("  Sector Start Addresses:");
141         for (i = 0; i < info->sector_count; i++) {
142                 if ((i % 5) == 0) {
143                         printf ("\n   ");
144                 }
145                 printf (" %08lX%s",
146                         info->start[i],
147                         info->protect[i] ? " (RO)" : "     ");
148         }
149         printf ("\n");
150
151   Done:
152 }
153
154 /*-----------------------------------------------------------------------
155  */
156
157 int flash_erase (flash_info_t * info, int s_first, int s_last)
158 {
159         ulong result;
160
161 #if 0
162         int cflag;
163 #endif
164         int iflag, prot, sect;
165         int rc = ERR_OK;
166         int chip1, chip2;
167
168         debug ("flash_erase: s_first %d  s_last %d\n", s_first, s_last);
169
170         /* first look for protection bits */
171
172         if (info->flash_id == FLASH_UNKNOWN)
173                 return ERR_UNKNOWN_FLASH_TYPE;
174
175         if ((s_first < 0) || (s_first > s_last)) {
176                 return ERR_INVAL;
177         }
178
179         if ((info->flash_id & FLASH_VENDMASK) !=
180                 (AMD_MANUFACT & FLASH_VENDMASK)) {
181                 return ERR_UNKNOWN_FLASH_VENDOR;
182         }
183
184         prot = 0;
185         for (sect = s_first; sect <= s_last; ++sect) {
186                 if (info->protect[sect]) {
187                         prot++;
188                 }
189         }
190
191         if (prot) {
192                 printf ("- Warning: %d protected sectors will not be erased!\n",
193                         prot);
194         } else {
195                 printf ("\n");
196         }
197
198         /*
199          * Disable interrupts which might cause a timeout
200          * here. Remember that our exception vectors are
201          * at address 0 in the flash, and we don't want a
202          * (ticker) exception to happen while the flash
203          * chip is in programming mode.
204          */
205 #if 0
206         cflag = icache_status ();
207         icache_disable ();
208 #endif
209         iflag = disable_interrupts ();
210
211         /* Start erase on unprotected sectors */
212         for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
213
214                 debug ("Erasing sector %2d @ %08lX... ",
215                         sect, info->start[sect]);
216
217                 /* arm simple, non interrupt dependent timer */
218                 reset_timer_masked ();
219
220                 if (info->protect[sect] == 0) { /* not protected */
221                         vu_long *addr = (vu_long *) (info->start[sect]);
222
223                         MEM_FLASH_ADDR1 = CMD_UNLOCK1;
224                         MEM_FLASH_ADDR2 = CMD_UNLOCK2;
225                         MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
226
227                         MEM_FLASH_ADDR1 = CMD_UNLOCK1;
228                         MEM_FLASH_ADDR2 = CMD_UNLOCK2;
229                         *addr = CMD_ERASE_CONFIRM;
230
231                         /* wait until flash is ready */
232                         chip1 = chip2 = 0;
233
234                         do {
235                                 result = *addr;
236
237                                 /* check timeout */
238                                 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
239                                         MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
240                                         chip1 = TMO;
241                                         break;
242                                 }
243
244                                 if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
245                                         chip1 = READY;
246
247                                 if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
248                                         chip1 = ERR;
249
250                                 if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
251                                         chip2 = READY;
252
253                                 if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
254                                         chip2 = ERR;
255
256                         } while (!chip1 || !chip2);
257
258                         MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
259
260                         if (chip1 == ERR || chip2 == ERR) {
261                                 rc = ERR_PROG_ERROR;
262                                 goto outahere;
263                         }
264                         if (chip1 == TMO) {
265                                 rc = ERR_TIMOUT;
266                                 goto outahere;
267                         }
268
269 #if 0
270                         printf ("ok.\n");
271                 } else {                /* it was protected */
272
273                         printf ("protected!\n");
274 #endif
275                 }
276         }
277
278 outahere:
279         /* allow flash to settle - wait 10 ms */
280         udelay_masked (10000);
281
282         if (iflag)
283                 enable_interrupts ();
284
285 #if 0
286         if (cflag)
287                 icache_enable ();
288 #endif
289         return rc;
290 }
291
292 /*-----------------------------------------------------------------------
293  * Copy memory to flash
294  */
295
296 volatile static int write_word (flash_info_t * info, ulong dest,
297                                                                 ulong data)
298 {
299         vu_long *addr = (vu_long *) dest;
300         ulong result;
301         int rc = ERR_OK;
302
303 #if 0
304         int cflag;
305 #endif
306         int iflag;
307         int chip1, chip2;
308
309         /*
310          * Check if Flash is (sufficiently) erased
311          */
312         result = *addr;
313         if ((result & data) != data)
314                 return ERR_NOT_ERASED;
315
316         /*
317          * Disable interrupts which might cause a timeout
318          * here. Remember that our exception vectors are
319          * at address 0 in the flash, and we don't want a
320          * (ticker) exception to happen while the flash
321          * chip is in programming mode.
322          */
323 #if 0
324         cflag = icache_status ();
325         icache_disable ();
326 #endif
327         iflag = disable_interrupts ();
328
329         MEM_FLASH_ADDR1 = CMD_UNLOCK1;
330         MEM_FLASH_ADDR2 = CMD_UNLOCK2;
331         MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
332         *addr = CMD_PROGRAM;
333         *addr = data;
334
335         /* arm simple, non interrupt dependent timer */
336         reset_timer_masked ();
337
338         /* wait until flash is ready */
339         chip1 = chip2 = 0;
340         do {
341                 result = *addr;
342
343                 /* check timeout */
344                 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
345                         chip1 = ERR | TMO;
346                         break;
347                 }
348                 if (!chip1 && ((result & 0x80) == (data & 0x80)))
349                         chip1 = READY;
350
351                 if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
352                         result = *addr;
353
354                         if ((result & 0x80) == (data & 0x80))
355                                 chip1 = READY;
356                         else
357                                 chip1 = ERR;
358                 }
359
360                 if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
361                         chip2 = READY;
362
363                 if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR)) {
364                         result = *addr;
365
366                         if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
367                                 chip2 = READY;
368                         else
369                                 chip2 = ERR;
370                 }
371
372         } while (!chip1 || !chip2);
373
374         *addr = CMD_READ_ARRAY;
375
376         if (chip1 == ERR || chip2 == ERR || *addr != data)
377                 rc = ERR_PROG_ERROR;
378
379         if (iflag)
380                 enable_interrupts ();
381
382 #if 0
383         if (cflag)
384                 icache_enable ();
385 #endif
386
387         return rc;
388 }
389
390 /*-----------------------------------------------------------------------
391  * Copy memory to flash.
392  */
393
394 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
395 {
396         ulong cp, wp, data;
397         int l;
398         int i, rc;
399
400         wp = (addr & ~3);       /* get lower word aligned address */
401
402         /*
403          * handle unaligned start bytes
404          */
405         if ((l = addr - wp) != 0) {
406                 data = 0;
407                 for (i = 0, cp = wp; i < l; ++i, ++cp) {
408                         data = (data >> 8) | (*(uchar *) cp << 24);
409                 }
410                 for (; i < 4 && cnt > 0; ++i) {
411                         data = (data >> 8) | (*src++ << 24);
412                         --cnt;
413                         ++cp;
414                 }
415                 for (; cnt == 0 && i < 4; ++i, ++cp) {
416                         data = (data >> 8) | (*(uchar *) cp << 24);
417                 }
418
419                 if ((rc = write_word (info, wp, data)) != 0) {
420                         return (rc);
421                 }
422                 wp += 4;
423         }
424
425         /*
426          * handle word aligned part
427          */
428         while (cnt >= 4) {
429                 data = *((vu_long *) src);
430                 if ((rc = write_word (info, wp, data)) != 0) {
431                         return (rc);
432                 }
433                 src += 4;
434                 wp += 4;
435                 cnt -= 4;
436         }
437
438         if (cnt == 0) {
439                 return ERR_OK;
440         }
441
442         /*
443          * handle unaligned tail bytes
444          */
445         data = 0;
446         for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
447                 data = (data >> 8) | (*src++ << 24);
448                 --cnt;
449         }
450         for (; i < 4; ++i, ++cp) {
451                 data = (data >> 8) | (*(uchar *) cp << 24);
452         }
453
454         return write_word (info, wp, data);
455 }