vserver 1.9.5.x5
[linux-2.6.git] / arch / sh / kernel / setup.c
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 <asm/uaccess.h>
24 #include <asm/io.h>
25 #include <asm/io_generic.h>
26 #include <asm/sections.h>
27 #include <asm/irq.h>
28 #include <asm/setup.h>
29
30 #ifdef CONFIG_SH_KGDB
31 #include <asm/kgdb.h>
32 static int kgdb_parse_options(char *options);
33 #endif
34 extern void * __rd_start, * __rd_end;
35 /*
36  * Machine setup..
37  */
38
39 /*
40  * Initialize loops_per_jiffy as 10000000 (1000MIPS).
41  * This value will be used at the very early stage of serial setup.
42  * The bigger value means no problem.
43  */
44 struct sh_cpuinfo boot_cpu_data = { CPU_SH_NONE, 0, 10000000, };
45 struct screen_info screen_info;
46 unsigned char aux_device_present = 0xaa;
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 #if defined(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_GENERIC
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 #ifdef CONFIG_SH_UNKNOWN
211         sh_mv = mv_unknown;
212 #endif
213
214         /*
215          * Manually walk the vec, fill in anything that the board hasn't yet
216          * by hand, wrapping to the generic implementation.
217          */
218 #define mv_set(elem) do { \
219         if (!sh_mv.mv_##elem) \
220                 sh_mv.mv_##elem = generic_##elem; \
221 } while (0)
222
223         mv_set(inb);    mv_set(inw);    mv_set(inl);
224         mv_set(outb);   mv_set(outw);   mv_set(outl);
225
226         mv_set(inb_p);  mv_set(inw_p);  mv_set(inl_p);
227         mv_set(outb_p); mv_set(outw_p); mv_set(outl_p);
228
229         mv_set(insb);   mv_set(insw);   mv_set(insl);
230         mv_set(outsb);  mv_set(outsw);  mv_set(outsl);
231
232         mv_set(readb);  mv_set(readw);  mv_set(readl);
233         mv_set(writeb); mv_set(writew); mv_set(writel);
234
235         mv_set(ioremap);
236         mv_set(iounmap);
237
238         mv_set(isa_port2addr);
239         mv_set(irq_demux);
240
241 #ifdef CONFIG_SH_UNKNOWN
242         __set_io_port_base(mv_io_base);
243 #endif
244
245         return 0;
246 }
247
248 void __init setup_arch(char **cmdline_p)
249 {
250         unsigned long bootmap_size;
251         unsigned long start_pfn, max_pfn, max_low_pfn;
252
253 #ifdef CONFIG_EARLY_PRINTK
254         extern void enable_early_printk(void);
255
256         enable_early_printk();
257 #endif
258 #ifdef CONFIG_CMDLINE_BOOL
259         strcpy(COMMAND_LINE, CONFIG_CMDLINE);
260 #endif
261
262         ROOT_DEV = old_decode_dev(ORIG_ROOT_DEV);
263
264 #ifdef CONFIG_BLK_DEV_RAM
265         rd_image_start = RAMDISK_FLAGS & RAMDISK_IMAGE_START_MASK;
266         rd_prompt = ((RAMDISK_FLAGS & RAMDISK_PROMPT_FLAG) != 0);
267         rd_doload = ((RAMDISK_FLAGS & RAMDISK_LOAD_FLAG) != 0);
268 #endif
269
270         if (!MOUNT_ROOT_RDONLY)
271                 root_mountflags &= ~MS_RDONLY;
272         init_mm.start_code = (unsigned long) _text;
273         init_mm.end_code = (unsigned long) _etext;
274         init_mm.end_data = (unsigned long) _edata;
275         init_mm.brk = (unsigned long) _end;
276
277         code_resource.start = virt_to_bus(_text);
278         code_resource.end = virt_to_bus(_etext)-1;
279         data_resource.start = virt_to_bus(_etext);
280         data_resource.end = virt_to_bus(_edata)-1;
281
282         sh_mv_setup(cmdline_p);
283
284 #define PFN_UP(x)       (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
285 #define PFN_DOWN(x)     ((x) >> PAGE_SHIFT)
286 #define PFN_PHYS(x)     ((x) << PAGE_SHIFT)
287
288 #ifdef CONFIG_DISCONTIGMEM
289         NODE_DATA(0)->bdata = &discontig_node_bdata[0];
290         NODE_DATA(1)->bdata = &discontig_node_bdata[1];
291
292         bootmap_size = init_bootmem_node(NODE_DATA(1), 
293                                          PFN_UP(__MEMORY_START_2ND),
294                                          PFN_UP(__MEMORY_START_2ND),
295                                          PFN_DOWN(__MEMORY_START_2ND+__MEMORY_SIZE_2ND));
296         free_bootmem_node(NODE_DATA(1), __MEMORY_START_2ND, __MEMORY_SIZE_2ND);
297         reserve_bootmem_node(NODE_DATA(1), __MEMORY_START_2ND, bootmap_size);
298 #endif
299
300         /*
301          * Find the highest page frame number we have available
302          */
303         max_pfn = PFN_DOWN(__pa(memory_end));
304
305         /*
306          * Determine low and high memory ranges:
307          */
308         max_low_pfn = max_pfn;
309
310         /*
311          * Partially used pages are not usable - thus
312          * we are rounding upwards:
313          */
314         start_pfn = PFN_UP(__pa(_end));
315
316         /*
317          * Find a proper area for the bootmem bitmap. After this
318          * bootstrap step all allocations (until the page allocator
319          * is intact) must be done via bootmem_alloc().
320          */
321         bootmap_size = init_bootmem_node(NODE_DATA(0), start_pfn,
322                                          __MEMORY_START>>PAGE_SHIFT,
323                                          max_low_pfn);
324         /*
325          * Register fully available low RAM pages with the bootmem allocator.
326          */
327         {
328                 unsigned long curr_pfn, last_pfn, pages;
329
330                 /*
331                  * We are rounding up the start address of usable memory:
332                  */
333                 curr_pfn = PFN_UP(__MEMORY_START);
334                 /*
335                  * ... and at the end of the usable range downwards:
336                  */
337                 last_pfn = PFN_DOWN(__pa(memory_end));
338
339                 if (last_pfn > max_low_pfn)
340                         last_pfn = max_low_pfn;
341
342                 pages = last_pfn - curr_pfn;
343                 free_bootmem_node(NODE_DATA(0), PFN_PHYS(curr_pfn),
344                                   PFN_PHYS(pages));
345         }
346
347         /*
348          * Reserve the kernel text and
349          * Reserve the bootmem bitmap. We do this in two steps (first step
350          * was init_bootmem()), because this catches the (definitely buggy)
351          * case of us accidentally initializing the bootmem allocator with
352          * an invalid RAM area.
353          */
354         reserve_bootmem_node(NODE_DATA(0), __MEMORY_START+PAGE_SIZE,
355                 (PFN_PHYS(start_pfn)+bootmap_size+PAGE_SIZE-1)-__MEMORY_START);
356
357         /*
358          * reserve physical page 0 - it's a special BIOS page on many boxes,
359          * enabling clean reboots, SMP operation, laptop functions.
360          */
361         reserve_bootmem_node(NODE_DATA(0), __MEMORY_START, PAGE_SIZE);
362
363 #ifdef CONFIG_BLK_DEV_INITRD
364         ROOT_DEV = MKDEV(RAMDISK_MAJOR, 0);
365         if (&__rd_start != &__rd_end) {
366                 LOADER_TYPE = 1;
367                 INITRD_START = PHYSADDR((unsigned long)&__rd_start) - __MEMORY_START;
368                 INITRD_SIZE = (unsigned long)&__rd_end - (unsigned long)&__rd_start;
369         }
370
371         if (LOADER_TYPE && INITRD_START) {
372                 if (INITRD_START + INITRD_SIZE <= (max_low_pfn << PAGE_SHIFT)) {
373                         reserve_bootmem_node(NODE_DATA(0), INITRD_START+__MEMORY_START, INITRD_SIZE);
374                         initrd_start =
375                                 INITRD_START ? INITRD_START + PAGE_OFFSET + __MEMORY_START : 0;
376                         initrd_end = initrd_start + INITRD_SIZE;
377                 } else {
378                         printk("initrd extends beyond end of memory "
379                             "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
380                                     INITRD_START + INITRD_SIZE,
381                                     max_low_pfn << PAGE_SHIFT);
382                         initrd_start = 0;
383                 }
384         }
385 #endif
386
387 #ifdef CONFIG_DUMMY_CONSOLE
388         conswitchp = &dummy_con;
389 #endif
390
391         /* Perform the machine specific initialisation */
392         platform_setup();
393
394         paging_init();
395 }
396
397 struct sh_machine_vector* __init get_mv_byname(const char* name)
398 {
399         extern int strcasecmp(const char *, const char *);
400         extern long __machvec_start, __machvec_end;
401         struct sh_machine_vector *all_vecs =
402                 (struct sh_machine_vector *)&__machvec_start;
403
404         int i, n = ((unsigned long)&__machvec_end
405                     - (unsigned long)&__machvec_start)/
406                 sizeof(struct sh_machine_vector);
407
408         for (i = 0; i < n; ++i) {
409                 struct sh_machine_vector *mv = &all_vecs[i];
410                 if (mv == NULL)
411                         continue;
412                 if (strcasecmp(name, get_system_type()) == 0) {
413                         return mv;
414                 }
415         }
416         return NULL;
417 }
418
419 static struct cpu cpu[NR_CPUS];
420
421 static int __init topology_init(void)
422 {
423         int cpu_id;
424
425         for (cpu_id = 0; cpu_id < NR_CPUS; cpu_id++)
426                 if (cpu_possible(cpu_id))
427                         register_cpu(&cpu[cpu_id], cpu_id, NULL);
428
429         return 0;
430 }
431
432 subsys_initcall(topology_init);
433
434 static const char *cpu_name[] = {
435         [CPU_SH7604]    = "SH7604",
436         [CPU_SH7705]    = "SH7705",
437         [CPU_SH7708]    = "SH7708",
438         [CPU_SH7729]    = "SH7729",
439         [CPU_SH7300]    = "SH7300",
440         [CPU_SH7750]    = "SH7750",
441         [CPU_SH7750S]   = "SH7750S",
442         [CPU_SH7750R]   = "SH7750R",
443         [CPU_SH7751]    = "SH7751",
444         [CPU_SH7751R]   = "SH7751R",
445         [CPU_SH7760]    = "SH7760",
446         [CPU_SH73180]   = "SH73180",
447         [CPU_ST40RA]    = "ST40RA",
448         [CPU_ST40GX1]   = "ST40GX1",
449         [CPU_SH4_202]   = "SH4-202",
450         [CPU_SH4_501]   = "SH4-501",
451         [CPU_SH_NONE]   = "Unknown"
452 };
453
454 const char *get_cpu_subtype(void)
455 {
456         return cpu_name[boot_cpu_data.type];
457 }
458
459 #ifdef CONFIG_PROC_FS
460 static const char *cpu_flags[] = {
461         "none", "fpu", "p2flush", "mmuassoc", "dsp", "perfctr",
462 };
463
464 static void show_cpuflags(struct seq_file *m)
465 {
466         unsigned long i;
467
468         seq_printf(m, "cpu flags\t:");
469
470         if (!cpu_data->flags) {
471                 seq_printf(m, " %s\n", cpu_flags[0]);
472                 return;
473         }
474
475         for (i = 0; i < cpu_data->flags; i++)
476                 if ((cpu_data->flags & (1 << i)))
477                         seq_printf(m, " %s", cpu_flags[i+1]);
478
479         seq_printf(m, "\n");
480 }
481
482 static void show_cacheinfo(struct seq_file *m, const char *type, struct cache_info info)
483 {
484         unsigned int cache_size;
485
486         cache_size = info.ways * info.sets * info.linesz;
487
488         seq_printf(m, "%s size\t: %dKiB\n", type, cache_size >> 10);
489 }
490
491 /*
492  *      Get CPU information for use by the procfs.
493  */
494 static int show_cpuinfo(struct seq_file *m, void *v)
495 {
496         unsigned int cpu = smp_processor_id();
497
498         if (!cpu && cpu_online(cpu))
499                 seq_printf(m, "machine\t\t: %s\n", get_system_type());
500
501         seq_printf(m, "processor\t: %d\n", cpu);
502         seq_printf(m, "cpu family\t: %s\n", system_utsname.machine);
503         seq_printf(m, "cpu type\t: %s\n", get_cpu_subtype());
504
505         show_cpuflags(m);
506
507         seq_printf(m, "cache type\t: ");
508
509         /*
510          * Check for what type of cache we have, we support both the
511          * unified cache on the SH-2 and SH-3, as well as the harvard
512          * style cache on the SH-4.
513          */
514         if (test_bit(SH_CACHE_COMBINED, &(boot_cpu_data.icache.flags))) {
515                 seq_printf(m, "unified\n");
516                 show_cacheinfo(m, "cache", boot_cpu_data.icache);
517         } else {
518                 seq_printf(m, "split (harvard)\n");
519                 show_cacheinfo(m, "icache", boot_cpu_data.icache);
520                 show_cacheinfo(m, "dcache", boot_cpu_data.dcache);
521         }
522
523         seq_printf(m, "bogomips\t: %lu.%02lu\n",
524                      boot_cpu_data.loops_per_jiffy/(500000/HZ),
525                      (boot_cpu_data.loops_per_jiffy/(5000/HZ)) % 100);
526
527 #define PRINT_CLOCK(name, value) \
528         seq_printf(m, name " clock\t: %d.%02dMHz\n", \
529                      ((value) / 1000000), ((value) % 1000000)/10000)
530         
531         PRINT_CLOCK("cpu", boot_cpu_data.cpu_clock);
532         PRINT_CLOCK("bus", boot_cpu_data.bus_clock);
533 #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
534         PRINT_CLOCK("memory", boot_cpu_data.memory_clock);
535 #endif
536         PRINT_CLOCK("module", boot_cpu_data.module_clock);
537
538         return 0;
539 }
540
541
542 static void *c_start(struct seq_file *m, loff_t *pos)
543 {
544         return *pos < NR_CPUS ? cpu_data + *pos : NULL;
545 }
546 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
547 {
548         ++*pos;
549         return c_start(m, pos);
550 }
551 static void c_stop(struct seq_file *m, void *v)
552 {
553 }
554 struct seq_operations cpuinfo_op = {
555         .start  = c_start,
556         .next   = c_next,
557         .stop   = c_stop,
558         .show   = show_cpuinfo,
559 };
560 #endif /* CONFIG_PROC_FS */
561
562 #ifdef CONFIG_SH_KGDB
563 /*
564  * Parse command-line kgdb options.  By default KGDB is enabled,
565  * entered on error (or other action) using default serial info.
566  * The command-line option can include a serial port specification
567  * and an action to override default or configured behavior.
568  */
569 struct kgdb_sermap kgdb_sci_sermap =
570 { "ttySC", 5, kgdb_sci_setup, NULL };
571
572 struct kgdb_sermap *kgdb_serlist = &kgdb_sci_sermap;
573 struct kgdb_sermap *kgdb_porttype = &kgdb_sci_sermap;
574
575 void kgdb_register_sermap(struct kgdb_sermap *map)
576 {
577         struct kgdb_sermap *last;
578
579         for (last = kgdb_serlist; last->next; last = last->next)
580                 ;
581         last->next = map;
582         if (!map->namelen) {
583                 map->namelen = strlen(map->name);
584         }
585 }
586
587 static int __init kgdb_parse_options(char *options)
588 {
589         char c;
590         int baud;
591
592         /* Check for port spec (or use default) */
593
594         /* Determine port type and instance */
595         if (!memcmp(options, "tty", 3)) {
596                 struct kgdb_sermap *map = kgdb_serlist;
597
598                 while (map && memcmp(options, map->name, map->namelen))
599                         map = map->next;
600
601                 if (!map) {
602                         KGDB_PRINTK("unknown port spec in %s\n", options);
603                         return -1;
604                 }
605
606                 kgdb_porttype = map;
607                 kgdb_serial_setup = map->setup_fn;
608                 kgdb_portnum = options[map->namelen] - '0';
609                 options += map->namelen + 1;
610
611                 options = (*options == ',') ? options+1 : options;
612                 
613                 /* Read optional parameters (baud/parity/bits) */
614                 baud = simple_strtoul(options, &options, 10);
615                 if (baud != 0) {
616                         kgdb_baud = baud;
617
618                         c = toupper(*options);
619                         if (c == 'E' || c == 'O' || c == 'N') {
620                                 kgdb_parity = c;
621                                 options++;
622                         }
623
624                         c = *options;
625                         if (c == '7' || c == '8') {
626                                 kgdb_bits = c;
627                                 options++;
628                         }
629                         options = (*options == ',') ? options+1 : options;
630                 }
631         }
632
633         /* Check for action specification */
634         if (!memcmp(options, "halt", 4)) {
635                 kgdb_halt = 1;
636                 options += 4;
637         } else if (!memcmp(options, "disabled", 8)) {
638                 kgdb_enabled = 0;
639                 options += 8;
640         }
641
642         if (*options) {
643                 KGDB_PRINTK("ignored unknown options: %s\n", options);
644                 return 0;
645         }
646         return 1;
647 }
648 __setup("kgdb=", kgdb_parse_options);
649 #endif /* CONFIG_SH_KGDB */
650