bd96801_wdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 ROHM Semiconductors
  4. *
  5. * ROHM BD96801 watchdog driver
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mfd/rohm-bd96801.h>
  11. #include <linux/mfd/rohm-generic.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reboot.h>
  16. #include <linux/regmap.h>
  17. #include <linux/watchdog.h>
  18. static bool nowayout;
  19. module_param(nowayout, bool, 0);
  20. MODULE_PARM_DESC(nowayout,
  21. "Watchdog cannot be stopped once started (default=\"false\")");
  22. #define BD96801_WD_TMO_SHORT_MASK 0x70
  23. #define BD96801_WD_RATIO_MASK 0x3
  24. #define BD96801_WD_TYPE_MASK 0x4
  25. #define BD96801_WD_TYPE_SLOW 0x4
  26. #define BD96801_WD_TYPE_WIN 0x0
  27. #define BD96801_WD_EN_MASK 0x3
  28. #define BD96801_WD_IF_EN 0x1
  29. #define BD96801_WD_QA_EN 0x2
  30. #define BD96801_WD_DISABLE 0x0
  31. #define BD96801_WD_ASSERT_MASK 0x8
  32. #define BD96801_WD_ASSERT_RST 0x8
  33. #define BD96801_WD_ASSERT_IRQ 0x0
  34. #define BD96801_WD_FEED_MASK 0x1
  35. #define BD96801_WD_FEED 0x1
  36. /* 1.1 mS */
  37. #define FASTNG_MIN 11
  38. #define FASTNG_MAX_US (100 * FASTNG_MIN << 7)
  39. #define SLOWNG_MAX_US (16 * FASTNG_MAX_US)
  40. #define BD96801_WDT_DEFAULT_MARGIN_MS 1843
  41. /* Unit is seconds */
  42. #define DEFAULT_TIMEOUT 30
  43. /*
  44. * BD96801 WDG supports window mode so the TMO consists of SHORT and LONG
  45. * timeout values. SHORT time is meaningful only in window mode where feeding
  46. * period shorter than SHORT would be an error. LONG time is used to detect if
  47. * feeding is not occurring within given time limit (SoC SW hangs). The LONG
  48. * timeout time is a multiple of (2, 4, 8 or 16 times) the SHORT timeout.
  49. */
  50. struct wdtbd96801 {
  51. struct device *dev;
  52. struct regmap *regmap;
  53. struct watchdog_device wdt;
  54. };
  55. static int bd96801_wdt_ping(struct watchdog_device *wdt)
  56. {
  57. struct wdtbd96801 *w = watchdog_get_drvdata(wdt);
  58. return regmap_update_bits(w->regmap, BD96801_REG_WD_FEED,
  59. BD96801_WD_FEED_MASK, BD96801_WD_FEED);
  60. }
  61. static int bd96801_wdt_start(struct watchdog_device *wdt)
  62. {
  63. struct wdtbd96801 *w = watchdog_get_drvdata(wdt);
  64. return regmap_update_bits(w->regmap, BD96801_REG_WD_CONF,
  65. BD96801_WD_EN_MASK, BD96801_WD_IF_EN);
  66. }
  67. static int bd96801_wdt_stop(struct watchdog_device *wdt)
  68. {
  69. struct wdtbd96801 *w = watchdog_get_drvdata(wdt);
  70. return regmap_update_bits(w->regmap, BD96801_REG_WD_CONF,
  71. BD96801_WD_EN_MASK, BD96801_WD_DISABLE);
  72. }
  73. static const struct watchdog_info bd96801_wdt_info = {
  74. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
  75. WDIOF_SETTIMEOUT,
  76. .identity = "BD96801 Watchdog",
  77. };
  78. static const struct watchdog_ops bd96801_wdt_ops = {
  79. .start = bd96801_wdt_start,
  80. .stop = bd96801_wdt_stop,
  81. .ping = bd96801_wdt_ping,
  82. };
  83. static int find_closest_fast(unsigned int target, int *sel, unsigned int *val)
  84. {
  85. unsigned int window = FASTNG_MIN;
  86. int i;
  87. for (i = 0; i < 8 && window < target; i++)
  88. window <<= 1;
  89. if (i == 8)
  90. return -EINVAL;
  91. *val = window;
  92. *sel = i;
  93. return 0;
  94. }
  95. static int find_closest_slow_by_fast(unsigned int fast_val, unsigned int *target,
  96. int *slowsel)
  97. {
  98. static const int multipliers[] = {2, 4, 8, 16};
  99. int sel;
  100. for (sel = 0; sel < ARRAY_SIZE(multipliers) &&
  101. multipliers[sel] * fast_val < *target; sel++)
  102. ;
  103. if (sel == ARRAY_SIZE(multipliers))
  104. return -EINVAL;
  105. *slowsel = sel;
  106. *target = multipliers[sel] * fast_val;
  107. return 0;
  108. }
  109. static int find_closest_slow(unsigned int *target, int *slow_sel, int *fast_sel)
  110. {
  111. static const int multipliers[] = {2, 4, 8, 16};
  112. unsigned int window = FASTNG_MIN;
  113. unsigned int val = 0;
  114. int i, j;
  115. for (i = 0; i < 8; i++) {
  116. for (j = 0; j < ARRAY_SIZE(multipliers); j++) {
  117. unsigned int slow;
  118. slow = window * multipliers[j];
  119. if (slow >= *target && (!val || slow < val)) {
  120. val = slow;
  121. *fast_sel = i;
  122. *slow_sel = j;
  123. }
  124. }
  125. window <<= 1;
  126. }
  127. if (!val)
  128. return -EINVAL;
  129. *target = val;
  130. return 0;
  131. }
  132. static int bd96801_set_wdt_mode(struct wdtbd96801 *w, unsigned int hw_margin,
  133. unsigned int hw_margin_min)
  134. {
  135. int fastng, slowng, type, ret, reg, mask;
  136. struct device *dev = w->dev;
  137. if (hw_margin_min * 1000 > FASTNG_MAX_US) {
  138. dev_err(dev, "Unsupported fast timeout %u uS [max %u]\n",
  139. hw_margin_min * 1000, FASTNG_MAX_US);
  140. return -EINVAL;
  141. }
  142. if (hw_margin * 1000 > SLOWNG_MAX_US) {
  143. dev_err(dev, "Unsupported slow timeout %u uS [max %u]\n",
  144. hw_margin * 1000, SLOWNG_MAX_US);
  145. return -EINVAL;
  146. }
  147. /*
  148. * Convert to 100uS to guarantee reasonable timeouts fit in
  149. * 32bit maintaining also a decent accuracy.
  150. */
  151. hw_margin *= 10;
  152. hw_margin_min *= 10;
  153. if (hw_margin_min) {
  154. unsigned int min;
  155. type = BD96801_WD_TYPE_WIN;
  156. dev_dbg(dev, "Setting type WINDOW 0x%x\n", type);
  157. ret = find_closest_fast(hw_margin_min, &fastng, &min);
  158. if (ret)
  159. return ret;
  160. ret = find_closest_slow_by_fast(min, &hw_margin, &slowng);
  161. if (ret) {
  162. dev_err(dev,
  163. "can't support slow timeout %u uS using fast %u uS. [max slow %u uS]\n",
  164. hw_margin * 100, min * 100, min * 100 * 16);
  165. return ret;
  166. }
  167. w->wdt.min_hw_heartbeat_ms = min / 10;
  168. } else {
  169. type = BD96801_WD_TYPE_SLOW;
  170. dev_dbg(dev, "Setting type SLOW 0x%x\n", type);
  171. ret = find_closest_slow(&hw_margin, &slowng, &fastng);
  172. if (ret)
  173. return ret;
  174. }
  175. w->wdt.max_hw_heartbeat_ms = hw_margin / 10;
  176. fastng = FIELD_PREP(BD96801_WD_TMO_SHORT_MASK, fastng);
  177. reg = slowng | fastng;
  178. mask = BD96801_WD_RATIO_MASK | BD96801_WD_TMO_SHORT_MASK;
  179. ret = regmap_update_bits(w->regmap, BD96801_REG_WD_TMO,
  180. mask, reg);
  181. if (ret)
  182. return ret;
  183. ret = regmap_update_bits(w->regmap, BD96801_REG_WD_CONF,
  184. BD96801_WD_TYPE_MASK, type);
  185. return ret;
  186. }
  187. static int bd96801_set_heartbeat_from_hw(struct wdtbd96801 *w,
  188. unsigned int conf_reg)
  189. {
  190. int ret;
  191. unsigned int val, sel, fast;
  192. /*
  193. * The BD96801 supports a somewhat peculiar QA-mode, which we do not
  194. * support in this driver. If the QA-mode is enabled then we just
  195. * warn and bail-out.
  196. */
  197. if ((conf_reg & BD96801_WD_EN_MASK) != BD96801_WD_IF_EN) {
  198. dev_err(w->dev, "watchdog set to Q&A mode - exiting\n");
  199. return -EINVAL;
  200. }
  201. ret = regmap_read(w->regmap, BD96801_REG_WD_TMO, &val);
  202. if (ret)
  203. return ret;
  204. sel = FIELD_GET(BD96801_WD_TMO_SHORT_MASK, val);
  205. fast = FASTNG_MIN << sel;
  206. sel = (val & BD96801_WD_RATIO_MASK) + 1;
  207. w->wdt.max_hw_heartbeat_ms = (fast << sel) / USEC_PER_MSEC;
  208. if ((conf_reg & BD96801_WD_TYPE_MASK) == BD96801_WD_TYPE_WIN)
  209. w->wdt.min_hw_heartbeat_ms = fast / USEC_PER_MSEC;
  210. return 0;
  211. }
  212. static int init_wdg_hw(struct wdtbd96801 *w)
  213. {
  214. u32 hw_margin[2];
  215. int count, ret;
  216. u32 hw_margin_max = BD96801_WDT_DEFAULT_MARGIN_MS, hw_margin_min = 0;
  217. count = device_property_count_u32(w->dev->parent, "rohm,hw-timeout-ms");
  218. if (count < 0 && count != -EINVAL)
  219. return count;
  220. if (count > 0) {
  221. if (count > ARRAY_SIZE(hw_margin))
  222. return -EINVAL;
  223. ret = device_property_read_u32_array(w->dev->parent,
  224. "rohm,hw-timeout-ms",
  225. &hw_margin[0], count);
  226. if (ret < 0)
  227. return ret;
  228. if (count == 1)
  229. hw_margin_max = hw_margin[0];
  230. if (count == 2) {
  231. if (hw_margin[1] > hw_margin[0]) {
  232. hw_margin_max = hw_margin[1];
  233. hw_margin_min = hw_margin[0];
  234. } else {
  235. hw_margin_max = hw_margin[0];
  236. hw_margin_min = hw_margin[1];
  237. }
  238. }
  239. }
  240. ret = bd96801_set_wdt_mode(w, hw_margin_max, hw_margin_min);
  241. if (ret)
  242. return ret;
  243. ret = device_property_match_string(w->dev->parent, "rohm,wdg-action",
  244. "prstb");
  245. if (ret >= 0) {
  246. ret = regmap_update_bits(w->regmap, BD96801_REG_WD_CONF,
  247. BD96801_WD_ASSERT_MASK,
  248. BD96801_WD_ASSERT_RST);
  249. return ret;
  250. }
  251. ret = device_property_match_string(w->dev->parent, "rohm,wdg-action",
  252. "intb-only");
  253. if (ret >= 0) {
  254. ret = regmap_update_bits(w->regmap, BD96801_REG_WD_CONF,
  255. BD96801_WD_ASSERT_MASK,
  256. BD96801_WD_ASSERT_IRQ);
  257. return ret;
  258. }
  259. return 0;
  260. }
  261. static irqreturn_t bd96801_irq_hnd(int irq, void *data)
  262. {
  263. emergency_restart();
  264. return IRQ_NONE;
  265. }
  266. static int bd96801_wdt_probe(struct platform_device *pdev)
  267. {
  268. struct wdtbd96801 *w;
  269. int ret, irq;
  270. unsigned int val;
  271. w = devm_kzalloc(&pdev->dev, sizeof(*w), GFP_KERNEL);
  272. if (!w)
  273. return -ENOMEM;
  274. w->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  275. w->dev = &pdev->dev;
  276. w->wdt.info = &bd96801_wdt_info;
  277. w->wdt.ops = &bd96801_wdt_ops;
  278. w->wdt.parent = pdev->dev.parent;
  279. w->wdt.timeout = DEFAULT_TIMEOUT;
  280. watchdog_set_drvdata(&w->wdt, w);
  281. ret = regmap_read(w->regmap, BD96801_REG_WD_CONF, &val);
  282. if (ret)
  283. return dev_err_probe(&pdev->dev, ret,
  284. "Failed to get the watchdog state\n");
  285. /*
  286. * If the WDG is already enabled we assume it is configured by boot.
  287. * In this case we just update the hw-timeout based on values set to
  288. * the timeout / mode registers and leave the hardware configs
  289. * untouched.
  290. */
  291. if ((val & BD96801_WD_EN_MASK) != BD96801_WD_DISABLE) {
  292. dev_dbg(&pdev->dev, "watchdog was running during probe\n");
  293. ret = bd96801_set_heartbeat_from_hw(w, val);
  294. if (ret)
  295. return ret;
  296. set_bit(WDOG_HW_RUNNING, &w->wdt.status);
  297. } else {
  298. /* If WDG is not running so we will initializate it */
  299. ret = init_wdg_hw(w);
  300. if (ret)
  301. return ret;
  302. }
  303. dev_dbg(w->dev, "heartbeat set to %u - %u\n",
  304. w->wdt.min_hw_heartbeat_ms, w->wdt.max_hw_heartbeat_ms);
  305. watchdog_init_timeout(&w->wdt, 0, pdev->dev.parent);
  306. watchdog_set_nowayout(&w->wdt, nowayout);
  307. watchdog_stop_on_reboot(&w->wdt);
  308. irq = platform_get_irq_byname(pdev, "bd96801-wdg");
  309. if (irq > 0) {
  310. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  311. bd96801_irq_hnd,
  312. IRQF_ONESHOT, "bd96801-wdg",
  313. NULL);
  314. if (ret)
  315. return dev_err_probe(&pdev->dev, ret,
  316. "Failed to register IRQ\n");
  317. }
  318. return devm_watchdog_register_device(&pdev->dev, &w->wdt);
  319. }
  320. static const struct platform_device_id bd96801_wdt_id[] = {
  321. { "bd96801-wdt", },
  322. { }
  323. };
  324. MODULE_DEVICE_TABLE(platform, bd96801_wdt_id);
  325. static struct platform_driver bd96801_wdt = {
  326. .driver = {
  327. .name = "bd96801-wdt"
  328. },
  329. .probe = bd96801_wdt_probe,
  330. .id_table = bd96801_wdt_id,
  331. };
  332. module_platform_driver(bd96801_wdt);
  333. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  334. MODULE_DESCRIPTION("BD96801 watchdog driver");
  335. MODULE_LICENSE("GPL");