ifxmips.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Copyright (C) 2008 John Crispin <[email protected]>
  17. * Based on EP93xx wdt driver
  18. */
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/uaccess.h>
  26. #include <ifxmips.h>
  27. #include <ifxmips_cgu.h>
  28. #define IFXMIPS_WDT_PW1 0x00BE0000
  29. #define IFXMIPS_WDT_PW2 0x00DC0000
  30. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  31. static int wdt_ok_to_close;
  32. #endif
  33. static int wdt_timeout = 30;
  34. int ifxmips_wdt_enable(unsigned int timeout)
  35. {
  36. u32 fpi;
  37. fpi = cgu_get_io_region_clock();
  38. ifxmips_w32(IFXMIPS_WDT_PW1, IFXMIPS_BIU_WDT_CR);
  39. ifxmips_w32(IFXMIPS_WDT_PW2 |
  40. (0x3 << 26) | /* PWL */
  41. (0x3 << 24) | /* CLKDIV */
  42. (0x1 << 31) | /* enable */
  43. ((timeout * (fpi / 0x40000)) + 0x1000), /* reload */
  44. IFXMIPS_BIU_WDT_CR);
  45. return 0;
  46. }
  47. void ifxmips_wdt_disable(void)
  48. {
  49. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  50. wdt_ok_to_close = 0;
  51. #endif
  52. ifxmips_w32(IFXMIPS_WDT_PW1, IFXMIPS_BIU_WDT_CR);
  53. ifxmips_w32(IFXMIPS_WDT_PW2, IFXMIPS_BIU_WDT_CR);
  54. }
  55. static ssize_t ifxmips_wdt_write(struct file *file, const char __user *data,
  56. size_t len, loff_t *ppos)
  57. {
  58. size_t i;
  59. if (!len)
  60. return 0;
  61. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  62. for (i = 0; i != len; i++) {
  63. char c;
  64. if (get_user(c, data + i))
  65. return -EFAULT;
  66. if (c == 'V')
  67. wdt_ok_to_close = 1;
  68. }
  69. #endif
  70. ifxmips_wdt_enable(wdt_timeout);
  71. return len;
  72. }
  73. static struct watchdog_info ident = {
  74. .options = WDIOF_MAGICCLOSE,
  75. .identity = "ifxmips Watchdog",
  76. };
  77. static int ifxmips_wdt_ioctl(struct inode *inode, struct file *file,
  78. unsigned int cmd, unsigned long arg)
  79. {
  80. int ret = -ENOTTY;
  81. switch (cmd) {
  82. case WDIOC_GETSUPPORT:
  83. ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
  84. sizeof(ident)) ? -EFAULT : 0;
  85. break;
  86. case WDIOC_GETTIMEOUT:
  87. ret = put_user(wdt_timeout, (int __user *)arg);
  88. break;
  89. case WDIOC_SETTIMEOUT:
  90. ret = get_user(wdt_timeout, (int __user *)arg);
  91. break;
  92. case WDIOC_KEEPALIVE:
  93. ifxmips_wdt_enable(wdt_timeout);
  94. ret = 0;
  95. break;
  96. }
  97. return ret;
  98. }
  99. static int ifxmips_wdt_open(struct inode *inode, struct file *file)
  100. {
  101. ifxmips_wdt_enable(wdt_timeout);
  102. return nonseekable_open(inode, file);
  103. }
  104. static int ifxmips_wdt_release(struct inode *inode, struct file *file)
  105. {
  106. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  107. if (wdt_ok_to_close)
  108. ifxmips_wdt_disable();
  109. else
  110. #endif
  111. printk(KERN_ERR "ifxmips_wdt: watchdog closed without warning,"
  112. " rebooting system\n");
  113. return 0;
  114. }
  115. static const struct file_operations ifxmips_wdt_fops = {
  116. .owner = THIS_MODULE,
  117. .write = ifxmips_wdt_write,
  118. .ioctl = ifxmips_wdt_ioctl,
  119. .open = ifxmips_wdt_open,
  120. .release = ifxmips_wdt_release,
  121. };
  122. static struct miscdevice ifxmips_wdt_miscdev = {
  123. .minor = WATCHDOG_MINOR,
  124. .name = "watchdog",
  125. .fops = &ifxmips_wdt_fops,
  126. };
  127. static int ifxmips_wdt_probe(struct platform_device *dev)
  128. {
  129. int err;
  130. err = misc_register(&ifxmips_wdt_miscdev);
  131. if (err)
  132. printk(KERN_INFO "ifxmips_wdt: error creating device\n");
  133. else
  134. printk(KERN_INFO "ifxmips_wdt: loaded\n");
  135. return err;
  136. }
  137. static int ifxmips_wdt_remove(struct platform_device *dev)
  138. {
  139. ifxmips_wdt_disable();
  140. misc_deregister(&ifxmips_wdt_miscdev);
  141. return 0;
  142. }
  143. static struct platform_driver ifxmips_wdt_driver = {
  144. .probe = ifxmips_wdt_probe,
  145. .remove = ifxmips_wdt_remove,
  146. .driver = {
  147. .name = "ifxmips_wdt",
  148. .owner = THIS_MODULE,
  149. },
  150. };
  151. static int __init init_ifxmips_wdt(void)
  152. {
  153. int ret = platform_driver_register(&ifxmips_wdt_driver);
  154. if (ret)
  155. printk(KERN_INFO "ifxmips_wdt: error registering platfom driver!");
  156. return ret;
  157. }
  158. static void __exit exit_ifxmips_wdt(void)
  159. {
  160. platform_driver_unregister(&ifxmips_wdt_driver);
  161. }
  162. module_init(init_ifxmips_wdt);
  163. module_exit(exit_ifxmips_wdt);
  164. MODULE_AUTHOR("John Crispin <[email protected]>");
  165. MODULE_DESCRIPTION("ifxmips Watchdog");
  166. MODULE_LICENSE("GPL");
  167. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);