4ddb339700b9453b1c02b793a0d03b302984c570
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / powernv / setup.c
1 /*
2  * PowerNV setup code.
3  *
4  * Copyright 2011 IBM Corp.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #undef DEBUG
13
14 #include <linux/cpu.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/tty.h>
19 #include <linux/reboot.h>
20 #include <linux/init.h>
21 #include <linux/console.h>
22 #include <linux/delay.h>
23 #include <linux/irq.h>
24 #include <linux/seq_file.h>
25 #include <linux/of.h>
26 #include <linux/interrupt.h>
27 #include <linux/bug.h>
28
29 #include <asm/machdep.h>
30 #include <asm/firmware.h>
31 #include <asm/xics.h>
32 #include <asm/rtas.h>
33 #include <asm/opal.h>
34
35 #include "powernv.h"
36
37 static void __init pnv_setup_arch(void)
38 {
39         /* Initialize SMP */
40         pnv_smp_init();
41
42         /* Setup PCI */
43         pnv_pci_init();
44
45         /* Setup RTC and NVRAM callbacks */
46         if (firmware_has_feature(FW_FEATURE_OPAL))
47                 opal_nvram_init();
48
49         /* Enable NAP mode */
50         powersave_nap = 1;
51
52         /* XXX PMCS */
53 }
54
55 static void __init pnv_init_early(void)
56 {
57         /*
58          * Initialize the LPC bus now so that legacy serial
59          * ports can be found on it
60          */
61         opal_lpc_init();
62
63 #ifdef CONFIG_HVC_OPAL
64         if (firmware_has_feature(FW_FEATURE_OPAL))
65                 hvc_opal_init_early();
66         else
67 #endif
68                 add_preferred_console("hvc", 0, NULL);
69 }
70
71 static void __init pnv_init_IRQ(void)
72 {
73         xics_init();
74
75         WARN_ON(!ppc_md.get_irq);
76 }
77
78 static void pnv_show_cpuinfo(struct seq_file *m)
79 {
80         struct device_node *root;
81         const char *model = "";
82
83         root = of_find_node_by_path("/");
84         if (root)
85                 model = of_get_property(root, "model", NULL);
86         seq_printf(m, "machine\t\t: PowerNV %s\n", model);
87         if (firmware_has_feature(FW_FEATURE_OPALv3))
88                 seq_printf(m, "firmware\t: OPAL v3\n");
89         else if (firmware_has_feature(FW_FEATURE_OPALv2))
90                 seq_printf(m, "firmware\t: OPAL v2\n");
91         else if (firmware_has_feature(FW_FEATURE_OPAL))
92                 seq_printf(m, "firmware\t: OPAL v1\n");
93         else
94                 seq_printf(m, "firmware\t: BML\n");
95         of_node_put(root);
96 }
97
98 static void  __noreturn pnv_restart(char *cmd)
99 {
100         long rc = OPAL_BUSY;
101
102         opal_notifier_disable();
103
104         while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
105                 rc = opal_cec_reboot();
106                 if (rc == OPAL_BUSY_EVENT)
107                         opal_poll_events(NULL);
108                 else
109                         mdelay(10);
110         }
111         for (;;)
112                 opal_poll_events(NULL);
113 }
114
115 static void __noreturn pnv_power_off(void)
116 {
117         long rc = OPAL_BUSY;
118
119         opal_notifier_disable();
120
121         while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
122                 rc = opal_cec_power_down(0);
123                 if (rc == OPAL_BUSY_EVENT)
124                         opal_poll_events(NULL);
125                 else
126                         mdelay(10);
127         }
128         for (;;)
129                 opal_poll_events(NULL);
130 }
131
132 static void __noreturn pnv_halt(void)
133 {
134         pnv_power_off();
135 }
136
137 static void pnv_progress(char *s, unsigned short hex)
138 {
139 }
140
141 static void pnv_shutdown(void)
142 {
143         /* Let the PCI code clear up IODA tables */
144         pnv_pci_shutdown();
145
146         /* And unregister all OPAL interrupts so they don't fire
147          * up while we kexec
148          */
149         opal_shutdown();
150 }
151
152 #ifdef CONFIG_KEXEC
153 static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
154 {
155         xics_kexec_teardown_cpu(secondary);
156 }
157 #endif /* CONFIG_KEXEC */
158
159 static void __init pnv_setup_machdep_opal(void)
160 {
161         ppc_md.get_boot_time = opal_get_boot_time;
162         ppc_md.get_rtc_time = opal_get_rtc_time;
163         ppc_md.set_rtc_time = opal_set_rtc_time;
164         ppc_md.restart = pnv_restart;
165         ppc_md.power_off = pnv_power_off;
166         ppc_md.halt = pnv_halt;
167         ppc_md.machine_check_exception = opal_machine_check;
168 }
169
170 #ifdef CONFIG_PPC_POWERNV_RTAS
171 static void __init pnv_setup_machdep_rtas(void)
172 {
173         if (rtas_token("get-time-of-day") != RTAS_UNKNOWN_SERVICE) {
174                 ppc_md.get_boot_time = rtas_get_boot_time;
175                 ppc_md.get_rtc_time = rtas_get_rtc_time;
176                 ppc_md.set_rtc_time = rtas_set_rtc_time;
177         }
178         ppc_md.restart = rtas_restart;
179         ppc_md.power_off = rtas_power_off;
180         ppc_md.halt = rtas_halt;
181 }
182 #endif /* CONFIG_PPC_POWERNV_RTAS */
183
184 static int __init pnv_probe(void)
185 {
186         unsigned long root = of_get_flat_dt_root();
187
188         if (!of_flat_dt_is_compatible(root, "ibm,powernv"))
189                 return 0;
190
191         hpte_init_native();
192
193         if (firmware_has_feature(FW_FEATURE_OPAL))
194                 pnv_setup_machdep_opal();
195 #ifdef CONFIG_PPC_POWERNV_RTAS
196         else if (rtas.base)
197                 pnv_setup_machdep_rtas();
198 #endif /* CONFIG_PPC_POWERNV_RTAS */
199
200         pr_debug("PowerNV detected !\n");
201
202         return 1;
203 }
204
205 define_machine(powernv) {
206         .name                   = "PowerNV",
207         .probe                  = pnv_probe,
208         .init_early             = pnv_init_early,
209         .setup_arch             = pnv_setup_arch,
210         .init_IRQ               = pnv_init_IRQ,
211         .show_cpuinfo           = pnv_show_cpuinfo,
212         .progress               = pnv_progress,
213         .machine_shutdown       = pnv_shutdown,
214         .power_save             = power7_idle,
215         .calibrate_decr         = generic_calibrate_decr,
216 #ifdef CONFIG_KEXEC
217         .kexec_cpu_down         = pnv_kexec_cpu_down,
218 #endif
219 };