pnx4008_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <linux/delay.h>
  33. #include <linux/reboot.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;
  70. static DEFINE_SPINLOCK(io_lock);
  71. static void __iomem *wdt_base;
  72. static 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 int pnx4008_restart_handler(struct watchdog_device *wdd,
  109. unsigned long mode, void *cmd)
  110. {
  111. const char *boot_cmd = cmd;
  112. /*
  113. * Verify if a "cmd" passed from the userspace program rebooting
  114. * the system; if available, handle it.
  115. * - For details, see the 'reboot' syscall in kernel/reboot.c
  116. * - If the received "cmd" is not supported, use the default mode.
  117. */
  118. if (boot_cmd) {
  119. if (boot_cmd[0] == 'h')
  120. mode = REBOOT_HARD;
  121. else if (boot_cmd[0] == 's')
  122. mode = REBOOT_SOFT;
  123. }
  124. if (mode == REBOOT_SOFT) {
  125. /* Force match output active */
  126. writel(EXT_MATCH0, WDTIM_EMR(wdt_base));
  127. /* Internal reset on match output (RESOUT_N not asserted) */
  128. writel(M_RES1, WDTIM_MCTRL(wdt_base));
  129. } else {
  130. /* Instant assert of RESETOUT_N with pulse length 1mS */
  131. writel(13000, WDTIM_PULSE(wdt_base));
  132. writel(M_RES2 | RESFRC1 | RESFRC2, WDTIM_MCTRL(wdt_base));
  133. }
  134. /* Wait for watchdog to reset system */
  135. mdelay(1000);
  136. return NOTIFY_DONE;
  137. }
  138. static const struct watchdog_info pnx4008_wdt_ident = {
  139. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
  140. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  141. .identity = "PNX4008 Watchdog",
  142. };
  143. static const struct watchdog_ops pnx4008_wdt_ops = {
  144. .owner = THIS_MODULE,
  145. .start = pnx4008_wdt_start,
  146. .stop = pnx4008_wdt_stop,
  147. .set_timeout = pnx4008_wdt_set_timeout,
  148. .restart = pnx4008_restart_handler,
  149. };
  150. static struct watchdog_device pnx4008_wdd = {
  151. .info = &pnx4008_wdt_ident,
  152. .ops = &pnx4008_wdt_ops,
  153. .timeout = DEFAULT_HEARTBEAT,
  154. .min_timeout = 1,
  155. .max_timeout = MAX_HEARTBEAT,
  156. };
  157. static int pnx4008_wdt_probe(struct platform_device *pdev)
  158. {
  159. struct resource *r;
  160. int ret = 0;
  161. watchdog_init_timeout(&pnx4008_wdd, heartbeat, &pdev->dev);
  162. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  163. wdt_base = devm_ioremap_resource(&pdev->dev, r);
  164. if (IS_ERR(wdt_base))
  165. return PTR_ERR(wdt_base);
  166. wdt_clk = devm_clk_get(&pdev->dev, NULL);
  167. if (IS_ERR(wdt_clk))
  168. return PTR_ERR(wdt_clk);
  169. ret = clk_prepare_enable(wdt_clk);
  170. if (ret)
  171. return ret;
  172. pnx4008_wdd.bootstatus = (readl(WDTIM_RES(wdt_base)) & WDOG_RESET) ?
  173. WDIOF_CARDRESET : 0;
  174. pnx4008_wdd.parent = &pdev->dev;
  175. watchdog_set_nowayout(&pnx4008_wdd, nowayout);
  176. watchdog_set_restart_priority(&pnx4008_wdd, 128);
  177. pnx4008_wdt_stop(&pnx4008_wdd); /* disable for now */
  178. ret = watchdog_register_device(&pnx4008_wdd);
  179. if (ret < 0) {
  180. dev_err(&pdev->dev, "cannot register watchdog device\n");
  181. goto disable_clk;
  182. }
  183. dev_info(&pdev->dev, "heartbeat %d sec\n", pnx4008_wdd.timeout);
  184. return 0;
  185. disable_clk:
  186. clk_disable_unprepare(wdt_clk);
  187. return ret;
  188. }
  189. static int pnx4008_wdt_remove(struct platform_device *pdev)
  190. {
  191. watchdog_unregister_device(&pnx4008_wdd);
  192. clk_disable_unprepare(wdt_clk);
  193. return 0;
  194. }
  195. #ifdef CONFIG_OF
  196. static const struct of_device_id pnx4008_wdt_match[] = {
  197. { .compatible = "nxp,pnx4008-wdt" },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE(of, pnx4008_wdt_match);
  201. #endif
  202. static struct platform_driver platform_wdt_driver = {
  203. .driver = {
  204. .name = "pnx4008-watchdog",
  205. .of_match_table = of_match_ptr(pnx4008_wdt_match),
  206. },
  207. .probe = pnx4008_wdt_probe,
  208. .remove = pnx4008_wdt_remove,
  209. };
  210. module_platform_driver(platform_wdt_driver);
  211. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  212. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
  213. MODULE_DESCRIPTION("PNX4008 Watchdog Driver");
  214. module_param(heartbeat, uint, 0);
  215. MODULE_PARM_DESC(heartbeat,
  216. "Watchdog heartbeat period in seconds from 1 to "
  217. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  218. __MODULE_STRING(DEFAULT_HEARTBEAT));
  219. module_param(nowayout, bool, 0);
  220. MODULE_PARM_DESC(nowayout,
  221. "Set to 1 to keep watchdog running after device release");
  222. MODULE_LICENSE("GPL");
  223. MODULE_ALIAS("platform:pnx4008-watchdog");