blob: 798eabdc21c1db9dcf0cbbc0d5c7db80458f0023 [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>
20#include <watchdog.h>
21#include <command.h>
22#include <mpc8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020023#include <netdev.h>
24#include <asm/cache.h>
Christophe Leroy10ff63a2018-03-16 17:20:43 +010025#include <asm/cpm_8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020026#include <linux/compiler.h>
27#include <asm/io.h>
28
29#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090030#include <linux/libfdt.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020031#include <fdt_support.h>
32#endif
33
34DECLARE_GLOBAL_DATA_PTR;
35
Christophe Leroy48f896d2017-07-06 10:33:17 +020036static int check_CPU(long clock, uint pvr, uint immr)
Christophe Leroy069fa832017-07-06 10:23:22 +020037{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +010038 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroyea31cb22017-07-13 15:09:58 +020039 uint k;
Christophe Leroy069fa832017-07-06 10:23:22 +020040 char buf[32];
Christophe Leroy069fa832017-07-06 10:23:22 +020041
42 /* the highest 16 bits should be 0x0050 for a 860 */
43
Christophe Leroy0a121f72018-03-16 17:20:35 +010044 if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
Christophe Leroy069fa832017-07-06 10:23:22 +020045 return -1;
46
47 k = (immr << 16) |
Christophe Leroy394f9b32017-07-06 10:33:13 +020048 in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
Christophe Leroy069fa832017-07-06 10:23:22 +020049
50 /*
51 * Some boards use sockets so different CPUs can be used.
52 * We have to check chip version in run time.
53 */
54 switch (k) {
55 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
56 case 0x08010004: /* Rev. A.0 */
Christophe Leroyea31cb22017-07-13 15:09:58 +020057 printf("MPC866xxxZPnnA");
58 break;
Christophe Leroy069fa832017-07-06 10:23:22 +020059 case 0x08000003: /* Rev. 0.3 */
Christophe Leroyea31cb22017-07-13 15:09:58 +020060 printf("MPC866xxxZPnn");
Christophe Leroy069fa832017-07-06 10:23:22 +020061 break;
Christophe Leroyea31cb22017-07-13 15:09:58 +020062 case 0x09000000: /* 870/875/880/885 */
63 puts("MPC885ZPnn");
Christophe Leroy069fa832017-07-06 10:23:22 +020064 break;
65
Christophe Leroy48f896d2017-07-06 10:33:17 +020066 default:
Christophe Leroyea31cb22017-07-13 15:09:58 +020067 printf("unknown MPC86x (0x%08x)", k);
Christophe Leroy48f896d2017-07-06 10:33:17 +020068 break;
Christophe Leroy069fa832017-07-06 10:23:22 +020069 }
70
Christophe Leroy48f896d2017-07-06 10:33:17 +020071 printf(" at %s MHz: ", strmhz(buf, clock));
Christophe Leroy069fa832017-07-06 10:23:22 +020072
73 print_size(checkicache(), " I-Cache ");
74 print_size(checkdcache(), " D-Cache");
75
76 /* do we have a FEC (860T/P or 852/859/866/885)? */
77
Christophe Leroy394f9b32017-07-06 10:33:13 +020078 out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
79 if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
Christophe Leroy48f896d2017-07-06 10:33:17 +020080 printf(" FEC present");
Christophe Leroy069fa832017-07-06 10:23:22 +020081
Christophe Leroy48f896d2017-07-06 10:33:17 +020082 putc('\n');
Christophe Leroy069fa832017-07-06 10:23:22 +020083
84 return 0;
85}
86
87/* ------------------------------------------------------------------------- */
88
Christophe Leroy48f896d2017-07-06 10:33:17 +020089int checkcpu(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020090{
91 ulong clock = gd->cpu_clk;
Christophe Leroybda89472018-03-16 17:20:39 +010092 uint immr = get_immr(); /* Return full IMMR contents */
Christophe Leroy48f896d2017-07-06 10:33:17 +020093 uint pvr = get_pvr();
Christophe Leroy069fa832017-07-06 10:23:22 +020094
Christophe Leroy48f896d2017-07-06 10:33:17 +020095 puts("CPU: ");
Christophe Leroy069fa832017-07-06 10:23:22 +020096
Christophe Leroy48f896d2017-07-06 10:33:17 +020097 return check_CPU(clock, pvr, immr);
Christophe Leroy069fa832017-07-06 10:23:22 +020098}
99
100/* ------------------------------------------------------------------------- */
101/* L1 i-cache */
102
Christophe Leroy48f896d2017-07-06 10:33:17 +0200103int checkicache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200104{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200105 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
106 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200107 u32 cacheon = rd_ic_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200108 /* probe in flash memoryarea */
109 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +0200110 u32 m;
111 u32 lines = -1;
112
Christophe Leroy48f896d2017-07-06 10:33:17 +0200113 wr_ic_cst(IDC_UNALL);
114 wr_ic_cst(IDC_INVALL);
115 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200116 __asm__ volatile ("isync");
117
Christophe Leroy48f896d2017-07-06 10:33:17 +0200118 while (!((m = rd_ic_cst()) & IDC_CERR2)) {
119 wr_ic_adr(k);
120 wr_ic_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200121 __asm__ volatile ("isync");
122
123 lines++;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200124 k += 0x10; /* the number of bytes in a cacheline */
Christophe Leroy069fa832017-07-06 10:23:22 +0200125 }
126
Christophe Leroy48f896d2017-07-06 10:33:17 +0200127 wr_ic_cst(IDC_UNALL);
128 wr_ic_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200129
130 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200131 wr_ic_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200132 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200133 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200134
135 __asm__ volatile ("isync");
136
137 return lines << 4;
138};
139
140/* ------------------------------------------------------------------------- */
141/* L1 d-cache */
142/* call with cache disabled */
143
Christophe Leroy48f896d2017-07-06 10:33:17 +0200144int checkdcache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200145{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200146 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
147 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200148 u32 cacheon = rd_dc_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200149 /* probe in flash memoryarea */
150 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +0200151 u32 m;
152 u32 lines = -1;
153
Christophe Leroy48f896d2017-07-06 10:33:17 +0200154 wr_dc_cst(IDC_UNALL);
155 wr_dc_cst(IDC_INVALL);
156 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200157
Christophe Leroy48f896d2017-07-06 10:33:17 +0200158 while (!((m = rd_dc_cst()) & IDC_CERR2)) {
159 wr_dc_adr(k);
160 wr_dc_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200161 lines++;
162 k += 0x10; /* the number of bytes in a cacheline */
163 }
164
Christophe Leroy48f896d2017-07-06 10:33:17 +0200165 wr_dc_cst(IDC_UNALL);
166 wr_dc_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200167
168 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200169 wr_dc_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200170 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200171 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200172
173 return lines << 4;
174};
175
176/* ------------------------------------------------------------------------- */
177
Christophe Leroy48f896d2017-07-06 10:33:17 +0200178void upmconfig(uint upm, uint *table, uint size)
Christophe Leroy069fa832017-07-06 10:23:22 +0200179{
180 uint i;
181 uint addr = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200182 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
183 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy069fa832017-07-06 10:23:22 +0200184
185 for (i = 0; i < size; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200186 out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */
187 out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */
Christophe Leroy069fa832017-07-06 10:23:22 +0200188 addr++;
189 }
190}
191
192/* ------------------------------------------------------------------------- */
193
Christophe Leroy48f896d2017-07-06 10:33:17 +0200194int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Christophe Leroy069fa832017-07-06 10:23:22 +0200195{
196 ulong msr, addr;
197
Christophe Leroy394f9b32017-07-06 10:33:13 +0200198 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200199
Christophe Leroy394f9b32017-07-06 10:33:13 +0200200 /* Checkstop Reset enable */
201 setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200202
203 /* Interrupts and MMU off */
204 __asm__ volatile ("mtspr 81, 0");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200205 __asm__ volatile ("mfmsr %0" : "=r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200206
207 msr &= ~0x1030;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200208 __asm__ volatile ("mtmsr %0" : : "r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200209
210 /*
211 * Trying to execute the next instruction at a non-existing address
212 * should cause a machine check, resulting in reset
213 */
214#ifdef CONFIG_SYS_RESET_ADDRESS
215 addr = CONFIG_SYS_RESET_ADDRESS;
216#else
217 /*
Christophe Leroy48f896d2017-07-06 10:33:17 +0200218 * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address,
219 * CONFIG_SYS_MONITOR_BASE - sizeof (ulong) is usually a valid address.
220 * Better pick an address known to be invalid on your system and assign
221 * it to CONFIG_SYS_RESET_ADDRESS.
Christophe Leroy069fa832017-07-06 10:23:22 +0200222 * "(ulong)-1" used to be a good choice for many systems...
223 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200224 addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
Christophe Leroy069fa832017-07-06 10:23:22 +0200225#endif
Christophe Leroy48f896d2017-07-06 10:33:17 +0200226 ((void (*)(void)) addr)();
Christophe Leroy069fa832017-07-06 10:23:22 +0200227 return 1;
228}
229
230/* ------------------------------------------------------------------------- */
231
232/*
233 * Get timebase clock frequency (like cpu_clk in Hz)
234 *
235 * See sections 14.2 and 14.6 of the User's Manual
236 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200237unsigned long get_tbclk(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200238{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +0100239 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200240 ulong oscclk, factor, pll;
241
Christophe Leroy394f9b32017-07-06 10:33:13 +0200242 if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200243 return gd->cpu_clk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200244
Christophe Leroy394f9b32017-07-06 10:33:13 +0200245 pll = in_be32(&immap->im_clkrst.car_plprcr);
Christophe Leroy069fa832017-07-06 10:23:22 +0200246
247#define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
248
249 /*
250 * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
251 * factor is calculated as follows:
252 *
253 * MFN
254 * MFI + -------
255 * MFD + 1
256 * factor = -----------------
257 * (PDF + 1) * 2^S
258 *
259 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200260 factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
261 (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
Christophe Leroy069fa832017-07-06 10:23:22 +0200262
263 oscclk = gd->cpu_clk / factor;
264
Christophe Leroy394f9b32017-07-06 10:33:13 +0200265 if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
266 factor > 2)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200267 return oscclk / 4;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200268
Christophe Leroy48f896d2017-07-06 10:33:17 +0200269 return oscclk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200270}
271
Christophe Leroy069fa832017-07-06 10:23:22 +0200272/*
273 * Initializes on-chip ethernet controllers.
274 * to override, implement board_eth_init()
275 */
276int cpu_eth_init(bd_t *bis)
277{
Christophe Leroy56ef30a2017-07-06 10:33:23 +0200278#if defined(CONFIG_MPC8XX_FEC)
Christophe Leroy069fa832017-07-06 10:23:22 +0200279 fec_initialize(bis);
280#endif
281 return 0;
282}