sbsa_gwdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <[email protected]>
  7. * Suravee Suthikulpanit <[email protected]>
  8. * Al Stone <[email protected]>
  9. * Timur Tabi <[email protected]>
  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/module.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/of.h>
  48. #include <linux/of_device.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/uaccess.h>
  51. #include <linux/watchdog.h>
  52. #include <asm/arch_timer.h>
  53. #define DRV_NAME "sbsa-gwdt"
  54. #define WATCHDOG_NAME "SBSA Generic Watchdog"
  55. /* SBSA Generic Watchdog register definitions */
  56. /* refresh frame */
  57. #define SBSA_GWDT_WRR 0x000
  58. /* control frame */
  59. #define SBSA_GWDT_WCS 0x000
  60. #define SBSA_GWDT_WOR 0x008
  61. #define SBSA_GWDT_WCV 0x010
  62. /* refresh/control frame */
  63. #define SBSA_GWDT_W_IIDR 0xfcc
  64. #define SBSA_GWDT_IDR 0xfd0
  65. /* Watchdog Control and Status Register */
  66. #define SBSA_GWDT_WCS_EN BIT(0)
  67. #define SBSA_GWDT_WCS_WS0 BIT(1)
  68. #define SBSA_GWDT_WCS_WS1 BIT(2)
  69. #define SBSA_GWDT_VERSION_MASK 0xF
  70. #define SBSA_GWDT_VERSION_SHIFT 16
  71. /**
  72. * struct sbsa_gwdt - Internal representation of the SBSA GWDT
  73. * @wdd: kernel watchdog_device structure
  74. * @clk: store the System Counter clock frequency, in Hz.
  75. * @version: store the architecture version
  76. * @refresh_base: Virtual address of the watchdog refresh frame
  77. * @control_base: Virtual address of the watchdog control frame
  78. */
  79. struct sbsa_gwdt {
  80. struct watchdog_device wdd;
  81. u32 clk;
  82. int version;
  83. void __iomem *refresh_base;
  84. void __iomem *control_base;
  85. };
  86. #define DEFAULT_TIMEOUT 10 /* seconds */
  87. static unsigned int timeout;
  88. module_param(timeout, uint, 0);
  89. MODULE_PARM_DESC(timeout,
  90. "Watchdog timeout in seconds. (>=0, default="
  91. __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  92. /*
  93. * action refers to action taken when watchdog gets WS0
  94. * 0 = skip
  95. * 1 = panic
  96. * defaults to skip (0)
  97. */
  98. static int action;
  99. module_param(action, int, 0);
  100. MODULE_PARM_DESC(action, "after watchdog gets WS0 interrupt, do: "
  101. "0 = skip(*) 1 = panic");
  102. static bool nowayout = WATCHDOG_NOWAYOUT;
  103. module_param(nowayout, bool, S_IRUGO);
  104. MODULE_PARM_DESC(nowayout,
  105. "Watchdog cannot be stopped once started (default="
  106. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  107. /*
  108. * Arm Base System Architecture 1.0 introduces watchdog v1 which
  109. * increases the length watchdog offset register to 48 bits.
  110. * - For version 0: WOR is 32 bits;
  111. * - For version 1: WOR is 48 bits which comprises the register
  112. * offset 0x8 and 0xC, and the bits [63:48] are reserved which are
  113. * Read-As-Zero and Writes-Ignored.
  114. */
  115. static u64 sbsa_gwdt_reg_read(struct sbsa_gwdt *gwdt)
  116. {
  117. if (gwdt->version == 0)
  118. return readl(gwdt->control_base + SBSA_GWDT_WOR);
  119. else
  120. return lo_hi_readq(gwdt->control_base + SBSA_GWDT_WOR);
  121. }
  122. static void sbsa_gwdt_reg_write(u64 val, struct sbsa_gwdt *gwdt)
  123. {
  124. if (gwdt->version == 0)
  125. writel((u32)val, gwdt->control_base + SBSA_GWDT_WOR);
  126. else
  127. lo_hi_writeq(val, gwdt->control_base + SBSA_GWDT_WOR);
  128. }
  129. /*
  130. * watchdog operation functions
  131. */
  132. static int sbsa_gwdt_set_timeout(struct watchdog_device *wdd,
  133. unsigned int timeout)
  134. {
  135. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  136. wdd->timeout = timeout;
  137. timeout = clamp_t(unsigned int, timeout, 1, wdd->max_hw_heartbeat_ms / 1000);
  138. if (action)
  139. sbsa_gwdt_reg_write((u64)gwdt->clk * timeout, gwdt);
  140. else
  141. /*
  142. * In the single stage mode, The first signal (WS0) is ignored,
  143. * the timeout is (WOR * 2), so the WOR should be configured
  144. * to half value of timeout.
  145. */
  146. sbsa_gwdt_reg_write(((u64)gwdt->clk / 2) * timeout, gwdt);
  147. return 0;
  148. }
  149. static unsigned int sbsa_gwdt_get_timeleft(struct watchdog_device *wdd)
  150. {
  151. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  152. u64 timeleft = 0;
  153. /*
  154. * In the single stage mode, if WS0 is deasserted
  155. * (watchdog is in the first stage),
  156. * timeleft = WOR + (WCV - system counter)
  157. */
  158. if (!action &&
  159. !(readl(gwdt->control_base + SBSA_GWDT_WCS) & SBSA_GWDT_WCS_WS0))
  160. timeleft += sbsa_gwdt_reg_read(gwdt);
  161. timeleft += lo_hi_readq(gwdt->control_base + SBSA_GWDT_WCV) -
  162. arch_timer_read_counter();
  163. do_div(timeleft, gwdt->clk);
  164. return timeleft;
  165. }
  166. static int sbsa_gwdt_keepalive(struct watchdog_device *wdd)
  167. {
  168. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  169. /*
  170. * Writing WRR for an explicit watchdog refresh.
  171. * You can write anyting (like 0).
  172. */
  173. writel(0, gwdt->refresh_base + SBSA_GWDT_WRR);
  174. return 0;
  175. }
  176. static void sbsa_gwdt_get_version(struct watchdog_device *wdd)
  177. {
  178. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  179. int ver;
  180. ver = readl(gwdt->control_base + SBSA_GWDT_W_IIDR);
  181. ver = (ver >> SBSA_GWDT_VERSION_SHIFT) & SBSA_GWDT_VERSION_MASK;
  182. gwdt->version = ver;
  183. }
  184. static int sbsa_gwdt_start(struct watchdog_device *wdd)
  185. {
  186. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  187. /* writing WCS will cause an explicit watchdog refresh */
  188. writel(SBSA_GWDT_WCS_EN, gwdt->control_base + SBSA_GWDT_WCS);
  189. return 0;
  190. }
  191. static int sbsa_gwdt_stop(struct watchdog_device *wdd)
  192. {
  193. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  194. /* Simply write 0 to WCS to clean WCS_EN bit */
  195. writel(0, gwdt->control_base + SBSA_GWDT_WCS);
  196. return 0;
  197. }
  198. static irqreturn_t sbsa_gwdt_interrupt(int irq, void *dev_id)
  199. {
  200. panic(WATCHDOG_NAME " timeout");
  201. return IRQ_HANDLED;
  202. }
  203. static const struct watchdog_info sbsa_gwdt_info = {
  204. .identity = WATCHDOG_NAME,
  205. .options = WDIOF_SETTIMEOUT |
  206. WDIOF_KEEPALIVEPING |
  207. WDIOF_MAGICCLOSE |
  208. WDIOF_CARDRESET,
  209. };
  210. static const struct watchdog_ops sbsa_gwdt_ops = {
  211. .owner = THIS_MODULE,
  212. .start = sbsa_gwdt_start,
  213. .stop = sbsa_gwdt_stop,
  214. .ping = sbsa_gwdt_keepalive,
  215. .set_timeout = sbsa_gwdt_set_timeout,
  216. .get_timeleft = sbsa_gwdt_get_timeleft,
  217. };
  218. static int sbsa_gwdt_probe(struct platform_device *pdev)
  219. {
  220. void __iomem *rf_base, *cf_base;
  221. struct device *dev = &pdev->dev;
  222. struct watchdog_device *wdd;
  223. struct sbsa_gwdt *gwdt;
  224. int ret, irq;
  225. u32 status;
  226. gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
  227. if (!gwdt)
  228. return -ENOMEM;
  229. platform_set_drvdata(pdev, gwdt);
  230. cf_base = devm_platform_ioremap_resource(pdev, 0);
  231. if (IS_ERR(cf_base))
  232. return PTR_ERR(cf_base);
  233. rf_base = devm_platform_ioremap_resource(pdev, 1);
  234. if (IS_ERR(rf_base))
  235. return PTR_ERR(rf_base);
  236. /*
  237. * Get the frequency of system counter from the cp15 interface of ARM
  238. * Generic timer. We don't need to check it, because if it returns "0",
  239. * system would panic in very early stage.
  240. */
  241. gwdt->clk = arch_timer_get_cntfrq();
  242. gwdt->refresh_base = rf_base;
  243. gwdt->control_base = cf_base;
  244. wdd = &gwdt->wdd;
  245. wdd->parent = dev;
  246. wdd->info = &sbsa_gwdt_info;
  247. wdd->ops = &sbsa_gwdt_ops;
  248. wdd->min_timeout = 1;
  249. wdd->timeout = DEFAULT_TIMEOUT;
  250. watchdog_set_drvdata(wdd, gwdt);
  251. watchdog_set_nowayout(wdd, nowayout);
  252. sbsa_gwdt_get_version(wdd);
  253. if (gwdt->version == 0)
  254. wdd->max_hw_heartbeat_ms = U32_MAX / gwdt->clk * 1000;
  255. else
  256. wdd->max_hw_heartbeat_ms = GENMASK_ULL(47, 0) / gwdt->clk * 1000;
  257. status = readl(cf_base + SBSA_GWDT_WCS);
  258. if (status & SBSA_GWDT_WCS_WS1) {
  259. dev_warn(dev, "System reset by WDT.\n");
  260. wdd->bootstatus |= WDIOF_CARDRESET;
  261. }
  262. if (status & SBSA_GWDT_WCS_EN)
  263. set_bit(WDOG_HW_RUNNING, &wdd->status);
  264. if (action) {
  265. irq = platform_get_irq(pdev, 0);
  266. if (irq < 0) {
  267. action = 0;
  268. dev_warn(dev, "unable to get ws0 interrupt.\n");
  269. } else {
  270. /*
  271. * In case there is a pending ws0 interrupt, just ping
  272. * the watchdog before registering the interrupt routine
  273. */
  274. writel(0, rf_base + SBSA_GWDT_WRR);
  275. if (devm_request_irq(dev, irq, sbsa_gwdt_interrupt, 0,
  276. pdev->name, gwdt)) {
  277. action = 0;
  278. dev_warn(dev, "unable to request IRQ %d.\n",
  279. irq);
  280. }
  281. }
  282. if (!action)
  283. dev_warn(dev, "falling back to single stage mode.\n");
  284. }
  285. /*
  286. * In the single stage mode, The first signal (WS0) is ignored,
  287. * the timeout is (WOR * 2), so the maximum timeout should be doubled.
  288. */
  289. if (!action)
  290. wdd->max_hw_heartbeat_ms *= 2;
  291. watchdog_init_timeout(wdd, timeout, dev);
  292. /*
  293. * Update timeout to WOR.
  294. * Because of the explicit watchdog refresh mechanism,
  295. * it's also a ping, if watchdog is enabled.
  296. */
  297. sbsa_gwdt_set_timeout(wdd, wdd->timeout);
  298. watchdog_stop_on_reboot(wdd);
  299. ret = devm_watchdog_register_device(dev, wdd);
  300. if (ret)
  301. return ret;
  302. dev_info(dev, "Initialized with %ds timeout @ %u Hz, action=%d.%s\n",
  303. wdd->timeout, gwdt->clk, action,
  304. status & SBSA_GWDT_WCS_EN ? " [enabled]" : "");
  305. return 0;
  306. }
  307. /* Disable watchdog if it is active during suspend */
  308. static int __maybe_unused sbsa_gwdt_suspend(struct device *dev)
  309. {
  310. struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
  311. if (watchdog_active(&gwdt->wdd))
  312. sbsa_gwdt_stop(&gwdt->wdd);
  313. return 0;
  314. }
  315. /* Enable watchdog if necessary */
  316. static int __maybe_unused sbsa_gwdt_resume(struct device *dev)
  317. {
  318. struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
  319. if (watchdog_active(&gwdt->wdd))
  320. sbsa_gwdt_start(&gwdt->wdd);
  321. return 0;
  322. }
  323. static const struct dev_pm_ops sbsa_gwdt_pm_ops = {
  324. SET_SYSTEM_SLEEP_PM_OPS(sbsa_gwdt_suspend, sbsa_gwdt_resume)
  325. };
  326. static const struct of_device_id sbsa_gwdt_of_match[] = {
  327. { .compatible = "arm,sbsa-gwdt", },
  328. {},
  329. };
  330. MODULE_DEVICE_TABLE(of, sbsa_gwdt_of_match);
  331. static const struct platform_device_id sbsa_gwdt_pdev_match[] = {
  332. { .name = DRV_NAME, },
  333. {},
  334. };
  335. MODULE_DEVICE_TABLE(platform, sbsa_gwdt_pdev_match);
  336. static struct platform_driver sbsa_gwdt_driver = {
  337. .driver = {
  338. .name = DRV_NAME,
  339. .pm = &sbsa_gwdt_pm_ops,
  340. .of_match_table = sbsa_gwdt_of_match,
  341. },
  342. .probe = sbsa_gwdt_probe,
  343. .id_table = sbsa_gwdt_pdev_match,
  344. };
  345. module_platform_driver(sbsa_gwdt_driver);
  346. MODULE_DESCRIPTION("SBSA Generic Watchdog Driver");
  347. MODULE_AUTHOR("Fu Wei <[email protected]>");
  348. MODULE_AUTHOR("Suravee Suthikulpanit <[email protected]>");
  349. MODULE_AUTHOR("Al Stone <[email protected]>");
  350. MODULE_AUTHOR("Timur Tabi <[email protected]>");
  351. MODULE_LICENSE("GPL v2");