blob: 6b5bcfd08ebb1b700543457284ff3555ac654994 [file] [log] [blame]
Jeff Ohlstein2b9c21c2012-04-30 18:57:53 -07001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/reboot.h>
19#include <linux/io.h>
20#include <linux/delay.h>
21#include <linux/pm.h>
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -070022#include <linux/cpu.h>
23#include <linux/interrupt.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/mfd/pmic8058.h>
25#include <linux/mfd/pmic8901.h>
Jeff Ohlstein28009a82011-07-25 19:21:26 -070026#include <linux/mfd/pm8xxx/misc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#include <asm/mach-types.h>
29
30#include <mach/msm_iomap.h>
31#include <mach/restart.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <mach/socinfo.h>
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -070033#include <mach/irqs.h>
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -070034#include <mach/scm.h>
35#include "msm_watchdog.h"
Rohit Vaswanif688fa62011-10-13 18:13:10 -070036#include "timer.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
Rohit Vaswanif688fa62011-10-13 18:13:10 -070038#define WDT0_RST 0x38
39#define WDT0_EN 0x40
40#define WDT0_BARK_TIME 0x4C
41#define WDT0_BITE_TIME 0x5C
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042
43#define PSHOLD_CTL_SU (MSM_TLMM_BASE + 0x820)
44
45#define RESTART_REASON_ADDR 0x65C
46#define DLOAD_MODE_ADDR 0x0
47
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -070048#define SCM_IO_DISABLE_PMIC_ARBITER 1
49
Jaeseong GIMb8499412012-06-26 10:36:57 -070050#ifdef CONFIG_LGE_CRASH_HANDLER
51#define LGE_ERROR_HANDLER_MAGIC_NUM 0xA97F2C46
52#define LGE_ERROR_HANDLER_MAGIC_ADDR 0x18
53void *lge_error_handler_cookie_addr;
54#endif
55
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056static int restart_mode;
57void *restart_reason;
58
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -070059int pmic_reset_irq;
Rohit Vaswanif688fa62011-10-13 18:13:10 -070060static void __iomem *msm_tmr0_base;
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -070061
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062#ifdef CONFIG_MSM_DLOAD_MODE
63static int in_panic;
64static void *dload_mode_addr;
65
66/* Download mode master kill-switch */
67static int dload_set(const char *val, struct kernel_param *kp);
Jaeseong GIM04e39432012-07-18 18:09:00 -070068static int download_mode = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069module_param_call(download_mode, dload_set, param_get_int,
70 &download_mode, 0644);
71
72static int panic_prep_restart(struct notifier_block *this,
73 unsigned long event, void *ptr)
74{
75 in_panic = 1;
76 return NOTIFY_DONE;
77}
78
79static struct notifier_block panic_blk = {
80 .notifier_call = panic_prep_restart,
81};
82
83static void set_dload_mode(int on)
84{
85 if (dload_mode_addr) {
86 __raw_writel(on ? 0xE47B337D : 0, dload_mode_addr);
87 __raw_writel(on ? 0xCE14091A : 0,
88 dload_mode_addr + sizeof(unsigned int));
Jaeseong GIMb8499412012-06-26 10:36:57 -070089#ifdef CONFIG_LGE_CRASH_HANDLER
90 __raw_writel(on ? LGE_ERROR_HANDLER_MAGIC_NUM : 0,
91 lge_error_handler_cookie_addr);
92#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 mb();
94 }
95}
96
97static int dload_set(const char *val, struct kernel_param *kp)
98{
99 int ret;
100 int old_val = download_mode;
101
102 ret = param_set_int(val, kp);
103
104 if (ret)
105 return ret;
106
107 /* If download_mode is not zero or one, ignore. */
108 if (download_mode >> 1) {
109 download_mode = old_val;
110 return -EINVAL;
111 }
112
113 set_dload_mode(download_mode);
114
115 return 0;
116}
117#else
118#define set_dload_mode(x) do {} while (0)
119#endif
120
121void msm_set_restart_mode(int mode)
122{
123 restart_mode = mode;
124}
125EXPORT_SYMBOL(msm_set_restart_mode);
126
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700127static void __msm_power_off(int lower_pshold)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128{
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700129 printk(KERN_CRIT "Powering off the SoC\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130#ifdef CONFIG_MSM_DLOAD_MODE
131 set_dload_mode(0);
132#endif
Jeff Ohlstein28009a82011-07-25 19:21:26 -0700133 pm8xxx_reset_pwr_off(0);
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530134
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -0700135 if (lower_pshold) {
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700136 __raw_writel(0, PSHOLD_CTL_SU);
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -0700137 mdelay(10000);
138 printk(KERN_ERR "Powering off has failed\n");
139 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140 return;
141}
142
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700143static void msm_power_off(void)
144{
145 /* MSM initiated power off, lower ps_hold */
146 __msm_power_off(1);
147}
148
149static void cpu_power_off(void *data)
150{
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -0700151 int rc;
152
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700153 pr_err("PMIC Initiated shutdown %s cpu=%d\n", __func__,
154 smp_processor_id());
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -0700155 if (smp_processor_id() == 0) {
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700156 /*
157 * PMIC initiated power off, do not lower ps_hold, pmic will
158 * shut msm down
159 */
160 __msm_power_off(0);
161
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -0700162 pet_watchdog();
163 pr_err("Calling scm to disable arbiter\n");
164 /* call secure manager to disable arbiter and never return */
165 rc = scm_call_atomic1(SCM_SVC_PWR,
166 SCM_IO_DISABLE_PMIC_ARBITER, 1);
167
168 pr_err("SCM returned even when asked to busy loop rc=%d\n", rc);
169 pr_err("waiting on pmic to shut msm down\n");
170 }
171
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700172 preempt_disable();
173 while (1)
174 ;
175}
176
177static irqreturn_t resout_irq_handler(int irq, void *dev_id)
178{
179 pr_warn("%s PMIC Initiated shutdown\n", __func__);
180 oops_in_progress = 1;
181 smp_call_function_many(cpu_online_mask, cpu_power_off, NULL, 0);
182 if (smp_processor_id() == 0)
183 cpu_power_off(NULL);
184 preempt_disable();
185 while (1)
186 ;
187 return IRQ_HANDLED;
188}
189
Jaeseong GIMb8499412012-06-26 10:36:57 -0700190#ifdef CONFIG_LGE_CRASH_HANDLER
191static int ssr_magic_number = 0;
192#define SUBSYS_NAME_MAX_LENGTH 40
193
194int get_ssr_magic_number(void)
195{
196 return ssr_magic_number;
197}
198
199void set_ssr_magic_number(const char* subsys_name)
200{
201 int i;
202 const char *subsys_list[] = {
203 "modem", "riva", "dsps", "lpass",
204 "external_modem", "gss",
205 };
206
207 ssr_magic_number = (0x6d630000 | 0x0000f000);
208
209 for (i=0; i < ARRAY_SIZE(subsys_list); i++) {
210 if (!strncmp(subsys_list[i], subsys_name,
211 SUBSYS_NAME_MAX_LENGTH)) {
212 ssr_magic_number = (0x6d630000 | ((i+1)<<12));
213 break;
214 }
215 }
216}
217
218void set_kernel_crash_magic_number(void)
219{
220 if (ssr_magic_number == 0)
221 __raw_writel(0x6d630100, restart_reason);
222 else
223 __raw_writel(restart_mode, restart_reason);
224}
225#endif /* CONFIG_LGE_CRASH_HANDLER */
226
Jeff Ohlsteindd0dd9b2012-05-29 17:47:21 -0700227void msm_restart(char mode, const char *cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228{
229
230#ifdef CONFIG_MSM_DLOAD_MODE
231
232 /* This looks like a normal reboot at this point. */
233 set_dload_mode(0);
234
235 /* Write download mode flags if we're panic'ing */
236 set_dload_mode(in_panic);
237
238 /* Write download mode flags if restart_mode says so */
Jaeseong GIMb8499412012-06-26 10:36:57 -0700239 if (restart_mode == RESTART_DLOAD) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 set_dload_mode(1);
Jaeseong GIMb8499412012-06-26 10:36:57 -0700241#ifdef CONFIG_LGE_CRASH_HANDLER
242 writel(0x6d63c421, restart_reason);
243 goto reset;
244#endif
245 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246
247 /* Kill download mode if master-kill switch is set */
248 if (!download_mode)
249 set_dload_mode(0);
250#endif
251
252 printk(KERN_NOTICE "Going down for restart now\n");
253
Jeff Ohlstein28009a82011-07-25 19:21:26 -0700254 pm8xxx_reset_pwr_off(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255
Jaeseong GIMb8499412012-06-26 10:36:57 -0700256#ifdef CONFIG_LGE_CRASH_HANDLER
257 if (in_panic == 1) {
258 set_kernel_crash_magic_number();
259 } else {
260 if (cmd != NULL) {
261 if (!strncmp(cmd, "bootloader", 10)) {
262 __raw_writel(0x77665500, restart_reason);
263 } else if (!strncmp(cmd, "recovery", 8)) {
264 __raw_writel(0x77665502, restart_reason);
265 } else if (!strncmp(cmd, "oem-", 4)) {
266 unsigned long code;
267 code = simple_strtoul(cmd+4, NULL, 16) & 0xff;
268 __raw_writel(0x6f656d00, restart_reason);
269 } else if (!strncmp(cmd, "recovery", 8)) {
270 __raw_writel(0x77665502, restart_reason);
271 } else {
272 __raw_writel(0x77665501, restart_reason);
273 }
274 }
275 }
276reset:
277#else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 if (cmd != NULL) {
279 if (!strncmp(cmd, "bootloader", 10)) {
280 __raw_writel(0x77665500, restart_reason);
281 } else if (!strncmp(cmd, "recovery", 8)) {
282 __raw_writel(0x77665502, restart_reason);
283 } else if (!strncmp(cmd, "oem-", 4)) {
284 unsigned long code;
285 code = simple_strtoul(cmd + 4, NULL, 16) & 0xff;
286 __raw_writel(0x6f656d00 | code, restart_reason);
287 } else {
288 __raw_writel(0x77665501, restart_reason);
289 }
290 }
Jaeseong GIMb8499412012-06-26 10:36:57 -0700291#endif /* CONFIG_LGE_CRASH_HANDLER */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292
Rohit Vaswanif688fa62011-10-13 18:13:10 -0700293 __raw_writel(0, msm_tmr0_base + WDT0_EN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294 if (!(machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())) {
295 mb();
296 __raw_writel(0, PSHOLD_CTL_SU); /* Actually reset the chip */
297 mdelay(5000);
298 pr_notice("PS_HOLD didn't work, falling back to watchdog\n");
299 }
300
Rohit Vaswanif688fa62011-10-13 18:13:10 -0700301 __raw_writel(1, msm_tmr0_base + WDT0_RST);
302 __raw_writel(5*0x31F3, msm_tmr0_base + WDT0_BARK_TIME);
303 __raw_writel(0x31F3, msm_tmr0_base + WDT0_BITE_TIME);
304 __raw_writel(1, msm_tmr0_base + WDT0_EN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305
306 mdelay(10000);
307 printk(KERN_ERR "Restarting has failed\n");
308}
309
Jeff Ohlsteina238a982012-05-14 15:45:54 -0700310static int __init msm_pmic_restart_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311{
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700312 int rc;
313
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700314 if (pmic_reset_irq != 0) {
315 rc = request_any_context_irq(pmic_reset_irq,
316 resout_irq_handler, IRQF_TRIGGER_HIGH,
317 "restart_from_pmic", NULL);
318 if (rc < 0)
319 pr_err("pmic restart irq fail rc = %d\n", rc);
320 } else {
321 pr_warn("no pmic restart interrupt specified\n");
322 }
323
Jaeseong GIMb8499412012-06-26 10:36:57 -0700324#ifdef CONFIG_LGE_CRASH_HANDLER
325 __raw_writel(0x6d63ad00, restart_reason);
326#endif
327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 return 0;
329}
330
Jeff Ohlsteina238a982012-05-14 15:45:54 -0700331late_initcall(msm_pmic_restart_init);
332
333static int __init msm_restart_init(void)
334{
335#ifdef CONFIG_MSM_DLOAD_MODE
336 atomic_notifier_chain_register(&panic_notifier_list, &panic_blk);
337 dload_mode_addr = MSM_IMEM_BASE + DLOAD_MODE_ADDR;
Jaeseong GIMb8499412012-06-26 10:36:57 -0700338#ifdef CONFIG_LGE_CRASH_HANDLER
339 lge_error_handler_cookie_addr = MSM_IMEM_BASE +
340 LGE_ERROR_HANDLER_MAGIC_ADDR;
341#endif
Jeff Ohlsteina238a982012-05-14 15:45:54 -0700342 set_dload_mode(download_mode);
343#endif
344 msm_tmr0_base = msm_timer_get_timer0_base();
345 restart_reason = MSM_IMEM_BASE + RESTART_REASON_ADDR;
346 pm_power_off = msm_power_off;
347
348 return 0;
349}
350early_initcall(msm_restart_init);