rc32434_wdt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * IDT Interprise 79RC32434 watchdog driver
  3. *
  4. * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org>
  5. * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
  6. *
  7. * based on
  8. * SoftDog 0.05: A Software Watchdog Device
  9. *
  10. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  11. * All Rights Reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. *
  18. */
  19. #include <linux/module.h> /* For module specific items */
  20. #include <linux/moduleparam.h> /* For new moduleparam's */
  21. #include <linux/types.h> /* For standard types (like size_t) */
  22. #include <linux/errno.h> /* For the -ENODEV/... values */
  23. #include <linux/kernel.h> /* For printk/panic/... */
  24. #include <linux/fs.h> /* For file operations */
  25. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV
  26. (WATCHDOG_MINOR) */
  27. #include <linux/watchdog.h> /* For the watchdog specific items */
  28. #include <linux/init.h> /* For __init/__exit/... */
  29. #include <linux/platform_device.h> /* For platform_driver framework */
  30. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  31. #include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */
  32. #define PFX KBUILD_MODNAME ": "
  33. #define VERSION "0.5"
  34. static struct {
  35. unsigned long inuse;
  36. } rc32434_wdt_device;
  37. static struct integ __iomem *wdt_reg;
  38. static int expect_close;
  39. /* Board internal clock speed in Hz,
  40. * the watchdog timer ticks at. */
  41. extern unsigned int idt_cpu_freq;
  42. /* translate wtcompare value to seconds and vice versa */
  43. #define WTCOMP2SEC(x) (x / idt_cpu_freq)
  44. #define SEC2WTCOMP(x) (x * idt_cpu_freq)
  45. /* Use a default timeout of 20s. This should be
  46. * safe for CPU clock speeds up to 400MHz, as
  47. * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */
  48. #define WATCHDOG_TIMEOUT 20
  49. static int timeout = WATCHDOG_TIMEOUT;
  50. module_param(timeout, int, 0);
  51. MODULE_PARM_DESC(timeout, "Watchdog timeout value, in seconds (default="
  52. WATCHDOG_TIMEOUT ")");
  53. static int nowayout = WATCHDOG_NOWAYOUT;
  54. module_param(nowayout, int, 0);
  55. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  56. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  57. /* apply or and nand masks to data read from addr and write back */
  58. #define SET_BITS(addr, or, nand) \
  59. writel((readl(&addr) | or) & ~nand, &addr)
  60. static int rc32434_wdt_set(int new_timeout)
  61. {
  62. int max_to = WTCOMP2SEC((u32)-1);
  63. if (new_timeout < 0 || new_timeout > max_to) {
  64. printk(KERN_ERR PFX "timeout value must be between 0 and %d",
  65. max_to);
  66. return -EINVAL;
  67. }
  68. timeout = new_timeout;
  69. writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare);
  70. return 0;
  71. }
  72. static void rc32434_wdt_start(void)
  73. {
  74. u32 or, nand;
  75. /* zero the counter before enabling */
  76. writel(0, &wdt_reg->wtcount);
  77. /* don't generate a non-maskable interrupt,
  78. * do a warm reset instead */
  79. nand = 1 << RC32434_ERR_WNE;
  80. or = 1 << RC32434_ERR_WRE;
  81. /* reset the ERRCS timeout bit in case it's set */
  82. nand |= 1 << RC32434_ERR_WTO;
  83. SET_BITS(wdt_reg->errcs, or, nand);
  84. /* set the timeout (either default or based on module param) */
  85. rc32434_wdt_set(timeout);
  86. /* reset WTC timeout bit and enable WDT */
  87. nand = 1 << RC32434_WTC_TO;
  88. or = 1 << RC32434_WTC_EN;
  89. SET_BITS(wdt_reg->wtc, or, nand);
  90. printk(KERN_INFO PFX "Started watchdog timer.\n");
  91. }
  92. static void rc32434_wdt_stop(void)
  93. {
  94. /* Disable WDT */
  95. SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN);
  96. printk(KERN_INFO PFX "Stopped watchdog timer.\n");
  97. }
  98. static void rc32434_wdt_ping(void)
  99. {
  100. writel(0, &wdt_reg->wtcount);
  101. }
  102. static int rc32434_wdt_open(struct inode *inode, struct file *file)
  103. {
  104. if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
  105. return -EBUSY;
  106. if (nowayout)
  107. __module_get(THIS_MODULE);
  108. rc32434_wdt_start();
  109. rc32434_wdt_ping();
  110. return nonseekable_open(inode, file);
  111. }
  112. static int rc32434_wdt_release(struct inode *inode, struct file *file)
  113. {
  114. if (expect_close == 42) {
  115. rc32434_wdt_stop();
  116. module_put(THIS_MODULE);
  117. } else {
  118. printk(KERN_CRIT PFX
  119. "device closed unexpectedly. WDT will not stop!\n");
  120. rc32434_wdt_ping();
  121. }
  122. clear_bit(0, &rc32434_wdt_device.inuse);
  123. return 0;
  124. }
  125. static ssize_t rc32434_wdt_write(struct file *file, const char *data,
  126. size_t len, loff_t *ppos)
  127. {
  128. if (len) {
  129. if (!nowayout) {
  130. size_t i;
  131. /* In case it was set long ago */
  132. expect_close = 0;
  133. for (i = 0; i != len; i++) {
  134. char c;
  135. if (get_user(c, data + i))
  136. return -EFAULT;
  137. if (c == 'V')
  138. expect_close = 42;
  139. }
  140. }
  141. rc32434_wdt_ping();
  142. return len;
  143. }
  144. return 0;
  145. }
  146. static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
  147. unsigned long arg)
  148. {
  149. void __user *argp = (void __user *)arg;
  150. int new_timeout;
  151. unsigned int value;
  152. static struct watchdog_info ident = {
  153. .options = WDIOF_SETTIMEOUT |
  154. WDIOF_KEEPALIVEPING |
  155. WDIOF_MAGICCLOSE,
  156. .identity = "RC32434_WDT Watchdog",
  157. };
  158. switch (cmd) {
  159. case WDIOC_GETSUPPORT:
  160. if (copy_to_user(argp, &ident, sizeof(ident)))
  161. return -EFAULT;
  162. break;
  163. case WDIOC_GETSTATUS:
  164. case WDIOC_GETBOOTSTATUS:
  165. value = 0;
  166. if (copy_to_user(argp, &value, sizeof(int)))
  167. return -EFAULT;
  168. break;
  169. case WDIOC_SETOPTIONS:
  170. if (copy_from_user(&value, argp, sizeof(int)))
  171. return -EFAULT;
  172. switch (value) {
  173. case WDIOS_ENABLECARD:
  174. rc32434_wdt_start();
  175. break;
  176. case WDIOS_DISABLECARD:
  177. rc32434_wdt_stop();
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. break;
  183. case WDIOC_KEEPALIVE:
  184. rc32434_wdt_ping();
  185. break;
  186. case WDIOC_SETTIMEOUT:
  187. if (copy_from_user(&new_timeout, argp, sizeof(int)))
  188. return -EFAULT;
  189. if (rc32434_wdt_set(new_timeout))
  190. return -EINVAL;
  191. /* Fall through */
  192. case WDIOC_GETTIMEOUT:
  193. return copy_to_user(argp, &timeout, sizeof(int));
  194. default:
  195. return -ENOTTY;
  196. }
  197. return 0;
  198. }
  199. static struct file_operations rc32434_wdt_fops = {
  200. .owner = THIS_MODULE,
  201. .llseek = no_llseek,
  202. .write = rc32434_wdt_write,
  203. .unlocked_ioctl = rc32434_wdt_ioctl,
  204. .open = rc32434_wdt_open,
  205. .release = rc32434_wdt_release,
  206. };
  207. static struct miscdevice rc32434_wdt_miscdev = {
  208. .minor = WATCHDOG_MINOR,
  209. .name = "watchdog",
  210. .fops = &rc32434_wdt_fops,
  211. };
  212. static char banner[] __devinitdata = KERN_INFO PFX
  213. "Watchdog Timer version " VERSION ", timer margin: %d sec\n";
  214. static int __devinit rc32434_wdt_probe(struct platform_device *pdev)
  215. {
  216. int ret;
  217. struct resource *r;
  218. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
  219. if (!r) {
  220. printk(KERN_ERR PFX "failed to retrieve resources\n");
  221. return -ENODEV;
  222. }
  223. wdt_reg = ioremap_nocache(r->start, r->end - r->start);
  224. if (!wdt_reg) {
  225. printk(KERN_ERR PFX "failed to remap I/O resources\n");
  226. return -ENXIO;
  227. }
  228. /* Check that the heartbeat value is within it's range;
  229. * if not reset to the default */
  230. if (rc32434_wdt_set(timeout)) {
  231. rc32434_wdt_set(WATCHDOG_TIMEOUT);
  232. printk(KERN_INFO PFX
  233. "timeout value must be between 0 and %d\n",
  234. WTCOMP2SEC((u32)-1));
  235. }
  236. ret = misc_register(&rc32434_wdt_miscdev);
  237. if (ret < 0) {
  238. printk(KERN_ERR PFX "failed to register watchdog device\n");
  239. goto unmap;
  240. }
  241. printk(banner, timeout);
  242. return 0;
  243. unmap:
  244. iounmap(wdt_reg);
  245. return ret;
  246. }
  247. static int __devexit rc32434_wdt_remove(struct platform_device *pdev)
  248. {
  249. misc_deregister(&rc32434_wdt_miscdev);
  250. iounmap(wdt_reg);
  251. return 0;
  252. }
  253. static void rc32434_wdt_shutdown(struct platform_device *pdev)
  254. {
  255. rc32434_wdt_stop();
  256. }
  257. static struct platform_driver rc32434_wdt_driver = {
  258. .probe = rc32434_wdt_probe,
  259. .remove = __devexit_p(rc32434_wdt_remove),
  260. .shutdown = rc32434_wdt_shutdown,
  261. .driver = {
  262. .name = "rc32434_wdt",
  263. }
  264. };
  265. static int __init rc32434_wdt_init(void)
  266. {
  267. return platform_driver_register(&rc32434_wdt_driver);
  268. }
  269. static void __exit rc32434_wdt_exit(void)
  270. {
  271. platform_driver_unregister(&rc32434_wdt_driver);
  272. }
  273. module_init(rc32434_wdt_init);
  274. module_exit(rc32434_wdt_exit);
  275. MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
  276. "Florian Fainelli <florian@openwrt.org>");
  277. MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
  278. MODULE_LICENSE("GPL");
  279. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);