tegra_wdt.c 7.7 KB

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