blob: 4a1698d3bca326c0d7473edaa61b3371ece1d63c [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 Glassa9dc0682019-12-28 10:44:59 -070021#include <time.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070022#include <vsprintf.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020023#include <watchdog.h>
24#include <command.h>
25#include <mpc8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020026#include <netdev.h>
27#include <asm/cache.h>
Christophe Leroy10ff63a2018-03-16 17:20:43 +010028#include <asm/cpm_8xx.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020029#include <linux/compiler.h>
30#include <asm/io.h>
31
32#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090033#include <linux/libfdt.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020034#include <fdt_support.h>
35#endif
36
37DECLARE_GLOBAL_DATA_PTR;
38
Christophe Leroy069fa832017-07-06 10:23:22 +020039/* ------------------------------------------------------------------------- */
40/* L1 i-cache */
41
Christophe Leroy48f896d2017-07-06 10:33:17 +020042int checkicache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020043{
Christophe Leroy394f9b32017-07-06 10:33:13 +020044 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
45 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020046 u32 cacheon = rd_ic_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020047 /* probe in flash memoryarea */
48 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020049 u32 m;
50 u32 lines = -1;
51
Christophe Leroy48f896d2017-07-06 10:33:17 +020052 wr_ic_cst(IDC_UNALL);
53 wr_ic_cst(IDC_INVALL);
54 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020055 __asm__ volatile ("isync");
56
Christophe Leroy48f896d2017-07-06 10:33:17 +020057 while (!((m = rd_ic_cst()) & IDC_CERR2)) {
58 wr_ic_adr(k);
59 wr_ic_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +020060 __asm__ volatile ("isync");
61
62 lines++;
Christophe Leroy48f896d2017-07-06 10:33:17 +020063 k += 0x10; /* the number of bytes in a cacheline */
Christophe Leroy069fa832017-07-06 10:23:22 +020064 }
65
Christophe Leroy48f896d2017-07-06 10:33:17 +020066 wr_ic_cst(IDC_UNALL);
67 wr_ic_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +020068
69 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +020070 wr_ic_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020071 else
Christophe Leroy48f896d2017-07-06 10:33:17 +020072 wr_ic_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020073
74 __asm__ volatile ("isync");
75
76 return lines << 4;
77};
78
79/* ------------------------------------------------------------------------- */
80/* L1 d-cache */
81/* call with cache disabled */
82
Simon Glass1ba89d72019-11-14 12:57:38 -070083static int checkdcache(void)
Christophe Leroy069fa832017-07-06 10:23:22 +020084{
Christophe Leroy394f9b32017-07-06 10:33:13 +020085 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
86 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy48f896d2017-07-06 10:33:17 +020087 u32 cacheon = rd_dc_cst() & IDC_ENABLED;
Christophe Leroy394f9b32017-07-06 10:33:13 +020088 /* probe in flash memoryarea */
89 u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff;
Christophe Leroy069fa832017-07-06 10:23:22 +020090 u32 m;
91 u32 lines = -1;
92
Christophe Leroy48f896d2017-07-06 10:33:17 +020093 wr_dc_cst(IDC_UNALL);
94 wr_dc_cst(IDC_INVALL);
95 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +020096
Christophe Leroy48f896d2017-07-06 10:33:17 +020097 while (!((m = rd_dc_cst()) & IDC_CERR2)) {
98 wr_dc_adr(k);
99 wr_dc_cst(IDC_LDLCK);
Christophe Leroy069fa832017-07-06 10:23:22 +0200100 lines++;
101 k += 0x10; /* the number of bytes in a cacheline */
102 }
103
Christophe Leroy48f896d2017-07-06 10:33:17 +0200104 wr_dc_cst(IDC_UNALL);
105 wr_dc_cst(IDC_INVALL);
Christophe Leroy069fa832017-07-06 10:23:22 +0200106
107 if (cacheon)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200108 wr_dc_cst(IDC_ENABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200109 else
Christophe Leroy48f896d2017-07-06 10:33:17 +0200110 wr_dc_cst(IDC_DISABLE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200111
112 return lines << 4;
113};
114
Simon Glass1ba89d72019-11-14 12:57:38 -0700115static int check_CPU(long clock, uint pvr, uint immr)
116{
117 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
118 uint k;
119 char buf[32];
120
121 /* the highest 16 bits should be 0x0050 for a 860 */
122
123 if (PVR_VER(pvr) != PVR_VER(PVR_8xx))
124 return -1;
125
126 k = (immr << 16) |
127 in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]);
128
129 /*
130 * Some boards use sockets so different CPUs can be used.
131 * We have to check chip version in run time.
132 */
133 switch (k) {
134 /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */
135 case 0x08010004: /* Rev. A.0 */
136 printf("MPC866xxxZPnnA");
137 break;
138 case 0x08000003: /* Rev. 0.3 */
139 printf("MPC866xxxZPnn");
140 break;
141 case 0x09000000: /* 870/875/880/885 */
142 puts("MPC885ZPnn");
143 break;
144
145 default:
146 printf("unknown MPC86x (0x%08x)", k);
147 break;
148 }
149
150 printf(" at %s MHz: ", strmhz(buf, clock));
151
152 print_size(checkicache(), " I-Cache ");
153 print_size(checkdcache(), " D-Cache");
154
155 /* do we have a FEC (860T/P or 852/859/866/885)? */
156
157 out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678);
158 if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678)
159 printf(" FEC present");
160
161 putc('\n');
162
163 return 0;
164}
165
166/* ------------------------------------------------------------------------- */
167
168int checkcpu(void)
169{
170 ulong clock = gd->cpu_clk;
171 uint immr = get_immr(); /* Return full IMMR contents */
172 uint pvr = get_pvr();
173
174 puts("CPU: ");
175
176 return check_CPU(clock, pvr, immr);
177}
178
Christophe Leroy069fa832017-07-06 10:23:22 +0200179/* ------------------------------------------------------------------------- */
180
Christophe Leroy48f896d2017-07-06 10:33:17 +0200181void upmconfig(uint upm, uint *table, uint size)
Christophe Leroy069fa832017-07-06 10:23:22 +0200182{
183 uint i;
184 uint addr = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200185 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
186 memctl8xx_t __iomem *memctl = &immap->im_memctl;
Christophe Leroy069fa832017-07-06 10:23:22 +0200187
188 for (i = 0; i < size; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200189 out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */
190 out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */
Christophe Leroy069fa832017-07-06 10:23:22 +0200191 addr++;
192 }
193}
194
195/* ------------------------------------------------------------------------- */
196
Christophe Leroy48f896d2017-07-06 10:33:17 +0200197int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Christophe Leroy069fa832017-07-06 10:23:22 +0200198{
199 ulong msr, addr;
200
Christophe Leroy394f9b32017-07-06 10:33:13 +0200201 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200202
Christophe Leroy394f9b32017-07-06 10:33:13 +0200203 /* Checkstop Reset enable */
204 setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR);
Christophe Leroy069fa832017-07-06 10:23:22 +0200205
206 /* Interrupts and MMU off */
207 __asm__ volatile ("mtspr 81, 0");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200208 __asm__ volatile ("mfmsr %0" : "=r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200209
210 msr &= ~0x1030;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200211 __asm__ volatile ("mtmsr %0" : : "r" (msr));
Christophe Leroy069fa832017-07-06 10:23:22 +0200212
213 /*
214 * Trying to execute the next instruction at a non-existing address
215 * should cause a machine check, resulting in reset
216 */
217#ifdef CONFIG_SYS_RESET_ADDRESS
218 addr = CONFIG_SYS_RESET_ADDRESS;
219#else
220 /*
Christophe Leroy48f896d2017-07-06 10:33:17 +0200221 * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address,
222 * CONFIG_SYS_MONITOR_BASE - sizeof (ulong) is usually a valid address.
223 * Better pick an address known to be invalid on your system and assign
224 * it to CONFIG_SYS_RESET_ADDRESS.
Christophe Leroy069fa832017-07-06 10:23:22 +0200225 * "(ulong)-1" used to be a good choice for many systems...
226 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200227 addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong);
Christophe Leroy069fa832017-07-06 10:23:22 +0200228#endif
Christophe Leroy48f896d2017-07-06 10:33:17 +0200229 ((void (*)(void)) addr)();
Christophe Leroy069fa832017-07-06 10:23:22 +0200230 return 1;
231}
232
233/* ------------------------------------------------------------------------- */
234
235/*
236 * Get timebase clock frequency (like cpu_clk in Hz)
237 *
238 * See sections 14.2 and 14.6 of the User's Manual
239 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200240unsigned long get_tbclk(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200241{
Christophe Leroy5c59bdf2018-03-16 17:20:33 +0100242 immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200243 ulong oscclk, factor, pll;
244
Christophe Leroy394f9b32017-07-06 10:33:13 +0200245 if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200246 return gd->cpu_clk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200247
Christophe Leroy394f9b32017-07-06 10:33:13 +0200248 pll = in_be32(&immap->im_clkrst.car_plprcr);
Christophe Leroy069fa832017-07-06 10:23:22 +0200249
250#define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT)
251
252 /*
253 * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication
254 * factor is calculated as follows:
255 *
256 * MFN
257 * MFI + -------
258 * MFD + 1
259 * factor = -----------------
260 * (PDF + 1) * 2^S
261 *
262 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200263 factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) /
264 (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S));
Christophe Leroy069fa832017-07-06 10:23:22 +0200265
266 oscclk = gd->cpu_clk / factor;
267
Christophe Leroy394f9b32017-07-06 10:33:13 +0200268 if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 ||
269 factor > 2)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200270 return oscclk / 4;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200271
Christophe Leroy48f896d2017-07-06 10:33:17 +0200272 return oscclk / 16;
Christophe Leroy069fa832017-07-06 10:23:22 +0200273}
274
Christophe Leroy069fa832017-07-06 10:23:22 +0200275/*
276 * Initializes on-chip ethernet controllers.
277 * to override, implement board_eth_init()
278 */
279int cpu_eth_init(bd_t *bis)
280{
Christophe Leroy56ef30a2017-07-06 10:33:23 +0200281#if defined(CONFIG_MPC8XX_FEC)
Christophe Leroy069fa832017-07-06 10:23:22 +0200282 fec_initialize(bis);
283#endif
284 return 0;
285}