pnx4008_wdt.c 6.1 KB

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