imx_sc_wdt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018-2019 NXP.
  4. */
  5. #include <linux/arm-smccc.h>
  6. #include <linux/firmware/imx/sci.h>
  7. #include <linux/io.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reboot.h>
  15. #include <linux/watchdog.h>
  16. #define DEFAULT_TIMEOUT 60
  17. /*
  18. * Software timer tick implemented in scfw side, support 10ms to 0xffffffff ms
  19. * in theory, but for normal case, 1s~128s is enough, you can change this max
  20. * value in case it's not enough.
  21. */
  22. #define MAX_TIMEOUT 128
  23. #define IMX_SIP_TIMER 0xC2000002
  24. #define IMX_SIP_TIMER_START_WDOG 0x01
  25. #define IMX_SIP_TIMER_STOP_WDOG 0x02
  26. #define IMX_SIP_TIMER_SET_WDOG_ACT 0x03
  27. #define IMX_SIP_TIMER_PING_WDOG 0x04
  28. #define IMX_SIP_TIMER_SET_TIMEOUT_WDOG 0x05
  29. #define IMX_SIP_TIMER_GET_WDOG_STAT 0x06
  30. #define IMX_SIP_TIMER_SET_PRETIME_WDOG 0x07
  31. #define SC_TIMER_WDOG_ACTION_PARTITION 0
  32. #define SC_IRQ_WDOG 1
  33. #define SC_IRQ_GROUP_WDOG 1
  34. static bool nowayout = WATCHDOG_NOWAYOUT;
  35. module_param(nowayout, bool, 0000);
  36. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  37. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  38. struct imx_sc_wdt_device {
  39. struct watchdog_device wdd;
  40. struct notifier_block wdt_notifier;
  41. };
  42. static int imx_sc_wdt_ping(struct watchdog_device *wdog)
  43. {
  44. struct arm_smccc_res res;
  45. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG,
  46. 0, 0, 0, 0, 0, 0, &res);
  47. return 0;
  48. }
  49. static int imx_sc_wdt_start(struct watchdog_device *wdog)
  50. {
  51. struct arm_smccc_res res;
  52. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
  53. 0, 0, 0, 0, 0, 0, &res);
  54. if (res.a0)
  55. return -EACCES;
  56. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT,
  57. SC_TIMER_WDOG_ACTION_PARTITION,
  58. 0, 0, 0, 0, 0, &res);
  59. return res.a0 ? -EACCES : 0;
  60. }
  61. static int imx_sc_wdt_stop(struct watchdog_device *wdog)
  62. {
  63. struct arm_smccc_res res;
  64. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
  65. 0, 0, 0, 0, 0, 0, &res);
  66. return res.a0 ? -EACCES : 0;
  67. }
  68. static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog,
  69. unsigned int timeout)
  70. {
  71. struct arm_smccc_res res;
  72. wdog->timeout = timeout;
  73. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG,
  74. timeout * 1000, 0, 0, 0, 0, 0, &res);
  75. return res.a0 ? -EACCES : 0;
  76. }
  77. static int imx_sc_wdt_set_pretimeout(struct watchdog_device *wdog,
  78. unsigned int pretimeout)
  79. {
  80. struct arm_smccc_res res;
  81. /*
  82. * SCU firmware calculates pretimeout based on current time
  83. * stamp instead of watchdog timeout stamp, need to convert
  84. * the pretimeout to SCU firmware's timeout value.
  85. */
  86. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_PRETIME_WDOG,
  87. (wdog->timeout - pretimeout) * 1000, 0, 0, 0,
  88. 0, 0, &res);
  89. if (res.a0)
  90. return -EACCES;
  91. wdog->pretimeout = pretimeout;
  92. return 0;
  93. }
  94. static int imx_sc_wdt_notify(struct notifier_block *nb,
  95. unsigned long event, void *group)
  96. {
  97. struct imx_sc_wdt_device *imx_sc_wdd =
  98. container_of(nb,
  99. struct imx_sc_wdt_device,
  100. wdt_notifier);
  101. if (event & SC_IRQ_WDOG &&
  102. *(u8 *)group == SC_IRQ_GROUP_WDOG)
  103. watchdog_notify_pretimeout(&imx_sc_wdd->wdd);
  104. return 0;
  105. }
  106. static void imx_sc_wdt_action(void *data)
  107. {
  108. struct notifier_block *wdt_notifier = data;
  109. imx_scu_irq_unregister_notifier(wdt_notifier);
  110. imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
  111. SC_IRQ_WDOG,
  112. false);
  113. }
  114. static const struct watchdog_ops imx_sc_wdt_ops = {
  115. .owner = THIS_MODULE,
  116. .start = imx_sc_wdt_start,
  117. .stop = imx_sc_wdt_stop,
  118. .ping = imx_sc_wdt_ping,
  119. .set_timeout = imx_sc_wdt_set_timeout,
  120. .set_pretimeout = imx_sc_wdt_set_pretimeout,
  121. };
  122. static struct watchdog_info imx_sc_wdt_info = {
  123. .identity = "i.MX SC watchdog timer",
  124. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  125. WDIOF_MAGICCLOSE,
  126. };
  127. static int imx_sc_wdt_probe(struct platform_device *pdev)
  128. {
  129. struct imx_sc_wdt_device *imx_sc_wdd;
  130. struct watchdog_device *wdog;
  131. struct device *dev = &pdev->dev;
  132. int ret;
  133. imx_sc_wdd = devm_kzalloc(dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
  134. if (!imx_sc_wdd)
  135. return -ENOMEM;
  136. platform_set_drvdata(pdev, imx_sc_wdd);
  137. wdog = &imx_sc_wdd->wdd;
  138. wdog->info = &imx_sc_wdt_info;
  139. wdog->ops = &imx_sc_wdt_ops;
  140. wdog->min_timeout = 1;
  141. wdog->max_timeout = MAX_TIMEOUT;
  142. wdog->parent = dev;
  143. wdog->timeout = DEFAULT_TIMEOUT;
  144. watchdog_init_timeout(wdog, 0, dev);
  145. ret = imx_sc_wdt_set_timeout(wdog, wdog->timeout);
  146. if (ret)
  147. return ret;
  148. watchdog_stop_on_reboot(wdog);
  149. watchdog_stop_on_unregister(wdog);
  150. ret = imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
  151. SC_IRQ_WDOG,
  152. true);
  153. if (ret) {
  154. dev_warn(dev, "Enable irq failed, pretimeout NOT supported\n");
  155. goto register_device;
  156. }
  157. imx_sc_wdd->wdt_notifier.notifier_call = imx_sc_wdt_notify;
  158. ret = imx_scu_irq_register_notifier(&imx_sc_wdd->wdt_notifier);
  159. if (ret) {
  160. imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
  161. SC_IRQ_WDOG,
  162. false);
  163. dev_warn(dev,
  164. "Register irq notifier failed, pretimeout NOT supported\n");
  165. goto register_device;
  166. }
  167. ret = devm_add_action_or_reset(dev, imx_sc_wdt_action,
  168. &imx_sc_wdd->wdt_notifier);
  169. if (!ret)
  170. imx_sc_wdt_info.options |= WDIOF_PRETIMEOUT;
  171. else
  172. dev_warn(dev, "Add action failed, pretimeout NOT supported\n");
  173. register_device:
  174. return devm_watchdog_register_device(dev, wdog);
  175. }
  176. static int __maybe_unused imx_sc_wdt_suspend(struct device *dev)
  177. {
  178. struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev);
  179. if (watchdog_active(&imx_sc_wdd->wdd))
  180. imx_sc_wdt_stop(&imx_sc_wdd->wdd);
  181. return 0;
  182. }
  183. static int __maybe_unused imx_sc_wdt_resume(struct device *dev)
  184. {
  185. struct imx_sc_wdt_device *imx_sc_wdd = dev_get_drvdata(dev);
  186. if (watchdog_active(&imx_sc_wdd->wdd))
  187. imx_sc_wdt_start(&imx_sc_wdd->wdd);
  188. return 0;
  189. }
  190. static SIMPLE_DEV_PM_OPS(imx_sc_wdt_pm_ops,
  191. imx_sc_wdt_suspend, imx_sc_wdt_resume);
  192. static const struct of_device_id imx_sc_wdt_dt_ids[] = {
  193. { .compatible = "fsl,imx-sc-wdt", },
  194. { /* sentinel */ }
  195. };
  196. MODULE_DEVICE_TABLE(of, imx_sc_wdt_dt_ids);
  197. static struct platform_driver imx_sc_wdt_driver = {
  198. .probe = imx_sc_wdt_probe,
  199. .driver = {
  200. .name = "imx-sc-wdt",
  201. .of_match_table = imx_sc_wdt_dt_ids,
  202. .pm = &imx_sc_wdt_pm_ops,
  203. },
  204. };
  205. module_platform_driver(imx_sc_wdt_driver);
  206. MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
  207. MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
  208. MODULE_LICENSE("GPL v2");