blob: 25723d51899afb1a9e06a1776ec3c2156dd2bf4c [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
Stepan Moskovchenkoa000d132012-08-14 21:05:14 -070050#ifdef CONFIG_MSM_RESTART_V2
51#define use_restart_v2() 1
52#else
53#define use_restart_v2() 0
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);
68static int download_mode = 1;
69module_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));
89 mb();
90 }
91}
92
93static int dload_set(const char *val, struct kernel_param *kp)
94{
95 int ret;
96 int old_val = download_mode;
97
98 ret = param_set_int(val, kp);
99
100 if (ret)
101 return ret;
102
103 /* If download_mode is not zero or one, ignore. */
104 if (download_mode >> 1) {
105 download_mode = old_val;
106 return -EINVAL;
107 }
108
109 set_dload_mode(download_mode);
110
111 return 0;
112}
113#else
114#define set_dload_mode(x) do {} while (0)
115#endif
116
117void msm_set_restart_mode(int mode)
118{
119 restart_mode = mode;
120}
121EXPORT_SYMBOL(msm_set_restart_mode);
122
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700123static void __msm_power_off(int lower_pshold)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124{
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700125 printk(KERN_CRIT "Powering off the SoC\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126#ifdef CONFIG_MSM_DLOAD_MODE
127 set_dload_mode(0);
128#endif
Jeff Ohlstein28009a82011-07-25 19:21:26 -0700129 pm8xxx_reset_pwr_off(0);
Anirudh Ghayalc2019332011-11-12 06:29:10 +0530130
Abhijeet Dharmapurikar9259fef2011-09-24 19:07:48 -0700131 if (lower_pshold) {
Shashank Mittale77ce162012-08-28 18:19:20 -0700132 if (!use_restart_v2())
133 __raw_writel(0, PSHOLD_CTL_SU);
134 else
135 __raw_writel(0, MSM_MPM2_PSHOLD_BASE);
136
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
Stepan Moskovchenkoa000d132012-08-14 21:05:14 -0700190static void msm_restart_prepare(const char *cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192#ifdef CONFIG_MSM_DLOAD_MODE
193
194 /* This looks like a normal reboot at this point. */
195 set_dload_mode(0);
196
197 /* Write download mode flags if we're panic'ing */
198 set_dload_mode(in_panic);
199
200 /* Write download mode flags if restart_mode says so */
201 if (restart_mode == RESTART_DLOAD)
202 set_dload_mode(1);
203
204 /* Kill download mode if master-kill switch is set */
205 if (!download_mode)
206 set_dload_mode(0);
207#endif
208
Jeff Ohlstein28009a82011-07-25 19:21:26 -0700209 pm8xxx_reset_pwr_off(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210
211 if (cmd != NULL) {
212 if (!strncmp(cmd, "bootloader", 10)) {
213 __raw_writel(0x77665500, restart_reason);
214 } else if (!strncmp(cmd, "recovery", 8)) {
215 __raw_writel(0x77665502, restart_reason);
216 } else if (!strncmp(cmd, "oem-", 4)) {
217 unsigned long code;
218 code = simple_strtoul(cmd + 4, NULL, 16) & 0xff;
219 __raw_writel(0x6f656d00 | code, restart_reason);
220 } else {
221 __raw_writel(0x77665501, restart_reason);
222 }
223 }
Stepan Moskovchenkoa000d132012-08-14 21:05:14 -0700224}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225
Stepan Moskovchenkoa000d132012-08-14 21:05:14 -0700226void msm_restart(char mode, const char *cmd)
227{
228 printk(KERN_NOTICE "Going down for restart now\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229
Subbaraman Narayanamurthye31f2792012-08-22 21:07:22 -0700230 msm_restart_prepare(cmd);
231
Stepan Moskovchenkoa000d132012-08-14 21:05:14 -0700232 if (!use_restart_v2()) {
Stepan Moskovchenkoa000d132012-08-14 21:05:14 -0700233 __raw_writel(0, msm_tmr0_base + WDT0_EN);
234 if (!(machine_is_msm8x60_fusion() ||
235 machine_is_msm8x60_fusn_ffa())) {
236 mb();
237 /* Actually reset the chip */
238 __raw_writel(0, PSHOLD_CTL_SU);
239 mdelay(5000);
240 pr_notice("PS_HOLD didn't work, falling back to watchdog\n");
241 }
242
243 __raw_writel(1, msm_tmr0_base + WDT0_RST);
244 __raw_writel(5*0x31F3, msm_tmr0_base + WDT0_BARK_TIME);
245 __raw_writel(0x31F3, msm_tmr0_base + WDT0_BITE_TIME);
246 __raw_writel(1, msm_tmr0_base + WDT0_EN);
247 } else
248 __raw_writel(0, MSM_MPM2_PSHOLD_BASE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249
250 mdelay(10000);
251 printk(KERN_ERR "Restarting has failed\n");
252}
253
Jeff Ohlsteina238a9852012-05-14 15:45:54 -0700254static int __init msm_pmic_restart_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255{
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700256 int rc;
257
Abhijeet Dharmapurikar6d565fd2011-09-15 18:49:56 -0700258 if (pmic_reset_irq != 0) {
259 rc = request_any_context_irq(pmic_reset_irq,
260 resout_irq_handler, IRQF_TRIGGER_HIGH,
261 "restart_from_pmic", NULL);
262 if (rc < 0)
263 pr_err("pmic restart irq fail rc = %d\n", rc);
264 } else {
265 pr_warn("no pmic restart interrupt specified\n");
266 }
267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 return 0;
269}
270
Jeff Ohlsteina238a9852012-05-14 15:45:54 -0700271late_initcall(msm_pmic_restart_init);
272
273static int __init msm_restart_init(void)
274{
275#ifdef CONFIG_MSM_DLOAD_MODE
276 atomic_notifier_chain_register(&panic_notifier_list, &panic_blk);
277 dload_mode_addr = MSM_IMEM_BASE + DLOAD_MODE_ADDR;
278 set_dload_mode(download_mode);
279#endif
280 msm_tmr0_base = msm_timer_get_timer0_base();
281 restart_reason = MSM_IMEM_BASE + RESTART_REASON_ADDR;
282 pm_power_off = msm_power_off;
283
284 return 0;
285}
286early_initcall(msm_restart_init);