init.c 8.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * arch/sh/kernel/cpu/init.c
 *
 * CPU init code
 *
6
 * Copyright (C) 2002 - 2009  Paul Mundt
7
 * Copyright (C) 2003  Richard Curnow
L
Linus Torvalds 已提交
8 9 10 11 12 13 14
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/init.h>
#include <linux/kernel.h>
P
Paul Mundt 已提交
15
#include <linux/mm.h>
16
#include <linux/log2.h>
P
Paul Mundt 已提交
17
#include <asm/mmu_context.h>
L
Linus Torvalds 已提交
18 19
#include <asm/processor.h>
#include <asm/uaccess.h>
20
#include <asm/page.h>
L
Linus Torvalds 已提交
21 22
#include <asm/cacheflush.h>
#include <asm/cache.h>
23
#include <asm/elf.h>
L
Linus Torvalds 已提交
24
#include <asm/io.h>
25
#include <asm/smp.h>
26
#include <asm/sh_bios.h>
D
David Howells 已提交
27
#include <asm/setup.h>
L
Linus Torvalds 已提交
28

29 30 31 32 33 34 35 36 37 38
#ifdef CONFIG_SH_FPU
#define cpu_has_fpu	1
#else
#define cpu_has_fpu	0
#endif

#ifdef CONFIG_SH_DSP
#define cpu_has_dsp	1
#else
#define cpu_has_dsp	0
39
#endif
L
Linus Torvalds 已提交
40 41 42 43 44

/*
 * Generic wrapper for command line arguments to disable on-chip
 * peripherals (nofpu, nodsp, and so forth).
 */
45
#define onchip_setup(x)					\
46
static int x##_disabled = !cpu_has_##x;			\
47
							\
