3787957abe1749b310a51398161c088e916f7877
[linux-2.6.git] / arch / ppc64 / kernel / pSeries_setup.c
1 /*
2  *  linux/arch/ppc/kernel/setup.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Adapted from 'alpha' version by Gary Thomas
6  *  Modified by Cort Dougan (cort@cs.nmt.edu)
7  *  Modified by PPC64 Team, IBM Corp
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version
12  * 2 of the License, or (at your option) any later version.
13  */
14
15 /*
16  * bootup setup stuff..
17  */
18
19 #undef DEBUG
20
21 #include <linux/config.h>
22 #include <linux/errno.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/slab.h>
29 #include <linux/user.h>
30 #include <linux/a.out.h>
31 #include <linux/tty.h>
32 #include <linux/major.h>
33 #include <linux/interrupt.h>
34 #include <linux/reboot.h>
35 #include <linux/init.h>
36 #include <linux/ioport.h>
37 #include <linux/console.h>
38 #include <linux/pci.h>
39 #include <linux/version.h>
40 #include <linux/adb.h>
41 #include <linux/module.h>
42 #include <linux/delay.h>
43
44 #include <linux/irq.h>
45 #include <linux/seq_file.h>
46 #include <linux/root_dev.h>
47
48 #include <asm/mmu.h>
49 #include <asm/processor.h>
50 #include <asm/io.h>
51 #include <asm/pgtable.h>
52 #include <asm/prom.h>
53 #include <asm/rtas.h>
54 #include <asm/pci-bridge.h>
55 #include <asm/iommu.h>
56 #include <asm/dma.h>
57 #include <asm/machdep.h>
58 #include <asm/irq.h>
59 #include <asm/naca.h>
60 #include <asm/time.h>
61 #include <asm/nvram.h>
62
63 #include "i8259.h"
64 #include <asm/xics.h>
65 #include <asm/ppcdebug.h>
66 #include <asm/cputable.h>
67
68 #include "mpic.h"
69
70 #ifdef DEBUG
71 #define DBG(fmt...) udbg_printf(fmt)
72 #else
73 #define DBG(fmt...)
74 #endif
75
76 extern void find_and_init_phbs(void);
77 extern void pSeries_final_fixup(void);
78
79 extern void pSeries_get_boot_time(struct rtc_time *rtc_time);
80 extern void pSeries_get_rtc_time(struct rtc_time *rtc_time);
81 extern int  pSeries_set_rtc_time(struct rtc_time *rtc_time);
82 extern void find_udbg_vterm(void);
83 extern void SystemReset_FWNMI(void), MachineCheck_FWNMI(void);  /* from head.S */
84 extern void generic_find_legacy_serial_ports(unsigned int *default_speed);
85
86 int fwnmi_active;  /* TRUE if an FWNMI handler is present */
87
88 unsigned long  virtPython0Facilities = 0;  // python0 facility area (memory mapped io) (64-bit format) VIRTUAL address.
89
90 extern unsigned long loops_per_jiffy;
91
92 extern unsigned long ppc_proc_freq;
93 extern unsigned long ppc_tb_freq;
94
95 static volatile void __iomem * chrp_int_ack_special;
96 struct mpic *pSeries_mpic;
97
98 void pSeries_get_cpuinfo(struct seq_file *m)
99 {
100         struct device_node *root;
101         const char *model = "";
102
103         root = of_find_node_by_path("/");
104         if (root)
105                 model = get_property(root, "model", NULL);
106         seq_printf(m, "machine\t\t: CHRP %s\n", model);
107         of_node_put(root);
108 }
109
110 /* Initialize firmware assisted non-maskable interrupts if
111  * the firmware supports this feature.
112  *
113  */
114 static void __init fwnmi_init(void)
115 {
116         int ret;
117         int ibm_nmi_register = rtas_token("ibm,nmi-register");
118         if (ibm_nmi_register == RTAS_UNKNOWN_SERVICE)
119                 return;
120         ret = rtas_call(ibm_nmi_register, 2, 1, NULL,
121                         __pa((unsigned long)SystemReset_FWNMI),
122                         __pa((unsigned long)MachineCheck_FWNMI));
123         if (ret == 0)
124                 fwnmi_active = 1;
125 }
126
127 static int pSeries_irq_cascade(struct pt_regs *regs, void *data)
128 {
129         if (chrp_int_ack_special)
130                 return readb(chrp_int_ack_special);
131         else
132                 return i8259_irq(smp_processor_id());
133 }
134
135 static void __init pSeries_init_mpic(void)
136 {
137         unsigned int *addrp;
138         struct device_node *np;
139         int i;
140
141         /* All ISUs are setup, complete initialization */
142         mpic_init(pSeries_mpic);
143
144         /* Check what kind of cascade ACK we have */
145         if (!(np = of_find_node_by_name(NULL, "pci"))
146             || !(addrp = (unsigned int *)
147                  get_property(np, "8259-interrupt-acknowledge", NULL)))
148                 printk(KERN_ERR "Cannot find pci to get ack address\n");
149         else
150                 chrp_int_ack_special = ioremap(addrp[prom_n_addr_cells(np)-1], 1);
151         of_node_put(np);
152
153         /* Setup the legacy interrupts & controller */
154         for (i = 0; i < NUM_ISA_INTERRUPTS; i++)
155                 irq_desc[i].handler = &i8259_pic;
156         i8259_init(0);
157
158         /* Hook cascade to mpic */
159         mpic_setup_cascade(NUM_ISA_INTERRUPTS, pSeries_irq_cascade, NULL);
160 }
161
162 static void __init pSeries_setup_mpic(void)
163 {
164         unsigned int *opprop;
165         unsigned long openpic_addr = 0;
166         unsigned char senses[NR_IRQS - NUM_ISA_INTERRUPTS];
167         struct device_node *root;
168         int irq_count;
169
170         /* Find the Open PIC if present */
171         root = of_find_node_by_path("/");
172         opprop = (unsigned int *) get_property(root, "platform-open-pic", NULL);
173         if (opprop != 0) {
174                 int n = prom_n_addr_cells(root);
175
176                 for (openpic_addr = 0; n > 0; --n)
177                         openpic_addr = (openpic_addr << 32) + *opprop++;
178                 printk(KERN_DEBUG "OpenPIC addr: %lx\n", openpic_addr);
179         }
180         of_node_put(root);
181
182         BUG_ON(openpic_addr == 0);
183
184         /* Get the sense values from OF */
185         prom_get_irq_senses(senses, NUM_ISA_INTERRUPTS, NR_IRQS);
186         
187         /* Setup the openpic driver */
188         irq_count = NR_IRQS - NUM_ISA_INTERRUPTS - 4; /* leave room for IPIs */
189         pSeries_mpic = mpic_alloc(openpic_addr, MPIC_PRIMARY,
190                                   16, 16, irq_count, /* isu size, irq offset, irq count */ 
191                                   NR_IRQS - 4, /* ipi offset */
192                                   senses, irq_count, /* sense & sense size */
193                                   " MPIC     ");
194 }
195
196 static void __init pSeries_setup_arch(void)
197 {
198         /* Fixup ppc_md depending on the type of interrupt controller */
199         if (naca->interrupt_controller == IC_OPEN_PIC) {
200                 ppc_md.init_IRQ       = pSeries_init_mpic; 
201                 ppc_md.get_irq        = mpic_get_irq;
202                 /* Allocate the mpic now, so that find_and_init_phbs() can
203                  * fill the ISUs */
204                 pSeries_setup_mpic();
205         } else {
206                 ppc_md.init_IRQ       = xics_init_IRQ;
207                 ppc_md.get_irq        = xics_get_irq;
208         }
209
210 #ifdef CONFIG_SMP
211         smp_init_pSeries();
212 #endif
213         /* openpic global configuration register (64-bit format). */
214         /* openpic Interrupt Source Unit pointer (64-bit format). */
215         /* python0 facility area (mmio) (64-bit format) REAL address. */
216
217         /* init to some ~sane value until calibrate_delay() runs */
218         loops_per_jiffy = 50000000;
219
220         if (ROOT_DEV == 0) {
221                 printk("No ramdisk, default root is /dev/sda2\n");
222                 ROOT_DEV = Root_SDA2;
223         }
224
225         fwnmi_init();
226
227         /* Find and initialize PCI host bridges */
228         /* iSeries needs to be done much later. */
229         eeh_init();
230         find_and_init_phbs();
231
232 #ifdef CONFIG_DUMMY_CONSOLE
233         conswitchp = &dummy_con;
234 #endif
235
236         pSeries_nvram_init();
237
238         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
239                 vpa_init(boot_cpuid);
240 }
241
242 static int __init pSeries_init_panel(void)
243 {
244         /* Manually leave the kernel version on the panel. */
245         ppc_md.progress("Linux ppc64\n", 0);
246         ppc_md.progress(UTS_RELEASE, 0);
247
248         return 0;
249 }
250 arch_initcall(pSeries_init_panel);
251
252
253 /* Build up the firmware_features bitmask field
254  * using contents of device-tree/ibm,hypertas-functions.
255  * Ultimately this functionality may be moved into prom.c prom_init().
256  */
257 void __init fw_feature_init(void)
258 {
259         struct device_node * dn;
260         char * hypertas;
261         unsigned int len;
262
263         DBG(" -> fw_feature_init()\n");
264
265         cur_cpu_spec->firmware_features = 0;
266         dn = of_find_node_by_path("/rtas");
267         if (dn == NULL) {
268                 printk(KERN_ERR "WARNING ! Cannot find RTAS in device-tree !\n");
269                 goto no_rtas;
270         }
271
272         hypertas = get_property(dn, "ibm,hypertas-functions", &len);
273         if (hypertas) {
274                 while (len > 0){
275                         int i, hypertas_len;
276                         /* check value against table of strings */
277                         for(i=0; i < FIRMWARE_MAX_FEATURES ;i++) {
278                                 if ((firmware_features_table[i].name) &&
279                                     (strcmp(firmware_features_table[i].name,hypertas))==0) {
280                                         /* we have a match */
281                                         cur_cpu_spec->firmware_features |= 
282                                                 (firmware_features_table[i].val);
283                                         break;
284                                 } 
285                         }
286                         hypertas_len = strlen(hypertas);
287                         len -= hypertas_len +1;
288                         hypertas+= hypertas_len +1;
289                 }
290         }
291
292         of_node_put(dn);
293  no_rtas:
294         printk(KERN_INFO "firmware_features = 0x%lx\n", 
295                cur_cpu_spec->firmware_features);
296
297         DBG(" <- fw_feature_init()\n");
298 }
299
300
301 static  void __init pSeries_discover_pic(void)
302 {
303         struct device_node *np;
304         char *typep;
305
306         /*
307          * Setup interrupt mapping options that are needed for finish_device_tree
308          * to properly parse the OF interrupt tree & do the virtual irq mapping
309          */
310         __irq_offset_value = NUM_ISA_INTERRUPTS;
311         naca->interrupt_controller = IC_INVALID;
312         for (np = NULL; (np = of_find_node_by_name(np, "interrupt-controller"));) {
313                 typep = (char *)get_property(np, "compatible", NULL);
314                 if (strstr(typep, "open-pic"))
315                         naca->interrupt_controller = IC_OPEN_PIC;
316                 else if (strstr(typep, "ppc-xicp"))
317                         naca->interrupt_controller = IC_PPC_XIC;
318                 else
319                         printk("initialize_naca: failed to recognize"
320                                " interrupt-controller\n");
321                 break;
322         }
323 }
324
325 static void pSeries_cpu_die(void)
326 {
327         local_irq_disable();
328         /* Some hardware requires clearing the CPPR, while other hardware does not
329          * it is safe either way
330          */
331         pSeriesLP_cppr_info(0, 0);
332         rtas_stop_self();
333         /* Should never get here... */
334         BUG();
335         for(;;);
336 }
337
338
339 /*
340  * Early initialization.  Relocation is on but do not reference unbolted pages
341  */
342 static void __init pSeries_init_early(void)
343 {
344         void *comport;
345         int iommu_off = 0;
346         unsigned int default_speed;
347
348         DBG(" -> pSeries_init_early()\n");
349
350         fw_feature_init();
351         
352         if (systemcfg->platform & PLATFORM_LPAR)
353                 hpte_init_lpar();
354         else {
355                 hpte_init_native();
356                 iommu_off = (of_chosen &&
357                              get_property(of_chosen, "linux,iommu-off", NULL));
358         }
359
360         generic_find_legacy_serial_ports(&default_speed);
361
362         if (systemcfg->platform & PLATFORM_LPAR)
363                 find_udbg_vterm();
364         else if (naca->serialPortAddr) {
365                 /* Map the uart for udbg. */
366                 comport = (void *)__ioremap(naca->serialPortAddr, 16, _PAGE_NO_CACHE);
367                 udbg_init_uart(comport, default_speed);
368
369                 ppc_md.udbg_putc = udbg_putc;
370                 ppc_md.udbg_getc = udbg_getc;
371                 ppc_md.udbg_getc_poll = udbg_getc_poll;
372                 DBG("Hello World !\n");
373         }
374
375
376         if (iommu_off)
377                 pci_dma_init_direct();
378         else
379                 tce_init_pSeries();
380
381         pSeries_discover_pic();
382
383         DBG(" <- pSeries_init_early()\n");
384 }
385
386
387 static void pSeries_progress(char *s, unsigned short hex)
388 {
389         struct device_node *root;
390         int width, *p;
391         char *os;
392         static int display_character, set_indicator;
393         static int max_width;
394         static spinlock_t progress_lock = SPIN_LOCK_UNLOCKED;
395         static int pending_newline = 0;  /* did last write end with unprinted newline? */
396
397         if (!rtas.base)
398                 return;
399
400         if (max_width == 0) {
401                 if ((root = find_path_device("/rtas")) &&
402                      (p = (unsigned int *)get_property(root,
403                                                        "ibm,display-line-length",
404                                                        NULL)))
405                         max_width = *p;
406                 else
407                         max_width = 0x10;
408                 display_character = rtas_token("display-character");
409                 set_indicator = rtas_token("set-indicator");
410         }
411
412         if (display_character == RTAS_UNKNOWN_SERVICE) {
413                 /* use hex display if available */
414                 if (set_indicator != RTAS_UNKNOWN_SERVICE)
415                         rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
416                 return;
417         }
418
419         spin_lock(&progress_lock);
420
421         /*
422          * Last write ended with newline, but we didn't print it since
423          * it would just clear the bottom line of output. Print it now
424          * instead.
425          *
426          * If no newline is pending, print a CR to start output at the
427          * beginning of the line.
428          */
429         if (pending_newline) {
430                 rtas_call(display_character, 1, 1, NULL, '\r');
431                 rtas_call(display_character, 1, 1, NULL, '\n');
432                 pending_newline = 0;
433         } else {
434                 rtas_call(display_character, 1, 1, NULL, '\r');
435         }
436  
437         width = max_width;
438         os = s;
439         while (*os) {
440                 if (*os == '\n' || *os == '\r') {
441                         /* Blank to end of line. */
442                         while (width-- > 0)
443                                 rtas_call(display_character, 1, 1, NULL, ' ');
444  
445                         /* If newline is the last character, save it
446                          * until next call to avoid bumping up the
447                          * display output.
448                          */
449                         if (*os == '\n' && !os[1]) {
450                                 pending_newline = 1;
451                                 spin_unlock(&progress_lock);
452                                 return;
453                         }
454  
455                         /* RTAS wants CR-LF, not just LF */
456  
457                         if (*os == '\n') {
458                                 rtas_call(display_character, 1, 1, NULL, '\r');
459                                 rtas_call(display_character, 1, 1, NULL, '\n');
460                         } else {
461                                 /* CR might be used to re-draw a line, so we'll
462                                  * leave it alone and not add LF.
463                                  */
464                                 rtas_call(display_character, 1, 1, NULL, *os);
465                         }
466  
467                         width = max_width;
468                 } else {
469                         width--;
470                         rtas_call(display_character, 1, 1, NULL, *os);
471                 }
472  
473                 os++;
474  
475                 /* if we overwrite the screen length */
476                 if (width <= 0)
477                         while ((*os != 0) && (*os != '\n') && (*os != '\r'))
478                                 os++;
479         }
480  
481         /* Blank to end of line. */
482         while (width-- > 0)
483                 rtas_call(display_character, 1, 1, NULL, ' ');
484
485         spin_unlock(&progress_lock);
486 }
487
488 extern void setup_default_decr(void);
489
490 /* Some sane defaults: 125 MHz timebase, 1GHz processor */
491 #define DEFAULT_TB_FREQ         125000000UL
492 #define DEFAULT_PROC_FREQ       (DEFAULT_TB_FREQ * 8)
493
494 static void __init pSeries_calibrate_decr(void)
495 {
496         struct device_node *cpu;
497         struct div_result divres;
498         unsigned int *fp;
499         int node_found;
500
501         /*
502          * The cpu node should have a timebase-frequency property
503          * to tell us the rate at which the decrementer counts.
504          */
505         cpu = of_find_node_by_type(NULL, "cpu");
506
507         ppc_tb_freq = DEFAULT_TB_FREQ;          /* hardcoded default */
508         node_found = 0;
509         if (cpu != 0) {
510                 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
511                                                   NULL);
512                 if (fp != 0) {
513                         node_found = 1;
514                         ppc_tb_freq = *fp;
515                 }
516         }
517         if (!node_found)
518                 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
519                                 "(not found)\n");
520
521         ppc_proc_freq = DEFAULT_PROC_FREQ;
522         node_found = 0;
523         if (cpu != 0) {
524                 fp = (unsigned int *)get_property(cpu, "clock-frequency",
525                                                   NULL);
526                 if (fp != 0) {
527                         node_found = 1;
528                         ppc_proc_freq = *fp;
529                 }
530         }
531         if (!node_found)
532                 printk(KERN_ERR "WARNING: Estimating processor frequency "
533                                 "(not found)\n");
534
535         of_node_put(cpu);
536
537         printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
538                ppc_tb_freq/1000000, ppc_tb_freq%1000000);
539         printk(KERN_INFO "time_init: processor frequency   = %lu.%.6lu MHz\n",
540                ppc_proc_freq/1000000, ppc_proc_freq%1000000);
541
542         tb_ticks_per_jiffy = ppc_tb_freq / HZ;
543         tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
544         tb_ticks_per_usec = ppc_tb_freq / 1000000;
545         tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
546         div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
547         tb_to_xs = divres.result_low;
548
549         setup_default_decr();
550 }
551
552 static int pSeries_check_legacy_ioport(unsigned int baseport)
553 {
554         struct device_node *np;
555
556 #define I8042_DATA_REG  0x60
557 #define FDC_BASE        0x3f0
558
559
560         switch(baseport) {
561         case I8042_DATA_REG:
562                 np = of_find_node_by_type(NULL, "8042");
563                 if (np == NULL)
564                         return -ENODEV;
565                 of_node_put(np);
566                 break;
567         case FDC_BASE:
568                 np = of_find_node_by_type(NULL, "fdc");
569                 if (np == NULL)
570                         return -ENODEV;
571                 of_node_put(np);
572                 break;
573         }
574         return 0;
575 }
576
577 /*
578  * Called very early, MMU is off, device-tree isn't unflattened
579  */
580 extern struct machdep_calls pSeries_md;
581
582 static int __init pSeries_probe(int platform)
583 {
584         if (platform != PLATFORM_PSERIES &&
585             platform != PLATFORM_PSERIES_LPAR)
586                 return 0;
587
588         /* if we have some ppc_md fixups for LPAR to do, do
589          * it here ...
590          */
591
592         return 1;
593 }
594
595 struct machdep_calls __initdata pSeries_md = {
596         .probe                  = pSeries_probe,
597         .setup_arch             = pSeries_setup_arch,
598         .init_early             = pSeries_init_early,
599         .get_cpuinfo            = pSeries_get_cpuinfo,
600         .log_error              = pSeries_log_error,
601         .pcibios_fixup          = pSeries_final_fixup,
602         .restart                = rtas_restart,
603         .power_off              = rtas_power_off,
604         .halt                   = rtas_halt,
605         .panic                  = rtas_os_term,
606         .cpu_die                = pSeries_cpu_die,
607         .get_boot_time          = pSeries_get_boot_time,
608         .get_rtc_time           = pSeries_get_rtc_time,
609         .set_rtc_time           = pSeries_set_rtc_time,
610         .calibrate_decr         = pSeries_calibrate_decr,
611         .progress               = pSeries_progress,
612         .check_legacy_ioport    = pSeries_check_legacy_ioport,
613 };