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> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/mach-types.h> |
| 29 | |
| 30 | #include <mach/msm_iomap.h> |
| 31 | #include <mach/restart.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | #include <mach/socinfo.h> |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 33 | #include <mach/irqs.h> |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 34 | #include <mach/scm.h> |
| 35 | #include "msm_watchdog.h" |
Rohit Vaswani | f688fa6 | 2011-10-13 18:13:10 -0700 | [diff] [blame] | 36 | #include "timer.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | |
Rohit Vaswani | f688fa6 | 2011-10-13 18:13:10 -0700 | [diff] [blame] | 38 | #define WDT0_RST 0x38 |
| 39 | #define WDT0_EN 0x40 |
| 40 | #define WDT0_BARK_TIME 0x4C |
| 41 | #define WDT0_BITE_TIME 0x5C |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 42 | |
| 43 | #define PSHOLD_CTL_SU (MSM_TLMM_BASE + 0x820) |
| 44 | |
| 45 | #define RESTART_REASON_ADDR 0x65C |
| 46 | #define DLOAD_MODE_ADDR 0x0 |
| 47 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 48 | #define SCM_IO_DISABLE_PMIC_ARBITER 1 |
| 49 | |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_MSM_RESTART_V2 |
| 51 | #define use_restart_v2() 1 |
| 52 | #else |
| 53 | #define use_restart_v2() 0 |
| 54 | #endif |
| 55 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 56 | static int restart_mode; |
| 57 | void *restart_reason; |
| 58 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 59 | int pmic_reset_irq; |
Rohit Vaswani | f688fa6 | 2011-10-13 18:13:10 -0700 | [diff] [blame] | 60 | static void __iomem *msm_tmr0_base; |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 61 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 62 | #ifdef CONFIG_MSM_DLOAD_MODE |
| 63 | static int in_panic; |
| 64 | static void *dload_mode_addr; |
| 65 | |
| 66 | /* Download mode master kill-switch */ |
| 67 | static int dload_set(const char *val, struct kernel_param *kp); |
| 68 | static int download_mode = 1; |
| 69 | module_param_call(download_mode, dload_set, param_get_int, |
| 70 | &download_mode, 0644); |
| 71 | |
| 72 | static 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 | |
| 79 | static struct notifier_block panic_blk = { |
| 80 | .notifier_call = panic_prep_restart, |
| 81 | }; |
| 82 | |
| 83 | static 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 | |
| 93 | static 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 | |
| 117 | void msm_set_restart_mode(int mode) |
| 118 | { |
| 119 | restart_mode = mode; |
| 120 | } |
| 121 | EXPORT_SYMBOL(msm_set_restart_mode); |
| 122 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 123 | static void __msm_power_off(int lower_pshold) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 124 | { |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 125 | printk(KERN_CRIT "Powering off the SoC\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | #ifdef CONFIG_MSM_DLOAD_MODE |
| 127 | set_dload_mode(0); |
| 128 | #endif |
Jeff Ohlstein | 28009a8 | 2011-07-25 19:21:26 -0700 | [diff] [blame] | 129 | pm8xxx_reset_pwr_off(0); |
Anirudh Ghayal | c201933 | 2011-11-12 06:29:10 +0530 | [diff] [blame] | 130 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 131 | if (lower_pshold) { |
Shashank Mittal | e77ce16 | 2012-08-28 18:19:20 -0700 | [diff] [blame^] | 132 | if (!use_restart_v2()) |
| 133 | __raw_writel(0, PSHOLD_CTL_SU); |
| 134 | else |
| 135 | __raw_writel(0, MSM_MPM2_PSHOLD_BASE); |
| 136 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 137 | mdelay(10000); |
| 138 | printk(KERN_ERR "Powering off has failed\n"); |
| 139 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 143 | static void msm_power_off(void) |
| 144 | { |
| 145 | /* MSM initiated power off, lower ps_hold */ |
| 146 | __msm_power_off(1); |
| 147 | } |
| 148 | |
| 149 | static void cpu_power_off(void *data) |
| 150 | { |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 151 | int rc; |
| 152 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 153 | pr_err("PMIC Initiated shutdown %s cpu=%d\n", __func__, |
| 154 | smp_processor_id()); |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 155 | if (smp_processor_id() == 0) { |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 156 | /* |
| 157 | * PMIC initiated power off, do not lower ps_hold, pmic will |
| 158 | * shut msm down |
| 159 | */ |
| 160 | __msm_power_off(0); |
| 161 | |
Abhijeet Dharmapurikar | 9259fef | 2011-09-24 19:07:48 -0700 | [diff] [blame] | 162 | 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 Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 172 | preempt_disable(); |
| 173 | while (1) |
| 174 | ; |
| 175 | } |
| 176 | |
| 177 | static 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 Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 190 | static void msm_restart_prepare(const char *cmd) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | #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 Ohlstein | 28009a8 | 2011-07-25 19:21:26 -0700 | [diff] [blame] | 209 | pm8xxx_reset_pwr_off(1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 210 | |
| 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 Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 224 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 225 | |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 226 | void msm_restart(char mode, const char *cmd) |
| 227 | { |
| 228 | printk(KERN_NOTICE "Going down for restart now\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | |
Subbaraman Narayanamurthy | e31f279 | 2012-08-22 21:07:22 -0700 | [diff] [blame] | 230 | msm_restart_prepare(cmd); |
| 231 | |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 232 | if (!use_restart_v2()) { |
Stepan Moskovchenko | a000d13 | 2012-08-14 21:05:14 -0700 | [diff] [blame] | 233 | __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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 249 | |
| 250 | mdelay(10000); |
| 251 | printk(KERN_ERR "Restarting has failed\n"); |
| 252 | } |
| 253 | |
Jeff Ohlstein | a238a985 | 2012-05-14 15:45:54 -0700 | [diff] [blame] | 254 | static int __init msm_pmic_restart_init(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 255 | { |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 256 | int rc; |
| 257 | |
Abhijeet Dharmapurikar | 6d565fd | 2011-09-15 18:49:56 -0700 | [diff] [blame] | 258 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Jeff Ohlstein | a238a985 | 2012-05-14 15:45:54 -0700 | [diff] [blame] | 271 | late_initcall(msm_pmic_restart_init); |
| 272 | |
| 273 | static 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 | } |
| 286 | early_initcall(msm_restart_init); |