rc32434_wdt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h> /* For module specific items */
  21. #include <linux/moduleparam.h> /* For new moduleparam's */
  22. #include <linux/types.h> /* For standard types (like size_t) */
  23. #include <linux/errno.h> /* For the -ENODEV/... values */
  24. #include <linux/kernel.h> /* For printk/panic/... */
  25. #include <linux/fs.h> /* For file operations */
  26. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV
  27. (WATCHDOG_MINOR) */
  28. #include <linux/watchdog.h> /* For the watchdog specific items */
  29. #include <linux/init.h> /* For __init/__exit/... */
  30. #include <linux/platform_device.h> /* For platform_driver framework */
  31. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  32. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  33. #include <linux/io.h> /* For devm_ioremap_nocache */
  34. #include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */
  35. #define VERSION "1.0"
  36. static struct {
  37. unsigned long inuse;
  38. spinlock_t io_lock;
  39. } rc32434_wdt_device;
  40. static struct integ __iomem *wdt_reg;
  41. static int expect_close;
  42. /* Board internal clock speed in Hz,
  43. * the watchdog timer ticks at. */
  44. extern unsigned int idt_cpu_freq;
  45. /* translate wtcompare value to seconds and vice versa */
  46. #define WTCOMP2SEC(x) (x / idt_cpu_freq)
  47. #define SEC2WTCOMP(x) (x * idt_cpu_freq)
  48. /* Use a default timeout of 20s. This should be
  49. * safe for CPU clock speeds up to 400MHz, as
  50. * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */
  51. #define WATCHDOG_TIMEOUT 20
  52. static int timeout = WATCHDOG_TIMEOUT;
  53. module_param(timeout, int, 0);
  54. MODULE_PARM_DESC(timeout, "Watchdog timeout value, in seconds (default="
  55. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  56. static bool nowayout = WATCHDOG_NOWAYOUT;
  57. module_param(nowayout, bool, 0);
  58. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  59. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  60. /* apply or and nand masks to data read from addr and write back */
  61. #define SET_BITS(addr, or, nand) \
  62. writel((readl(&addr) | or) & ~nand, &addr)
  63. static int rc32434_wdt_set(int new_timeout)
  64. {
  65. int max_to = WTCOMP2SEC((u32)-1);
  66. if (new_timeout < 0 || new_timeout > max_to) {
  67. pr_err("timeout value must be between 0 and %d\n", 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. pr_info("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. pr_info("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. pr_crit("device closed unexpectedly. WDT will not stop!\n");
  129. rc32434_wdt_ping();
  130. }
  131. clear_bit(0, &rc32434_wdt_device.inuse);
  132. return 0;
  133. }
  134. static ssize_t rc32434_wdt_write(struct file *file, const char *data,
  135. size_t len, loff_t *ppos)
  136. {
  137. if (len) {
  138. if (!nowayout) {
  139. size_t i;
  140. /* In case it was set long ago */
  141. expect_close = 0;
  142. for (i = 0; i != len; i++) {
  143. char c;
  144. if (get_user(c, data + i))
  145. return -EFAULT;
  146. if (c == 'V')
  147. expect_close = 42;
  148. }
  149. }
  150. rc32434_wdt_ping();
  151. return len;
  152. }
  153. return 0;
  154. }
  155. static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
  156. unsigned long arg)
  157. {
  158. void __user *argp = (void __user *)arg;
  159. int new_timeout;
  160. unsigned int value;
  161. static const struct watchdog_info ident = {
  162. .options = WDIOF_SETTIMEOUT |
  163. WDIOF_KEEPALIVEPING |
  164. WDIOF_MAGICCLOSE,
  165. .identity = "RC32434_WDT Watchdog",
  166. };
  167. switch (cmd) {
  168. case WDIOC_GETSUPPORT:
  169. if (copy_to_user(argp, &ident, sizeof(ident)))
  170. return -EFAULT;
  171. break;
  172. case WDIOC_GETSTATUS:
  173. case WDIOC_GETBOOTSTATUS:
  174. value = 0;
  175. if (copy_to_user(argp, &value, sizeof(int)))
  176. return -EFAULT;
  177. break;
  178. case WDIOC_SETOPTIONS:
  179. if (copy_from_user(&value, argp, sizeof(int)))
  180. return -EFAULT;
  181. switch (value) {
  182. case WDIOS_ENABLECARD:
  183. rc32434_wdt_start();
  184. break;
  185. case WDIOS_DISABLECARD:
  186. rc32434_wdt_stop();
  187. break;
  188. default:
  189. return -EINVAL;
  190. }
  191. break;
  192. case WDIOC_KEEPALIVE:
  193. rc32434_wdt_ping();
  194. break;
  195. case WDIOC_SETTIMEOUT:
  196. if (copy_from_user(&new_timeout, argp, sizeof(int)))
  197. return -EFAULT;
  198. if (rc32434_wdt_set(new_timeout))
  199. return -EINVAL;
  200. /* Fall through */
  201. case WDIOC_GETTIMEOUT:
  202. return copy_to_user(argp, &timeout, sizeof(int));
  203. default:
  204. return -ENOTTY;
  205. }
  206. return 0;
  207. }
  208. static const struct file_operations rc32434_wdt_fops = {
  209. .owner = THIS_MODULE,
  210. .llseek = no_llseek,
  211. .write = rc32434_wdt_write,
  212. .unlocked_ioctl = rc32434_wdt_ioctl,
  213. .open = rc32434_wdt_open,
  214. .release = rc32434_wdt_release,
  215. };
  216. static struct miscdevice rc32434_wdt_miscdev = {
  217. .minor = WATCHDOG_MINOR,
  218. .name = "watchdog",
  219. .fops = &rc32434_wdt_fops,
  220. };
  221. static int rc32434_wdt_probe(struct platform_device *pdev)
  222. {
  223. int ret;
  224. struct resource *r;
  225. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
  226. if (!r) {
  227. pr_err("failed to retrieve resources\n");
  228. return -ENODEV;
  229. }
  230. wdt_reg = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
  231. if (!wdt_reg) {
  232. pr_err("failed to remap I/O resources\n");
  233. return -ENXIO;
  234. }
  235. spin_lock_init(&rc32434_wdt_device.io_lock);
  236. /* Make sure the watchdog is not running */
  237. rc32434_wdt_stop();
  238. /* Check that the heartbeat value is within it's range;
  239. * if not reset to the default */
  240. if (rc32434_wdt_set(timeout)) {
  241. rc32434_wdt_set(WATCHDOG_TIMEOUT);
  242. pr_info("timeout value must be between 0 and %d\n",
  243. WTCOMP2SEC((u32)-1));
  244. }
  245. ret = misc_register(&rc32434_wdt_miscdev);
  246. if (ret < 0) {
  247. pr_err("failed to register watchdog device\n");
  248. return ret;
  249. }
  250. pr_info("Watchdog Timer version " VERSION ", timer margin: %d sec\n",
  251. timeout);
  252. return 0;
  253. }
  254. static int rc32434_wdt_remove(struct platform_device *pdev)
  255. {
  256. misc_deregister(&rc32434_wdt_miscdev);
  257. return 0;
  258. }
  259. static void rc32434_wdt_shutdown(struct platform_device *pdev)
  260. {
  261. rc32434_wdt_stop();
  262. }
  263. static struct platform_driver rc32434_wdt_driver = {
  264. .probe = rc32434_wdt_probe,
  265. .remove = rc32434_wdt_remove,
  266. .shutdown = rc32434_wdt_shutdown,
  267. .driver = {
  268. .name = "rc32434_wdt",
  269. }
  270. };
  271. module_platform_driver(rc32434_wdt_driver);
  272. MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
  273. "Florian Fainelli <florian@openwrt.org>");
  274. MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
  275. MODULE_LICENSE("GPL");
  276. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);