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