blob: 1b7c7fa01b8e682df705c9e70c4077fbdedab4db [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 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>
Christophe Leroy069fa832017-07-06 10:23:22 +020030#include <linux/compiler.h>
31#include <asm/io.h>
32
33#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090034#include <linux/libfdt.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020035#include <fdt_support.h>
36#endif
37
38DECLARE_GLOBAL_DATA_PTR;
39
Christophe Leroy069fa832017-07-06 10:23:22 +020040/* ------------------------------------------------------------------------- */
41/* L1 i-cache */
42
Christophe Leroy48f896d2017-07-06 10:33:17 +020043int checkicache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020044{
Christophe Leroy394f9b32017-07-06 10:33:13 +020045 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
46 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020047 u32 cacheon = rd_ic_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020048 /* probe in flash memoryarea */
49 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020050 u32 m;
51 u32 lines = -1;
52
Christophe Leroy48f896d2017-07-06 10:33:17 +020053 wr_ic_cst(IDC_UNALL);
54 wr_ic_cst(IDC_INVALL);
55 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020056 __asm__ volatile ("isync");
57
Christophe Leroy48f896d2017-07-06 10:33:17 +020058 while (!((m = rd_ic_cst()) & IDC_CERR2)) {
59 wr_ic_adr(k);
60 wr_ic_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +020061 __asm__ volatile ("isync");
62
63 lines++;
Christophe Leroy48f896d2017-07-06 10:33:17 +020064 k += 0x10; /* the number of bytes in a cacheline */
Christophe Leroy069fa832017-07-06 10:23:22 +020065 }
66
Christophe Leroy48f896d2017-07-06 10:33:17 +020067 wr_ic_cst(IDC_UNALL);
68 wr_ic_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +020069
70 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +020071 wr_ic_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020072 else
Christophe Leroy48f896d2017-07-06 10:33:17 +020073 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020074
75 __asm__ volatile ("isync");
76
77 return lines << 4;
78};
79
80/* ------------------------------------------------------------------------- */
81/* L1 d-cache */
82/* call with cache disabled */
83
Simon Glass1ba89d72019-11-14 12:57:38 -070084static int checkdcache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020085{
Christophe Leroy394f9b32017-07-06 10:33:13 +020086 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
87 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020088 u32 cacheon = rd_dc_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020089 /* probe in flash memoryarea */
90 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020091 u32 m;
92 u32 lines = -1;
93
Christophe Leroy48f896d2017-07-06 10:33:17 +020094 wr_dc_cst(IDC_UNALL);
95 wr_dc_cst(IDC_INVALL);
96 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020097
Christophe Leroy48f896d2017-07-06 10:33:17 +020098 while (!((m = rd_dc_cst()) & IDC_CERR2)) {
99 wr_dc_adr(k);
100 wr_dc_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200101 lines++;
102 k += 0x10; /* the number of bytes in a cacheline */
103 }
104
Christophe Leroy48f896d2017-07-06 10:33:17 +0200105 wr_dc_cst(IDC_UNALL);
106 wr_dc_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200107
108 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200109 wr_dc_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200110 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200111 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200112
113 return lines << 4;
114};
115
Simon Glass1ba89d72019-11-14 12:57:38 -0700116static int check_CPU(long clock, uint pvr, uint immr)
117{
118 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
119 uint k;
120 char buf[32];
121
122 /* the highest 16 bits should be 0x0050 for a 860 */
123
124 if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
125 return -1;
126
127 k = (immr << 16) |
128 in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
129
130 /*
131 * Some boards use sockets so different CPUs can be used.
132 * We have to check chip version in run time.
133 */
134 switch (k) {
135 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
136 case 0x08010004: /* Rev. A.0 */
137 printf("MPC866xxxZPnnA");
138 break;
139 case 0x08000003: /* Rev. 0.3 */
140 printf("MPC866xxxZPnn");
141 break;
142 case 0x09000000: /* 870/875/880/885 */
143 puts("MPC885ZPnn");
144 break;
145
146 default:
147 printf("unknown MPC86x (0x%08x)", k);
148 break;
149 }
150
151 printf(" at %s MHz: ", strmhz(buf, clock));
152
153 print_size(checkicache(), " I-Cache ");
154 print_size(checkdcache(), " D-Cache");
155
156 /* do we have a FEC (860T/P or 852/859/866/885)? */
157
158 out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
159 if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
160 printf(" FEC present");
161
162 putc('\n');
163
164 return 0;
165}
166
167/* ------------------------------------------------------------------------- */
168
169int checkcpu(void)
170{
171 ulong clock = gd->cpu_clk;
172 uint immr = get_immr(); /* Return full IMMR contents */
173 uint pvr = get_pvr();
174
175 puts("CPU: ");
176
177 return check_CPU(clock, pvr, immr);
178}
179
Christophe Leroy069fa832017-07-06 10:23:22 +0200180/* ------------------------------------------------------------------------- */
181
Christophe Leroy48f896d2017-07-06 10:33:17 +0200182void upmconfig(uint upm, uint *table, uint size)
Christophe Leroy069fa832017-07-06 10:23:22 +0200183{
184 uint i;
185 uint addr = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200186 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
187 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy069fa832017-07-06 10:23:22 +0200188
189 for (i = 0; i < size; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200190 out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */
191 out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */
Christophe Leroy069fa832017-07-06 10:23:22 +0200192 addr++;
193 }
194}
195
196/* ------------------------------------------------------------------------- */
197
Simon Glassed38aef2020-05-10 11:40:03 -0600198int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Christophe Leroy069fa832017-07-06 10:23:22 +0200199{
200 ulong msr, addr;
201
Christophe Leroy394f9b32017-07-06 10:33:13 +0200202 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200203
Christophe Leroy394f9b32017-07-06 10:33:13 +0200204 /* Checkstop Reset enable */
205 setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200206
207 /* Interrupts and MMU off */
208 __asm__ volatile ("mtspr 81, 0");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200209 __asm__ volatile ("mfmsr %0" : "=r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200210
211 msr &= ~0x1030;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200212 __asm__ volatile ("mtmsr %0" : : "r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200213
214 /*
215 * Trying to execute the next instruction at a non-existing address
216 * should cause a machine check, resulting in reset
217 */
218#ifdef CONFIG_SYS_RESET_ADDRESS
219 addr = CONFIG_SYS_RESET_ADDRESS;
220#else
221 /*
Christophe Leroy48f896d2017-07-06 10:33:17 +0200222 * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address,
223 * CONFIG_SYS_MONITOR_BASE - sizeof (ulong) is usually a valid address.
224 * Better pick an address known to be invalid on your system and assign
225 * it to CONFIG_SYS_RESET_ADDRESS.
Christophe Leroy069fa832017-07-06 10:23:22 +0200226 * "(ulong)-1" used to be a good choice for many systems...
227 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200228 addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
Christophe Leroy069fa832017-07-06 10:23:22 +0200229#endif
Christophe Leroy48f896d2017-07-06 10:33:17 +0200230 ((void (*)(void)) addr)();
Christophe Leroy069fa832017-07-06 10:23:22 +0200231 return 1;
232}
233
234/* ------------------------------------------------------------------------- */
235
236/*
237 * Get timebase clock frequency (like cpu_clk in Hz)
238 *
239 * See sections 14.2 and 14.6 of the User's Manual
240 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200241unsigned long get_tbclk(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200242{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +0100243 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200244 ulong oscclk, factor, pll;
245
Christophe Leroy394f9b32017-07-06 10:33:13 +0200246 if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200247 return gd->cpu_clk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200248
Christophe Leroy394f9b32017-07-06 10:33:13 +0200249 pll = in_be32(&immap->im_clkrst.car_plprcr);
Christophe Leroy069fa832017-07-06 10:23:22 +0200250
251#define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
252
253 /*
254 * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
255 * factor is calculated as follows:
256 *
257 * MFN
258 * MFI + -------
259 * MFD + 1
260 * factor = -----------------
261 * (PDF + 1) * 2^S
262 *
263 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200264 factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
265 (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
Christophe Leroy069fa832017-07-06 10:23:22 +0200266
267 oscclk = gd->cpu_clk / factor;
268
Christophe Leroy394f9b32017-07-06 10:33:13 +0200269 if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
270 factor > 2)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200271 return oscclk / 4;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200272
Christophe Leroy48f896d2017-07-06 10:33:17 +0200273 return oscclk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200274}
275
Christophe Leroy069fa832017-07-06 10:23:22 +0200276/*
277 * Initializes on-chip ethernet controllers.
278 * to override, implement board_eth_init()
279 */
280int cpu_eth_init(bd_t *bis)
281{
Christophe Leroy56ef30a2017-07-06 10:33:23 +0200282#if defined(CONFIG_MPC8XX_FEC)
Christophe Leroy069fa832017-07-06 10:23:22 +0200283 fec_initialize(bis);
284#endif
285 return 0;
286}