Improve "reset" command's interaction with watchdog.
[oweals/u-boot.git] / cpu / mpc86xx / cpu.c
1 /*
2  * Copyright 2004 Freescale Semiconductor
3  * Jeff Brown (jeffrey@freescale.com)
4  * Srikanth Srinivasan (srikanth.srinivasan@freescale.com)
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26 #include <watchdog.h>
27 #include <command.h>
28 #include <asm/cache.h>
29 #include <mpc86xx.h>
30
31 #if defined(CONFIG_OF_FLAT_TREE)
32 #include <ft_build.h>
33 #endif
34
35 extern unsigned long get_board_sys_clk(ulong dummy);
36
37
38 static __inline__ unsigned long get_dbat3u (void)
39 {
40         unsigned long dbat3u;
41         asm volatile("mfspr %0, 542" : "=r" (dbat3u) :);
42         return dbat3u;
43 }
44
45 static __inline__ unsigned long get_dbat3l (void)
46 {
47         unsigned long dbat3l;
48         asm volatile("mfspr %0, 543" : "=r" (dbat3l) :);
49         return dbat3l;
50 }
51
52 static __inline__ unsigned long get_msr (void)
53 {
54         unsigned long msr;
55         asm volatile("mfmsr %0" : "=r" (msr) :);
56         return msr;
57 }
58
59
60 int checkcpu (void)
61 {
62         sys_info_t sysinfo;
63         uint pvr, svr;
64         uint ver;
65         uint major, minor;
66         uint lcrr;              /* local bus clock ratio register */
67         uint clkdiv;            /* clock divider portion of lcrr */
68
69         puts("Freescale PowerPC\n");
70
71         pvr = get_pvr();
72         ver = PVR_VER(pvr);
73         major = PVR_MAJ(pvr);
74         minor = PVR_MIN(pvr);
75
76         puts("CPU:\n");
77
78         printf("    Core: ");
79
80         switch (ver) {
81         case PVR_VER(PVR_86xx):
82             puts("E600");
83             break;
84         default:
85             puts("Unknown");
86             break;
87         }
88         printf(", Version: %d.%d, (0x%08x)\n", major, minor, pvr);
89
90         svr = get_svr();
91         ver = SVR_VER(svr);
92         major = SVR_MAJ(svr);
93         minor = SVR_MIN(svr);
94
95         puts("    System: ");
96         switch (ver) {
97         case SVR_8641:
98                 puts("8641");
99                 break;
100         case SVR_8641D:
101                 puts("8641D");
102                 break;
103         default:
104                 puts("Unknown");
105                 break;
106         }
107         printf(", Version: %d.%d, (0x%08x)\n", major, minor, svr);
108
109         get_sys_info(&sysinfo);
110
111         puts("    Clocks: ");
112         printf("CPU:%4lu MHz, ", sysinfo.freqProcessor / 1000000);
113         printf("MPX:%4lu MHz, ", sysinfo.freqSystemBus / 1000000);
114         printf("DDR:%4lu MHz, ", sysinfo.freqSystemBus / 2000000);
115
116 #if defined(CFG_LBC_LCRR)
117         lcrr = CFG_LBC_LCRR;
118 #else
119         {
120             volatile immap_t *immap = (immap_t *)CFG_IMMR;
121             volatile ccsr_lbc_t *lbc= &immap->im_lbc;
122
123             lcrr = lbc->lcrr;
124         }
125 #endif
126         clkdiv = lcrr & 0x0f;
127         if (clkdiv == 2 || clkdiv == 4 || clkdiv == 8) {
128                 printf("LBC:%4lu MHz\n",
129                        sysinfo.freqSystemBus / 1000000 / clkdiv);
130         } else {
131                 printf("    LBC: unknown (lcrr: 0x%08x)\n", lcrr);
132         }
133
134         printf("    L2: ");
135         if (get_l2cr() & 0x80000000)
136                 printf("Enabled\n");
137         else
138                 printf("Disabled\n");
139
140         return 0;
141 }
142
143
144 /* -------------------------------------------------------------------- */
145
146 static inline void
147 soft_restart(unsigned long addr)
148 {
149
150 #ifndef CONFIG_MPC8641HPCN
151
152         /* SRR0 has system reset vector, SRR1 has default MSR value */
153         /* rfi restores MSR from SRR1 and sets the PC to the SRR0 value */
154
155         __asm__ __volatile__ ("mtspr    26, %0"         :: "r" (addr));
156         __asm__ __volatile__ ("li       4, (1 << 6)"    ::: "r4");
157         __asm__ __volatile__ ("mtspr    27, 4");
158         __asm__ __volatile__ ("rfi");
159
160 #else /* CONFIG_MPC8641HPCN */
161         out8(PIXIS_BASE+PIXIS_RST,0);
162 #endif /* !CONFIG_MPC8641HPCN */
163         while(1);       /* not reached */
164 }
165
166
167
168 #ifdef CONFIG_MPC8641HPCN
169
170 int set_px_sysclk(ulong sysclk)
171 {
172          u8 sysclk_s, sysclk_r, sysclk_v, vclkh, vclkl, sysclk_aux;
173
174          /* Per table 27, page 58 of MPC8641HPCN spec*/
175          switch(sysclk)
176          {
177             case 33:
178                sysclk_s = 0x04;
179                sysclk_r = 0x04;
180                sysclk_v = 0x07;
181                sysclk_aux = 0x00;
182                break;
183             case 40:
184                sysclk_s = 0x01;
185                sysclk_r = 0x1F;
186                sysclk_v = 0x20;
187                sysclk_aux = 0x01;
188                break;
189             case 50:
190                sysclk_s = 0x01;
191                sysclk_r = 0x1F;
192                sysclk_v = 0x2A;
193                sysclk_aux = 0x02;
194                break;
195             case 66:
196                sysclk_s = 0x01;
197                sysclk_r = 0x04;
198                sysclk_v = 0x04;
199                sysclk_aux = 0x03;
200                break;
201             case 83:
202                sysclk_s = 0x01;
203                sysclk_r = 0x1F;
204                sysclk_v = 0x4B;
205                sysclk_aux = 0x04;
206                break;
207             case 100:
208                sysclk_s = 0x01;
209                sysclk_r = 0x1F;
210                sysclk_v = 0x5C;
211                sysclk_aux = 0x05;
212                break;
213             case 134:
214                sysclk_s = 0x06;
215                sysclk_r = 0x1F;
216                sysclk_v = 0x3B;
217               sysclk_aux = 0x06;
218                break;
219             case 166:
220                sysclk_s = 0x06;
221                sysclk_r = 0x1F;
222                sysclk_v = 0x4B;
223                sysclk_aux = 0x07;
224                break;
225             default:
226                printf("Unsupported SYSCLK frequency.\n");
227                return 0;
228          }
229
230          vclkh = (sysclk_s << 5) | sysclk_r ;
231          vclkl = sysclk_v;
232          out8(PIXIS_BASE+PIXIS_VCLKH,vclkh);
233          out8(PIXIS_BASE+PIXIS_VCLKL,vclkl);
234
235          out8(PIXIS_BASE+PIXIS_AUX,sysclk_aux);
236
237          return 1;
238 }
239
240 int set_px_mpxpll(ulong mpxpll)
241 {
242          u8 tmp;
243          u8 val;
244          switch(mpxpll)
245          {
246             case 2:
247             case 4:
248             case 6:
249             case 8:
250             case 10:
251             case 12:
252             case 14:
253             case 16:
254                val = (u8)mpxpll;
255                break;
256             default:
257                printf("Unsupported MPXPLL ratio.\n");
258                return 0;
259          }
260
261          tmp = in8(PIXIS_BASE+PIXIS_VSPEED1);
262          tmp = (tmp & 0xF0) | (val & 0x0F);
263          out8(PIXIS_BASE+PIXIS_VSPEED1,tmp);
264
265          return 1;
266 }
267
268 int set_px_corepll(ulong corepll)
269 {
270          u8 tmp;
271          u8 val;
272
273          switch ((int)corepll) {
274             case 20:
275                val = 0x08;
276                break;
277             case 25:
278                val = 0x0C;
279                break;
280             case 30:
281                val = 0x10;
282                break;
283             case 35:
284                val = 0x1C;
285                break;
286             case 40:
287                val = 0x14;
288                break;
289             case 45:
290                val = 0x0E;
291                break;
292             default:
293                printf("Unsupported COREPLL ratio.\n");
294                return 0;
295          }
296
297          tmp = in8(PIXIS_BASE+PIXIS_VSPEED0);
298          tmp = (tmp & 0xE0) | (val & 0x1F);
299          out8(PIXIS_BASE+PIXIS_VSPEED0,tmp);
300
301          return 1;
302 }
303
304 void read_from_px_regs(int set)
305 {
306          u8 tmp, mask = 0x1C;
307          tmp = in8(PIXIS_BASE+PIXIS_VCFGEN0);
308          if (set)
309             tmp = tmp | mask;
310          else
311             tmp = tmp & ~mask;
312          out8(PIXIS_BASE+PIXIS_VCFGEN0,tmp);
313 }
314
315 void read_from_px_regs_altbank(int set)
316 {
317          u8 tmp, mask = 0x04;
318          tmp = in8(PIXIS_BASE+PIXIS_VCFGEN1);
319          if (set)
320             tmp = tmp | mask;
321          else
322             tmp = tmp & ~mask;
323          out8(PIXIS_BASE+PIXIS_VCFGEN1,tmp);
324 }
325
326 void set_altbank(void)
327 {
328          u8 tmp;
329          tmp = in8(PIXIS_BASE+PIXIS_VBOOT);
330          tmp ^= 0x40;
331          out8(PIXIS_BASE+PIXIS_VBOOT,tmp);
332  }
333
334
335 void set_px_go(void)
336 {
337          u8 tmp;
338          tmp = in8(PIXIS_BASE+PIXIS_VCTL);
339          tmp = tmp & 0x1E;
340          out8(PIXIS_BASE+PIXIS_VCTL,tmp);
341          tmp = in8(PIXIS_BASE+PIXIS_VCTL);
342          tmp = tmp | 0x01;
343          out8(PIXIS_BASE+PIXIS_VCTL,tmp);
344 }
345
346 void set_px_go_with_watchdog(void)
347 {
348          u8 tmp;
349          tmp = in8(PIXIS_BASE+PIXIS_VCTL);
350          tmp = tmp & 0x1E;
351          out8(PIXIS_BASE+PIXIS_VCTL,tmp);
352          tmp = in8(PIXIS_BASE+PIXIS_VCTL);
353          tmp = tmp | 0x09;
354          out8(PIXIS_BASE+PIXIS_VCTL,tmp);
355 }
356
357 int disable_watchdog(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
358 {
359         u8 tmp;
360         tmp = in8(PIXIS_BASE+PIXIS_VCTL);
361         tmp = tmp & 0x1E;
362         out8(PIXIS_BASE+PIXIS_VCTL,tmp);
363         tmp = in8(PIXIS_BASE + PIXIS_VCTL);
364         tmp &= ~ 0x08; /* setting VCTL[WDEN] to 0 to disable watch dog */
365         out8(PIXIS_BASE + PIXIS_VCTL, tmp);
366         return 0;
367 }
368
369 U_BOOT_CMD(
370         diswd, 1, 0, disable_watchdog,
371         "diswd  - Disable watchdog timer \n",
372         NULL
373 );
374
375 /* This function takes the non-integral cpu:mpx pll ratio
376  * and converts it to an integer that can be used to assign
377  * FPGA register values.
378  * input: strptr i.e. argv[2]
379 */
380
381 ulong strfractoint(uchar *strptr)
382 {
383    int i,j,retval,intarr_len=0, decarr_len=0, mulconst, no_dec=0;
384    ulong intval =0, decval=0;
385    uchar intarr[3], decarr[3];
386
387    /* Assign the integer part to intarr[]
388     * If there is no decimal point i.e.
389     * if the ratio is an integral value
390     * simply create the intarr.
391    */
392    i=0;
393    while(strptr[i] != 46)
394    {
395       if(strptr[i] == 0)
396       {
397          no_dec = 1;
398          break;    /* Break from loop once the end of string is reached */
399       }
400
401       intarr[i] = strptr[i];
402       i++;
403    }
404
405    intarr_len = i; /* Assign length of integer part to intarr_len*/
406    intarr[i] = '\0'; /* */
407
408    if(no_dec)
409    {
410       mulconst=10; /* Currently needed only for single digit corepll ratios */
411       decval = 0;
412    }
413    else
414    {
415       j=0;
416       i++; /* Skipping the decimal point */
417       while ((strptr[i] > 47) && (strptr[i] < 58))
418       {
419          decarr[j] = strptr[i];
420          i++;
421          j++;
422       }
423
424       decarr_len = j;
425       decarr[j] = '\0';
426
427       mulconst=1;
428       for(i=0; i<decarr_len;i++)
429          mulconst = mulconst*10;
430       decval = simple_strtoul(decarr,NULL,10);
431    }
432
433    intval = simple_strtoul(intarr,NULL,10);
434    intval = intval*mulconst;
435
436    retval = intval+decval;
437
438    return retval;
439
440 }
441
442
443 #endif  /* CONFIG_MPC8641HPCN */
444
445
446 /* no generic way to do board reset. simply call soft_reset. */
447 void
448 do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
449 {
450         char cmd;
451         ulong addr, val;
452         ulong corepll;
453
454 #ifdef CFG_RESET_ADDRESS
455         addr = CFG_RESET_ADDRESS;
456 #else
457         /*
458          * note: when CFG_MONITOR_BASE points to a RAM address,
459          * CFG_MONITOR_BASE - sizeof (ulong) is usually a valid
460          * address. Better pick an address known to be invalid on your
461          * system and assign it to CFG_RESET_ADDRESS.
462          */
463         addr = CFG_MONITOR_BASE - sizeof (ulong);
464 #endif
465
466 #ifndef CONFIG_MPC8641HPCN
467
468         /* flush and disable I/D cache */
469         __asm__ __volatile__ ("mfspr    3, 1008"        ::: "r3");
470         __asm__ __volatile__ ("ori      5, 5, 0xcc00"   ::: "r5");
471         __asm__ __volatile__ ("ori      4, 3, 0xc00"    ::: "r4");
472         __asm__ __volatile__ ("andc     5, 3, 5"        ::: "r5");
473         __asm__ __volatile__ ("sync");
474         __asm__ __volatile__ ("mtspr    1008, 4");
475         __asm__ __volatile__ ("isync");
476         __asm__ __volatile__ ("sync");
477         __asm__ __volatile__ ("mtspr    1008, 5");
478         __asm__ __volatile__ ("isync");
479         __asm__ __volatile__ ("sync");
480
481         soft_restart(addr);
482
483 #else /* CONFIG_MPC8641HPCN */
484
485         if (argc > 1) {
486                 cmd = argv[1][1];
487                 switch(cmd) {
488                 case 'f':    /* reset with frequency changed */
489                         if (argc < 5)
490                                 goto my_usage;
491                         read_from_px_regs(0);
492
493                         val = set_px_sysclk(simple_strtoul(argv[2],NULL,10));
494
495                         corepll = strfractoint(argv[3]);
496                         val = val + set_px_corepll(corepll);
497                         val = val + set_px_mpxpll(simple_strtoul(argv[4],
498                                                                  NULL, 10));
499                         if (val == 3) {
500                                 printf("Setting registers VCFGEN0 and VCTL\n");
501                                 read_from_px_regs(1);
502                                 printf("Resetting board with values from VSPEED0, VSPEED1, VCLKH, and VCLKL ....\n");
503                                 set_px_go();
504                         } else
505                                 goto my_usage;
506
507                         while (1); /* Not reached */
508
509                 case 'l':
510                         if (argv[2][1] == 'f') {
511                                 read_from_px_regs(0);
512                                 read_from_px_regs_altbank(0);
513                                 /* reset with frequency changed */
514                                 val = set_px_sysclk(simple_strtoul(argv[3],NULL,10));
515
516                                 corepll = strfractoint(argv[4]);
517                                 val = val + set_px_corepll(corepll);
518                                 val = val + set_px_mpxpll(simple_strtoul(argv[5],NULL,10));
519                                 if (val == 3) {
520                                         printf("Setting registers VCFGEN0, VCFGEN1, VBOOT, and VCTL\n");
521                                         set_altbank();
522                                         read_from_px_regs(1);
523                                         read_from_px_regs_altbank(1);
524                                         printf("Enabling watchdog timer on the FPGA and resetting board with values from VSPEED0, VSPEED1, VCLKH, and VCLKL to boot from the other bank ....\n");
525                                         set_px_go_with_watchdog();
526                                 } else
527                                         goto my_usage;
528
529                                 while(1); /* Not reached */
530                         } else if(argv[2][1] == 'd'){
531                                 /* Reset from next bank without changing frequencies but with watchdog timer enabled */
532                                 read_from_px_regs(0);
533                                 read_from_px_regs_altbank(0);
534                                 printf("Setting registers VCFGEN1, VBOOT, and VCTL\n");
535                                 set_altbank();
536                                 read_from_px_regs_altbank(1);
537                                 printf("Enabling watchdog timer on the FPGA and resetting board to boot from the other bank....\n");
538                                 set_px_go_with_watchdog();
539                                 while(1); /* Not reached */
540                         } else {
541                                 /* Reset from next bank without changing frequency and without watchdog timer enabled */
542                                 read_from_px_regs(0);
543                                 read_from_px_regs_altbank(0);
544                                 if(argc > 2)
545                                         goto my_usage;
546                                 printf("Setting registers VCFGNE1, VBOOT, and VCTL\n");
547                                 set_altbank();
548                                 read_from_px_regs_altbank(1);
549                                 printf("Resetting board to boot from the other bank....\n");
550                                 set_px_go();
551                         }
552
553                 default:
554                         goto my_usage;
555                 }
556
557 my_usage:
558                 printf("\nUsage: reset cf <SYSCLK freq> <COREPLL ratio> <MPXPLL ratio>\n");
559                 printf("       reset altbank [cf <SYSCLK freq> <COREPLL ratio> <MPXPLL ratio>]\n");
560                 printf("For example:   reset cf 40 2.5 10\n");
561                 printf("See MPC8641HPCN Design Workbook for valid values of command line parameters.\n");
562                 return;
563         } else
564                 out8(PIXIS_BASE+PIXIS_RST,0);
565
566 #endif /* !CONFIG_MPC8641HPCN */
567
568         while(1);       /* not reached */
569 }
570
571
572 /*
573  * Get timebase clock frequency
574  */
575 unsigned long get_tbclk(void)
576 {
577         sys_info_t  sys_info;
578
579         get_sys_info(&sys_info);
580         return (sys_info.freqSystemBus + 3L) / 4L;
581 }
582
583
584 #if defined(CONFIG_WATCHDOG)
585 void
586 watchdog_reset(void)
587 {
588 }
589 #endif  /* CONFIG_WATCHDOG */
590
591
592 #if defined(CONFIG_DDR_ECC)
593 void dma_init(void)
594 {
595         volatile immap_t *immap = (immap_t *)CFG_IMMR;
596         volatile ccsr_dma_t *dma = &immap->im_dma;
597
598         dma->satr0 = 0x00040000;
599         dma->datr0 = 0x00040000;
600         asm("sync; isync");
601         return;
602 }
603
604 uint dma_check(void)
605 {
606         volatile immap_t *immap = (immap_t *)CFG_IMMR;
607         volatile ccsr_dma_t *dma = &immap->im_dma;
608         volatile uint status = dma->sr0;
609
610         /* While the channel is busy, spin */
611         while((status & 4) == 4) {
612                 status = dma->sr0;
613         }
614
615         if (status != 0) {
616                 printf ("DMA Error: status = %x\n", status);
617         }
618         return status;
619 }
620
621 int dma_xfer(void *dest, uint count, void *src)
622 {
623         volatile immap_t *immap = (immap_t *)CFG_IMMR;
624         volatile ccsr_dma_t *dma = &immap->im_dma;
625
626         dma->dar0 = (uint) dest;
627         dma->sar0 = (uint) src;
628         dma->bcr0 = count;
629         dma->mr0 = 0xf000004;
630         asm("sync;isync");
631         dma->mr0 = 0xf000005;
632         asm("sync;isync");
633         return dma_check();
634 }
635
636 #endif  /* CONFIG_DDR_ECC */
637
638
639 #ifdef CONFIG_OF_FLAT_TREE
640 void ft_cpu_setup(void *blob, bd_t *bd)
641 {
642         u32 *p;
643         ulong clock;
644         int len;
645
646         clock = bd->bi_busfreq;
647         p = ft_get_prop(blob, "/cpus/" OF_CPU "/bus-frequency", &len);
648         if (p != NULL)
649                 *p = cpu_to_be32(clock);
650
651         p = ft_get_prop(blob, "/" OF_SOC "/serial@4500/clock-frequency", &len);
652         if (p != NULL)
653                 *p = cpu_to_be32(clock);
654
655         p = ft_get_prop(blob, "/" OF_SOC "/serial@4600/clock-frequency", &len);
656         if (p != NULL)
657                 *p = cpu_to_be32(clock);
658
659 #if defined(CONFIG_MPC86XX_TSEC1)
660         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@24000/address", &len);
661         memcpy(p, bd->bi_enetaddr, 6);
662 #endif
663
664 #if defined(CONFIG_MPC86XX_TSEC2)
665         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@25000/address", &len);
666         memcpy(p, bd->bi_enet1addr, 6);
667 #endif
668
669 #if defined(CONFIG_MPC86XX_TSEC3)
670         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@26000/address", &len);
671         memcpy(p, bd->bi_enet2addr, 6);
672 #endif
673
674 #if defined(CONFIG_MPC86XX_TSEC4)
675         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@27000/address", &len);
676          memcpy(p, bd->bi_enet3addr, 6);
677 #endif
678
679 }
680 #endif