arm: nvidia and smdk6400: use common code for machine type
[oweals/u-boot.git] / board / modnet50 / flash.c
1 /*
2  * (C) Copyright 2002
3  * MAZeT GmbH <www.mazet.de>
4  * Stephan Linz <linz@mazet.de>, <linz@li-pro.net>
5  *
6  * The most stuff comes from PPCBoot and Linux.
7  *
8  * IMMS gGmbH <www.imms.de>
9  * Thomas Elste <info@elste.org>
10  *
11  * Modifications for ModNET50 Board
12  *
13  * See file CREDITS for list of people who contributed to this
14  * project.
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation; either version 2 of
19  * the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29  * MA 02111-1307 USA
30  */
31
32 #include <common.h>
33 #include <asm/arch/netarm_registers.h>
34
35 #define SCR   (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_SYSTEM_CONTROL))
36
37 #define ALIGN_ABORT_OFF         SCR = SCR & ~NETARM_GEN_SYS_CFG_ALIGN_ABORT
38 #define ALIGN_ABORT_ON          SCR = SCR |  NETARM_GEN_SYS_CFG_ALIGN_ABORT
39
40 #define PROG_ADDR               (0x555*2)
41 #define SETUP_ADDR              (0x555*2)
42 #define ID_ADDR                 (0x555*2)
43 #define UNLOCK_ADDR1            (0x555*2)
44 #define UNLOCK_ADDR2            (0x2AA*2)
45
46 #define UNLOCK_CMD1             (0xAA)
47 #define UNLOCK_CMD2             (0x55)
48 #define ERASE_SUSPEND_CMD       (0xB0)
49 #define ERASE_RESUME_CMD        (0x30)
50 #define RESET_CMD               (0xF0)
51 #define ID_CMD                  (0x90)
52 #define SECERASE_CMD            (0x30)
53 #define CHIPERASE_CMD           (0x10)
54 #define PROG_CMD                (0xa0)
55 #define SETUP_CMD               (0x80)
56
57 #define DQ2                     (0x04)
58 #define DQ3                     (DQ2*2)
59 #define DQ5                     (DQ3*4)
60 #define DQ6                     (DQ5*2)
61
62 #define WRITE_UNLOCK(addr) { \
63   *(volatile __u16*)(addr + UNLOCK_ADDR1) = (__u16)UNLOCK_CMD1; \
64   *(volatile __u16*)(addr + UNLOCK_ADDR2) = (__u16)UNLOCK_CMD2; \
65 }
66
67 #define CONFIG_AM29_RESERVED    (0)
68 #define K                       (1024)
69 #define MB                      (4)
70
71 #define CELL_SIZE               (64*K)
72 #define DEVICE_SIZE             (MB*K*K)
73 #define CELLS_PER_DEVICE        (DEVICE_SIZE/CELL_SIZE)
74 #define RESERVED_CELLS          (CONFIG_AM29_RESERVED*K)/CELL_SIZE
75 #define MAX_FLASH_DEVICES       (1)
76 #define AVAIL_SIZE              (DEVICE_SIZE*MAX_FLASH_DEVICES - RESERVED_CELLS*CELL_SIZE)
77
78
79 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
80 static __u16 toggling_bits;
81
82
83 /*-----------------------------------------------------------------------
84  */
85 ulong flash_get_size (ulong baseaddr, flash_info_t * info)
86 {
87         short i;
88         __u16 flashtest;
89
90         /* Write auto select command sequence and test FLASH answer */
91         WRITE_UNLOCK (baseaddr);
92         *(volatile __u16 *) (baseaddr + ID_ADDR) = (__u16) ID_CMD;
93         flashtest /* manufacturer ID */  = *(volatile __u16 *) (baseaddr);
94         *(volatile __u16 *) (baseaddr + ID_ADDR) = (__u16) RESET_CMD;
95
96         switch ((__u32) ((flashtest << 16) + flashtest)) {
97         case AMD_MANUFACT:
98                 info->flash_id = FLASH_MAN_AMD & FLASH_VENDMASK;
99                 break;
100         case FUJ_MANUFACT:
101                 info->flash_id = FLASH_MAN_FUJ & FLASH_VENDMASK;
102                 break;
103         default:
104                 info->flash_id = FLASH_UNKNOWN;
105                 info->sector_count = 0;
106                 info->size = 0;
107                 return (0);     /* no or unknown flash */
108         }
109
110         /* Write auto select command sequence and test FLASH answer */
111         WRITE_UNLOCK (baseaddr);
112         *(volatile __u16 *) (baseaddr + ID_ADDR) = (__u16) ID_CMD;
113         flashtest /* device ID */  = *(volatile __u16 *) (baseaddr + 2);
114         *(volatile __u16 *) (baseaddr + ID_ADDR) = (__u16) RESET_CMD;
115
116         /* toggling_bits = (flashtest == TOSHIBA)?(DQ6):(DQ2|DQ6); */
117         toggling_bits = (DQ2 | DQ6);
118
119         switch ((__u32) ((flashtest << 16) + flashtest)) {
120         case AMD_ID_LV160B:
121                 info->flash_id +=
122                         (FLASH_AM160LV | FLASH_AM160B) & FLASH_TYPEMASK;
123                 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
124                 info->size = CONFIG_SYS_FLASH_SIZE;
125                 /* 1*16K Boot Block
126                    2*8K Parameter Block
127                    1*32K Small Main Block */
128                 info->start[0] = baseaddr;
129                 info->start[1] = baseaddr + 0x4000;
130                 info->start[2] = baseaddr + 0x6000;
131                 info->start[3] = baseaddr + 0x8000;
132                 for (i = 1; i < info->sector_count; i++)
133                         info->start[3 + i] = baseaddr + i * CONFIG_SYS_MAIN_SECT_SIZE;
134                 break;
135         default:
136                 info->flash_id = FLASH_UNKNOWN;
137                 return (0);     /* no or unknown flash */
138         }
139
140         for (i = 0; i < info->sector_count; i++) {
141                 /* Write auto select command sequence and test FLASH answer */
142                 WRITE_UNLOCK (info->start[i]);
143                 *(volatile __u16 *) (info->start[i] + ID_ADDR) = (__u16) ID_CMD;
144                 flashtest /* protected verify */  = *(volatile __u16 *) (info->start[i] + 4);
145                 *(volatile __u16 *) (info->start[i] + ID_ADDR) = (__u16) RESET_CMD;
146                 if (flashtest & 0x0001) {
147                         info->protect[i] = 1;   /* D0 = 1 if protected */
148                 } else {
149                         info->protect[i] = 0;
150                 }
151         }
152         return (info->size);
153 }
154
155 /*-----------------------------------------------------------------------
156  */
157 ulong flash_init (void)
158 {
159         ulong size = 0;
160         int i;
161
162         /* Init: no FLASHes known */
163         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
164                 flash_info[i].flash_id = FLASH_UNKNOWN;
165         }
166
167         /* Static FLASH Bank configuration here (only one bank) */
168         size = flash_get_size (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
169
170         if (flash_info[0].flash_id == FLASH_UNKNOWN || size == 0) {
171                 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
172                         size, size >> 20);
173         }
174
175         /*
176          * protect monitor and environment sectors
177          */
178         flash_protect (FLAG_PROTECT_SET,
179                        CONFIG_SYS_FLASH_BASE,
180                        CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
181                        &flash_info[0]);
182
183         flash_protect (FLAG_PROTECT_SET,
184                        CONFIG_ENV_ADDR,
185                        CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
186
187         return size;
188 }
189
190 /*-----------------------------------------------------------------------
191  */
192 void flash_print_info (flash_info_t * info)
193 {
194         int i;
195
196         if (info->flash_id == FLASH_UNKNOWN) {
197                 printf ("missing or unknown FLASH type\n");
198                 return;
199         }
200
201         switch (info->flash_id & FLASH_VENDMASK) {
202         case FLASH_MAN_AMD:
203                 printf ("AMD ");
204                 break;
205         case FLASH_MAN_FUJ:
206                 printf ("Fujitsu ");
207                 break;
208         default:
209                 printf ("Unknown Vendor ");
210                 break;
211         }
212
213         switch (info->flash_id & FLASH_TYPEMASK) {
214         case FLASH_AMDL323B:
215                 printf ("29DL323B (32 M, bottom sector)\n");
216                 break;
217         case (FLASH_AM160LV | FLASH_AM160B):
218                 printf ("29LV160BE (1M x 16, bottom sector)\n");
219                 break;
220         default:
221                 printf ("Unknown Chip Type\n");
222                 break;
223         }
224
225         printf ("  Size: %ld MB in %d Sectors\n",
226                 info->size >> 20, info->sector_count);
227         printf ("  Sector Start Addresses:");
228         for (i = 0; i < info->sector_count; i++) {
229                 if ((i % 4) == 0)
230                         printf ("\n   ");
231                 printf (" S%02d @ 0x%08lX%s", i,
232                         info->start[i], info->protect[i] ? " !" : "  ");
233         }
234         printf ("\n");
235         return;
236 }
237
238 /*-----------------------------------------------------------------------
239  */
240 int flash_check_protection (flash_info_t * info, int s_first, int s_last)
241 {
242         int sect, prot = 0;
243
244         for (sect = s_first; sect <= s_last; sect++)
245                 if (info->protect[sect])
246                         prot++;
247         if (prot)
248                 printf ("- can't erase %d protected sectors\n", prot);
249         return prot;
250 }
251
252 /*-----------------------------------------------------------------------
253  */
254 int flash_check_erase_amd (ulong start)
255 {
256         __u16 v1, v2;
257
258         v1 = *(volatile __u16 *) (start);
259         v2 = *(volatile __u16 *) (start);
260
261         if (((v1 ^ v2) & toggling_bits) == toggling_bits) {
262                 if (((v1 | v2) & DQ5) == DQ5) {
263                         printf ("[DQ5] ");
264                         /* OOPS: exceeded timing limits */
265
266                         v1 = *(volatile __u16 *) (start);
267                         v2 = *(volatile __u16 *) (start);
268
269                         if (((v1 ^ v2) & toggling_bits) == toggling_bits) {
270
271                                 printf ("[%s] ",
272                                         ((toggling_bits & (DQ2 | DQ6)) ==
273                                          (DQ2 | DQ6)) ? "DQ2,DQ6" : "DQ6");
274
275                                 /* OOPS: there is an erasure in progress,
276                                  *       try to reset chip */
277                                 *(volatile __u16 *) (start) =
278                                         (__u16) RESET_CMD;
279
280                                 return 1;       /* still busy */
281                         }
282                 }
283                 return 1;       /* still busy */
284         }
285         return 0;               /* be free */
286 }
287
288 /*-----------------------------------------------------------------------
289  */
290 int flash_erase (flash_info_t * info, int s_first, int s_last)
291 {
292         int flag, sect, setup_offset = 0;
293         int rc = ERR_OK;
294         ulong start;
295
296         if (info->flash_id == FLASH_UNKNOWN) {
297                 printf ("- missing\n");
298                 return ERR_UNKNOWN_FLASH_TYPE;
299         }
300
301         if ((s_first < 0) || (s_first > s_last)) {
302                 printf ("- no sectors to erase\n");
303                 return ERR_INVAL;
304         }
305
306         if (flash_check_protection (info, s_first, s_last))
307                 return ERR_PROTECTED;
308
309         switch (info->flash_id & FLASH_VENDMASK) {
310         case FLASH_MAN_FUJ:
311         case FLASH_MAN_AMD:
312                 switch (info->flash_id & FLASH_TYPEMASK) {
313                 case (FLASH_AM160LV | FLASH_AM160B):
314                         setup_offset = UNLOCK_ADDR1;    /* just the adress for setup_cmd differs */
315                 case FLASH_AMDL323B:
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                         flag = disable_interrupts ();
324                         /* Start erase on unprotected sectors */
325                         for (sect = s_first; sect <= s_last && !ctrlc ();
326                              sect++) {
327                                 printf ("Erasing sector %2d ... ", sect);
328
329                                 if (info->protect[sect] == 0) {
330                                         /* not protected */
331                                         /* Write sector erase command sequence */
332                                         WRITE_UNLOCK (info->start[0]);
333                                         *(volatile __u16 *) (info->start[0] +
334                                                              setup_offset) =
335                                                 (__u16) SETUP_CMD;
336                                         WRITE_UNLOCK (info->start[0]);
337                                         *(volatile __u16 *) (info->
338                                                              start[sect]) =
339                                                 (__u16) SECERASE_CMD;
340
341                                         /* wait some time */
342                                         start = get_timer(0);
343                                         while (get_timer(start) < 1000) {
344                                         }
345
346                                         /* arm simple, non interrupt dependent timer */
347                                         start = get_timer(0);
348                                         while (flash_check_erase_amd (info->start[sect])) {
349                                                 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
350                                                         printf ("timeout!\n");
351                                                         /* OOPS: reach timeout,
352                                                          * try to reset chip
353                                                          */
354                                                         *(volatile __u16 *) (info-> start[sect]) = (__u16) RESET_CMD;
355                                                         rc = ERR_TIMOUT;
356                                                         goto outahere_323B;
357                                                 }
358                                         }
359                                         printf ("ok.\n");
360                                 } else {
361                                         printf ("protected!\n");
362                                 }
363                         }
364                         if (ctrlc ())
365                                 printf ("User Interrupt!\n");
366 outahere_323B:
367                         /* allow flash to settle - wait 10 ms */
368                         udelay_masked (10000);
369                         if (flag)
370                                 enable_interrupts ();
371                         return rc;
372                 default:
373                         printf ("- unknown chip type\n");
374                         return ERR_UNKNOWN_FLASH_TYPE;
375                 }
376                 break;
377         default:
378                 printf ("- unknown vendor ");
379                 return ERR_UNKNOWN_FLASH_VENDOR;
380         }
381 }
382
383 /*-----------------------------------------------------------------------
384  */
385 int flash_check_write_amd (ulong dest)
386 {
387         __u16 v1, v2;
388
389         v1 = *(volatile __u16 *) (dest);
390         v2 = *(volatile __u16 *) (dest);
391
392         /* DQ6 toggles during write */
393         if (((v1 ^ v2) & DQ6) == DQ6) {
394                 if (((v1 | v2) & DQ5) == DQ5) {
395                         printf ("[DQ5] @ %08lX\n", dest);
396
397                         /* OOPS: exceeded timing limits,
398                          *       try to reset chip */
399                         *(volatile __u16 *) (dest) = (__u16) RESET_CMD;
400                         return 0;       /* be free */
401                 }
402                 return 1;       /* still busy */
403         }
404
405         return 0;               /* be free */
406 }
407
408 /*-----------------------------------------------------------------------
409  * Copy memory to flash
410  */
411 static int write_word (flash_info_t * info, ulong dest, ushort data)
412 {
413         int rc = ERR_OK;
414         int flag;
415         ulong start;
416
417         /* Check if Flash is (sufficiently) erased */
418         if ((*(__u16 *) (dest) & data) != data)
419                 return ERR_NOT_ERASED;
420
421         /*
422          * Disable interrupts which might cause a timeout
423          * here. Remember that our exception vectors are
424          * at address 0 in the flash, and we don't want a
425          * (ticker) exception to happen while the flash
426          * chip is in programming mode.
427          */
428         flag = disable_interrupts ();
429
430         /* Write program command sequence */
431         WRITE_UNLOCK (info->start[0]);
432
433         /* Flash dependend program seqence */
434         switch (info->flash_id & FLASH_VENDMASK) {
435         case FLASH_MAN_FUJ:
436         case FLASH_MAN_AMD:
437                 switch (info->flash_id & FLASH_TYPEMASK) {
438                 case (FLASH_AM160LV | FLASH_AM160B):
439                         *(volatile __u16 *) (info->start[0] + UNLOCK_ADDR1) =
440                                 (__u16) PROG_CMD;
441                         *(volatile __u16 *) (dest) = (__u16) data;
442                         break;
443                 case FLASH_AMDL323B:
444                         *(volatile __u16 *) (dest) = (__u16) PROG_CMD;
445                         *(volatile __u16 *) (dest) = (__u16) data;
446                         break;
447                 }
448         }
449
450         /* arm simple, non interrupt dependent timer */
451         start = get_timer(0);
452
453         while (flash_check_write_amd (dest)) {
454                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
455                         printf ("timeout! @ %08lX\n", dest);
456                         /* OOPS: reach timeout,
457                          *       try to reset chip */
458                         *(volatile __u16 *) (dest) = (__u16) RESET_CMD;
459
460                         rc = ERR_TIMOUT;
461                         goto outahere_323B;
462                 }
463         }
464
465         /* Check if Flash was (accurately) written */
466         if (*(__u16 *) (dest) != data)
467                 rc = ERR_PROG_ERROR;
468
469 outahere_323B:
470         if (flag)
471                 enable_interrupts ();
472         return rc;
473 }
474
475 /*-----------------------------------------------------------------------
476  * Copy memory to flash.
477  */
478 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
479 {
480         ulong cp, wp;
481         ushort data;
482         int l;
483         int i, rc;
484
485         wp = (addr & ~1);       /* get lower word aligned address */
486
487         /*
488          * handle unaligned start bytes
489          */
490         if ((l = addr - wp) != 0) {
491                 data = 0;
492                 for (i = 0, cp = wp; i < l; ++i, ++cp) {
493                         data = (data >> 8) | (*(uchar *) cp << 8);
494                 }
495                 for (; i < 2 && cnt > 0; ++i) {
496                         data = (data >> 8) | (*src++ << 8);
497                         --cnt;
498                         ++cp;
499                 }
500                 for (; cnt == 0 && i < 2; ++i, ++cp) {
501                         data = (data >> 8) | (*(uchar *) cp << 8);
502                 }
503
504                 if ((rc = write_word (info, wp, data)) != 0) {
505                         return (rc);
506                 }
507                 wp += 2;
508         }
509
510         /*
511          * handle word aligned part
512          */
513         while (cnt >= 2) {
514                 data = *((ushort *) src);
515                 if ((rc = write_word (info, wp, data)) != 0)
516                         return (rc);
517                 src += 2;
518                 wp += 2;
519                 cnt -= 2;
520         }
521
522         if (cnt == 0)
523                 return ERR_OK;
524
525         /*
526          * handle unaligned tail bytes
527          */
528         data = 0;
529         for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
530                 data = (data >> 8) | (*src++ << 8);
531                 --cnt;
532         }
533         for (; i < 2; ++i, ++cp) {
534                 data = (data >> 8) | (*(uchar *) cp << 8);
535         }
536
537         return write_word (info, wp, data);
538 }