1/*
2 * arch/sh/kernel/setup.c
3 *
4 * This file handles the architecture-dependent parts of initialization
5 *
6 *  Copyright (C) 1999  Niibe Yutaka
7 *  Copyright (C) 2002 - 2007 Paul Mundt
8 */
9#include <linux/screen_info.h>
10#include <linux/ioport.h>
11#include <linux/init.h>
12#include <linux/initrd.h>
13#include <linux/bootmem.h>
14#include <linux/console.h>
15#include <linux/seq_file.h>
16#include <linux/root_dev.h>
17#include <linux/utsname.h>
18#include <linux/nodemask.h>
19#include <linux/cpu.h>
20#include <linux/pfn.h>
21#include <linux/fs.h>
22#include <linux/mm.h>
23#include <linux/kexec.h>
24#include <asm/uaccess.h>
25#include <asm/io.h>
26#include <asm/sections.h>
27#include <asm/irq.h>
28#include <asm/setup.h>
29#include <asm/clock.h>
30#include <asm/mmu_context.h>
31
32extern void * __rd_start, * __rd_end;
33
34/*
35 * Machine setup..
36 */
37
38/*
39 * Initialize loops_per_jiffy as 10000000 (1000MIPS).
40 * This value will be used at the very early stage of serial setup.
41 * The bigger value means no problem.
42 */
43struct sh_cpuinfo boot_cpu_data = { CPU_SH_NONE, 10000000, };
44#ifdef CONFIG_VT
45struct screen_info screen_info;
46#endif
47
48#if defined(CONFIG_SH_UNKNOWN)
49struct sh_machine_vector sh_mv;
50#endif
51
52extern int root_mountflags;
53
54#define MV_NAME_SIZE 32
55
56static struct sh_machine_vector* __init get_mv_byname(const char* name);
57
58/*
59 * This is set up by the setup-routine at boot-time
60 */
61#define PARAM	((unsigned char *)empty_zero_page)
62
63#define MOUNT_ROOT_RDONLY (*(unsigned long *) (PARAM+0x000))
64#define RAMDISK_FLAGS (*(unsigned long *) (PARAM+0x004))
65#define ORIG_ROOT_DEV (*(unsigned long *) (PARAM+0x008))
66#define LOADER_TYPE (*(unsigned long *) (PARAM+0x00c))
67#define INITRD_START (*(unsigned long *) (PARAM+0x010))
68#define INITRD_SIZE (*(unsigned long *) (PARAM+0x014))
69/* ... */
70#define COMMAND_LINE ((char *) (PARAM+0x100))
71
72#define RAMDISK_IMAGE_START_MASK	0x07FF
73#define RAMDISK_PROMPT_FLAG		0x8000
74#define RAMDISK_LOAD_FLAG		0x4000
75
76static char __initdata command_line[COMMAND_LINE_SIZE] = { 0, };
77
78static struct resource code_resource = { .name = "Kernel code", };
79static struct resource data_resource = { .name = "Kernel data", };
80
81unsigned long memory_start, memory_end;
82
83static inline void parse_cmdline (char ** cmdline_p, char mv_name[MV_NAME_SIZE],
84				  struct sh_machine_vector** mvp,
85				  unsigned long *mv_io_base)
86{
87	char c = ' ', *to = command_line, *from = COMMAND_LINE;
88	int len = 0;
89
90	/* Save unparsed command line copy for /proc/cmdline */
91	memcpy(boot_command_line, COMMAND_LINE, COMMAND_LINE_SIZE);
92	boot_command_line[COMMAND_LINE_SIZE-1] = '\0';
93
94	memory_start = (unsigned long)PAGE_OFFSET+__MEMORY_START;
95	memory_end = memory_start + __MEMORY_SIZE;
96
97	for (;;) {
98		if (c == ' ' && !memcmp(from, "mem=", 4)) {
99			if (to != command_line)
100				to--;
101			{
102				unsigned long mem_size;
103
104				mem_size = memparse(from+4, &from);
105				memory_end = memory_start + mem_size;
106			}
107		}
108
109		if (c == ' ' && !memcmp(from, "sh_mv=", 6)) {
110			char* mv_end;
111			char* mv_comma;
112			int mv_len;
113			if (to != command_line)
114				to--;
115			from += 6;
116			mv_end = strchr(from, ' ');
117			if (mv_end == NULL)
118				mv_end = from + strlen(from);
119
120			mv_comma = strchr(from, ',');
121			if ((mv_comma != NULL) && (mv_comma < mv_end)) {
122				int ints[3];
123				get_options(mv_comma+1, ARRAY_SIZE(ints), ints);
124				*mv_io_base = ints[1];
125				mv_len = mv_comma - from;
126			} else {
127				mv_len = mv_end - from;
128			}
129			if (mv_len > (MV_NAME_SIZE-1))
130				mv_len = MV_NAME_SIZE-1;
131			memcpy(mv_name, from, mv_len);
132			mv_name[mv_len] = '\0';
133			from = mv_end;
134
135			*mvp = get_mv_byname(mv_name);
136		}
137
138		c = *(from++);
139		if (!c)
140			break;
141		if (COMMAND_LINE_SIZE <= ++len)
142			break;
143		*(to++) = c;
144	}
145	*to = '\0';
146	*cmdline_p = command_line;
147}
148
149static int __init sh_mv_setup(char **cmdline_p)
150{
151#ifdef CONFIG_SH_UNKNOWN
152	extern struct sh_machine_vector mv_unknown;
153#endif
154	struct sh_machine_vector *mv = NULL;
155	char mv_name[MV_NAME_SIZE] = "";
156	unsigned long mv_io_base = 0;
157
158	parse_cmdline(cmdline_p, mv_name, &mv, &mv_io_base);
159
160#ifdef CONFIG_SH_UNKNOWN
161	if (mv == NULL) {
162		mv = &mv_unknown;
163		if (*mv_name != '\0') {
164			printk("Warning: Unsupported machine %s, using unknown\n",
165			       mv_name);
166		}
167	}
168	sh_mv = *mv;
169#endif
170
171	/*
172	 * Manually walk the vec, fill in anything that the board hasn't yet
173	 * by hand, wrapping to the generic implementation.
174	 */
175#define mv_set(elem) do { \
176	if (!sh_mv.mv_##elem) \
177		sh_mv.mv_##elem = generic_##elem; \
178} while (0)
179
180	mv_set(inb);	mv_set(inw);	mv_set(inl);
181	mv_set(outb);	mv_set(outw);	mv_set(outl);
182
183	mv_set(inb_p);	mv_set(inw_p);	mv_set(inl_p);
184	mv_set(outb_p);	mv_set(outw_p);	mv_set(outl_p);
185
186	mv_set(insb);	mv_set(insw);	mv_set(insl);
187	mv_set(outsb);	mv_set(outsw);	mv_set(outsl);
188
189	mv_set(readb);	mv_set(readw);	mv_set(readl);
190	mv_set(writeb);	mv_set(writew);	mv_set(writel);
191
192	mv_set(ioport_map);
193	mv_set(ioport_unmap);
194	mv_set(irq_demux);
195
196#ifdef CONFIG_SH_UNKNOWN
197	__set_io_port_base(mv_io_base);
198#endif
199
200	if (!sh_mv.mv_nr_irqs)
201		sh_mv.mv_nr_irqs = NR_IRQS;
202
203	return 0;
204}
205
206/*
207 * Register fully available low RAM pages with the bootmem allocator.
208 */
209static void __init register_bootmem_low_pages(void)
210{
211	unsigned long curr_pfn, last_pfn, pages;
212
213	/*
214	 * We are rounding up the start address of usable memory:
215	 */
216	curr_pfn = PFN_UP(__MEMORY_START);
217
218	/*
219	 * ... and at the end of the usable range downwards:
220	 */
221	last_pfn = PFN_DOWN(__pa(memory_end));
222
223	if (last_pfn > max_low_pfn)
224		last_pfn = max_low_pfn;
225
226	pages = last_pfn - curr_pfn;
227	free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(pages));
228}
229
230void __init setup_bootmem_allocator(unsigned long start_pfn)
231{
232	unsigned long bootmap_size;
233
234	/*
235	 * Find a proper area for the bootmem bitmap. After this
236	 * bootstrap step all allocations (until the page allocator
237	 * is intact) must be done via bootmem_alloc().
238	 */
239	bootmap_size = init_bootmem_node(NODE_DATA(0), start_pfn,
240					 min_low_pfn, max_low_pfn);
241
242	register_bootmem_low_pages();
243
244	node_set_online(0);
245
246	/*
247	 * Reserve the kernel text and
248	 * Reserve the bootmem bitmap. We do this in two steps (first step
249	 * was init_bootmem()), because this catches the (definitely buggy)
250	 * case of us accidentally initializing the bootmem allocator with
251	 * an invalid RAM area.
252	 */
253	reserve_bootmem(__MEMORY_START+PAGE_SIZE,
254		(PFN_PHYS(start_pfn)+bootmap_size+PAGE_SIZE-1)-__MEMORY_START);
255
256	/*
257	 * reserve physical page 0 - it's a special BIOS page on many boxes,
258	 * enabling clean reboots, SMP operation, laptop functions.
259	 */
260	reserve_bootmem(__MEMORY_START, PAGE_SIZE);
261
262#ifdef CONFIG_BLK_DEV_INITRD
263	ROOT_DEV = MKDEV(RAMDISK_MAJOR, 0);
264	if (&__rd_start != &__rd_end) {
265		LOADER_TYPE = 1;
266		INITRD_START = PHYSADDR((unsigned long)&__rd_start) -
267					__MEMORY_START;
268		INITRD_SIZE = (unsigned long)&__rd_end -
269			      (unsigned long)&__rd_start;
270	}
271
272	if (LOADER_TYPE && INITRD_START) {
273		if (INITRD_START + INITRD_SIZE <= (max_low_pfn << PAGE_SHIFT)) {
274			reserve_bootmem(INITRD_START + __MEMORY_START,
275					INITRD_SIZE);
276			initrd_start = INITRD_START + PAGE_OFFSET +
277					__MEMORY_START;
278			initrd_end = initrd_start + INITRD_SIZE;
279		} else {
280			printk("initrd extends beyond end of memory "
281			    "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
282				    INITRD_START + INITRD_SIZE,
283				    max_low_pfn << PAGE_SHIFT);
284			initrd_start = 0;
285		}
286	}
287#endif
288#ifdef CONFIG_KEXEC
289	if (crashk_res.start != crashk_res.end)
290		reserve_bootmem(crashk_res.start,
291			crashk_res.end - crashk_res.start + 1);
292#endif
293}
294
295#ifndef CONFIG_NEED_MULTIPLE_NODES
296static void __init setup_memory(void)
297{
298	unsigned long start_pfn;
299
300	/*
301	 * Partially used pages are not usable - thus
302	 * we are rounding upwards:
303	 */
304	start_pfn = PFN_UP(__pa(_end));
305	setup_bootmem_allocator(start_pfn);
306}
307#else
308extern void __init setup_memory(void);
309#endif
310
311void __init setup_arch(char **cmdline_p)
312{
313	enable_mmu();
314
315#ifdef CONFIG_CMDLINE_BOOL
316	strcpy(COMMAND_LINE, CONFIG_CMDLINE);
317#endif
318
319	ROOT_DEV = old_decode_dev(ORIG_ROOT_DEV);
320
321#ifdef CONFIG_BLK_DEV_RAM
322	rd_image_start = RAMDISK_FLAGS & RAMDISK_IMAGE_START_MASK;
323	rd_prompt = ((RAMDISK_FLAGS & RAMDISK_PROMPT_FLAG) != 0);
324	rd_doload = ((RAMDISK_FLAGS & RAMDISK_LOAD_FLAG) != 0);
325#endif
326
327	if (!MOUNT_ROOT_RDONLY)
328		root_mountflags &= ~MS_RDONLY;
329	init_mm.start_code = (unsigned long) _text;
330	init_mm.end_code = (unsigned long) _etext;
331	init_mm.end_data = (unsigned long) _edata;
332	init_mm.brk = (unsigned long) _end;
333
334	code_resource.start = virt_to_phys(_text);
335	code_resource.end = virt_to_phys(_etext)-1;
336	data_resource.start = virt_to_phys(_etext);
337	data_resource.end = virt_to_phys(_edata)-1;
338
339	parse_early_param();
340
341	sh_mv_setup(cmdline_p);
342
343	/*
344	 * Find the highest page frame number we have available
345	 */
346	max_pfn = PFN_DOWN(__pa(memory_end));
347
348	/*
349	 * Determine low and high memory ranges:
350	 */
351	max_low_pfn = max_pfn;
352	min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
353
354	nodes_clear(node_online_map);
355	setup_memory();
356	paging_init();
357	sparse_init();
358
359#ifdef CONFIG_DUMMY_CONSOLE
360	conswitchp = &dummy_con;
361#endif
362
363	/* Perform the machine specific initialisation */
364	if (likely(sh_mv.mv_setup))
365		sh_mv.mv_setup(cmdline_p);
366}
367
368struct sh_machine_vector* __init get_mv_byname(const char* name)
369{
370	extern long __machvec_start, __machvec_end;
371	struct sh_machine_vector *all_vecs =
372		(struct sh_machine_vector *)&__machvec_start;
373
374	int i, n = ((unsigned long)&__machvec_end
375		    - (unsigned long)&__machvec_start)/
376		sizeof(struct sh_machine_vector);
377
378	for (i = 0; i < n; ++i) {
379		struct sh_machine_vector *mv = &all_vecs[i];
380		if (mv == NULL)
381			continue;
382		if (strcasecmp(name, get_system_type()) == 0) {
383			return mv;
384		}
385	}
386	return NULL;
387}
388
389static struct cpu cpu[NR_CPUS];
390
391static int __init topology_init(void)
392{
393	int cpu_id;
394
395	for_each_possible_cpu(cpu_id)
396		register_cpu(&cpu[cpu_id], cpu_id);
397
398	return 0;
399}
400
401subsys_initcall(topology_init);
402
403static const char *cpu_name[] = {
404	[CPU_SH7206]	= "SH7206",	[CPU_SH7619]	= "SH7619",
405	[CPU_SH7604]	= "SH7604",	[CPU_SH7300]	= "SH7300",
406	[CPU_SH7705]	= "SH7705",	[CPU_SH7706]	= "SH7706",
407	[CPU_SH7707]	= "SH7707",	[CPU_SH7708]	= "SH7708",
408	[CPU_SH7709]	= "SH7709",	[CPU_SH7710]	= "SH7710",
409	[CPU_SH7712]	= "SH7712",
410	[CPU_SH7729]	= "SH7729",	[CPU_SH7750]	= "SH7750",
411	[CPU_SH7750S]	= "SH7750S",	[CPU_SH7750R]	= "SH7750R",
412	[CPU_SH7751]	= "SH7751",	[CPU_SH7751R]	= "SH7751R",
413	[CPU_SH7760]	= "SH7760",	[CPU_SH73180]	= "SH73180",
414	[CPU_ST40RA]	= "ST40RA",	[CPU_ST40GX1]	= "ST40GX1",
415	[CPU_SH4_202]	= "SH4-202",	[CPU_SH4_501]	= "SH4-501",
416	[CPU_SH7770]	= "SH7770",	[CPU_SH7780]	= "SH7780",
417	[CPU_SH7781]	= "SH7781",	[CPU_SH7343]	= "SH7343",
418	[CPU_SH7785]	= "SH7785",	[CPU_SH7722]	= "SH7722",
419	[CPU_SH_NONE]	= "Unknown"
420};
421
422const char *get_cpu_subtype(struct sh_cpuinfo *c)
423{
424	return cpu_name[c->type];
425}
426
427#ifdef CONFIG_PROC_FS
428/* Symbolic CPU flags, keep in sync with asm/cpu-features.h */
429static const char *cpu_flags[] = {
430	"none", "fpu", "p2flush", "mmuassoc", "dsp", "perfctr",
431	"ptea", "llsc", "l2", "op32", NULL
432};
433
434static void show_cpuflags(struct seq_file *m, struct sh_cpuinfo *c)
435{
436	unsigned long i;
437
438	seq_printf(m, "cpu flags\t:");
439
440	if (!c->flags) {
441		seq_printf(m, " %s\n", cpu_flags[0]);
442		return;
443	}
444
445	for (i = 0; cpu_flags[i]; i++)
446		if ((c->flags & (1 << i)))
447			seq_printf(m, " %s", cpu_flags[i+1]);
448
449	seq_printf(m, "\n");
450}
451
452static void show_cacheinfo(struct seq_file *m, const char *type,
453			   struct cache_info info)
454{
455	unsigned int cache_size;
456
457	cache_size = info.ways * info.sets * info.linesz;
458
459	seq_printf(m, "%s size\t: %2dKiB (%d-way)\n",
460		   type, cache_size >> 10, info.ways);
461}
462
463/*
464 *	Get CPU information for use by the procfs.
465 */
466static int show_cpuinfo(struct seq_file *m, void *v)
467{
468	struct sh_cpuinfo *c = v;
469	unsigned int cpu = c - cpu_data;
470
471	if (!cpu_online(cpu))
472		return 0;
473
474	if (cpu == 0)
475		seq_printf(m, "machine\t\t: %s\n", get_system_type());
476
477	seq_printf(m, "processor\t: %d\n", cpu);
478	seq_printf(m, "cpu family\t: %s\n", init_utsname()->machine);
479	seq_printf(m, "cpu type\t: %s\n", get_cpu_subtype(c));
480
481	show_cpuflags(m, c);
482
483	seq_printf(m, "cache type\t: ");
484
485	/*
486	 * Check for what type of cache we have, we support both the
487	 * unified cache on the SH-2 and SH-3, as well as the harvard
488	 * style cache on the SH-4.
489	 */
490	if (c->icache.flags & SH_CACHE_COMBINED) {
491		seq_printf(m, "unified\n");
492		show_cacheinfo(m, "cache", c->icache);
493	} else {
494		seq_printf(m, "split (harvard)\n");
495		show_cacheinfo(m, "icache", c->icache);
496		show_cacheinfo(m, "dcache", c->dcache);
497	}
498
499	/* Optional secondary cache */
500	if (c->flags & CPU_HAS_L2_CACHE)
501		show_cacheinfo(m, "scache", c->scache);
502
503	seq_printf(m, "bogomips\t: %lu.%02lu\n",
504		     c->loops_per_jiffy/(500000/HZ),
505		     (c->loops_per_jiffy/(5000/HZ)) % 100);
506
507	return 0;
508}
509
510static void *c_start(struct seq_file *m, loff_t *pos)
511{
512	return *pos < NR_CPUS ? cpu_data + *pos : NULL;
513}
514static void *c_next(struct seq_file *m, void *v, loff_t *pos)
515{
516	++*pos;
517	return c_start(m, pos);
518}
519static void c_stop(struct seq_file *m, void *v)
520{
521}
522struct seq_operations cpuinfo_op = {
523	.start	= c_start,
524	.next	= c_next,
525	.stop	= c_stop,
526	.show	= show_cpuinfo,
527};
528#endif /* CONFIG_PROC_FS */
529