xref: /linux/arch/sh/kernel/setup.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /* $Id: setup.c,v 1.30 2003/10/13 07:21:19 lethal Exp $
2  *
3  *  linux/arch/sh/kernel/setup.c
4  *
5  *  Copyright (C) 1999  Niibe Yutaka
6  *  Copyright (C) 2002, 2003  Paul Mundt
7  */
8 
9 /*
10  * This file handles the architecture-dependent parts of initialization
11  */
12 
13 #include <linux/tty.h>
14 #include <linux/ioport.h>
15 #include <linux/init.h>
16 #include <linux/initrd.h>
17 #include <linux/bootmem.h>
18 #include <linux/console.h>
19 #include <linux/seq_file.h>
20 #include <linux/root_dev.h>
21 #include <linux/utsname.h>
22 #include <linux/cpu.h>
23 #include <linux/pfn.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 
31 #ifdef CONFIG_SH_KGDB
32 #include <asm/kgdb.h>
33 static int kgdb_parse_options(char *options);
34 #endif
35 extern void * __rd_start, * __rd_end;
36 /*
37  * Machine setup..
38  */
39 
40 /*
41  * Initialize loops_per_jiffy as 10000000 (1000MIPS).
42  * This value will be used at the very early stage of serial setup.
43  * The bigger value means no problem.
44  */
45 struct sh_cpuinfo boot_cpu_data = { CPU_SH_NONE, 10000000, };
46 struct screen_info screen_info;
47 
48 #if defined(CONFIG_SH_UNKNOWN)
49 struct sh_machine_vector sh_mv;
50 #endif
51 
52 /* We need this to satisfy some external references. */
53 struct screen_info screen_info = {
54         0, 25,                  /* orig-x, orig-y */
55         0,                      /* unused */
56         0,                      /* orig-video-page */
57         0,                      /* orig-video-mode */
58         80,                     /* orig-video-cols */
59         0,0,0,                  /* ega_ax, ega_bx, ega_cx */
60         25,                     /* orig-video-lines */
61         0,                      /* orig-video-isVGA */
62         16                      /* orig-video-points */
63 };
64 
65 extern void platform_setup(void);
66 extern char *get_system_type(void);
67 extern int root_mountflags;
68 
69 #define MV_NAME_SIZE 32
70 
71 static struct sh_machine_vector* __init get_mv_byname(const char* name);
72 
73 /*
74  * This is set up by the setup-routine at boot-time
75  */
76 #define PARAM	((unsigned char *)empty_zero_page)
77 
78 #define MOUNT_ROOT_RDONLY (*(unsigned long *) (PARAM+0x000))
79 #define RAMDISK_FLAGS (*(unsigned long *) (PARAM+0x004))
80 #define ORIG_ROOT_DEV (*(unsigned long *) (PARAM+0x008))
81 #define LOADER_TYPE (*(unsigned long *) (PARAM+0x00c))
82 #define INITRD_START (*(unsigned long *) (PARAM+0x010))
83 #define INITRD_SIZE (*(unsigned long *) (PARAM+0x014))
84 /* ... */
85 #define COMMAND_LINE ((char *) (PARAM+0x100))
86 
87 #define RAMDISK_IMAGE_START_MASK	0x07FF
88 #define RAMDISK_PROMPT_FLAG		0x8000
89 #define RAMDISK_LOAD_FLAG		0x4000
90 
91 static char command_line[COMMAND_LINE_SIZE] = { 0, };
92 
93 struct resource standard_io_resources[] = {
94 	{ "dma1", 0x00, 0x1f },
95 	{ "pic1", 0x20, 0x3f },
96 	{ "timer", 0x40, 0x5f },
97 	{ "keyboard", 0x60, 0x6f },
98 	{ "dma page reg", 0x80, 0x8f },
99 	{ "pic2", 0xa0, 0xbf },
100 	{ "dma2", 0xc0, 0xdf },
101 	{ "fpu", 0xf0, 0xff }
102 };
103 
104 #define STANDARD_IO_RESOURCES (sizeof(standard_io_resources)/sizeof(struct resource))
105 
106 /* System RAM - interrupted by the 640kB-1M hole */
107 #define code_resource (ram_resources[3])
108 #define data_resource (ram_resources[4])
109 static struct resource ram_resources[] = {
110 	{ "System RAM", 0x000000, 0x09ffff, IORESOURCE_BUSY },
111 	{ "System RAM", 0x100000, 0x100000, IORESOURCE_BUSY },
112 	{ "Video RAM area", 0x0a0000, 0x0bffff },
113 	{ "Kernel code", 0x100000, 0 },
114 	{ "Kernel data", 0, 0 }
115 };
116 
117 unsigned long memory_start, memory_end;
118 
119 static inline void parse_cmdline (char ** cmdline_p, char mv_name[MV_NAME_SIZE],
120 				  struct sh_machine_vector** mvp,
121 				  unsigned long *mv_io_base,
122 				  int *mv_mmio_enable)
123 {
124 	char c = ' ', *to = command_line, *from = COMMAND_LINE;
125 	int len = 0;
126 
127 	/* Save unparsed command line copy for /proc/cmdline */
128 	memcpy(saved_command_line, COMMAND_LINE, COMMAND_LINE_SIZE);
129 	saved_command_line[COMMAND_LINE_SIZE-1] = '\0';
130 
131 	memory_start = (unsigned long)PAGE_OFFSET+__MEMORY_START;
132 	memory_end = memory_start + __MEMORY_SIZE;
133 
134 	for (;;) {
135 		/*
136 		 * "mem=XXX[kKmM]" defines a size of memory.
137 		 */
138 		if (c == ' ' && !memcmp(from, "mem=", 4)) {
139 			if (to != command_line)
140 				to--;
141 			{
142 				unsigned long mem_size;
143 
144 				mem_size = memparse(from+4, &from);
145 				memory_end = memory_start + mem_size;
146 			}
147 		}
148 		if (c == ' ' && !memcmp(from, "sh_mv=", 6)) {
149 			char* mv_end;
150 			char* mv_comma;
151 			int mv_len;
152 			if (to != command_line)
153 				to--;
154 			from += 6;
155 			mv_end = strchr(from, ' ');
156 			if (mv_end == NULL)
157 				mv_end = from + strlen(from);
158 
159 			mv_comma = strchr(from, ',');
160 			if ((mv_comma != NULL) && (mv_comma < mv_end)) {
161 				int ints[3];
162 				get_options(mv_comma+1, ARRAY_SIZE(ints), ints);
163 				*mv_io_base = ints[1];
164 				*mv_mmio_enable = ints[2];
165 				mv_len = mv_comma - from;
166 			} else {
167 				mv_len = mv_end - from;
168 			}
169 			if (mv_len > (MV_NAME_SIZE-1))
170 				mv_len = MV_NAME_SIZE-1;
171 			memcpy(mv_name, from, mv_len);
172 			mv_name[mv_len] = '\0';
173 			from = mv_end;
174 
175 			*mvp = get_mv_byname(mv_name);
176 		}
177 		c = *(from++);
178 		if (!c)
179 			break;
180 		if (COMMAND_LINE_SIZE <= ++len)
181 			break;
182 		*(to++) = c;
183 	}
184 	*to = '\0';
185 	*cmdline_p = command_line;
186 }
187 
188 static int __init sh_mv_setup(char **cmdline_p)
189 {
190 #ifdef CONFIG_SH_UNKNOWN
191 	extern struct sh_machine_vector mv_unknown;
192 #endif
193 	struct sh_machine_vector *mv = NULL;
194 	char mv_name[MV_NAME_SIZE] = "";
195 	unsigned long mv_io_base = 0;
196 	int mv_mmio_enable = 0;
197 
198 	parse_cmdline(cmdline_p, mv_name, &mv, &mv_io_base, &mv_mmio_enable);
199 
200 #ifdef CONFIG_SH_UNKNOWN
201 	if (mv == NULL) {
202 		mv = &mv_unknown;
203 		if (*mv_name != '\0') {
204 			printk("Warning: Unsupported machine %s, using unknown\n",
205 			       mv_name);
206 		}
207 	}
208 	sh_mv = *mv;
209 #endif
210 
211 	/*
212 	 * Manually walk the vec, fill in anything that the board hasn't yet
213 	 * by hand, wrapping to the generic implementation.
214 	 */
215 #define mv_set(elem) do { \
216 	if (!sh_mv.mv_##elem) \
217 		sh_mv.mv_##elem = generic_##elem; \
218 } while (0)
219 
220 	mv_set(inb);	mv_set(inw);	mv_set(inl);
221 	mv_set(outb);	mv_set(outw);	mv_set(outl);
222 
223 	mv_set(inb_p);	mv_set(inw_p);	mv_set(inl_p);
224 	mv_set(outb_p);	mv_set(outw_p);	mv_set(outl_p);
225 
226 	mv_set(insb);	mv_set(insw);	mv_set(insl);
227 	mv_set(outsb);	mv_set(outsw);	mv_set(outsl);
228 
229 	mv_set(readb);	mv_set(readw);	mv_set(readl);
230 	mv_set(writeb);	mv_set(writew);	mv_set(writel);
231 
232 	mv_set(ioport_map);
233 	mv_set(ioport_unmap);
234 	mv_set(irq_demux);
235 
236 #ifdef CONFIG_SH_UNKNOWN
237 	__set_io_port_base(mv_io_base);
238 #endif
239 
240 	return 0;
241 }
242 
243 void __init setup_arch(char **cmdline_p)
244 {
245 	unsigned long bootmap_size;
246 	unsigned long start_pfn, max_pfn, max_low_pfn;
247 
248 #ifdef CONFIG_EARLY_PRINTK
249 	extern void enable_early_printk(void);
250 
251 	enable_early_printk();
252 #endif
253 #ifdef CONFIG_CMDLINE_BOOL
254         strcpy(COMMAND_LINE, CONFIG_CMDLINE);
255 #endif
256 
257 	ROOT_DEV = old_decode_dev(ORIG_ROOT_DEV);
258 
259 #ifdef CONFIG_BLK_DEV_RAM
260 	rd_image_start = RAMDISK_FLAGS & RAMDISK_IMAGE_START_MASK;
261 	rd_prompt = ((RAMDISK_FLAGS & RAMDISK_PROMPT_FLAG) != 0);
262 	rd_doload = ((RAMDISK_FLAGS & RAMDISK_LOAD_FLAG) != 0);
263 #endif
264 
265 	if (!MOUNT_ROOT_RDONLY)
266 		root_mountflags &= ~MS_RDONLY;
267 	init_mm.start_code = (unsigned long) _text;
268 	init_mm.end_code = (unsigned long) _etext;
269 	init_mm.end_data = (unsigned long) _edata;
270 	init_mm.brk = (unsigned long) _end;
271 
272 	code_resource.start = (unsigned long)virt_to_phys(_text);
273 	code_resource.end = (unsigned long)virt_to_phys(_etext)-1;
274 	data_resource.start = (unsigned long)virt_to_phys(_etext);
275 	data_resource.end = (unsigned long)virt_to_phys(_edata)-1;
276 
277 	sh_mv_setup(cmdline_p);
278 
279 	/*
280 	 * Find the highest page frame number we have available
281 	 */
282 	max_pfn = PFN_DOWN(__pa(memory_end));
283 
284 	/*
285 	 * Determine low and high memory ranges:
286 	 */
287 	max_low_pfn = max_pfn;
288 
289 	/*
290 	 * Partially used pages are not usable - thus
291 	 * we are rounding upwards:
292 	 */
293 	start_pfn = PFN_UP(__pa(_end));
294 
295 	/*
296 	 * Find a proper area for the bootmem bitmap. After this
297 	 * bootstrap step all allocations (until the page allocator
298 	 * is intact) must be done via bootmem_alloc().
299 	 */
300 	bootmap_size = init_bootmem_node(NODE_DATA(0), start_pfn,
301 					 __MEMORY_START>>PAGE_SHIFT,
302 					 max_low_pfn);
303 	/*
304 	 * Register fully available low RAM pages with the bootmem allocator.
305 	 */
306 	{
307 		unsigned long curr_pfn, last_pfn, pages;
308 
309 		/*
310 		 * We are rounding up the start address of usable memory:
311 		 */
312 		curr_pfn = PFN_UP(__MEMORY_START);
313 		/*
314 		 * ... and at the end of the usable range downwards:
315 		 */
316 		last_pfn = PFN_DOWN(__pa(memory_end));
317 
318 		if (last_pfn > max_low_pfn)
319 			last_pfn = max_low_pfn;
320 
321 		pages = last_pfn - curr_pfn;
322 		free_bootmem_node(NODE_DATA(0), PFN_PHYS(curr_pfn),
323 				  PFN_PHYS(pages));
324 	}
325 
326 	/*
327 	 * Reserve the kernel text and
328 	 * Reserve the bootmem bitmap. We do this in two steps (first step
329 	 * was init_bootmem()), because this catches the (definitely buggy)
330 	 * case of us accidentally initializing the bootmem allocator with
331 	 * an invalid RAM area.
332 	 */
333 	reserve_bootmem_node(NODE_DATA(0), __MEMORY_START+PAGE_SIZE,
334 		(PFN_PHYS(start_pfn)+bootmap_size+PAGE_SIZE-1)-__MEMORY_START);
335 
336 	/*
337 	 * reserve physical page 0 - it's a special BIOS page on many boxes,
338 	 * enabling clean reboots, SMP operation, laptop functions.
339 	 */
340 	reserve_bootmem_node(NODE_DATA(0), __MEMORY_START, PAGE_SIZE);
341 
342 #ifdef CONFIG_BLK_DEV_INITRD
343 	ROOT_DEV = MKDEV(RAMDISK_MAJOR, 0);
344 	if (&__rd_start != &__rd_end) {
345 		LOADER_TYPE = 1;
346 		INITRD_START = PHYSADDR((unsigned long)&__rd_start) - __MEMORY_START;
347 		INITRD_SIZE = (unsigned long)&__rd_end - (unsigned long)&__rd_start;
348 	}
349 
350 	if (LOADER_TYPE && INITRD_START) {
351 		if (INITRD_START + INITRD_SIZE <= (max_low_pfn << PAGE_SHIFT)) {
352 			reserve_bootmem_node(NODE_DATA(0), INITRD_START+__MEMORY_START, INITRD_SIZE);
353 			initrd_start =
354 				INITRD_START ? INITRD_START + PAGE_OFFSET + __MEMORY_START : 0;
355 			initrd_end = initrd_start + INITRD_SIZE;
356 		} else {
357 			printk("initrd extends beyond end of memory "
358 			    "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
359 				    INITRD_START + INITRD_SIZE,
360 				    max_low_pfn << PAGE_SHIFT);
361 			initrd_start = 0;
362 		}
363 	}
364 #endif
365 
366 #ifdef CONFIG_DUMMY_CONSOLE
367 	conswitchp = &dummy_con;
368 #endif
369 
370 	/* Perform the machine specific initialisation */
371 	platform_setup();
372 
373 	paging_init();
374 }
375 
376 struct sh_machine_vector* __init get_mv_byname(const char* name)
377 {
378 	extern int strcasecmp(const char *, const char *);
379 	extern long __machvec_start, __machvec_end;
380 	struct sh_machine_vector *all_vecs =
381 		(struct sh_machine_vector *)&__machvec_start;
382 
383 	int i, n = ((unsigned long)&__machvec_end
384 		    - (unsigned long)&__machvec_start)/
385 		sizeof(struct sh_machine_vector);
386 
387 	for (i = 0; i < n; ++i) {
388 		struct sh_machine_vector *mv = &all_vecs[i];
389 		if (mv == NULL)
390 			continue;
391 		if (strcasecmp(name, get_system_type()) == 0) {
392 			return mv;
393 		}
394 	}
395 	return NULL;
396 }
397 
398 static struct cpu cpu[NR_CPUS];
399 
400 static int __init topology_init(void)
401 {
402 	int cpu_id;
403 
404 	for_each_possible_cpu(cpu_id)
405 		register_cpu(&cpu[cpu_id], cpu_id, NULL);
406 
407 	return 0;
408 }
409 
410 subsys_initcall(topology_init);
411 
412 static const char *cpu_name[] = {
413 	[CPU_SH7604]	= "SH7604",
414 	[CPU_SH7705]	= "SH7705",
415 	[CPU_SH7708]	= "SH7708",
416 	[CPU_SH7729]	= "SH7729",
417 	[CPU_SH7300]	= "SH7300",
418 	[CPU_SH7750]	= "SH7750",
419 	[CPU_SH7750S]	= "SH7750S",
420 	[CPU_SH7750R]	= "SH7750R",
421 	[CPU_SH7751]	= "SH7751",
422 	[CPU_SH7751R]	= "SH7751R",
423 	[CPU_SH7760]	= "SH7760",
424 	[CPU_SH73180]	= "SH73180",
425 	[CPU_ST40RA]	= "ST40RA",
426 	[CPU_ST40GX1]	= "ST40GX1",
427 	[CPU_SH4_202]	= "SH4-202",
428 	[CPU_SH4_501]	= "SH4-501",
429 	[CPU_SH7770]	= "SH7770",
430 	[CPU_SH7780]	= "SH7780",
431 	[CPU_SH7781]	= "SH7781",
432 	[CPU_SH_NONE]	= "Unknown"
433 };
434 
435 const char *get_cpu_subtype(void)
436 {
437 	return cpu_name[boot_cpu_data.type];
438 }
439 
440 #ifdef CONFIG_PROC_FS
441 static const char *cpu_flags[] = {
442 	"none", "fpu", "p2flush", "mmuassoc", "dsp", "perfctr", "ptea", NULL
443 };
444 
445 static void show_cpuflags(struct seq_file *m)
446 {
447 	unsigned long i;
448 
449 	seq_printf(m, "cpu flags\t:");
450 
451 	if (!cpu_data->flags) {
452 		seq_printf(m, " %s\n", cpu_flags[0]);
453 		return;
454 	}
455 
456 	for (i = 0; cpu_flags[i]; i++)
457 		if ((cpu_data->flags & (1 << i)))
458 			seq_printf(m, " %s", cpu_flags[i+1]);
459 
460 	seq_printf(m, "\n");
461 }
462 
463 static void show_cacheinfo(struct seq_file *m, const char *type, struct cache_info info)
464 {
465 	unsigned int cache_size;
466 
467 	cache_size = info.ways * info.sets * info.linesz;
468 
469 	seq_printf(m, "%s size\t: %2dKiB (%d-way)\n",
470 		   type, cache_size >> 10, info.ways);
471 }
472 
473 /*
474  *	Get CPU information for use by the procfs.
475  */
476 static int show_cpuinfo(struct seq_file *m, void *v)
477 {
478 	unsigned int cpu = smp_processor_id();
479 
480 	if (!cpu && cpu_online(cpu))
481 		seq_printf(m, "machine\t\t: %s\n", get_system_type());
482 
483 	seq_printf(m, "processor\t: %d\n", cpu);
484 	seq_printf(m, "cpu family\t: %s\n", system_utsname.machine);
485 	seq_printf(m, "cpu type\t: %s\n", get_cpu_subtype());
486 
487 	show_cpuflags(m);
488 
489 	seq_printf(m, "cache type\t: ");
490 
491 	/*
492 	 * Check for what type of cache we have, we support both the
493 	 * unified cache on the SH-2 and SH-3, as well as the harvard
494 	 * style cache on the SH-4.
495 	 */
496 	if (test_bit(SH_CACHE_COMBINED, &(boot_cpu_data.icache.flags))) {
497 		seq_printf(m, "unified\n");
498 		show_cacheinfo(m, "cache", boot_cpu_data.icache);
499 	} else {
500 		seq_printf(m, "split (harvard)\n");
501 		show_cacheinfo(m, "icache", boot_cpu_data.icache);
502 		show_cacheinfo(m, "dcache", boot_cpu_data.dcache);
503 	}
504 
505 	seq_printf(m, "bogomips\t: %lu.%02lu\n",
506 		     boot_cpu_data.loops_per_jiffy/(500000/HZ),
507 		     (boot_cpu_data.loops_per_jiffy/(5000/HZ)) % 100);
508 
509 	return show_clocks(m);
510 }
511 
512 static void *c_start(struct seq_file *m, loff_t *pos)
513 {
514 	return *pos < NR_CPUS ? cpu_data + *pos : NULL;
515 }
516 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
517 {
518 	++*pos;
519 	return c_start(m, pos);
520 }
521 static void c_stop(struct seq_file *m, void *v)
522 {
523 }
524 struct seq_operations cpuinfo_op = {
525 	.start	= c_start,
526 	.next	= c_next,
527 	.stop	= c_stop,
528 	.show	= show_cpuinfo,
529 };
530 #endif /* CONFIG_PROC_FS */
531 
532 #ifdef CONFIG_SH_KGDB
533 /*
534  * Parse command-line kgdb options.  By default KGDB is enabled,
535  * entered on error (or other action) using default serial info.
536  * The command-line option can include a serial port specification
537  * and an action to override default or configured behavior.
538  */
539 struct kgdb_sermap kgdb_sci_sermap =
540 { "ttySC", 5, kgdb_sci_setup, NULL };
541 
542 struct kgdb_sermap *kgdb_serlist = &kgdb_sci_sermap;
543 struct kgdb_sermap *kgdb_porttype = &kgdb_sci_sermap;
544 
545 void kgdb_register_sermap(struct kgdb_sermap *map)
546 {
547 	struct kgdb_sermap *last;
548 
549 	for (last = kgdb_serlist; last->next; last = last->next)
550 		;
551 	last->next = map;
552 	if (!map->namelen) {
553 		map->namelen = strlen(map->name);
554 	}
555 }
556 
557 static int __init kgdb_parse_options(char *options)
558 {
559 	char c;
560 	int baud;
561 
562 	/* Check for port spec (or use default) */
563 
564 	/* Determine port type and instance */
565 	if (!memcmp(options, "tty", 3)) {
566 		struct kgdb_sermap *map = kgdb_serlist;
567 
568 		while (map && memcmp(options, map->name, map->namelen))
569 			map = map->next;
570 
571 		if (!map) {
572 			KGDB_PRINTK("unknown port spec in %s\n", options);
573 			return -1;
574 		}
575 
576 		kgdb_porttype = map;
577 		kgdb_serial_setup = map->setup_fn;
578 		kgdb_portnum = options[map->namelen] - '0';
579 		options += map->namelen + 1;
580 
581 		options = (*options == ',') ? options+1 : options;
582 
583 		/* Read optional parameters (baud/parity/bits) */
584 		baud = simple_strtoul(options, &options, 10);
585 		if (baud != 0) {
586 			kgdb_baud = baud;
587 
588 			c = toupper(*options);
589 			if (c == 'E' || c == 'O' || c == 'N') {
590 				kgdb_parity = c;
591 				options++;
592 			}
593 
594 			c = *options;
595 			if (c == '7' || c == '8') {
596 				kgdb_bits = c;
597 				options++;
598 			}
599 			options = (*options == ',') ? options+1 : options;
600 		}
601 	}
602 
603 	/* Check for action specification */
604 	if (!memcmp(options, "halt", 4)) {
605 		kgdb_halt = 1;
606 		options += 4;
607 	} else if (!memcmp(options, "disabled", 8)) {
608 		kgdb_enabled = 0;
609 		options += 8;
610 	}
611 
612 	if (*options) {
613                 KGDB_PRINTK("ignored unknown options: %s\n", options);
614 		return 0;
615 	}
616 	return 1;
617 }
618 __setup("kgdb=", kgdb_parse_options);
619 #endif /* CONFIG_SH_KGDB */
620 
621