blob: 2e23422280a790b7b7bae11947b1de9355731d58 [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/system.h>
22#include <asm/cacheflush.h>
23#include <asm/cache.h>
Paul Mundtcd012042007-12-10 15:50:28 +090024#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
Paul Mundtaba10302007-09-21 18:32:32 +090026#include <asm/smp.h>
Paul Mundtc881cbc2007-11-10 20:18:18 +090027#ifdef CONFIG_SUPERH32
28#include <asm/ubc.h>
29#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Paul Mundt0ea820c2010-01-13 12:51:40 +090031#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 Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Generic wrapper for command line arguments to disable on-chip
45 * peripherals (nofpu, nodsp, and so forth).
46 */
Paul Mundt0ea820c2010-01-13 12:51:40 +090047#define onchip_setup(x) \
48static int x##_disabled __initdata = !cpu_has_##x; \
49 \
50static int __init x##_setup(char *opts) \
51{ \
52 x##_disabled = 1; \
53 return 1; \
54} \
Linus Torvalds1da177e2005-04-16 15:20:36 -070055__setup("no" __stringify(x), x##_setup);
56
57onchip_setup(fpu);
58onchip_setup(dsp);
59
Paul Mundt45ed2852007-03-08 18:12:17 +090060#ifdef CONFIG_SPECULATIVE_EXECUTION
61#define CPUOPM 0xff2f0000
62#define CPUOPM_RABD (1 << 5)
63
64static 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 Mundt7dd66622009-08-15 07:43:21 +090077#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
83static void __init expmask_init(void)
84{
85 unsigned long expmask = __raw_readl(EXPMASK);
86
87 /*
88 * Future proofing.
89 *
Paul Mundt6e8a0d12009-12-04 16:22:11 +090090 * 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 Mundt7dd66622009-08-15 07:43:21 +090093 */
Paul Mundt6e8a0d12009-12-04 16:22:11 +090094 expmask &= ~(EXPMASK_RTEDS | EXPMASK_BRDSSLP | EXPMASK_MMCAW);
Paul Mundt7dd66622009-08-15 07:43:21 +090095
96 __raw_writel(expmask, EXPMASK);
97 ctrl_barrier();
98}
99#else
100#define expmask_init() do { } while (0)
101#endif
102
Kuninori Morimotofab88d92009-06-02 02:49:20 +0000103/* 2nd-level cache init */
104void __uses_jump_to_uncached __attribute__ ((weak)) l2_cache_init(void)
105{
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * Generic first-level cache init
110 */
Paul Mundt27a511c2007-11-10 20:25:28 +0900111#ifdef CONFIG_SUPERH32
Stuart Menefycbaa1182007-11-30 17:06:36 +0900112static void __uses_jump_to_uncached cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 unsigned long ccr, flags;
115
Stuart Menefycbaa1182007-11-30 17:06:36 +0900116 jump_to_uncached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ccr = ctrl_inl(CCR);
118
119 /*
Richard Curnowb638d0b2006-09-27 14:09:26 +0900120 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700129 */
130 if (ccr & CCR_CACHE_ENABLE) {
131 unsigned long ways, waysize, addrstart;
132
Paul Mundt11c19652006-12-25 10:19:56 +0900133 waysize = current_cpu_data.dcache.sets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Yoshinori Sato9d4436a2006-11-05 15:40:13 +0900135#ifdef CCR_CACHE_ORA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
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 Sato9d4436a2006-11-05 15:40:13 +0900142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Paul Mundt11c19652006-12-25 10:19:56 +0900144 waysize <<= current_cpu_data.dcache.entry_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
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 Mundt11c19652006-12-25 10:19:56 +0900152 ways = current_cpu_data.dcache.ways;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 addrstart = CACHE_OC_ADDRESS_ARRAY;
155 do {
156 unsigned long addr;
157
158 for (addr = addrstart;
159 addr < addrstart + waysize;
Paul Mundt11c19652006-12-25 10:19:56 +0900160 addr += current_cpu_data.dcache.linesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 ctrl_outl(0, addr);
162
Paul Mundt11c19652006-12-25 10:19:56 +0900163 addrstart += current_cpu_data.dcache.way_incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 } 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 Mundt11c19652006-12-25 10:19:56 +0900175 if (current_cpu_data.dcache.ways > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 flags |= CCR_CACHE_EMODE;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900177 else
178 flags &= ~CCR_CACHE_EMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#endif
180
Paul Mundte7bd34a2007-07-31 17:07:28 +0900181#if defined(CONFIG_CACHE_WRITETHROUGH)
182 /* Write-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 flags |= CCR_CACHE_WT;
Paul Mundte7bd34a2007-07-31 17:07:28 +0900184#elif defined(CONFIG_CACHE_WRITEBACK)
185 /* Write-back */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 flags |= CCR_CACHE_CB;
Paul Mundte7bd34a2007-07-31 17:07:28 +0900187#else
188 /* Off */
189 flags &= ~CCR_CACHE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
191
Kuninori Morimotofab88d92009-06-02 02:49:20 +0000192 l2_cache_init();
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ctrl_outl(flags, CCR);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900195 back_to_cached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
Paul Mundt27a511c2007-11-10 20:25:28 +0900197#else
198#define cache_init() do { } while (0)
199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Paul Mundtcd012042007-12-10 15:50:28 +0900201#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
207static 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 Mundt0ea820c2010-01-13 12:51:40 +0900222static 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 Torvalds1da177e2005-04-16 15:20:36 -0700234#ifdef CONFIG_SH_DSP
235static 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
249static 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 Mundt11c19652006-12-25 10:19:56 +0900269 current_cpu_data.flags |= CPU_HAS_DSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Paul Mundt0ea820c2010-01-13 12:51:40 +0900271 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700277 /* Now that we've determined the DSP status, clear the DSP bit. */
278 release_dsp();
279}
Paul Mundt0ea820c2010-01-13 12:51:40 +0900280#else
281static inline void __init dsp_init(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#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 Mundtaba10302007-09-21 18:32:32 +0900300
Paul Mundtb2839ed2008-03-06 12:43:38 +0900301asmlinkage void __init sh_cpu_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Paul Mundtaba10302007-09-21 18:32:32 +0900303 current_thread_info()->cpu = hard_smp_processor_id();
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* First, probe the CPU */
306 detect_cpu_and_cache_system();
307
Paul Mundtffe1b4e2007-03-12 16:15:22 +0900308 if (current_cpu_data.type == CPU_SH_NONE)
309 panic("Unknown CPU");
310
Paul Mundt27a511c2007-11-10 20:25:28 +0900311 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Init the cache */
326 cache_init();
327
Paul Mundtcd012042007-12-10 15:50:28 +0900328 if (raw_smp_processor_id() == 0) {
Paul Mundtaba10302007-09-21 18:32:32 +0900329 shm_align_mask = max_t(unsigned long,
330 current_cpu_data.dcache.way_size - 1,
331 PAGE_SIZE - 1);
Paul Mundtf3c25752006-09-27 18:36:17 +0900332
Paul Mundtcd012042007-12-10 15:50:28 +0900333 /* Boot CPU sets the cache shape */
334 detect_cache_shape();
335 }
336
Paul Mundt0ea820c2010-01-13 12:51:40 +0900337 fpu_init();
338 dsp_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900340 /*
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 Mundt45ed2852007-03-08 18:12:17 +0900346 speculative_execution_init();
Paul Mundt7dd66622009-08-15 07:43:21 +0900347 expmask_init();
Paul Mundt0ea820c2010-01-13 12:51:40 +0900348
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 Torvalds1da177e2005-04-16 15:20:36 -0700354}