imx_sc_wdt.c 6.3 KB

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