]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/lubbock/flash.c
84c09a853c7152e404dbd02afbe3ce0374269420
[karo-tx-uboot.git] / board / lubbock / flash.c
1 /*
2  * (C) Copyright 2002
3  * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4  *
5  * (C) Copyright 2002
6  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Marius Groeger <mgroeger@sysgo.de>
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 #include <common.h>
29
30 #define FLASH_BANK_SIZE 0x2000000
31 #define MAIN_SECT_SIZE  0x40000         /* 2x16 = 256k per sector */
32
33 flash_info_t    flash_info[CFG_MAX_FLASH_BANKS];
34
35
36 /*-----------------------------------------------------------------------
37  */
38
39 ulong flash_init(void)
40 {
41     int i, j;
42     ulong size = 0;
43
44     for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
45     {
46         ulong flashbase = 0;
47         flash_info[i].flash_id =
48           (INTEL_MANUFACT & FLASH_VENDMASK) |
49           (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
50         flash_info[i].size = FLASH_BANK_SIZE;
51         flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
52         memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
53         switch (i)
54         {
55            case 0:
56                 flashbase = PHYS_FLASH_1;
57                 break;
58            case 1:
59                 flashbase = PHYS_FLASH_2;
60                 break;
61            default:
62                 panic("configured to many flash banks!\n");
63                 break;
64         }
65         for (j = 0; j < flash_info[i].sector_count; j++)
66         {
67             flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
68         }
69         size += flash_info[i].size;
70     }
71
72     /* Protect monitor and environment sectors
73      */
74     flash_protect(FLAG_PROTECT_SET,
75                   CFG_FLASH_BASE,
76                   CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
77                   &flash_info[0]);
78
79     flash_protect(FLAG_PROTECT_SET,
80                   CFG_ENV_ADDR,
81                   CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
82                   &flash_info[0]);
83
84     return size;
85 }
86
87 /*-----------------------------------------------------------------------
88  */
89 void flash_print_info  (flash_info_t *info)
90 {
91     int i, j;
92
93     for (j=0; j<CFG_MAX_FLASH_BANKS; j++)
94     {
95         switch (info->flash_id & FLASH_VENDMASK)
96         {
97         case (INTEL_MANUFACT & FLASH_VENDMASK):
98                 printf("Intel: ");
99                 break;
100         default:
101                 printf("Unknown Vendor ");
102                 break;
103         }
104
105         switch (info->flash_id & FLASH_TYPEMASK)
106         {
107         case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
108                 printf("28F128J3 (128Mbit)\n");
109                 break;
110         default:
111                 printf("Unknown Chip Type\n");
112                 goto Done;
113                 break;
114         }
115
116         printf("  Size: %ld MB in %d Sectors\n",
117                 info->size >> 20, info->sector_count);
118
119         printf("  Sector Start Addresses:");
120         for (i = 0; i < info->sector_count; i++)
121         {
122                 if ((i % 5) == 0)
123                 {
124                 printf ("\n   ");
125                 }
126                 printf (" %08lX%s", info->start[i],
127                         info->protect[i] ? " (RO)" : "     ");
128         }
129         printf ("\n");
130         info++;
131     }
132
133 Done:
134 }
135
136 /*-----------------------------------------------------------------------
137  */
138
139 int     flash_erase (flash_info_t *info, int s_first, int s_last)
140 {
141     int flag, prot, sect;
142     int rc = ERR_OK;
143
144     if (info->flash_id == FLASH_UNKNOWN)
145         return ERR_UNKNOWN_FLASH_TYPE;
146
147     if ((s_first < 0) || (s_first > s_last)) {
148         return ERR_INVAL;
149     }
150
151     if ((info->flash_id & FLASH_VENDMASK) !=
152         (INTEL_MANUFACT & FLASH_VENDMASK)) {
153         return ERR_UNKNOWN_FLASH_VENDOR;
154     }
155
156     prot = 0;
157     for (sect=s_first; sect<=s_last; ++sect) {
158         if (info->protect[sect]) {
159             prot++;
160         }
161     }
162     if (prot)
163         return ERR_PROTECTED;
164
165     /*
166      * Disable interrupts which might cause a timeout
167      * here. Remember that our exception vectors are
168      * at address 0 in the flash, and we don't want a
169      * (ticker) exception to happen while the flash
170      * chip is in programming mode.
171      */
172     flag = disable_interrupts();
173
174     /* Start erase on unprotected sectors */
175     for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
176
177         printf("Erasing sector %2d ... ", sect);
178
179         /* arm simple, non interrupt dependent timer */
180         reset_timer_masked();
181
182         if (info->protect[sect] == 0) { /* not protected */
183             vu_short *addr = (vu_short *)(info->start[sect]);
184
185             *addr = 0x20;       /* erase setup */
186             *addr = 0xD0;       /* erase confirm */
187
188             while ((*addr & 0x80) != 0x80) {
189                 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
190                     *addr = 0xB0; /* suspend erase */
191                     *addr = 0xFF;       /* reset to read mode */
192                     rc = ERR_TIMOUT;
193                     goto outahere;
194                 }
195             }
196
197             /* clear status register command */
198             *addr = 0x50;
199             /* reset to read mode */
200             *addr = 0xFF;
201         }
202         printf("ok.\n");
203     }
204     if (ctrlc())
205       printf("User Interrupt!\n");
206
207 outahere:
208
209     /* allow flash to settle - wait 10 ms */
210     udelay_masked(10000);
211
212     if (flag)
213       enable_interrupts();
214
215     return rc;
216 }
217
218 /*-----------------------------------------------------------------------
219  * Copy memory to flash
220  */
221
222 static int write_word (flash_info_t *info, ulong dest, ushort data)
223 {
224     vu_short *addr = (vu_short *)dest, val;
225     int rc = ERR_OK;
226     int flag;
227
228     /* Check if Flash is (sufficiently) erased
229      */
230     if ((*addr & data) != data)
231         return ERR_NOT_ERASED;
232
233     /*
234      * Disable interrupts which might cause a timeout
235      * here. Remember that our exception vectors are
236      * at address 0 in the flash, and we don't want a
237      * (ticker) exception to happen while the flash
238      * chip is in programming mode.
239      */
240     flag = disable_interrupts();
241
242     /* clear status register command */
243     *addr = 0x50;
244
245     /* program set-up command */
246     *addr = 0x40;
247
248     /* latch address/data */
249     *addr = data;
250
251     /* arm simple, non interrupt dependent timer */
252     reset_timer_masked();
253
254     /* wait while polling the status register */
255     while(((val = *addr) & 0x80) != 0x80)
256     {
257         if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
258             rc = ERR_TIMOUT;
259             /* suspend program command */
260             *addr = 0xB0;
261             goto outahere;
262         }
263     }
264
265     if(val & 0x1A) {    /* check for error */
266         printf("\nFlash write error %02x at address %08lx\n",
267            (int)val, (unsigned long)dest);
268         if(val & (1<<3)) {
269             printf("Voltage range error.\n");
270             rc = ERR_PROG_ERROR;
271             goto outahere;
272         }
273         if(val & (1<<1)) {
274             printf("Device protect error.\n");
275             rc = ERR_PROTECTED;
276             goto outahere;
277         }
278         if(val & (1<<4)) {
279             printf("Programming error.\n");
280             rc = ERR_PROG_ERROR;
281             goto outahere;
282         }
283         rc = ERR_PROG_ERROR;
284         goto outahere;
285     }
286
287 outahere:
288     /* read array command */
289     *addr = 0xFF;
290
291     if (flag)
292       enable_interrupts();
293
294     return rc;
295 }
296
297 /*-----------------------------------------------------------------------
298  * Copy memory to flash.
299  */
300
301 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
302 {
303     ulong cp, wp;
304     ushort data;
305     int l;
306     int i, rc;
307
308     wp = (addr & ~1);   /* get lower word aligned address */
309
310     /*
311      * handle unaligned start bytes
312      */
313     if ((l = addr - wp) != 0) {
314         data = 0;
315         for (i=0, cp=wp; i<l; ++i, ++cp) {
316             data = (data >> 8) | (*(uchar *)cp << 8);
317         }
318         for (; i<2 && cnt>0; ++i) {
319             data = (data >> 8) | (*src++ << 8);
320             --cnt;
321             ++cp;
322         }
323         for (; cnt==0 && i<2; ++i, ++cp) {
324             data = (data >> 8) | (*(uchar *)cp << 8);
325         }
326
327         if ((rc = write_word(info, wp, data)) != 0) {
328             return (rc);
329         }
330         wp += 2;
331     }
332
333     /*
334      * handle word aligned part
335      */
336     while (cnt >= 2) {
337         data = *((vu_short*)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 == 0) {
347         return ERR_OK;
348     }
349
350     /*
351      * handle unaligned tail bytes
352      */
353     data = 0;
354     for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
355         data = (data >> 8) | (*src++ << 8);
356         --cnt;
357     }
358     for (; i<2; ++i, ++cp) {
359         data = (data >> 8) | (*(uchar *)cp << 8);
360     }
361
362     return write_word(info, wp, data);
363 }