Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/kernel/cpu/init.c |
| 3 | * |
| 4 | * CPU init code |
| 5 | * |
Paul Mundt | 7dd6662 | 2009-08-15 07:43:21 +0900 | [diff] [blame] | 6 | * Copyright (C) 2002 - 2009 Paul Mundt |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 7 | * Copyright (C) 2003 Richard Curnow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file "COPYING" in the main directory of this archive |
| 11 | * for more details. |
| 12 | */ |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 15 | #include <linux/mm.h> |
Paul Mundt | cd01204 | 2007-12-10 15:50:28 +0900 | [diff] [blame] | 16 | #include <linux/log2.h> |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 17 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/processor.h> |
| 19 | #include <asm/uaccess.h> |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 20 | #include <asm/page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/system.h> |
| 22 | #include <asm/cacheflush.h> |
| 23 | #include <asm/cache.h> |
Paul Mundt | cd01204 | 2007-12-10 15:50:28 +0900 | [diff] [blame] | 24 | #include <asm/elf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/io.h> |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 26 | #include <asm/smp.h> |
Paul Mundt | c881cbc | 2007-11-10 20:18:18 +0900 | [diff] [blame] | 27 | #ifdef CONFIG_SUPERH32 |
| 28 | #include <asm/ubc.h> |
| 29 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame^] | 31 | #ifdef CONFIG_SH_FPU |
| 32 | #define cpu_has_fpu 1 |
| 33 | #else |
| 34 | #define cpu_has_fpu 0 |
| 35 | #endif |
| 36 | |
| 37 | #ifdef CONFIG_SH_DSP |
| 38 | #define cpu_has_dsp 1 |
| 39 | #else |
| 40 | #define cpu_has_dsp 0 |
| 41 | #endif |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Generic wrapper for command line arguments to disable on-chip |
| 45 | * peripherals (nofpu, nodsp, and so forth). |
| 46 | */ |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame^] | 47 | #define onchip_setup(x) \ |
| 48 | static int x##_disabled __initdata = !cpu_has_##x; \ |
| 49 | \ |
| 50 | static int __init x##_setup(char *opts) \ |
| 51 | { \ |
| 52 | x##_disabled = 1; \ |
| 53 | return 1; \ |
| 54 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | __setup("no" __stringify(x), x##_setup); |
| 56 | |
| 57 | onchip_setup(fpu); |
| 58 | onchip_setup(dsp); |
| 59 | |
Paul Mundt | 45ed285 | 2007-03-08 18:12:17 +0900 | [diff] [blame] | 60 | #ifdef CONFIG_SPECULATIVE_EXECUTION |
| 61 | #define CPUOPM 0xff2f0000 |
| 62 | #define CPUOPM_RABD (1 << 5) |
| 63 | |
| 64 | static void __init speculative_execution_init(void) |
| 65 | { |
| 66 | /* Clear RABD */ |
| 67 | ctrl_outl(ctrl_inl(CPUOPM) & ~CPUOPM_RABD, CPUOPM); |
| 68 | |
| 69 | /* Flush the update */ |
| 70 | (void)ctrl_inl(CPUOPM); |
| 71 | ctrl_barrier(); |
| 72 | } |
| 73 | #else |
| 74 | #define speculative_execution_init() do { } while (0) |
| 75 | #endif |
| 76 | |
Paul Mundt | 7dd6662 | 2009-08-15 07:43:21 +0900 | [diff] [blame] | 77 | #ifdef CONFIG_CPU_SH4A |
| 78 | #define EXPMASK 0xff2f0004 |
| 79 | #define EXPMASK_RTEDS (1 << 0) |
| 80 | #define EXPMASK_BRDSSLP (1 << 1) |
| 81 | #define EXPMASK_MMCAW (1 << 4) |
| 82 | |
| 83 | static void __init expmask_init(void) |
| 84 | { |
| 85 | unsigned long expmask = __raw_readl(EXPMASK); |
| 86 | |
| 87 | /* |
| 88 | * Future proofing. |
| 89 | * |
Paul Mundt | 6e8a0d1 | 2009-12-04 16:22:11 +0900 | [diff] [blame] | 90 | * Disable support for slottable sleep instruction, non-nop |
| 91 | * instructions in the rte delay slot, and associative writes to |
| 92 | * the memory-mapped cache array. |
Paul Mundt | 7dd6662 | 2009-08-15 07:43:21 +0900 | [diff] [blame] | 93 | */ |
Paul Mundt | 6e8a0d1 | 2009-12-04 16:22:11 +0900 | [diff] [blame] | 94 | expmask &= ~(EXPMASK_RTEDS | EXPMASK_BRDSSLP | EXPMASK_MMCAW); |
Paul Mundt | 7dd6662 | 2009-08-15 07:43:21 +0900 | [diff] [blame] | 95 | |
| 96 | __raw_writel(expmask, EXPMASK); |
| 97 | ctrl_barrier(); |
| 98 | } |
| 99 | #else |
| 100 | #define expmask_init() do { } while (0) |
| 101 | #endif |
| 102 | |
Kuninori Morimoto | fab88d9 | 2009-06-02 02:49:20 +0000 | [diff] [blame] | 103 | /* 2nd-level cache init */ |
| 104 | void __uses_jump_to_uncached __attribute__ ((weak)) l2_cache_init(void) |
| 105 | { |
| 106 | } |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* |
| 109 | * Generic first-level cache init |
| 110 | */ |
Paul Mundt | 27a511c | 2007-11-10 20:25:28 +0900 | [diff] [blame] | 111 | #ifdef CONFIG_SUPERH32 |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 112 | static void __uses_jump_to_uncached cache_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | unsigned long ccr, flags; |
| 115 | |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 116 | jump_to_uncached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | ccr = ctrl_inl(CCR); |
| 118 | |
| 119 | /* |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 120 | * At this point we don't know whether the cache is enabled or not - a |
| 121 | * bootloader may have enabled it. There are at least 2 things that |
| 122 | * could be dirty in the cache at this point: |
| 123 | * 1. kernel command line set up by boot loader |
| 124 | * 2. spilled registers from the prolog of this function |
| 125 | * => before re-initialising the cache, we must do a purge of the whole |
| 126 | * cache out to memory for safety. As long as nothing is spilled |
| 127 | * during the loop to lines that have already been done, this is safe. |
| 128 | * - RPC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | */ |
| 130 | if (ccr & CCR_CACHE_ENABLE) { |
| 131 | unsigned long ways, waysize, addrstart; |
| 132 | |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 133 | waysize = current_cpu_data.dcache.sets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Yoshinori Sato | 9d4436a | 2006-11-05 15:40:13 +0900 | [diff] [blame] | 135 | #ifdef CCR_CACHE_ORA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* |
| 137 | * If the OC is already in RAM mode, we only have |
| 138 | * half of the entries to flush.. |
| 139 | */ |
| 140 | if (ccr & CCR_CACHE_ORA) |
| 141 | waysize >>= 1; |
Yoshinori Sato | 9d4436a | 2006-11-05 15:40:13 +0900 | [diff] [blame] | 142 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 144 | waysize <<= current_cpu_data.dcache.entry_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | #ifdef CCR_CACHE_EMODE |
| 147 | /* If EMODE is not set, we only have 1 way to flush. */ |
| 148 | if (!(ccr & CCR_CACHE_EMODE)) |
| 149 | ways = 1; |
| 150 | else |
| 151 | #endif |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 152 | ways = current_cpu_data.dcache.ways; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | addrstart = CACHE_OC_ADDRESS_ARRAY; |
| 155 | do { |
| 156 | unsigned long addr; |
| 157 | |
| 158 | for (addr = addrstart; |
| 159 | addr < addrstart + waysize; |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 160 | addr += current_cpu_data.dcache.linesz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | ctrl_outl(0, addr); |
| 162 | |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 163 | addrstart += current_cpu_data.dcache.way_incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } while (--ways); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Default CCR values .. enable the caches |
| 169 | * and invalidate them immediately.. |
| 170 | */ |
| 171 | flags = CCR_CACHE_ENABLE | CCR_CACHE_INVALIDATE; |
| 172 | |
| 173 | #ifdef CCR_CACHE_EMODE |
| 174 | /* Force EMODE if possible */ |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 175 | if (current_cpu_data.dcache.ways > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | flags |= CCR_CACHE_EMODE; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 177 | else |
| 178 | flags &= ~CCR_CACHE_EMODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | #endif |
| 180 | |
Paul Mundt | e7bd34a | 2007-07-31 17:07:28 +0900 | [diff] [blame] | 181 | #if defined(CONFIG_CACHE_WRITETHROUGH) |
| 182 | /* Write-through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | flags |= CCR_CACHE_WT; |
Paul Mundt | e7bd34a | 2007-07-31 17:07:28 +0900 | [diff] [blame] | 184 | #elif defined(CONFIG_CACHE_WRITEBACK) |
| 185 | /* Write-back */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | flags |= CCR_CACHE_CB; |
Paul Mundt | e7bd34a | 2007-07-31 17:07:28 +0900 | [diff] [blame] | 187 | #else |
| 188 | /* Off */ |
| 189 | flags &= ~CCR_CACHE_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | #endif |
| 191 | |
Kuninori Morimoto | fab88d9 | 2009-06-02 02:49:20 +0000 | [diff] [blame] | 192 | l2_cache_init(); |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | ctrl_outl(flags, CCR); |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 195 | back_to_cached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
Paul Mundt | 27a511c | 2007-11-10 20:25:28 +0900 | [diff] [blame] | 197 | #else |
| 198 | #define cache_init() do { } while (0) |
| 199 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Paul Mundt | cd01204 | 2007-12-10 15:50:28 +0900 | [diff] [blame] | 201 | #define CSHAPE(totalsize, linesize, assoc) \ |
| 202 | ((totalsize & ~0xff) | (linesize << 4) | assoc) |
| 203 | |
| 204 | #define CACHE_DESC_SHAPE(desc) \ |
| 205 | CSHAPE((desc).way_size * (desc).ways, ilog2((desc).linesz), (desc).ways) |
| 206 | |
| 207 | static void detect_cache_shape(void) |
| 208 | { |
| 209 | l1d_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.dcache); |
| 210 | |
| 211 | if (current_cpu_data.dcache.flags & SH_CACHE_COMBINED) |
| 212 | l1i_cache_shape = l1d_cache_shape; |
| 213 | else |
| 214 | l1i_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.icache); |
| 215 | |
| 216 | if (current_cpu_data.flags & CPU_HAS_L2_CACHE) |
| 217 | l2_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.scache); |
| 218 | else |
| 219 | l2_cache_shape = -1; /* No S-cache */ |
| 220 | } |
| 221 | |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame^] | 222 | static void __init fpu_init(void) |
| 223 | { |
| 224 | /* Disable the FPU */ |
| 225 | if (fpu_disabled && (current_cpu_data.flags & CPU_HAS_FPU)) { |
| 226 | printk("FPU Disabled\n"); |
| 227 | current_cpu_data.flags &= ~CPU_HAS_FPU; |
| 228 | } |
| 229 | |
| 230 | disable_fpu(); |
| 231 | clear_used_math(); |
| 232 | } |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | #ifdef CONFIG_SH_DSP |
| 235 | static void __init release_dsp(void) |
| 236 | { |
| 237 | unsigned long sr; |
| 238 | |
| 239 | /* Clear SR.DSP bit */ |
| 240 | __asm__ __volatile__ ( |
| 241 | "stc\tsr, %0\n\t" |
| 242 | "and\t%1, %0\n\t" |
| 243 | "ldc\t%0, sr\n\t" |
| 244 | : "=&r" (sr) |
| 245 | : "r" (~SR_DSP) |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | static void __init dsp_init(void) |
| 250 | { |
| 251 | unsigned long sr; |
| 252 | |
| 253 | /* |
| 254 | * Set the SR.DSP bit, wait for one instruction, and then read |
| 255 | * back the SR value. |
| 256 | */ |
| 257 | __asm__ __volatile__ ( |
| 258 | "stc\tsr, %0\n\t" |
| 259 | "or\t%1, %0\n\t" |
| 260 | "ldc\t%0, sr\n\t" |
| 261 | "nop\n\t" |
| 262 | "stc\tsr, %0\n\t" |
| 263 | : "=&r" (sr) |
| 264 | : "r" (SR_DSP) |
| 265 | ); |
| 266 | |
| 267 | /* If the DSP bit is still set, this CPU has a DSP */ |
| 268 | if (sr & SR_DSP) |
Paul Mundt | 11c1965 | 2006-12-25 10:19:56 +0900 | [diff] [blame] | 269 | current_cpu_data.flags |= CPU_HAS_DSP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame^] | 271 | /* Disable the DSP */ |
| 272 | if (dsp_disabled && (current_cpu_data.flags & CPU_HAS_DSP)) { |
| 273 | printk("DSP Disabled\n"); |
| 274 | current_cpu_data.flags &= ~CPU_HAS_DSP; |
| 275 | } |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* Now that we've determined the DSP status, clear the DSP bit. */ |
| 278 | release_dsp(); |
| 279 | } |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame^] | 280 | #else |
| 281 | static inline void __init dsp_init(void) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | #endif /* CONFIG_SH_DSP */ |
| 283 | |
| 284 | /** |
| 285 | * sh_cpu_init |
| 286 | * |
| 287 | * This is our initial entry point for each CPU, and is invoked on the boot |
| 288 | * CPU prior to calling start_kernel(). For SMP, a combination of this and |
| 289 | * start_secondary() will bring up each processor to a ready state prior |
| 290 | * to hand forking the idle loop. |
| 291 | * |
| 292 | * We do all of the basic processor init here, including setting up the |
| 293 | * caches, FPU, DSP, kicking the UBC, etc. By the time start_kernel() is |
| 294 | * hit (and subsequently platform_setup()) things like determining the |
| 295 | * CPU subtype and initial configuration will all be done. |
| 296 | * |
| 297 | * Each processor family is still responsible for doing its own probing |
| 298 | * and cache configuration in detect_cpu_and_cache_system(). |
| 299 | */ |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 300 | |
Paul Mundt | b2839ed | 2008-03-06 12:43:38 +0900 | [diff] [blame] | 301 | asmlinkage void __init sh_cpu_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 303 | current_thread_info()->cpu = hard_smp_processor_id(); |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | /* First, probe the CPU */ |
| 306 | detect_cpu_and_cache_system(); |
| 307 | |
Paul Mundt | ffe1b4e | 2007-03-12 16:15:22 +0900 | [diff] [blame] | 308 | if (current_cpu_data.type == CPU_SH_NONE) |
| 309 | panic("Unknown CPU"); |
| 310 | |
Paul Mundt | 27a511c | 2007-11-10 20:25:28 +0900 | [diff] [blame] | 311 | /* First setup the rest of the I-cache info */ |
| 312 | current_cpu_data.icache.entry_mask = current_cpu_data.icache.way_incr - |
| 313 | current_cpu_data.icache.linesz; |
| 314 | |
| 315 | current_cpu_data.icache.way_size = current_cpu_data.icache.sets * |
| 316 | current_cpu_data.icache.linesz; |
| 317 | |
| 318 | /* And the D-cache too */ |
| 319 | current_cpu_data.dcache.entry_mask = current_cpu_data.dcache.way_incr - |
| 320 | current_cpu_data.dcache.linesz; |
| 321 | |
| 322 | current_cpu_data.dcache.way_size = current_cpu_data.dcache.sets * |
| 323 | current_cpu_data.dcache.linesz; |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* Init the cache */ |
| 326 | cache_init(); |
| 327 | |
Paul Mundt | cd01204 | 2007-12-10 15:50:28 +0900 | [diff] [blame] | 328 | if (raw_smp_processor_id() == 0) { |
Paul Mundt | aba1030 | 2007-09-21 18:32:32 +0900 | [diff] [blame] | 329 | shm_align_mask = max_t(unsigned long, |
| 330 | current_cpu_data.dcache.way_size - 1, |
| 331 | PAGE_SIZE - 1); |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 332 | |
Paul Mundt | cd01204 | 2007-12-10 15:50:28 +0900 | [diff] [blame] | 333 | /* Boot CPU sets the cache shape */ |
| 334 | detect_cache_shape(); |
| 335 | } |
| 336 | |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame^] | 337 | fpu_init(); |
| 338 | dsp_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 340 | /* |
| 341 | * Initialize the per-CPU ASID cache very early, since the |
| 342 | * TLB flushing routines depend on this being setup. |
| 343 | */ |
| 344 | current_cpu_data.asid_cache = NO_CONTEXT; |
| 345 | |
Paul Mundt | 45ed285 | 2007-03-08 18:12:17 +0900 | [diff] [blame] | 346 | speculative_execution_init(); |
Paul Mundt | 7dd6662 | 2009-08-15 07:43:21 +0900 | [diff] [blame] | 347 | expmask_init(); |
Paul Mundt | 0ea820c | 2010-01-13 12:51:40 +0900 | [diff] [blame^] | 348 | |
| 349 | /* |
| 350 | * Boot processor to setup the FP and extended state context info. |
| 351 | */ |
| 352 | if (raw_smp_processor_id() == 0) |
| 353 | init_thread_xstate(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |