mpc83xx_wdt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * mpc83xx_wdt.c - MPC83xx watchdog userspace interface
  3. *
  4. * Authors: Dave Updegraff <dave@cray.org>
  5. * Kumar Gala <galak@kernel.crashing.org>
  6. * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
  7. * ..and from sc520_wdt
  8. *
  9. * Note: it appears that you can only actually ENABLE or DISABLE the thing
  10. * once after POR. Once enabled, you cannot disable, and vice versa.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/fs.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/module.h>
  23. #include <linux/watchdog.h>
  24. #include <asm/io.h>
  25. #include <asm/uaccess.h>
  26. struct mpc83xx_wdt {
  27. __be32 res0;
  28. __be32 swcrr; /* System watchdog control register */
  29. #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
  30. #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
  31. #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
  32. #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
  33. __be32 swcnr; /* System watchdog count register */
  34. u8 res1[2];
  35. __be16 swsrr; /* System watchdog service register */
  36. u8 res2[0xF0];
  37. };
  38. static struct mpc83xx_wdt __iomem *wd_base;
  39. static u16 timeout = 0xffff;
  40. module_param(timeout, ushort, 0);
  41. MODULE_PARM_DESC(timeout, "Watchdog timeout in ticks. (0<timeout<65536, default=65535");
  42. static int reset = 1;
  43. module_param(reset, bool, 0);
  44. MODULE_PARM_DESC(reset, "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
  45. /*
  46. * We always prescale, but if someone really doesn't want to they can set this
  47. * to 0
  48. */
  49. static int prescale = 1;
  50. static unsigned int timeout_sec;
  51. static unsigned long wdt_is_open;
  52. static spinlock_t wdt_spinlock;
  53. static void mpc83xx_wdt_keepalive(void)
  54. {
  55. /* Ping the WDT */
  56. spin_lock(&wdt_spinlock);
  57. out_be16(&wd_base->swsrr, 0x556c);
  58. out_be16(&wd_base->swsrr, 0xaa39);
  59. spin_unlock(&wdt_spinlock);
  60. }
  61. static ssize_t mpc83xx_wdt_write(struct file *file, const char __user *buf,
  62. size_t count, loff_t *ppos)
  63. {
  64. if (count)
  65. mpc83xx_wdt_keepalive();
  66. return count;
  67. }
  68. static int mpc83xx_wdt_open(struct inode *inode, struct file *file)
  69. {
  70. u32 tmp = SWCRR_SWEN;
  71. if (test_and_set_bit(0, &wdt_is_open))
  72. return -EBUSY;
  73. /* Once we start the watchdog we can't stop it */
  74. __module_get(THIS_MODULE);
  75. /* Good, fire up the show */
  76. if (prescale)
  77. tmp |= SWCRR_SWPR;
  78. if (reset)
  79. tmp |= SWCRR_SWRI;
  80. tmp |= timeout << 16;
  81. out_be32(&wd_base->swcrr, tmp);
  82. return nonseekable_open(inode, file);
  83. }
  84. static int mpc83xx_wdt_release(struct inode *inode, struct file *file)
  85. {
  86. printk(KERN_CRIT "Unexpected close, not stopping watchdog!\n");
  87. mpc83xx_wdt_keepalive();
  88. clear_bit(0, &wdt_is_open);
  89. return 0;
  90. }
  91. static int mpc83xx_wdt_ioctl(struct inode *inode, struct file *file,
  92. unsigned int cmd, unsigned long arg)
  93. {
  94. void __user *argp = (void __user *)arg;
  95. int __user *p = argp;
  96. static struct watchdog_info ident = {
  97. .options = WDIOF_KEEPALIVEPING,
  98. .firmware_version = 1,
  99. .identity = "MPC83xx",
  100. };
  101. switch (cmd) {
  102. case WDIOC_GETSUPPORT:
  103. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  104. case WDIOC_KEEPALIVE:
  105. mpc83xx_wdt_keepalive();
  106. return 0;
  107. case WDIOC_GETTIMEOUT:
  108. return put_user(timeout_sec, p);
  109. default:
  110. return -ENOTTY;
  111. }
  112. }
  113. static const struct file_operations mpc83xx_wdt_fops = {
  114. .owner = THIS_MODULE,
  115. .llseek = no_llseek,
  116. .write = mpc83xx_wdt_write,
  117. .ioctl = mpc83xx_wdt_ioctl,
  118. .open = mpc83xx_wdt_open,
  119. .release = mpc83xx_wdt_release,
  120. };
  121. static struct miscdevice mpc83xx_wdt_miscdev = {
  122. .minor = WATCHDOG_MINOR,
  123. .name = "watchdog",
  124. .fops = &mpc83xx_wdt_fops,
  125. };
  126. static int __devinit mpc83xx_wdt_probe(struct platform_device *dev)
  127. {
  128. struct resource *r;
  129. int ret;
  130. unsigned int *freq = dev->dev.platform_data;
  131. /* get a pointer to the register memory */
  132. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  133. if (!r) {
  134. ret = -ENODEV;
  135. goto err_out;
  136. }
  137. wd_base = ioremap(r->start, sizeof (struct mpc83xx_wdt));
  138. if (wd_base == NULL) {
  139. ret = -ENOMEM;
  140. goto err_out;
  141. }
  142. ret = misc_register(&mpc83xx_wdt_miscdev);
  143. if (ret) {
  144. printk(KERN_ERR "cannot register miscdev on minor=%d "
  145. "(err=%d)\n",
  146. WATCHDOG_MINOR, ret);
  147. goto err_unmap;
  148. }
  149. /* Calculate the timeout in seconds */
  150. if (prescale)
  151. timeout_sec = (timeout * 0x10000) / (*freq);
  152. else
  153. timeout_sec = timeout / (*freq);
  154. printk(KERN_INFO "WDT driver for MPC83xx initialized. "
  155. "mode:%s timeout=%d (%d seconds)\n",
  156. reset ? "reset":"interrupt", timeout, timeout_sec);
  157. spin_lock_init(&wdt_spinlock);
  158. return 0;
  159. err_unmap:
  160. iounmap(wd_base);
  161. err_out:
  162. return ret;
  163. }
  164. static int __devexit mpc83xx_wdt_remove(struct platform_device *dev)
  165. {
  166. misc_deregister(&mpc83xx_wdt_miscdev);
  167. iounmap(wd_base);
  168. return 0;
  169. }
  170. static struct platform_driver mpc83xx_wdt_driver = {
  171. .probe = mpc83xx_wdt_probe,
  172. .remove = __devexit_p(mpc83xx_wdt_remove),
  173. .driver = {
  174. .name = "mpc83xx_wdt",
  175. },
  176. };
  177. static int __init mpc83xx_wdt_init(void)
  178. {
  179. return platform_driver_register(&mpc83xx_wdt_driver);
  180. }
  181. static void __exit mpc83xx_wdt_exit(void)
  182. {
  183. platform_driver_unregister(&mpc83xx_wdt_driver);
  184. }
  185. module_init(mpc83xx_wdt_init);
  186. module_exit(mpc83xx_wdt_exit);
  187. MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
  188. MODULE_DESCRIPTION("Driver for watchdog timer in MPC83xx uProcessor");
  189. MODULE_LICENSE("GPL");
  190. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);