* Get (mostly) rid of CFG_MONITOR_LEN definition; compute real length
[oweals/u-boot.git] / board / pm826 / flash.c
1 /*
2  * (C) Copyright 2001, 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * Flash Routines for Intel devices
6  *
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 #include <common.h>
28 #include <mpc8xx.h>
29
30
31 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
32
33 /*-----------------------------------------------------------------------
34  */
35 ulong flash_get_size (volatile unsigned long *baseaddr,
36                                           flash_info_t * info)
37 {
38         short i;
39         unsigned long flashtest_h, flashtest_l;
40
41         info->sector_count = info->size = 0;
42         info->flash_id = FLASH_UNKNOWN;
43
44         /* Write query command sequence and test FLASH answer
45          */
46         baseaddr[0] = 0x00980098;
47         baseaddr[1] = 0x00980098;
48
49         flashtest_h = baseaddr[0];      /* manufacturer ID      */
50         flashtest_l = baseaddr[1];
51
52         if (flashtest_h != INTEL_MANUFACT || flashtest_l != INTEL_MANUFACT)
53                 return (0);             /* no or unknown flash  */
54
55         flashtest_h = baseaddr[2];      /* device ID            */
56         flashtest_l = baseaddr[3];
57
58         if (flashtest_h != flashtest_l)
59                 return (0);
60
61         switch (flashtest_h) {
62         case INTEL_ID_28F160C3B:
63                 info->flash_id = FLASH_28F160C3B;
64                 info->sector_count = 39;
65                 info->size = 0x00800000;        /* 4 * 2 MB = 8 MB      */
66                 break;
67         case INTEL_ID_28F160F3B:
68                 info->flash_id = FLASH_28F160F3B;
69                 info->sector_count = 39;
70                 info->size = 0x00800000;        /* 4 * 2 MB = 8 MB      */
71                 break;
72         default:
73                 return (0);                     /* no or unknown flash  */
74         }
75
76         info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
77
78         if (info->flash_id & FLASH_BTYPE) {
79                 volatile unsigned long *tmp = baseaddr;
80
81                 /* set up sector start adress table (bottom sector type)
82                  * AND unlock the sectors (if our chip is 160C3)
83                  */
84                 for (i = 0; i < info->sector_count; i++) {
85                         if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) {
86                                 tmp[0] = 0x00600060;
87                                 tmp[1] = 0x00600060;
88                                 tmp[0] = 0x00D000D0;
89                                 tmp[1] = 0x00D000D0;
90                         }
91                         info->start[i] = (uint) tmp;
92                         tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith       */
93                 }
94         }
95
96         memset (info->protect, 0, info->sector_count);
97
98         baseaddr[0] = 0x00FF00FF;
99         baseaddr[1] = 0x00FF00FF;
100
101         return (info->size);
102 }
103
104 /*-----------------------------------------------------------------------
105  */
106 unsigned long flash_init (void)
107 {
108         unsigned long size_b0 = 0;
109         int i;
110
111         /* Init: no FLASHes known
112          */
113         for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
114                 flash_info[i].flash_id = FLASH_UNKNOWN;
115         }
116
117         /* Static FLASH Bank configuration here (only one bank) */
118
119         size_b0 = flash_get_size ((ulong *) CFG_FLASH0_BASE, &flash_info[0]);
120         if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
121                 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
122                                 size_b0, size_b0 >> 20);
123         }
124
125         /* protect monitor and environment sectors
126          */
127
128 #ifndef CONFIG_BOOT_ROM
129         /* If U-Boot is  booted from ROM the CFG_MONITOR_BASE > CFG_FLASH0_BASE
130          * but we shouldn't protect it.
131          */
132
133 # if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
134         flash_protect (FLAG_PROTECT_SET,
135                        CFG_MONITOR_BASE,
136                        CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
137         );
138 # endif
139 #endif  /* CONFIG_BOOT_ROM */
140
141 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
142 # ifndef  CFG_ENV_SIZE
143 #  define CFG_ENV_SIZE  CFG_ENV_SECT_SIZE
144 # endif
145         flash_protect (FLAG_PROTECT_SET,
146                        CFG_ENV_ADDR,
147                        CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
148 #endif
149
150         return (size_b0);
151 }
152
153 /*-----------------------------------------------------------------------
154  */
155 void flash_print_info (flash_info_t * info)
156 {
157         int i;
158
159         if (info->flash_id == FLASH_UNKNOWN) {
160                 printf ("missing or unknown FLASH type\n");
161                 return;
162         }
163
164         switch ((info->flash_id >> 16) & 0xff) {
165         case 0x89:
166                 printf ("INTEL ");
167                 break;
168         default:
169                 printf ("Unknown Vendor ");
170                 break;
171         }
172
173         switch (info->flash_id & FLASH_TYPEMASK) {
174         case FLASH_28F160C3B:
175                 printf ("28F160C3B (16 M, bottom sector)\n");
176                 break;
177         case FLASH_28F160F3B:
178                 printf ("28F160F3B (16 M, bottom sector)\n");
179                 break;
180         default:
181                 printf ("Unknown Chip Type\n");
182                 break;
183         }
184
185         printf ("  Size: %ld MB in %d Sectors\n",
186                         info->size >> 20, info->sector_count);
187
188         printf ("  Sector Start Addresses:");
189         for (i = 0; i < info->sector_count; ++i) {
190                 if ((i % 5) == 0)
191                         printf ("\n   ");
192                 printf (" %08lX%s",
193                         info->start[i],
194                         info->protect[i] ? " (RO)" : "     "
195                 );
196         }
197         printf ("\n");
198 }
199
200 /*-----------------------------------------------------------------------
201  */
202 int flash_erase (flash_info_t * info, int s_first, int s_last)
203 {
204         int flag, prot, sect;
205         ulong start, now, last;
206
207         if ((s_first < 0) || (s_first > s_last)) {
208                 if (info->flash_id == FLASH_UNKNOWN) {
209                         printf ("- missing\n");
210                 } else {
211                         printf ("- no sectors to erase\n");
212                 }
213                 return 1;
214         }
215
216         prot = 0;
217         for (sect = s_first; sect <= s_last; sect++) {
218                 if (info->protect[sect])
219                         prot++;
220         }
221
222         if (prot) {
223                 printf ("- Warning: %d protected sectors will not be erased!\n",
224                                 prot);
225         } else {
226                 printf ("\n");
227         }
228
229         /* Start erase on unprotected sectors
230          */
231         for (sect = s_first; sect <= s_last; sect++) {
232                 volatile ulong *addr =
233                                 (volatile unsigned long *) info->start[sect];
234
235                 start = get_timer (0);
236                 last = start;
237                 if (info->protect[sect] == 0) {
238                         /* Disable interrupts which might cause a timeout here
239                          */
240                         flag = disable_interrupts ();
241
242                         /* Erase the block
243                          */
244                         addr[0] = 0x00200020;
245                         addr[1] = 0x00200020;
246                         addr[0] = 0x00D000D0;
247                         addr[1] = 0x00D000D0;
248
249                         /* re-enable interrupts if necessary
250                          */
251                         if (flag)
252                                 enable_interrupts ();
253
254                         /* wait at least 80us - let's wait 1 ms
255                          */
256                         udelay (1000);
257
258                         last = start;
259                         while ((addr[0] & 0x00800080) != 0x00800080 ||
260                                    (addr[1] & 0x00800080) != 0x00800080) {
261                                 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
262                                         printf ("Timeout (erase suspended!)\n");
263                                         /* Suspend erase
264                                          */
265                                         addr[0] = 0x00B000B0;
266                                         addr[1] = 0x00B000B0;
267                                         goto DONE;
268                                 }
269                                 /* show that we're waiting
270                                  */
271                                 if ((now - last) > 1000) {      /* every second */
272                                         serial_putc ('.');
273                                         last = now;
274                                 }
275                         }
276                         if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
277                                 printf ("*** ERROR: erase failed!\n");
278                                 goto DONE;
279                         }
280                 }
281                 /* Clear status register and reset to read mode
282                  */
283                 addr[0] = 0x00500050;
284                 addr[1] = 0x00500050;
285                 addr[0] = 0x00FF00FF;
286                 addr[1] = 0x00FF00FF;
287         }
288
289         printf (" done\n");
290
291 DONE:
292         return 0;
293 }
294
295 static int write_word (flash_info_t *, volatile unsigned long *, ulong);
296
297 /*-----------------------------------------------------------------------
298  * Copy memory to flash, returns:
299  * 0 - OK
300  * 1 - write timeout
301  * 2 - Flash not erased
302  */
303 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
304 {
305         ulong v;
306         int i, l, cc = cnt, res = 0;
307
308
309         for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
310                 l = (addr & 3);
311                 addr &= ~3;
312
313                 for (i = 0; i < 4; i++) {
314                         v = (v << 8) + (i < l || i - l >= cc ?
315                                 *((unsigned char *) addr + i) : *src++);
316                 }
317
318                 if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
319                         break;
320         }
321
322         return (res);
323 }
324
325 /*-----------------------------------------------------------------------
326  * Write a word to Flash, returns:
327  * 0 - OK
328  * 1 - write timeout
329  * 2 - Flash not erased
330  */
331 static int write_word (flash_info_t * info, volatile unsigned long *addr,
332                                            ulong data)
333 {
334         int flag, res = 0;
335         ulong start;
336
337         /* Check if Flash is (sufficiently) erased
338          */
339         if ((*addr & data) != data)
340                 return (2);
341
342         /* Disable interrupts which might cause a timeout here
343          */
344         flag = disable_interrupts ();
345
346         *addr = 0x00400040;
347         *addr = data;
348
349         /* re-enable interrupts if necessary
350          */
351         if (flag)
352                 enable_interrupts ();
353
354         start = get_timer (0);
355         while ((*addr & 0x00800080) != 0x00800080) {
356                 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
357                         /* Suspend program
358                          */
359                         *addr = 0x00B000B0;
360                         res = 1;
361                         goto OUT;
362                 }
363         }
364
365         if (*addr & 0x00220022) {
366                 printf ("*** ERROR: program failed!\n");
367                 res = 1;
368         }
369
370 OUT:
371         /* Clear status register and reset to read mode
372          */
373         *addr = 0x00500050;
374         *addr = 0x00FF00FF;
375
376         return (res);
377 }