orion_wdt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * drivers/watchdog/orion_wdt.c
  3. *
  4. * Watchdog driver for Orion/Kirkwood processors
  5. *
  6. * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/clk.h>
  24. #include <linux/err.h>
  25. #include <linux/of.h>
  26. #include <mach/bridge-regs.h>
  27. /*
  28. * Watchdog timer block registers.
  29. */
  30. #define TIMER_CTRL 0x0000
  31. #define WDT_EN 0x0010
  32. #define WDT_VAL 0x0024
  33. #define WDT_MAX_CYCLE_COUNT 0xffffffff
  34. #define WDT_IN_USE 0
  35. #define WDT_OK_TO_CLOSE 1
  36. static bool nowayout = WATCHDOG_NOWAYOUT;
  37. static int heartbeat = -1; /* module parameter (seconds) */
  38. static unsigned int wdt_max_duration; /* (seconds) */
  39. static struct clk *clk;
  40. static unsigned int wdt_tclk;
  41. static void __iomem *wdt_reg;
  42. static DEFINE_SPINLOCK(wdt_lock);
  43. static int orion_wdt_ping(struct watchdog_device *wdt_dev)
  44. {
  45. spin_lock(&wdt_lock);
  46. /* Reload watchdog duration */
  47. writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
  48. spin_unlock(&wdt_lock);
  49. return 0;
  50. }
  51. static int orion_wdt_start(struct watchdog_device *wdt_dev)
  52. {
  53. u32 reg;
  54. spin_lock(&wdt_lock);
  55. /* Set watchdog duration */
  56. writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
  57. /* Clear watchdog timer interrupt */
  58. reg = readl(BRIDGE_CAUSE);
  59. reg &= ~WDT_INT_REQ;
  60. writel(reg, BRIDGE_CAUSE);
  61. /* Enable watchdog timer */
  62. reg = readl(wdt_reg + TIMER_CTRL);
  63. reg |= WDT_EN;
  64. writel(reg, wdt_reg + TIMER_CTRL);
  65. /* Enable reset on watchdog */
  66. reg = readl(RSTOUTn_MASK);
  67. reg |= WDT_RESET_OUT_EN;
  68. writel(reg, RSTOUTn_MASK);
  69. spin_unlock(&wdt_lock);
  70. return 0;
  71. }
  72. static int orion_wdt_stop(struct watchdog_device *wdt_dev)
  73. {
  74. u32 reg;
  75. spin_lock(&wdt_lock);
  76. /* Disable reset on watchdog */
  77. reg = readl(RSTOUTn_MASK);
  78. reg &= ~WDT_RESET_OUT_EN;
  79. writel(reg, RSTOUTn_MASK);
  80. /* Disable watchdog timer */
  81. reg = readl(wdt_reg + TIMER_CTRL);
  82. reg &= ~WDT_EN;
  83. writel(reg, wdt_reg + TIMER_CTRL);
  84. spin_unlock(&wdt_lock);
  85. return 0;
  86. }
  87. static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
  88. {
  89. unsigned int time_left;
  90. spin_lock(&wdt_lock);
  91. time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
  92. spin_unlock(&wdt_lock);
  93. return time_left;
  94. }
  95. static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
  96. unsigned int timeout)
  97. {
  98. wdt_dev->timeout = timeout;
  99. return 0;
  100. }
  101. static const struct watchdog_info orion_wdt_info = {
  102. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  103. .identity = "Orion Watchdog",
  104. };
  105. static const struct watchdog_ops orion_wdt_ops = {
  106. .owner = THIS_MODULE,
  107. .start = orion_wdt_start,
  108. .stop = orion_wdt_stop,
  109. .ping = orion_wdt_ping,
  110. .set_timeout = orion_wdt_set_timeout,
  111. .get_timeleft = orion_wdt_get_timeleft,
  112. };
  113. static struct watchdog_device orion_wdt = {
  114. .info = &orion_wdt_info,
  115. .ops = &orion_wdt_ops,
  116. .min_timeout = 1,
  117. };
  118. static int orion_wdt_probe(struct platform_device *pdev)
  119. {
  120. struct resource *res;
  121. int ret;
  122. clk = devm_clk_get(&pdev->dev, NULL);
  123. if (IS_ERR(clk)) {
  124. dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
  125. return -ENODEV;
  126. }
  127. clk_prepare_enable(clk);
  128. wdt_tclk = clk_get_rate(clk);
  129. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  130. if (!res)
  131. return -ENODEV;
  132. wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  133. if (!wdt_reg)
  134. return -ENOMEM;
  135. wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
  136. orion_wdt.timeout = wdt_max_duration;
  137. orion_wdt.max_timeout = wdt_max_duration;
  138. watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
  139. watchdog_set_nowayout(&orion_wdt, nowayout);
  140. ret = watchdog_register_device(&orion_wdt);
  141. if (ret) {
  142. clk_disable_unprepare(clk);
  143. return ret;
  144. }
  145. pr_info("Initial timeout %d sec%s\n",
  146. orion_wdt.timeout, nowayout ? ", nowayout" : "");
  147. return 0;
  148. }
  149. static int orion_wdt_remove(struct platform_device *pdev)
  150. {
  151. watchdog_unregister_device(&orion_wdt);
  152. clk_disable_unprepare(clk);
  153. return 0;
  154. }
  155. static void orion_wdt_shutdown(struct platform_device *pdev)
  156. {
  157. orion_wdt_stop(&orion_wdt);
  158. }
  159. static const struct of_device_id orion_wdt_of_match_table[] = {
  160. { .compatible = "marvell,orion-wdt", },
  161. {},
  162. };
  163. MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
  164. static struct platform_driver orion_wdt_driver = {
  165. .probe = orion_wdt_probe,
  166. .remove = orion_wdt_remove,
  167. .shutdown = orion_wdt_shutdown,
  168. .driver = {
  169. .owner = THIS_MODULE,
  170. .name = "orion_wdt",
  171. .of_match_table = of_match_ptr(orion_wdt_of_match_table),
  172. },
  173. };
  174. module_platform_driver(orion_wdt_driver);
  175. MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
  176. MODULE_DESCRIPTION("Orion Processor Watchdog");
  177. module_param(heartbeat, int, 0);
  178. MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
  179. module_param(nowayout, bool, 0);
  180. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  181. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  182. MODULE_LICENSE("GPL");
  183. MODULE_ALIAS("platform:orion_wdt");
  184. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);