mv64x60_wdt.c 6.0 KB

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