blob: 0604433e7265dc7f7801797126079975ae4ddc82 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christophe Leroy069fa832017-07-06 10:23:22 +02002/*
3 * (C) Copyright 2000-2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Christophe Leroy069fa832017-07-06 10:23:22 +02005 */
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>
Simon Glass1fa70f82019-11-14 12:57:34 -070020#include <cpu_func.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070021#include <vsprintf.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020022#include <watchdog.h>
23#include <command.h>
24#include <mpc8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020025#include <netdev.h>
26#include <asm/cache.h>
Christophe Leroy10ff63a2018-03-16 17:20:43 +010027#include <asm/cpm_8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020028#include <linux/compiler.h>
29#include <asm/io.h>
30
31#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090032#include <linux/libfdt.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020033#include <fdt_support.h>
34#endif
35
36DECLARE_GLOBAL_DATA_PTR;
37
Christophe Leroy069fa832017-07-06 10:23:22 +020038/* ------------------------------------------------------------------------- */
39/* L1 i-cache */
40
Christophe Leroy48f896d2017-07-06 10:33:17 +020041int checkicache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020042{
Christophe Leroy394f9b32017-07-06 10:33:13 +020043 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
44 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020045 u32 cacheon = rd_ic_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020046 /* probe in flash memoryarea */
47 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020048 u32 m;
49 u32 lines = -1;
50
Christophe Leroy48f896d2017-07-06 10:33:17 +020051 wr_ic_cst(IDC_UNALL);
52 wr_ic_cst(IDC_INVALL);
53 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020054 __asm__ volatile ("isync");
55
Christophe Leroy48f896d2017-07-06 10:33:17 +020056 while (!((m = rd_ic_cst()) & IDC_CERR2)) {
57 wr_ic_adr(k);
58 wr_ic_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +020059 __asm__ volatile ("isync");
60
61 lines++;
Christophe Leroy48f896d2017-07-06 10:33:17 +020062 k += 0x10; /* the number of bytes in a cacheline */
Christophe Leroy069fa832017-07-06 10:23:22 +020063 }
64
Christophe Leroy48f896d2017-07-06 10:33:17 +020065 wr_ic_cst(IDC_UNALL);
66 wr_ic_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +020067
68 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +020069 wr_ic_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020070 else
Christophe Leroy48f896d2017-07-06 10:33:17 +020071 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020072
73 __asm__ volatile ("isync");
74
75 return lines << 4;
76};
77
78/* ------------------------------------------------------------------------- */
79/* L1 d-cache */
80/* call with cache disabled */
81
Simon Glass1ba89d72019-11-14 12:57:38 -070082static int checkdcache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020083{
Christophe Leroy394f9b32017-07-06 10:33:13 +020084 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
85 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020086 u32 cacheon = rd_dc_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020087 /* probe in flash memoryarea */
88 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020089 u32 m;
90 u32 lines = -1;
91
Christophe Leroy48f896d2017-07-06 10:33:17 +020092 wr_dc_cst(IDC_UNALL);
93 wr_dc_cst(IDC_INVALL);
94 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020095
Christophe Leroy48f896d2017-07-06 10:33:17 +020096 while (!((m = rd_dc_cst()) & IDC_CERR2)) {
97 wr_dc_adr(k);
98 wr_dc_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +020099 lines++;
100 k += 0x10; /* the number of bytes in a cacheline */
101 }
102
Christophe Leroy48f896d2017-07-06 10:33:17 +0200103 wr_dc_cst(IDC_UNALL);
104 wr_dc_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200105
106 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200107 wr_dc_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200108 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200109 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200110
111 return lines << 4;
112};
113
Simon Glass1ba89d72019-11-14 12:57:38 -0700114static int check_CPU(long clock, uint pvr, uint immr)
115{
116 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
117 uint k;
118 char buf[32];
119
120 /* the highest 16 bits should be 0x0050 for a 860 */
121
122 if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
123 return -1;
124
125 k = (immr << 16) |
126 in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
127
128 /*
129 * Some boards use sockets so different CPUs can be used.
130 * We have to check chip version in run time.
131 */
132 switch (k) {
133 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
134 case 0x08010004: /* Rev. A.0 */
135 printf("MPC866xxxZPnnA");
136 break;
137 case 0x08000003: /* Rev. 0.3 */
138 printf("MPC866xxxZPnn");
139 break;
140 case 0x09000000: /* 870/875/880/885 */
141 puts("MPC885ZPnn");
142 break;
143
144 default:
145 printf("unknown MPC86x (0x%08x)", k);
146 break;
147 }
148
149 printf(" at %s MHz: ", strmhz(buf, clock));
150
151 print_size(checkicache(), " I-Cache ");
152 print_size(checkdcache(), " D-Cache");
153
154 /* do we have a FEC (860T/P or 852/859/866/885)? */
155
156 out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
157 if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
158 printf(" FEC present");
159
160 putc('\n');
161
162 return 0;
163}
164
165/* ------------------------------------------------------------------------- */
166
167int checkcpu(void)
168{
169 ulong clock = gd->cpu_clk;
170 uint immr = get_immr(); /* Return full IMMR contents */
171 uint pvr = get_pvr();
172
173 puts("CPU: ");
174
175 return check_CPU(clock, pvr, immr);
176}
177
Christophe Leroy069fa832017-07-06 10:23:22 +0200178/* ------------------------------------------------------------------------- */
179
Christophe Leroy48f896d2017-07-06 10:33:17 +0200180void upmconfig(uint upm, uint *table, uint size)
Christophe Leroy069fa832017-07-06 10:23:22 +0200181{
182 uint i;
183 uint addr = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200184 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
185 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy069fa832017-07-06 10:23:22 +0200186
187 for (i = 0; i < size; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200188 out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */
189 out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */
Christophe Leroy069fa832017-07-06 10:23:22 +0200190 addr++;
191 }
192}
193
194/* ------------------------------------------------------------------------- */
195
Christophe Leroy48f896d2017-07-06 10:33:17 +0200196int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Christophe Leroy069fa832017-07-06 10:23:22 +0200197{
198 ulong msr, addr;
199
Christophe Leroy394f9b32017-07-06 10:33:13 +0200200 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200201
Christophe Leroy394f9b32017-07-06 10:33:13 +0200202 /* Checkstop Reset enable */
203 setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200204
205 /* Interrupts and MMU off */
206 __asm__ volatile ("mtspr 81, 0");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200207 __asm__ volatile ("mfmsr %0" : "=r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200208
209 msr &= ~0x1030;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200210 __asm__ volatile ("mtmsr %0" : : "r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200211
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 /*
Christophe Leroy48f896d2017-07-06 10:33:17 +0200220 * 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.
Christophe Leroy069fa832017-07-06 10:23:22 +0200224 * "(ulong)-1" used to be a good choice for many systems...
225 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200226 addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
Christophe Leroy069fa832017-07-06 10:23:22 +0200227#endif
Christophe Leroy48f896d2017-07-06 10:33:17 +0200228 ((void (*)(void)) addr)();
Christophe Leroy069fa832017-07-06 10:23:22 +0200229 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 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200239unsigned long get_tbclk(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200240{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +0100241 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200242 ulong oscclk, factor, pll;
243
Christophe Leroy394f9b32017-07-06 10:33:13 +0200244 if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200245 return gd->cpu_clk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200246
Christophe Leroy394f9b32017-07-06 10:33:13 +0200247 pll = in_be32(&immap->im_clkrst.car_plprcr);
Christophe Leroy069fa832017-07-06 10:23:22 +0200248
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 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200262 factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
263 (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
Christophe Leroy069fa832017-07-06 10:23:22 +0200264
265 oscclk = gd->cpu_clk / factor;
266
Christophe Leroy394f9b32017-07-06 10:33:13 +0200267 if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
268 factor > 2)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200269 return oscclk / 4;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200270
Christophe Leroy48f896d2017-07-06 10:33:17 +0200271 return oscclk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200272}
273
Christophe Leroy069fa832017-07-06 10:23:22 +0200274/*
275 * Initializes on-chip ethernet controllers.
276 * to override, implement board_eth_init()
277 */
278int cpu_eth_init(bd_t *bis)
279{
Christophe Leroy56ef30a2017-07-06 10:33:23 +0200280#if defined(CONFIG_MPC8XX_FEC)
Christophe Leroy069fa832017-07-06 10:23:22 +0200281 fec_initialize(bis);
282#endif
283 return 0;
284}