blob: b9afd312ec65cbd5463da81eb359c21538bc9530 [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
Simon Glass1fa70f82019-11-14 12:57:34 -070019#include <cpu_func.h>
Simon Glass1ab16922022-07-31 12:28:48 -060020#include <display_options.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <net.h>
Simon Glassa9dc0682019-12-28 10:44:59 -070022#include <time.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070023#include <vsprintf.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020024#include <watchdog.h>
25#include <command.h>
26#include <mpc8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020027#include <netdev.h>
28#include <asm/cache.h>
Christophe Leroy10ff63a2018-03-16 17:20:43 +010029#include <asm/cpm_8xx.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060030#include <asm/global_data.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020031#include <linux/compiler.h>
32#include <asm/io.h>
33
34#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090035#include <linux/libfdt.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020036#include <fdt_support.h>
37#endif
38
39DECLARE_GLOBAL_DATA_PTR;
40
Christophe Leroy069fa832017-07-06 10:23:22 +020041/* ------------------------------------------------------------------------- */
42/* L1 i-cache */
43
Christophe Leroy48f896d2017-07-06 10:33:17 +020044int checkicache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020045{
Christophe Leroy394f9b32017-07-06 10:33:13 +020046 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
47 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020048 u32 cacheon = rd_ic_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020049 /* probe in flash memoryarea */
50 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020051 u32 m;
52 u32 lines = -1;
53
Christophe Leroy48f896d2017-07-06 10:33:17 +020054 wr_ic_cst(IDC_UNALL);
55 wr_ic_cst(IDC_INVALL);
56 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020057 __asm__ volatile ("isync");
58
Christophe Leroy48f896d2017-07-06 10:33:17 +020059 while (!((m = rd_ic_cst()) & IDC_CERR2)) {
60 wr_ic_adr(k);
61 wr_ic_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +020062 __asm__ volatile ("isync");
63
64 lines++;
Christophe Leroy48f896d2017-07-06 10:33:17 +020065 k += 0x10; /* the number of bytes in a cacheline */
Christophe Leroy069fa832017-07-06 10:23:22 +020066 }
67
Christophe Leroy48f896d2017-07-06 10:33:17 +020068 wr_ic_cst(IDC_UNALL);
69 wr_ic_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +020070
71 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +020072 wr_ic_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020073 else
Christophe Leroy48f896d2017-07-06 10:33:17 +020074 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020075
76 __asm__ volatile ("isync");
77
78 return lines << 4;
79};
80
81/* ------------------------------------------------------------------------- */
82/* L1 d-cache */
83/* call with cache disabled */
84
Simon Glass1ba89d72019-11-14 12:57:38 -070085static int checkdcache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020086{
Christophe Leroy394f9b32017-07-06 10:33:13 +020087 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
88 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020089 u32 cacheon = rd_dc_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020090 /* probe in flash memoryarea */
91 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020092 u32 m;
93 u32 lines = -1;
94
Christophe Leroy48f896d2017-07-06 10:33:17 +020095 wr_dc_cst(IDC_UNALL);
96 wr_dc_cst(IDC_INVALL);
97 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020098
Christophe Leroy48f896d2017-07-06 10:33:17 +020099 while (!((m = rd_dc_cst()) & IDC_CERR2)) {
100 wr_dc_adr(k);
101 wr_dc_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200102 lines++;
103 k += 0x10; /* the number of bytes in a cacheline */
104 }
105
Christophe Leroy48f896d2017-07-06 10:33:17 +0200106 wr_dc_cst(IDC_UNALL);
107 wr_dc_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200108
109 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200110 wr_dc_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200111 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200112 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200113
114 return lines << 4;
115};
116
Simon Glass1ba89d72019-11-14 12:57:38 -0700117static int check_CPU(long clock, uint pvr, uint immr)
118{
119 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
120 uint k;
121 char buf[32];
122
123 /* the highest 16 bits should be 0x0050 for a 860 */
124
125 if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
126 return -1;
127
128 k = (immr << 16) |
Christophe Leroye6050cc2023-05-03 10:31:19 +0200129 in_be16((u16 __iomem *)&immap->im_cpm.cp_dpmem[PROFF_REVNUM]);
Simon Glass1ba89d72019-11-14 12:57:38 -0700130
131 /*
132 * Some boards use sockets so different CPUs can be used.
133 * We have to check chip version in run time.
134 */
135 switch (k) {
136 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
137 case 0x08010004: /* Rev. A.0 */
138 printf("MPC866xxxZPnnA");
139 break;
140 case 0x08000003: /* Rev. 0.3 */
141 printf("MPC866xxxZPnn");
142 break;
143 case 0x09000000: /* 870/875/880/885 */
144 puts("MPC885ZPnn");
145 break;
146
147 default:
148 printf("unknown MPC86x (0x%08x)", k);
149 break;
150 }
151
152 printf(" at %s MHz: ", strmhz(buf, clock));
153
154 print_size(checkicache(), " I-Cache ");
155 print_size(checkdcache(), " D-Cache");
156
157 /* do we have a FEC (860T/P or 852/859/866/885)? */
158
159 out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
160 if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
161 printf(" FEC present");
162
163 putc('\n');
164
165 return 0;
166}
167
168/* ------------------------------------------------------------------------- */
169
170int checkcpu(void)
171{
172 ulong clock = gd->cpu_clk;
173 uint immr = get_immr(); /* Return full IMMR contents */
174 uint pvr = get_pvr();
175
176 puts("CPU: ");
177
178 return check_CPU(clock, pvr, immr);
179}
180
Christophe Leroy069fa832017-07-06 10:23:22 +0200181/* ------------------------------------------------------------------------- */
182
Christophe Leroy48f896d2017-07-06 10:33:17 +0200183void upmconfig(uint upm, uint *table, uint size)
Christophe Leroy069fa832017-07-06 10:23:22 +0200184{
185 uint i;
186 uint addr = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200187 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
188 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy069fa832017-07-06 10:23:22 +0200189
190 for (i = 0; i < size; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200191 out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */
192 out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */
Christophe Leroy069fa832017-07-06 10:23:22 +0200193 addr++;
194 }
195}
196
197/* ------------------------------------------------------------------------- */
198
Simon Glassed38aef2020-05-10 11:40:03 -0600199int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Christophe Leroy069fa832017-07-06 10:23:22 +0200200{
201 ulong msr, addr;
202
Christophe Leroy394f9b32017-07-06 10:33:13 +0200203 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200204
Christophe Leroy394f9b32017-07-06 10:33:13 +0200205 /* Checkstop Reset enable */
206 setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200207
208 /* Interrupts and MMU off */
209 __asm__ volatile ("mtspr 81, 0");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200210 __asm__ volatile ("mfmsr %0" : "=r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200211
212 msr &= ~0x1030;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200213 __asm__ volatile ("mtmsr %0" : : "r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200214
215 /*
216 * Trying to execute the next instruction at a non-existing address
217 * should cause a machine check, resulting in reset
Ovidiu Panaitfe19a542022-01-31 09:20:21 +0200218 *
Christophe Leroy48f896d2017-07-06 10:33:17 +0200219 * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address,
220 * CONFIG_SYS_MONITOR_BASE - sizeof (ulong) is usually a valid address.
Christophe Leroy069fa832017-07-06 10:23:22 +0200221 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200222 addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
Ovidiu Panaitfe19a542022-01-31 09:20:21 +0200223
Christophe Leroy48f896d2017-07-06 10:33:17 +0200224 ((void (*)(void)) addr)();
Christophe Leroy069fa832017-07-06 10:23:22 +0200225 return 1;
226}
227
228/* ------------------------------------------------------------------------- */
229
230/*
231 * Get timebase clock frequency (like cpu_clk in Hz)
232 *
233 * See sections 14.2 and 14.6 of the User's Manual
234 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200235unsigned long get_tbclk(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200236{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +0100237 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200238 ulong oscclk, factor, pll;
239
Christophe Leroy394f9b32017-07-06 10:33:13 +0200240 if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200241 return gd->cpu_clk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200242
Christophe Leroy394f9b32017-07-06 10:33:13 +0200243 pll = in_be32(&immap->im_clkrst.car_plprcr);
Christophe Leroy069fa832017-07-06 10:23:22 +0200244
245#define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
246
247 /*
248 * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
249 * factor is calculated as follows:
250 *
251 * MFN
252 * MFI + -------
253 * MFD + 1
254 * factor = -----------------
255 * (PDF + 1) * 2^S
256 *
257 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200258 factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
259 (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
Christophe Leroy069fa832017-07-06 10:23:22 +0200260
261 oscclk = gd->cpu_clk / factor;
262
Christophe Leroy394f9b32017-07-06 10:33:13 +0200263 if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
264 factor > 2)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200265 return oscclk / 4;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200266
Christophe Leroy48f896d2017-07-06 10:33:17 +0200267 return oscclk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200268}