mv64x60_wdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <linux/mv643xx.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/io.h>
  27. #define MV64x60_WDT_WDC_OFFSET 0
  28. /*
  29. * The watchdog configuration register contains a pair of 2-bit fields,
  30. * 1. a reload field, bits 27-26, which triggers a reload of
  31. * the countdown register, and
  32. * 2. an enable field, bits 25-24, which toggles between
  33. * enabling and disabling the watchdog timer.
  34. * Bit 31 is a read-only field which indicates whether the
  35. * watchdog timer is currently enabled.
  36. *
  37. * The low 24 bits contain the timer reload value.
  38. */
  39. #define MV64x60_WDC_ENABLE_SHIFT 24
  40. #define MV64x60_WDC_SERVICE_SHIFT 26
  41. #define MV64x60_WDC_ENABLED_SHIFT 31
  42. #define MV64x60_WDC_ENABLED_TRUE 1
  43. #define MV64x60_WDC_ENABLED_FALSE 0
  44. /* Flags bits */
  45. #define MV64x60_WDOG_FLAG_OPENED 0
  46. static unsigned long wdt_flags;
  47. static int wdt_status;
  48. static void __iomem *mv64x60_wdt_regs;
  49. static int mv64x60_wdt_timeout;
  50. static int mv64x60_wdt_count;
  51. static unsigned int bus_clk;
  52. static char expect_close;
  53. static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
  54. static int nowayout = WATCHDOG_NOWAYOUT;
  55. module_param(nowayout, int, 0);
  56. MODULE_PARM_DESC(nowayout,
  57. "Watchdog cannot be stopped once started (default="
  58. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  59. static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
  60. {
  61. u32 data;
  62. u32 enabled;
  63. int ret = 0;
  64. spin_lock(&mv64x60_wdt_spinlock);
  65. data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  66. enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
  67. /* only toggle the requested field if enabled state matches predicate */
  68. if ((enabled ^ enabled_predicate) == 0) {
  69. /* We write a 1, then a 2 -- to the appropriate field */
  70. data = (1 << field_shift) | mv64x60_wdt_count;
  71. writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  72. data = (2 << field_shift) | mv64x60_wdt_count;
  73. writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  74. ret = 1;
  75. }
  76. spin_unlock(&mv64x60_wdt_spinlock);
  77. return ret;
  78. }
  79. static void mv64x60_wdt_service(void)
  80. {
  81. mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
  82. MV64x60_WDC_SERVICE_SHIFT);
  83. }
  84. static void mv64x60_wdt_handler_enable(void)
  85. {
  86. if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
  87. MV64x60_WDC_ENABLE_SHIFT)) {
  88. mv64x60_wdt_service();
  89. printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
  90. }
  91. }
  92. static void mv64x60_wdt_handler_disable(void)
  93. {
  94. if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
  95. MV64x60_WDC_ENABLE_SHIFT))
  96. printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
  97. }
  98. static void mv64x60_wdt_set_timeout(unsigned int timeout)
  99. {
  100. /* maximum bus cycle count is 0xFFFFFFFF */
  101. if (timeout > 0xFFFFFFFF / bus_clk)
  102. timeout = 0xFFFFFFFF / bus_clk;
  103. mv64x60_wdt_count = timeout * bus_clk >> 8;
  104. mv64x60_wdt_timeout = timeout;
  105. }
  106. static int mv64x60_wdt_open(struct inode *inode, struct file *file)
  107. {
  108. if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
  109. return -EBUSY;
  110. if (nowayout)
  111. __module_get(THIS_MODULE);
  112. mv64x60_wdt_handler_enable();
  113. return nonseekable_open(inode, file);
  114. }
  115. static int mv64x60_wdt_release(struct inode *inode, struct file *file)
  116. {
  117. if (expect_close == 42)
  118. mv64x60_wdt_handler_disable();
  119. else {
  120. printk(KERN_CRIT
  121. "mv64x60_wdt: unexpected close, not stopping timer!\n");
  122. mv64x60_wdt_service();
  123. }
  124. expect_close = 0;
  125. clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
  126. return 0;
  127. }
  128. static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
  129. size_t len, loff_t *ppos)
  130. {
  131. if (len) {
  132. if (!nowayout) {
  133. size_t i;
  134. expect_close = 0;
  135. for (i = 0; i != len; i++) {
  136. char c;
  137. if (get_user(c, data + i))
  138. return -EFAULT;
  139. if (c == 'V')
  140. expect_close = 42;
  141. }
  142. }
  143. mv64x60_wdt_service();
  144. }
  145. return len;
  146. }
  147. static long mv64x60_wdt_ioctl(struct file *file,
  148. unsigned int cmd, unsigned long arg)
  149. {
  150. int timeout;
  151. int options;
  152. void __user *argp = (void __user *)arg;
  153. static const struct watchdog_info info = {
  154. .options = WDIOF_SETTIMEOUT |
  155. WDIOF_MAGICCLOSE |
  156. WDIOF_KEEPALIVEPING,
  157. .firmware_version = 0,
  158. .identity = "MV64x60 watchdog",
  159. };
  160. switch (cmd) {
  161. case WDIOC_GETSUPPORT:
  162. if (copy_to_user(argp, &info, sizeof(info)))
  163. return -EFAULT;
  164. break;
  165. case WDIOC_GETSTATUS:
  166. case WDIOC_GETBOOTSTATUS:
  167. if (put_user(wdt_status, (int __user *)argp))
  168. return -EFAULT;
  169. wdt_status &= ~WDIOF_KEEPALIVEPING;
  170. break;
  171. case WDIOC_GETTEMP:
  172. return -EOPNOTSUPP;
  173. case WDIOC_SETOPTIONS:
  174. if (get_user(options, (int __user *)argp))
  175. return -EFAULT;
  176. if (options & WDIOS_DISABLECARD)
  177. mv64x60_wdt_handler_disable();
  178. if (options & WDIOS_ENABLECARD)
  179. mv64x60_wdt_handler_enable();
  180. break;
  181. case WDIOC_KEEPALIVE:
  182. mv64x60_wdt_service();
  183. wdt_status |= WDIOF_KEEPALIVEPING;
  184. break;
  185. case WDIOC_SETTIMEOUT:
  186. if (get_user(timeout, (int __user *)argp))
  187. return -EFAULT;
  188. mv64x60_wdt_set_timeout(timeout);
  189. /* Fall through */
  190. case WDIOC_GETTIMEOUT:
  191. if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
  192. return -EFAULT;
  193. break;
  194. default:
  195. return -ENOTTY;
  196. }
  197. return 0;
  198. }
  199. static const struct file_operations mv64x60_wdt_fops = {
  200. .owner = THIS_MODULE,
  201. .llseek = no_llseek,
  202. .write = mv64x60_wdt_write,
  203. .unlocked_ioctl = mv64x60_wdt_ioctl,
  204. .open = mv64x60_wdt_open,
  205. .release = mv64x60_wdt_release,
  206. };
  207. static struct miscdevice mv64x60_wdt_miscdev = {
  208. .minor = WATCHDOG_MINOR,
  209. .name = "watchdog",
  210. .fops = &mv64x60_wdt_fops,
  211. };
  212. static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
  213. {
  214. struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
  215. struct resource *r;
  216. int timeout = 10;
  217. bus_clk = 133; /* in MHz */
  218. if (pdata) {
  219. timeout = pdata->timeout;
  220. bus_clk = pdata->bus_clk;
  221. }
  222. /* Since bus_clk is truncated MHz, actual frequency could be
  223. * up to 1MHz higher. Round up, since it's better to time out
  224. * too late than too soon.
  225. */
  226. bus_clk++;
  227. bus_clk *= 1000000; /* convert to Hz */
  228. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  229. if (!r)
  230. return -ENODEV;
  231. mv64x60_wdt_regs = ioremap(r->start, resource_size(r));
  232. if (mv64x60_wdt_regs == NULL)
  233. return -ENOMEM;
  234. mv64x60_wdt_set_timeout(timeout);
  235. mv64x60_wdt_handler_disable(); /* in case timer was already running */
  236. return misc_register(&mv64x60_wdt_miscdev);
  237. }
  238. static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
  239. {
  240. misc_deregister(&mv64x60_wdt_miscdev);
  241. mv64x60_wdt_handler_disable();
  242. iounmap(mv64x60_wdt_regs);
  243. return 0;
  244. }
  245. static struct platform_driver mv64x60_wdt_driver = {
  246. .probe = mv64x60_wdt_probe,
  247. .remove = __devexit_p(mv64x60_wdt_remove),
  248. .driver = {
  249. .owner = THIS_MODULE,
  250. .name = MV64x60_WDT_NAME,
  251. },
  252. };
  253. static int __init mv64x60_wdt_init(void)
  254. {
  255. printk(KERN_INFO "MV64x60 watchdog driver\n");
  256. return platform_driver_register(&mv64x60_wdt_driver);
  257. }
  258. static void __exit mv64x60_wdt_exit(void)
  259. {
  260. platform_driver_unregister(&mv64x60_wdt_driver);
  261. }
  262. module_init(mv64x60_wdt_init);
  263. module_exit(mv64x60_wdt_exit);
  264. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  265. MODULE_DESCRIPTION("MV64x60 watchdog driver");
  266. MODULE_LICENSE("GPL");
  267. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  268. MODULE_ALIAS("platform:" MV64x60_WDT_NAME);