sbsa_gwdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * SBSA(Server Base System Architecture) Generic Watchdog driver
  3. *
  4. * Copyright (c) 2015, Linaro Ltd.
  5. * Author: Fu Wei <fu.wei@linaro.org>
  6. * Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>
  7. * Al Stone <al.stone@linaro.org>
  8. * Timur Tabi <timur@codeaurora.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * ARM SBSA Generic Watchdog has two stage timeouts:
  20. * the first signal (WS0) is for alerting the system by interrupt,
  21. * the second one (WS1) is a real hardware reset.
  22. * More details about the hardware specification of this device:
  23. * ARM DEN0029B - Server Base System Architecture (SBSA)
  24. *
  25. * This driver can operate ARM SBSA Generic Watchdog as a single stage watchdog
  26. * or a two stages watchdog, it's set up by the module parameter "action".
  27. * In the single stage mode, when the timeout is reached, your system
  28. * will be reset by WS1. The first signal (WS0) is ignored.
  29. * In the two stages mode, when the timeout is reached, the first signal (WS0)
  30. * will trigger panic. If the system is getting into trouble and cannot be reset
  31. * by panic or restart properly by the kdump kernel(if supported), then the
  32. * second stage (as long as the first stage) will be reached, system will be
  33. * reset by WS1. This function can help administrator to backup the system
  34. * context info by panic console output or kdump.
  35. *
  36. * SBSA GWDT:
  37. * if action is 1 (the two stages mode):
  38. * |--------WOR-------WS0--------WOR-------WS1
  39. * |----timeout-----(panic)----timeout-----reset
  40. *
  41. * if action is 0 (the single stage mode):
  42. * |------WOR-----WS0(ignored)-----WOR------WS1
  43. * |--------------timeout-------------------reset
  44. *
  45. * Note: Since this watchdog timer has two stages, and each stage is determined
  46. * by WOR, in the single stage mode, the timeout is (WOR * 2); in the two
  47. * stages mode, the timeout is WOR. The maximum timeout in the two stages mode
  48. * is half of that in the single stage mode.
  49. *
  50. */
  51. #include <linux/io.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/module.h>
  54. #include <linux/moduleparam.h>
  55. #include <linux/of.h>
  56. #include <linux/of_device.h>
  57. #include <linux/platform_device.h>
  58. #include <linux/uaccess.h>
  59. #include <linux/watchdog.h>
  60. #include <asm/arch_timer.h>
  61. #define DRV_NAME "sbsa-gwdt"
  62. #define WATCHDOG_NAME "SBSA Generic Watchdog"
  63. /* SBSA Generic Watchdog register definitions */
  64. /* refresh frame */
  65. #define SBSA_GWDT_WRR 0x000
  66. /* control frame */
  67. #define SBSA_GWDT_WCS 0x000
  68. #define SBSA_GWDT_WOR 0x008
  69. #define SBSA_GWDT_WCV 0x010
  70. /* refresh/control frame */
  71. #define SBSA_GWDT_W_IIDR 0xfcc
  72. #define SBSA_GWDT_IDR 0xfd0
  73. /* Watchdog Control and Status Register */
  74. #define SBSA_GWDT_WCS_EN BIT(0)
  75. #define SBSA_GWDT_WCS_WS0 BIT(1)
  76. #define SBSA_GWDT_WCS_WS1 BIT(2)
  77. /**
  78. * struct sbsa_gwdt - Internal representation of the SBSA GWDT
  79. * @wdd: kernel watchdog_device structure
  80. * @clk: store the System Counter clock frequency, in Hz.
  81. * @refresh_base: Virtual address of the watchdog refresh frame
  82. * @control_base: Virtual address of the watchdog control frame
  83. */
  84. struct sbsa_gwdt {
  85. struct watchdog_device wdd;
  86. u32 clk;
  87. void __iomem *refresh_base;
  88. void __iomem *control_base;
  89. };
  90. #define DEFAULT_TIMEOUT 10 /* seconds */
  91. static unsigned int timeout;
  92. module_param(timeout, uint, 0);
  93. MODULE_PARM_DESC(timeout,
  94. "Watchdog timeout in seconds. (>=0, default="
  95. __MODULE_STRING(DEFAULT_TIMEOUT) ")");
  96. /*
  97. * action refers to action taken when watchdog gets WS0
  98. * 0 = skip
  99. * 1 = panic
  100. * defaults to skip (0)
  101. */
  102. static int action;
  103. module_param(action, int, 0);
  104. MODULE_PARM_DESC(action, "after watchdog gets WS0 interrupt, do: "
  105. "0 = skip(*) 1 = panic");
  106. static bool nowayout = WATCHDOG_NOWAYOUT;
  107. module_param(nowayout, bool, S_IRUGO);
  108. MODULE_PARM_DESC(nowayout,
  109. "Watchdog cannot be stopped once started (default="
  110. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  111. /*
  112. * watchdog operation functions
  113. */
  114. static int sbsa_gwdt_set_timeout(struct watchdog_device *wdd,
  115. unsigned int timeout)
  116. {
  117. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  118. wdd->timeout = timeout;
  119. if (action)
  120. writel(gwdt->clk * timeout,
  121. gwdt->control_base + SBSA_GWDT_WOR);
  122. else
  123. /*
  124. * In the single stage mode, The first signal (WS0) is ignored,
  125. * the timeout is (WOR * 2), so the WOR should be configured
  126. * to half value of timeout.
  127. */
  128. writel(gwdt->clk / 2 * timeout,
  129. gwdt->control_base + SBSA_GWDT_WOR);
  130. return 0;
  131. }
  132. static unsigned int sbsa_gwdt_get_timeleft(struct watchdog_device *wdd)
  133. {
  134. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  135. u64 timeleft = 0;
  136. /*
  137. * In the single stage mode, if WS0 is deasserted
  138. * (watchdog is in the first stage),
  139. * timeleft = WOR + (WCV - system counter)
  140. */
  141. if (!action &&
  142. !(readl(gwdt->control_base + SBSA_GWDT_WCS) & SBSA_GWDT_WCS_WS0))
  143. timeleft += readl(gwdt->control_base + SBSA_GWDT_WOR);
  144. timeleft += readq(gwdt->control_base + SBSA_GWDT_WCV) -
  145. arch_counter_get_cntvct();
  146. do_div(timeleft, gwdt->clk);
  147. return timeleft;
  148. }
  149. static int sbsa_gwdt_keepalive(struct watchdog_device *wdd)
  150. {
  151. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  152. /*
  153. * Writing WRR for an explicit watchdog refresh.
  154. * You can write anyting (like 0).
  155. */
  156. writel(0, gwdt->refresh_base + SBSA_GWDT_WRR);
  157. return 0;
  158. }
  159. static int sbsa_gwdt_start(struct watchdog_device *wdd)
  160. {
  161. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  162. /* writing WCS will cause an explicit watchdog refresh */
  163. writel(SBSA_GWDT_WCS_EN, gwdt->control_base + SBSA_GWDT_WCS);
  164. return 0;
  165. }
  166. static int sbsa_gwdt_stop(struct watchdog_device *wdd)
  167. {
  168. struct sbsa_gwdt *gwdt = watchdog_get_drvdata(wdd);
  169. /* Simply write 0 to WCS to clean WCS_EN bit */
  170. writel(0, gwdt->control_base + SBSA_GWDT_WCS);
  171. return 0;
  172. }
  173. static irqreturn_t sbsa_gwdt_interrupt(int irq, void *dev_id)
  174. {
  175. panic(WATCHDOG_NAME " timeout");
  176. return IRQ_HANDLED;
  177. }
  178. static struct watchdog_info sbsa_gwdt_info = {
  179. .identity = WATCHDOG_NAME,
  180. .options = WDIOF_SETTIMEOUT |
  181. WDIOF_KEEPALIVEPING |
  182. WDIOF_MAGICCLOSE |
  183. WDIOF_CARDRESET,
  184. };
  185. static struct watchdog_ops sbsa_gwdt_ops = {
  186. .owner = THIS_MODULE,
  187. .start = sbsa_gwdt_start,
  188. .stop = sbsa_gwdt_stop,
  189. .ping = sbsa_gwdt_keepalive,
  190. .set_timeout = sbsa_gwdt_set_timeout,
  191. .get_timeleft = sbsa_gwdt_get_timeleft,
  192. };
  193. static int sbsa_gwdt_probe(struct platform_device *pdev)
  194. {
  195. void __iomem *rf_base, *cf_base;
  196. struct device *dev = &pdev->dev;
  197. struct watchdog_device *wdd;
  198. struct sbsa_gwdt *gwdt;
  199. struct resource *res;
  200. int ret, irq;
  201. u32 status;
  202. gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
  203. if (!gwdt)
  204. return -ENOMEM;
  205. platform_set_drvdata(pdev, gwdt);
  206. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  207. cf_base = devm_ioremap_resource(dev, res);
  208. if (IS_ERR(cf_base))
  209. return PTR_ERR(cf_base);
  210. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  211. rf_base = devm_ioremap_resource(dev, res);
  212. if (IS_ERR(rf_base))
  213. return PTR_ERR(rf_base);
  214. /*
  215. * Get the frequency of system counter from the cp15 interface of ARM
  216. * Generic timer. We don't need to check it, because if it returns "0",
  217. * system would panic in very early stage.
  218. */
  219. gwdt->clk = arch_timer_get_cntfrq();
  220. gwdt->refresh_base = rf_base;
  221. gwdt->control_base = cf_base;
  222. wdd = &gwdt->wdd;
  223. wdd->parent = dev;
  224. wdd->info = &sbsa_gwdt_info;
  225. wdd->ops = &sbsa_gwdt_ops;
  226. wdd->min_timeout = 1;
  227. wdd->max_hw_heartbeat_ms = U32_MAX / gwdt->clk * 1000;
  228. wdd->timeout = DEFAULT_TIMEOUT;
  229. watchdog_set_drvdata(wdd, gwdt);
  230. watchdog_set_nowayout(wdd, nowayout);
  231. status = readl(cf_base + SBSA_GWDT_WCS);
  232. if (status & SBSA_GWDT_WCS_WS1) {
  233. dev_warn(dev, "System reset by WDT.\n");
  234. wdd->bootstatus |= WDIOF_CARDRESET;
  235. }
  236. if (status & SBSA_GWDT_WCS_EN)
  237. set_bit(WDOG_HW_RUNNING, &wdd->status);
  238. if (action) {
  239. irq = platform_get_irq(pdev, 0);
  240. if (irq < 0) {
  241. action = 0;
  242. dev_warn(dev, "unable to get ws0 interrupt.\n");
  243. } else {
  244. /*
  245. * In case there is a pending ws0 interrupt, just ping
  246. * the watchdog before registering the interrupt routine
  247. */
  248. writel(0, rf_base + SBSA_GWDT_WRR);
  249. if (devm_request_irq(dev, irq, sbsa_gwdt_interrupt, 0,
  250. pdev->name, gwdt)) {
  251. action = 0;
  252. dev_warn(dev, "unable to request IRQ %d.\n",
  253. irq);
  254. }
  255. }
  256. if (!action)
  257. dev_warn(dev, "falling back to single stage mode.\n");
  258. }
  259. /*
  260. * In the single stage mode, The first signal (WS0) is ignored,
  261. * the timeout is (WOR * 2), so the maximum timeout should be doubled.
  262. */
  263. if (!action)
  264. wdd->max_hw_heartbeat_ms *= 2;
  265. watchdog_init_timeout(wdd, timeout, dev);
  266. /*
  267. * Update timeout to WOR.
  268. * Because of the explicit watchdog refresh mechanism,
  269. * it's also a ping, if watchdog is enabled.
  270. */
  271. sbsa_gwdt_set_timeout(wdd, wdd->timeout);
  272. ret = watchdog_register_device(wdd);
  273. if (ret)
  274. return ret;
  275. dev_info(dev, "Initialized with %ds timeout @ %u Hz, action=%d.%s\n",
  276. wdd->timeout, gwdt->clk, action,
  277. status & SBSA_GWDT_WCS_EN ? " [enabled]" : "");
  278. return 0;
  279. }
  280. static void sbsa_gwdt_shutdown(struct platform_device *pdev)
  281. {
  282. struct sbsa_gwdt *gwdt = platform_get_drvdata(pdev);
  283. sbsa_gwdt_stop(&gwdt->wdd);
  284. }
  285. static int sbsa_gwdt_remove(struct platform_device *pdev)
  286. {
  287. struct sbsa_gwdt *gwdt = platform_get_drvdata(pdev);
  288. watchdog_unregister_device(&gwdt->wdd);
  289. return 0;
  290. }
  291. /* Disable watchdog if it is active during suspend */
  292. static int __maybe_unused sbsa_gwdt_suspend(struct device *dev)
  293. {
  294. struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
  295. if (watchdog_active(&gwdt->wdd))
  296. sbsa_gwdt_stop(&gwdt->wdd);
  297. return 0;
  298. }
  299. /* Enable watchdog if necessary */
  300. static int __maybe_unused sbsa_gwdt_resume(struct device *dev)
  301. {
  302. struct sbsa_gwdt *gwdt = dev_get_drvdata(dev);
  303. if (watchdog_active(&gwdt->wdd))
  304. sbsa_gwdt_start(&gwdt->wdd);
  305. return 0;
  306. }
  307. static const struct dev_pm_ops sbsa_gwdt_pm_ops = {
  308. SET_SYSTEM_SLEEP_PM_OPS(sbsa_gwdt_suspend, sbsa_gwdt_resume)
  309. };
  310. static const struct of_device_id sbsa_gwdt_of_match[] = {
  311. { .compatible = "arm,sbsa-gwdt", },
  312. {},
  313. };
  314. MODULE_DEVICE_TABLE(of, sbsa_gwdt_of_match);
  315. static const struct platform_device_id sbsa_gwdt_pdev_match[] = {
  316. { .name = DRV_NAME, },
  317. {},
  318. };
  319. MODULE_DEVICE_TABLE(platform, sbsa_gwdt_pdev_match);
  320. static struct platform_driver sbsa_gwdt_driver = {
  321. .driver = {
  322. .name = DRV_NAME,
  323. .pm = &sbsa_gwdt_pm_ops,
  324. .of_match_table = sbsa_gwdt_of_match,
  325. },
  326. .probe = sbsa_gwdt_probe,
  327. .remove = sbsa_gwdt_remove,
  328. .shutdown = sbsa_gwdt_shutdown,
  329. .id_table = sbsa_gwdt_pdev_match,
  330. };
  331. module_platform_driver(sbsa_gwdt_driver);
  332. MODULE_DESCRIPTION("SBSA Generic Watchdog Driver");
  333. MODULE_AUTHOR("Fu Wei <fu.wei@linaro.org>");
  334. MODULE_AUTHOR("Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>");
  335. MODULE_AUTHOR("Al Stone <al.stone@linaro.org>");
  336. MODULE_AUTHOR("Timur Tabi <timur@codeaurora.org>");
  337. MODULE_LICENSE("GPL v2");
  338. MODULE_ALIAS("platform:" DRV_NAME);