rc32434_wdt.c 7.4 KB

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