txx9wdt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
  3. *
  4. * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/types.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/fs.h>
  17. #include <linux/init.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #include <linux/io.h>
  23. #include <asm/txx9tmr.h>
  24. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  25. static int timeout = TIMER_MARGIN; /* in seconds */
  26. module_param(timeout, int, 0);
  27. MODULE_PARM_DESC(timeout,
  28. "Watchdog timeout in seconds. "
  29. "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
  30. "default=" __MODULE_STRING(TIMER_MARGIN) ")");
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(nowayout, bool, 0);
  33. MODULE_PARM_DESC(nowayout,
  34. "Watchdog cannot be stopped once started "
  35. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  36. #define WD_TIMER_CCD 7 /* 1/256 */
  37. #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
  38. #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
  39. static unsigned long txx9wdt_alive;
  40. static int expect_close;
  41. static struct txx9_tmr_reg __iomem *txx9wdt_reg;
  42. static struct clk *txx9_imclk;
  43. static DEFINE_SPINLOCK(txx9_lock);
  44. static void txx9wdt_ping(void)
  45. {
  46. spin_lock(&txx9_lock);
  47. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  48. spin_unlock(&txx9_lock);
  49. }
  50. static void txx9wdt_start(void)
  51. {
  52. spin_lock(&txx9_lock);
  53. __raw_writel(WD_TIMER_CLK * timeout, &txx9wdt_reg->cpra);
  54. __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
  55. __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
  56. __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
  57. &txx9wdt_reg->tcr);
  58. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  59. spin_unlock(&txx9_lock);
  60. }
  61. static void txx9wdt_stop(void)
  62. {
  63. spin_lock(&txx9_lock);
  64. __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
  65. __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
  66. &txx9wdt_reg->tcr);
  67. spin_unlock(&txx9_lock);
  68. }
  69. static int txx9wdt_open(struct inode *inode, struct file *file)
  70. {
  71. if (test_and_set_bit(0, &txx9wdt_alive))
  72. return -EBUSY;
  73. if (__raw_readl(&txx9wdt_reg->tcr) & TXx9_TMTCR_TCE) {
  74. clear_bit(0, &txx9wdt_alive);
  75. return -EBUSY;
  76. }
  77. if (nowayout)
  78. __module_get(THIS_MODULE);
  79. txx9wdt_start();
  80. return nonseekable_open(inode, file);
  81. }
  82. static int txx9wdt_release(struct inode *inode, struct file *file)
  83. {
  84. if (expect_close)
  85. txx9wdt_stop();
  86. else {
  87. pr_crit("Unexpected close, not stopping watchdog!\n");
  88. txx9wdt_ping();
  89. }
  90. clear_bit(0, &txx9wdt_alive);
  91. expect_close = 0;
  92. return 0;
  93. }
  94. static ssize_t txx9wdt_write(struct file *file, const char __user *data,
  95. size_t len, loff_t *ppos)
  96. {
  97. if (len) {
  98. if (!nowayout) {
  99. size_t i;
  100. expect_close = 0;
  101. for (i = 0; i != len; i++) {
  102. char c;
  103. if (get_user(c, data + i))
  104. return -EFAULT;
  105. if (c == 'V')
  106. expect_close = 1;
  107. }
  108. }
  109. txx9wdt_ping();
  110. }
  111. return len;
  112. }
  113. static long txx9wdt_ioctl(struct file *file, unsigned int cmd,
  114. unsigned long arg)
  115. {
  116. void __user *argp = (void __user *)arg;
  117. int __user *p = argp;
  118. int new_timeout;
  119. static const struct watchdog_info ident = {
  120. .options = WDIOF_SETTIMEOUT |
  121. WDIOF_KEEPALIVEPING |
  122. WDIOF_MAGICCLOSE,
  123. .firmware_version = 0,
  124. .identity = "Hardware Watchdog for TXx9",
  125. };
  126. switch (cmd) {
  127. case WDIOC_GETSUPPORT:
  128. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  129. case WDIOC_GETSTATUS:
  130. case WDIOC_GETBOOTSTATUS:
  131. return put_user(0, p);
  132. case WDIOC_KEEPALIVE:
  133. txx9wdt_ping();
  134. return 0;
  135. case WDIOC_SETTIMEOUT:
  136. if (get_user(new_timeout, p))
  137. return -EFAULT;
  138. if (new_timeout < 1 || new_timeout > WD_MAX_TIMEOUT)
  139. return -EINVAL;
  140. timeout = new_timeout;
  141. txx9wdt_stop();
  142. txx9wdt_start();
  143. /* Fall */
  144. case WDIOC_GETTIMEOUT:
  145. return put_user(timeout, p);
  146. default:
  147. return -ENOTTY;
  148. }
  149. }
  150. static const struct file_operations txx9wdt_fops = {
  151. .owner = THIS_MODULE,
  152. .llseek = no_llseek,
  153. .write = txx9wdt_write,
  154. .unlocked_ioctl = txx9wdt_ioctl,
  155. .open = txx9wdt_open,
  156. .release = txx9wdt_release,
  157. };
  158. static struct miscdevice txx9wdt_miscdev = {
  159. .minor = WATCHDOG_MINOR,
  160. .name = "watchdog",
  161. .fops = &txx9wdt_fops,
  162. };
  163. static int __init txx9wdt_probe(struct platform_device *dev)
  164. {
  165. struct resource *res;
  166. int ret;
  167. txx9_imclk = clk_get(NULL, "imbus_clk");
  168. if (IS_ERR(txx9_imclk)) {
  169. ret = PTR_ERR(txx9_imclk);
  170. txx9_imclk = NULL;
  171. goto exit;
  172. }
  173. ret = clk_enable(txx9_imclk);
  174. if (ret) {
  175. clk_put(txx9_imclk);
  176. txx9_imclk = NULL;
  177. goto exit;
  178. }
  179. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  180. if (!res)
  181. goto exit_busy;
  182. if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res),
  183. "txx9wdt"))
  184. goto exit_busy;
  185. txx9wdt_reg = devm_ioremap(&dev->dev, res->start, resource_size(res));
  186. if (!txx9wdt_reg)
  187. goto exit_busy;
  188. ret = misc_register(&txx9wdt_miscdev);
  189. if (ret) {
  190. goto exit;
  191. }
  192. pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
  193. timeout, WD_MAX_TIMEOUT, nowayout);
  194. return 0;
  195. exit_busy:
  196. ret = -EBUSY;
  197. exit:
  198. if (txx9_imclk) {
  199. clk_disable(txx9_imclk);
  200. clk_put(txx9_imclk);
  201. }
  202. return ret;
  203. }
  204. static int __exit txx9wdt_remove(struct platform_device *dev)
  205. {
  206. misc_deregister(&txx9wdt_miscdev);
  207. clk_disable(txx9_imclk);
  208. clk_put(txx9_imclk);
  209. return 0;
  210. }
  211. static void txx9wdt_shutdown(struct platform_device *dev)
  212. {
  213. txx9wdt_stop();
  214. }
  215. static struct platform_driver txx9wdt_driver = {
  216. .remove = __exit_p(txx9wdt_remove),
  217. .shutdown = txx9wdt_shutdown,
  218. .driver = {
  219. .name = "txx9wdt",
  220. .owner = THIS_MODULE,
  221. },
  222. };
  223. static int __init watchdog_init(void)
  224. {
  225. return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe);
  226. }
  227. static void __exit watchdog_exit(void)
  228. {
  229. platform_driver_unregister(&txx9wdt_driver);
  230. }
  231. module_init(watchdog_init);
  232. module_exit(watchdog_exit);
  233. MODULE_DESCRIPTION("TXx9 Watchdog Driver");
  234. MODULE_LICENSE("GPL");
  235. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  236. MODULE_ALIAS("platform:txx9wdt");