]> git.sur5r.net Git - u-boot/blob - cpu/mpc86xx/cpu.c
Merge branch 'mpc86xx'
[u-boot] / 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,tmp;
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 /* This function takes the non-integral cpu:mpx pll ratio
358  * and converts it to an integer that can be used to assign
359  * FPGA register values.
360  * input: strptr i.e. argv[2]
361 */
362
363 ulong strfractoint(uchar *strptr)
364 {
365    int i,j,retval,intarr_len=0, decarr_len=0, mulconst, no_dec=0;
366    ulong intval =0, decval=0;
367    uchar intarr[3], decarr[3];
368
369    /* Assign the integer part to intarr[]
370     * If there is no decimal point i.e.
371     * if the ratio is an integral value
372     * simply create the intarr.
373    */
374    i=0;
375    while(strptr[i] != 46)
376    {
377       if(strptr[i] == 0)
378       {
379          no_dec = 1;
380          break;    /* Break from loop once the end of string is reached */
381       }
382
383       intarr[i] = strptr[i];
384       i++;
385    }
386
387    intarr_len = i; /* Assign length of integer part to intarr_len*/
388    intarr[i] = '\0'; /* */
389
390    if(no_dec)
391    {
392       mulconst=10; /* Currently needed only for single digit corepll ratios */
393       decval = 0;
394    }
395    else
396    {
397       j=0;
398       i++; /* Skipping the decimal point */
399       while ((strptr[i] > 47) && (strptr[i] < 58))
400       {
401          decarr[j] = strptr[i];
402          i++;
403          j++;
404       }
405
406       decarr_len = j;
407       decarr[j] = '\0';
408
409       mulconst=1;
410       for(i=0; i<decarr_len;i++)
411          mulconst = mulconst*10;
412       decval = simple_strtoul(decarr,NULL,10);
413    }
414
415    intval = simple_strtoul(intarr,NULL,10);
416    intval = intval*mulconst;
417
418    retval = intval+decval;
419
420    return retval;
421
422 }
423
424
425 #endif  /* CONFIG_MPC8641HPCN */
426
427
428 /* no generic way to do board reset. simply call soft_reset. */
429 void
430 do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
431 {
432         char cmd;
433         ulong addr, val;
434         ulong corepll;
435
436 #ifdef CFG_RESET_ADDRESS
437         addr = CFG_RESET_ADDRESS;
438 #else
439         /*
440          * note: when CFG_MONITOR_BASE points to a RAM address,
441          * CFG_MONITOR_BASE - sizeof (ulong) is usually a valid
442          * address. Better pick an address known to be invalid on your
443          * system and assign it to CFG_RESET_ADDRESS.
444          */
445         addr = CFG_MONITOR_BASE - sizeof (ulong);
446 #endif
447
448 #ifndef CONFIG_MPC8641HPCN
449
450         /* flush and disable I/D cache */
451         __asm__ __volatile__ ("mfspr    3, 1008"        ::: "r3");
452         __asm__ __volatile__ ("ori      5, 5, 0xcc00"   ::: "r5");
453         __asm__ __volatile__ ("ori      4, 3, 0xc00"    ::: "r4");
454         __asm__ __volatile__ ("andc     5, 3, 5"        ::: "r5");
455         __asm__ __volatile__ ("sync");
456         __asm__ __volatile__ ("mtspr    1008, 4");
457         __asm__ __volatile__ ("isync");
458         __asm__ __volatile__ ("sync");
459         __asm__ __volatile__ ("mtspr    1008, 5");
460         __asm__ __volatile__ ("isync");
461         __asm__ __volatile__ ("sync");
462
463         soft_restart(addr);
464
465 #else /* CONFIG_MPC8641HPCN */
466
467         if (argc > 1) {
468                 cmd = argv[1][1];
469                 switch(cmd) {
470                 case 'f':    /* reset with frequency changed */
471                         if (argc < 5)
472                                 goto my_usage;
473                         read_from_px_regs(0);
474
475                         val = set_px_sysclk(simple_strtoul(argv[2],NULL,10));
476
477                         corepll = strfractoint(argv[3]);
478                         val = val + set_px_corepll(corepll);
479                         val = val + set_px_mpxpll(simple_strtoul(argv[4],
480                                                                  NULL, 10));
481                         if (val == 3) {
482                                 printf("Setting registers VCFGEN0 and VCTL\n");
483                                 read_from_px_regs(1);
484                                 printf("Resetting board with values from VSPEED0, VSPEED1, VCLKH, and VCLKL ....\n");
485                                 set_px_go();
486                         } else
487                                 goto my_usage;
488
489                         while (1); /* Not reached */
490
491                 case 'l':
492                         if (argv[2][1] == 'f') {
493                                 read_from_px_regs(0);
494                                 read_from_px_regs_altbank(0);
495                                 /* reset with frequency changed */
496                                 val = set_px_sysclk(simple_strtoul(argv[3],NULL,10));
497
498                                 corepll = strfractoint(argv[4]);
499                                 val = val + set_px_corepll(corepll);
500                                 val = val + set_px_mpxpll(simple_strtoul(argv[5],NULL,10));
501                                 if (val == 3) {
502                                         printf("Setting registers VCFGEN0, VCFGEN1, VBOOT, and VCTL\n");
503                                         set_altbank();
504                                         read_from_px_regs(1);
505                                         read_from_px_regs_altbank(1);
506                                         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");
507                                         set_px_go_with_watchdog();
508                                 } else
509                                         goto my_usage;
510
511                                 while(1); /* Not reached */
512                         } else {
513                                 /* Reset from next bank without changing frequencies */
514                                 read_from_px_regs(0);
515                                 read_from_px_regs_altbank(0);
516                                 if(argc > 2)
517                                         goto my_usage;
518                                 printf("Setting registers VCFGEN1, VBOOT, and VCTL\n");
519                                 set_altbank();
520                                 read_from_px_regs_altbank(1);
521                                 printf("Enabling watchdog timer on the FPGA and resetting board to boot from the other bank....\n");
522                                 set_px_go_with_watchdog();
523                                 while(1); /* Not reached */
524                         }
525
526                 default:
527                         goto my_usage;
528                 }
529
530 my_usage:
531                 printf("\nUsage: reset cf <SYSCLK freq> <COREPLL ratio> <MPXPLL ratio>\n");
532                 printf("       reset altbank [cf <SYSCLK freq> <COREPLL ratio> <MPXPLL ratio>]\n");
533                 printf("For example:   reset cf 40 2.5 10\n");
534                 printf("See MPC8641HPCN Design Workbook for valid values of command line parameters.\n");
535                 return;
536         } else
537                 out8(PIXIS_BASE+PIXIS_RST,0);
538
539 #endif /* !CONFIG_MPC8641HPCN */
540
541         while(1);       /* not reached */
542 }
543
544
545 /*
546  * Get timebase clock frequency
547  */
548 unsigned long get_tbclk(void)
549 {
550         sys_info_t  sys_info;
551
552         get_sys_info(&sys_info);
553         return (sys_info.freqSystemBus + 3L) / 4L;
554 }
555
556
557 #if defined(CONFIG_WATCHDOG)
558 void
559 watchdog_reset(void)
560 {
561 }
562 #endif  /* CONFIG_WATCHDOG */
563
564
565 #if defined(CONFIG_DDR_ECC)
566 void dma_init(void)
567 {
568         volatile immap_t *immap = (immap_t *)CFG_IMMR;
569         volatile ccsr_dma_t *dma = &immap->im_dma;
570
571         dma->satr0 = 0x00040000;
572         dma->datr0 = 0x00040000;
573         asm("sync; isync");
574         return;
575 }
576
577 uint dma_check(void)
578 {
579         volatile immap_t *immap = (immap_t *)CFG_IMMR;
580         volatile ccsr_dma_t *dma = &immap->im_dma;
581         volatile uint status = dma->sr0;
582
583         /* While the channel is busy, spin */
584         while((status & 4) == 4) {
585                 status = dma->sr0;
586         }
587
588         if (status != 0) {
589                 printf ("DMA Error: status = %x\n", status);
590         }
591         return status;
592 }
593
594 int dma_xfer(void *dest, uint count, void *src)
595 {
596         volatile immap_t *immap = (immap_t *)CFG_IMMR;
597         volatile ccsr_dma_t *dma = &immap->im_dma;
598
599         dma->dar0 = (uint) dest;
600         dma->sar0 = (uint) src;
601         dma->bcr0 = count;
602         dma->mr0 = 0xf000004;
603         asm("sync;isync");
604         dma->mr0 = 0xf000005;
605         asm("sync;isync");
606         return dma_check();
607 }
608
609 #endif  /* CONFIG_DDR_ECC */
610
611
612 #ifdef CONFIG_OF_FLAT_TREE
613 void ft_cpu_setup(void *blob, bd_t *bd)
614 {
615         u32 *p;
616         ulong clock;
617         int len;
618
619         clock = bd->bi_busfreq;
620         p = ft_get_prop(blob, "/cpus/" OF_CPU "/bus-frequency", &len);
621         if (p != NULL)
622                 *p = cpu_to_be32(clock);
623
624         p = ft_get_prop(blob, "/" OF_SOC "/serial@4500/clock-frequency", &len);
625         if (p != NULL)
626                 *p = cpu_to_be32(clock);
627
628         p = ft_get_prop(blob, "/" OF_SOC "/serial@4600/clock-frequency", &len);
629         if (p != NULL)
630                 *p = cpu_to_be32(clock);
631
632 #if defined(CONFIG_MPC86XX_TSEC1)
633         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@24000/address", &len);
634         memcpy(p, bd->bi_enetaddr, 6);
635 #endif
636
637 #if defined(CONFIG_MPC86XX_TSEC2)
638         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@25000/address", &len);
639         memcpy(p, bd->bi_enet1addr, 6);
640 #endif
641
642 #if defined(CONFIG_MPC86XX_TSEC3)
643         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@26000/address", &len);
644         memcpy(p, bd->bi_enet2addr, 6);
645 #endif
646
647 #if defined(CONFIG_MPC86XX_TSEC4)
648         p = ft_get_prop(blob, "/" OF_SOC "/ethernet@27000/address", &len);
649          memcpy(p, bd->bi_enet3addr, 6);
650 #endif
651
652 }
653 #endif