tegra_wdt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. return tegra_wdt_start(wdd);
  123. return 0;
  124. }
  125. static unsigned int tegra_wdt_get_timeleft(struct watchdog_device *wdd)
  126. {
  127. struct tegra_wdt *wdt = watchdog_get_drvdata(wdd);
  128. u32 val;
  129. int count;
  130. int exp;
  131. val = readl(wdt->wdt_regs + WDT_STS);
  132. /* Current countdown (from timeout) */
  133. count = (val >> WDT_STS_COUNT_SHIFT) & WDT_STS_COUNT_MASK;
  134. /* Number of expirations (we are waiting for the 4th expiration) */
  135. exp = (val >> WDT_STS_EXP_SHIFT) & WDT_STS_EXP_MASK;
  136. /*
  137. * The entire thing is divided by 4 because we are ticking down 4 times
  138. * faster due to needing to wait for the 4th expiration.
  139. */
  140. return (((3 - exp) * wdd->timeout) + count) / 4;
  141. }
  142. static const struct watchdog_info tegra_wdt_info = {
  143. .options = WDIOF_SETTIMEOUT |
  144. WDIOF_MAGICCLOSE |
  145. WDIOF_KEEPALIVEPING,
  146. .firmware_version = 0,
  147. .identity = "Tegra Watchdog",
  148. };
  149. static struct watchdog_ops tegra_wdt_ops = {
  150. .owner = THIS_MODULE,
  151. .start = tegra_wdt_start,
  152. .stop = tegra_wdt_stop,
  153. .ping = tegra_wdt_ping,
  154. .set_timeout = tegra_wdt_set_timeout,
  155. .get_timeleft = tegra_wdt_get_timeleft,
  156. };
  157. static int tegra_wdt_probe(struct platform_device *pdev)
  158. {
  159. struct watchdog_device *wdd;
  160. struct tegra_wdt *wdt;
  161. struct resource *res;
  162. void __iomem *regs;
  163. int ret;
  164. /* This is the timer base. */
  165. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. regs = devm_ioremap_resource(&pdev->dev, res);
  167. if (IS_ERR(regs))
  168. return PTR_ERR(regs);
  169. /*
  170. * Allocate our watchdog driver data, which has the
  171. * struct watchdog_device nested within it.
  172. */
  173. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  174. if (!wdt)
  175. return -ENOMEM;
  176. /* Initialize struct tegra_wdt. */
  177. wdt->wdt_regs = regs + WDT_BASE;
  178. wdt->tmr_regs = regs + WDT_TIMER_BASE;
  179. /* Initialize struct watchdog_device. */
  180. wdd = &wdt->wdd;
  181. wdd->timeout = heartbeat;
  182. wdd->info = &tegra_wdt_info;
  183. wdd->ops = &tegra_wdt_ops;
  184. wdd->min_timeout = MIN_WDT_TIMEOUT;
  185. wdd->max_timeout = MAX_WDT_TIMEOUT;
  186. watchdog_set_drvdata(wdd, wdt);
  187. watchdog_set_nowayout(wdd, nowayout);
  188. ret = watchdog_register_device(wdd);
  189. if (ret) {
  190. dev_err(&pdev->dev,
  191. "failed to register watchdog device\n");
  192. return ret;
  193. }
  194. platform_set_drvdata(pdev, wdt);
  195. dev_info(&pdev->dev,
  196. "initialized (heartbeat = %d sec, nowayout = %d)\n",
  197. heartbeat, nowayout);
  198. return 0;
  199. }
  200. static int tegra_wdt_remove(struct platform_device *pdev)
  201. {
  202. struct tegra_wdt *wdt = platform_get_drvdata(pdev);
  203. tegra_wdt_stop(&wdt->wdd);
  204. watchdog_unregister_device(&wdt->wdd);
  205. dev_info(&pdev->dev, "removed wdt\n");
  206. return 0;
  207. }
  208. #ifdef CONFIG_PM_SLEEP
  209. static int tegra_wdt_runtime_suspend(struct device *dev)
  210. {
  211. struct tegra_wdt *wdt = dev_get_drvdata(dev);
  212. if (watchdog_active(&wdt->wdd))
  213. tegra_wdt_stop(&wdt->wdd);
  214. return 0;
  215. }
  216. static int tegra_wdt_runtime_resume(struct device *dev)
  217. {
  218. struct tegra_wdt *wdt = dev_get_drvdata(dev);
  219. if (watchdog_active(&wdt->wdd))
  220. tegra_wdt_start(&wdt->wdd);
  221. return 0;
  222. }
  223. #endif
  224. static const struct of_device_id tegra_wdt_of_match[] = {
  225. { .compatible = "nvidia,tegra30-timer", },
  226. { },
  227. };
  228. MODULE_DEVICE_TABLE(of, tegra_wdt_of_match);
  229. static const struct dev_pm_ops tegra_wdt_pm_ops = {
  230. SET_SYSTEM_SLEEP_PM_OPS(tegra_wdt_runtime_suspend,
  231. tegra_wdt_runtime_resume)
  232. };
  233. static struct platform_driver tegra_wdt_driver = {
  234. .probe = tegra_wdt_probe,
  235. .remove = tegra_wdt_remove,
  236. .driver = {
  237. .owner = THIS_MODULE,
  238. .name = "tegra-wdt",
  239. .pm = &tegra_wdt_pm_ops,
  240. .of_match_table = tegra_wdt_of_match,
  241. },
  242. };
  243. module_platform_driver(tegra_wdt_driver);
  244. MODULE_AUTHOR("NVIDIA Corporation");
  245. MODULE_DESCRIPTION("Tegra Watchdog Driver");
  246. MODULE_LICENSE("GPL v2");