kempld_wdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kontron PLD watchdog driver
  4. *
  5. * Copyright (c) 2010-2013 Kontron Europe GmbH
  6. * Author: Michael Brunner <michael.brunner@kontron.com>
  7. *
  8. * Note: From the PLD watchdog point of view timeout and pretimeout are
  9. * defined differently than in the kernel.
  10. * First the pretimeout stage runs out before the timeout stage gets
  11. * active.
  12. *
  13. * Kernel/API: P-----| pretimeout
  14. * |-----------------------T timeout
  15. * Watchdog: |-----------------P pretimeout_stage
  16. * |-----T timeout_stage
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/mfd/kempld.h>
  24. #define KEMPLD_WDT_STAGE_TIMEOUT(x) (0x1b + (x) * 4)
  25. #define KEMPLD_WDT_STAGE_CFG(x) (0x18 + (x))
  26. #define STAGE_CFG_GET_PRESCALER(x) (((x) & 0x30) >> 4)
  27. #define STAGE_CFG_SET_PRESCALER(x) (((x) & 0x3) << 4)
  28. #define STAGE_CFG_PRESCALER_MASK 0x30
  29. #define STAGE_CFG_ACTION_MASK 0x7
  30. #define STAGE_CFG_ASSERT (1 << 3)
  31. #define KEMPLD_WDT_MAX_STAGES 2
  32. #define KEMPLD_WDT_KICK 0x16
  33. #define KEMPLD_WDT_CFG 0x17
  34. #define KEMPLD_WDT_CFG_ENABLE 0x10
  35. #define KEMPLD_WDT_CFG_ENABLE_LOCK 0x8
  36. #define KEMPLD_WDT_CFG_GLOBAL_LOCK 0x80
  37. enum {
  38. ACTION_NONE = 0,
  39. ACTION_RESET,
  40. ACTION_NMI,
  41. ACTION_SMI,
  42. ACTION_SCI,
  43. ACTION_DELAY,
  44. };
  45. enum {
  46. STAGE_TIMEOUT = 0,
  47. STAGE_PRETIMEOUT,
  48. };
  49. enum {
  50. PRESCALER_21 = 0,
  51. PRESCALER_17,
  52. PRESCALER_12,
  53. };
  54. static const u32 kempld_prescaler[] = {
  55. [PRESCALER_21] = (1 << 21) - 1,
  56. [PRESCALER_17] = (1 << 17) - 1,
  57. [PRESCALER_12] = (1 << 12) - 1,
  58. 0,
  59. };
  60. struct kempld_wdt_stage {
  61. unsigned int id;
  62. u32 mask;
  63. };
  64. struct kempld_wdt_data {
  65. struct kempld_device_data *pld;
  66. struct watchdog_device wdd;
  67. unsigned int pretimeout;
  68. struct kempld_wdt_stage stage[KEMPLD_WDT_MAX_STAGES];
  69. #ifdef CONFIG_PM
  70. u8 pm_status_store;
  71. #endif
  72. };
  73. #define DEFAULT_TIMEOUT 30 /* seconds */
  74. #define DEFAULT_PRETIMEOUT 0
  75. static unsigned int timeout = DEFAULT_TIMEOUT;
  76. module_param(timeout, uint, 0);
  77. MODULE_PARM_DESC(timeout,
  78. "Watchdog timeout in seconds. (>=0, default="
  79. __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  80. static unsigned int pretimeout = DEFAULT_PRETIMEOUT;
  81. module_param(pretimeout, uint, 0);
  82. MODULE_PARM_DESC(pretimeout,
  83. "Watchdog pretimeout in seconds. (>=0, default="
  84. __MODULE_STRING(DEFAULT_PRETIMEOUT) ")");
  85. static bool nowayout = WATCHDOG_NOWAYOUT;
  86. module_param(nowayout, bool, 0);
  87. MODULE_PARM_DESC(nowayout,
  88. "Watchdog cannot be stopped once started (default="
  89. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  90. static int kempld_wdt_set_stage_action(struct kempld_wdt_data *wdt_data,
  91. struct kempld_wdt_stage *stage,
  92. u8 action)
  93. {
  94. struct kempld_device_data *pld = wdt_data->pld;
  95. u8 stage_cfg;
  96. if (!stage || !stage->mask)
  97. return -EINVAL;
  98. kempld_get_mutex(pld);
  99. stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
  100. stage_cfg &= ~STAGE_CFG_ACTION_MASK;
  101. stage_cfg |= (action & STAGE_CFG_ACTION_MASK);
  102. if (action == ACTION_RESET)
  103. stage_cfg |= STAGE_CFG_ASSERT;
  104. else
  105. stage_cfg &= ~STAGE_CFG_ASSERT;
  106. kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->id), stage_cfg);
  107. kempld_release_mutex(pld);
  108. return 0;
  109. }
  110. static int kempld_wdt_set_stage_timeout(struct kempld_wdt_data *wdt_data,
  111. struct kempld_wdt_stage *stage,
  112. unsigned int timeout)
  113. {
  114. struct kempld_device_data *pld = wdt_data->pld;
  115. u32 prescaler;
  116. u64 stage_timeout64;
  117. u32 stage_timeout;
  118. u32 remainder;
  119. u8 stage_cfg;
  120. prescaler = kempld_prescaler[PRESCALER_21];
  121. if (!stage)
  122. return -EINVAL;
  123. stage_timeout64 = (u64)timeout * pld->pld_clock;
  124. remainder = do_div(stage_timeout64, prescaler);
  125. if (remainder)
  126. stage_timeout64++;
  127. if (stage_timeout64 > stage->mask)
  128. return -EINVAL;
  129. stage_timeout = stage_timeout64 & stage->mask;
  130. kempld_get_mutex(pld);
  131. stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
  132. stage_cfg &= ~STAGE_CFG_PRESCALER_MASK;
  133. stage_cfg |= STAGE_CFG_SET_PRESCALER(PRESCALER_21);
  134. kempld_write8(pld, KEMPLD_WDT_STAGE_CFG(stage->id), stage_cfg);
  135. kempld_write32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->id),
  136. stage_timeout);
  137. kempld_release_mutex(pld);
  138. return 0;
  139. }
  140. /*
  141. * kempld_get_mutex must be called prior to calling this function.
  142. */
  143. static unsigned int kempld_wdt_get_timeout(struct kempld_wdt_data *wdt_data,
  144. struct kempld_wdt_stage *stage)
  145. {
  146. struct kempld_device_data *pld = wdt_data->pld;
  147. unsigned int timeout;
  148. u64 stage_timeout;
  149. u32 prescaler;
  150. u32 remainder;
  151. u8 stage_cfg;
  152. if (!stage->mask)
  153. return 0;
  154. stage_cfg = kempld_read8(pld, KEMPLD_WDT_STAGE_CFG(stage->id));
  155. stage_timeout = kempld_read32(pld, KEMPLD_WDT_STAGE_TIMEOUT(stage->id));
  156. prescaler = kempld_prescaler[STAGE_CFG_GET_PRESCALER(stage_cfg)];
  157. stage_timeout = (stage_timeout & stage->mask) * prescaler;
  158. remainder = do_div(stage_timeout, pld->pld_clock);
  159. if (remainder)
  160. stage_timeout++;
  161. timeout = stage_timeout;
  162. WARN_ON_ONCE(timeout != stage_timeout);
  163. return timeout;
  164. }
  165. static int kempld_wdt_set_timeout(struct watchdog_device *wdd,
  166. unsigned int timeout)
  167. {
  168. struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  169. struct kempld_wdt_stage *pretimeout_stage;
  170. struct kempld_wdt_stage *timeout_stage;
  171. int ret;
  172. timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
  173. pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
  174. if (pretimeout_stage->mask && wdt_data->pretimeout > 0)
  175. timeout = wdt_data->pretimeout;
  176. ret = kempld_wdt_set_stage_action(wdt_data, timeout_stage,
  177. ACTION_RESET);
  178. if (ret)
  179. return ret;
  180. ret = kempld_wdt_set_stage_timeout(wdt_data, timeout_stage,
  181. timeout);
  182. if (ret)
  183. return ret;
  184. wdd->timeout = timeout;
  185. return 0;
  186. }
  187. static int kempld_wdt_set_pretimeout(struct watchdog_device *wdd,
  188. unsigned int pretimeout)
  189. {
  190. struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  191. struct kempld_wdt_stage *pretimeout_stage;
  192. u8 action = ACTION_NONE;
  193. int ret;
  194. pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
  195. if (!pretimeout_stage->mask)
  196. return -ENXIO;
  197. if (pretimeout > wdd->timeout)
  198. return -EINVAL;
  199. if (pretimeout > 0)
  200. action = ACTION_NMI;
  201. ret = kempld_wdt_set_stage_action(wdt_data, pretimeout_stage,
  202. action);
  203. if (ret)
  204. return ret;
  205. ret = kempld_wdt_set_stage_timeout(wdt_data, pretimeout_stage,
  206. wdd->timeout - pretimeout);
  207. if (ret)
  208. return ret;
  209. wdt_data->pretimeout = pretimeout;
  210. return 0;
  211. }
  212. static void kempld_wdt_update_timeouts(struct kempld_wdt_data *wdt_data)
  213. {
  214. struct kempld_device_data *pld = wdt_data->pld;
  215. struct kempld_wdt_stage *pretimeout_stage;
  216. struct kempld_wdt_stage *timeout_stage;
  217. unsigned int pretimeout, timeout;
  218. pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
  219. timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
  220. kempld_get_mutex(pld);
  221. pretimeout = kempld_wdt_get_timeout(wdt_data, pretimeout_stage);
  222. timeout = kempld_wdt_get_timeout(wdt_data, timeout_stage);
  223. kempld_release_mutex(pld);
  224. if (pretimeout)
  225. wdt_data->pretimeout = timeout;
  226. else
  227. wdt_data->pretimeout = 0;
  228. wdt_data->wdd.timeout = pretimeout + timeout;
  229. }
  230. static int kempld_wdt_start(struct watchdog_device *wdd)
  231. {
  232. struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  233. struct kempld_device_data *pld = wdt_data->pld;
  234. u8 status;
  235. int ret;
  236. ret = kempld_wdt_set_timeout(wdd, wdd->timeout);
  237. if (ret)
  238. return ret;
  239. kempld_get_mutex(pld);
  240. status = kempld_read8(pld, KEMPLD_WDT_CFG);
  241. status |= KEMPLD_WDT_CFG_ENABLE;
  242. kempld_write8(pld, KEMPLD_WDT_CFG, status);
  243. status = kempld_read8(pld, KEMPLD_WDT_CFG);
  244. kempld_release_mutex(pld);
  245. /* Check if the watchdog was enabled */
  246. if (!(status & KEMPLD_WDT_CFG_ENABLE))
  247. return -EACCES;
  248. return 0;
  249. }
  250. static int kempld_wdt_stop(struct watchdog_device *wdd)
  251. {
  252. struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  253. struct kempld_device_data *pld = wdt_data->pld;
  254. u8 status;
  255. kempld_get_mutex(pld);
  256. status = kempld_read8(pld, KEMPLD_WDT_CFG);
  257. status &= ~KEMPLD_WDT_CFG_ENABLE;
  258. kempld_write8(pld, KEMPLD_WDT_CFG, status);
  259. status = kempld_read8(pld, KEMPLD_WDT_CFG);
  260. kempld_release_mutex(pld);
  261. /* Check if the watchdog was disabled */
  262. if (status & KEMPLD_WDT_CFG_ENABLE)
  263. return -EACCES;
  264. return 0;
  265. }
  266. static int kempld_wdt_keepalive(struct watchdog_device *wdd)
  267. {
  268. struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  269. struct kempld_device_data *pld = wdt_data->pld;
  270. kempld_get_mutex(pld);
  271. kempld_write8(pld, KEMPLD_WDT_KICK, 'K');
  272. kempld_release_mutex(pld);
  273. return 0;
  274. }
  275. static long kempld_wdt_ioctl(struct watchdog_device *wdd, unsigned int cmd,
  276. unsigned long arg)
  277. {
  278. struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  279. void __user *argp = (void __user *)arg;
  280. int ret = -ENOIOCTLCMD;
  281. int __user *p = argp;
  282. int new_value;
  283. switch (cmd) {
  284. case WDIOC_SETPRETIMEOUT:
  285. if (get_user(new_value, p))
  286. return -EFAULT;
  287. ret = kempld_wdt_set_pretimeout(wdd, new_value);
  288. if (ret)
  289. return ret;
  290. ret = kempld_wdt_keepalive(wdd);
  291. break;
  292. case WDIOC_GETPRETIMEOUT:
  293. ret = put_user(wdt_data->pretimeout, (int __user *)arg);
  294. break;
  295. }
  296. return ret;
  297. }
  298. static int kempld_wdt_probe_stages(struct watchdog_device *wdd)
  299. {
  300. struct kempld_wdt_data *wdt_data = watchdog_get_drvdata(wdd);
  301. struct kempld_device_data *pld = wdt_data->pld;
  302. struct kempld_wdt_stage *pretimeout_stage;
  303. struct kempld_wdt_stage *timeout_stage;
  304. u8 index, data, data_orig;
  305. u32 mask;
  306. int i, j;
  307. pretimeout_stage = &wdt_data->stage[STAGE_PRETIMEOUT];
  308. timeout_stage = &wdt_data->stage[STAGE_TIMEOUT];
  309. pretimeout_stage->mask = 0;
  310. timeout_stage->mask = 0;
  311. for (i = 0; i < 3; i++) {
  312. index = KEMPLD_WDT_STAGE_TIMEOUT(i);
  313. mask = 0;
  314. kempld_get_mutex(pld);
  315. /* Probe each byte individually. */
  316. for (j = 0; j < 4; j++) {
  317. data_orig = kempld_read8(pld, index + j);
  318. kempld_write8(pld, index + j, 0x00);
  319. data = kempld_read8(pld, index + j);
  320. /* A failed write means this byte is reserved */
  321. if (data != 0x00)
  322. break;
  323. kempld_write8(pld, index + j, data_orig);
  324. mask |= 0xff << (j * 8);
  325. }
  326. kempld_release_mutex(pld);
  327. /* Assign available stages to timeout and pretimeout */
  328. if (!timeout_stage->mask) {
  329. timeout_stage->mask = mask;
  330. timeout_stage->id = i;
  331. } else {
  332. if (pld->feature_mask & KEMPLD_FEATURE_BIT_NMI) {
  333. pretimeout_stage->mask = timeout_stage->mask;
  334. timeout_stage->mask = mask;
  335. pretimeout_stage->id = timeout_stage->id;
  336. timeout_stage->id = i;
  337. }
  338. break;
  339. }
  340. }
  341. if (!timeout_stage->mask)
  342. return -ENODEV;
  343. return 0;
  344. }
  345. static const struct watchdog_info kempld_wdt_info = {
  346. .identity = "KEMPLD Watchdog",
  347. .options = WDIOF_SETTIMEOUT |
  348. WDIOF_KEEPALIVEPING |
  349. WDIOF_MAGICCLOSE |
  350. WDIOF_PRETIMEOUT
  351. };
  352. static const struct watchdog_ops kempld_wdt_ops = {
  353. .owner = THIS_MODULE,
  354. .start = kempld_wdt_start,
  355. .stop = kempld_wdt_stop,
  356. .ping = kempld_wdt_keepalive,
  357. .set_timeout = kempld_wdt_set_timeout,
  358. .ioctl = kempld_wdt_ioctl,
  359. };
  360. static int kempld_wdt_probe(struct platform_device *pdev)
  361. {
  362. struct kempld_device_data *pld = dev_get_drvdata(pdev->dev.parent);
  363. struct kempld_wdt_data *wdt_data;
  364. struct device *dev = &pdev->dev;
  365. struct watchdog_device *wdd;
  366. u8 status;
  367. int ret = 0;
  368. wdt_data = devm_kzalloc(dev, sizeof(*wdt_data), GFP_KERNEL);
  369. if (!wdt_data)
  370. return -ENOMEM;
  371. wdt_data->pld = pld;
  372. wdd = &wdt_data->wdd;
  373. wdd->parent = dev;
  374. kempld_get_mutex(pld);
  375. status = kempld_read8(pld, KEMPLD_WDT_CFG);
  376. kempld_release_mutex(pld);
  377. /* Enable nowayout if watchdog is already locked */
  378. if (status & (KEMPLD_WDT_CFG_ENABLE_LOCK |
  379. KEMPLD_WDT_CFG_GLOBAL_LOCK)) {
  380. if (!nowayout)
  381. dev_warn(dev,
  382. "Forcing nowayout - watchdog lock enabled!\n");
  383. nowayout = true;
  384. }
  385. wdd->info = &kempld_wdt_info;
  386. wdd->ops = &kempld_wdt_ops;
  387. watchdog_set_drvdata(wdd, wdt_data);
  388. watchdog_set_nowayout(wdd, nowayout);
  389. ret = kempld_wdt_probe_stages(wdd);
  390. if (ret)
  391. return ret;
  392. kempld_wdt_set_timeout(wdd, timeout);
  393. kempld_wdt_set_pretimeout(wdd, pretimeout);
  394. /* Check if watchdog is already enabled */
  395. if (status & KEMPLD_WDT_CFG_ENABLE) {
  396. /* Get current watchdog settings */
  397. kempld_wdt_update_timeouts(wdt_data);
  398. dev_info(dev, "Watchdog was already enabled\n");
  399. }
  400. platform_set_drvdata(pdev, wdt_data);
  401. watchdog_stop_on_reboot(wdd);
  402. watchdog_stop_on_unregister(wdd);
  403. ret = devm_watchdog_register_device(dev, wdd);
  404. if (ret)
  405. return ret;
  406. dev_info(dev, "Watchdog registered with %ds timeout\n", wdd->timeout);
  407. return 0;
  408. }
  409. #ifdef CONFIG_PM
  410. /* Disable watchdog if it is active during suspend */
  411. static int kempld_wdt_suspend(struct platform_device *pdev,
  412. pm_message_t message)
  413. {
  414. struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
  415. struct kempld_device_data *pld = wdt_data->pld;
  416. struct watchdog_device *wdd = &wdt_data->wdd;
  417. kempld_get_mutex(pld);
  418. wdt_data->pm_status_store = kempld_read8(pld, KEMPLD_WDT_CFG);
  419. kempld_release_mutex(pld);
  420. kempld_wdt_update_timeouts(wdt_data);
  421. if (wdt_data->pm_status_store & KEMPLD_WDT_CFG_ENABLE)
  422. return kempld_wdt_stop(wdd);
  423. return 0;
  424. }
  425. /* Enable watchdog and configure it if necessary */
  426. static int kempld_wdt_resume(struct platform_device *pdev)
  427. {
  428. struct kempld_wdt_data *wdt_data = platform_get_drvdata(pdev);
  429. struct watchdog_device *wdd = &wdt_data->wdd;
  430. /*
  431. * If watchdog was stopped before suspend be sure it gets disabled
  432. * again, for the case BIOS has enabled it during resume
  433. */
  434. if (wdt_data->pm_status_store & KEMPLD_WDT_CFG_ENABLE)
  435. return kempld_wdt_start(wdd);
  436. else
  437. return kempld_wdt_stop(wdd);
  438. }
  439. #else
  440. #define kempld_wdt_suspend NULL
  441. #define kempld_wdt_resume NULL
  442. #endif
  443. static struct platform_driver kempld_wdt_driver = {
  444. .driver = {
  445. .name = "kempld-wdt",
  446. },
  447. .probe = kempld_wdt_probe,
  448. .suspend = kempld_wdt_suspend,
  449. .resume = kempld_wdt_resume,
  450. };
  451. module_platform_driver(kempld_wdt_driver);
  452. MODULE_DESCRIPTION("KEM PLD Watchdog Driver");
  453. MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
  454. MODULE_LICENSE("GPL");