bcm47xx_wdt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Watchdog driver for Broadcom BCM47XX
  3. *
  4. * Copyright (C) 2008 Aleksandar Radovanovic <biblbroks@sezampro.rs>
  5. * Copyright (C) 2009 Matthieu CASTET <castet.matthieu@free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/errno.h>
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/reboot.h>
  21. #include <linux/types.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/timer.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/ssb/ssb_embedded.h>
  27. #include <asm/mach-bcm47xx/bcm47xx.h>
  28. #define DRV_NAME "bcm47xx_wdt"
  29. #define WDT_DEFAULT_TIME 30 /* seconds */
  30. #define WDT_MAX_TIME 255 /* seconds */
  31. static int wdt_time = WDT_DEFAULT_TIME;
  32. static int nowayout = WATCHDOG_NOWAYOUT;
  33. module_param(wdt_time, int, 0);
  34. MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
  35. __MODULE_STRING(WDT_DEFAULT_TIME) ")");
  36. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  37. module_param(nowayout, int, 0);
  38. MODULE_PARM_DESC(nowayout,
  39. "Watchdog cannot be stopped once started (default="
  40. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  41. #endif
  42. static unsigned long bcm47xx_wdt_busy;
  43. static char expect_release;
  44. static struct timer_list wdt_timer;
  45. static atomic_t ticks;
  46. static inline void bcm47xx_wdt_hw_start(void)
  47. {
  48. /* this is 2,5s on 100Mhz clock and 2s on 133 Mhz */
  49. ssb_watchdog_timer_set(&ssb_bcm47xx, 0xfffffff);
  50. }
  51. static inline int bcm47xx_wdt_hw_stop(void)
  52. {
  53. return ssb_watchdog_timer_set(&ssb_bcm47xx, 0);
  54. }
  55. static void bcm47xx_timer_tick(unsigned long unused)
  56. {
  57. if (!atomic_dec_and_test(&ticks)) {
  58. bcm47xx_wdt_hw_start();
  59. mod_timer(&wdt_timer, jiffies + HZ);
  60. } else {
  61. printk(KERN_CRIT DRV_NAME "Watchdog will fire soon!!!\n");
  62. }
  63. }
  64. static inline void bcm47xx_wdt_pet(void)
  65. {
  66. atomic_set(&ticks, wdt_time);
  67. }
  68. static void bcm47xx_wdt_start(void)
  69. {
  70. bcm47xx_wdt_pet();
  71. bcm47xx_timer_tick(0);
  72. }
  73. static void bcm47xx_wdt_pause(void)
  74. {
  75. del_timer_sync(&wdt_timer);
  76. bcm47xx_wdt_hw_stop();
  77. }
  78. static void bcm47xx_wdt_stop(void)
  79. {
  80. bcm47xx_wdt_pause();
  81. }
  82. static int bcm47xx_wdt_settimeout(int new_time)
  83. {
  84. if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
  85. return -EINVAL;
  86. wdt_time = new_time;
  87. return 0;
  88. }
  89. static int bcm47xx_wdt_open(struct inode *inode, struct file *file)
  90. {
  91. if (test_and_set_bit(0, &bcm47xx_wdt_busy))
  92. return -EBUSY;
  93. bcm47xx_wdt_start();
  94. return nonseekable_open(inode, file);
  95. }
  96. static int bcm47xx_wdt_release(struct inode *inode, struct file *file)
  97. {
  98. if (expect_release == 42) {
  99. bcm47xx_wdt_stop();
  100. } else {
  101. printk(KERN_CRIT DRV_NAME
  102. ": Unexpected close, not stopping watchdog!\n");
  103. bcm47xx_wdt_start();
  104. }
  105. clear_bit(0, &bcm47xx_wdt_busy);
  106. expect_release = 0;
  107. return 0;
  108. }
  109. static ssize_t bcm47xx_wdt_write(struct file *file, const char __user *data,
  110. size_t len, loff_t *ppos)
  111. {
  112. if (len) {
  113. if (!nowayout) {
  114. size_t i;
  115. expect_release = 0;
  116. for (i = 0; i != len; i++) {
  117. char c;
  118. if (get_user(c, data + i))
  119. return -EFAULT;
  120. if (c == 'V')
  121. expect_release = 42;
  122. }
  123. }
  124. bcm47xx_wdt_pet();
  125. }
  126. return len;
  127. }
  128. static const struct watchdog_info bcm47xx_wdt_info = {
  129. .identity = DRV_NAME,
  130. .options = WDIOF_SETTIMEOUT |
  131. WDIOF_KEEPALIVEPING |
  132. WDIOF_MAGICCLOSE,
  133. };
  134. static long bcm47xx_wdt_ioctl(struct file *file,
  135. unsigned int cmd, unsigned long arg)
  136. {
  137. void __user *argp = (void __user *)arg;
  138. int __user *p = argp;
  139. int new_value, retval = -EINVAL;
  140. switch (cmd) {
  141. case WDIOC_GETSUPPORT:
  142. return copy_to_user(argp, &bcm47xx_wdt_info,
  143. sizeof(bcm47xx_wdt_info)) ? -EFAULT : 0;
  144. case WDIOC_GETSTATUS:
  145. case WDIOC_GETBOOTSTATUS:
  146. return put_user(0, p);
  147. case WDIOC_SETOPTIONS:
  148. if (get_user(new_value, p))
  149. return -EFAULT;
  150. if (new_value & WDIOS_DISABLECARD) {
  151. bcm47xx_wdt_stop();
  152. retval = 0;
  153. }
  154. if (new_value & WDIOS_ENABLECARD) {
  155. bcm47xx_wdt_start();
  156. retval = 0;
  157. }
  158. return retval;
  159. case WDIOC_KEEPALIVE:
  160. bcm47xx_wdt_pet();
  161. return 0;
  162. case WDIOC_SETTIMEOUT:
  163. if (get_user(new_value, p))
  164. return -EFAULT;
  165. if (bcm47xx_wdt_settimeout(new_value))
  166. return -EINVAL;
  167. bcm47xx_wdt_pet();
  168. case WDIOC_GETTIMEOUT:
  169. return put_user(wdt_time, p);
  170. default:
  171. return -ENOTTY;
  172. }
  173. }
  174. static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
  175. unsigned long code, void *unused)
  176. {
  177. if (code == SYS_DOWN || code == SYS_HALT)
  178. bcm47xx_wdt_stop();
  179. return NOTIFY_DONE;
  180. }
  181. static const struct file_operations bcm47xx_wdt_fops = {
  182. .owner = THIS_MODULE,
  183. .llseek = no_llseek,
  184. .unlocked_ioctl = bcm47xx_wdt_ioctl,
  185. .open = bcm47xx_wdt_open,
  186. .release = bcm47xx_wdt_release,
  187. .write = bcm47xx_wdt_write,
  188. };
  189. static struct miscdevice bcm47xx_wdt_miscdev = {
  190. .minor = WATCHDOG_MINOR,
  191. .name = "watchdog",
  192. .fops = &bcm47xx_wdt_fops,
  193. };
  194. static struct notifier_block bcm47xx_wdt_notifier = {
  195. .notifier_call = bcm47xx_wdt_notify_sys,
  196. };
  197. static int __init bcm47xx_wdt_init(void)
  198. {
  199. int ret;
  200. if (bcm47xx_wdt_hw_stop() < 0)
  201. return -ENODEV;
  202. setup_timer(&wdt_timer, bcm47xx_timer_tick, 0L);
  203. if (bcm47xx_wdt_settimeout(wdt_time)) {
  204. bcm47xx_wdt_settimeout(WDT_DEFAULT_TIME);
  205. printk(KERN_INFO DRV_NAME ": "
  206. "wdt_time value must be 0 < wdt_time < %d, using %d\n",
  207. (WDT_MAX_TIME + 1), wdt_time);
  208. }
  209. ret = register_reboot_notifier(&bcm47xx_wdt_notifier);
  210. if (ret)
  211. return ret;
  212. ret = misc_register(&bcm47xx_wdt_miscdev);
  213. if (ret) {
  214. unregister_reboot_notifier(&bcm47xx_wdt_notifier);
  215. return ret;
  216. }
  217. printk(KERN_INFO "BCM47xx Watchdog Timer enabled (%d seconds%s)\n",
  218. wdt_time, nowayout ? ", nowayout" : "");
  219. return 0;
  220. }
  221. static void __exit bcm47xx_wdt_exit(void)
  222. {
  223. if (!nowayout)
  224. bcm47xx_wdt_stop();
  225. misc_deregister(&bcm47xx_wdt_miscdev);
  226. unregister_reboot_notifier(&bcm47xx_wdt_notifier);
  227. }
  228. module_init(bcm47xx_wdt_init);
  229. module_exit(bcm47xx_wdt_exit);
  230. MODULE_AUTHOR("Aleksandar Radovanovic");
  231. MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
  232. MODULE_LICENSE("GPL");
  233. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);