blob: 89b4b76c0d763d58f6c77229ddc8963fbc8d5508 [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
31/*
32 * Generic wrapper for command line arguments to disable on-chip
33 * peripherals (nofpu, nodsp, and so forth).
34 */
35#define onchip_setup(x) \
36static int x##_disabled __initdata = 0; \
37 \
38static int __init x##_setup(char *opts) \
39{ \
40 x##_disabled = 1; \
OGAWA Hirofumi9b410462006-03-31 02:30:33 -080041 return 1; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070042} \
43__setup("no" __stringify(x), x##_setup);
44
45onchip_setup(fpu);
46onchip_setup(dsp);
47
Paul Mundt45ed2852007-03-08 18:12:17 +090048#ifdef CONFIG_SPECULATIVE_EXECUTION
49#define CPUOPM 0xff2f0000
50#define CPUOPM_RABD (1 << 5)
51
52static void __init speculative_execution_init(void)
53{
54 /* Clear RABD */
55 ctrl_outl(ctrl_inl(CPUOPM) & ~CPUOPM_RABD, CPUOPM);
56
57 /* Flush the update */
58 (void)ctrl_inl(CPUOPM);
59 ctrl_barrier();
60}
61#else
62#define speculative_execution_init() do { } while (0)
63#endif
64
Paul Mundt7dd66622009-08-15 07:43:21 +090065#ifdef CONFIG_CPU_SH4A
66#define EXPMASK 0xff2f0004
67#define EXPMASK_RTEDS (1 << 0)
68#define EXPMASK_BRDSSLP (1 << 1)
69#define EXPMASK_MMCAW (1 << 4)
70
71static void __init expmask_init(void)
72{
73 unsigned long expmask = __raw_readl(EXPMASK);
74
75 /*
76 * Future proofing.
77 *
Paul Mundt6e8a0d12009-12-04 16:22:11 +090078 * Disable support for slottable sleep instruction, non-nop
79 * instructions in the rte delay slot, and associative writes to
80 * the memory-mapped cache array.
Paul Mundt7dd66622009-08-15 07:43:21 +090081 */
Paul Mundt6e8a0d12009-12-04 16:22:11 +090082 expmask &= ~(EXPMASK_RTEDS | EXPMASK_BRDSSLP | EXPMASK_MMCAW);
Paul Mundt7dd66622009-08-15 07:43:21 +090083
84 __raw_writel(expmask, EXPMASK);
85 ctrl_barrier();
86}
87#else
88#define expmask_init() do { } while (0)
89#endif
90
Kuninori Morimotofab88d92009-06-02 02:49:20 +000091/* 2nd-level cache init */
92void __uses_jump_to_uncached __attribute__ ((weak)) l2_cache_init(void)
93{
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Generic first-level cache init
98 */
Paul Mundt27a511c2007-11-10 20:25:28 +090099#ifdef CONFIG_SUPERH32
Stuart Menefycbaa1182007-11-30 17:06:36 +0900100static void __uses_jump_to_uncached cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 unsigned long ccr, flags;
103
Stuart Menefycbaa1182007-11-30 17:06:36 +0900104 jump_to_uncached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 ccr = ctrl_inl(CCR);
106
107 /*
Richard Curnowb638d0b2006-09-27 14:09:26 +0900108 * At this point we don't know whether the cache is enabled or not - a
109 * bootloader may have enabled it. There are at least 2 things that
110 * could be dirty in the cache at this point:
111 * 1. kernel command line set up by boot loader
112 * 2. spilled registers from the prolog of this function
113 * => before re-initialising the cache, we must do a purge of the whole
114 * cache out to memory for safety. As long as nothing is spilled
115 * during the loop to lines that have already been done, this is safe.
116 * - RPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118 if (ccr & CCR_CACHE_ENABLE) {
119 unsigned long ways, waysize, addrstart;
120
Paul Mundt11c19652006-12-25 10:19:56 +0900121 waysize = current_cpu_data.dcache.sets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Yoshinori Sato9d4436a2006-11-05 15:40:13 +0900123#ifdef CCR_CACHE_ORA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /*
125 * If the OC is already in RAM mode, we only have
126 * half of the entries to flush..
127 */
128 if (ccr & CCR_CACHE_ORA)
129 waysize >>= 1;
Yoshinori Sato9d4436a2006-11-05 15:40:13 +0900130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Paul Mundt11c19652006-12-25 10:19:56 +0900132 waysize <<= current_cpu_data.dcache.entry_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#ifdef CCR_CACHE_EMODE
135 /* If EMODE is not set, we only have 1 way to flush. */
136 if (!(ccr & CCR_CACHE_EMODE))
137 ways = 1;
138 else
139#endif
Paul Mundt11c19652006-12-25 10:19:56 +0900140 ways = current_cpu_data.dcache.ways;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 addrstart = CACHE_OC_ADDRESS_ARRAY;
143 do {
144 unsigned long addr;
145
146 for (addr = addrstart;
147 addr < addrstart + waysize;
Paul Mundt11c19652006-12-25 10:19:56 +0900148 addr += current_cpu_data.dcache.linesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ctrl_outl(0, addr);
150
Paul Mundt11c19652006-12-25 10:19:56 +0900151 addrstart += current_cpu_data.dcache.way_incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 } while (--ways);
153 }
154
155 /*
156 * Default CCR values .. enable the caches
157 * and invalidate them immediately..
158 */
159 flags = CCR_CACHE_ENABLE | CCR_CACHE_INVALIDATE;
160
161#ifdef CCR_CACHE_EMODE
162 /* Force EMODE if possible */
Paul Mundt11c19652006-12-25 10:19:56 +0900163 if (current_cpu_data.dcache.ways > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 flags |= CCR_CACHE_EMODE;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900165 else
166 flags &= ~CCR_CACHE_EMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#endif
168
Paul Mundte7bd34a2007-07-31 17:07:28 +0900169#if defined(CONFIG_CACHE_WRITETHROUGH)
170 /* Write-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 flags |= CCR_CACHE_WT;
Paul Mundte7bd34a2007-07-31 17:07:28 +0900172#elif defined(CONFIG_CACHE_WRITEBACK)
173 /* Write-back */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 flags |= CCR_CACHE_CB;
Paul Mundte7bd34a2007-07-31 17:07:28 +0900175#else
176 /* Off */
177 flags &= ~CCR_CACHE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
179
Kuninori Morimotofab88d92009-06-02 02:49:20 +0000180 l2_cache_init();
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ctrl_outl(flags, CCR);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900183 back_to_cached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
Paul Mundt27a511c2007-11-10 20:25:28 +0900185#else
186#define cache_init() do { } while (0)
187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Paul Mundtcd012042007-12-10 15:50:28 +0900189#define CSHAPE(totalsize, linesize, assoc) \
190 ((totalsize & ~0xff) | (linesize << 4) | assoc)
191
192#define CACHE_DESC_SHAPE(desc) \
193 CSHAPE((desc).way_size * (desc).ways, ilog2((desc).linesz), (desc).ways)
194
195static void detect_cache_shape(void)
196{
197 l1d_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.dcache);
198
199 if (current_cpu_data.dcache.flags & SH_CACHE_COMBINED)
200 l1i_cache_shape = l1d_cache_shape;
201 else
202 l1i_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.icache);
203
204 if (current_cpu_data.flags & CPU_HAS_L2_CACHE)
205 l2_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.scache);
206 else
207 l2_cache_shape = -1; /* No S-cache */
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#ifdef CONFIG_SH_DSP
211static void __init release_dsp(void)
212{
213 unsigned long sr;
214
215 /* Clear SR.DSP bit */
216 __asm__ __volatile__ (
217 "stc\tsr, %0\n\t"
218 "and\t%1, %0\n\t"
219 "ldc\t%0, sr\n\t"
220 : "=&r" (sr)
221 : "r" (~SR_DSP)
222 );
223}
224
225static void __init dsp_init(void)
226{
227 unsigned long sr;
228
229 /*
230 * Set the SR.DSP bit, wait for one instruction, and then read
231 * back the SR value.
232 */
233 __asm__ __volatile__ (
234 "stc\tsr, %0\n\t"
235 "or\t%1, %0\n\t"
236 "ldc\t%0, sr\n\t"
237 "nop\n\t"
238 "stc\tsr, %0\n\t"
239 : "=&r" (sr)
240 : "r" (SR_DSP)
241 );
242
243 /* If the DSP bit is still set, this CPU has a DSP */
244 if (sr & SR_DSP)
Paul Mundt11c19652006-12-25 10:19:56 +0900245 current_cpu_data.flags |= CPU_HAS_DSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* Now that we've determined the DSP status, clear the DSP bit. */
248 release_dsp();
249}
250#endif /* CONFIG_SH_DSP */
251
252/**
253 * sh_cpu_init
254 *
255 * This is our initial entry point for each CPU, and is invoked on the boot
256 * CPU prior to calling start_kernel(). For SMP, a combination of this and
257 * start_secondary() will bring up each processor to a ready state prior
258 * to hand forking the idle loop.
259 *
260 * We do all of the basic processor init here, including setting up the
261 * caches, FPU, DSP, kicking the UBC, etc. By the time start_kernel() is
262 * hit (and subsequently platform_setup()) things like determining the
263 * CPU subtype and initial configuration will all be done.
264 *
265 * Each processor family is still responsible for doing its own probing
266 * and cache configuration in detect_cpu_and_cache_system().
267 */
Paul Mundtaba10302007-09-21 18:32:32 +0900268
Paul Mundtb2839ed2008-03-06 12:43:38 +0900269asmlinkage void __init sh_cpu_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Paul Mundtaba10302007-09-21 18:32:32 +0900271 current_thread_info()->cpu = hard_smp_processor_id();
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* First, probe the CPU */
274 detect_cpu_and_cache_system();
275
Paul Mundtffe1b4e2007-03-12 16:15:22 +0900276 if (current_cpu_data.type == CPU_SH_NONE)
277 panic("Unknown CPU");
278
Paul Mundt27a511c2007-11-10 20:25:28 +0900279 /* First setup the rest of the I-cache info */
280 current_cpu_data.icache.entry_mask = current_cpu_data.icache.way_incr -
281 current_cpu_data.icache.linesz;
282
283 current_cpu_data.icache.way_size = current_cpu_data.icache.sets *
284 current_cpu_data.icache.linesz;
285
286 /* And the D-cache too */
287 current_cpu_data.dcache.entry_mask = current_cpu_data.dcache.way_incr -
288 current_cpu_data.dcache.linesz;
289
290 current_cpu_data.dcache.way_size = current_cpu_data.dcache.sets *
291 current_cpu_data.dcache.linesz;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Init the cache */
294 cache_init();
295
Paul Mundtcd012042007-12-10 15:50:28 +0900296 if (raw_smp_processor_id() == 0) {
Paul Mundtaba10302007-09-21 18:32:32 +0900297 shm_align_mask = max_t(unsigned long,
298 current_cpu_data.dcache.way_size - 1,
299 PAGE_SIZE - 1);
Paul Mundtf3c25752006-09-27 18:36:17 +0900300
Paul Mundtcd012042007-12-10 15:50:28 +0900301 /* Boot CPU sets the cache shape */
302 detect_cache_shape();
303 }
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* Disable the FPU */
306 if (fpu_disabled) {
307 printk("FPU Disabled\n");
Paul Mundt11c19652006-12-25 10:19:56 +0900308 current_cpu_data.flags &= ~CPU_HAS_FPU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 /* FPU initialization */
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +0100312 disable_fpu();
Paul Mundt11c19652006-12-25 10:19:56 +0900313 if ((current_cpu_data.flags & CPU_HAS_FPU)) {
Stuart Menefyd3ea9fa2009-09-25 18:25:10 +0100314 current_thread_info()->status &= ~TS_USEDFPU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 clear_used_math();
316 }
317
Paul Mundtaec5e0e2006-12-25 09:51:47 +0900318 /*
319 * Initialize the per-CPU ASID cache very early, since the
320 * TLB flushing routines depend on this being setup.
321 */
322 current_cpu_data.asid_cache = NO_CONTEXT;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#ifdef CONFIG_SH_DSP
325 /* Probe for DSP */
326 dsp_init();
327
328 /* Disable the DSP */
329 if (dsp_disabled) {
330 printk("DSP Disabled\n");
Paul Mundt11c19652006-12-25 10:19:56 +0900331 current_cpu_data.flags &= ~CPU_HAS_DSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 release_dsp();
333 }
334#endif
335
Paul Mundt45ed2852007-03-08 18:12:17 +0900336 speculative_execution_init();
Paul Mundt7dd66622009-08-15 07:43:21 +0900337 expmask_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}