pnx833x_wdt.c 7.1 KB

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