mlx_wdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Mellanox watchdog driver
  4. *
  5. * Copyright (C) 2019 Mellanox Technologies
  6. * Copyright (C) 2019 Michael Shych <mshych@mellanox.com>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/log2.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_data/mlxreg.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/types.h>
  18. #include <linux/watchdog.h>
  19. #define MLXREG_WDT_CLOCK_SCALE 1000
  20. #define MLXREG_WDT_MAX_TIMEOUT_TYPE1 32
  21. #define MLXREG_WDT_MAX_TIMEOUT_TYPE2 255
  22. #define MLXREG_WDT_MIN_TIMEOUT 1
  23. #define MLXREG_WDT_OPTIONS_BASE (WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | \
  24. WDIOF_SETTIMEOUT)
  25. /**
  26. * struct mlxreg_wdt - wd private data:
  27. *
  28. * @wdd: watchdog device;
  29. * @device: basic device;
  30. * @pdata: data received from platform driver;
  31. * @regmap: register map of parent device;
  32. * @timeout: defined timeout in sec.;
  33. * @action_idx: index for direct access to action register;
  34. * @timeout_idx:index for direct access to TO register;
  35. * @tleft_idx: index for direct access to time left register;
  36. * @ping_idx: index for direct access to ping register;
  37. * @reset_idx: index for direct access to reset cause register;
  38. * @wd_type: watchdog HW type;
  39. */
  40. struct mlxreg_wdt {
  41. struct watchdog_device wdd;
  42. struct mlxreg_core_platform_data *pdata;
  43. void *regmap;
  44. int action_idx;
  45. int timeout_idx;
  46. int tleft_idx;
  47. int ping_idx;
  48. int reset_idx;
  49. enum mlxreg_wdt_type wdt_type;
  50. };
  51. static void mlxreg_wdt_check_card_reset(struct mlxreg_wdt *wdt)
  52. {
  53. struct mlxreg_core_data *reg_data;
  54. u32 regval;
  55. int rc;
  56. if (wdt->reset_idx == -EINVAL)
  57. return;
  58. if (!(wdt->wdd.info->options & WDIOF_CARDRESET))
  59. return;
  60. reg_data = &wdt->pdata->data[wdt->reset_idx];
  61. rc = regmap_read(wdt->regmap, reg_data->reg, &regval);
  62. if (!rc) {
  63. if (regval & ~reg_data->mask) {
  64. wdt->wdd.bootstatus = WDIOF_CARDRESET;
  65. dev_info(wdt->wdd.parent,
  66. "watchdog previously reset the CPU\n");
  67. }
  68. }
  69. }
  70. static int mlxreg_wdt_start(struct watchdog_device *wdd)
  71. {
  72. struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
  73. struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->action_idx];
  74. return regmap_update_bits(wdt->regmap, reg_data->reg, ~reg_data->mask,
  75. BIT(reg_data->bit));
  76. }
  77. static int mlxreg_wdt_stop(struct watchdog_device *wdd)
  78. {
  79. struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
  80. struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->action_idx];
  81. return regmap_update_bits(wdt->regmap, reg_data->reg, ~reg_data->mask,
  82. ~BIT(reg_data->bit));
  83. }
  84. static int mlxreg_wdt_ping(struct watchdog_device *wdd)
  85. {
  86. struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
  87. struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->ping_idx];
  88. return regmap_update_bits_base(wdt->regmap, reg_data->reg,
  89. ~reg_data->mask, BIT(reg_data->bit),
  90. NULL, false, true);
  91. }
  92. static int mlxreg_wdt_set_timeout(struct watchdog_device *wdd,
  93. unsigned int timeout)
  94. {
  95. struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
  96. struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->timeout_idx];
  97. u32 regval, set_time, hw_timeout;
  98. int rc;
  99. if (wdt->wdt_type == MLX_WDT_TYPE1) {
  100. rc = regmap_read(wdt->regmap, reg_data->reg, &regval);
  101. if (rc)
  102. return rc;
  103. hw_timeout = order_base_2(timeout * MLXREG_WDT_CLOCK_SCALE);
  104. regval = (regval & reg_data->mask) | hw_timeout;
  105. /* Rowndown to actual closest number of sec. */
  106. set_time = BIT(hw_timeout) / MLXREG_WDT_CLOCK_SCALE;
  107. } else {
  108. set_time = timeout;
  109. regval = timeout;
  110. }
  111. wdd->timeout = set_time;
  112. rc = regmap_write(wdt->regmap, reg_data->reg, regval);
  113. if (!rc) {
  114. /*
  115. * Restart watchdog with new timeout period
  116. * if watchdog is already started.
  117. */
  118. if (watchdog_active(wdd)) {
  119. rc = mlxreg_wdt_stop(wdd);
  120. if (!rc)
  121. rc = mlxreg_wdt_start(wdd);
  122. }
  123. }
  124. return rc;
  125. }
  126. static unsigned int mlxreg_wdt_get_timeleft(struct watchdog_device *wdd)
  127. {
  128. struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
  129. struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->tleft_idx];
  130. u32 regval;
  131. int rc;
  132. rc = regmap_read(wdt->regmap, reg_data->reg, &regval);
  133. /* Return 0 timeleft in case of failure register read. */
  134. return rc == 0 ? regval : 0;
  135. }
  136. static const struct watchdog_ops mlxreg_wdt_ops_type1 = {
  137. .start = mlxreg_wdt_start,
  138. .stop = mlxreg_wdt_stop,
  139. .ping = mlxreg_wdt_ping,
  140. .set_timeout = mlxreg_wdt_set_timeout,
  141. .owner = THIS_MODULE,
  142. };
  143. static const struct watchdog_ops mlxreg_wdt_ops_type2 = {
  144. .start = mlxreg_wdt_start,
  145. .stop = mlxreg_wdt_stop,
  146. .ping = mlxreg_wdt_ping,
  147. .set_timeout = mlxreg_wdt_set_timeout,
  148. .get_timeleft = mlxreg_wdt_get_timeleft,
  149. .owner = THIS_MODULE,
  150. };
  151. static const struct watchdog_info mlxreg_wdt_main_info = {
  152. .options = MLXREG_WDT_OPTIONS_BASE
  153. | WDIOF_CARDRESET,
  154. .identity = "mlx-wdt-main",
  155. };
  156. static const struct watchdog_info mlxreg_wdt_aux_info = {
  157. .options = MLXREG_WDT_OPTIONS_BASE
  158. | WDIOF_ALARMONLY,
  159. .identity = "mlx-wdt-aux",
  160. };
  161. static void mlxreg_wdt_config(struct mlxreg_wdt *wdt,
  162. struct mlxreg_core_platform_data *pdata)
  163. {
  164. struct mlxreg_core_data *data = pdata->data;
  165. int i;
  166. wdt->reset_idx = -EINVAL;
  167. for (i = 0; i < pdata->counter; i++, data++) {
  168. if (strnstr(data->label, "action", sizeof(data->label)))
  169. wdt->action_idx = i;
  170. else if (strnstr(data->label, "timeout", sizeof(data->label)))
  171. wdt->timeout_idx = i;
  172. else if (strnstr(data->label, "timeleft", sizeof(data->label)))
  173. wdt->tleft_idx = i;
  174. else if (strnstr(data->label, "ping", sizeof(data->label)))
  175. wdt->ping_idx = i;
  176. else if (strnstr(data->label, "reset", sizeof(data->label)))
  177. wdt->reset_idx = i;
  178. }
  179. wdt->pdata = pdata;
  180. if (strnstr(pdata->identity, mlxreg_wdt_main_info.identity,
  181. sizeof(mlxreg_wdt_main_info.identity)))
  182. wdt->wdd.info = &mlxreg_wdt_main_info;
  183. else
  184. wdt->wdd.info = &mlxreg_wdt_aux_info;
  185. wdt->wdt_type = pdata->version;
  186. if (wdt->wdt_type == MLX_WDT_TYPE2) {
  187. wdt->wdd.ops = &mlxreg_wdt_ops_type2;
  188. wdt->wdd.max_timeout = MLXREG_WDT_MAX_TIMEOUT_TYPE2;
  189. } else {
  190. wdt->wdd.ops = &mlxreg_wdt_ops_type1;
  191. wdt->wdd.max_timeout = MLXREG_WDT_MAX_TIMEOUT_TYPE1;
  192. }
  193. wdt->wdd.min_timeout = MLXREG_WDT_MIN_TIMEOUT;
  194. }
  195. static int mlxreg_wdt_init_timeout(struct mlxreg_wdt *wdt,
  196. struct mlxreg_core_platform_data *pdata)
  197. {
  198. u32 timeout;
  199. timeout = pdata->data[wdt->timeout_idx].health_cntr;
  200. return mlxreg_wdt_set_timeout(&wdt->wdd, timeout);
  201. }
  202. static int mlxreg_wdt_probe(struct platform_device *pdev)
  203. {
  204. struct device *dev = &pdev->dev;
  205. struct mlxreg_core_platform_data *pdata;
  206. struct mlxreg_wdt *wdt;
  207. int rc;
  208. pdata = dev_get_platdata(dev);
  209. if (!pdata) {
  210. dev_err(dev, "Failed to get platform data.\n");
  211. return -EINVAL;
  212. }
  213. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  214. if (!wdt)
  215. return -ENOMEM;
  216. wdt->wdd.parent = dev;
  217. wdt->regmap = pdata->regmap;
  218. mlxreg_wdt_config(wdt, pdata);
  219. if ((pdata->features & MLXREG_CORE_WD_FEATURE_NOWAYOUT))
  220. watchdog_set_nowayout(&wdt->wdd, WATCHDOG_NOWAYOUT);
  221. watchdog_stop_on_reboot(&wdt->wdd);
  222. watchdog_stop_on_unregister(&wdt->wdd);
  223. watchdog_set_drvdata(&wdt->wdd, wdt);
  224. rc = mlxreg_wdt_init_timeout(wdt, pdata);
  225. if (rc)
  226. goto register_error;
  227. if ((pdata->features & MLXREG_CORE_WD_FEATURE_START_AT_BOOT)) {
  228. rc = mlxreg_wdt_start(&wdt->wdd);
  229. if (rc)
  230. goto register_error;
  231. set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
  232. }
  233. mlxreg_wdt_check_card_reset(wdt);
  234. rc = devm_watchdog_register_device(dev, &wdt->wdd);
  235. register_error:
  236. if (rc)
  237. dev_err(dev, "Cannot register watchdog device (err=%d)\n", rc);
  238. return rc;
  239. }
  240. static struct platform_driver mlxreg_wdt_driver = {
  241. .probe = mlxreg_wdt_probe,
  242. .driver = {
  243. .name = "mlx-wdt",
  244. },
  245. };
  246. module_platform_driver(mlxreg_wdt_driver);
  247. MODULE_AUTHOR("Michael Shych <michaelsh@mellanox.com>");
  248. MODULE_DESCRIPTION("Mellanox watchdog driver");
  249. MODULE_LICENSE("GPL");
  250. MODULE_ALIAS("platform:mlx-wdt");