orion5x_wdt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * drivers/watchdog/orion5x_wdt.c
  3. *
  4. * Watchdog driver for Orion5x processors
  5. *
  6. * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/fs.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/init.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/io.h>
  22. #include <linux/spinlock.h>
  23. /*
  24. * Watchdog timer block registers.
  25. */
  26. #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
  27. #define WDT_EN 0x0010
  28. #define WDT_VAL (TIMER_VIRT_BASE + 0x0024)
  29. #define WDT_MAX_DURATION (0xffffffff / ORION5X_TCLK)
  30. #define WDT_IN_USE 0
  31. #define WDT_OK_TO_CLOSE 1
  32. static int nowayout = WATCHDOG_NOWAYOUT;
  33. static int heartbeat = WDT_MAX_DURATION; /* (seconds) */
  34. static unsigned long wdt_status;
  35. static spinlock_t wdt_lock;
  36. static void wdt_enable(void)
  37. {
  38. u32 reg;
  39. spin_lock(&wdt_lock);
  40. /* Set watchdog duration */
  41. writel(ORION5X_TCLK * heartbeat, WDT_VAL);
  42. /* Clear watchdog timer interrupt */
  43. reg = readl(BRIDGE_CAUSE);
  44. reg &= ~WDT_INT_REQ;
  45. writel(reg, BRIDGE_CAUSE);
  46. /* Enable watchdog timer */
  47. reg = readl(TIMER_CTRL);
  48. reg |= WDT_EN;
  49. writel(reg, TIMER_CTRL);
  50. /* Enable reset on watchdog */
  51. reg = readl(CPU_RESET_MASK);
  52. reg |= WDT_RESET;
  53. writel(reg, CPU_RESET_MASK);
  54. spin_unlock(&wdt_lock);
  55. }
  56. static void wdt_disable(void)
  57. {
  58. u32 reg;
  59. spin_lock(&wdt_lock);
  60. /* Disable reset on watchdog */
  61. reg = readl(CPU_RESET_MASK);
  62. reg &= ~WDT_RESET;
  63. writel(reg, CPU_RESET_MASK);
  64. /* Disable watchdog timer */
  65. reg = readl(TIMER_CTRL);
  66. reg &= ~WDT_EN;
  67. writel(reg, TIMER_CTRL);
  68. spin_unlock(&wdt_lock);
  69. }
  70. static int orion5x_wdt_get_timeleft(int *time_left)
  71. {
  72. spin_lock(&wdt_lock);
  73. *time_left = readl(WDT_VAL) / ORION5X_TCLK;
  74. spin_unlock(&wdt_lock);
  75. return 0;
  76. }
  77. static int orion5x_wdt_open(struct inode *inode, struct file *file)
  78. {
  79. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  80. return -EBUSY;
  81. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  82. wdt_enable();
  83. return nonseekable_open(inode, file);
  84. }
  85. static ssize_t orion5x_wdt_write(struct file *file, const char *data,
  86. size_t len, loff_t *ppos)
  87. {
  88. if (len) {
  89. if (!nowayout) {
  90. size_t i;
  91. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  92. for (i = 0; i != len; i++) {
  93. char c;
  94. if (get_user(c, data + i))
  95. return -EFAULT;
  96. if (c == 'V')
  97. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  98. }
  99. }
  100. wdt_enable();
  101. }
  102. return len;
  103. }
  104. static struct watchdog_info ident = {
  105. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  106. WDIOF_KEEPALIVEPING,
  107. .identity = "Orion5x Watchdog",
  108. };
  109. static long orion5x_wdt_ioctl(struct file *file, unsigned int cmd,
  110. unsigned long arg)
  111. {
  112. int ret = -ENOTTY;
  113. int time;
  114. switch (cmd) {
  115. case WDIOC_GETSUPPORT:
  116. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  117. sizeof(ident)) ? -EFAULT : 0;
  118. break;
  119. case WDIOC_GETSTATUS:
  120. case WDIOC_GETBOOTSTATUS:
  121. ret = put_user(0, (int *)arg);
  122. break;
  123. case WDIOC_KEEPALIVE:
  124. wdt_enable();
  125. ret = 0;
  126. break;
  127. case WDIOC_SETTIMEOUT:
  128. ret = get_user(time, (int *)arg);
  129. if (ret)
  130. break;
  131. if (time <= 0 || time > WDT_MAX_DURATION) {
  132. ret = -EINVAL;
  133. break;
  134. }
  135. heartbeat = time;
  136. wdt_enable();
  137. /* Fall through */
  138. case WDIOC_GETTIMEOUT:
  139. ret = put_user(heartbeat, (int *)arg);
  140. break;
  141. case WDIOC_GETTIMELEFT:
  142. if (orion5x_wdt_get_timeleft(&time)) {
  143. ret = -EINVAL;
  144. break;
  145. }
  146. ret = put_user(time, (int *)arg);
  147. break;
  148. }
  149. return ret;
  150. }
  151. static int orion5x_wdt_release(struct inode *inode, struct file *file)
  152. {
  153. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  154. wdt_disable();
  155. else
  156. printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
  157. "timer will not stop\n");
  158. clear_bit(WDT_IN_USE, &wdt_status);
  159. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  160. return 0;
  161. }
  162. static const struct file_operations orion5x_wdt_fops = {
  163. .owner = THIS_MODULE,
  164. .llseek = no_llseek,
  165. .write = orion5x_wdt_write,
  166. .unlocked_ioctl = orion5x_wdt_ioctl,
  167. .open = orion5x_wdt_open,
  168. .release = orion5x_wdt_release,
  169. };
  170. static struct miscdevice orion5x_wdt_miscdev = {
  171. .minor = WATCHDOG_MINOR,
  172. .name = "watchdog",
  173. .fops = &orion5x_wdt_fops,
  174. };
  175. static int __init orion5x_wdt_init(void)
  176. {
  177. int ret;
  178. spin_lock_init(&wdt_lock);
  179. ret = misc_register(&orion5x_wdt_miscdev);
  180. if (ret == 0)
  181. printk("Orion5x Watchdog Timer: heartbeat %d sec\n",
  182. heartbeat);
  183. return ret;
  184. }
  185. static void __exit orion5x_wdt_exit(void)
  186. {
  187. misc_deregister(&orion5x_wdt_miscdev);
  188. }
  189. module_init(orion5x_wdt_init);
  190. module_exit(orion5x_wdt_exit);
  191. MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
  192. MODULE_DESCRIPTION("Orion5x Processor Watchdog");
  193. module_param(heartbeat, int, 0);
  194. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default is "
  195. __MODULE_STRING(WDT_MAX_DURATION) ")");
  196. module_param(nowayout, int, 0);
  197. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  198. MODULE_LICENSE("GPL");
  199. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);