ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / chrp_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 #include <linux/config.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/stddef.h>
25 #include <linux/unistd.h>
26 #include <linux/slab.h>
27 #include <linux/user.h>
28 #include <linux/a.out.h>
29 #include <linux/tty.h>
30 #include <linux/major.h>
31 #include <linux/interrupt.h>
32 #include <linux/reboot.h>
33 #include <linux/init.h>
34 #include <linux/ioport.h>
35 #include <linux/console.h>
36 #include <linux/pci.h>
37 #include <linux/version.h>
38 #include <linux/adb.h>
39 #include <linux/module.h>
40 #include <linux/delay.h>
41
42 #include <linux/irq.h>
43 #include <linux/seq_file.h>
44 #include <linux/root_dev.h>
45
46 #include <asm/mmu.h>
47 #include <asm/processor.h>
48 #include <asm/io.h>
49 #include <asm/pgtable.h>
50 #include <asm/prom.h>
51 #include <asm/rtas.h>
52 #include <asm/pci-bridge.h>
53 #include <asm/iommu.h>
54 #include <asm/dma.h>
55 #include <asm/machdep.h>
56 #include <asm/irq.h>
57 #include <asm/naca.h>
58 #include <asm/time.h>
59 #include <asm/nvram.h>
60
61 #include "i8259.h"
62 #include "open_pic.h"
63 #include <asm/xics.h>
64 #include <asm/ppcdebug.h>
65 #include <asm/cputable.h>
66
67 void chrp_progress(char *, unsigned short);
68
69 extern void pSeries_init_openpic(void);
70
71 extern void find_and_init_phbs(void);
72 extern void pSeries_final_fixup(void);
73
74 extern void pSeries_get_boot_time(struct rtc_time *rtc_time);
75 extern void pSeries_get_rtc_time(struct rtc_time *rtc_time);
76 extern int  pSeries_set_rtc_time(struct rtc_time *rtc_time);
77 void pSeries_calibrate_decr(void);
78 void fwnmi_init(void);
79 extern void SystemReset_FWNMI(void), MachineCheck_FWNMI(void);  /* from head.S */
80 int fwnmi_active;  /* TRUE if an FWNMI handler is present */
81
82 dev_t boot_dev;
83 unsigned long  virtPython0Facilities = 0;  // python0 facility area (memory mapped io) (64-bit format) VIRTUAL address.
84
85 extern unsigned long loops_per_jiffy;
86
87 extern unsigned long ppc_proc_freq;
88 extern unsigned long ppc_tb_freq;
89
90 void chrp_get_cpuinfo(struct seq_file *m)
91 {
92         struct device_node *root;
93         const char *model = "";
94
95         root = of_find_node_by_path("/");
96         if (root)
97                 model = get_property(root, "model", NULL);
98         seq_printf(m, "machine\t\t: CHRP %s\n", model);
99         of_node_put(root);
100 }
101
102 #define I8042_DATA_REG 0x60
103
104 void __init chrp_request_regions(void)
105 {
106         struct device_node *i8042;
107
108         request_region(0x20,0x20,"pic1");
109         request_region(0xa0,0x20,"pic2");
110         request_region(0x00,0x20,"dma1");
111         request_region(0x40,0x20,"timer");
112         request_region(0x80,0x10,"dma page reg");
113         request_region(0xc0,0x20,"dma2");
114
115         /*
116          * Some machines have an unterminated i8042 so check the device
117          * tree and reserve the region if it does not appear. Later on
118          * the i8042 code will try and reserve this region and fail.
119          */
120         if (!(i8042 = of_find_node_by_type(NULL, "8042")))
121                 request_region(I8042_DATA_REG, 16, "reserved (no i8042)");
122         of_node_put(i8042);
123 }
124
125 void __init
126 chrp_setup_arch(void)
127 {
128         struct device_node *root;
129         unsigned int *opprop;
130         
131         /* openpic global configuration register (64-bit format). */
132         /* openpic Interrupt Source Unit pointer (64-bit format). */
133         /* python0 facility area (mmio) (64-bit format) REAL address. */
134
135         /* init to some ~sane value until calibrate_delay() runs */
136         loops_per_jiffy = 50000000;
137
138         if (ROOT_DEV == 0) {
139                 printk("No ramdisk, default root is /dev/sda2\n");
140                 ROOT_DEV = Root_SDA2;
141         }
142
143         printk("Boot arguments: %s\n", cmd_line);
144
145         fwnmi_init();
146
147 #ifndef CONFIG_PPC_ISERIES
148         /* Find and initialize PCI host bridges */
149         /* iSeries needs to be done much later. */
150         eeh_init();
151         find_and_init_phbs();
152 #endif
153
154         /* Find the Open PIC if present */
155         root = of_find_node_by_path("/");
156         opprop = (unsigned int *) get_property(root,
157                                 "platform-open-pic", NULL);
158         if (opprop != 0) {
159                 int n = prom_n_addr_cells(root);
160                 unsigned long openpic;
161
162                 for (openpic = 0; n > 0; --n)
163                         openpic = (openpic << 32) + *opprop++;
164                 printk(KERN_DEBUG "OpenPIC addr: %lx\n", openpic);
165                 OpenPIC_Addr = __ioremap(openpic, 0x40000, _PAGE_NO_CACHE);
166         }
167         of_node_put(root);
168
169 #ifdef CONFIG_DUMMY_CONSOLE
170         conswitchp = &dummy_con;
171 #endif
172
173 #ifdef CONFIG_PPC_PSERIES
174         pSeries_nvram_init();
175 #endif
176 }
177
178 void __init
179 chrp_init2(void)
180 {
181         /* Manually leave the kernel version on the panel. */
182         ppc_md.progress("Linux ppc64\n", 0);
183         ppc_md.progress(UTS_RELEASE, 0);
184 }
185
186 /* Initialize firmware assisted non-maskable interrupts if
187  * the firmware supports this feature.
188  *
189  */
190 void __init fwnmi_init(void)
191 {
192         long ret;
193         int ibm_nmi_register = rtas_token("ibm,nmi-register");
194         if (ibm_nmi_register == RTAS_UNKNOWN_SERVICE)
195                 return;
196         ret = rtas_call(ibm_nmi_register, 2, 1, NULL,
197                         __pa((unsigned long)SystemReset_FWNMI),
198                         __pa((unsigned long)MachineCheck_FWNMI));
199         if (ret == 0)
200                 fwnmi_active = 1;
201 }
202
203 /* Early initialization.  Relocation is on but do not reference unbolted pages */
204 void __init pSeries_init_early(void)
205 {
206         void *comport;
207
208         hpte_init_pSeries();
209
210         if (ppc64_iommu_off)
211                 pci_dma_init_direct();
212         else
213                 tce_init_pSeries();
214
215 #ifdef CONFIG_SMP
216         smp_init_pSeries();
217 #endif
218
219         /* Map the uart for udbg. */
220         comport = (void *)__ioremap(naca->serialPortAddr, 16, _PAGE_NO_CACHE);
221         udbg_init_uart(comport);
222
223         ppc_md.udbg_putc = udbg_putc;
224         ppc_md.udbg_getc = udbg_getc;
225         ppc_md.udbg_getc_poll = udbg_getc_poll;
226 }
227
228 void __init
229 chrp_init(unsigned long r3, unsigned long r4, unsigned long r5,
230           unsigned long r6, unsigned long r7)
231 {
232         struct device_node * dn;
233         char * hypertas;
234         unsigned int len;
235
236         ppc_md.setup_arch     = chrp_setup_arch;
237         ppc_md.get_cpuinfo    = chrp_get_cpuinfo;
238         if (naca->interrupt_controller == IC_OPEN_PIC) {
239                 ppc_md.init_IRQ       = pSeries_init_openpic; 
240                 ppc_md.get_irq        = openpic_get_irq;
241         } else {
242                 ppc_md.init_IRQ       = xics_init_IRQ;
243                 ppc_md.get_irq        = xics_get_irq;
244         }
245
246         ppc_md.init           = chrp_init2;
247
248         ppc_md.pcibios_fixup  = pSeries_final_fixup;
249
250         ppc_md.restart        = rtas_restart;
251         ppc_md.power_off      = rtas_power_off;
252         ppc_md.halt           = rtas_halt;
253         ppc_md.panic          = rtas_os_term;
254
255         ppc_md.get_boot_time  = pSeries_get_boot_time;
256         ppc_md.get_rtc_time   = pSeries_get_rtc_time;
257         ppc_md.set_rtc_time   = pSeries_set_rtc_time;
258         ppc_md.calibrate_decr = pSeries_calibrate_decr;
259
260         ppc_md.progress       = chrp_progress;
261
262         /* Build up the firmware_features bitmask field
263          * using contents of device-tree/ibm,hypertas-functions.
264          * Ultimately this functionality may be moved into prom.c prom_init().
265          */
266         cur_cpu_spec->firmware_features = 0;
267         dn = of_find_node_by_path("/rtas");
268         if (dn == NULL) {
269                 printk(KERN_ERR "WARNING ! Cannot find RTAS in device-tree !\n");
270                 goto no_rtas;
271         }
272
273         hypertas = get_property(dn, "ibm,hypertas-functions", &len);
274         if (hypertas) {
275                 while (len > 0){
276                         int i, hypertas_len;
277                         /* check value against table of strings */
278                         for(i=0; i < FIRMWARE_MAX_FEATURES ;i++) {
279                                 if ((firmware_features_table[i].name) &&
280                                     (strcmp(firmware_features_table[i].name,hypertas))==0) {
281                                         /* we have a match */
282                                         cur_cpu_spec->firmware_features |= 
283                                                 (firmware_features_table[i].val);
284                                         break;
285                                 } 
286                         }
287                         hypertas_len = strlen(hypertas);
288                         len -= hypertas_len +1;
289                         hypertas+= hypertas_len +1;
290                 }
291         }
292
293         of_node_put(dn);
294  no_rtas:
295         printk(KERN_INFO "firmware_features = 0x%lx\n", 
296                cur_cpu_spec->firmware_features);
297 }
298
299 void
300 chrp_progress(char *s, unsigned short hex)
301 {
302         struct device_node *root;
303         int width, *p;
304         char *os;
305         static int display_character, set_indicator;
306         static int max_width;
307         static spinlock_t progress_lock = SPIN_LOCK_UNLOCKED;
308         static int pending_newline = 0;  /* did last write end with unprinted newline? */
309
310         if (!rtas.base)
311                 return;
312
313         if (max_width == 0) {
314                 if ( (root = find_path_device("/rtas")) &&
315                      (p = (unsigned int *)get_property(root,
316                                                        "ibm,display-line-length",
317                                                        NULL)) )
318                         max_width = *p;
319                 else
320                         max_width = 0x10;
321                 display_character = rtas_token("display-character");
322                 set_indicator = rtas_token("set-indicator");
323         }
324         if (display_character == RTAS_UNKNOWN_SERVICE) {
325                 /* use hex display */
326                 if (set_indicator == RTAS_UNKNOWN_SERVICE)
327                         return;
328                 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
329                 return;
330         }
331
332         if(display_character == RTAS_UNKNOWN_SERVICE) {
333                 /* use hex display if available */
334                 if(set_indicator != RTAS_UNKNOWN_SERVICE)
335                         rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
336                 return;
337         }
338
339         spin_lock(&progress_lock);
340
341         /* Last write ended with newline, but we didn't print it since
342          * it would just clear the bottom line of output. Print it now
343          * instead.
344          *
345          * If no newline is pending, print a CR to start output at the
346          * beginning of the line.
347          */
348         if(pending_newline) {
349                 rtas_call(display_character, 1, 1, NULL, '\r');
350                 rtas_call(display_character, 1, 1, NULL, '\n');
351                 pending_newline = 0;
352         } else
353                 rtas_call(display_character, 1, 1, NULL, '\r');
354  
355         width = max_width;
356         os = s;
357         while (*os) {
358                 if(*os == '\n' || *os == '\r') {
359                         /* Blank to end of line. */
360                         while(width-- > 0)
361                                 rtas_call(display_character, 1, 1, NULL, ' ');
362  
363                         /* If newline is the last character, save it
364                          * until next call to avoid bumping up the
365                          * display output.
366                          */
367                         if(*os == '\n' && !os[1]) {
368                                 pending_newline = 1;
369                                 spin_unlock(&progress_lock);
370                                 return;
371                         }
372  
373                         /* RTAS wants CR-LF, not just LF */
374  
375                         if(*os == '\n') {
376                                 rtas_call(display_character, 1, 1, NULL, '\r');
377                                 rtas_call(display_character, 1, 1, NULL, '\n');
378                         } else {
379                                 /* CR might be used to re-draw a line, so we'll
380                                  * leave it alone and not add LF.
381                                  */
382                                 rtas_call(display_character, 1, 1, NULL, *os);
383                         }
384  
385                         width = max_width;
386                 } else {
387                         width--;
388                         rtas_call(display_character, 1, 1, NULL, *os);
389                 }
390  
391                 os++;
392  
393                 /* if we overwrite the screen length */
394                 if ( width <= 0 )
395                         while ( (*os != 0) && (*os != '\n') && (*os != '\r') )
396                                 os++;
397         }
398  
399         /* Blank to end of line. */
400         while ( width-- > 0 )
401                 rtas_call(display_character, 1, 1, NULL, ' ' );
402
403         spin_unlock(&progress_lock);
404 }
405
406 extern void setup_default_decr(void);
407
408 /* Some sane defaults: 125 MHz timebase, 1GHz processor */
409 #define DEFAULT_TB_FREQ         125000000UL
410 #define DEFAULT_PROC_FREQ       (DEFAULT_TB_FREQ * 8)
411
412 void __init pSeries_calibrate_decr(void)
413 {
414         struct device_node *cpu;
415         struct div_result divres;
416         unsigned int *fp;
417         int node_found;
418
419         /*
420          * The cpu node should have a timebase-frequency property
421          * to tell us the rate at which the decrementer counts.
422          */
423         cpu = of_find_node_by_type(NULL, "cpu");
424
425         ppc_tb_freq = DEFAULT_TB_FREQ;          /* hardcoded default */
426         node_found = 0;
427         if (cpu != 0) {
428                 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
429                                                   NULL);
430                 if (fp != 0) {
431                         node_found = 1;
432                         ppc_tb_freq = *fp;
433                 }
434         }
435         if (!node_found)
436                 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
437                                 "(not found)\n");
438
439         ppc_proc_freq = DEFAULT_PROC_FREQ;
440         node_found = 0;
441         if (cpu != 0) {
442                 fp = (unsigned int *)get_property(cpu, "clock-frequency",
443                                                   NULL);
444                 if (fp != 0) {
445                         node_found = 1;
446                         ppc_proc_freq = *fp;
447                 }
448         }
449         if (!node_found)
450                 printk(KERN_ERR "WARNING: Estimating processor frequency "
451                                 "(not found)\n");
452
453         of_node_put(cpu);
454
455         printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
456                ppc_tb_freq/1000000, ppc_tb_freq%1000000);
457         printk(KERN_INFO "time_init: processor frequency   = %lu.%.6lu MHz\n",
458                ppc_proc_freq/1000000, ppc_proc_freq%1000000);
459
460         tb_ticks_per_jiffy = ppc_tb_freq / HZ;
461         tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
462         tb_ticks_per_usec = ppc_tb_freq / 1000000;
463         tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
464         div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
465         tb_to_xs = divres.result_low;
466
467         setup_default_decr();
468 }