rc32434_wdt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 struct miscdevice */
  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 <linux/io.h> /* For devm_ioremap_nocache */
  33. #include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */
  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 bool nowayout = WATCHDOG_NOWAYOUT;
  56. module_param(nowayout, bool, 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. pr_err("timeout value must be between 0 and %d\n", max_to);
  67. return -EINVAL;
  68. }
  69. timeout = new_timeout;
  70. spin_lock(&rc32434_wdt_device.io_lock);
  71. writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare);
  72. spin_unlock(&rc32434_wdt_device.io_lock);
  73. return 0;
  74. }
  75. static void rc32434_wdt_start(void)
  76. {
  77. u32 or, nand;
  78. spin_lock(&rc32434_wdt_device.io_lock);
  79. /* zero the counter before enabling */
  80. writel(0, &wdt_reg->wtcount);
  81. /* don't generate a non-maskable interrupt,
  82. * do a warm reset instead */
  83. nand = 1 << RC32434_ERR_WNE;
  84. or = 1 << RC32434_ERR_WRE;
  85. /* reset the ERRCS timeout bit in case it's set */
  86. nand |= 1 << RC32434_ERR_WTO;
  87. SET_BITS(wdt_reg->errcs, or, nand);
  88. /* set the timeout (either default or based on module param) */
  89. rc32434_wdt_set(timeout);
  90. /* reset WTC timeout bit and enable WDT */
  91. nand = 1 << RC32434_WTC_TO;
  92. or = 1 << RC32434_WTC_EN;
  93. SET_BITS(wdt_reg->wtc, or, nand);
  94. spin_unlock(&rc32434_wdt_device.io_lock);
  95. pr_info("Started watchdog timer\n");
  96. }
  97. static void rc32434_wdt_stop(void)
  98. {
  99. spin_lock(&rc32434_wdt_device.io_lock);
  100. /* Disable WDT */
  101. SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN);
  102. spin_unlock(&rc32434_wdt_device.io_lock);
  103. pr_info("Stopped watchdog timer\n");
  104. }
  105. static void rc32434_wdt_ping(void)
  106. {
  107. spin_lock(&rc32434_wdt_device.io_lock);
  108. writel(0, &wdt_reg->wtcount);
  109. spin_unlock(&rc32434_wdt_device.io_lock);
  110. }
  111. static int rc32434_wdt_open(struct inode *inode, struct file *file)
  112. {
  113. if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
  114. return -EBUSY;
  115. if (nowayout)
  116. __module_get(THIS_MODULE);
  117. rc32434_wdt_start();
  118. rc32434_wdt_ping();
  119. return nonseekable_open(inode, file);
  120. }
  121. static int rc32434_wdt_release(struct inode *inode, struct file *file)
  122. {
  123. if (expect_close == 42) {
  124. rc32434_wdt_stop();
  125. module_put(THIS_MODULE);
  126. } else {
  127. pr_crit("device closed unexpectedly. WDT will not stop!\n");
  128. rc32434_wdt_ping();
  129. }
  130. clear_bit(0, &rc32434_wdt_device.inuse);
  131. return 0;
  132. }
  133. static ssize_t rc32434_wdt_write(struct file *file, const char *data,
  134. size_t len, loff_t *ppos)
  135. {
  136. if (len) {
  137. if (!nowayout) {
  138. size_t i;
  139. /* In case it was set long ago */
  140. expect_close = 0;
  141. for (i = 0; i != len; i++) {
  142. char c;
  143. if (get_user(c, data + i))
  144. return -EFAULT;
  145. if (c == 'V')
  146. expect_close = 42;
  147. }
  148. }
  149. rc32434_wdt_ping();
  150. return len;
  151. }
  152. return 0;
  153. }
  154. static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
  155. unsigned long arg)
  156. {
  157. void __user *argp = (void __user *)arg;
  158. int new_timeout;
  159. unsigned int value;
  160. static const struct watchdog_info ident = {
  161. .options = WDIOF_SETTIMEOUT |
  162. WDIOF_KEEPALIVEPING |
  163. WDIOF_MAGICCLOSE,
  164. .identity = "RC32434_WDT Watchdog",
  165. };
  166. switch (cmd) {
  167. case WDIOC_GETSUPPORT:
  168. if (copy_to_user(argp, &ident, sizeof(ident)))
  169. return -EFAULT;
  170. break;
  171. case WDIOC_GETSTATUS:
  172. case WDIOC_GETBOOTSTATUS:
  173. value = 0;
  174. if (copy_to_user(argp, &value, sizeof(int)))
  175. return -EFAULT;
  176. break;
  177. case WDIOC_SETOPTIONS:
  178. if (copy_from_user(&value, argp, sizeof(int)))
  179. return -EFAULT;
  180. switch (value) {
  181. case WDIOS_ENABLECARD:
  182. rc32434_wdt_start();
  183. break;
  184. case WDIOS_DISABLECARD:
  185. rc32434_wdt_stop();
  186. break;
  187. default:
  188. return -EINVAL;
  189. }
  190. break;
  191. case WDIOC_KEEPALIVE:
  192. rc32434_wdt_ping();
  193. break;
  194. case WDIOC_SETTIMEOUT:
  195. if (copy_from_user(&new_timeout, argp, sizeof(int)))
  196. return -EFAULT;
  197. if (rc32434_wdt_set(new_timeout))
  198. return -EINVAL;
  199. /* Fall through */
  200. case WDIOC_GETTIMEOUT:
  201. return copy_to_user(argp, &timeout, sizeof(int)) ? -EFAULT : 0;
  202. default:
  203. return -ENOTTY;
  204. }
  205. return 0;
  206. }
  207. static const struct file_operations rc32434_wdt_fops = {
  208. .owner = THIS_MODULE,
  209. .llseek = no_llseek,
  210. .write = rc32434_wdt_write,
  211. .unlocked_ioctl = rc32434_wdt_ioctl,
  212. .open = rc32434_wdt_open,
  213. .release = rc32434_wdt_release,
  214. };
  215. static struct miscdevice rc32434_wdt_miscdev = {
  216. .minor = WATCHDOG_MINOR,
  217. .name = "watchdog",
  218. .fops = &rc32434_wdt_fops,
  219. };
  220. static int rc32434_wdt_probe(struct platform_device *pdev)
  221. {
  222. int ret;
  223. struct resource *r;
  224. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
  225. if (!r) {
  226. pr_err("failed to retrieve resources\n");
  227. return -ENODEV;
  228. }
  229. wdt_reg = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
  230. if (!wdt_reg) {
  231. pr_err("failed to remap I/O resources\n");
  232. return -ENXIO;
  233. }
  234. spin_lock_init(&rc32434_wdt_device.io_lock);
  235. /* Make sure the watchdog is not running */
  236. rc32434_wdt_stop();
  237. /* Check that the heartbeat value is within it's range;
  238. * if not reset to the default */
  239. if (rc32434_wdt_set(timeout)) {
  240. rc32434_wdt_set(WATCHDOG_TIMEOUT);
  241. pr_info("timeout value must be between 0 and %d\n",
  242. WTCOMP2SEC((u32)-1));
  243. }
  244. ret = misc_register(&rc32434_wdt_miscdev);
  245. if (ret < 0) {
  246. pr_err("failed to register watchdog device\n");
  247. return ret;
  248. }
  249. pr_info("Watchdog Timer version " VERSION ", timer margin: %d sec\n",
  250. timeout);
  251. return 0;
  252. }
  253. static int rc32434_wdt_remove(struct platform_device *pdev)
  254. {
  255. misc_deregister(&rc32434_wdt_miscdev);
  256. return 0;
  257. }
  258. static void rc32434_wdt_shutdown(struct platform_device *pdev)
  259. {
  260. rc32434_wdt_stop();
  261. }
  262. static struct platform_driver rc32434_wdt_driver = {
  263. .probe = rc32434_wdt_probe,
  264. .remove = rc32434_wdt_remove,
  265. .shutdown = rc32434_wdt_shutdown,
  266. .driver = {
  267. .name = "rc32434_wdt",
  268. }
  269. };
  270. module_platform_driver(rc32434_wdt_driver);
  271. MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
  272. "Florian Fainelli <florian@openwrt.org>");
  273. MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
  274. MODULE_LICENSE("GPL");