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 | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 21 | #include <net.h> |
Simon Glass | a9dc068 | 2019-12-28 10:44:59 -0700 | [diff] [blame] | 22 | #include <time.h> |
Simon Glass | f5c208d | 2019-11-14 12:57:20 -0700 | [diff] [blame] | 23 | #include <vsprintf.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 24 | #include <watchdog.h> |
| 25 | #include <command.h> |
| 26 | #include <mpc8xx.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 27 | #include <netdev.h> |
| 28 | #include <asm/cache.h> |
Christophe Leroy | 10ff63a | 2018-03-16 17:20:43 +0100 | [diff] [blame] | 29 | #include <asm/cpm_8xx.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 30 | #include <asm/global_data.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 31 | #include <linux/compiler.h> |
| 32 | #include <asm/io.h> |
| 33 | |
| 34 | #if defined(CONFIG_OF_LIBFDT) |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 35 | #include <linux/libfdt.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 36 | #include <fdt_support.h> |
| 37 | #endif |
| 38 | |
| 39 | DECLARE_GLOBAL_DATA_PTR; |
| 40 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 41 | /* ------------------------------------------------------------------------- */ |
| 42 | /* L1 i-cache */ |
| 43 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 44 | int checkicache(void) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 45 | { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 46 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 47 | memctl8xx_t __iomem *memctl = &immap->im_memctl; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 48 | u32 cacheon = rd_ic_cst() & IDC_ENABLED; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 49 | /* probe in flash memoryarea */ |
| 50 | u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 51 | u32 m; |
| 52 | u32 lines = -1; |
| 53 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 54 | wr_ic_cst(IDC_UNALL); |
| 55 | wr_ic_cst(IDC_INVALL); |
| 56 | wr_ic_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 57 | __asm__ volatile ("isync"); |
| 58 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 59 | while (!((m = rd_ic_cst()) & IDC_CERR2)) { |
| 60 | wr_ic_adr(k); |
| 61 | wr_ic_cst(IDC_LDLCK); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 62 | __asm__ volatile ("isync"); |
| 63 | |
| 64 | lines++; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 65 | k += 0x10; /* the number of bytes in a cacheline */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 68 | wr_ic_cst(IDC_UNALL); |
| 69 | wr_ic_cst(IDC_INVALL); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 70 | |
| 71 | if (cacheon) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 72 | wr_ic_cst(IDC_ENABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 73 | else |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 74 | wr_ic_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 75 | |
| 76 | __asm__ volatile ("isync"); |
| 77 | |
| 78 | return lines << 4; |
| 79 | }; |
| 80 | |
| 81 | /* ------------------------------------------------------------------------- */ |
| 82 | /* L1 d-cache */ |
| 83 | /* call with cache disabled */ |
| 84 | |
Simon Glass | 1ba89d7 | 2019-11-14 12:57:38 -0700 | [diff] [blame] | 85 | static int checkdcache(void) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 86 | { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 87 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 88 | memctl8xx_t __iomem *memctl = &immap->im_memctl; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 89 | u32 cacheon = rd_dc_cst() & IDC_ENABLED; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 90 | /* probe in flash memoryarea */ |
| 91 | u32 k = in_be32(&memctl->memc_br0) & ~0x00007fff; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 92 | u32 m; |
| 93 | u32 lines = -1; |
| 94 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 95 | wr_dc_cst(IDC_UNALL); |
| 96 | wr_dc_cst(IDC_INVALL); |
| 97 | wr_dc_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 98 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 99 | while (!((m = rd_dc_cst()) & IDC_CERR2)) { |
| 100 | wr_dc_adr(k); |
| 101 | wr_dc_cst(IDC_LDLCK); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 102 | lines++; |
| 103 | k += 0x10; /* the number of bytes in a cacheline */ |
| 104 | } |
| 105 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 106 | wr_dc_cst(IDC_UNALL); |
| 107 | wr_dc_cst(IDC_INVALL); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 108 | |
| 109 | if (cacheon) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 110 | wr_dc_cst(IDC_ENABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 111 | else |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 112 | wr_dc_cst(IDC_DISABLE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 113 | |
| 114 | return lines << 4; |
| 115 | }; |
| 116 | |
Simon Glass | 1ba89d7 | 2019-11-14 12:57:38 -0700 | [diff] [blame] | 117 | static int check_CPU(long clock, uint pvr, uint immr) |
| 118 | { |
| 119 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 120 | uint k; |
| 121 | char buf[32]; |
| 122 | |
| 123 | /* the highest 16 bits should be 0x0050 for a 860 */ |
| 124 | |
| 125 | if (PVR_VER(pvr) != PVR_VER(PVR_8xx)) |
| 126 | return -1; |
| 127 | |
| 128 | k = (immr << 16) | |
| 129 | in_be16(&immap->im_cpm.cp_dparam16[PROFF_REVNUM / sizeof(u16)]); |
| 130 | |
| 131 | /* |
| 132 | * Some boards use sockets so different CPUs can be used. |
| 133 | * We have to check chip version in run time. |
| 134 | */ |
| 135 | switch (k) { |
| 136 | /* MPC866P/MPC866T/MPC859T/MPC859DSL/MPC852T */ |
| 137 | case 0x08010004: /* Rev. A.0 */ |
| 138 | printf("MPC866xxxZPnnA"); |
| 139 | break; |
| 140 | case 0x08000003: /* Rev. 0.3 */ |
| 141 | printf("MPC866xxxZPnn"); |
| 142 | break; |
| 143 | case 0x09000000: /* 870/875/880/885 */ |
| 144 | puts("MPC885ZPnn"); |
| 145 | break; |
| 146 | |
| 147 | default: |
| 148 | printf("unknown MPC86x (0x%08x)", k); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | printf(" at %s MHz: ", strmhz(buf, clock)); |
| 153 | |
| 154 | print_size(checkicache(), " I-Cache "); |
| 155 | print_size(checkdcache(), " D-Cache"); |
| 156 | |
| 157 | /* do we have a FEC (860T/P or 852/859/866/885)? */ |
| 158 | |
| 159 | out_be32(&immap->im_cpm.cp_fec.fec_addr_low, 0x12345678); |
| 160 | if (in_be32(&immap->im_cpm.cp_fec.fec_addr_low) == 0x12345678) |
| 161 | printf(" FEC present"); |
| 162 | |
| 163 | putc('\n'); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* ------------------------------------------------------------------------- */ |
| 169 | |
| 170 | int checkcpu(void) |
| 171 | { |
| 172 | ulong clock = gd->cpu_clk; |
| 173 | uint immr = get_immr(); /* Return full IMMR contents */ |
| 174 | uint pvr = get_pvr(); |
| 175 | |
| 176 | puts("CPU: "); |
| 177 | |
| 178 | return check_CPU(clock, pvr, immr); |
| 179 | } |
| 180 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 181 | /* ------------------------------------------------------------------------- */ |
| 182 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 183 | void upmconfig(uint upm, uint *table, uint size) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 184 | { |
| 185 | uint i; |
| 186 | uint addr = 0; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 187 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 188 | memctl8xx_t __iomem *memctl = &immap->im_memctl; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 189 | |
| 190 | for (i = 0; i < size; i++) { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 191 | out_be32(&memctl->memc_mdr, table[i]); /* (16-15) */ |
| 192 | out_be32(&memctl->memc_mcr, addr | upm); /* (16-16) */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 193 | addr++; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* ------------------------------------------------------------------------- */ |
| 198 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 199 | 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] | 200 | { |
| 201 | ulong msr, addr; |
| 202 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 203 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 204 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 205 | /* Checkstop Reset enable */ |
| 206 | setbits_be32(&immap->im_clkrst.car_plprcr, PLPRCR_CSR); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 207 | |
| 208 | /* Interrupts and MMU off */ |
| 209 | __asm__ volatile ("mtspr 81, 0"); |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 210 | __asm__ volatile ("mfmsr %0" : "=r" (msr)); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 211 | |
| 212 | msr &= ~0x1030; |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 213 | __asm__ volatile ("mtmsr %0" : : "r" (msr)); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * Trying to execute the next instruction at a non-existing address |
| 217 | * should cause a machine check, resulting in reset |
Ovidiu Panait | fe19a54 | 2022-01-31 09:20:21 +0200 | [diff] [blame^] | 218 | * |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 219 | * note: when CONFIG_SYS_MONITOR_BASE points to a RAM address, |
| 220 | * CONFIG_SYS_MONITOR_BASE - sizeof (ulong) is usually a valid address. |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 221 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 222 | addr = CONFIG_SYS_MONITOR_BASE - sizeof(ulong); |
Ovidiu Panait | fe19a54 | 2022-01-31 09:20:21 +0200 | [diff] [blame^] | 223 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 224 | ((void (*)(void)) addr)(); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | /* ------------------------------------------------------------------------- */ |
| 229 | |
| 230 | /* |
| 231 | * Get timebase clock frequency (like cpu_clk in Hz) |
| 232 | * |
| 233 | * See sections 14.2 and 14.6 of the User's Manual |
| 234 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 235 | unsigned long get_tbclk(void) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 236 | { |
Christophe Leroy | 5c59bdf | 2018-03-16 17:20:33 +0100 | [diff] [blame] | 237 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 238 | ulong oscclk, factor, pll; |
| 239 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 240 | if (in_be32(&immap->im_clkrst.car_sccr) & SCCR_TBS) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 241 | return gd->cpu_clk / 16; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 242 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 243 | pll = in_be32(&immap->im_clkrst.car_plprcr); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 244 | |
| 245 | #define PLPRCR_val(a) ((pll & PLPRCR_ ## a ## _MSK) >> PLPRCR_ ## a ## _SHIFT) |
| 246 | |
| 247 | /* |
| 248 | * For newer PQ1 chips (MPC866/87x/88x families), PLL multiplication |
| 249 | * factor is calculated as follows: |
| 250 | * |
| 251 | * MFN |
| 252 | * MFI + ------- |
| 253 | * MFD + 1 |
| 254 | * factor = ----------------- |
| 255 | * (PDF + 1) * 2^S |
| 256 | * |
| 257 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 258 | factor = (PLPRCR_val(MFI) + PLPRCR_val(MFN) / (PLPRCR_val(MFD) + 1)) / |
| 259 | (PLPRCR_val(PDF) + 1) / (1 << PLPRCR_val(S)); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 260 | |
| 261 | oscclk = gd->cpu_clk / factor; |
| 262 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 263 | if ((in_be32(&immap->im_clkrst.car_sccr) & SCCR_RTSEL) == 0 || |
| 264 | factor > 2) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 265 | return oscclk / 4; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 266 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 267 | return oscclk / 16; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 270 | /* |
| 271 | * Initializes on-chip ethernet controllers. |
| 272 | * to override, implement board_eth_init() |
| 273 | */ |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 274 | int cpu_eth_init(struct bd_info *bis) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 275 | { |
Christophe Leroy | 56ef30a | 2017-07-06 10:33:23 +0200 | [diff] [blame] | 276 | #if defined(CONFIG_MPC8XX_FEC) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 277 | fec_initialize(bis); |
| 278 | #endif |
| 279 | return 0; |
| 280 | } |