board/genietv/flash.c: CodingStyle cleanup
[platform/kernel/u-boot.git] / board / genietv / flash.c
1 /*
2  * (C) Copyright 2000
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 #include <mpc8xx.h>
26
27 flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
28
29 /*-----------------------------------------------------------------------
30  * Functions
31  */
32 static ulong flash_get_size(vu_long *addr, flash_info_t *info);
33 static int write_word(flash_info_t *info, ulong dest, ulong data);
34 static void flash_get_offsets(ulong base, flash_info_t *info);
35
36 /*-----------------------------------------------------------------------
37  */
38
39 unsigned long flash_init(void)
40 {
41         unsigned long size_b0, size_b1;
42         int i;
43
44         /* Init: no FLASHes known */
45         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
46                 flash_info[i].flash_id = FLASH_UNKNOWN;
47
48         /* Detect size */
49         size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
50                         &flash_info[0]);
51
52         /* Setup offsets */
53         flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
54
55 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
56         /* Monitor protection ON by default */
57         flash_protect(FLAG_PROTECT_SET,
58                       CONFIG_SYS_MONITOR_BASE,
59                       CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
60                       &flash_info[0]);
61 #endif
62
63         size_b1 = 0 ;
64
65         flash_info[1].flash_id = FLASH_UNKNOWN;
66         flash_info[1].sector_count = -1;
67
68         flash_info[0].size = size_b0;
69         flash_info[1].size = size_b1;
70
71         return size_b0 + size_b1;
72 }
73
74 /*-----------------------------------------------------------------------
75  * Fix this to support variable sector sizes
76 */
77 static void flash_get_offsets(ulong base, flash_info_t *info)
78 {
79         int i;
80
81         /* set up sector start address table */
82         if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
83                 /* set sector offsets for bottom boot block type        */
84                 for (i = 0; i < info->sector_count; i++)
85                         info->start[i] = base + (i * 0x00010000);
86         }
87 }
88
89 /*-----------------------------------------------------------------------
90  */
91 void flash_print_info(flash_info_t *info)
92 {
93         int i;
94
95         if (info->flash_id == FLASH_UNKNOWN) {
96                 puts("missing or unknown FLASH type\n");
97                 return;
98         }
99
100         switch (info->flash_id & FLASH_VENDMASK) {
101         case FLASH_MAN_AMD:
102                 printf("AMD ");
103                 break;
104         case FLASH_MAN_FUJ:
105                 printf("FUJITSU ");
106                 break;
107         case FLASH_MAN_BM:
108                 printf("BRIGHT MICRO ");
109                 break;
110         default:
111                 printf("Unknown Vendor ");
112                 break;
113         }
114
115         switch (info->flash_id & FLASH_TYPEMASK) {
116         case FLASH_AM040:
117                 printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
118                 break;
119         case FLASH_AM400B:
120                 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
121                 break;
122         case FLASH_AM400T:
123                 printf("AM29LV400T (4 Mbit, top boot sector)\n");
124                 break;
125         case FLASH_AM800B:
126                 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
127                 break;
128         case FLASH_AM800T:
129                 printf("AM29LV800T (8 Mbit, top boot sector)\n");
130                 break;
131         case FLASH_AM160B:
132                 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
133                 break;
134         case FLASH_AM160T:
135                 printf("AM29LV160T (16 Mbit, top boot sector)\n");
136                 break;
137         case FLASH_AM320B:
138                 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
139                 break;
140         case FLASH_AM320T:
141                 printf("AM29LV320T (32 Mbit, top boot sector)\n");
142                 break;
143         default:
144                 printf("Unknown Chip Type\n");
145                 break;
146         }
147
148         if (info->size >> 20) {
149                 printf("  Size: %ld MB in %d Sectors\n",
150                         info->size >> 20,
151                         info->sector_count);
152         } else {
153                 printf("  Size: %ld KB in %d Sectors\n",
154                         info->size >> 10,
155                         info->sector_count);
156         }
157
158         puts("  Sector Start Addresses:");
159
160         for (i = 0; i < info->sector_count; ++i) {
161                 if ((i % 5) == 0)
162                         puts("\n   ");
163
164                 printf(" %08lX%s",
165                         info->start[i],
166                         info->protect[i] ? " (RO)" : "     ");
167         }
168
169         putc('\n');
170         return;
171 }
172 /*-----------------------------------------------------------------------
173  */
174
175 /*
176  * The following code cannot be run from FLASH!
177  */
178
179 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
180 {
181         short i;
182         volatile unsigned char *caddr;
183         char value;
184
185         caddr = (volatile unsigned char *)addr ;
186
187         /* Write auto select command: read Manufacturer ID */
188
189         debug("Base address is: %08x\n", caddr);
190
191         caddr[0x0555] = 0xAA;
192         caddr[0x02AA] = 0x55;
193         caddr[0x0555] = 0x90;
194
195         value = caddr[0];
196
197         debug("Manufact ID: %02x\n", value);
198
199         switch (value) {
200         case 0x1: /* AMD_MANUFACT */
201                 info->flash_id = FLASH_MAN_AMD;
202                 break;
203         case 0x4: /* FUJ_MANUFACT */
204                 info->flash_id = FLASH_MAN_FUJ;
205                 break;
206         default:
207                 info->flash_id = FLASH_UNKNOWN;
208                 info->sector_count = 0;
209                 info->size = 0;
210                 break;
211         }
212
213         value = caddr[1];                       /* device ID            */
214
215         debug("Device ID: %02x\n", value);
216
217         switch (value) {
218         case AMD_ID_LV040B:
219                 info->flash_id += FLASH_AM040;
220                 info->sector_count = 8;
221                 info->size = 0x00080000;
222                 break;                          /* => 512Kb             */
223
224         default:
225                 info->flash_id = FLASH_UNKNOWN;
226                 return 0;                       /* => no or unknown flash */
227         }
228
229         flash_get_offsets((ulong)addr, &flash_info[0]);
230
231         /* check for protected sectors */
232         for (i = 0; i < info->sector_count; i++) {
233                 /*
234                  * read sector protection at sector address,
235                  * (A7 .. A0) = 0x02
236                  * D0 = 1 if protected
237                  */
238                 caddr = (volatile unsigned char *)(info->start[i]);
239                 info->protect[i] = caddr[2] & 1;
240         }
241
242         /*
243          * Prevent writes to uninitialized FLASH.
244          */
245         if (info->flash_id != FLASH_UNKNOWN) {
246                 caddr = (volatile unsigned char *)info->start[0];
247                 *caddr = 0xF0;  /* reset bank */
248         }
249
250         return info->size;
251 }
252
253 int     flash_erase(flash_info_t *info, int s_first, int s_last)
254 {
255         volatile unsigned char *addr =
256                 (volatile unsigned char *)(info->start[0]);
257         int flag, prot, sect, l_sect;
258         ulong start, now, last;
259
260         if ((s_first < 0) || (s_first > s_last)) {
261                 if (info->flash_id == FLASH_UNKNOWN)
262                         printf("- missing\n");
263                 else
264                         printf("- no sectors to erase\n");
265
266                 return 1;
267         }
268
269         if ((info->flash_id == FLASH_UNKNOWN) ||
270             (info->flash_id > FLASH_AMD_COMP)) {
271                 printf("Can't erase unknown flash type - aborted\n");
272                 return 1;
273         }
274
275         prot = 0;
276         for (sect = s_first; sect <= s_last; ++sect) {
277                 if (info->protect[sect])
278                         prot++;
279         }
280
281         if (prot) {
282                 printf("- Warning: %d protected sectors will not be erased!\n",
283                         prot);
284         } else {
285                 printf("\n");
286         }
287
288         l_sect = -1;
289
290         /* Disable interrupts which might cause a timeout here */
291         flag = disable_interrupts();
292
293         addr[0x0555] = 0xAA;
294         addr[0x02AA] = 0x55;
295         addr[0x0555] = 0x80;
296         addr[0x0555] = 0xAA;
297         addr[0x02AA] = 0x55;
298
299         /* Start erase on unprotected sectors */
300         for (sect = s_first; sect <= s_last; sect++) {
301                 if (info->protect[sect] == 0) { /* not protected */
302                         addr = (volatile unsigned char *)(info->start[sect]);
303                         addr[0] = 0x30;
304                         l_sect = sect;
305                 }
306         }
307
308         /* re-enable interrupts if necessary */
309         if (flag)
310                 enable_interrupts();
311
312         /* wait at least 80us - let's wait 1 ms */
313         udelay(1000);
314
315         /*
316          * We wait for the last triggered sector
317          */
318         if (l_sect < 0)
319                 goto DONE;
320
321         start = get_timer(0);
322         last  = start;
323         addr = (volatile unsigned char *)(info->start[l_sect]);
324
325         while ((addr[0] & 0xFF) != 0xFF) {
326
327                 now = get_timer(start);
328
329                 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
330                         printf("Timeout\n");
331                         return 1;
332                 }
333                 /* show that we're waiting */
334                 if ((now - last) > 1000) {      /* every second */
335                         putc('.');
336                         last = now;
337                 }
338         }
339
340 DONE:
341         /* reset to read mode */
342         addr = (volatile unsigned char *)info->start[0];
343
344         addr[0] = 0xF0; /* reset bank */
345
346         printf(" done\n");
347         return 0;
348 }
349
350 /*-----------------------------------------------------------------------
351  * Copy memory to flash, returns:
352  * 0 - OK
353  * 1 - write timeout
354  * 2 - Flash not erased
355  */
356
357 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
358 {
359         ulong cp, wp, data;
360         int i, l, rc;
361
362         wp = (addr & ~3);       /* get lower word aligned address */
363
364         /*
365          * handle unaligned start bytes
366          */
367         l = addr - wp;
368
369         if (l != 0) {
370                 data = 0;
371                 for (i = 0, cp = wp; i < l; ++i, ++cp)
372                         data = (data << 8) | (*(uchar *)cp);
373
374                 for (; i < 4 && cnt > 0; ++i) {
375                         data = (data << 8) | *src++;
376                         --cnt;
377                         ++cp;
378                 }
379                 for (; cnt == 0 && i < 4; ++i, ++cp)
380                         data = (data << 8) | (*(uchar *)cp);
381
382                 rc = write_word(info, wp, data);
383
384                 if (rc != 0)
385                         return rc;
386
387                 wp += 4;
388         }
389
390         /*
391          * handle word aligned part
392          */
393         while (cnt >= 4) {
394                 data = 0;
395                 for (i = 0; i < 4; ++i)
396                         data = (data << 8) | *src++;
397
398                 rc = write_word(info, wp, data);
399
400                 if (rc != 0)
401                         return rc;
402
403                 wp  += 4;
404                 cnt -= 4;
405         }
406
407         if (cnt == 0)
408                 return 0;
409
410         /*
411          * handle unaligned tail bytes
412          */
413         data = 0;
414         for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
415                 data = (data << 8) | *src++;
416                 --cnt;
417         }
418         for (; i < 4; ++i, ++cp)
419                 data = (data << 8) | (*(uchar *)cp);
420
421         return write_word(info, wp, data);
422 }
423
424 /*-----------------------------------------------------------------------
425  * Write a word to Flash, returns:
426  * 0 - OK
427  * 1 - write timeout
428  * 2 - Flash not erased
429  */
430 static int write_word(flash_info_t *info, ulong dest, ulong data)
431 {
432         volatile unsigned char *cdest, *cdata;
433         volatile unsigned char *addr =
434                 (volatile unsigned char *)(info->start[0]);
435         ulong start;
436         int flag, count = 4 ;
437
438         cdest = (volatile unsigned char *)dest ;
439         cdata = (volatile unsigned char *)&data ;
440
441         /* Check if Flash is (sufficiently) erased */
442         if ((*((vu_long *)dest) & data) != data)
443                 return 2;
444
445         while (count--) {
446
447                 /* Disable interrupts which might cause a timeout here */
448                 flag = disable_interrupts();
449
450                 addr[0x0555] = 0xAA;
451                 addr[0x02AA] = 0x55;
452                 addr[0x0555] = 0xA0;
453
454                 *cdest = *cdata;
455
456                 /* re-enable interrupts if necessary */
457                 if (flag)
458                         enable_interrupts();
459
460                 /* data polling for D7 */
461                 start = get_timer(0);
462                 while ((*cdest ^ *cdata) & 0x80) {
463                         if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
464                                 return 1;
465                 }
466
467                 cdata++ ;
468                 cdest++ ;
469         }
470         return 0;
471 }