pnx833x_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * PNX833x Hardware Watchdog Driver
  3. * Copyright 2008 NXP Semiconductors
  4. * Daniel Laird <daniel.j.laird@nxp.com>
  5. * Andre McCurdy <andre.mccurdy@nxp.com>
  6. *
  7. * Heavily based upon - IndyDog 0.3
  8. * A Hardware Watchdog Device for SGI IP22
  9. *
  10. * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>, All Rights Reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * based on softdog.c by Alan Cox <alan@redhat.com>
  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/fs.h>
  25. #include <linux/mm.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/watchdog.h>
  28. #include <linux/notifier.h>
  29. #include <linux/reboot.h>
  30. #include <linux/init.h>
  31. #include <asm/mach-pnx833x/pnx833x.h>
  32. #define WATCHDOG_TIMEOUT 30 /* 30 sec Maximum timeout */
  33. #define WATCHDOG_COUNT_FREQUENCY 68000000U /* Watchdog counts at 68MHZ. */
  34. #define PNX_WATCHDOG_TIMEOUT (WATCHDOG_TIMEOUT * WATCHDOG_COUNT_FREQUENCY)
  35. #define PNX_TIMEOUT_VALUE 2040000000U
  36. /** CONFIG block */
  37. #define PNX833X_CONFIG (0x07000U)
  38. #define PNX833X_CONFIG_CPU_WATCHDOG (0x54)
  39. #define PNX833X_CONFIG_CPU_WATCHDOG_COMPARE (0x58)
  40. #define PNX833X_CONFIG_CPU_COUNTERS_CONTROL (0x1c)
  41. /** RESET block */
  42. #define PNX833X_RESET (0x08000U)
  43. #define PNX833X_RESET_CONFIG (0x08)
  44. static int pnx833x_wdt_alive;
  45. /* Set default timeout in MHZ.*/
  46. static int pnx833x_wdt_timeout = PNX_WATCHDOG_TIMEOUT;
  47. module_param(pnx833x_wdt_timeout, int, 0);
  48. MODULE_PARM_DESC(timeout, "Watchdog timeout in Mhz. (68Mhz clock), default="
  49. __MODULE_STRING(PNX_TIMEOUT_VALUE) "(30 seconds).");
  50. static bool nowayout = WATCHDOG_NOWAYOUT;
  51. module_param(nowayout, bool, 0);
  52. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  53. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  54. #define START_DEFAULT 1
  55. static int start_enabled = START_DEFAULT;
  56. module_param(start_enabled, int, 0);
  57. MODULE_PARM_DESC(start_enabled, "Watchdog is started on module insertion "
  58. "(default=" __MODULE_STRING(START_DEFAULT) ")");
  59. static void pnx833x_wdt_start(void)
  60. {
  61. /* Enable watchdog causing reset. */
  62. PNX833X_REG(PNX833X_RESET + PNX833X_RESET_CONFIG) |= 0x1;
  63. /* Set timeout.*/
  64. PNX833X_REG(PNX833X_CONFIG +
  65. PNX833X_CONFIG_CPU_WATCHDOG_COMPARE) = pnx833x_wdt_timeout;
  66. /* Enable watchdog. */
  67. PNX833X_REG(PNX833X_CONFIG +
  68. PNX833X_CONFIG_CPU_COUNTERS_CONTROL) |= 0x1;
  69. pr_info("Started watchdog timer\n");
  70. }
  71. static void pnx833x_wdt_stop(void)
  72. {
  73. /* Disable watchdog causing reset. */
  74. PNX833X_REG(PNX833X_RESET + PNX833X_CONFIG) &= 0xFFFFFFFE;
  75. /* Disable watchdog.*/
  76. PNX833X_REG(PNX833X_CONFIG +
  77. PNX833X_CONFIG_CPU_COUNTERS_CONTROL) &= 0xFFFFFFFE;
  78. pr_info("Stopped watchdog timer\n");
  79. }
  80. static void pnx833x_wdt_ping(void)
  81. {
  82. PNX833X_REG(PNX833X_CONFIG +
  83. PNX833X_CONFIG_CPU_WATCHDOG_COMPARE) = pnx833x_wdt_timeout;
  84. }
  85. /*
  86. * Allow only one person to hold it open
  87. */
  88. static int pnx833x_wdt_open(struct inode *inode, struct file *file)
  89. {
  90. if (test_and_set_bit(0, &pnx833x_wdt_alive))
  91. return -EBUSY;
  92. if (nowayout)
  93. __module_get(THIS_MODULE);
  94. /* Activate timer */
  95. if (!start_enabled)
  96. pnx833x_wdt_start();
  97. pnx833x_wdt_ping();
  98. pr_info("Started watchdog timer\n");
  99. return nonseekable_open(inode, file);
  100. }
  101. static int pnx833x_wdt_release(struct inode *inode, struct file *file)
  102. {
  103. /* Shut off the timer.
  104. * Lock it in if it's a module and we defined ...NOWAYOUT */
  105. if (!nowayout)
  106. pnx833x_wdt_stop(); /* Turn the WDT off */
  107. clear_bit(0, &pnx833x_wdt_alive);
  108. return 0;
  109. }
  110. static ssize_t pnx833x_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  111. {
  112. /* Refresh the timer. */
  113. if (len)
  114. pnx833x_wdt_ping();
  115. return len;
  116. }
  117. static long pnx833x_wdt_ioctl(struct file *file, unsigned int cmd,
  118. unsigned long arg)
  119. {
  120. int options, new_timeout = 0;
  121. uint32_t timeout, timeout_left = 0;
  122. static const struct watchdog_info ident = {
  123. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
  124. .firmware_version = 0,
  125. .identity = "Hardware Watchdog for PNX833x",
  126. };
  127. switch (cmd) {
  128. default:
  129. return -ENOTTY;
  130. case WDIOC_GETSUPPORT:
  131. if (copy_to_user((struct watchdog_info *)arg,
  132. &ident, sizeof(ident)))
  133. return -EFAULT;
  134. return 0;
  135. case WDIOC_GETSTATUS:
  136. case WDIOC_GETBOOTSTATUS:
  137. return put_user(0, (int *)arg);
  138. case WDIOC_SETOPTIONS:
  139. if (get_user(options, (int *)arg))
  140. return -EFAULT;
  141. if (options & WDIOS_DISABLECARD)
  142. pnx833x_wdt_stop();
  143. if (options & WDIOS_ENABLECARD)
  144. pnx833x_wdt_start();
  145. return 0;
  146. case WDIOC_KEEPALIVE:
  147. pnx833x_wdt_ping();
  148. return 0;
  149. case WDIOC_SETTIMEOUT:
  150. {
  151. if (get_user(new_timeout, (int *)arg))
  152. return -EFAULT;
  153. pnx833x_wdt_timeout = new_timeout;
  154. PNX833X_REG(PNX833X_CONFIG +
  155. PNX833X_CONFIG_CPU_WATCHDOG_COMPARE) = new_timeout;
  156. return put_user(new_timeout, (int *)arg);
  157. }
  158. case WDIOC_GETTIMEOUT:
  159. timeout = PNX833X_REG(PNX833X_CONFIG +
  160. PNX833X_CONFIG_CPU_WATCHDOG_COMPARE);
  161. return put_user(timeout, (int *)arg);
  162. case WDIOC_GETTIMELEFT:
  163. timeout_left = PNX833X_REG(PNX833X_CONFIG +
  164. PNX833X_CONFIG_CPU_WATCHDOG);
  165. return put_user(timeout_left, (int *)arg);
  166. }
  167. }
  168. static int pnx833x_wdt_notify_sys(struct notifier_block *this,
  169. unsigned long code, void *unused)
  170. {
  171. if (code == SYS_DOWN || code == SYS_HALT)
  172. pnx833x_wdt_stop(); /* Turn the WDT off */
  173. return NOTIFY_DONE;
  174. }
  175. static const struct file_operations pnx833x_wdt_fops = {
  176. .owner = THIS_MODULE,
  177. .llseek = no_llseek,
  178. .write = pnx833x_wdt_write,
  179. .unlocked_ioctl = pnx833x_wdt_ioctl,
  180. .open = pnx833x_wdt_open,
  181. .release = pnx833x_wdt_release,
  182. };
  183. static struct miscdevice pnx833x_wdt_miscdev = {
  184. .minor = WATCHDOG_MINOR,
  185. .name = "watchdog",
  186. .fops = &pnx833x_wdt_fops,
  187. };
  188. static struct notifier_block pnx833x_wdt_notifier = {
  189. .notifier_call = pnx833x_wdt_notify_sys,
  190. };
  191. static int __init watchdog_init(void)
  192. {
  193. int ret, cause;
  194. /* Lets check the reason for the reset.*/
  195. cause = PNX833X_REG(PNX833X_RESET);
  196. /*If bit 31 is set then watchdog was cause of reset.*/
  197. if (cause & 0x80000000) {
  198. pr_info("The system was previously reset due to the watchdog firing - please investigate...\n");
  199. }
  200. ret = register_reboot_notifier(&pnx833x_wdt_notifier);
  201. if (ret) {
  202. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  203. return ret;
  204. }
  205. ret = misc_register(&pnx833x_wdt_miscdev);
  206. if (ret) {
  207. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  208. WATCHDOG_MINOR, ret);
  209. unregister_reboot_notifier(&pnx833x_wdt_notifier);
  210. return ret;
  211. }
  212. pr_info("Hardware Watchdog Timer for PNX833x: Version 0.1\n");
  213. if (start_enabled)
  214. pnx833x_wdt_start();
  215. return 0;
  216. }
  217. static void __exit watchdog_exit(void)
  218. {
  219. misc_deregister(&pnx833x_wdt_miscdev);
  220. unregister_reboot_notifier(&pnx833x_wdt_notifier);
  221. }
  222. module_init(watchdog_init);
  223. module_exit(watchdog_exit);
  224. MODULE_AUTHOR("Daniel Laird/Andre McCurdy");
  225. MODULE_DESCRIPTION("Hardware Watchdog Device for PNX833x");
  226. MODULE_LICENSE("GPL");