48
static int x##_setup(char *opts)			\
49 50 51 52
{							\
	x##_disabled = 1;				\
	return 1;					\
}							\
L
Linus Torvalds 已提交
53 54 55 56 57
__setup("no" __stringify(x), x##_setup);

onchip_setup(fpu);
onchip_setup(dsp);

58 59 60 61
#ifdef CONFIG_SPECULATIVE_EXECUTION
#define CPUOPM		0xff2f0000
#define CPUOPM_RABD	(1 << 5)

62
static void speculative_execution_init(void)
63 64
{
	/* Clear RABD */
65
	__raw_writel(__raw_readl(CPUOPM) & ~CPUOPM_RABD, CPUOPM);
66 67

	/* Flush the update */
68
	(void)__raw_readl(CPUOPM);
69 70 71 72 73 74
	ctrl_barrier();
}
#else
#define speculative_execution_init()	do { } while (0)
#endif

75 76 77 78 79 80
#ifdef CONFIG_CPU_SH4A
#define EXPMASK			0xff2f0004
#define EXPMASK_RTEDS		(1 << 0)
#define EXPMASK_BRDSSLP		(1 << 1)
#define EXPMASK_MMCAW		(1 << 4)

81
static void expmask_init(void)
82 83 84 85 86 87
{
	unsigned long expmask = __raw_readl(EXPMASK);

	/*
	 * Future proofing.
	 *
88 89 90
	 * Disable support for slottable sleep instruction, non-nop
	 * instructions in the rte delay slot, and associative writes to
	 * the memory-mapped cache array.
91
	 */
92
	expmask &= ~(EXPMASK_RTEDS | EXPMASK_BRDSSLP | EXPMASK_MMCAW);
93 94 95 96 97 98 99 100

	__raw_writel(expmask, EXPMASK);
	ctrl_barrier();
}
#else
#define expmask_init()	do { } while (0)
#endif

101
/* 2nd-level cache init */
102
void __attribute__ ((weak)) l2_cache_init(void)
103 104 105
{
}

L
Linus Torvalds 已提交
106 107 108
/*
 * Generic first-level cache init
 */
109
#if defined(CONFIG_SUPERH32) && !defined(CONFIG_CPU_J2)
110
static void cache_init(void)
L
Linus Torvalds 已提交
111 112 113
{
	unsigned long ccr, flags;

114
	jump_to_uncached();
115
	ccr = __raw_readl(SH_CCR);
L
Linus Torvalds 已提交
116 117

	/*
118 119 120 121 122 123 124 125 126
	 * At this point we don't know whether the cache is enabled or not - a
	 * bootloader may have enabled it.  There are at least 2 things that
	 * could be dirty in the cache at this point:
	 * 1. kernel command line set up by boot loader
	 * 2. spilled registers from the prolog of this function
	 * => before re-initialising the cache, we must do a purge of the whole
	 * cache out to memory for safety.  As long as nothing is spilled
	 * during the loop to lines that have already been done, this is safe.
	 * - RPC
L
Linus Torvalds 已提交
127 128 129 130
	 */
	if (ccr & CCR_CACHE_ENABLE) {
		unsigned long ways, waysize, addrstart;

131
		waysize = current_cpu_data.dcache.sets;
L
Linus Torvalds 已提交
132

133
#ifdef CCR_CACHE_ORA
L
Linus Torvalds 已提交
134 135 136 137 138 139
		/*
		 * If the OC is already in RAM mode, we only have
		 * half of the entries to flush..
		 */
		if (ccr & CCR_CACHE_ORA)
			waysize >>= 1;
140
#endif
L
Linus Torvalds 已提交
141

142
		waysize <<= current_cpu_data.dcache.entry_shift;
L
Linus Torvalds 已提交
143 144 145 146 147 148 149

#ifdef CCR_CACHE_EMODE
		/* If EMODE is not set, we only have 1 way to flush. */
		if (!(ccr & CCR_CACHE_EMODE))
			ways = 1;
		else
#endif
150
			ways = current_cpu_data.dcache.ways;
L
Linus Torvalds 已提交
151 152 153 154 155 156 157

		addrstart = CACHE_OC_ADDRESS_ARRAY;
		do {
			unsigned long addr;

			for (addr = addrstart;
			     addr < addrstart + waysize;
158
			     addr += current_cpu_data.dcache.linesz)
159
				__raw_writel(0, addr);
L
Linus Torvalds 已提交
160

161
			addrstart += current_cpu_data.dcache.way_incr;
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170 171 172
		} while (--ways);
	}

	/*
	 * Default CCR values .. enable the caches
	 * and invalidate them immediately..
	 */
	flags = CCR_CACHE_ENABLE | CCR_CACHE_INVALIDATE;

#ifdef CCR_CACHE_EMODE
	/* Force EMODE if possible */
173
	if (current_cpu_data.dcache.ways > 1)
L
Linus Torvalds 已提交
174
		flags |= CCR_CACHE_EMODE;
175 176
	else
		flags &= ~CCR_CACHE_EMODE;
L
Linus Torvalds 已提交
177 178
#endif

179 180
#if defined(CONFIG_CACHE_WRITETHROUGH)
	/* Write-through */
L
Linus Torvalds 已提交
181
	flags |= CCR_CACHE_WT;
182 183
#elif defined(CONFIG_CACHE_WRITEBACK)
	/* Write-back */
L
Linus Torvalds 已提交
184
	flags |= CCR_CACHE_CB;
185 186 187
#else
	/* Off */
	flags &= ~CCR_CACHE_ENABLE;
L
Linus Torvalds 已提交
188 189
#endif

190 191
	l2_cache_init();

192
	__raw_writel(flags, SH_CCR);
193
	back_to_cached();
L
Linus Torvalds 已提交
194
}
195 196 197
#else
#define cache_init()	do { } while (0)
#endif
L
Linus Torvalds 已提交
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#define CSHAPE(totalsize, linesize, assoc) \
	((totalsize & ~0xff) | (linesize << 4) | assoc)

#define CACHE_DESC_SHAPE(desc)	\
	CSHAPE((desc).way_size * (desc).ways, ilog2((desc).linesz), (desc).ways)

static void detect_cache_shape(void)
{
	l1d_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.dcache);

	if (current_cpu_data.dcache.flags & SH_CACHE_COMBINED)
		l1i_cache_shape = l1d_cache_shape;
	else
		l1i_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.icache);

	if (current_cpu_data.flags & CPU_HAS_L2_CACHE)
		l2_cache_shape = CACHE_DESC_SHAPE(current_cpu_data.scache);
	else
		l2_cache_shape = -1; /* No S-cache */
}

220
static void fpu_init(void)
221 222 223 224 225 226 227 228 229 230 231
{
	/* Disable the FPU */
	if (fpu_disabled && (current_cpu_data.flags & CPU_HAS_FPU)) {
		printk("FPU Disabled\n");
		current_cpu_data.flags &= ~CPU_HAS_FPU;
	}

	disable_fpu();
	clear_used_math();
}

L
Linus Torvalds 已提交
232
#ifdef CONFIG_SH_DSP
233
static void release_dsp(void)
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246
{
	unsigned long sr;

	/* Clear SR.DSP bit */
	__asm__ __volatile__ (
		"stc\tsr, %0\n\t"
		"and\t%1, %0\n\t"
		"ldc\t%0, sr\n\t"
		: "=&r" (sr)
		: "r" (~SR_DSP)
	);
}

