]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/ssv/common/flash.c
rename CFG_ macros to CONFIG_SYS
[karo-tx-uboot.git] / board / ssv / common / flash.c
1 /*
2  * (C) Copyright 2000-2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2004, Li-Pro.Net <www.li-pro.net>
6  * Stephan Linz <linz@li-pro.net>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27
28 #include <common.h>
29 #include <watchdog.h>
30 #include <nios.h>
31
32 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
33
34 /*--------------------------------------------------------------------*/
35 void flash_print_info (flash_info_t * info)
36 {
37         int i, k;
38         unsigned long size;
39         int erased;
40         volatile unsigned char *flash;
41
42         printf ("  Size: %ld KB in %d Sectors\n",
43                 info->size >> 10, info->sector_count);
44         printf ("  Sector Start Addresses:");
45         for (i = 0; i < info->sector_count; ++i) {
46
47                 /* Check if whole sector is erased */
48                 if (i != (info->sector_count - 1))
49                         size = info->start[i + 1] - info->start[i];
50                 else
51                         size = info->start[0] + info->size - info->start[i];
52                 erased = 1;
53                 flash = (volatile unsigned char *) info->start[i];
54                 for (k = 0; k < size; k++) {
55                         if (*flash++ != 0xff) {
56                                 erased = 0;
57                                 break;
58                         }
59                 }
60
61                 /* Print the info */
62                 if ((i % 5) == 0)
63                         printf ("\n   ");
64                 printf (" %08lX%s%s", info->start[i], erased ? " E" : "  ",
65                         info->protect[i] ? "RO " : "   ");
66         }
67         printf ("\n");
68 }
69
70 /*-------------------------------------------------------------------*/
71
72
73 int flash_erase (flash_info_t * info, int s_first, int s_last)
74 {
75         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
76         volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
77         int prot, sect, wait;
78         unsigned oldpri;
79         ulong start;
80
81         /* Some sanity checking */
82         if ((s_first < 0) || (s_first > s_last)) {
83                 printf ("- no sectors to erase\n");
84                 return 1;
85         }
86
87         prot = 0;
88         for (sect = s_first; sect <= s_last; ++sect) {
89                 if (info->protect[sect]) {
90                         prot++;
91                 }
92         }
93         if (prot) {
94                 printf ("- Warning: %d protected sectors will not be erased!\n",
95                         prot);
96         } else {
97                 printf ("\n");
98         }
99
100 #ifdef DEBUG
101         for (sect = s_first; sect <= s_last; sect++) {
102                 printf("- Erase: Sect: %i @ 0x%08x\n", sect,  info->start[sect]);
103         }
104 #endif
105
106         /* NOTE: disabling interrupts on Nios can be very bad since it
107          * also disables the LO_LIMIT exception. It's better here to
108          * set the interrupt priority to 3 & restore it when we're done.
109          */
110         oldpri = ipri (3);
111
112         /* It's ok to erase multiple sectors provided we don't delay more
113          * than 50 usec between cmds ... at which point the erase time-out
114          * occurs. So don't go and put printf() calls in the loop ... it
115          * won't be very helpful ;-)
116          */
117         for (sect = s_first; sect <= s_last; sect++) {
118                 if (info->protect[sect] == 0) { /* not protected */
119                         addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
120                         *addr = 0xf0;
121                         *(addr+0xAAA/2) = 0xaa;
122                         *(addr+0x554/2) = 0x55;
123                         *(addr+0xAAA/2) = 0x80;
124                         *(addr+0xAAA/2) = 0xaa;
125                         *(addr+0x554/2) = 0x55;
126                         *addr2 = 0x30;
127                         /* Now just wait for 0xffff & provide some user
128                          * feedback while we wait. Here we have to grant
129                          * timer interrupts. Otherwise get_timer() can't
130                          * work right. */
131                         ipri(oldpri);
132                         start = get_timer (0);
133                         while (*addr2 != 0xffff) {
134                                 for (wait = 8; wait; wait--) {
135                                         udelay (125 * 1000);
136                                 }
137                                 putc ('.');
138                                 if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
139                                         printf ("timeout\n");
140                                         return 1;
141                                 }
142                         }
143                         oldpri = ipri (3); /* disallow non important irqs again */
144                 }
145         }
146
147         printf ("\n");
148         *addr = 0xf0;
149
150         /* Restore interrupt priority */
151         ipri (oldpri);
152
153         return 0;
154 }
155
156 /*-----------------------------------------------------------------------
157  * Copy memory to flash, returns:
158  * 0 - OK
159  * 1 - write timeout
160  * 2 - Flash not erased
161  */
162
163 int write_buff (flash_info_t * info, uchar * srcbuffer, ulong addr, ulong cnt)
164 {
165
166         volatile CONFIG_SYS_FLASH_WORD_SIZE *cmd = (vu_short *) info->start[0];
167         volatile CONFIG_SYS_FLASH_WORD_SIZE *dst = (vu_short *) addr;
168         CONFIG_SYS_FLASH_WORD_SIZE *src = (void *) srcbuffer;
169         CONFIG_SYS_FLASH_WORD_SIZE b;
170         unsigned oldpri;
171         ulong start;
172
173         cnt /= sizeof(CONFIG_SYS_FLASH_WORD_SIZE);
174         while (cnt) {
175                 /* Check for sufficient erase */
176                 b = *src;
177                 if ((*dst & b) != b) {
178                         printf ("%02x : %02x\n", *dst, b);
179                         return (2);
180                 }
181
182                 /* Disable interrupts other than window underflow
183                  * (interrupt priority 2)
184                  */
185                 oldpri = ipri (3);
186                 *(cmd+0xAAA/2) = 0xaa;
187                 *(cmd+0x554/2) = 0x55;
188                 *(cmd+0xAAA/2) = 0xa0;
189                 ipri (oldpri);
190                 *dst = b;
191
192                 /* Verify write */
193                 start = get_timer (0);
194                 while (*dst != b) {
195                         if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
196                                 *cmd = 0xf0;
197                                 return 1;
198                         }
199                 }
200                 dst++;
201                 src++;
202                 cnt--;
203         }
204
205         *cmd = 0xf0;
206         return (0);
207 }