rc32434_wdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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@redhat.com>, All Rights Reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/fs.h>
  22. #include <linux/mm.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/reboot.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/init.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/bootinfo.h>
  31. #include <asm/time.h>
  32. #include <asm/mach-rc32434/integ.h>
  33. #define MAX_TIMEOUT 20
  34. #define RC32434_WDT_INTERVAL (15 * HZ)
  35. #define VERSION "0.2"
  36. static struct {
  37. struct completion stop;
  38. int running;
  39. struct timer_list timer;
  40. int queue;
  41. int default_ticks;
  42. unsigned long inuse;
  43. } rc32434_wdt_device;
  44. static struct integ __iomem *wdt_reg;
  45. static int ticks = 100 * HZ;
  46. static int expect_close;
  47. static int timeout;
  48. static int nowayout = WATCHDOG_NOWAYOUT;
  49. module_param(nowayout, int, 0);
  50. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  51. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  52. static void rc32434_wdt_start(void)
  53. {
  54. u32 val;
  55. if (!rc32434_wdt_device.inuse) {
  56. writel(0, &wdt_reg->wtcount);
  57. val = RC32434_ERR_WRE;
  58. writel(readl(&wdt_reg->errcs) | val, &wdt_reg->errcs);
  59. val = RC32434_WTC_EN;
  60. writel(readl(&wdt_reg->wtc) | val, &wdt_reg->wtc);
  61. }
  62. rc32434_wdt_device.running++;
  63. }
  64. static void rc32434_wdt_stop(void)
  65. {
  66. u32 val;
  67. if (rc32434_wdt_device.running) {
  68. val = ~RC32434_WTC_EN;
  69. writel(readl(&wdt_reg->wtc) & val, &wdt_reg->wtc);
  70. val = ~RC32434_ERR_WRE;
  71. writel(readl(&wdt_reg->errcs) & val, &wdt_reg->errcs);
  72. rc32434_wdt_device.running = 0;
  73. }
  74. }
  75. static void rc32434_wdt_set(int new_timeout)
  76. {
  77. u32 cmp = new_timeout * HZ;
  78. u32 state, val;
  79. timeout = new_timeout;
  80. /*
  81. * store and disable WTC
  82. */
  83. state = (u32)(readl(&wdt_reg->wtc) & RC32434_WTC_EN);
  84. val = ~RC32434_WTC_EN;
  85. writel(readl(&wdt_reg->wtc) & val, &wdt_reg->wtc);
  86. writel(0, &wdt_reg->wtcount);
  87. writel(cmp, &wdt_reg->wtcompare);
  88. /*
  89. * restore WTC
  90. */
  91. writel(readl(&wdt_reg->wtc) | state, &wdt_reg);
  92. }
  93. static void rc32434_wdt_reset(void)
  94. {
  95. ticks = rc32434_wdt_device.default_ticks;
  96. }
  97. static void rc32434_wdt_update(unsigned long unused)
  98. {
  99. if (rc32434_wdt_device.running)
  100. ticks--;
  101. writel(0, &wdt_reg->wtcount);
  102. if (rc32434_wdt_device.queue && ticks)
  103. mod_timer(&rc32434_wdt_device.timer,
  104. jiffies + RC32434_WDT_INTERVAL);
  105. else
  106. complete(&rc32434_wdt_device.stop);
  107. }
  108. static int rc32434_wdt_open(struct inode *inode, struct file *file)
  109. {
  110. if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
  111. return -EBUSY;
  112. if (nowayout)
  113. __module_get(THIS_MODULE);
  114. return nonseekable_open(inode, file);
  115. }
  116. static int rc32434_wdt_release(struct inode *inode, struct file *file)
  117. {
  118. if (expect_close && nowayout == 0) {
  119. rc32434_wdt_stop();
  120. printk(KERN_INFO KBUILD_MODNAME ": disabling watchdog timer\n");
  121. module_put(THIS_MODULE);
  122. } else
  123. printk(KERN_CRIT KBUILD_MODNAME
  124. ": device closed unexpectedly. WDT will not stop !\n");
  125. clear_bit(0, &rc32434_wdt_device.inuse);
  126. return 0;
  127. }
  128. static ssize_t rc32434_wdt_write(struct file *file, const char *data,
  129. size_t len, loff_t *ppos)
  130. {
  131. if (len) {
  132. if (!nowayout) {
  133. size_t i;
  134. /* In case it was set long ago */
  135. expect_close = 0;
  136. for (i = 0; i != len; i++) {
  137. char c;
  138. if (get_user(c, data + i))
  139. return -EFAULT;
  140. if (c == 'V')
  141. expect_close = 1;
  142. }
  143. }
  144. rc32434_wdt_update(0);
  145. return len;
  146. }
  147. return 0;
  148. }
  149. static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
  150. unsigned long arg)
  151. {
  152. void __user *argp = (void __user *)arg;
  153. int new_timeout;
  154. unsigned int value;
  155. static struct watchdog_info ident = {
  156. .options = WDIOF_SETTIMEOUT |
  157. WDIOF_KEEPALIVEPING |
  158. WDIOF_MAGICCLOSE,
  159. .identity = "RC32434_WDT Watchdog",
  160. };
  161. switch (cmd) {
  162. case WDIOC_KEEPALIVE:
  163. rc32434_wdt_reset();
  164. break;
  165. case WDIOC_GETSTATUS:
  166. case WDIOC_GETBOOTSTATUS:
  167. value = readl(&wdt_reg->wtcount);
  168. if (copy_to_user(argp, &value, sizeof(int)))
  169. return -EFAULT;
  170. break;
  171. case WDIOC_GETSUPPORT:
  172. if (copy_to_user(argp, &ident, sizeof(ident)))
  173. return -EFAULT;
  174. break;
  175. case WDIOC_SETOPTIONS:
  176. if (copy_from_user(&value, argp, sizeof(int)))
  177. return -EFAULT;
  178. switch (value) {
  179. case WDIOS_ENABLECARD:
  180. rc32434_wdt_start();
  181. break;
  182. case WDIOS_DISABLECARD:
  183. rc32434_wdt_stop();
  184. default:
  185. return -EINVAL;
  186. }
  187. break;
  188. case WDIOC_SETTIMEOUT:
  189. if (copy_from_user(&new_timeout, argp, sizeof(int)))
  190. return -EFAULT;
  191. if (new_timeout < 1)
  192. return -EINVAL;
  193. if (new_timeout > MAX_TIMEOUT)
  194. return -EINVAL;
  195. rc32434_wdt_set(new_timeout);
  196. case WDIOC_GETTIMEOUT:
  197. return copy_to_user(argp, &timeout, sizeof(int));
  198. default:
  199. return -ENOTTY;
  200. }
  201. return 0;
  202. }
  203. static struct file_operations rc32434_wdt_fops = {
  204. .owner = THIS_MODULE,
  205. .llseek = no_llseek,
  206. .write = rc32434_wdt_write,
  207. .unlocked_ioctl = rc32434_wdt_ioctl,
  208. .open = rc32434_wdt_open,
  209. .release = rc32434_wdt_release,
  210. };
  211. static struct miscdevice rc32434_wdt_miscdev = {
  212. .minor = WATCHDOG_MINOR,
  213. .name = "watchdog",
  214. .fops = &rc32434_wdt_fops,
  215. };
  216. static char banner[] = KERN_INFO KBUILD_MODNAME
  217. ": Watchdog Timer version " VERSION ", timer margin: %d sec\n";
  218. static int rc32434_wdt_probe(struct platform_device *pdev)
  219. {
  220. int ret;
  221. struct resource *r;
  222. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb500_wdt_res");
  223. if (!r) {
  224. printk(KERN_ERR KBUILD_MODNAME
  225. "failed to retrieve resources\n");
  226. return -ENODEV;
  227. }
  228. wdt_reg = ioremap_nocache(r->start, r->end - r->start);
  229. if (!wdt_reg) {
  230. printk(KERN_ERR KBUILD_MODNAME
  231. "failed to remap I/O resources\n");
  232. return -ENXIO;
  233. }
  234. ret = misc_register(&rc32434_wdt_miscdev);
  235. if (ret < 0) {
  236. printk(KERN_ERR KBUILD_MODNAME
  237. "failed to register watchdog device\n");
  238. goto unmap;
  239. }
  240. init_completion(&rc32434_wdt_device.stop);
  241. rc32434_wdt_device.queue = 0;
  242. clear_bit(0, &rc32434_wdt_device.inuse);
  243. setup_timer(&rc32434_wdt_device.timer, rc32434_wdt_update, 0L);
  244. rc32434_wdt_device.default_ticks = ticks;
  245. rc32434_wdt_start();
  246. printk(banner, timeout);
  247. return 0;
  248. unmap:
  249. iounmap(wdt_reg);
  250. return ret;
  251. }
  252. static int rc32434_wdt_remove(struct platform_device *pdev)
  253. {
  254. if (rc32434_wdt_device.queue) {
  255. rc32434_wdt_device.queue = 0;
  256. wait_for_completion(&rc32434_wdt_device.stop);
  257. }
  258. misc_deregister(&rc32434_wdt_miscdev);
  259. iounmap(wdt_reg);
  260. return 0;
  261. }
  262. static struct platform_driver rc32434_wdt = {
  263. .probe = rc32434_wdt_probe,
  264. .remove = rc32434_wdt_remove,
  265. .driver = {
  266. .name = "rc32434_wdt",
  267. }
  268. };
  269. static int __init rc32434_wdt_init(void)
  270. {
  271. return platform_driver_register(&rc32434_wdt);
  272. }
  273. static void __exit rc32434_wdt_exit(void)
  274. {
  275. platform_driver_unregister(&rc32434_wdt);
  276. }
  277. module_init(rc32434_wdt_init);
  278. module_exit(rc32434_wdt_exit);
  279. MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
  280. "Florian Fainelli <florian@openwrt.org>");
  281. MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
  282. MODULE_LICENSE("GPL");
  283. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);