txx9wdt.c 6.3 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. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/types.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/watchdog.h>
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <asm/txx9tmr.h>
  23. #define TIMER_MARGIN 60 /* Default is 60 seconds */
  24. static int timeout = TIMER_MARGIN; /* in seconds */
  25. module_param(timeout, int, 0);
  26. MODULE_PARM_DESC(timeout,
  27. "Watchdog timeout in seconds. "
  28. "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
  29. "default=" __MODULE_STRING(TIMER_MARGIN) ")");
  30. static int nowayout = WATCHDOG_NOWAYOUT;
  31. module_param(nowayout, int, 0);
  32. MODULE_PARM_DESC(nowayout,
  33. "Watchdog cannot be stopped once started "
  34. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  35. #define WD_TIMER_CCD 7 /* 1/256 */
  36. #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
  37. #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
  38. static unsigned long txx9wdt_alive;
  39. static int expect_close;
  40. static struct txx9_tmr_reg __iomem *txx9wdt_reg;
  41. static struct clk *txx9_imclk;
  42. static DEFINE_SPINLOCK(txx9_lock);
  43. static void txx9wdt_ping(void)
  44. {
  45. spin_lock(&txx9_lock);
  46. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  47. spin_unlock(&txx9_lock);
  48. }
  49. static void txx9wdt_start(void)
  50. {
  51. spin_lock(&txx9_lock);
  52. __raw_writel(WD_TIMER_CLK * timeout, &txx9wdt_reg->cpra);
  53. __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
  54. __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
  55. __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
  56. &txx9wdt_reg->tcr);
  57. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  58. spin_unlock(&txx9_lock);
  59. }
  60. static void txx9wdt_stop(void)
  61. {
  62. spin_lock(&txx9_lock);
  63. __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
  64. __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
  65. &txx9wdt_reg->tcr);
  66. spin_unlock(&txx9_lock);
  67. }
  68. static int txx9wdt_open(struct inode *inode, struct file *file)
  69. {
  70. if (test_and_set_bit(0, &txx9wdt_alive))
  71. return -EBUSY;
  72. if (__raw_readl(&txx9wdt_reg->tcr) & TXx9_TMTCR_TCE) {
  73. clear_bit(0, &txx9wdt_alive);
  74. return -EBUSY;
  75. }
  76. if (nowayout)
  77. __module_get(THIS_MODULE);
  78. txx9wdt_start();
  79. return nonseekable_open(inode, file);
  80. }
  81. static int txx9wdt_release(struct inode *inode, struct file *file)
  82. {
  83. if (expect_close)
  84. txx9wdt_stop();
  85. else {
  86. printk(KERN_CRIT "txx9wdt: "
  87. "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. printk(KERN_INFO "Hardware Watchdog Timer for TXx9: "
  193. "timeout=%d sec (max %ld) (nowayout= %d)\n",
  194. timeout, WD_MAX_TIMEOUT, nowayout);
  195. return 0;
  196. exit_busy:
  197. ret = -EBUSY;
  198. exit:
  199. if (txx9_imclk) {
  200. clk_disable(txx9_imclk);
  201. clk_put(txx9_imclk);
  202. }
  203. return ret;
  204. }
  205. static int __exit txx9wdt_remove(struct platform_device *dev)
  206. {
  207. misc_deregister(&txx9wdt_miscdev);
  208. clk_disable(txx9_imclk);
  209. clk_put(txx9_imclk);
  210. return 0;
  211. }
  212. static void txx9wdt_shutdown(struct platform_device *dev)
  213. {
  214. txx9wdt_stop();
  215. }
  216. static struct platform_driver txx9wdt_driver = {
  217. .remove = __exit_p(txx9wdt_remove),
  218. .shutdown = txx9wdt_shutdown,
  219. .driver = {
  220. .name = "txx9wdt",
  221. .owner = THIS_MODULE,
  222. },
  223. };
  224. static int __init watchdog_init(void)
  225. {
  226. return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe);
  227. }
  228. static void __exit watchdog_exit(void)
  229. {
  230. platform_driver_unregister(&txx9wdt_driver);
  231. }
  232. module_init(watchdog_init);
  233. module_exit(watchdog_exit);
  234. MODULE_DESCRIPTION("TXx9 Watchdog Driver");
  235. MODULE_LICENSE("GPL");
  236. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  237. MODULE_ALIAS("platform:txx9wdt");