blob: 9b587fbbe877aaa8c7b2cf7461f356544ab0f1e0 [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 Glass1ab16922022-07-31 12:28:48 -060021#include <display_options.h>
Simon Glass274e0b02020-05-10 11:39:56 -060022#include <net.h>
Simon Glassa9dc0682019-12-28 10:44:59 -070023#include <time.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070024#include <vsprintf.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020025#include <watchdog.h>
26#include <command.h>
27#include <mpc8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020028#include <netdev.h>
29#include <asm/cache.h>
Christophe Leroy10ff63a2018-03-16 17:20:43 +010030#include <asm/cpm_8xx.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060031#include <asm/global_data.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020032#include <linux/compiler.h>
33#include <asm/io.h>
34
35#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090036#include <linux/libfdt.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020037#include <fdt_support.h>
38#endif
39
40DECLARE_GLOBAL_DATA_PTR;
41
Christophe Leroy069fa832017-07-06 10:23:22 +020042/* ------------------------------------------------------------------------- */
43/* L1 i-cache */
44
Christophe Leroy48f896d2017-07-06 10:33:17 +020045int checkicache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020046{
Christophe Leroy394f9b32017-07-06 10:33:13 +020047 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
48 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020049 u32 cacheon = rd_ic_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020050 /* probe in flash memoryarea */
51 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020052 u32 m;
53 u32 lines = -1;
54
Christophe Leroy48f896d2017-07-06 10:33:17 +020055 wr_ic_cst(IDC_UNALL);
56 wr_ic_cst(IDC_INVALL);
57 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020058 __asm__ volatile ("isync");
59
Christophe Leroy48f896d2017-07-06 10:33:17 +020060 while (!((m = rd_ic_cst()) & IDC_CERR2)) {
61 wr_ic_adr(k);
62 wr_ic_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +020063 __asm__ volatile ("isync");
64
65 lines++;
Christophe Leroy48f896d2017-07-06 10:33:17 +020066 k += 0x10; /* the number of bytes in a cacheline */
Christophe Leroy069fa832017-07-06 10:23:22 +020067 }
68
Christophe Leroy48f896d2017-07-06 10:33:17 +020069 wr_ic_cst(IDC_UNALL);
70 wr_ic_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +020071
72 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +020073 wr_ic_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020074 else
Christophe Leroy48f896d2017-07-06 10:33:17 +020075 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020076
77 __asm__ volatile ("isync");
78
79 return lines << 4;
80};
81
82/* ------------------------------------------------------------------------- */
83/* L1 d-cache */
84/* call with cache disabled */
85
Simon Glass1ba89d72019-11-14 12:57:38 -070086static int checkdcache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020087{
Christophe Leroy394f9b32017-07-06 10:33:13 +020088 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
89 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020090 u32 cacheon = rd_dc_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020091 /* probe in flash memoryarea */
92 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020093 u32 m;
94 u32 lines = -1;
95
Christophe Leroy48f896d2017-07-06 10:33:17 +020096 wr_dc_cst(IDC_UNALL);
97 wr_dc_cst(IDC_INVALL);
98 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020099
Christophe Leroy48f896d2017-07-06 10:33:17 +0200100 while (!((m = rd_dc_cst()) & IDC_CERR2)) {
101 wr_dc_adr(k);
102 wr_dc_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200103 lines++;
104 k += 0x10; /* the number of bytes in a cacheline */
105 }
106
Christophe Leroy48f896d2017-07-06 10:33:17 +0200107 wr_dc_cst(IDC_UNALL);
108 wr_dc_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200109
110 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200111 wr_dc_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200112 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200113 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200114
115 return lines << 4;
116};
117
Simon Glass1ba89d72019-11-14 12:57:38 -0700118static int check_CPU(long clock, uint pvr, uint immr)
119{
120 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
121 uint k;
122 char buf[32];
123
124 /* the highest 16 bits should be 0x0050 for a 860 */
125
126 if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
127 return -1;
128
129 k = (immr << 16) |
130 in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
131
132 /*
133 * Some boards use sockets so different CPUs can be used.
134 * We have to check chip version in run time.
135 */
136 switch (k) {
137 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
138 case 0x08010004: /* Rev. A.0 */
139 printf("MPC866xxxZPnnA");
140 break;
141 case 0x08000003: /* Rev. 0.3 */
142 printf("MPC866xxxZPnn");
143 break;
144 case 0x09000000: /* 870/875/880/885 */
145 puts("MPC885ZPnn");
146 break;
147
148 default:
149 printf("unknown MPC86x (0x%08x)", k);
150 break;
151 }
152
153 printf(" at %s MHz: ", strmhz(buf, clock));
154
155 print_size(checkicache(), " I-Cache ");
156 print_size(checkdcache(), " D-Cache");
157
158 /* do we have a FEC (860T/P or 852/859/866/885)? */
159
160 out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
161 if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
162 printf(" FEC present");
163
164 putc('\n');
165
166 return 0;
167}
168
169/* ------------------------------------------------------------------------- */
170
171int checkcpu(void)
172{
173 ulong clock = gd->cpu_clk;
174 uint immr = get_immr(); /* Return full IMMR contents */
175 uint pvr = get_pvr();
176
177 puts("CPU: ");
178
179 return check_CPU(clock, pvr, immr);
180}
181
Christophe Leroy069fa832017-07-06 10:23:22 +0200182/* ------------------------------------------------------------------------- */
183
Christophe Leroy48f896d2017-07-06 10:33:17 +0200184void upmconfig(uint upm, uint *table, uint size)
Christophe Leroy069fa832017-07-06 10:23:22 +0200185{
186 uint i;
187 uint addr = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200188 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
189 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy069fa832017-07-06 10:23:22 +0200190
191 for (i = 0; i < size; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200192 out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */
193 out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */
Christophe Leroy069fa832017-07-06 10:23:22 +0200194 addr++;
195 }
196}
197
198/* ------------------------------------------------------------------------- */
199
Simon Glassed38aef2020-05-10 11:40:03 -0600200int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Christophe Leroy069fa832017-07-06 10:23:22 +0200201{
202 ulong msr, addr;
203
Christophe Leroy394f9b32017-07-06 10:33:13 +0200204 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200205
Christophe Leroy394f9b32017-07-06 10:33:13 +0200206 /* Checkstop Reset enable */
207 setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200208
209 /* Interrupts and MMU off */
210 __asm__ volatile ("mtspr 81, 0");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200211 __asm__ volatile ("mfmsr %0" : "=r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200212
213 msr &= ~0x1030;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200214 __asm__ volatile ("mtmsr %0" : : "r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200215
216 /*
217 * Trying to execute the next instruction at a non-existing address
218 * should cause a machine check, resulting in reset
Ovidiu Panaitfe19a542022-01-31 09:20:21 +0200219 *
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.
Christophe Leroy069fa832017-07-06 10:23:22 +0200222 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200223 addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
Ovidiu Panaitfe19a542022-01-31 09:20:21 +0200224
Christophe Leroy48f896d2017-07-06 10:33:17 +0200225 ((void (*)(void)) addr)();
Christophe Leroy069fa832017-07-06 10:23:22 +0200226 return 1;
227}
228
229/* ------------------------------------------------------------------------- */
230
231/*
232 * Get timebase clock frequency (like cpu_clk in Hz)
233 *
234 * See sections 14.2 and 14.6 of the User's Manual
235 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200236unsigned long get_tbclk(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200237{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +0100238 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200239 ulong oscclk, factor, pll;
240
Christophe Leroy394f9b32017-07-06 10:33:13 +0200241 if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200242 return gd->cpu_clk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200243
Christophe Leroy394f9b32017-07-06 10:33:13 +0200244 pll = in_be32(&immap->im_clkrst.car_plprcr);
Christophe Leroy069fa832017-07-06 10:23:22 +0200245
246#define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
247
248 /*
249 * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
250 * factor is calculated as follows:
251 *
252 * MFN
253 * MFI + -------
254 * MFD + 1
255 * factor = -----------------
256 * (PDF + 1) * 2^S
257 *
258 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200259 factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
260 (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
Christophe Leroy069fa832017-07-06 10:23:22 +0200261
262 oscclk = gd->cpu_clk / factor;
263
Christophe Leroy394f9b32017-07-06 10:33:13 +0200264 if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
265 factor > 2)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200266 return oscclk / 4;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200267
Christophe Leroy48f896d2017-07-06 10:33:17 +0200268 return oscclk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200269}