orion_wdt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <mach/bridge-regs.h>
  26. /*
  27. * Watchdog timer block registers.
  28. */
  29. #define TIMER_CTRL 0x0000
  30. #define WDT_EN 0x0010
  31. #define WDT_VAL 0x0024
  32. #define WDT_MAX_CYCLE_COUNT 0xffffffff
  33. #define WDT_IN_USE 0
  34. #define WDT_OK_TO_CLOSE 1
  35. static bool nowayout = WATCHDOG_NOWAYOUT;
  36. static int heartbeat = -1; /* module parameter (seconds) */
  37. static unsigned int wdt_max_duration; /* (seconds) */
  38. static struct clk *clk;
  39. static unsigned int wdt_tclk;
  40. static void __iomem *wdt_reg;
  41. static DEFINE_SPINLOCK(wdt_lock);
  42. static int orion_wdt_ping(struct watchdog_device *wdt_dev)
  43. {
  44. spin_lock(&wdt_lock);
  45. /* Reload watchdog duration */
  46. writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
  47. spin_unlock(&wdt_lock);
  48. return 0;
  49. }
  50. static int orion_wdt_start(struct watchdog_device *wdt_dev)
  51. {
  52. u32 reg;
  53. spin_lock(&wdt_lock);
  54. /* Set watchdog duration */
  55. writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
  56. /* Clear watchdog timer interrupt */
  57. reg = readl(BRIDGE_CAUSE);
  58. reg &= ~WDT_INT_REQ;
  59. writel(reg, BRIDGE_CAUSE);
  60. /* Enable watchdog timer */
  61. reg = readl(wdt_reg + TIMER_CTRL);
  62. reg |= WDT_EN;
  63. writel(reg, wdt_reg + TIMER_CTRL);
  64. /* Enable reset on watchdog */
  65. reg = readl(RSTOUTn_MASK);
  66. reg |= WDT_RESET_OUT_EN;
  67. writel(reg, RSTOUTn_MASK);
  68. spin_unlock(&wdt_lock);
  69. return 0;
  70. }
  71. static int orion_wdt_stop(struct watchdog_device *wdt_dev)
  72. {
  73. u32 reg;
  74. spin_lock(&wdt_lock);
  75. /* Disable reset on watchdog */
  76. reg = readl(RSTOUTn_MASK);
  77. reg &= ~WDT_RESET_OUT_EN;
  78. writel(reg, RSTOUTn_MASK);
  79. /* Disable watchdog timer */
  80. reg = readl(wdt_reg + TIMER_CTRL);
  81. reg &= ~WDT_EN;
  82. writel(reg, wdt_reg + TIMER_CTRL);
  83. spin_unlock(&wdt_lock);
  84. return 0;
  85. }
  86. static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
  87. {
  88. unsigned int time_left;
  89. spin_lock(&wdt_lock);
  90. time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
  91. spin_unlock(&wdt_lock);
  92. return time_left;
  93. }
  94. static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
  95. unsigned int timeout)
  96. {
  97. wdt_dev->timeout = timeout;
  98. return 0;
  99. }
  100. static const struct watchdog_info orion_wdt_info = {
  101. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  102. .identity = "Orion Watchdog",
  103. };
  104. static const struct watchdog_ops orion_wdt_ops = {
  105. .owner = THIS_MODULE,
  106. .start = orion_wdt_start,
  107. .stop = orion_wdt_stop,
  108. .ping = orion_wdt_ping,
  109. .set_timeout = orion_wdt_set_timeout,
  110. .get_timeleft = orion_wdt_get_timeleft,
  111. };
  112. static struct watchdog_device orion_wdt = {
  113. .info = &orion_wdt_info,
  114. .ops = &orion_wdt_ops,
  115. };
  116. static int __devinit orion_wdt_probe(struct platform_device *pdev)
  117. {
  118. struct resource *res;
  119. int ret;
  120. clk = devm_clk_get(&pdev->dev, NULL);
  121. if (IS_ERR(clk)) {
  122. dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
  123. return -ENODEV;
  124. }
  125. clk_prepare_enable(clk);
  126. wdt_tclk = clk_get_rate(clk);
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  129. if (!wdt_reg)
  130. return -ENOMEM;
  131. wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
  132. if ((heartbeat < 1) || (heartbeat > wdt_max_duration))
  133. heartbeat = wdt_max_duration;
  134. orion_wdt.timeout = heartbeat;
  135. orion_wdt.min_timeout = 1;
  136. orion_wdt.max_timeout = wdt_max_duration;
  137. watchdog_set_nowayout(&orion_wdt, nowayout);
  138. ret = watchdog_register_device(&orion_wdt);
  139. if (ret) {
  140. clk_disable_unprepare(clk);
  141. return ret;
  142. }
  143. pr_info("Initial timeout %d sec%s\n",
  144. heartbeat, nowayout ? ", nowayout" : "");
  145. return 0;
  146. }
  147. static int __devexit orion_wdt_remove(struct platform_device *pdev)
  148. {
  149. watchdog_unregister_device(&orion_wdt);
  150. clk_disable_unprepare(clk);
  151. return 0;
  152. }
  153. static void orion_wdt_shutdown(struct platform_device *pdev)
  154. {
  155. orion_wdt_stop(&orion_wdt);
  156. }
  157. static struct platform_driver orion_wdt_driver = {
  158. .probe = orion_wdt_probe,
  159. .remove = __devexit_p(orion_wdt_remove),
  160. .shutdown = orion_wdt_shutdown,
  161. .driver = {
  162. .owner = THIS_MODULE,
  163. .name = "orion_wdt",
  164. },
  165. };
  166. module_platform_driver(orion_wdt_driver);
  167. MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
  168. MODULE_DESCRIPTION("Orion Processor Watchdog");
  169. module_param(heartbeat, int, 0);
  170. MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
  171. module_param(nowayout, bool, 0);
  172. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  173. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  174. MODULE_LICENSE("GPL");
  175. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);