Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2002 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 5 | */ |
| 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 Glass | 1fa70f8 | 2019-11-14 12:57:34 -0700 | [diff] [blame] | 20 | #include <cpu_func.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 21 | #include <display_options.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 22 | #include <net.h> |
Simon Glass | a9dc068 | 2019-12-28 10:44:59 -0700 | [diff] [blame] | 23 | #include <time.h> |
Simon Glass | f5c208d | 2019-11-14 12:57:20 -0700 | [diff] [blame] | 24 | #include <vsprintf.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 25 | #include <watchdog.h> |
| 26 | #include <command.h> |
| 27 | #include <mpc8xx.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 28 | #include <netdev.h> |
| 29 | #include <asm/cache.h> |
Christophe Leroy | 10ff63a | 2018-03-16 17:20:43 +0100 | [diff] [blame] | 30 | #include <asm/cpm_8xx.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 31 | #include <asm/global_data.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 32 | #include <linux/compiler.h> |
| 33 | #include <asm/io.h> |
| 34 | |
| 35 | #if defined(CONFIG_OF_LIBFDT) |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 36 | #include <linux/libfdt.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 37 | #include <fdt_support.h> |
| 38 | #endif |
| 39 | |
| 40 | DECLARE_GLOBAL_DATA_PTR; |
| 41 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 42 | /* ------------------------------------------------------------------------- */ |
| 43 | /* L1 i-cache */ |
| 44 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 45 | int checkicache(void) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 46 | { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 47 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 48 | memctl8xx_t __iomem *memctl = &immap->im_memctl; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 49 | u32 cacheon = rd_ic_cst() & IDC_ENABLED; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 50 | /* probe in flash memoryarea */ |
| 51 | u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 52 | u32 m; |
| 53 | u32 lines = -1; |
| 54 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 55 | wr_ic_cst(IDC_UNALL); |
| 56 | wr_ic_cst(IDC_INVALL); |
| 57 | wr_ic_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 58 | __asm__ volatile ("isync"); |
| 59 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 60 | while (!((m = rd_ic_cst()) & IDC_CERR2)) { |
| 61 | wr_ic_adr(k); |
| 62 | wr_ic_cst(IDC_LDLCK); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 63 | __asm__ volatile ("isync"); |
| 64 | |
| 65 | lines++; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 66 | k += 0x10; /* the number of bytes in a cacheline */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 69 | wr_ic_cst(IDC_UNALL); |
| 70 | wr_ic_cst(IDC_INVALL); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 71 | |
| 72 | if (cacheon) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 73 | wr_ic_cst(IDC_ENABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 74 | else |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 75 | wr_ic_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 76 | |
| 77 | __asm__ volatile ("isync"); |
| 78 | |
| 79 | return lines << 4; |
| 80 | }; |
| 81 | |
| 82 | /* ------------------------------------------------------------------------- */ |
| 83 | /* L1 d-cache */ |
| 84 | /* call with cache disabled */ |
| 85 | |
Simon Glass | 1ba89d7 | 2019-11-14 12:57:38 -0700 | [diff] [blame] | 86 | static int checkdcache(void) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 87 | { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 88 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 89 | memctl8xx_t __iomem *memctl = &immap->im_memctl; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 90 | u32 cacheon = rd_dc_cst() & IDC_ENABLED; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 91 | /* probe in flash memoryarea */ |
| 92 | u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 93 | u32 m; |
| 94 | u32 lines = -1; |
| 95 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 96 | wr_dc_cst(IDC_UNALL); |
| 97 | wr_dc_cst(IDC_INVALL); |
| 98 | wr_dc_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 99 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 100 | while (!((m = rd_dc_cst()) & IDC_CERR2)) { |
| 101 | wr_dc_adr(k); |
| 102 | wr_dc_cst(IDC_LDLCK); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 103 | lines++; |
| 104 | k += 0x10; /* the number of bytes in a cacheline */ |
| 105 | } |
| 106 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 107 | wr_dc_cst(IDC_UNALL); |
| 108 | wr_dc_cst(IDC_INVALL); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 109 | |
| 110 | if (cacheon) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 111 | wr_dc_cst(IDC_ENABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 112 | else |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 113 | wr_dc_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 114 | |
| 115 | return lines << 4; |
| 116 | }; |
| 117 | |
Simon Glass | 1ba89d7 | 2019-11-14 12:57:38 -0700 | [diff] [blame] | 118 | static 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 | |
| 171 | int 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 Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 182 | /* ------------------------------------------------------------------------- */ |
| 183 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 184 | void upmconfig(uint upm, uint *table, uint size) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 185 | { |
| 186 | uint i; |
| 187 | uint addr = 0; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 188 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 189 | memctl8xx_t __iomem *memctl = &immap->im_memctl; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 190 | |
| 191 | for (i = 0; i < size; i++) { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 192 | out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */ |
| 193 | out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 194 | addr++; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* ------------------------------------------------------------------------- */ |
| 199 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 200 | int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 201 | { |
| 202 | ulong msr, addr; |
| 203 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 204 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 205 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 206 | /* Checkstop Reset enable */ |
| 207 | setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 208 | |
| 209 | /* Interrupts and MMU off */ |
| 210 | __asm__ volatile ("mtspr 81, 0"); |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 211 | __asm__ volatile ("mfmsr %0" : "=r" (msr)); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 212 | |
| 213 | msr &= ~0x1030; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 214 | __asm__ volatile ("mtmsr %0" : : "r" (msr)); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Trying to execute the next instruction at a non-existing address |
| 218 | * should cause a machine check, resulting in reset |
Ovidiu Panait | fe19a54 | 2022-01-31 09:20:21 +0200 | [diff] [blame] | 219 | * |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 220 | * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address, |
| 221 | * CONFIG_SYS_MONITOR_BASE - sizeof (ulong) is usually a valid address. |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 222 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 223 | addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong); |
Ovidiu Panait | fe19a54 | 2022-01-31 09:20:21 +0200 | [diff] [blame] | 224 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 225 | ((void (*)(void)) addr)(); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 226 | 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 Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 236 | unsigned long get_tbclk(void) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 237 | { |
Christophe Leroy | 5c59bdf | 2018-03-16 17:20:33 +0100 | [diff] [blame] | 238 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 239 | ulong oscclk, factor, pll; |
| 240 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 241 | if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 242 | return gd->cpu_clk / 16; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 243 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 244 | pll = in_be32(&immap->im_clkrst.car_plprcr); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 245 | |
| 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 Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 259 | factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) / |
| 260 | (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S)); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 261 | |
| 262 | oscclk = gd->cpu_clk / factor; |
| 263 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 264 | if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 || |
| 265 | factor > 2) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 266 | return oscclk / 4; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 267 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 268 | return oscclk / 16; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 269 | } |