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