rave-sp-wdt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for watchdog aspect of for Zodiac Inflight Innovations RAVE
  4. * Supervisory Processor(SP) MCU
  5. *
  6. * Copyright (C) 2017 Zodiac Inflight Innovation
  7. *
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mfd/rave-sp.h>
  12. #include <linux/module.h>
  13. #include <linux/nvmem-consumer.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reboot.h>
  17. #include <linux/slab.h>
  18. #include <linux/watchdog.h>
  19. enum {
  20. RAVE_SP_RESET_BYTE = 1,
  21. RAVE_SP_RESET_REASON_NORMAL = 0,
  22. RAVE_SP_RESET_DELAY_MS = 500,
  23. };
  24. /**
  25. * struct rave_sp_wdt_variant - RAVE SP watchdog variant
  26. *
  27. * @max_timeout: Largest possible watchdog timeout setting
  28. * @min_timeout: Smallest possible watchdog timeout setting
  29. *
  30. * @configure: Function to send configuration command
  31. * @restart: Function to send "restart" command
  32. */
  33. struct rave_sp_wdt_variant {
  34. unsigned int max_timeout;
  35. unsigned int min_timeout;
  36. int (*configure)(struct watchdog_device *, bool);
  37. int (*restart)(struct watchdog_device *);
  38. };
  39. /**
  40. * struct rave_sp_wdt - RAVE SP watchdog
  41. *
  42. * @wdd: Underlying watchdog device
  43. * @sp: Pointer to parent RAVE SP device
  44. * @variant: Device specific variant information
  45. * @reboot_notifier: Reboot notifier implementing machine reset
  46. */
  47. struct rave_sp_wdt {
  48. struct watchdog_device wdd;
  49. struct rave_sp *sp;
  50. const struct rave_sp_wdt_variant *variant;
  51. struct notifier_block reboot_notifier;
  52. };
  53. static struct rave_sp_wdt *to_rave_sp_wdt(struct watchdog_device *wdd)
  54. {
  55. return container_of(wdd, struct rave_sp_wdt, wdd);
  56. }
  57. static int rave_sp_wdt_exec(struct watchdog_device *wdd, void *data,
  58. size_t data_size)
  59. {
  60. return rave_sp_exec(to_rave_sp_wdt(wdd)->sp,
  61. data, data_size, NULL, 0);
  62. }
  63. static int rave_sp_wdt_legacy_configure(struct watchdog_device *wdd, bool on)
  64. {
  65. u8 cmd[] = {
  66. [0] = RAVE_SP_CMD_SW_WDT,
  67. [1] = 0,
  68. [2] = 0,
  69. [3] = on,
  70. [4] = on ? wdd->timeout : 0,
  71. };
  72. return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
  73. }
  74. static int rave_sp_wdt_rdu_configure(struct watchdog_device *wdd, bool on)
  75. {
  76. u8 cmd[] = {
  77. [0] = RAVE_SP_CMD_SW_WDT,
  78. [1] = 0,
  79. [2] = on,
  80. [3] = (u8)wdd->timeout,
  81. [4] = (u8)(wdd->timeout >> 8),
  82. };
  83. return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
  84. }
  85. /**
  86. * rave_sp_wdt_configure - Configure watchdog device
  87. *
  88. * @wdd: Device to configure
  89. * @on: Desired state of the watchdog timer (ON/OFF)
  90. *
  91. * This function configures two aspects of the watchdog timer:
  92. *
  93. * - Wheither it is ON or OFF
  94. * - Its timeout duration
  95. *
  96. * with first aspect specified via function argument and second via
  97. * the value of 'wdd->timeout'.
  98. */
  99. static int rave_sp_wdt_configure(struct watchdog_device *wdd, bool on)
  100. {
  101. return to_rave_sp_wdt(wdd)->variant->configure(wdd, on);
  102. }
  103. static int rave_sp_wdt_legacy_restart(struct watchdog_device *wdd)
  104. {
  105. u8 cmd[] = {
  106. [0] = RAVE_SP_CMD_RESET,
  107. [1] = 0,
  108. [2] = RAVE_SP_RESET_BYTE
  109. };
  110. return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
  111. }
  112. static int rave_sp_wdt_rdu_restart(struct watchdog_device *wdd)
  113. {
  114. u8 cmd[] = {
  115. [0] = RAVE_SP_CMD_RESET,
  116. [1] = 0,
  117. [2] = RAVE_SP_RESET_BYTE,
  118. [3] = RAVE_SP_RESET_REASON_NORMAL
  119. };
  120. return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
  121. }
  122. static int rave_sp_wdt_reboot_notifier(struct notifier_block *nb,
  123. unsigned long action, void *data)
  124. {
  125. /*
  126. * Restart handler is called in atomic context which means we
  127. * can't communicate to SP via UART. Luckily for use SP will
  128. * wait 500ms before actually resetting us, so we ask it to do
  129. * so here and let the rest of the system go on wrapping
  130. * things up.
  131. */
  132. if (action == SYS_DOWN || action == SYS_HALT) {
  133. struct rave_sp_wdt *sp_wd =
  134. container_of(nb, struct rave_sp_wdt, reboot_notifier);
  135. const int ret = sp_wd->variant->restart(&sp_wd->wdd);
  136. if (ret < 0)
  137. dev_err(sp_wd->wdd.parent,
  138. "Failed to issue restart command (%d)", ret);
  139. return NOTIFY_OK;
  140. }
  141. return NOTIFY_DONE;
  142. }
  143. static int rave_sp_wdt_restart(struct watchdog_device *wdd,
  144. unsigned long action, void *data)
  145. {
  146. /*
  147. * The actual work was done by reboot notifier above. SP
  148. * firmware waits 500 ms before issuing reset, so let's hang
  149. * here for twice that delay and hopefuly we'd never reach
  150. * the return statement.
  151. */
  152. mdelay(2 * RAVE_SP_RESET_DELAY_MS);
  153. return -EIO;
  154. }
  155. static int rave_sp_wdt_start(struct watchdog_device *wdd)
  156. {
  157. int ret;
  158. ret = rave_sp_wdt_configure(wdd, true);
  159. if (!ret)
  160. set_bit(WDOG_HW_RUNNING, &wdd->status);
  161. return ret;
  162. }
  163. static int rave_sp_wdt_stop(struct watchdog_device *wdd)
  164. {
  165. return rave_sp_wdt_configure(wdd, false);
  166. }
  167. static int rave_sp_wdt_set_timeout(struct watchdog_device *wdd,
  168. unsigned int timeout)
  169. {
  170. wdd->timeout = timeout;
  171. return rave_sp_wdt_configure(wdd, watchdog_active(wdd));
  172. }
  173. static int rave_sp_wdt_ping(struct watchdog_device *wdd)
  174. {
  175. u8 cmd[] = {
  176. [0] = RAVE_SP_CMD_PET_WDT,
  177. [1] = 0,
  178. };
  179. return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
  180. }
  181. static const struct watchdog_info rave_sp_wdt_info = {
  182. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  183. .identity = "RAVE SP Watchdog",
  184. };
  185. static const struct watchdog_ops rave_sp_wdt_ops = {
  186. .owner = THIS_MODULE,
  187. .start = rave_sp_wdt_start,
  188. .stop = rave_sp_wdt_stop,
  189. .ping = rave_sp_wdt_ping,
  190. .set_timeout = rave_sp_wdt_set_timeout,
  191. .restart = rave_sp_wdt_restart,
  192. };
  193. static const struct rave_sp_wdt_variant rave_sp_wdt_legacy = {
  194. .max_timeout = 255,
  195. .min_timeout = 1,
  196. .configure = rave_sp_wdt_legacy_configure,
  197. .restart = rave_sp_wdt_legacy_restart,
  198. };
  199. static const struct rave_sp_wdt_variant rave_sp_wdt_rdu = {
  200. .max_timeout = 180,
  201. .min_timeout = 60,
  202. .configure = rave_sp_wdt_rdu_configure,
  203. .restart = rave_sp_wdt_rdu_restart,
  204. };
  205. static const struct of_device_id rave_sp_wdt_of_match[] = {
  206. {
  207. .compatible = "zii,rave-sp-watchdog-legacy",
  208. .data = &rave_sp_wdt_legacy,
  209. },
  210. {
  211. .compatible = "zii,rave-sp-watchdog",
  212. .data = &rave_sp_wdt_rdu,
  213. },
  214. { /* sentinel */ }
  215. };
  216. static int rave_sp_wdt_probe(struct platform_device *pdev)
  217. {
  218. struct device *dev = &pdev->dev;
  219. struct watchdog_device *wdd;
  220. struct rave_sp_wdt *sp_wd;
  221. struct nvmem_cell *cell;
  222. __le16 timeout = 0;
  223. int ret;
  224. sp_wd = devm_kzalloc(dev, sizeof(*sp_wd), GFP_KERNEL);
  225. if (!sp_wd)
  226. return -ENOMEM;
  227. sp_wd->variant = of_device_get_match_data(dev);
  228. sp_wd->sp = dev_get_drvdata(dev->parent);
  229. wdd = &sp_wd->wdd;
  230. wdd->parent = dev;
  231. wdd->info = &rave_sp_wdt_info;
  232. wdd->ops = &rave_sp_wdt_ops;
  233. wdd->min_timeout = sp_wd->variant->min_timeout;
  234. wdd->max_timeout = sp_wd->variant->max_timeout;
  235. wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  236. wdd->timeout = 60;
  237. cell = nvmem_cell_get(dev, "wdt-timeout");
  238. if (!IS_ERR(cell)) {
  239. size_t len;
  240. void *value = nvmem_cell_read(cell, &len);
  241. if (!IS_ERR(value)) {
  242. memcpy(&timeout, value, min(len, sizeof(timeout)));
  243. kfree(value);
  244. }
  245. nvmem_cell_put(cell);
  246. }
  247. watchdog_init_timeout(wdd, le16_to_cpu(timeout), dev);
  248. watchdog_set_restart_priority(wdd, 255);
  249. watchdog_stop_on_unregister(wdd);
  250. sp_wd->reboot_notifier.notifier_call = rave_sp_wdt_reboot_notifier;
  251. ret = devm_register_reboot_notifier(dev, &sp_wd->reboot_notifier);
  252. if (ret) {
  253. dev_err(dev, "Failed to register reboot notifier\n");
  254. return ret;
  255. }
  256. /*
  257. * We don't know if watchdog is running now. To be sure, let's
  258. * start it and depend on watchdog core to ping it
  259. */
  260. wdd->max_hw_heartbeat_ms = wdd->max_timeout * 1000;
  261. ret = rave_sp_wdt_start(wdd);
  262. if (ret) {
  263. dev_err(dev, "Watchdog didn't start\n");
  264. return ret;
  265. }
  266. ret = devm_watchdog_register_device(dev, wdd);
  267. if (ret) {
  268. rave_sp_wdt_stop(wdd);
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. static struct platform_driver rave_sp_wdt_driver = {
  274. .probe = rave_sp_wdt_probe,
  275. .driver = {
  276. .name = KBUILD_MODNAME,
  277. .of_match_table = rave_sp_wdt_of_match,
  278. },
  279. };
  280. module_platform_driver(rave_sp_wdt_driver);
  281. MODULE_DEVICE_TABLE(of, rave_sp_wdt_of_match);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
  284. MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
  285. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  286. MODULE_DESCRIPTION("RAVE SP Watchdog driver");
  287. MODULE_ALIAS("platform:rave-sp-watchdog");