common: Move checkcpu() out of common.h
[platform/kernel/u-boot.git] / arch / powerpc / cpu / mpc8xx / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2002
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 /*
8  * m8xx.c
9  *
10  * CPU specific code
11  *
12  * written or collected and sometimes rewritten by
13  * Magnus Damm <damm@bitsmart.com>
14  *
15  * minor modifications by
16  * Wolfgang Denk <wd@denx.de>
17  */
18
19 #include <common.h>
20 #include <cpu_func.h>
21 #include <vsprintf.h>
22 #include <watchdog.h>
23 #include <command.h>
24 #include <mpc8xx.h>
25 #include <netdev.h>
26 #include <asm/cache.h>
27 #include <asm/cpm_8xx.h>
28 #include <linux/compiler.h>
29 #include <asm/io.h>
30
31 #if defined(CONFIG_OF_LIBFDT)
32 #include <linux/libfdt.h>
33 #include <fdt_support.h>
34 #endif
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 static int check_CPU(long clock, uint pvr, uint immr)
39 {
40         immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
41         uint k;
42         char buf[32];
43
44         /* the highest 16 bits should be 0x0050 for a 860 */
45
46         if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
47                 return -1;
48
49         k = (immr << 16) |
50             in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
51
52         /*
53          * Some boards use sockets so different CPUs can be used.
54          * We have to check chip version in run time.
55          */
56         switch (k) {
57                 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
58         case 0x08010004:                /* Rev. A.0 */
59                 printf("MPC866xxxZPnnA");
60                 break;
61         case 0x08000003:                /* Rev. 0.3 */
62                 printf("MPC866xxxZPnn");
63                 break;
64         case 0x09000000:                /* 870/875/880/885 */
65                 puts("MPC885ZPnn");
66                 break;
67
68         default:
69                 printf("unknown MPC86x (0x%08x)", k);
70                 break;
71         }
72
73         printf(" at %s MHz: ", strmhz(buf, clock));
74
75         print_size(checkicache(), " I-Cache ");
76         print_size(checkdcache(), " D-Cache");
77
78         /* do we have a FEC (860T/P or 852/859/866/885)? */
79
80         out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
81         if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
82                 printf(" FEC present");
83
84         putc('\n');
85
86         return 0;
87 }
88
89 /* ------------------------------------------------------------------------- */
90
91 int checkcpu(void)
92 {
93         ulong clock = gd->cpu_clk;
94         uint immr = get_immr(); /* Return full IMMR contents */
95         uint pvr = get_pvr();
96
97         puts("CPU:   ");
98
99         return check_CPU(clock, pvr, immr);
100 }
101
102 /* ------------------------------------------------------------------------- */
103 /* L1 i-cache                                                                */
104
105 int checkicache(void)
106 {
107         immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
108         memctl8xx_t __iomem *memctl = &immap->im_memctl;
109         u32 cacheon = rd_ic_cst() & IDC_ENABLED;
110         /* probe in flash memoryarea */
111         u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
112         u32 m;
113         u32 lines = -1;
114
115         wr_ic_cst(IDC_UNALL);
116         wr_ic_cst(IDC_INVALL);
117         wr_ic_cst(IDC_DISABLE);
118         __asm__ volatile ("isync");
119
120         while (!((m = rd_ic_cst()) & IDC_CERR2)) {
121                 wr_ic_adr(k);
122                 wr_ic_cst(IDC_LDLCK);
123                 __asm__ volatile ("isync");
124
125                 lines++;
126                 k += 0x10;      /* the number of bytes in a cacheline */
127         }
128
129         wr_ic_cst(IDC_UNALL);
130         wr_ic_cst(IDC_INVALL);
131
132         if (cacheon)
133                 wr_ic_cst(IDC_ENABLE);
134         else
135                 wr_ic_cst(IDC_DISABLE);
136
137         __asm__ volatile ("isync");
138
139         return lines << 4;
140 };
141
142 /* ------------------------------------------------------------------------- */
143 /* L1 d-cache                                                                */
144 /* call with cache disabled                                                  */
145
146 int checkdcache(void)
147 {
148         immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
149         memctl8xx_t __iomem *memctl = &immap->im_memctl;
150         u32 cacheon = rd_dc_cst() & IDC_ENABLED;
151         /* probe in flash memoryarea */
152         u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
153         u32 m;
154         u32 lines = -1;
155
156         wr_dc_cst(IDC_UNALL);
157         wr_dc_cst(IDC_INVALL);
158         wr_dc_cst(IDC_DISABLE);
159
160         while (!((m = rd_dc_cst()) & IDC_CERR2)) {
161                 wr_dc_adr(k);
162                 wr_dc_cst(IDC_LDLCK);
163                 lines++;
164                 k += 0x10;      /* the number of bytes in a cacheline */
165         }
166
167         wr_dc_cst(IDC_UNALL);
168         wr_dc_cst(IDC_INVALL);
169
170         if (cacheon)
171                 wr_dc_cst(IDC_ENABLE);
172         else
173                 wr_dc_cst(IDC_DISABLE);
174
175         return lines << 4;
176 };
177
178 /* ------------------------------------------------------------------------- */
179
180 void upmconfig(uint upm, uint *table, uint size)
181 {
182         uint i;
183         uint addr = 0;
184         immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
185         memctl8xx_t __iomem *memctl = &immap->im_memctl;
186
187         for (i = 0; i < size; i++) {
188                 out_be32(&memctl->memc_mdr, table[i]);          /* (16-15) */
189                 out_be32(&memctl->memc_mcr, addr | upm);        /* (16-16) */
190                 addr++;
191         }
192 }
193
194 /* ------------------------------------------------------------------------- */
195
196 int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
197 {
198         ulong msr, addr;
199
200         immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
201
202         /* Checkstop Reset enable */
203         setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
204
205         /* Interrupts and MMU off */
206         __asm__ volatile ("mtspr    81, 0");
207         __asm__ volatile ("mfmsr    %0" : "=r" (msr));
208
209         msr &= ~0x1030;
210         __asm__ volatile ("mtmsr    %0" : : "r" (msr));
211
212         /*
213          * Trying to execute the next instruction at a non-existing address
214          * should cause a machine check, resulting in reset
215          */
216 #ifdef CONFIG_SYS_RESET_ADDRESS
217         addr = CONFIG_SYS_RESET_ADDRESS;
218 #else
219         /*
220          * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address,
221          * CONFIG_SYS_MONITOR_BASE - sizeof (ulong) is usually a valid address.
222          * Better pick an address known to be invalid on your system and assign
223          * it to CONFIG_SYS_RESET_ADDRESS.
224          * "(ulong)-1" used to be a good choice for many systems...
225          */
226         addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
227 #endif
228         ((void (*)(void)) addr)();
229         return 1;
230 }
231
232 /* ------------------------------------------------------------------------- */
233
234 /*
235  * Get timebase clock frequency (like cpu_clk in Hz)
236  *
237  * See sections 14.2 and 14.6 of the User's Manual
238  */
239 unsigned long get_tbclk(void)
240 {
241         immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
242         ulong oscclk, factor, pll;
243
244         if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
245                 return gd->cpu_clk / 16;
246
247         pll = in_be32(&immap->im_clkrst.car_plprcr);
248
249 #define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
250
251         /*
252          * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
253          * factor is calculated as follows:
254          *
255          *                   MFN
256          *           MFI + -------
257          *                 MFD + 1
258          * factor =  -----------------
259          *           (PDF + 1) * 2^S
260          *
261          */
262         factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
263                  (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
264
265         oscclk = gd->cpu_clk / factor;
266
267         if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
268             factor > 2)
269                 return oscclk / 4;
270
271         return oscclk / 16;
272 }
273
274 /*
275  * Initializes on-chip ethernet controllers.
276  * to override, implement board_eth_init()
277  */
278 int cpu_eth_init(bd_t *bis)
279 {
280 #if defined(CONFIG_MPC8XX_FEC)
281         fec_initialize(bis);
282 #endif
283         return 0;
284 }