rc32434_wdt.c 8.6 KB

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