blob: bfd9e2798008051a6b18c69ccf0357d9bfd13210 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/kernel/cpu/init.c
3 *
4 * CPU init code
5 *
Paul Mundt7dd66622009-08-15 07:43:21 +09006 * Copyright (C) 2002 - 2009 Paul Mundt
Richard Curnowb638d0b2006-09-27 14:09:26 +09007 * Copyright (C) 2003 Richard Curnow
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 Mundtaec5e0e2006-12-25 09:51:47 +090015#include <linux/mm.h>
Paul Mundtcd012042007-12-10 15:50:28 +090016#include <linux/log2.h>
Paul Mundtaec5e0e2006-12-25 09:51:47 +090017#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/processor.h>
19#include <asm/uaccess.h>
Paul Mundtf3c25752006-09-27 18:36:17 +090020#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/cacheflush.h>
22#include <asm/cache.h>
Paul Mundtcd012042007-12-10 15:50:28 +090023#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/io.h>
Paul Mundtaba10302007-09-21 18:32:32 +090025#include <asm/smp.h>
Paul Mundt49f3bfe2010-02-17 12:33:22 +090026#include <asm/sh_bios.h>
David Howellse839ca52012-03-28 18:30:03 +010027#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Paul Mundt0ea820c2010-01-13 12:51:40 +090029#ifdef CONFIG_SH_FPU
30#define cpu_has_fpu 1
31#else
32#define cpu_has_fpu 0
33#endif
34
35#ifdef CONFIG_SH_DSP
36#define cpu_has_dsp 1
37#else
38#define cpu_has_dsp 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#endif
40
41/*
42 * Generic wrapper for command line arguments to disable on-chip
43 * peripherals (nofpu, nodsp, and so forth).
44 */
Paul Mundt0ea820c2010-01-13 12:51:40 +090045#define onchip_setup(x) \
Paul Gortmaker4603f532013-06-18 17:10:12 -040046static int x##_disabled = !cpu_has_##x; \
Paul Mundt0ea820c2010-01-13 12:51:40 +090047 \
Paul Gortmaker4603f532013-06-18 17:10:12 -040048static int x##_setup(char *opts) \
Paul Mundt0ea820c2010-01-13 12:51:40 +090049{ \
50 x##_disabled = 1; \
51 return 1; \
52} \
Linus Torvalds1da177e2005-04-16 15:20:36 -070053__setup("no" __stringify(x), x##_setup);
54
55onchip_setup(fpu);
56onchip_setup(dsp);
57
Paul Mundt45ed2852007-03-08 18:12:17 +090058#ifdef CONFIG_SPECULATIVE_EXECUTION
59#define CPUOPM 0xff2f0000
60#define CPUOPM_RABD (1 << 5)
61
Paul Gortmaker4603f532013-06-18 17:10:12 -040062static void speculative_execution_init(void)
Paul Mundt45ed2852007-03-08 18:12:17 +090063{
64 /* Clear RABD */
Paul Mundt9d56dd32010-01-26 12:58:40 +090065 __raw_writel(__raw_readl(CPUOPM) & ~CPUOPM_RABD, CPUOPM);
Paul Mundt45ed2852007-03-08 18:12:17 +090066
67 /* Flush the update */
Paul Mundt9d56dd32010-01-26 12:58:40 +090068 (void)__raw_readl(CPUOPM);
Paul Mundt45ed2852007-03-08 18:12:17 +090069 ctrl_barrier();
70}
71#else
72#define speculative_execution_init() do { } while (0)
73#endif
74
Paul Mundt7dd66622009-08-15 07:43:21 +090075#ifdef CONFIG_CPU_SH4A
76#define EXPMASK 0xff2f0004
77#define EXPMASK_RTEDS (1 << 0)
78#define EXPMASK_BRDSSLP (1 << 1)
79#define EXPMASK_MMCAW (1 << 4)
80
Paul Gortmaker4603f532013-06-18 17:10:12 -040081static void expmask_init(void)
Paul Mundt7dd66622009-08-15 07:43:21 +090082{
83 unsigned long expmask = __raw_readl(EXPMASK);
84
85 /*
86 * Future proofing.
87 *
Paul Mundt6e8a0d12009-12-04 16:22:11 +090088 * Disable support for slottable sleep instruction, non-nop
89 * instructions in the rte delay slot, and associative writes to
90 * the memory-mapped cache array.
Paul Mundt7dd66622009-08-15 07:43:21 +090091 */
Paul Mundt6e8a0d12009-12-04 16:22:11 +090092 expmask &= ~(EXPMASK_RTEDS | EXPMASK_BRDSSLP | EXPMASK_MMCAW);
Paul Mundt7dd66622009-08-15 07:43:21 +090093
94 __raw_writel(expmask, EXPMASK);
95 ctrl_barrier();
96}
97#else
98#define expmask_init() do { } while (0)
99#endif
100
Kuninori Morimotofab88d92009-06-02 02:49:20 +0000101/* 2nd-level cache init */
Paul Mundt2dc2f8e2010-01-21 16:05:25 +0900102void __attribute__ ((weak)) l2_cache_init(void)
Kuninori Morimotofab88d92009-06-02 02:49:20 +0000103{
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/*
107 * Generic first-level cache init
108 */
Paul Mundt27a511c2007-11-10 20:25:28 +0900109#ifdef CONFIG_SUPERH32
Paul Mundt2dc2f8e2010-01-21 16:05:25 +0900110static void cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 unsigned long ccr, flags;
113
Stuart Menefycbaa1182007-11-30 17:06:36 +0900114 jump_to_uncached();
Geert Uytterhoevena5f6ea22014-03-03 15:38:33 -0800115 ccr = __raw_readl(SH_CCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /*
Richard Curnowb638d0b2006-09-27 14:09:26 +0900118 * At this point we don't know whether the cache is enabled or not - a
119 * bootloader may have enabled it. There are at least 2 things that
120 * could be dirty in the cache at this point:
121 * 1. kernel command line set up by boot loader
122 * 2. spilled registers from the prolog of this function
123 * => before re-initialising the cache, we must do a purge of the whole
124 * cache out to memory for safety. As long as nothing is spilled
125 * during the loop to lines that have already been done, this is safe.
126 * - RPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
128 if (ccr & CCR_CACHE_ENABLE) {
129 unsigned long ways, waysize, addrstart;
130
Paul Mundt11c19652006-12-25 10:19:56 +0900131 waysize = current_cpu_data.dcache.sets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Yoshinori Sato9d4436a2006-11-05 15:40:13 +0900133#ifdef CCR_CACHE_ORA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * If the OC is already in RAM mode, we only have
136 * half of the entries to flush..
137 */
138 if (ccr & CCR_CACHE_ORA)
139 waysize >>= 1;
Yoshinori Sato9d4436a2006-11-05 15:40:13 +0900140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Paul Mundt11c19652006-12-25 10:19:56 +0900142 waysize <<= current_cpu_data.dcache.entry_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#ifdef CCR_CACHE_EMODE
145 /* If EMODE is not set, we only have 1 way to flush. */
146 if (!(ccr & CCR_CACHE_EMODE))
147 ways = 1;
148 else
149#endif
Paul Mundt11c19652006-12-25 10:19:56 +0900150 ways = current_cpu_data.dcache.ways;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 addrstart = CACHE_OC_ADDRESS_ARRAY;
153 do {
154 unsigned long addr;
155
156 for (addr = addrstart;
157 addr < addrstart + waysize;
Paul Mundt11c19652006-12-25 10:19:56 +0900158 addr += current_cpu_data.dcache.linesz)
Paul Mundt9d56dd32010-01-26 12:58:40 +0900159 __raw_writel(0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Paul Mundt11c19652006-12-25 10:19:56 +0900161 addrstart += current_cpu_data.dcache.way_incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 } while (--ways);
163 }
164
165 /*
166 * Default CCR values .. enable the caches
167 * and invalidate them immediately..
168 */
169 flags = CCR_CACHE_ENABLE | CCR_CACHE_INVALIDATE;
170
171#ifdef CCR_CACHE_EMODE
172 /* Force EMODE if possible */
Paul Mundt11c19652006-12-25 10:19:56 +0900173 if (current_cpu_data.dcache.ways > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 flags |= CCR_CACHE_EMODE;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900175 else
176 flags &= ~CCR_CACHE_EMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#endif
178
Paul Mundte7bd34a2007-07-31 17:07:28 +0900179#if defined(CONFIG_CACHE_WRITETHROUGH)
180 /* Write-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 flags |= CCR_CACHE_WT;
Paul Mundte7bd34a2007-07-31 17:07:28 +0900182#elif defined(CONFIG_CACHE_WRITEBACK)
183 /* Write-back */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 flags |= CCR_CACHE_CB;
Paul Mundte7bd34a2007-07-31 17:07:28 +0900185#else
186 /* Off */
187 flags &= ~CCR_CACHE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#endif
189
Kuninori Morimotofab88d92009-06-02 02:49:20 +0000190 l2_cache_init();
191
Geert Uytterhoevena5f6ea22014-03-03 15:38:33 -0800192 __raw_writel(flags, SH_CCR);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900193 back_to_cached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
Paul Mundt27a511c2007-11-10 20:25:28 +0900195#else
196#define cache_init() do { } while (0)
197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Paul Mundtcd012042007-12-10 15:50:28 +0900199#define CSHAPE(totalsize, linesize, assoc) \
200 ((totalsize & ~0xff) | (linesize << 4) | assoc)
201
202#define CACHE_DESC_SHAPE(desc) \
203 CSHAPE((desc).way_size * (desc).ways, ilog2((desc).linesz), (desc).ways)
204
205static void detect_cache_shape(void)
206{
207 l1d_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.dcache);
208
209 if (current_cpu_data.dcache.flags & SH_CACHE_COMBINED)
210 l1i_cache_shape = l1d_cache_shape;
211 else
212 l1i_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.icache);
213
214 if (current_cpu_data.flags & CPU_HAS_L2_CACHE)
215 l2_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.scache);
216 else
217 l2_cache_shape = -1; /* No S-cache */
218}
219
Paul Gortmaker4603f532013-06-18 17:10:12 -0400220static void fpu_init(void)
Paul Mundt0ea820c2010-01-13 12:51:40 +0900221{
222 /* Disable the FPU */
223 if (fpu_disabled && (current_cpu_data.flags & CPU_HAS_FPU)) {
224 printk("FPU Disabled\n");
225 current_cpu_data.flags &= ~CPU_HAS_FPU;
226 }
227
228 disable_fpu();
229 clear_used_math();
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#ifdef CONFIG_SH_DSP
Paul Gortmaker4603f532013-06-18 17:10:12 -0400233static void release_dsp(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 unsigned long sr;
236
237 /* Clear SR.DSP bit */
238 __asm__ __volatile__ (
239 "stc\tsr, %0\n\t"
240 "and\t%1, %0\n\t"
241 "ldc\t%0, sr\n\t"
242 : "=&r" (sr)
243 : "r" (~SR_DSP)
244 );
245}
246
Paul Gortmaker4603f532013-06-18 17:10:12 -0400247static void dsp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 unsigned long sr;
250
251 /*
252 * Set the SR.DSP bit, wait for one instruction, and then read
253 * back the SR value.
254 */
255 __asm__ __volatile__ (
256 "stc\tsr, %0\n\t"
257 "or\t%1, %0\n\t"
258 "ldc\t%0, sr\n\t"
259 "nop\n\t"
260 "stc\tsr, %0\n\t"
261 : "=&r" (sr)
262 : "r" (SR_DSP)
263 );
264
265 /* If the DSP bit is still set, this CPU has a DSP */
266 if (sr & SR_DSP)
Paul Mundt11c19652006-12-25 10:19:56 +0900267 current_cpu_data.flags |= CPU_HAS_DSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Paul Mundt0ea820c2010-01-13 12:51:40 +0900269 /* Disable the DSP */
270 if (dsp_disabled && (current_cpu_data.flags & CPU_HAS_DSP)) {
271 printk("DSP Disabled\n");
272 current_cpu_data.flags &= ~CPU_HAS_DSP;
273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Now that we've determined the DSP status, clear the DSP bit. */
276 release_dsp();
277}
Paul Mundt0ea820c2010-01-13 12:51:40 +0900278#else
Paul Gortmaker4603f532013-06-18 17:10:12 -0400279static inline void dsp_init(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif /* CONFIG_SH_DSP */
281
282/**
Paul Mundt4a6feab2010-04-21 12:20:42 +0900283 * cpu_init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
Paul Mundt7025bec2010-01-05 19:16:35 +0900285 * This is our initial entry point for each CPU, and is invoked on the
286 * boot CPU prior to calling start_kernel(). For SMP, a combination of
287 * this and start_secondary() will bring up each processor to a ready
288 * state prior to hand forking the idle loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *
Paul Mundt7025bec2010-01-05 19:16:35 +0900290 * We do all of the basic processor init here, including setting up
291 * the caches, FPU, DSP, etc. By the time start_kernel() is hit (and
292 * subsequently platform_setup()) things like determining the CPU
293 * subtype and initial configuration will all be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *
295 * Each processor family is still responsible for doing its own probing
Paul Mundta9079ca2010-04-21 12:01:06 +0900296 * and cache configuration in cpu_probe().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
Paul Gortmaker4603f532013-06-18 17:10:12 -0400298asmlinkage void cpu_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Paul Mundtaba10302007-09-21 18:32:32 +0900300 current_thread_info()->cpu = hard_smp_processor_id();
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* First, probe the CPU */
Paul Mundta9079ca2010-04-21 12:01:06 +0900303 cpu_probe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Paul Mundtffe1b4e2007-03-12 16:15:22 +0900305 if (current_cpu_data.type == CPU_SH_NONE)
306 panic("Unknown CPU");
307
Paul Mundt27a511c2007-11-10 20:25:28 +0900308 /* First setup the rest of the I-cache info */
309 current_cpu_data.icache.entry_mask = current_cpu_data.icache.way_incr -
310 current_cpu_data.icache.linesz;
311
312 current_cpu_data.icache.way_size = current_cpu_data.icache.sets *
313 current_cpu_data.icache.linesz;
314
315 /* And the D-cache too */
316 current_cpu_data.dcache.entry_mask = current_cpu_data.dcache.way_incr -
317 current_cpu_data.dcache.linesz;
318
319 current_cpu_data.dcache.way_size = current_cpu_data.dcache.sets *
320 current_cpu_data.dcache.linesz;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Init the cache */
323 cache_init();
324
Paul Mundtcd012042007-12-10 15:50:28 +0900325 if (raw_smp_processor_id() == 0) {
Rich Felker57155c62016-03-22 22:02:23 +0000326#ifdef CONFIG_MMU
Paul Mundtaba10302007-09-21 18:32:32 +0900327 shm_align_mask = max_t(unsigned long,
328 current_cpu_data.dcache.way_size - 1,
329 PAGE_SIZE - 1);
Rich Felker57155c62016-03-22 22:02:23 +0000330#else
331 shm_align_mask = PAGE_SIZE - 1;
332#endif
Paul Mundtf3c25752006-09-27 18:36:17 +0900333
Paul Mundtcd012042007-12-10 15:50:28 +0900334 /* Boot CPU sets the cache shape */
335 detect_cache_shape();
336 }
337
Paul Mundt0ea820c2010-01-13 12:51:40 +0900338 fpu_init();
339 dsp_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900341 /*
342 * Initialize the per-CPU ASID cache very early, since the
343 * TLB flushing routines depend on this being setup.
344 */
345 current_cpu_data.asid_cache = NO_CONTEXT;
346
Paul Mundt2f984922010-10-26 14:44:58 +0900347 current_cpu_data.phys_bits = __in_29bit_mode() ? 29 : 32;
348
Paul Mundt45ed2852007-03-08 18:12:17 +0900349 speculative_execution_init();
Paul Mundt7dd66622009-08-15 07:43:21 +0900350 expmask_init();
Paul Mundt0ea820c2010-01-13 12:51:40 +0900351
Paul Mundt49f3bfe2010-02-17 12:33:22 +0900352 /* Do the rest of the boot processor setup */
353 if (raw_smp_processor_id() == 0) {
354 /* Save off the BIOS VBR, if there is one */
355 sh_bios_vbr_init();
356
357 /*
358 * Setup VBR for boot CPU. Secondary CPUs do this through
359 * start_secondary().
360 */
361 per_cpu_trap_init();
362
363 /*
364 * Boot processor to setup the FP and extended state
365 * context info.
366 */
Paul Mundt0ea820c2010-01-13 12:51:40 +0900367 init_thread_xstate();
Paul Mundt49f3bfe2010-02-17 12:33:22 +0900368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}