bcm47xx_wdt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. switch (bcm47xx_bus_type) {
  50. #ifdef CONFIG_BCM47XX_SSB
  51. case BCM47XX_BUS_TYPE_SSB:
  52. ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 0xfffffff);
  53. break;
  54. #endif
  55. #ifdef CONFIG_BCM47XX_BCMA
  56. case BCM47XX_BUS_TYPE_BCMA:
  57. bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc,
  58. 0xfffffff);
  59. break;
  60. #endif
  61. }
  62. }
  63. static inline int bcm47xx_wdt_hw_stop(void)
  64. {
  65. switch (bcm47xx_bus_type) {
  66. #ifdef CONFIG_BCM47XX_SSB
  67. case BCM47XX_BUS_TYPE_SSB:
  68. return ssb_watchdog_timer_set(&bcm47xx_bus.ssb, 0);
  69. #endif
  70. #ifdef CONFIG_BCM47XX_BCMA
  71. case BCM47XX_BUS_TYPE_BCMA:
  72. bcma_chipco_watchdog_timer_set(&bcm47xx_bus.bcma.bus.drv_cc, 0);
  73. return 0;
  74. #endif
  75. }
  76. return -EINVAL;
  77. }
  78. static void bcm47xx_timer_tick(unsigned long unused)
  79. {
  80. if (!atomic_dec_and_test(&ticks)) {
  81. bcm47xx_wdt_hw_start();
  82. mod_timer(&wdt_timer, jiffies + HZ);
  83. } else {
  84. printk(KERN_CRIT DRV_NAME "Watchdog will fire soon!!!\n");
  85. }
  86. }
  87. static inline void bcm47xx_wdt_pet(void)
  88. {
  89. atomic_set(&ticks, wdt_time);
  90. }
  91. static void bcm47xx_wdt_start(void)
  92. {
  93. bcm47xx_wdt_pet();
  94. bcm47xx_timer_tick(0);
  95. }
  96. static void bcm47xx_wdt_pause(void)
  97. {
  98. del_timer_sync(&wdt_timer);
  99. bcm47xx_wdt_hw_stop();
  100. }
  101. static void bcm47xx_wdt_stop(void)
  102. {
  103. bcm47xx_wdt_pause();
  104. }
  105. static int bcm47xx_wdt_settimeout(int new_time)
  106. {
  107. if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
  108. return -EINVAL;
  109. wdt_time = new_time;
  110. return 0;
  111. }
  112. static int bcm47xx_wdt_open(struct inode *inode, struct file *file)
  113. {
  114. if (test_and_set_bit(0, &bcm47xx_wdt_busy))
  115. return -EBUSY;
  116. bcm47xx_wdt_start();
  117. return nonseekable_open(inode, file);
  118. }
  119. static int bcm47xx_wdt_release(struct inode *inode, struct file *file)
  120. {
  121. if (expect_release == 42) {
  122. bcm47xx_wdt_stop();
  123. } else {
  124. printk(KERN_CRIT DRV_NAME
  125. ": Unexpected close, not stopping watchdog!\n");
  126. bcm47xx_wdt_start();
  127. }
  128. clear_bit(0, &bcm47xx_wdt_busy);
  129. expect_release = 0;
  130. return 0;
  131. }
  132. static ssize_t bcm47xx_wdt_write(struct file *file, const char __user *data,
  133. size_t len, loff_t *ppos)
  134. {
  135. if (len) {
  136. if (!nowayout) {
  137. size_t i;
  138. expect_release = 0;
  139. for (i = 0; i != len; i++) {
  140. char c;
  141. if (get_user(c, data + i))
  142. return -EFAULT;
  143. if (c == 'V')
  144. expect_release = 42;
  145. }
  146. }
  147. bcm47xx_wdt_pet();
  148. }
  149. return len;
  150. }
  151. static const struct watchdog_info bcm47xx_wdt_info = {
  152. .identity = DRV_NAME,
  153. .options = WDIOF_SETTIMEOUT |
  154. WDIOF_KEEPALIVEPING |
  155. WDIOF_MAGICCLOSE,
  156. };
  157. static long bcm47xx_wdt_ioctl(struct file *file,
  158. unsigned int cmd, unsigned long arg)
  159. {
  160. void __user *argp = (void __user *)arg;
  161. int __user *p = argp;
  162. int new_value, retval = -EINVAL;
  163. switch (cmd) {
  164. case WDIOC_GETSUPPORT:
  165. return copy_to_user(argp, &bcm47xx_wdt_info,
  166. sizeof(bcm47xx_wdt_info)) ? -EFAULT : 0;
  167. case WDIOC_GETSTATUS:
  168. case WDIOC_GETBOOTSTATUS:
  169. return put_user(0, p);
  170. case WDIOC_SETOPTIONS:
  171. if (get_user(new_value, p))
  172. return -EFAULT;
  173. if (new_value & WDIOS_DISABLECARD) {
  174. bcm47xx_wdt_stop();
  175. retval = 0;
  176. }
  177. if (new_value & WDIOS_ENABLECARD) {
  178. bcm47xx_wdt_start();
  179. retval = 0;
  180. }
  181. return retval;
  182. case WDIOC_KEEPALIVE:
  183. bcm47xx_wdt_pet();
  184. return 0;
  185. case WDIOC_SETTIMEOUT:
  186. if (get_user(new_value, p))
  187. return -EFAULT;
  188. if (bcm47xx_wdt_settimeout(new_value))
  189. return -EINVAL;
  190. bcm47xx_wdt_pet();
  191. case WDIOC_GETTIMEOUT:
  192. return put_user(wdt_time, p);
  193. default:
  194. return -ENOTTY;
  195. }
  196. }
  197. static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
  198. unsigned long code, void *unused)
  199. {
  200. if (code == SYS_DOWN || code == SYS_HALT)
  201. bcm47xx_wdt_stop();
  202. return NOTIFY_DONE;
  203. }
  204. static const struct file_operations bcm47xx_wdt_fops = {
  205. .owner = THIS_MODULE,
  206. .llseek = no_llseek,
  207. .unlocked_ioctl = bcm47xx_wdt_ioctl,
  208. .open = bcm47xx_wdt_open,
  209. .release = bcm47xx_wdt_release,
  210. .write = bcm47xx_wdt_write,
  211. };
  212. static struct miscdevice bcm47xx_wdt_miscdev = {
  213. .minor = WATCHDOG_MINOR,
  214. .name = "watchdog",
  215. .fops = &bcm47xx_wdt_fops,
  216. };
  217. static struct notifier_block bcm47xx_wdt_notifier = {
  218. .notifier_call = bcm47xx_wdt_notify_sys,
  219. };
  220. static int __init bcm47xx_wdt_init(void)
  221. {
  222. int ret;
  223. if (bcm47xx_wdt_hw_stop() < 0)
  224. return -ENODEV;
  225. setup_timer(&wdt_timer, bcm47xx_timer_tick, 0L);
  226. if (bcm47xx_wdt_settimeout(wdt_time)) {
  227. bcm47xx_wdt_settimeout(WDT_DEFAULT_TIME);
  228. printk(KERN_INFO DRV_NAME ": "
  229. "wdt_time value must be 0 < wdt_time < %d, using %d\n",
  230. (WDT_MAX_TIME + 1), wdt_time);
  231. }
  232. ret = register_reboot_notifier(&bcm47xx_wdt_notifier);
  233. if (ret)
  234. return ret;
  235. ret = misc_register(&bcm47xx_wdt_miscdev);
  236. if (ret) {
  237. unregister_reboot_notifier(&bcm47xx_wdt_notifier);
  238. return ret;
  239. }
  240. printk(KERN_INFO "BCM47xx Watchdog Timer enabled (%d seconds%s)\n",
  241. wdt_time, nowayout ? ", nowayout" : "");
  242. return 0;
  243. }
  244. static void __exit bcm47xx_wdt_exit(void)
  245. {
  246. if (!nowayout)
  247. bcm47xx_wdt_stop();
  248. misc_deregister(&bcm47xx_wdt_miscdev);
  249. unregister_reboot_notifier(&bcm47xx_wdt_notifier);
  250. }
  251. module_init(bcm47xx_wdt_init);
  252. module_exit(bcm47xx_wdt_exit);
  253. MODULE_AUTHOR("Aleksandar Radovanovic");
  254. MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
  255. MODULE_LICENSE("GPL");
  256. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);