imx_sc_wdt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #define SC_TIMER_ERR_BUSY 10
  33. static bool nowayout = WATCHDOG_NOWAYOUT;
  34. module_param(nowayout, bool, 0000);
  35. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  36. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  37. struct imx_sc_wdt_device {
  38. struct watchdog_device wdd;
  39. struct notifier_block wdt_notifier;
  40. };
  41. static int imx_sc_wdt_ping(struct watchdog_device *wdog)
  42. {
  43. struct arm_smccc_res res;
  44. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_PING_WDOG,
  45. 0, 0, 0, 0, 0, 0, &res);
  46. return 0;
  47. }
  48. static bool imx_sc_wdt_is_running(void)
  49. {
  50. struct arm_smccc_res res;
  51. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
  52. 0, 0, 0, 0, 0, 0, &res);
  53. /* Already enabled (SC_TIMER_ERR_BUSY)? */
  54. if (res.a0 == SC_TIMER_ERR_BUSY)
  55. return true;
  56. /* Undo only if that was us who has (successfully) enabled the WDT */
  57. if (!res.a0)
  58. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
  59. 0, 0, 0, 0, 0, 0, &res);
  60. return false;
  61. }
  62. static int imx_sc_wdt_start(struct watchdog_device *wdog)
  63. {
  64. struct arm_smccc_res res;
  65. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_START_WDOG,
  66. 0, 0, 0, 0, 0, 0, &res);
  67. /* Ignore if already enabled(SC_TIMER_ERR_BUSY) */
  68. if (res.a0 && res.a0 != SC_TIMER_ERR_BUSY)
  69. return -EACCES;
  70. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_WDOG_ACT,
  71. SC_TIMER_WDOG_ACTION_PARTITION,
  72. 0, 0, 0, 0, 0, &res);
  73. return res.a0 ? -EACCES : 0;
  74. }
  75. static int imx_sc_wdt_stop(struct watchdog_device *wdog)
  76. {
  77. struct arm_smccc_res res;
  78. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_STOP_WDOG,
  79. 0, 0, 0, 0, 0, 0, &res);
  80. return res.a0 ? -EACCES : 0;
  81. }
  82. static int imx_sc_wdt_set_timeout(struct watchdog_device *wdog,
  83. unsigned int timeout)
  84. {
  85. struct arm_smccc_res res;
  86. wdog->timeout = timeout;
  87. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_TIMEOUT_WDOG,
  88. timeout * 1000, 0, 0, 0, 0, 0, &res);
  89. return res.a0 ? -EACCES : 0;
  90. }
  91. static int imx_sc_wdt_set_pretimeout(struct watchdog_device *wdog,
  92. unsigned int pretimeout)
  93. {
  94. struct arm_smccc_res res;
  95. /*
  96. * SCU firmware calculates pretimeout based on current time
  97. * stamp instead of watchdog timeout stamp, need to convert
  98. * the pretimeout to SCU firmware's timeout value.
  99. */
  100. arm_smccc_smc(IMX_SIP_TIMER, IMX_SIP_TIMER_SET_PRETIME_WDOG,
  101. (wdog->timeout - pretimeout) * 1000, 0, 0, 0,
  102. 0, 0, &res);
  103. if (res.a0)
  104. return -EACCES;
  105. wdog->pretimeout = pretimeout;
  106. return 0;
  107. }
  108. static int imx_sc_wdt_notify(struct notifier_block *nb,
  109. unsigned long event, void *group)
  110. {
  111. struct imx_sc_wdt_device *imx_sc_wdd =
  112. container_of(nb,
  113. struct imx_sc_wdt_device,
  114. wdt_notifier);
  115. if (event & SC_IRQ_WDOG &&
  116. *(u8 *)group == SC_IRQ_GROUP_WDOG)
  117. watchdog_notify_pretimeout(&imx_sc_wdd->wdd);
  118. return 0;
  119. }
  120. static void imx_sc_wdt_action(void *data)
  121. {
  122. struct notifier_block *wdt_notifier = data;
  123. imx_scu_irq_unregister_notifier(wdt_notifier);
  124. imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
  125. SC_IRQ_WDOG,
  126. false);
  127. }
  128. static const struct watchdog_ops imx_sc_wdt_ops = {
  129. .owner = THIS_MODULE,
  130. .start = imx_sc_wdt_start,
  131. .stop = imx_sc_wdt_stop,
  132. .ping = imx_sc_wdt_ping,
  133. .set_timeout = imx_sc_wdt_set_timeout,
  134. .set_pretimeout = imx_sc_wdt_set_pretimeout,
  135. };
  136. static struct watchdog_info imx_sc_wdt_info = {
  137. .identity = "i.MX SC watchdog timer",
  138. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  139. WDIOF_MAGICCLOSE,
  140. };
  141. static int imx_sc_wdt_probe(struct platform_device *pdev)
  142. {
  143. struct imx_sc_wdt_device *imx_sc_wdd;
  144. struct watchdog_device *wdog;
  145. struct device *dev = &pdev->dev;
  146. int ret;
  147. imx_sc_wdd = devm_kzalloc(dev, sizeof(*imx_sc_wdd), GFP_KERNEL);
  148. if (!imx_sc_wdd)
  149. return -ENOMEM;
  150. platform_set_drvdata(pdev, imx_sc_wdd);
  151. wdog = &imx_sc_wdd->wdd;
  152. wdog->info = &imx_sc_wdt_info;
  153. wdog->ops = &imx_sc_wdt_ops;
  154. wdog->min_timeout = 1;
  155. wdog->max_timeout = MAX_TIMEOUT;
  156. wdog->parent = dev;
  157. wdog->timeout = DEFAULT_TIMEOUT;
  158. watchdog_init_timeout(wdog, 0, dev);
  159. ret = imx_sc_wdt_set_timeout(wdog, wdog->timeout);
  160. if (ret)
  161. return ret;
  162. if (imx_sc_wdt_is_running())
  163. set_bit(WDOG_HW_RUNNING, &wdog->status);
  164. watchdog_stop_on_reboot(wdog);
  165. watchdog_stop_on_unregister(wdog);
  166. ret = imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
  167. SC_IRQ_WDOG,
  168. true);
  169. if (ret) {
  170. dev_warn(dev, "Enable irq failed, pretimeout NOT supported\n");
  171. goto register_device;
  172. }
  173. imx_sc_wdd->wdt_notifier.notifier_call = imx_sc_wdt_notify;
  174. ret = imx_scu_irq_register_notifier(&imx_sc_wdd->wdt_notifier);
  175. if (ret) {
  176. imx_scu_irq_group_enable(SC_IRQ_GROUP_WDOG,
  177. SC_IRQ_WDOG,
  178. false);
  179. dev_warn(dev,
  180. "Register irq notifier failed, pretimeout NOT supported\n");
  181. goto register_device;
  182. }
  183. ret = devm_add_action_or_reset(dev, imx_sc_wdt_action,
  184. &imx_sc_wdd->wdt_notifier);
  185. if (!ret)
  186. imx_sc_wdt_info.options |= WDIOF_PRETIMEOUT;
  187. else
  188. dev_warn(dev, "Add action failed, pretimeout NOT supported\n");
  189. register_device:
  190. return devm_watchdog_register_device(dev, wdog);
  191. }
  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. },
  203. };
  204. module_platform_driver(imx_sc_wdt_driver);
  205. MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
  206. MODULE_DESCRIPTION("NXP i.MX system controller watchdog driver");
  207. MODULE_LICENSE("GPL v2");