rc32434_wdt.c 7.1 KB

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