restart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  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. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/reboot.h>
  18. #include <linux/io.h>
  19. #include <linux/delay.h>
  20. #include <linux/pm.h>
  21. #include <linux/cpu.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mfd/pmic8058.h>
  24. #include <linux/mfd/pmic8901.h>
  25. #include <linux/mfd/pm8xxx/misc.h>
  26. #include <linux/qpnp/power-on.h>
  27. #include <asm/mach-types.h>
  28. #include <asm/cacheflush.h>
  29. #include <mach/msm_iomap.h>
  30. #include <mach/restart.h>
  31. #include <mach/socinfo.h>
  32. #ifdef CONFIG_SEC_DEBUG
  33. #include <mach/sec_debug.h>
  34. #include <linux/notifier.h>
  35. #include <linux/ftrace.h>
  36. #endif
  37. #include <mach/irqs.h>
  38. #include <mach/scm.h>
  39. #include "msm_watchdog.h"
  40. #include "timer.h"
  41. #include "wdog_debug.h"
  42. #define WDT0_RST 0x38
  43. #define WDT0_EN 0x40
  44. #define WDT0_BARK_TIME 0x4C
  45. #define WDT0_BITE_TIME 0x5C
  46. #define PSHOLD_CTL_SU (MSM_TLMM_BASE + 0x820)
  47. #define RESTART_REASON_ADDR 0x65C
  48. #define DLOAD_MODE_ADDR 0x0
  49. #define EMERGENCY_DLOAD_MODE_ADDR 0xFE0
  50. #define EMERGENCY_DLOAD_MAGIC1 0x322A4F99
  51. #define EMERGENCY_DLOAD_MAGIC2 0xC67E4350
  52. #define EMERGENCY_DLOAD_MAGIC3 0x77777777
  53. #define SCM_IO_DISABLE_PMIC_ARBITER 1
  54. #ifdef CONFIG_MSM_RESTART_V2
  55. #define use_restart_v2() 1
  56. #else
  57. #define use_restart_v2() 0
  58. #endif
  59. static int restart_mode;
  60. #ifndef CONFIG_SEC_DEBUG
  61. void *restart_reason;
  62. #endif
  63. #ifdef CONFIG_USER_RESET_DEBUG
  64. #define RESET_CAUSE_LPM_REBOOT 0x95
  65. void *reboot_cause;
  66. extern int poweroff_charging;
  67. #endif
  68. int pmic_reset_irq;
  69. static void __iomem *msm_tmr0_base;
  70. #ifdef CONFIG_MSM_DLOAD_MODE
  71. static int in_panic;
  72. static void *dload_mode_addr;
  73. static bool dload_mode_enabled;
  74. static void *emergency_dload_mode_addr;
  75. /* Download mode master kill-switch */
  76. static int dload_set(const char *val, struct kernel_param *kp);
  77. static int download_mode = 1;
  78. module_param_call(download_mode, dload_set, param_get_int,
  79. &download_mode, 0644);
  80. static int panic_prep_restart(struct notifier_block *this,
  81. unsigned long event, void *ptr)
  82. {
  83. in_panic = 1;
  84. return NOTIFY_DONE;
  85. }
  86. static struct notifier_block panic_blk = {
  87. .notifier_call = panic_prep_restart,
  88. };
  89. void set_dload_mode(int on)
  90. {
  91. if (dload_mode_addr) {
  92. __raw_writel(on ? 0xE47B337D : 0, dload_mode_addr);
  93. __raw_writel(on ? 0xCE14091A : 0,
  94. dload_mode_addr + sizeof(unsigned int));
  95. mb();
  96. dload_mode_enabled = on;
  97. #ifdef CONFIG_SEC_DEBUG
  98. pr_err("set_dload_mode <%d> ( %x )\n", on,
  99. (unsigned int) CALLER_ADDR0);
  100. #endif
  101. }
  102. }
  103. EXPORT_SYMBOL(set_dload_mode);
  104. #if 0
  105. static bool get_dload_mode(void)
  106. {
  107. return dload_mode_enabled;
  108. }
  109. #endif
  110. #if 0
  111. static void enable_emergency_dload_mode(void)
  112. {
  113. if (emergency_dload_mode_addr) {
  114. __raw_writel(EMERGENCY_DLOAD_MAGIC1,
  115. emergency_dload_mode_addr);
  116. __raw_writel(EMERGENCY_DLOAD_MAGIC2,
  117. emergency_dload_mode_addr +
  118. sizeof(unsigned int));
  119. __raw_writel(EMERGENCY_DLOAD_MAGIC3,
  120. emergency_dload_mode_addr +
  121. (2 * sizeof(unsigned int)));
  122. /* Need disable the pmic wdt, then the emergency dload mode
  123. * will not auto reset. */
  124. qpnp_pon_wd_config(0);
  125. mb();
  126. }
  127. }
  128. #endif
  129. static int dload_set(const char *val, struct kernel_param *kp)
  130. {
  131. int ret;
  132. int old_val = download_mode;
  133. ret = param_set_int(val, kp);
  134. if (ret)
  135. return ret;
  136. /* If download_mode is not zero or one, ignore. */
  137. if (download_mode >> 1) {
  138. download_mode = old_val;
  139. return -EINVAL;
  140. }
  141. set_dload_mode(download_mode);
  142. return 0;
  143. }
  144. #else
  145. void set_dload_mode(int on)
  146. {
  147. do {} while (0);
  148. }
  149. EXPORT_SYMBOL(set_dload_mode);
  150. #if 0
  151. static void enable_emergency_dload_mode(void)
  152. {
  153. printk(KERN_ERR "dload mode is not enabled on target\n");
  154. }
  155. #endif
  156. static bool get_dload_mode(void)
  157. {
  158. return false;
  159. }
  160. #endif
  161. void msm_set_restart_mode(int mode)
  162. {
  163. restart_mode = mode;
  164. }
  165. EXPORT_SYMBOL(msm_set_restart_mode);
  166. static bool scm_pmic_arbiter_disable_supported;
  167. /*
  168. * Force the SPMI PMIC arbiter to shutdown so that no more SPMI transactions
  169. * are sent from the MSM to the PMIC. This is required in order to avoid an
  170. * SPMI lockup on certain PMIC chips if PS_HOLD is lowered in the middle of
  171. * an SPMI transaction.
  172. */
  173. static void halt_spmi_pmic_arbiter(void)
  174. {
  175. if (scm_pmic_arbiter_disable_supported) {
  176. pr_crit("Calling SCM to disable SPMI PMIC arbiter\n");
  177. scm_call_atomic1(SCM_SVC_PWR, SCM_IO_DISABLE_PMIC_ARBITER, 0);
  178. }
  179. }
  180. static void __msm_power_off(int lower_pshold)
  181. {
  182. printk(KERN_CRIT "Powering off the SoC\n");
  183. #ifdef CONFIG_RESTART_REASON_DDR
  184. if(restart_reason_ddr_address) {
  185. /* Clear the stale magic number present in DDR restart reason address*/
  186. __raw_writel(0x00000000, restart_reason_ddr_address);
  187. printk(KERN_NOTICE "%s: Clearing the stale restart_reason: 0x%x \n", __func__,__raw_readl(restart_reason_ddr_address));
  188. }
  189. #endif
  190. #ifdef CONFIG_MSM_DLOAD_MODE
  191. set_dload_mode(0);
  192. #endif
  193. pm8xxx_reset_pwr_off(0);
  194. qpnp_pon_system_pwr_off(PON_POWER_OFF_SHUTDOWN);
  195. if (lower_pshold) {
  196. if (!use_restart_v2()) {
  197. __raw_writel(0, PSHOLD_CTL_SU);
  198. } else {
  199. halt_spmi_pmic_arbiter();
  200. __raw_writel(0, MSM_MPM2_PSHOLD_BASE);
  201. }
  202. mdelay(10000);
  203. printk(KERN_ERR "Powering off has failed\n");
  204. }
  205. return;
  206. }
  207. static void msm_power_off(void)
  208. {
  209. /* MSM initiated power off, lower ps_hold */
  210. __msm_power_off(1);
  211. }
  212. static void cpu_power_off(void *data)
  213. {
  214. int rc;
  215. pr_err("PMIC Initiated shutdown %s cpu=%d\n", __func__,
  216. smp_processor_id());
  217. if (smp_processor_id() == 0) {
  218. /*
  219. * PMIC initiated power off, do not lower ps_hold, pmic will
  220. * shut msm down
  221. */
  222. __msm_power_off(0);
  223. pet_watchdog();
  224. pr_err("Calling scm to disable arbiter\n");
  225. /* call secure manager to disable arbiter and never return */
  226. rc = scm_call_atomic1(SCM_SVC_PWR,
  227. SCM_IO_DISABLE_PMIC_ARBITER, 1);
  228. pr_err("SCM returned even when asked to busy loop rc=%d\n", rc);
  229. pr_err("waiting on pmic to shut msm down\n");
  230. }
  231. preempt_disable();
  232. while (1)
  233. ;
  234. }
  235. static irqreturn_t resout_irq_handler(int irq, void *dev_id)
  236. {
  237. pr_warn("%s PMIC Initiated shutdown\n", __func__);
  238. oops_in_progress = 1;
  239. smp_call_function_many(cpu_online_mask, cpu_power_off, NULL, 0);
  240. if (smp_processor_id() == 0)
  241. cpu_power_off(NULL);
  242. preempt_disable();
  243. while (1)
  244. ;
  245. return IRQ_HANDLED;
  246. }
  247. #if defined CONFIG_ID_BYPASS_SBL
  248. extern int otg_attached;
  249. #endif
  250. static void msm_restart_prepare(const char *cmd)
  251. {
  252. unsigned long value;
  253. unsigned int warm_reboot_set = 0;
  254. #ifdef CONFIG_RESTART_REASON_DDR
  255. unsigned int save_restart_reason;
  256. #endif
  257. #ifndef CONFIG_SEC_DEBUG
  258. #ifdef CONFIG_MSM_DLOAD_MODE
  259. /* This looks like a normal reboot at this point. */
  260. set_dload_mode(0);
  261. /* Write download mode flags if we're panic'ing */
  262. set_dload_mode(in_panic);
  263. /* Write download mode flags if restart_mode says so */
  264. if (restart_mode == RESTART_DLOAD)
  265. set_dload_mode(1);
  266. /* Kill download mode if master-kill switch is set */
  267. if (!download_mode)
  268. set_dload_mode(0);
  269. #endif
  270. #endif
  271. #ifdef CONFIG_SEC_DEBUG_LOW_LOG
  272. #ifdef CONFIG_MSM_DLOAD_MODE
  273. #ifdef CONFIG_SEC_DEBUG
  274. if (sec_debug_is_enabled()
  275. && ((restart_mode == RESTART_DLOAD) || in_panic))
  276. set_dload_mode(1);
  277. else
  278. set_dload_mode(0);
  279. #else
  280. set_dload_mode(0);
  281. set_dload_mode(in_panic);
  282. if (restart_mode == RESTART_DLOAD)
  283. set_dload_mode(1);
  284. #endif
  285. #endif
  286. #endif
  287. printk(KERN_NOTICE "Going down for restart now\n");
  288. warm_reboot_set = 0;
  289. #ifdef CONFIG_SEC_DEBUG
  290. if (!restart_reason)
  291. restart_reason = ioremap_nocache((unsigned long)(MSM_IMEM_BASE \
  292. + RESTART_REASON_ADDR), SZ_4K);
  293. #endif
  294. if (cmd != NULL) {
  295. printk(KERN_NOTICE " Reboot cmd=%s\n",cmd);
  296. if (!strncmp(cmd, "bootloader", 10)) {
  297. __raw_writel(0x77665500, restart_reason);
  298. warm_reboot_set = 1;
  299. } else if (!strncmp(cmd, "recovery", 8)) {
  300. __raw_writel(0x77665502, restart_reason);
  301. } else if (!strcmp(cmd, "rtc")) {
  302. __raw_writel(0x77665503, restart_reason);
  303. } else if (!strncmp(cmd, "oem-", 4)) {
  304. unsigned long code;
  305. code = simple_strtoul(cmd + 4, NULL, 16) & 0xff;
  306. __raw_writel(0x6f656d00 | code, restart_reason);
  307. #ifdef CONFIG_SEC_DEBUG
  308. } else if (!strncmp(cmd, "sec_debug_hw_reset", 18)) {
  309. __raw_writel(0x776655ee, restart_reason);
  310. warm_reboot_set = 1;
  311. #endif
  312. } else if (!strncmp(cmd, "download", 8)) {
  313. __raw_writel(0x12345671, restart_reason);
  314. warm_reboot_set = 1;
  315. } else if (!strncmp(cmd, "sud", 3)) {
  316. __raw_writel(0xabcf0000 | (cmd[3] - '0'),
  317. restart_reason);
  318. } else if (!strncmp(cmd, "debug", 5)
  319. && !kstrtoul(cmd + 5, 0, &value)) {
  320. __raw_writel(0xabcd0000 | value, restart_reason);
  321. } else if (!strncmp(cmd, "cpdebug", 7) /* set cp debug level */
  322. && !kstrtoul(cmd + 7, 0, &value)) {
  323. __raw_writel(0xfedc0000 | value, restart_reason);
  324. #if defined(CONFIG_SWITCH_DUAL_MODEM) || defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  325. } else if (!strncmp(cmd, "swsel", 5) /* set switch value */
  326. && !kstrtoul(cmd + 5, 0, &value)) {
  327. __raw_writel(0xabce0000 | value, restart_reason);
  328. #endif
  329. } else if (!strncmp(cmd, "nvbackup", 8)) {
  330. __raw_writel(0x77665511, restart_reason);
  331. warm_reboot_set = 1;
  332. } else if (!strncmp(cmd, "nvrestore", 9)) {
  333. __raw_writel(0x77665512, restart_reason);
  334. warm_reboot_set = 1;
  335. } else if (!strncmp(cmd, "nverase", 7)) {
  336. __raw_writel(0x77665514, restart_reason);
  337. warm_reboot_set = 1;
  338. } else if (!strncmp(cmd, "nvrecovery", 10)) {
  339. __raw_writel(0x77665515, restart_reason);
  340. warm_reboot_set = 1;
  341. #if 0
  342. } else if (!strncmp(cmd, "edl", 3)) {
  343. enable_emergency_dload_mode();
  344. warm_reboot_set = 1;
  345. #endif
  346. } else if (strlen(cmd) == 0) {
  347. printk(KERN_NOTICE "%s : value of cmd is NULL.\n", __func__);
  348. __raw_writel(0x12345678, restart_reason);
  349. #ifdef CONFIG_SEC_PERIPHERAL_SECURE_CHK
  350. } else if (!strncmp(cmd, "peripheral_hw_reset", 19)) {
  351. __raw_writel(0x77665507, restart_reason);
  352. warm_reboot_set = 1;
  353. #endif
  354. } else {
  355. #if defined CONFIG_ID_BYPASS_SBL
  356. if(otg_attached)
  357. {
  358. __raw_writel(0x77665509, restart_reason);
  359. warm_reboot_set = 1;
  360. }
  361. else
  362. #endif
  363. __raw_writel(0x12345678, restart_reason);
  364. }
  365. printk(KERN_NOTICE "%s : restart_reason = 0x%x\n",
  366. __func__, __raw_readl(restart_reason));
  367. }
  368. else {
  369. printk(KERN_NOTICE "%s: clear reset flag\n", __func__);
  370. warm_reboot_set = 1;
  371. #ifdef CONFIG_USER_RESET_DEBUG
  372. if(poweroff_charging) {
  373. reboot_cause = MSM_IMEM_BASE + 0x66C;
  374. __raw_writel(RESET_CAUSE_LPM_REBOOT, reboot_cause);
  375. }
  376. #endif
  377. __raw_writel(0x12345678, restart_reason);
  378. }
  379. printk(KERN_NOTICE "%s : restart_reason = 0x%x\n",
  380. __func__, __raw_readl(restart_reason));
  381. printk(KERN_NOTICE "%s : warm_reboot_set = %d\n",
  382. __func__, warm_reboot_set);
  383. #ifdef CONFIG_RESTART_REASON_SEC_PARAM
  384. //fixme : Enabling Hard reset
  385. /* Memory contents will be lost when when PMIC is configured for HARD RESET */
  386. if (warm_reboot_set == 1) {
  387. qpnp_pon_system_pwr_off(PON_POWER_OFF_WARM_RESET);
  388. printk(KERN_NOTICE "Configure as WARM RESET\n");
  389. }
  390. else {
  391. qpnp_pon_system_pwr_off(PON_POWER_OFF_HARD_RESET);
  392. printk(KERN_NOTICE "Configure as HARD RESET\n");
  393. }
  394. #else
  395. qpnp_pon_system_pwr_off(PON_POWER_OFF_WARM_RESET);
  396. #endif
  397. #ifdef CONFIG_RESTART_REASON_DDR
  398. if(restart_reason_ddr_address) {
  399. save_restart_reason = __raw_readl(restart_reason);
  400. /* Writting NORMAL BOOT magic number to DDR address*/
  401. __raw_writel(save_restart_reason, restart_reason_ddr_address);
  402. printk(KERN_NOTICE "%s: writting 0x%x to DDR restart reason address \n", __func__,__raw_readl(restart_reason_ddr_address));
  403. }
  404. #endif
  405. flush_cache_all();
  406. outer_flush_all();
  407. }
  408. void msm_restart(char mode, const char *cmd)
  409. {
  410. printk(KERN_NOTICE "%s: Going down for restart now\n", __func__);
  411. msm_restart_prepare(cmd);
  412. if (!use_restart_v2()) {
  413. __raw_writel(0, msm_tmr0_base + WDT0_EN);
  414. if (!(machine_is_msm8x60_fusion() ||
  415. machine_is_msm8x60_fusn_ffa())) {
  416. mb();
  417. /* Actually reset the chip */
  418. __raw_writel(0, PSHOLD_CTL_SU);
  419. mdelay(5000);
  420. pr_notice("PS_HOLD didn't work, falling back to watchdog\n");
  421. }
  422. __raw_writel(1, msm_tmr0_base + WDT0_RST);
  423. __raw_writel(5*0x31F3, msm_tmr0_base + WDT0_BARK_TIME);
  424. __raw_writel(0x31F3, msm_tmr0_base + WDT0_BITE_TIME);
  425. __raw_writel(1, msm_tmr0_base + WDT0_EN);
  426. } else {
  427. /* Needed to bypass debug image on some chips */
  428. msm_disable_wdog_debug();
  429. halt_spmi_pmic_arbiter();
  430. __raw_writel(0, MSM_MPM2_PSHOLD_BASE);
  431. }
  432. mdelay(10000);
  433. printk(KERN_ERR "Restarting has failed\n");
  434. }
  435. #ifdef CONFIG_SEC_DEBUG
  436. static int dload_mode_normal_reboot_handler(struct notifier_block *nb,
  437. unsigned long l, void *p)
  438. {
  439. set_dload_mode(0);
  440. return 0;
  441. }
  442. static struct notifier_block dload_reboot_block = {
  443. .notifier_call = dload_mode_normal_reboot_handler
  444. };
  445. #endif
  446. static int __init msm_pmic_restart_init(void)
  447. {
  448. int rc;
  449. if (use_restart_v2())
  450. return 0;
  451. if (pmic_reset_irq != 0) {
  452. rc = request_any_context_irq(pmic_reset_irq,
  453. resout_irq_handler, IRQF_TRIGGER_HIGH,
  454. "restart_from_pmic", NULL);
  455. if (rc < 0)
  456. pr_err("pmic restart irq fail rc = %d\n", rc);
  457. } else {
  458. pr_warn("no pmic restart interrupt specified\n");
  459. }
  460. return 0;
  461. }
  462. late_initcall(msm_pmic_restart_init);
  463. static int __init msm_restart_init(void)
  464. {
  465. #ifdef CONFIG_MSM_DLOAD_MODE
  466. atomic_notifier_chain_register(&panic_notifier_list, &panic_blk);
  467. dload_mode_addr = MSM_IMEM_BASE + DLOAD_MODE_ADDR;
  468. #ifdef CONFIG_SEC_DEBUG
  469. register_reboot_notifier(&dload_reboot_block);
  470. #endif
  471. #ifdef CONFIG_SEC_DEBUG_LOW_LOG
  472. if (!sec_debug_is_enabled()) {
  473. set_dload_mode(0);
  474. } else
  475. #endif
  476. emergency_dload_mode_addr = MSM_IMEM_BASE +
  477. EMERGENCY_DLOAD_MODE_ADDR;
  478. set_dload_mode(download_mode);
  479. #endif
  480. msm_tmr0_base = msm_timer_get_timer0_base();
  481. #ifndef CONFIG_SEC_DEBUG
  482. restart_reason = MSM_IMEM_BASE + RESTART_REASON_ADDR;
  483. #endif
  484. pm_power_off = msm_power_off;
  485. if (scm_is_call_available(SCM_SVC_PWR, SCM_IO_DISABLE_PMIC_ARBITER) > 0)
  486. scm_pmic_arbiter_disable_supported = true;
  487. return 0;
  488. }
  489. early_initcall(msm_restart_init);