Jeff Ohlstein | 2b9c21c | 2012-04-30 18:57:53 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 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 Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 22 | #include <linux/cpu.h> |
| 23 | #include <linux/interrupt.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include <linux/mfd/pmic8058.h> |
| 25 | #include <linux/mfd/pmic8901.h> |
Jeff Ohlstein | 28009a8 | 2011-07-25 19:21:26 -0700 | [diff] [blame] | 26 | #include <linux/mfd/pm8xxx/misc.h> |
Michael Bohan | e720422 | 2012-09-07 10:21:18 -0700 | [diff] [blame] | 27 | #include <linux/qpnp/power-on.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | |
| 29 | #include <asm/mach-types.h> |
Subbaraman Narayanamurthy | 0c7a287 | 2012-09-14 13:32:08 -0700 | [diff] [blame] | 30 | #include <asm/cacheflush.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | |
| 32 | #include <mach/msm_iomap.h> |
| 33 | #include <mach/restart.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | #include <mach/socinfo.h> |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 35 | #include <mach/irqs.h> |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 36 | #include <mach/scm.h> |
| 37 | #include "msm_watchdog.h" |
Rohit Vaswani | f688fa6 | 2011-10-13 18:13:10 -0700 | [diff] [blame] | 38 | #include "timer.h" |
Pushkar Joshi | 892bcfc | 2012-09-27 12:33:31 -0700 | [diff] [blame] | 39 | #include "wdog_debug.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 40 | |
Rohit Vaswani | f688fa6 | 2011-10-13 18:13:10 -0700 | [diff] [blame] | 41 | #define WDT0_RST 0x38 |
| 42 | #define WDT0_EN 0x40 |
| 43 | #define WDT0_BARK_TIME 0x4C |
| 44 | #define WDT0_BITE_TIME 0x5C |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | |
| 46 | #define PSHOLD_CTL_SU (MSM_TLMM_BASE + 0x820) |
| 47 | |
| 48 | #define RESTART_REASON_ADDR 0x65C |
| 49 | #define DLOAD_MODE_ADDR 0x0 |
| 50 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 51 | #define SCM_IO_DISABLE_PMIC_ARBITER 1 |
| 52 | |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_MSM_RESTART_V2 |
| 54 | #define use_restart_v2() 1 |
| 55 | #else |
| 56 | #define use_restart_v2() 0 |
| 57 | #endif |
| 58 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 59 | static int restart_mode; |
| 60 | void *restart_reason; |
| 61 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 62 | int pmic_reset_irq; |
Rohit Vaswani | f688fa6 | 2011-10-13 18:13:10 -0700 | [diff] [blame] | 63 | static void __iomem *msm_tmr0_base; |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 64 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 65 | #ifdef CONFIG_MSM_DLOAD_MODE |
| 66 | static int in_panic; |
| 67 | static void *dload_mode_addr; |
| 68 | |
| 69 | /* Download mode master kill-switch */ |
| 70 | static int dload_set(const char *val, struct kernel_param *kp); |
| 71 | static int download_mode = 1; |
| 72 | module_param_call(download_mode, dload_set, param_get_int, |
| 73 | &download_mode, 0644); |
| 74 | |
| 75 | static int panic_prep_restart(struct notifier_block *this, |
| 76 | unsigned long event, void *ptr) |
| 77 | { |
| 78 | in_panic = 1; |
| 79 | return NOTIFY_DONE; |
| 80 | } |
| 81 | |
| 82 | static struct notifier_block panic_blk = { |
| 83 | .notifier_call = panic_prep_restart, |
| 84 | }; |
| 85 | |
| 86 | static void set_dload_mode(int on) |
| 87 | { |
| 88 | if (dload_mode_addr) { |
| 89 | __raw_writel(on ? 0xE47B337D : 0, dload_mode_addr); |
| 90 | __raw_writel(on ? 0xCE14091A : 0, |
| 91 | dload_mode_addr + sizeof(unsigned int)); |
| 92 | mb(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static int dload_set(const char *val, struct kernel_param *kp) |
| 97 | { |
| 98 | int ret; |
| 99 | int old_val = download_mode; |
| 100 | |
| 101 | ret = param_set_int(val, kp); |
| 102 | |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
| 106 | /* If download_mode is not zero or one, ignore. */ |
| 107 | if (download_mode >> 1) { |
| 108 | download_mode = old_val; |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | |
| 112 | set_dload_mode(download_mode); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | #else |
| 117 | #define set_dload_mode(x) do {} while (0) |
| 118 | #endif |
| 119 | |
| 120 | void msm_set_restart_mode(int mode) |
| 121 | { |
| 122 | restart_mode = mode; |
| 123 | } |
| 124 | EXPORT_SYMBOL(msm_set_restart_mode); |
| 125 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 126 | static void __msm_power_off(int lower_pshold) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | { |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 128 | printk(KERN_CRIT "Powering off the SoC\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | #ifdef CONFIG_MSM_DLOAD_MODE |
| 130 | set_dload_mode(0); |
| 131 | #endif |
Jeff Ohlstein | 28009a8 | 2011-07-25 19:21:26 -0700 | [diff] [blame] | 132 | pm8xxx_reset_pwr_off(0); |
Michael Bohan | e720422 | 2012-09-07 10:21:18 -0700 | [diff] [blame] | 133 | qpnp_pon_system_pwr_off(0); |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 134 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 135 | if (lower_pshold) { |
Shashank Mittal | e77ce16 | 2012-08-28 18:19:20 -0700 | [diff] [blame] | 136 | if (!use_restart_v2()) |
| 137 | __raw_writel(0, PSHOLD_CTL_SU); |
| 138 | else |
| 139 | __raw_writel(0, MSM_MPM2_PSHOLD_BASE); |
| 140 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 141 | mdelay(10000); |
| 142 | printk(KERN_ERR "Powering off has failed\n"); |
| 143 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 147 | static void msm_power_off(void) |
| 148 | { |
| 149 | /* MSM initiated power off, lower ps_hold */ |
| 150 | __msm_power_off(1); |
| 151 | } |
| 152 | |
| 153 | static void cpu_power_off(void *data) |
| 154 | { |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 155 | int rc; |
| 156 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 157 | pr_err("PMIC Initiated shutdown %s cpu=%d\n", __func__, |
| 158 | smp_processor_id()); |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 159 | if (smp_processor_id() == 0) { |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 160 | /* |
| 161 | * PMIC initiated power off, do not lower ps_hold, pmic will |
| 162 | * shut msm down |
| 163 | */ |
| 164 | __msm_power_off(0); |
| 165 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 166 | pet_watchdog(); |
| 167 | pr_err("Calling scm to disable arbiter\n"); |
| 168 | /* call secure manager to disable arbiter and never return */ |
| 169 | rc = scm_call_atomic1(SCM_SVC_PWR, |
| 170 | SCM_IO_DISABLE_PMIC_ARBITER, 1); |
| 171 | |
| 172 | pr_err("SCM returned even when asked to busy loop rc=%d\n", rc); |
| 173 | pr_err("waiting on pmic to shut msm down\n"); |
| 174 | } |
| 175 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 176 | preempt_disable(); |
| 177 | while (1) |
| 178 | ; |
| 179 | } |
| 180 | |
| 181 | static irqreturn_t resout_irq_handler(int irq, void *dev_id) |
| 182 | { |
| 183 | pr_warn("%s PMIC Initiated shutdown\n", __func__); |
| 184 | oops_in_progress = 1; |
| 185 | smp_call_function_many(cpu_online_mask, cpu_power_off, NULL, 0); |
| 186 | if (smp_processor_id() == 0) |
| 187 | cpu_power_off(NULL); |
| 188 | preempt_disable(); |
| 189 | while (1) |
| 190 | ; |
| 191 | return IRQ_HANDLED; |
| 192 | } |
| 193 | |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 194 | static void msm_restart_prepare(const char *cmd) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 195 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | #ifdef CONFIG_MSM_DLOAD_MODE |
| 197 | |
| 198 | /* This looks like a normal reboot at this point. */ |
| 199 | set_dload_mode(0); |
| 200 | |
| 201 | /* Write download mode flags if we're panic'ing */ |
| 202 | set_dload_mode(in_panic); |
| 203 | |
| 204 | /* Write download mode flags if restart_mode says so */ |
| 205 | if (restart_mode == RESTART_DLOAD) |
| 206 | set_dload_mode(1); |
| 207 | |
| 208 | /* Kill download mode if master-kill switch is set */ |
| 209 | if (!download_mode) |
| 210 | set_dload_mode(0); |
| 211 | #endif |
| 212 | |
Jeff Ohlstein | 28009a8 | 2011-07-25 19:21:26 -0700 | [diff] [blame] | 213 | pm8xxx_reset_pwr_off(1); |
Michael Bohan | e720422 | 2012-09-07 10:21:18 -0700 | [diff] [blame] | 214 | qpnp_pon_system_pwr_off(1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | |
| 216 | if (cmd != NULL) { |
| 217 | if (!strncmp(cmd, "bootloader", 10)) { |
| 218 | __raw_writel(0x77665500, restart_reason); |
| 219 | } else if (!strncmp(cmd, "recovery", 8)) { |
| 220 | __raw_writel(0x77665502, restart_reason); |
| 221 | } else if (!strncmp(cmd, "oem-", 4)) { |
| 222 | unsigned long code; |
| 223 | code = simple_strtoul(cmd + 4, NULL, 16) & 0xff; |
| 224 | __raw_writel(0x6f656d00 | code, restart_reason); |
| 225 | } else { |
| 226 | __raw_writel(0x77665501, restart_reason); |
| 227 | } |
| 228 | } |
Subbaraman Narayanamurthy | 0c7a287 | 2012-09-14 13:32:08 -0700 | [diff] [blame] | 229 | |
| 230 | flush_cache_all(); |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 231 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 232 | |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 233 | void msm_restart(char mode, const char *cmd) |
| 234 | { |
| 235 | printk(KERN_NOTICE "Going down for restart now\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 236 | |
Subbaraman Narayanamurthy | e31f279 | 2012-08-22 21:07:22 -0700 | [diff] [blame] | 237 | msm_restart_prepare(cmd); |
| 238 | |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 239 | if (!use_restart_v2()) { |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 240 | __raw_writel(0, msm_tmr0_base + WDT0_EN); |
| 241 | if (!(machine_is_msm8x60_fusion() || |
| 242 | machine_is_msm8x60_fusn_ffa())) { |
| 243 | mb(); |
| 244 | /* Actually reset the chip */ |
| 245 | __raw_writel(0, PSHOLD_CTL_SU); |
| 246 | mdelay(5000); |
| 247 | pr_notice("PS_HOLD didn't work, falling back to watchdog\n"); |
| 248 | } |
| 249 | |
| 250 | __raw_writel(1, msm_tmr0_base + WDT0_RST); |
| 251 | __raw_writel(5*0x31F3, msm_tmr0_base + WDT0_BARK_TIME); |
| 252 | __raw_writel(0x31F3, msm_tmr0_base + WDT0_BITE_TIME); |
| 253 | __raw_writel(1, msm_tmr0_base + WDT0_EN); |
Pushkar Joshi | 892bcfc | 2012-09-27 12:33:31 -0700 | [diff] [blame] | 254 | } else { |
| 255 | /* Needed for 8974: Reset GCC_WDOG_DEBUG register */ |
| 256 | msm_disable_wdog_debug(); |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 257 | __raw_writel(0, MSM_MPM2_PSHOLD_BASE); |
Pushkar Joshi | 892bcfc | 2012-09-27 12:33:31 -0700 | [diff] [blame] | 258 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | |
| 260 | mdelay(10000); |
| 261 | printk(KERN_ERR "Restarting has failed\n"); |
| 262 | } |
| 263 | |
Jeff Ohlstein | a238a985 | 2012-05-14 15:45:54 -0700 | [diff] [blame] | 264 | static int __init msm_pmic_restart_init(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 265 | { |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 266 | int rc; |
| 267 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 268 | if (pmic_reset_irq != 0) { |
| 269 | rc = request_any_context_irq(pmic_reset_irq, |
| 270 | resout_irq_handler, IRQF_TRIGGER_HIGH, |
| 271 | "restart_from_pmic", NULL); |
| 272 | if (rc < 0) |
| 273 | pr_err("pmic restart irq fail rc = %d\n", rc); |
| 274 | } else { |
| 275 | pr_warn("no pmic restart interrupt specified\n"); |
| 276 | } |
| 277 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Jeff Ohlstein | a238a985 | 2012-05-14 15:45:54 -0700 | [diff] [blame] | 281 | late_initcall(msm_pmic_restart_init); |
| 282 | |
| 283 | static int __init msm_restart_init(void) |
| 284 | { |
| 285 | #ifdef CONFIG_MSM_DLOAD_MODE |
| 286 | atomic_notifier_chain_register(&panic_notifier_list, &panic_blk); |
| 287 | dload_mode_addr = MSM_IMEM_BASE + DLOAD_MODE_ADDR; |
| 288 | set_dload_mode(download_mode); |
| 289 | #endif |
| 290 | msm_tmr0_base = msm_timer_get_timer0_base(); |
| 291 | restart_reason = MSM_IMEM_BASE + RESTART_REASON_ADDR; |
| 292 | pm_power_off = msm_power_off; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | early_initcall(msm_restart_init); |