mv64x60_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface
  3. *
  4. * Author: James Chapman <jchapman@katalix.com>
  5. *
  6. * Platform-specific setup code should configure the dog to generate
  7. * interrupt or reset as required. This code only enables/disables
  8. * and services the watchdog.
  9. *
  10. * Derived from mpc8xx_wdt.c, with the following copyright.
  11. *
  12. * 2002 (c) Florian Schirmer <jolt@tuxbox.org> This file is licensed under
  13. * the terms of the GNU General Public License version 2. This program
  14. * is licensed "as is" without any warranty of any kind, whether express
  15. * or implied.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/fs.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/module.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/platform_device.h>
  25. #include <asm/mv64x60.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/io.h>
  28. /* MV64x60 WDC (config) register access definitions */
  29. #define MV64x60_WDC_CTL1_MASK (3 << 24)
  30. #define MV64x60_WDC_CTL1(val) ((val & 3) << 24)
  31. #define MV64x60_WDC_CTL2_MASK (3 << 26)
  32. #define MV64x60_WDC_CTL2(val) ((val & 3) << 26)
  33. /* Flags bits */
  34. #define MV64x60_WDOG_FLAG_OPENED 0
  35. #define MV64x60_WDOG_FLAG_ENABLED 1
  36. static unsigned long wdt_flags;
  37. static int wdt_status;
  38. static void __iomem *mv64x60_regs;
  39. static int mv64x60_wdt_timeout;
  40. static void mv64x60_wdt_reg_write(u32 val)
  41. {
  42. /* Allow write only to CTL1 / CTL2 fields, retaining values in
  43. * other fields.
  44. */
  45. u32 data = readl(mv64x60_regs + MV64x60_WDT_WDC);
  46. data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
  47. data |= val;
  48. writel(data, mv64x60_regs + MV64x60_WDT_WDC);
  49. }
  50. static void mv64x60_wdt_service(void)
  51. {
  52. /* Write 01 followed by 10 to CTL2 */
  53. mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01));
  54. mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02));
  55. }
  56. static void mv64x60_wdt_handler_disable(void)
  57. {
  58. if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
  59. /* Write 01 followed by 10 to CTL1 */
  60. mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
  61. mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
  62. printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
  63. }
  64. }
  65. static void mv64x60_wdt_handler_enable(void)
  66. {
  67. if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
  68. /* Write 01 followed by 10 to CTL1 */
  69. mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
  70. mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
  71. printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
  72. }
  73. }
  74. static int mv64x60_wdt_open(struct inode *inode, struct file *file)
  75. {
  76. if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
  77. return -EBUSY;
  78. mv64x60_wdt_service();
  79. mv64x60_wdt_handler_enable();
  80. nonseekable_open(inode, file);
  81. return 0;
  82. }
  83. static int mv64x60_wdt_release(struct inode *inode, struct file *file)
  84. {
  85. mv64x60_wdt_service();
  86. #if !defined(CONFIG_WATCHDOG_NOWAYOUT)
  87. mv64x60_wdt_handler_disable();
  88. #endif
  89. clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
  90. return 0;
  91. }
  92. static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
  93. size_t len, loff_t * ppos)
  94. {
  95. if (len)
  96. mv64x60_wdt_service();
  97. return len;
  98. }
  99. static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
  100. unsigned int cmd, unsigned long arg)
  101. {
  102. int timeout;
  103. void __user *argp = (void __user *)arg;
  104. static struct watchdog_info info = {
  105. .options = WDIOF_KEEPALIVEPING,
  106. .firmware_version = 0,
  107. .identity = "MV64x60 watchdog",
  108. };
  109. switch (cmd) {
  110. case WDIOC_GETSUPPORT:
  111. if (copy_to_user(argp, &info, sizeof(info)))
  112. return -EFAULT;
  113. break;
  114. case WDIOC_GETSTATUS:
  115. case WDIOC_GETBOOTSTATUS:
  116. if (put_user(wdt_status, (int __user *)argp))
  117. return -EFAULT;
  118. wdt_status &= ~WDIOF_KEEPALIVEPING;
  119. break;
  120. case WDIOC_GETTEMP:
  121. return -EOPNOTSUPP;
  122. case WDIOC_SETOPTIONS:
  123. return -EOPNOTSUPP;
  124. case WDIOC_KEEPALIVE:
  125. mv64x60_wdt_service();
  126. wdt_status |= WDIOF_KEEPALIVEPING;
  127. break;
  128. case WDIOC_SETTIMEOUT:
  129. return -EOPNOTSUPP;
  130. case WDIOC_GETTIMEOUT:
  131. timeout = mv64x60_wdt_timeout * HZ;
  132. if (put_user(timeout, (int __user *)argp))
  133. return -EFAULT;
  134. break;
  135. default:
  136. return -ENOIOCTLCMD;
  137. }
  138. return 0;
  139. }
  140. static struct file_operations mv64x60_wdt_fops = {
  141. .owner = THIS_MODULE,
  142. .llseek = no_llseek,
  143. .write = mv64x60_wdt_write,
  144. .ioctl = mv64x60_wdt_ioctl,
  145. .open = mv64x60_wdt_open,
  146. .release = mv64x60_wdt_release,
  147. };
  148. static struct miscdevice mv64x60_wdt_miscdev = {
  149. .minor = WATCHDOG_MINOR,
  150. .name = "watchdog",
  151. .fops = &mv64x60_wdt_fops,
  152. };
  153. static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
  154. {
  155. struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
  156. int bus_clk = 133;
  157. mv64x60_wdt_timeout = 10;
  158. if (pdata) {
  159. mv64x60_wdt_timeout = pdata->timeout;
  160. bus_clk = pdata->bus_clk;
  161. }
  162. mv64x60_regs = mv64x60_get_bridge_vbase();
  163. writel((mv64x60_wdt_timeout * (bus_clk * 1000000)) >> 8,
  164. mv64x60_regs + MV64x60_WDT_WDC);
  165. return misc_register(&mv64x60_wdt_miscdev);
  166. }
  167. static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
  168. {
  169. misc_deregister(&mv64x60_wdt_miscdev);
  170. mv64x60_wdt_service();
  171. mv64x60_wdt_handler_disable();
  172. return 0;
  173. }
  174. static struct platform_driver mv64x60_wdt_driver = {
  175. .probe = mv64x60_wdt_probe,
  176. .remove = __devexit_p(mv64x60_wdt_remove),
  177. .driver = {
  178. .owner = THIS_MODULE,
  179. .name = MV64x60_WDT_NAME,
  180. },
  181. };
  182. static struct platform_device *mv64x60_wdt_dev;
  183. static int __init mv64x60_wdt_init(void)
  184. {
  185. int ret;
  186. printk(KERN_INFO "MV64x60 watchdog driver\n");
  187. mv64x60_wdt_dev = platform_device_register_simple(MV64x60_WDT_NAME,
  188. -1, NULL, 0);
  189. if (IS_ERR(mv64x60_wdt_dev)) {
  190. ret = PTR_ERR(mv64x60_wdt_dev);
  191. goto out;
  192. }
  193. ret = platform_driver_register(&mv64x60_wdt_driver);
  194. out:
  195. return ret;
  196. }
  197. static void __exit mv64x60_wdt_exit(void)
  198. {
  199. platform_driver_unregister(&mv64x60_wdt_driver);
  200. platform_device_unregister(mv64x60_wdt_dev);
  201. }
  202. module_init(mv64x60_wdt_init);
  203. module_exit(mv64x60_wdt_exit);
  204. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  205. MODULE_DESCRIPTION("MV64x60 watchdog driver");
  206. MODULE_LICENSE("GPL");
  207. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);