tegra_wdt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/watchdog.h>
  12. /* minimum and maximum watchdog trigger timeout, in seconds */
  13. #define MIN_WDT_TIMEOUT 1
  14. #define MAX_WDT_TIMEOUT 255
  15. /*
  16. * Base of the WDT registers, from the timer base address. There are
  17. * actually 5 watchdogs that can be configured (by pairing with an available
  18. * timer), at bases 0x100 + (WDT ID) * 0x20, where WDT ID is 0 through 4.
  19. * This driver only configures the first watchdog (WDT ID 0).
  20. */
  21. #define WDT_BASE 0x100
  22. #define WDT_ID 0
  23. /*
  24. * Register base of the timer that's selected for pairing with the watchdog.
  25. * This driver arbitrarily uses timer 5, which is currently unused by
  26. * other drivers (in particular, the Tegra clocksource driver). If this
  27. * needs to change, take care that the new timer is not used by the
  28. * clocksource driver.
  29. */
  30. #define WDT_TIMER_BASE 0x60
  31. #define WDT_TIMER_ID 5
  32. /* WDT registers */
  33. #define WDT_CFG 0x0
  34. #define WDT_CFG_PERIOD_SHIFT 4
  35. #define WDT_CFG_PERIOD_MASK 0xff
  36. #define WDT_CFG_INT_EN (1 << 12)
  37. #define WDT_CFG_PMC2CAR_RST_EN (1 << 15)
  38. #define WDT_STS 0x4
  39. #define WDT_STS_COUNT_SHIFT 4
  40. #define WDT_STS_COUNT_MASK 0xff
  41. #define WDT_STS_EXP_SHIFT 12
  42. #define WDT_STS_EXP_MASK 0x3
  43. #define WDT_CMD 0x8
  44. #define WDT_CMD_START_COUNTER (1 << 0)
  45. #define WDT_CMD_DISABLE_COUNTER (1 << 1)
  46. #define WDT_UNLOCK (0xc)
  47. #define WDT_UNLOCK_PATTERN (0xc45a << 0)
  48. /* Timer registers */
  49. #define TIMER_PTV 0x0
  50. #define TIMER_EN (1 << 31)
  51. #define TIMER_PERIODIC (1 << 30)
  52. struct tegra_wdt {
  53. struct watchdog_device wdd;
  54. void __iomem *wdt_regs;
  55. void __iomem *tmr_regs;
  56. };
  57. #define WDT_HEARTBEAT 120
  58. static int heartbeat = WDT_HEARTBEAT;
  59. module_param(heartbeat, int, 0);
  60. MODULE_PARM_DESC(heartbeat,
  61. "Watchdog heartbeats in seconds. (default = "
  62. __MODULE_STRING(WDT_HEARTBEAT) ")");
  63. static bool nowayout = WATCHDOG_NOWAYOUT;
  64. module_param(nowayout, bool, 0);
  65. MODULE_PARM_DESC(nowayout,
  66. "Watchdog cannot be stopped once started (default="
  67. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  68. static int tegra_wdt_start(struct watchdog_device *wdd)
  69. {
  70. struct tegra_wdt *wdt = watchdog_get_drvdata(wdd);
  71. u32 val;
  72. /*
  73. * This thing has a fixed 1MHz clock. Normally, we would set the
  74. * period to 1 second by writing 1000000ul, but the watchdog system
  75. * reset actually occurs on the 4th expiration of this counter,
  76. * so we set the period to 1/4 of this amount.
  77. */
  78. val = 1000000ul / 4;
  79. val |= (TIMER_EN | TIMER_PERIODIC);
  80. writel(val, wdt->tmr_regs + TIMER_PTV);
  81. /*
  82. * Set number of periods and start counter.
  83. *
  84. * Interrupt handler is not required for user space
  85. * WDT accesses, since the caller is responsible to ping the
  86. * WDT to reset the counter before expiration, through ioctls.
  87. */
  88. val = WDT_TIMER_ID |
  89. (wdd->timeout << WDT_CFG_PERIOD_SHIFT) |
  90. WDT_CFG_PMC2CAR_RST_EN;
  91. writel(val, wdt->wdt_regs + WDT_CFG);
  92. writel(WDT_CMD_START_COUNTER, wdt->wdt_regs + WDT_CMD);
  93. return 0;
  94. }
  95. static int tegra_wdt_stop(struct watchdog_device *wdd)
  96. {
  97. struct tegra_wdt *wdt = watchdog_get_drvdata(wdd);
  98. writel(WDT_UNLOCK_PATTERN, wdt->wdt_regs + WDT_UNLOCK);
  99. writel(WDT_CMD_DISABLE_COUNTER, wdt->wdt_regs + WDT_CMD);
  100. writel(0, wdt->tmr_regs + TIMER_PTV);
  101. return 0;
  102. }
  103. static int tegra_wdt_ping(struct watchdog_device *wdd)
  104. {
  105. struct tegra_wdt *wdt = watchdog_get_drvdata(wdd);
  106. writel(WDT_CMD_START_COUNTER, wdt->wdt_regs + WDT_CMD);
  107. return 0;
  108. }
  109. static int tegra_wdt_set_timeout(struct watchdog_device *wdd,
  110. unsigned int timeout)
  111. {
  112. wdd->timeout = timeout;
  113. if (watchdog_active(wdd)) {
  114. tegra_wdt_stop(wdd);
  115. return tegra_wdt_start(wdd);
  116. }
  117. return 0;
  118. }
  119. static unsigned int tegra_wdt_get_timeleft(struct watchdog_device *wdd)
  120. {
  121. struct tegra_wdt *wdt = watchdog_get_drvdata(wdd);
  122. u32 val;
  123. int count;
  124. int exp;
  125. val = readl(wdt->wdt_regs + WDT_STS);
  126. /* Current countdown (from timeout) */
  127. count = (val >> WDT_STS_COUNT_SHIFT) & WDT_STS_COUNT_MASK;
  128. /* Number of expirations (we are waiting for the 4th expiration) */
  129. exp = (val >> WDT_STS_EXP_SHIFT) & WDT_STS_EXP_MASK;
  130. /*
  131. * The entire thing is divided by 4 because we are ticking down 4 times
  132. * faster due to needing to wait for the 4th expiration.
  133. */
  134. return (((3 - exp) * wdd->timeout) + count) / 4;
  135. }
  136. static const struct watchdog_info tegra_wdt_info = {
  137. .options = WDIOF_SETTIMEOUT |
  138. WDIOF_MAGICCLOSE |
  139. WDIOF_KEEPALIVEPING,
  140. .firmware_version = 0,
  141. .identity = "Tegra Watchdog",
  142. };
  143. static const struct watchdog_ops tegra_wdt_ops = {
  144. .owner = THIS_MODULE,
  145. .start = tegra_wdt_start,
  146. .stop = tegra_wdt_stop,
  147. .ping = tegra_wdt_ping,
  148. .set_timeout = tegra_wdt_set_timeout,
  149. .get_timeleft = tegra_wdt_get_timeleft,
  150. };
  151. static int tegra_wdt_probe(struct platform_device *pdev)
  152. {
  153. struct watchdog_device *wdd;
  154. struct tegra_wdt *wdt;
  155. struct resource *res;
  156. void __iomem *regs;
  157. int ret;
  158. /* This is the timer base. */
  159. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  160. regs = devm_ioremap_resource(&pdev->dev, res);
  161. if (IS_ERR(regs))
  162. return PTR_ERR(regs);
  163. /*
  164. * Allocate our watchdog driver data, which has the
  165. * struct watchdog_device nested within it.
  166. */
  167. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  168. if (!wdt)
  169. return -ENOMEM;
  170. /* Initialize struct tegra_wdt. */
  171. wdt->wdt_regs = regs + WDT_BASE;
  172. wdt->tmr_regs = regs + WDT_TIMER_BASE;
  173. /* Initialize struct watchdog_device. */
  174. wdd = &wdt->wdd;
  175. wdd->timeout = heartbeat;
  176. wdd->info = &tegra_wdt_info;
  177. wdd->ops = &tegra_wdt_ops;
  178. wdd->min_timeout = MIN_WDT_TIMEOUT;
  179. wdd->max_timeout = MAX_WDT_TIMEOUT;
  180. wdd->parent = &pdev->dev;
  181. watchdog_set_drvdata(wdd, wdt);
  182. watchdog_set_nowayout(wdd, nowayout);
  183. ret = devm_watchdog_register_device(&pdev->dev, wdd);
  184. if (ret) {
  185. dev_err(&pdev->dev,
  186. "failed to register watchdog device\n");
  187. return ret;
  188. }
  189. platform_set_drvdata(pdev, wdt);
  190. dev_info(&pdev->dev,
  191. "initialized (heartbeat = %d sec, nowayout = %d)\n",
  192. heartbeat, nowayout);
  193. return 0;
  194. }
  195. static int tegra_wdt_remove(struct platform_device *pdev)
  196. {
  197. struct tegra_wdt *wdt = platform_get_drvdata(pdev);
  198. tegra_wdt_stop(&wdt->wdd);
  199. dev_info(&pdev->dev, "removed wdt\n");
  200. return 0;
  201. }
  202. #ifdef CONFIG_PM_SLEEP
  203. static int tegra_wdt_runtime_suspend(struct device *dev)
  204. {
  205. struct tegra_wdt *wdt = dev_get_drvdata(dev);
  206. if (watchdog_active(&wdt->wdd))
  207. tegra_wdt_stop(&wdt->wdd);
  208. return 0;
  209. }
  210. static int tegra_wdt_runtime_resume(struct device *dev)
  211. {
  212. struct tegra_wdt *wdt = dev_get_drvdata(dev);
  213. if (watchdog_active(&wdt->wdd))
  214. tegra_wdt_start(&wdt->wdd);
  215. return 0;
  216. }
  217. #endif
  218. static const struct of_device_id tegra_wdt_of_match[] = {
  219. { .compatible = "nvidia,tegra30-timer", },
  220. { },
  221. };
  222. MODULE_DEVICE_TABLE(of, tegra_wdt_of_match);
  223. static const struct dev_pm_ops tegra_wdt_pm_ops = {
  224. SET_SYSTEM_SLEEP_PM_OPS(tegra_wdt_runtime_suspend,
  225. tegra_wdt_runtime_resume)
  226. };
  227. static struct platform_driver tegra_wdt_driver = {
  228. .probe = tegra_wdt_probe,
  229. .remove = tegra_wdt_remove,
  230. .driver = {
  231. .name = "tegra-wdt",
  232. .pm = &tegra_wdt_pm_ops,
  233. .of_match_table = tegra_wdt_of_match,
  234. },
  235. };
  236. module_platform_driver(tegra_wdt_driver);
  237. MODULE_AUTHOR("NVIDIA Corporation");
  238. MODULE_DESCRIPTION("Tegra Watchdog Driver");
  239. MODULE_LICENSE("GPL v2");