adm5120_wdt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * ADM5120_WDT 0.01: Infineon ADM5120 SoC watchdog driver
  3. * Copyright (c) Ondrej Zajicek <santiago@crfreenet.org>, 2007
  4. *
  5. * based on
  6. *
  7. * RC32434_WDT 0.01: IDT Interprise 79RC32434 watchdog driver
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/fs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/irq.h>
  21. #include <asm/bootinfo.h>
  22. #include <asm/mach-adm5120/adm5120_info.h>
  23. #include <asm/mach-adm5120/adm5120_defs.h>
  24. #include <asm/mach-adm5120/adm5120_switch.h>
  25. #define DEFAULT_TIMEOUT 15 /* (secs) Default is 15 seconds */
  26. #define MAX_TIMEOUT 327
  27. /* Max is 327 seconds, counter is 15-bit integer, step is 10 ms */
  28. #define NAME "adm5120_wdt"
  29. #define VERSION "0.1"
  30. static int expect_close;
  31. static int access;
  32. static unsigned int timeout = DEFAULT_TIMEOUT;
  33. static int nowayout = WATCHDOG_NOWAYOUT;
  34. module_param(nowayout, int, 0);
  35. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  36. MODULE_LICENSE("GPL");
  37. static inline void wdt_set_timeout(void)
  38. {
  39. u32 val = (1 << 31) | (((timeout * 100) & 0x7FFF) << 16);
  40. SW_WRITE_REG(SWITCH_REG_WDOG0, val);
  41. }
  42. /*
  43. It looks like WDOG0-register-write don't modify counter,
  44. but WDOG0-register-read resets counter.
  45. */
  46. static inline void wdt_reset_counter(void)
  47. {
  48. SW_READ_REG(SWITCH_REG_WDOG0);
  49. }
  50. static inline void wdt_disable(void)
  51. {
  52. SW_WRITE_REG(SWITCH_REG_WDOG0, 0x7FFF0000);
  53. }
  54. static int wdt_open(struct inode *inode, struct file *file)
  55. {
  56. /* Allow only one person to hold it open */
  57. if (access)
  58. return -EBUSY;
  59. if (nowayout)
  60. __module_get(THIS_MODULE);
  61. /* Activate timer */
  62. wdt_reset_counter();
  63. wdt_set_timeout();
  64. printk(KERN_INFO NAME ": enabling watchdog timer\n");
  65. access = 1;
  66. return 0;
  67. }
  68. static int wdt_release(struct inode *inode, struct file *file)
  69. {
  70. /*
  71. * Shut off the timer.
  72. * Lock it in if it's a module and we set nowayout
  73. */
  74. if (expect_close && (nowayout == 0)) {
  75. wdt_disable();
  76. printk(KERN_INFO NAME ": disabling watchdog timer\n");
  77. module_put(THIS_MODULE);
  78. } else
  79. printk(KERN_CRIT NAME ": device closed unexpectedly. WDT will not stop!\n");
  80. access = 0;
  81. return 0;
  82. }
  83. static ssize_t wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  84. {
  85. /* Refresh the timer. */
  86. if (len) {
  87. if (!nowayout) {
  88. size_t i;
  89. /* In case it was set long ago */
  90. expect_close = 0;
  91. for (i = 0; i != len; i++) {
  92. char c;
  93. if (get_user(c, data + i))
  94. return -EFAULT;
  95. if (c == 'V')
  96. expect_close = 1;
  97. }
  98. }
  99. wdt_reset_counter();
  100. return len;
  101. }
  102. return 0;
  103. }
  104. static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  105. {
  106. int new_timeout;
  107. static struct watchdog_info ident = {
  108. .options = WDIOF_SETTIMEOUT |
  109. WDIOF_KEEPALIVEPING |
  110. WDIOF_MAGICCLOSE,
  111. .firmware_version = 0,
  112. .identity = "ADM5120_WDT Watchdog",
  113. };
  114. switch (cmd) {
  115. default:
  116. return -ENOTTY;
  117. case WDIOC_GETSUPPORT:
  118. if (copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident)))
  119. return -EFAULT;
  120. return 0;
  121. case WDIOC_GETSTATUS:
  122. case WDIOC_GETBOOTSTATUS:
  123. return put_user(0, (int *)arg);
  124. case WDIOC_KEEPALIVE:
  125. wdt_reset_counter();
  126. return 0;
  127. case WDIOC_SETTIMEOUT:
  128. if (get_user(new_timeout, (int *)arg))
  129. return -EFAULT;
  130. if (new_timeout < 1)
  131. return -EINVAL;
  132. if (new_timeout > MAX_TIMEOUT)
  133. return -EINVAL;
  134. timeout = new_timeout;
  135. wdt_set_timeout();
  136. /* Fall */
  137. case WDIOC_GETTIMEOUT:
  138. return put_user(timeout, (int *)arg);
  139. }
  140. }
  141. static const struct file_operations wdt_fops = {
  142. .owner = THIS_MODULE,
  143. .llseek = no_llseek,
  144. .write = wdt_write,
  145. .unlocked_ioctl = wdt_ioctl,
  146. .open = wdt_open,
  147. .release = wdt_release,
  148. };
  149. static struct miscdevice wdt_miscdev = {
  150. .minor = WATCHDOG_MINOR,
  151. .name = "watchdog",
  152. .fops = &wdt_fops,
  153. };
  154. static char banner[] __initdata = KERN_INFO NAME ": Watchdog Timer version " VERSION "\n";
  155. static int __init watchdog_init(void)
  156. {
  157. int ret;
  158. ret = misc_register(&wdt_miscdev);
  159. if (ret)
  160. return ret;
  161. wdt_disable();
  162. printk(banner);
  163. return 0;
  164. }
  165. static void __exit watchdog_exit(void)
  166. {
  167. misc_deregister(&wdt_miscdev);
  168. }
  169. module_init(watchdog_init);
  170. module_exit(watchdog_exit);