sbsa_gwdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SBSA(Server Base System Architecture) Generic Watchdog driver
  4. *
  5. * Copyright (c) 2015, Linaro Ltd.
  6. * Author: Fu Wei <fu.wei@linaro.org>
  7. * Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>
  8. * Al Stone <al.stone@linaro.org>
  9. * Timur Tabi <timur@codeaurora.org>
  10. *
  11. * ARM SBSA Generic Watchdog has two stage timeouts:
  12. * the first signal (WS0) is for alerting the system by interrupt,
  13. * the second one (WS1) is a real hardware reset.
  14. * More details about the hardware specification of this device:
  15. * ARM DEN0029B - Server Base System Architecture (SBSA)
  16. *
  17. * This driver can operate ARM SBSA Generic Watchdog as a single stage watchdog
  18. * or a two stages watchdog, it's set up by the module parameter "action".
  19. * In the single stage mode, when the timeout is reached, your system
  20. * will be reset by WS1. The first signal (WS0) is ignored.
  21. * In the two stages mode, when the timeout is reached, the first signal (WS0)
  22. * will trigger panic. If the system is getting into trouble and cannot be reset
  23. * by panic or restart properly by the kdump kernel(if supported), then the
  24. * second stage (as long as the first stage) will be reached, system will be
  25. * reset by WS1. This function can help administrator to backup the system
  26. * context info by panic console output or kdump.
  27. *
  28. * SBSA GWDT:
  29. * if action is 1 (the two stages mode):
  30. * |--------WOR-------WS0--------WOR-------WS1
  31. * |----timeout-----(panic)----timeout-----reset
  32. *
  33. * if action is 0 (the single stage mode):
  34. * |------WOR-----WS0(ignored)-----WOR------WS1
  35. * |--------------timeout-------------------reset
  36. *
  37. * Note: Since this watchdog timer has two stages, and each stage is determined
  38. * by WOR, in the single stage mode, the timeout is (WOR * 2); in the two
  39. * stages mode, the timeout is WOR. The maximum timeout in the two stages mode
  40. * is half of that in the single stage mode.
  41. */
  42. #include <linux/io.h>
  43. #include <linux/io-64-nonatomic-lo-hi.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/mod_devicetable.h>
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/watchdog.h>
  51. #include <asm/arch_timer.h>
  52. #define DRV_NAME "sbsa-gwdt"
  53. #define WATCHDOG_NAME "SBSA Generic Watchdog"
  54. /* SBSA Generic Watchdog register definitions */
  55. /* refresh frame */
  56. #define SBSA_GWDT_WRR 0x000
  57. /* control frame */
  58. #define SBSA_GWDT_WCS 0x000
  59. #define SBSA_GWDT_WOR 0x008
  60. #define SBSA_GWDT_WCV 0x010
  61. /* refresh/control frame */
  62. #define SBSA_GWDT_W_IIDR 0xfcc
  63. #define SBSA_GWDT_IDR 0xfd0
  64. /* Watchdog Control and Status Register */
  65. #define SBSA_GWDT_WCS_EN BIT(0)
  66. #define SBSA_GWDT_WCS_WS0 BIT(1)
  67. #define SBSA_GWDT_WCS_WS1 BIT(2)
  68. #define SBSA_GWDT_VERSION_MASK GENMASK(3, 0)
  69. #define SBSA_GWDT_VERSION_SHIFT 16
  70. #define SBSA_GWDT_IMPL_MASK GENMASK(11, 0)
  71. #define SBSA_GWDT_IMPL_SHIFT 0
  72. #define SBSA_GWDT_IMPL_MEDIATEK 0x426
  73. /**
  74. * struct sbsa_gwdt - Internal representation of the SBSA GWDT
  75. * @wdd: kernel watchdog_device structure
  76. * @clk: store the System Counter clock frequency, in Hz.
  77. * @version: store the architecture version
  78. * @need_ws0_race_workaround:
  79. * indicate whether to adjust wdd->timeout to avoid a race with WS0
  80. * @refresh_base: Virtual address of the watchdog refresh frame
  81. * @control_base: Virtual address of the watchdog control frame
  82. */
  83. struct sbsa_gwdt {
  84. struct watchdog_device wdd;
  85. u32 clk;
  86. int version;
  87. bool need_ws0_race_workaround;
  88. void __iomem *refresh_base;
  89. void __iomem *control_base;
  90. };
  91. #define DEFAULT_TIMEOUT 10 /* seconds */
  92. static unsigned int timeout;
  93. module_param(timeout, uint, 0);
  94. MODULE_PARM_DESC(timeout,
  95. "Watchdog timeout in seconds. (>=0, default="
  96. __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  97. /*
  98. * action refers to action taken when watchdog gets WS0
  99. * 0 = skip
  100. * 1 = panic
  101. * defaults to skip (0)
  102. */
  103. static int action;
  104. module_param(action, int, 0);
  105. MODULE_PARM_DESC(action, "after watchdog gets WS0 interrupt, do: "
  106. "0 = skip(*) 1 = panic");
  107. static bool nowayout = WATCHDOG_NOWAYOUT;
  108. module_param(nowayout, bool, S_IRUGO);
  109. MODULE_PARM_DESC(nowayout,
  110. "Watchdog cannot be stopped once started (default="
  111. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  112. /*
  113. * Arm Base System Architecture 1.0 introduces watchdog v1 which
  114. * increases the length watchdog offset register to 48 bits.
  115. * - For version 0: WOR is 32 bits;
  116. * - For version 1: WOR is 48 bits which comprises the register
  117. * offset 0x8 and 0xC, and the bits [63:48] are reserved which are
  118. * Read-As-Zero and Writes-Ignored.
  119. */
  120. static u64 sbsa_gwdt_reg_read(struct sbsa_gwdt *gwdt)
  121. {
  122. if (gwdt->version == 0)
  123. return readl(gwdt->control_base + SBSA_GWDT_WOR);
  124. else
  125. return lo_hi_readq(gwdt->control_base + SBSA_GWDT_WOR);
  126. }
  127. static void sbsa_gwdt_reg_write(u64 val, struct sbsa_gwdt *gwdt)
  128. {
  129. if (gwdt->version == 0)
  130. writel((u32)val, gwdt->control_base + SBSA_GWDT_WOR);
  131. else
  132. lo_hi_writeq(val, gwdt->control_base + SBSA_GWDT_WOR);
  133. }
  134. /*
  135. * watchdog operation functions
  136. */
  137. static int sbsa_gwdt_set_timeout(struct watchdog_device *wdd,
  138. unsigned int timeout)
  139. {
  140. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  141. wdd->timeout = timeout;
  142. timeout = clamp_t(unsigned int, timeout, 1, wdd->max_hw_heartbeat_ms / 1000);
  143. if (action)
  144. sbsa_gwdt_reg_write((u64)gwdt->clk * timeout, gwdt);
  145. else
  146. /*
  147. * In the single stage mode, The first signal (WS0) is ignored,
  148. * the timeout is (WOR * 2), so the WOR should be configured
  149. * to half value of timeout.
  150. */
  151. sbsa_gwdt_reg_write(((u64)gwdt->clk / 2) * timeout, gwdt);
  152. /*
  153. * Some watchdog hardware has a race condition where it will ignore
  154. * sbsa_gwdt_keepalive() if it is called at the exact moment that a
  155. * timeout occurs and WS0 is being asserted. Unfortunately, the default
  156. * behavior of the watchdog core is very likely to trigger this race
  157. * when action=0 because it programs WOR to be half of the desired
  158. * timeout, and watchdog_next_keepalive() chooses the exact same time to
  159. * send keepalive pings.
  160. *
  161. * This triggers a race where sbsa_gwdt_keepalive() can be called right
  162. * as WS0 is being asserted, and affected hardware will ignore that
  163. * write and continue to assert WS0. After another (timeout / 2)
  164. * seconds, the same race happens again. If the driver wins then the
  165. * explicit refresh will reset WS0 to false but if the hardware wins,
  166. * then WS1 is asserted and the system resets.
  167. *
  168. * Avoid the problem by scheduling keepalive heartbeats one second later
  169. * than the WOR timeout.
  170. *
  171. * This workaround might not be needed in a future revision of the
  172. * hardware.
  173. */
  174. if (gwdt->need_ws0_race_workaround)
  175. wdd->min_hw_heartbeat_ms = timeout * 500 + 1000;
  176. return 0;
  177. }
  178. static unsigned int sbsa_gwdt_get_timeleft(struct watchdog_device *wdd)
  179. {
  180. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  181. u64 timeleft = 0;
  182. /*
  183. * In the single stage mode, if WS0 is deasserted
  184. * (watchdog is in the first stage),
  185. * timeleft = WOR + (WCV - system counter)
  186. */
  187. if (!action &&
  188. !(readl(gwdt->control_base + SBSA_GWDT_WCS) & SBSA_GWDT_WCS_WS0))
  189. timeleft += sbsa_gwdt_reg_read(gwdt);
  190. timeleft += lo_hi_readq(gwdt->control_base + SBSA_GWDT_WCV) -
  191. arch_timer_read_counter();
  192. do_div(timeleft, gwdt->clk);
  193. return timeleft;
  194. }
  195. static int sbsa_gwdt_keepalive(struct watchdog_device *wdd)
  196. {
  197. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  198. /*
  199. * Writing WRR for an explicit watchdog refresh.
  200. * You can write anyting (like 0).
  201. */
  202. writel(0, gwdt->refresh_base + SBSA_GWDT_WRR);
  203. return 0;
  204. }
  205. static void sbsa_gwdt_get_version(struct watchdog_device *wdd)
  206. {
  207. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  208. int iidr, ver, impl;
  209. iidr = readl(gwdt->control_base + SBSA_GWDT_W_IIDR);
  210. ver = (iidr >> SBSA_GWDT_VERSION_SHIFT) & SBSA_GWDT_VERSION_MASK;
  211. impl = (iidr >> SBSA_GWDT_IMPL_SHIFT) & SBSA_GWDT_IMPL_MASK;
  212. gwdt->version = ver;
  213. gwdt->need_ws0_race_workaround =
  214. !action && (impl == SBSA_GWDT_IMPL_MEDIATEK);
  215. }
  216. static int sbsa_gwdt_start(struct watchdog_device *wdd)
  217. {
  218. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  219. /* writing WCS will cause an explicit watchdog refresh */
  220. writel(SBSA_GWDT_WCS_EN, gwdt->control_base + SBSA_GWDT_WCS);
  221. return 0;
  222. }
  223. static int sbsa_gwdt_stop(struct watchdog_device *wdd)
  224. {
  225. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  226. /* Simply write 0 to WCS to clean WCS_EN bit */
  227. writel(0, gwdt->control_base + SBSA_GWDT_WCS);
  228. return 0;
  229. }
  230. static irqreturn_t sbsa_gwdt_interrupt(int irq, void *dev_id)
  231. {
  232. panic(WATCHDOG_NAME " timeout");
  233. return IRQ_HANDLED;
  234. }
  235. static const struct watchdog_info sbsa_gwdt_info = {
  236. .identity = WATCHDOG_NAME,
  237. .options = WDIOF_SETTIMEOUT |
  238. WDIOF_KEEPALIVEPING |
  239. WDIOF_MAGICCLOSE |
  240. WDIOF_CARDRESET,
  241. };
  242. static const struct watchdog_ops sbsa_gwdt_ops = {
  243. .owner = THIS_MODULE,
  244. .start = sbsa_gwdt_start,
  245. .stop = sbsa_gwdt_stop,
  246. .ping = sbsa_gwdt_keepalive,
  247. .set_timeout = sbsa_gwdt_set_timeout,
  248. .get_timeleft = sbsa_gwdt_get_timeleft,
  249. };
  250. static int sbsa_gwdt_probe(struct platform_device *pdev)
  251. {
  252. void __iomem *rf_base, *cf_base;
  253. struct device *dev = &pdev->dev;
  254. struct watchdog_device *wdd;
  255. struct sbsa_gwdt *gwdt;
  256. int ret, irq;
  257. u32 status;
  258. gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
  259. if (!gwdt)
  260. return -ENOMEM;
  261. platform_set_drvdata(pdev, gwdt);
  262. cf_base = devm_platform_ioremap_resource(pdev, 0);
  263. if (IS_ERR(cf_base))
  264. return PTR_ERR(cf_base);
  265. rf_base = devm_platform_ioremap_resource(pdev, 1);
  266. if (IS_ERR(rf_base))
  267. return PTR_ERR(rf_base);
  268. /*
  269. * Get the frequency of system counter from the cp15 interface of ARM
  270. * Generic timer. We don't need to check it, because if it returns "0",
  271. * system would panic in very early stage.
  272. */
  273. gwdt->clk = arch_timer_get_cntfrq();
  274. gwdt->refresh_base = rf_base;
  275. gwdt->control_base = cf_base;
  276. wdd = &gwdt->wdd;
  277. wdd->parent = dev;
  278. wdd->info = &sbsa_gwdt_info;
  279. wdd->ops = &sbsa_gwdt_ops;
  280. wdd->min_timeout = 1;
  281. wdd->timeout = DEFAULT_TIMEOUT;
  282. watchdog_set_drvdata(wdd, gwdt);
  283. watchdog_set_nowayout(wdd, nowayout);
  284. sbsa_gwdt_get_version(wdd);
  285. if (gwdt->version == 0)
  286. wdd->max_hw_heartbeat_ms = U32_MAX / gwdt->clk * 1000;
  287. else
  288. wdd->max_hw_heartbeat_ms = GENMASK_ULL(47, 0) / gwdt->clk * 1000;
  289. if (gwdt->need_ws0_race_workaround) {
  290. /*
  291. * A timeout of 3 seconds means that WOR will be set to 1.5
  292. * seconds and the heartbeat will be scheduled every 2.5
  293. * seconds.
  294. */
  295. wdd->min_timeout = 3;
  296. }
  297. status = readl(cf_base + SBSA_GWDT_WCS);
  298. if (status & SBSA_GWDT_WCS_WS1) {
  299. dev_warn(dev, "System reset by WDT.\n");
  300. wdd->bootstatus |= WDIOF_CARDRESET;
  301. }
  302. if (status & SBSA_GWDT_WCS_EN)
  303. set_bit(WDOG_HW_RUNNING, &wdd->status);
  304. if (action) {
  305. irq = platform_get_irq(pdev, 0);
  306. if (irq < 0) {
  307. action = 0;
  308. dev_warn(dev, "unable to get ws0 interrupt.\n");
  309. } else {
  310. /*
  311. * In case there is a pending ws0 interrupt, just ping
  312. * the watchdog before registering the interrupt routine
  313. */
  314. writel(0, rf_base + SBSA_GWDT_WRR);
  315. if (devm_request_irq(dev, irq, sbsa_gwdt_interrupt, 0,
  316. pdev->name, gwdt)) {
  317. action = 0;
  318. dev_warn(dev, "unable to request IRQ %d.\n",
  319. irq);
  320. }
  321. }
  322. if (!action)
  323. dev_warn(dev, "falling back to single stage mode.\n");
  324. }
  325. /*
  326. * In the single stage mode, The first signal (WS0) is ignored,
  327. * the timeout is (WOR * 2), so the maximum timeout should be doubled.
  328. */
  329. if (!action)
  330. wdd->max_hw_heartbeat_ms *= 2;
  331. watchdog_init_timeout(wdd, timeout, dev);
  332. /*
  333. * Update timeout to WOR.
  334. * Because of the explicit watchdog refresh mechanism,
  335. * it's also a ping, if watchdog is enabled.
  336. */
  337. sbsa_gwdt_set_timeout(wdd, wdd->timeout);
  338. watchdog_stop_on_reboot(wdd);
  339. ret = devm_watchdog_register_device(dev, wdd);
  340. if (ret)
  341. return ret;
  342. dev_info(dev, "Initialized with %ds timeout @ %u Hz, action=%d.%s\n",
  343. wdd->timeout, gwdt->clk, action,
  344. status & SBSA_GWDT_WCS_EN ? " [enabled]" : "");
  345. return 0;
  346. }
  347. /* Disable watchdog if it is active during suspend */
  348. static int __maybe_unused sbsa_gwdt_suspend(struct device *dev)
  349. {
  350. struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
  351. if (watchdog_hw_running(&gwdt->wdd))
  352. sbsa_gwdt_stop(&gwdt->wdd);
  353. return 0;
  354. }
  355. /* Enable watchdog if necessary */
  356. static int __maybe_unused sbsa_gwdt_resume(struct device *dev)
  357. {
  358. struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
  359. if (watchdog_hw_running(&gwdt->wdd))
  360. sbsa_gwdt_start(&gwdt->wdd);
  361. return 0;
  362. }
  363. static const struct dev_pm_ops sbsa_gwdt_pm_ops = {
  364. SET_SYSTEM_SLEEP_PM_OPS(sbsa_gwdt_suspend, sbsa_gwdt_resume)
  365. };
  366. static const struct of_device_id sbsa_gwdt_of_match[] = {
  367. { .compatible = "arm,sbsa-gwdt", },
  368. {},
  369. };
  370. MODULE_DEVICE_TABLE(of, sbsa_gwdt_of_match);
  371. static const struct platform_device_id sbsa_gwdt_pdev_match[] = {
  372. { .name = DRV_NAME, },
  373. {},
  374. };
  375. MODULE_DEVICE_TABLE(platform, sbsa_gwdt_pdev_match);
  376. static struct platform_driver sbsa_gwdt_driver = {
  377. .driver = {
  378. .name = DRV_NAME,
  379. .pm = &sbsa_gwdt_pm_ops,
  380. .of_match_table = sbsa_gwdt_of_match,
  381. },
  382. .probe = sbsa_gwdt_probe,
  383. .id_table = sbsa_gwdt_pdev_match,
  384. };
  385. module_platform_driver(sbsa_gwdt_driver);
  386. MODULE_DESCRIPTION("SBSA Generic Watchdog Driver");
  387. MODULE_AUTHOR("Fu Wei <fu.wei@linaro.org>");
  388. MODULE_AUTHOR("Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>");
  389. MODULE_AUTHOR("Al Stone <al.stone@linaro.org>");
  390. MODULE_AUTHOR("Timur Tabi <timur@codeaurora.org>");
  391. MODULE_LICENSE("GPL v2");