247
static void dsp_init(void)
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
	unsigned long sr;

	/*
	 * Set the SR.DSP bit, wait for one instruction, and then read
	 * back the SR value.
	 */
	__asm__ __volatile__ (
		"stc\tsr, %0\n\t"
		"or\t%1, %0\n\t"
		"ldc\t%0, sr\n\t"
		"nop\n\t"
		"stc\tsr, %0\n\t"
		: "=&r" (sr)
		: "r" (SR_DSP)
	);

	/* If the DSP bit is still set, this CPU has a DSP */
	if (sr & SR_DSP)
267
		current_cpu_data.flags |= CPU_HAS_DSP;
L
Linus Torvalds 已提交
268

269 270 271 272 273 274
	/* Disable the DSP */
	if (dsp_disabled && (current_cpu_data.flags & CPU_HAS_DSP)) {
		printk("DSP Disabled\n");
		current_cpu_data.flags &= ~CPU_HAS_DSP;
	}

L
Linus Torvalds 已提交
275 276 277
	/* Now that we've determined the DSP status, clear the DSP bit. */
	release_dsp();
}
278
#else
279
static inline void dsp_init(void) { }
L
Linus Torvalds 已提交
280 281 282
#endif /* CONFIG_SH_DSP */

/**
283
 * cpu_init
L
Linus Torvalds 已提交
284
 *
P
Paul Mundt 已提交
285 286 287 288
 * This is our initial entry point for each CPU, and is invoked on the
 * boot CPU prior to calling start_kernel(). For SMP, a combination of
 * this and start_secondary() will bring up each processor to a ready
 * state prior to hand forking the idle loop.
L
Linus Torvalds 已提交
289
 *
P
Paul Mundt 已提交
290 291 292 293
 * We do all of the basic processor init here, including setting up
 * the caches, FPU, DSP, etc. By the time start_kernel() is hit (and
 * subsequently platform_setup()) things like determining the CPU
 * subtype and initial configuration will all be done.
L
Linus Torvalds 已提交
294 295
 *
 * Each processor family is still responsible for doing its own probing
296
 * and cache configuration in cpu_probe().
L
Linus Torvalds 已提交
297
 */
298
asmlinkage void cpu_init(void)
L
Linus Torvalds 已提交
299
{
300 301
	current_thread_info()->cpu = hard_smp_processor_id();

L
Linus Torvalds 已提交
302
	/* First, probe the CPU */
303
	cpu_probe();
L
Linus Torvalds 已提交
304

305 306 307
	if (current_cpu_data.type == CPU_SH_NONE)
		panic("Unknown CPU");

308 309 310 311 312 313 314 315 316 317 318 319 320 321
	/* First setup the rest of the I-cache info */
	current_cpu_data.icache.entry_mask = current_cpu_data.icache.way_incr -
				      current_cpu_data.icache.linesz;

	current_cpu_data.icache.way_size = current_cpu_data.icache.sets *
				    current_cpu_data.icache.linesz;

	/* And the D-cache too */
	current_cpu_data.dcache.entry_mask = current_cpu_data.dcache.way_incr -
				      current_cpu_data.dcache.linesz;

	current_cpu_data.dcache.way_size = current_cpu_data.dcache.sets *
				    current_cpu_data.dcache.linesz;

L
Linus Torvalds 已提交
322 323 324
	/* Init the cache */
	cache_init();

325
	if (raw_smp_processor_id() == 0) {
326
#ifdef CONFIG_MMU
327 328 329
		shm_align_mask = max_t(unsigned long,
				       current_cpu_data.dcache.way_size - 1,
				       PAGE_SIZE - 1);
330 331 332
#else
		shm_align_mask = PAGE_SIZE - 1;
#endif
333

334 335 336 337
		/* Boot CPU sets the cache shape */
		detect_cache_shape();
	}

338 339
	fpu_init();
	dsp_init();
L
Linus Torvalds 已提交
340

P
Paul Mundt 已提交
341 342 343 344 345 346
	/*
	 * Initialize the per-CPU ASID cache very early, since the
	 * TLB flushing routines depend on this being setup.
	 */
	current_cpu_data.asid_cache = NO_CONTEXT;

347 348
	current_cpu_data.phys_bits = __in_29bit_mode() ? 29 : 32;

349
	speculative_execution_init();
350
	expmask_init();
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	/* Do the rest of the boot processor setup */
	if (raw_smp_processor_id() == 0) {
		/* Save off the BIOS VBR, if there is one */
		sh_bios_vbr_init();

		/*
		 * Setup VBR for boot CPU. Secondary CPUs do this through
		 * start_secondary().
		 */
		per_cpu_trap_init();

		/*
		 * Boot processor to setup the FP and extended state
		 * context info.
		 */
367
		init_thread_xstate();
368
	}
L
Linus Torvalds 已提交
369
}