blob: 5a81bed5a1028c74c9b52e26b927c04debe7da6a [file] [log] [blame]
Christophe Leroy069fa832017-07-06 10:23:22 +02001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8/*
9 * m8xx.c
10 *
11 * CPU specific code
12 *
13 * written or collected and sometimes rewritten by
14 * Magnus Damm <damm@bitsmart.com>
15 *
16 * minor modifications by
17 * Wolfgang Denk <wd@denx.de>
18 */
19
20#include <common.h>
21#include <watchdog.h>
22#include <command.h>
23#include <mpc8xx.h>
24#include <commproc.h>
25#include <netdev.h>
26#include <asm/cache.h>
27#include <linux/compiler.h>
28#include <asm/io.h>
29
30#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090031#include <linux/libfdt.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020032#include <fdt_support.h>
33#endif
34
35DECLARE_GLOBAL_DATA_PTR;
36
Christophe Leroy48f896d2017-07-06 10:33:17 +020037static int check_CPU(long clock, uint pvr, uint immr)
Christophe Leroy069fa832017-07-06 10:23:22 +020038{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +010039 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroyea31cb22017-07-13 15:09:58 +020040 uint k;
Christophe Leroy069fa832017-07-06 10:23:22 +020041 char buf[32];
Christophe Leroy069fa832017-07-06 10:23:22 +020042
43 /* the highest 16 bits should be 0x0050 for a 860 */
44
Christophe Leroy0a121f72018-03-16 17:20:35 +010045 if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
Christophe Leroy069fa832017-07-06 10:23:22 +020046 return -1;
47
48 k = (immr << 16) |
Christophe Leroy394f9b32017-07-06 10:33:13 +020049 in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
Christophe Leroy069fa832017-07-06 10:23:22 +020050
51 /*
52 * Some boards use sockets so different CPUs can be used.
53 * We have to check chip version in run time.
54 */
55 switch (k) {
56 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
57 case 0x08010004: /* Rev. A.0 */
Christophe Leroyea31cb22017-07-13 15:09:58 +020058 printf("MPC866xxxZPnnA");
59 break;
Christophe Leroy069fa832017-07-06 10:23:22 +020060 case 0x08000003: /* Rev. 0.3 */
Christophe Leroyea31cb22017-07-13 15:09:58 +020061 printf("MPC866xxxZPnn");
Christophe Leroy069fa832017-07-06 10:23:22 +020062 break;
Christophe Leroyea31cb22017-07-13 15:09:58 +020063 case 0x09000000: /* 870/875/880/885 */
64 puts("MPC885ZPnn");
Christophe Leroy069fa832017-07-06 10:23:22 +020065 break;
66
Christophe Leroy48f896d2017-07-06 10:33:17 +020067 default:
Christophe Leroyea31cb22017-07-13 15:09:58 +020068 printf("unknown MPC86x (0x%08x)", k);
Christophe Leroy48f896d2017-07-06 10:33:17 +020069 break;
Christophe Leroy069fa832017-07-06 10:23:22 +020070 }
71
Christophe Leroy48f896d2017-07-06 10:33:17 +020072 printf(" at %s MHz: ", strmhz(buf, clock));
Christophe Leroy069fa832017-07-06 10:23:22 +020073
74 print_size(checkicache(), " I-Cache ");
75 print_size(checkdcache(), " D-Cache");
76
77 /* do we have a FEC (860T/P or 852/859/866/885)? */
78
Christophe Leroy394f9b32017-07-06 10:33:13 +020079 out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
80 if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
Christophe Leroy48f896d2017-07-06 10:33:17 +020081 printf(" FEC present");
Christophe Leroy069fa832017-07-06 10:23:22 +020082
Christophe Leroy48f896d2017-07-06 10:33:17 +020083 putc('\n');
Christophe Leroy069fa832017-07-06 10:23:22 +020084
85 return 0;
86}
87
88/* ------------------------------------------------------------------------- */
89
Christophe Leroy48f896d2017-07-06 10:33:17 +020090int checkcpu(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020091{
92 ulong clock = gd->cpu_clk;
Christophe Leroy48f896d2017-07-06 10:33:17 +020093 uint immr = get_immr(0); /* Return full IMMR contents */
94 uint pvr = get_pvr();
Christophe Leroy069fa832017-07-06 10:23:22 +020095
Christophe Leroy48f896d2017-07-06 10:33:17 +020096 puts("CPU: ");
Christophe Leroy069fa832017-07-06 10:23:22 +020097
Christophe Leroy48f896d2017-07-06 10:33:17 +020098 return check_CPU(clock, pvr, immr);
Christophe Leroy069fa832017-07-06 10:23:22 +020099}
100
101/* ------------------------------------------------------------------------- */
102/* L1 i-cache */
103
Christophe Leroy48f896d2017-07-06 10:33:17 +0200104int checkicache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200105{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200106 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
107 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200108 u32 cacheon = rd_ic_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200109 /* probe in flash memoryarea */
110 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +0200111 u32 m;
112 u32 lines = -1;
113
Christophe Leroy48f896d2017-07-06 10:33:17 +0200114 wr_ic_cst(IDC_UNALL);
115 wr_ic_cst(IDC_INVALL);
116 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200117 __asm__ volatile ("isync");
118
Christophe Leroy48f896d2017-07-06 10:33:17 +0200119 while (!((m = rd_ic_cst()) & IDC_CERR2)) {
120 wr_ic_adr(k);
121 wr_ic_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200122 __asm__ volatile ("isync");
123
124 lines++;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200125 k += 0x10; /* the number of bytes in a cacheline */
Christophe Leroy069fa832017-07-06 10:23:22 +0200126 }
127
Christophe Leroy48f896d2017-07-06 10:33:17 +0200128 wr_ic_cst(IDC_UNALL);
129 wr_ic_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200130
131 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200132 wr_ic_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200133 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200134 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200135
136 __asm__ volatile ("isync");
137
138 return lines << 4;
139};
140
141/* ------------------------------------------------------------------------- */
142/* L1 d-cache */
143/* call with cache disabled */
144
Christophe Leroy48f896d2017-07-06 10:33:17 +0200145int checkdcache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200146{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200147 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
148 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200149 u32 cacheon = rd_dc_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200150 /* probe in flash memoryarea */
151 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +0200152 u32 m;
153 u32 lines = -1;
154
Christophe Leroy48f896d2017-07-06 10:33:17 +0200155 wr_dc_cst(IDC_UNALL);
156 wr_dc_cst(IDC_INVALL);
157 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200158
Christophe Leroy48f896d2017-07-06 10:33:17 +0200159 while (!((m = rd_dc_cst()) & IDC_CERR2)) {
160 wr_dc_adr(k);
161 wr_dc_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200162 lines++;
163 k += 0x10; /* the number of bytes in a cacheline */
164 }
165
Christophe Leroy48f896d2017-07-06 10:33:17 +0200166 wr_dc_cst(IDC_UNALL);
167 wr_dc_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200168
169 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200170 wr_dc_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200171 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200172 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200173
174 return lines << 4;
175};
176
177/* ------------------------------------------------------------------------- */
178
Christophe Leroy48f896d2017-07-06 10:33:17 +0200179void upmconfig(uint upm, uint *table, uint size)
Christophe Leroy069fa832017-07-06 10:23:22 +0200180{
181 uint i;
182 uint addr = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200183 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
184 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy069fa832017-07-06 10:23:22 +0200185
186 for (i = 0; i < size; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200187 out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */
188 out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */
Christophe Leroy069fa832017-07-06 10:23:22 +0200189 addr++;
190 }
191}
192
193/* ------------------------------------------------------------------------- */
194
Christophe Leroy48f896d2017-07-06 10:33:17 +0200195int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Christophe Leroy069fa832017-07-06 10:23:22 +0200196{
197 ulong msr, addr;
198
Christophe Leroy394f9b32017-07-06 10:33:13 +0200199 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200200
Christophe Leroy394f9b32017-07-06 10:33:13 +0200201 /* Checkstop Reset enable */
202 setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200203
204 /* Interrupts and MMU off */
205 __asm__ volatile ("mtspr 81, 0");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200206 __asm__ volatile ("mfmsr %0" : "=r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200207
208 msr &= ~0x1030;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200209 __asm__ volatile ("mtmsr %0" : : "r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200210
211 /*
212 * Trying to execute the next instruction at a non-existing address
213 * should cause a machine check, resulting in reset
214 */
215#ifdef CONFIG_SYS_RESET_ADDRESS
216 addr = CONFIG_SYS_RESET_ADDRESS;
217#else
218 /*
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.
221 * Better pick an address known to be invalid on your system and assign
222 * it to CONFIG_SYS_RESET_ADDRESS.
Christophe Leroy069fa832017-07-06 10:23:22 +0200223 * "(ulong)-1" used to be a good choice for many systems...
224 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200225 addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
Christophe Leroy069fa832017-07-06 10:23:22 +0200226#endif
Christophe Leroy48f896d2017-07-06 10:33:17 +0200227 ((void (*)(void)) addr)();
Christophe Leroy069fa832017-07-06 10:23:22 +0200228 return 1;
229}
230
231/* ------------------------------------------------------------------------- */
232
233/*
234 * Get timebase clock frequency (like cpu_clk in Hz)
235 *
236 * See sections 14.2 and 14.6 of the User's Manual
237 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200238unsigned long get_tbclk(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200239{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +0100240 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200241 ulong oscclk, factor, pll;
242
Christophe Leroy394f9b32017-07-06 10:33:13 +0200243 if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200244 return gd->cpu_clk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200245
Christophe Leroy394f9b32017-07-06 10:33:13 +0200246 pll = in_be32(&immap->im_clkrst.car_plprcr);
Christophe Leroy069fa832017-07-06 10:23:22 +0200247
248#define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
249
250 /*
251 * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
252 * factor is calculated as follows:
253 *
254 * MFN
255 * MFI + -------
256 * MFD + 1
257 * factor = -----------------
258 * (PDF + 1) * 2^S
259 *
260 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200261 factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
262 (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
Christophe Leroy069fa832017-07-06 10:23:22 +0200263
264 oscclk = gd->cpu_clk / factor;
265
Christophe Leroy394f9b32017-07-06 10:33:13 +0200266 if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
267 factor > 2)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200268 return oscclk / 4;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200269
Christophe Leroy48f896d2017-07-06 10:33:17 +0200270 return oscclk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200271}
272
273/* ------------------------------------------------------------------------- */
274
275#if defined(CONFIG_WATCHDOG)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200276void watchdog_reset(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200277{
Christophe Leroy48f896d2017-07-06 10:33:17 +0200278 int re_enable = disable_interrupts();
Christophe Leroy069fa832017-07-06 10:23:22 +0200279
Christophe Leroy394f9b32017-07-06 10:33:13 +0200280 reset_8xx_watchdog((immap_t __iomem *)CONFIG_SYS_IMMR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200281 if (re_enable)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200282 enable_interrupts();
Christophe Leroy069fa832017-07-06 10:23:22 +0200283}
284#endif /* CONFIG_WATCHDOG */
285
286#if defined(CONFIG_WATCHDOG)
287
Christophe Leroy394f9b32017-07-06 10:33:13 +0200288void reset_8xx_watchdog(immap_t __iomem *immr)
Christophe Leroy069fa832017-07-06 10:23:22 +0200289{
290 /*
291 * All other boards use the MPC8xx Internal Watchdog
292 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200293 out_be16(&immr->im_siu_conf.sc_swsr, 0x556c); /* write magic1 */
294 out_be16(&immr->im_siu_conf.sc_swsr, 0xaa39); /* write magic2 */
Christophe Leroy069fa832017-07-06 10:23:22 +0200295}
296#endif /* CONFIG_WATCHDOG */
297
298/*
299 * Initializes on-chip ethernet controllers.
300 * to override, implement board_eth_init()
301 */
302int cpu_eth_init(bd_t *bis)
303{
Christophe Leroy56ef30a2017-07-06 10:33:23 +0200304#if defined(CONFIG_MPC8XX_FEC)
Christophe Leroy069fa832017-07-06 10:23:22 +0200305 fec_initialize(bis);
306#endif
307 return 0;
308}