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