Removed hardcoded MxMR loop value from upmconfig() for MPC85xx.
[oweals/u-boot.git] / cpu / mpc85xx / cpu.c
1 /*
2  * Copyright 2004,2007,2008 Freescale Semiconductor, Inc.
3  * (C) Copyright 2002, 2003 Motorola Inc.
4  * Xianghua Xiao (X.Xiao@motorola.com)
5  *
6  * (C) Copyright 2000
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 #include <config.h>
29 #include <common.h>
30 #include <watchdog.h>
31 #include <command.h>
32 #include <tsec.h>
33 #include <asm/cache.h>
34 #include <asm/io.h>
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 struct cpu_type cpu_type_list [] = {
39         CPU_TYPE_ENTRY(8533, 8533),
40         CPU_TYPE_ENTRY(8533, 8533_E),
41         CPU_TYPE_ENTRY(8536, 8536),
42         CPU_TYPE_ENTRY(8536, 8536_E),
43         CPU_TYPE_ENTRY(8540, 8540),
44         CPU_TYPE_ENTRY(8541, 8541),
45         CPU_TYPE_ENTRY(8541, 8541_E),
46         CPU_TYPE_ENTRY(8543, 8543),
47         CPU_TYPE_ENTRY(8543, 8543_E),
48         CPU_TYPE_ENTRY(8544, 8544),
49         CPU_TYPE_ENTRY(8544, 8544_E),
50         CPU_TYPE_ENTRY(8545, 8545),
51         CPU_TYPE_ENTRY(8545, 8545_E),
52         CPU_TYPE_ENTRY(8547, 8547_E),
53         CPU_TYPE_ENTRY(8548, 8548),
54         CPU_TYPE_ENTRY(8548, 8548_E),
55         CPU_TYPE_ENTRY(8555, 8555),
56         CPU_TYPE_ENTRY(8555, 8555_E),
57         CPU_TYPE_ENTRY(8560, 8560),
58         CPU_TYPE_ENTRY(8567, 8567),
59         CPU_TYPE_ENTRY(8567, 8567_E),
60         CPU_TYPE_ENTRY(8568, 8568),
61         CPU_TYPE_ENTRY(8568, 8568_E),
62         CPU_TYPE_ENTRY(8572, 8572),
63         CPU_TYPE_ENTRY(8572, 8572_E),
64 };
65
66 struct cpu_type *identify_cpu(u32 ver)
67 {
68         int i;
69         for (i = 0; i < ARRAY_SIZE(cpu_type_list); i++)
70                 if (cpu_type_list[i].soc_ver == ver)
71                         return &cpu_type_list[i];
72
73         return NULL;
74 }
75
76 int checkcpu (void)
77 {
78         sys_info_t sysinfo;
79         uint lcrr;              /* local bus clock ratio register */
80         uint clkdiv;            /* clock divider portion of lcrr */
81         uint pvr, svr;
82         uint fam;
83         uint ver;
84         uint major, minor;
85         struct cpu_type *cpu;
86 #ifdef CONFIG_DDR_CLK_FREQ
87         volatile ccsr_gur_t *gur = (void *)(CFG_MPC85xx_GUTS_ADDR);
88         u32 ddr_ratio = ((gur->porpllsr) & 0x00003e00) >> 9;
89 #else
90         u32 ddr_ratio = 0;
91 #endif
92
93         svr = get_svr();
94         ver = SVR_SOC_VER(svr);
95         major = SVR_MAJ(svr);
96 #ifdef CONFIG_MPC8536
97         major &= 0x7; /* the msb of this nibble is a mfg code */
98 #endif
99         minor = SVR_MIN(svr);
100
101         puts("CPU:   ");
102
103         cpu = identify_cpu(ver);
104         if (cpu) {
105                 puts(cpu->name);
106
107                 if (IS_E_PROCESSOR(svr))
108                         puts("E");
109         } else {
110                 puts("Unknown");
111         }
112
113         printf(", Version: %d.%d, (0x%08x)\n", major, minor, svr);
114
115         pvr = get_pvr();
116         fam = PVR_FAM(pvr);
117         ver = PVR_VER(pvr);
118         major = PVR_MAJ(pvr);
119         minor = PVR_MIN(pvr);
120
121         printf("Core:  ");
122         switch (fam) {
123         case PVR_FAM(PVR_85xx):
124             puts("E500");
125             break;
126         default:
127             puts("Unknown");
128             break;
129         }
130         printf(", Version: %d.%d, (0x%08x)\n", major, minor, pvr);
131
132         get_sys_info(&sysinfo);
133
134         puts("Clock Configuration:\n");
135         printf("       CPU:%4lu MHz, ", DIV_ROUND_UP(sysinfo.freqProcessor,1000000));
136         printf("CCB:%4lu MHz,\n", DIV_ROUND_UP(sysinfo.freqSystemBus,1000000));
137
138         switch (ddr_ratio) {
139         case 0x0:
140                 printf("       DDR:%4lu MHz (%lu MT/s data rate), ",
141                 DIV_ROUND_UP(sysinfo.freqDDRBus,2000000), DIV_ROUND_UP(sysinfo.freqDDRBus,1000000));
142                 break;
143         case 0x7:
144                 printf("       DDR:%4lu MHz (%lu MT/s data rate) (Synchronous), ",
145                 DIV_ROUND_UP(sysinfo.freqDDRBus, 2000000), DIV_ROUND_UP(sysinfo.freqDDRBus, 1000000));
146                 break;
147         default:
148                 printf("       DDR:%4lu MHz (%lu MT/s data rate) (Asynchronous), ",
149                 DIV_ROUND_UP(sysinfo.freqDDRBus, 2000000), DIV_ROUND_UP(sysinfo.freqDDRBus,1000000));
150                 break;
151         }
152
153 #if defined(CFG_LBC_LCRR)
154         lcrr = CFG_LBC_LCRR;
155 #else
156         {
157             volatile ccsr_lbc_t *lbc = (void *)(CFG_MPC85xx_LBC_ADDR);
158
159             lcrr = lbc->lcrr;
160         }
161 #endif
162         clkdiv = lcrr & 0x0f;
163         if (clkdiv == 2 || clkdiv == 4 || clkdiv == 8) {
164 #if defined(CONFIG_MPC8548) || defined(CONFIG_MPC8544) || \
165     defined(CONFIG_MPC8572) || defined(CONFIG_MPC8536)
166                 /*
167                  * Yes, the entire PQ38 family use the same
168                  * bit-representation for twice the clock divider values.
169                  */
170                  clkdiv *= 2;
171 #endif
172                 printf("LBC:%4lu MHz\n",
173                        DIV_ROUND_UP(sysinfo.freqSystemBus, 1000000) / clkdiv);
174         } else {
175                 printf("LBC: unknown (lcrr: 0x%08x)\n", lcrr);
176         }
177
178 #ifdef CONFIG_CPM2
179         printf("CPM:   %lu Mhz\n", sysinfo.freqSystemBus / 1000000);
180 #endif
181
182         puts("L1:    D-cache 32 kB enabled\n       I-cache 32 kB enabled\n");
183
184         return 0;
185 }
186
187
188 /* ------------------------------------------------------------------------- */
189
190 int do_reset (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
191 {
192         uint pvr;
193         uint ver;
194         unsigned long val, msr;
195
196         pvr = get_pvr();
197         ver = PVR_VER(pvr);
198
199         if (ver & 1){
200         /* e500 v2 core has reset control register */
201                 volatile unsigned int * rstcr;
202                 rstcr = (volatile unsigned int *)(CFG_IMMR + 0xE00B0);
203                 *rstcr = 0x2;           /* HRESET_REQ */
204                 udelay(100);
205         }
206
207         /*
208          * Fallthrough if the code above failed
209          * Initiate hard reset in debug control register DBCR0
210          * Make sure MSR[DE] = 1
211          */
212
213         msr = mfmsr ();
214         msr |= MSR_DE;
215         mtmsr (msr);
216
217         val = mfspr(DBCR0);
218         val |= 0x70000000;
219         mtspr(DBCR0,val);
220
221         return 1;
222 }
223
224
225 /*
226  * Get timebase clock frequency
227  */
228 unsigned long get_tbclk (void)
229 {
230         return (gd->bus_clk + 4UL)/8UL;
231 }
232
233
234 #if defined(CONFIG_WATCHDOG)
235 void
236 watchdog_reset(void)
237 {
238         int re_enable = disable_interrupts();
239         reset_85xx_watchdog();
240         if (re_enable) enable_interrupts();
241 }
242
243 void
244 reset_85xx_watchdog(void)
245 {
246         /*
247          * Clear TSR(WIS) bit by writing 1
248          */
249         unsigned long val;
250         val = mfspr(SPRN_TSR);
251         val |= TSR_WIS;
252         mtspr(SPRN_TSR, val);
253 }
254 #endif  /* CONFIG_WATCHDOG */
255
256 #if defined(CONFIG_DDR_ECC)
257 void dma_init(void) {
258         volatile ccsr_dma_t *dma = (void *)(CFG_MPC85xx_DMA_ADDR);
259
260         dma->satr0 = 0x02c40000;
261         dma->datr0 = 0x02c40000;
262         dma->sr0 = 0xfffffff; /* clear any errors */
263         asm("sync; isync; msync");
264         return;
265 }
266
267 uint dma_check(void) {
268         volatile ccsr_dma_t *dma = (void *)(CFG_MPC85xx_DMA_ADDR);
269         volatile uint status = dma->sr0;
270
271         /* While the channel is busy, spin */
272         while((status & 4) == 4) {
273                 status = dma->sr0;
274         }
275
276         /* clear MR0[CS] channel start bit */
277         dma->mr0 &= 0x00000001;
278         asm("sync;isync;msync");
279
280         if (status != 0) {
281                 printf ("DMA Error: status = %x\n", status);
282         }
283         return status;
284 }
285
286 int dma_xfer(void *dest, uint count, void *src) {
287         volatile ccsr_dma_t *dma = (void *)(CFG_MPC85xx_DMA_ADDR);
288
289         dma->dar0 = (uint) dest;
290         dma->sar0 = (uint) src;
291         dma->bcr0 = count;
292         dma->mr0 = 0xf000004;
293         asm("sync;isync;msync");
294         dma->mr0 = 0xf000005;
295         asm("sync;isync;msync");
296         return dma_check();
297 }
298 #endif
299
300 /*
301  * Configures a UPM. The function requires the respective MxMR to be set
302  * before calling this function. "size" is the number or entries, not a sizeof.
303  */
304 void upmconfig (uint upm, uint * table, uint size)
305 {
306         int i, mdr, mad, old_mad = 0;
307         volatile u32 *mxmr;
308         volatile ccsr_lbc_t *lbc = (void *)(CFG_MPC85xx_LBC_ADDR);
309         volatile u32 *brp,*orp;
310         volatile u8* dummy = NULL;
311         int upmmask;
312
313         switch (upm) {
314         case UPMA:
315                 mxmr = &lbc->mamr;
316                 upmmask = BR_MS_UPMA;
317                 break;
318         case UPMB:
319                 mxmr = &lbc->mbmr;
320                 upmmask = BR_MS_UPMB;
321                 break;
322         case UPMC:
323                 mxmr = &lbc->mcmr;
324                 upmmask = BR_MS_UPMC;
325                 break;
326         default:
327                 printf("%s: Bad UPM index %d to configure\n", __FUNCTION__, upm);
328                 hang();
329         }
330
331         /* Find the address for the dummy write transaction */
332         for (brp = &lbc->br0, orp = &lbc->or0, i = 0; i < 8;
333                  i++, brp += 2, orp += 2) {
334
335                 /* Look for a valid BR with selected UPM */
336                 if ((in_be32(brp) & (BR_V | BR_MSEL)) == (BR_V | upmmask)) {
337                         dummy = (volatile u8*)(in_be32(brp) & BR_BA);
338                         break;
339                 }
340         }
341
342         if (i == 8) {
343                 printf("Error: %s() could not find matching BR\n", __FUNCTION__);
344                 hang();
345         }
346
347         for (i = 0; i < size; i++) {
348                 /* 1 */
349                 out_be32(mxmr,  (in_be32(mxmr) & 0x4fffffc0) | MxMR_OP_WARR | i);
350                 /* 2 */
351                 out_be32(&lbc->mdr, table[i]);
352                 /* 3 */
353                 mdr = in_be32(&lbc->mdr);
354                 /* 4 */
355                 *(volatile u8 *)dummy = 0;
356                 /* 5 */
357                 do {
358                         mad = in_be32(mxmr) & MxMR_MAD_MSK;
359                 } while (mad <= old_mad && !(!mad && i == (size-1)));
360                 old_mad = mad;
361         }
362         out_be32(mxmr, (in_be32(mxmr) & 0x4fffffc0) | MxMR_OP_NORM);
363 }
364
365
366 /*
367  * Initializes on-chip ethernet controllers.
368  * to override, implement board_eth_init()
369  */
370 int cpu_eth_init(bd_t *bis)
371 {
372 #if defined(CONFIG_TSEC_ENET) || defined(CONFIG_MPC85xx_FEC)
373         tsec_standard_init(bis);
374 #endif
375
376         return 0;
377 }