pnx4008_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * drivers/char/watchdog/pnx4008_wdt.c
  3. *
  4. * Watchdog driver for PNX4008 board
  5. *
  6. * Authors: Dmitry Chigirev <source@mvista.com>,
  7. * Vitaly Wool <vitalywool@gmail.com>
  8. * Based on sa1100 driver,
  9. * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
  10. *
  11. * 2005-2006 (c) MontaVista Software, Inc.
  12. *
  13. * (C) 2012 Wolfram Sang, Pengutronix
  14. *
  15. * This file is licensed under the terms of the GNU General Public License
  16. * version 2. This program is licensed "as is" without any warranty of any
  17. * kind, whether express or implied.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/clk.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/io.h>
  29. #include <linux/slab.h>
  30. #include <linux/err.h>
  31. #include <linux/of.h>
  32. #include <mach/hardware.h>
  33. /* WatchDog Timer - Chapter 23 Page 207 */
  34. #define DEFAULT_HEARTBEAT 19
  35. #define MAX_HEARTBEAT 60
  36. /* Watchdog timer register set definition */
  37. #define WDTIM_INT(p) ((p) + 0x0)
  38. #define WDTIM_CTRL(p) ((p) + 0x4)
  39. #define WDTIM_COUNTER(p) ((p) + 0x8)
  40. #define WDTIM_MCTRL(p) ((p) + 0xC)
  41. #define WDTIM_MATCH0(p) ((p) + 0x10)
  42. #define WDTIM_EMR(p) ((p) + 0x14)
  43. #define WDTIM_PULSE(p) ((p) + 0x18)
  44. #define WDTIM_RES(p) ((p) + 0x1C)
  45. /* WDTIM_INT bit definitions */
  46. #define MATCH_INT 1
  47. /* WDTIM_CTRL bit definitions */
  48. #define COUNT_ENAB 1
  49. #define RESET_COUNT (1 << 1)
  50. #define DEBUG_EN (1 << 2)
  51. /* WDTIM_MCTRL bit definitions */
  52. #define MR0_INT 1
  53. #undef RESET_COUNT0
  54. #define RESET_COUNT0 (1 << 2)
  55. #define STOP_COUNT0 (1 << 2)
  56. #define M_RES1 (1 << 3)
  57. #define M_RES2 (1 << 4)
  58. #define RESFRC1 (1 << 5)
  59. #define RESFRC2 (1 << 6)
  60. /* WDTIM_EMR bit definitions */
  61. #define EXT_MATCH0 1
  62. #define MATCH_OUTPUT_HIGH (2 << 4) /*a MATCH_CTRL setting */
  63. /* WDTIM_RES bit definitions */
  64. #define WDOG_RESET 1 /* read only */
  65. #define WDOG_COUNTER_RATE 13000000 /*the counter clock is 13 MHz fixed */
  66. static bool nowayout = WATCHDOG_NOWAYOUT;
  67. static unsigned int heartbeat = DEFAULT_HEARTBEAT;
  68. static DEFINE_SPINLOCK(io_lock);
  69. static void __iomem *wdt_base;
  70. static struct clk *wdt_clk;
  71. static int pnx4008_wdt_start(struct watchdog_device *wdd)
  72. {
  73. spin_lock(&io_lock);
  74. /* stop counter, initiate counter reset */
  75. writel(RESET_COUNT, WDTIM_CTRL(wdt_base));
  76. /*wait for reset to complete. 100% guarantee event */
  77. while (readl(WDTIM_COUNTER(wdt_base)))
  78. cpu_relax();
  79. /* internal and external reset, stop after that */
  80. writel(M_RES2 | STOP_COUNT0 | RESET_COUNT0, WDTIM_MCTRL(wdt_base));
  81. /* configure match output */
  82. writel(MATCH_OUTPUT_HIGH, WDTIM_EMR(wdt_base));
  83. /* clear interrupt, just in case */
  84. writel(MATCH_INT, WDTIM_INT(wdt_base));
  85. /* the longest pulse period 65541/(13*10^6) seconds ~ 5 ms. */
  86. writel(0xFFFF, WDTIM_PULSE(wdt_base));
  87. writel(wdd->timeout * WDOG_COUNTER_RATE, WDTIM_MATCH0(wdt_base));
  88. /*enable counter, stop when debugger active */
  89. writel(COUNT_ENAB | DEBUG_EN, WDTIM_CTRL(wdt_base));
  90. spin_unlock(&io_lock);
  91. return 0;
  92. }
  93. static int pnx4008_wdt_stop(struct watchdog_device *wdd)
  94. {
  95. spin_lock(&io_lock);
  96. writel(0, WDTIM_CTRL(wdt_base)); /*stop counter */
  97. spin_unlock(&io_lock);
  98. return 0;
  99. }
  100. static int pnx4008_wdt_set_timeout(struct watchdog_device *wdd,
  101. unsigned int new_timeout)
  102. {
  103. wdd->timeout = new_timeout;
  104. return 0;
  105. }
  106. static const struct watchdog_info pnx4008_wdt_ident = {
  107. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
  108. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  109. .identity = "PNX4008 Watchdog",
  110. };
  111. static const struct watchdog_ops pnx4008_wdt_ops = {
  112. .owner = THIS_MODULE,
  113. .start = pnx4008_wdt_start,
  114. .stop = pnx4008_wdt_stop,
  115. .set_timeout = pnx4008_wdt_set_timeout,
  116. };
  117. static struct watchdog_device pnx4008_wdd = {
  118. .info = &pnx4008_wdt_ident,
  119. .ops = &pnx4008_wdt_ops,
  120. .timeout = DEFAULT_HEARTBEAT,
  121. .min_timeout = 1,
  122. .max_timeout = MAX_HEARTBEAT,
  123. };
  124. static int pnx4008_wdt_probe(struct platform_device *pdev)
  125. {
  126. struct resource *r;
  127. int ret = 0;
  128. watchdog_init_timeout(&pnx4008_wdd, heartbeat, &pdev->dev);
  129. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  130. wdt_base = devm_ioremap_resource(&pdev->dev, r);
  131. if (IS_ERR(wdt_base))
  132. return PTR_ERR(wdt_base);
  133. wdt_clk = devm_clk_get(&pdev->dev, NULL);
  134. if (IS_ERR(wdt_clk))
  135. return PTR_ERR(wdt_clk);
  136. ret = clk_prepare_enable(wdt_clk);
  137. if (ret)
  138. return ret;
  139. pnx4008_wdd.bootstatus = (readl(WDTIM_RES(wdt_base)) & WDOG_RESET) ?
  140. WDIOF_CARDRESET : 0;
  141. pnx4008_wdd.parent = &pdev->dev;
  142. watchdog_set_nowayout(&pnx4008_wdd, nowayout);
  143. pnx4008_wdt_stop(&pnx4008_wdd); /* disable for now */
  144. ret = watchdog_register_device(&pnx4008_wdd);
  145. if (ret < 0) {
  146. dev_err(&pdev->dev, "cannot register watchdog device\n");
  147. goto disable_clk;
  148. }
  149. dev_info(&pdev->dev, "PNX4008 Watchdog Timer: heartbeat %d sec\n",
  150. pnx4008_wdd.timeout);
  151. return 0;
  152. disable_clk:
  153. clk_disable_unprepare(wdt_clk);
  154. return ret;
  155. }
  156. static int pnx4008_wdt_remove(struct platform_device *pdev)
  157. {
  158. watchdog_unregister_device(&pnx4008_wdd);
  159. clk_disable_unprepare(wdt_clk);
  160. return 0;
  161. }
  162. #ifdef CONFIG_OF
  163. static const struct of_device_id pnx4008_wdt_match[] = {
  164. { .compatible = "nxp,pnx4008-wdt" },
  165. { }
  166. };
  167. MODULE_DEVICE_TABLE(of, pnx4008_wdt_match);
  168. #endif
  169. static struct platform_driver platform_wdt_driver = {
  170. .driver = {
  171. .name = "pnx4008-watchdog",
  172. .of_match_table = of_match_ptr(pnx4008_wdt_match),
  173. },
  174. .probe = pnx4008_wdt_probe,
  175. .remove = pnx4008_wdt_remove,
  176. };
  177. module_platform_driver(platform_wdt_driver);
  178. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  179. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
  180. MODULE_DESCRIPTION("PNX4008 Watchdog Driver");
  181. module_param(heartbeat, uint, 0);
  182. MODULE_PARM_DESC(heartbeat,
  183. "Watchdog heartbeat period in seconds from 1 to "
  184. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  185. __MODULE_STRING(DEFAULT_HEARTBEAT));
  186. module_param(nowayout, bool, 0);
  187. MODULE_PARM_DESC(nowayout,
  188. "Set to 1 to keep watchdog running after device release");
  189. MODULE_LICENSE("GPL");
  190. MODULE_ALIAS("platform:pnx4008-watchdog");