cgbc_wdt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Congatec Board Controller watchdog driver
  4. *
  5. * Copyright (C) 2024 Bootlin
  6. * Author: Thomas Richard <thomas.richard@bootlin.com>
  7. */
  8. #include <linux/build_bug.h>
  9. #include <linux/device.h>
  10. #include <linux/limits.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/mfd/cgbc.h>
  15. #define CGBC_WDT_CMD_TRIGGER 0x27
  16. #define CGBC_WDT_CMD_INIT 0x28
  17. #define CGBC_WDT_DISABLE 0x00
  18. #define CGBC_WDT_MODE_SINGLE_EVENT 0x02
  19. #define CGBC_WDT_MIN_TIMEOUT 1
  20. #define CGBC_WDT_MAX_TIMEOUT ((U32_MAX >> 8) / 1000)
  21. #define CGBC_WDT_DEFAULT_TIMEOUT 30
  22. #define CGBC_WDT_DEFAULT_PRETIMEOUT 0
  23. enum action {
  24. ACTION_INT = 0,
  25. ACTION_SMI,
  26. ACTION_RESET,
  27. ACTION_BUTTON,
  28. };
  29. static unsigned int timeout;
  30. module_param(timeout, uint, 0);
  31. MODULE_PARM_DESC(timeout,
  32. "Watchdog timeout in seconds. (>=0, default="
  33. __MODULE_STRING(CGBC_WDT_DEFAULT_TIMEOUT) ")");
  34. static unsigned int pretimeout = CGBC_WDT_DEFAULT_PRETIMEOUT;
  35. module_param(pretimeout, uint, 0);
  36. MODULE_PARM_DESC(pretimeout,
  37. "Watchdog pretimeout in seconds. (>=0, default="
  38. __MODULE_STRING(CGBC_WDT_DEFAULT_PRETIMEOUT) ")");
  39. static bool nowayout = WATCHDOG_NOWAYOUT;
  40. module_param(nowayout, bool, 0);
  41. MODULE_PARM_DESC(nowayout,
  42. "Watchdog cannot be stopped once started (default="
  43. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  44. struct cgbc_wdt_data {
  45. struct cgbc_device_data *cgbc;
  46. struct watchdog_device wdd;
  47. };
  48. struct cgbc_wdt_cmd_cfg {
  49. u8 cmd;
  50. u8 mode;
  51. u8 action;
  52. u8 timeout1[3];
  53. u8 timeout2[3];
  54. u8 reserved[3];
  55. u8 delay[3];
  56. } __packed;
  57. static_assert(sizeof(struct cgbc_wdt_cmd_cfg) == 15);
  58. static int cgbc_wdt_start(struct watchdog_device *wdd)
  59. {
  60. struct cgbc_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  61. struct cgbc_device_data *cgbc = wdt_data->cgbc;
  62. unsigned int timeout1 = (wdd->timeout - wdd->pretimeout) * 1000;
  63. unsigned int timeout2 = wdd->pretimeout * 1000;
  64. u8 action;
  65. struct cgbc_wdt_cmd_cfg cmd_start = {
  66. .cmd = CGBC_WDT_CMD_INIT,
  67. .mode = CGBC_WDT_MODE_SINGLE_EVENT,
  68. .timeout1[0] = (u8)timeout1,
  69. .timeout1[1] = (u8)(timeout1 >> 8),
  70. .timeout1[2] = (u8)(timeout1 >> 16),
  71. .timeout2[0] = (u8)timeout2,
  72. .timeout2[1] = (u8)(timeout2 >> 8),
  73. .timeout2[2] = (u8)(timeout2 >> 16),
  74. };
  75. if (wdd->pretimeout) {
  76. action = 2;
  77. action |= ACTION_SMI << 2;
  78. action |= ACTION_RESET << 4;
  79. } else {
  80. action = 1;
  81. action |= ACTION_RESET << 2;
  82. }
  83. cmd_start.action = action;
  84. return cgbc_command(cgbc, &cmd_start, sizeof(cmd_start), NULL, 0, NULL);
  85. }
  86. static int cgbc_wdt_stop(struct watchdog_device *wdd)
  87. {
  88. struct cgbc_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  89. struct cgbc_device_data *cgbc = wdt_data->cgbc;
  90. struct cgbc_wdt_cmd_cfg cmd_stop = {
  91. .cmd = CGBC_WDT_CMD_INIT,
  92. .mode = CGBC_WDT_DISABLE,
  93. };
  94. return cgbc_command(cgbc, &cmd_stop, sizeof(cmd_stop), NULL, 0, NULL);
  95. }
  96. static int cgbc_wdt_keepalive(struct watchdog_device *wdd)
  97. {
  98. struct cgbc_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  99. struct cgbc_device_data *cgbc = wdt_data->cgbc;
  100. u8 cmd_ping = CGBC_WDT_CMD_TRIGGER;
  101. return cgbc_command(cgbc, &cmd_ping, sizeof(cmd_ping), NULL, 0, NULL);
  102. }
  103. static int cgbc_wdt_set_pretimeout(struct watchdog_device *wdd,
  104. unsigned int pretimeout)
  105. {
  106. wdd->pretimeout = pretimeout;
  107. if (watchdog_active(wdd))
  108. return cgbc_wdt_start(wdd);
  109. return 0;
  110. }
  111. static int cgbc_wdt_set_timeout(struct watchdog_device *wdd,
  112. unsigned int timeout)
  113. {
  114. if (timeout < wdd->pretimeout)
  115. wdd->pretimeout = 0;
  116. wdd->timeout = timeout;
  117. if (watchdog_active(wdd))
  118. return cgbc_wdt_start(wdd);
  119. return 0;
  120. }
  121. static const struct watchdog_info cgbc_wdt_info = {
  122. .identity = "CGBC Watchdog",
  123. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  124. WDIOF_MAGICCLOSE | WDIOF_PRETIMEOUT
  125. };
  126. static const struct watchdog_ops cgbc_wdt_ops = {
  127. .owner = THIS_MODULE,
  128. .start = cgbc_wdt_start,
  129. .stop = cgbc_wdt_stop,
  130. .ping = cgbc_wdt_keepalive,
  131. .set_timeout = cgbc_wdt_set_timeout,
  132. .set_pretimeout = cgbc_wdt_set_pretimeout,
  133. };
  134. static int cgbc_wdt_probe(struct platform_device *pdev)
  135. {
  136. struct cgbc_device_data *cgbc = dev_get_drvdata(pdev->dev.parent);
  137. struct device *dev = &pdev->dev;
  138. struct cgbc_wdt_data *wdt_data;
  139. struct watchdog_device *wdd;
  140. wdt_data = devm_kzalloc(dev, sizeof(*wdt_data), GFP_KERNEL);
  141. if (!wdt_data)
  142. return -ENOMEM;
  143. wdt_data->cgbc = cgbc;
  144. wdd = &wdt_data->wdd;
  145. wdd->parent = dev;
  146. wdd->info = &cgbc_wdt_info;
  147. wdd->ops = &cgbc_wdt_ops;
  148. wdd->max_timeout = CGBC_WDT_MAX_TIMEOUT;
  149. wdd->min_timeout = CGBC_WDT_MIN_TIMEOUT;
  150. watchdog_set_drvdata(wdd, wdt_data);
  151. watchdog_set_nowayout(wdd, nowayout);
  152. wdd->timeout = CGBC_WDT_DEFAULT_TIMEOUT;
  153. watchdog_init_timeout(wdd, timeout, dev);
  154. cgbc_wdt_set_pretimeout(wdd, pretimeout);
  155. platform_set_drvdata(pdev, wdt_data);
  156. watchdog_stop_on_reboot(wdd);
  157. watchdog_stop_on_unregister(wdd);
  158. return devm_watchdog_register_device(dev, wdd);
  159. }
  160. static struct platform_driver cgbc_wdt_driver = {
  161. .driver = {
  162. .name = "cgbc-wdt",
  163. },
  164. .probe = cgbc_wdt_probe,
  165. };
  166. module_platform_driver(cgbc_wdt_driver);
  167. MODULE_DESCRIPTION("Congatec Board Controller Watchdog Driver");
  168. MODULE_AUTHOR("Thomas Richard <thomas.richard@bootlin.com>");
  169. MODULE_LICENSE("GPL");