txx9wdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/reboot.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 int nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(nowayout, int, 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 void txx9wdt_ping(void)
  44. {
  45. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  46. }
  47. static void txx9wdt_start(void)
  48. {
  49. __raw_writel(WD_TIMER_CLK * timeout, &txx9wdt_reg->cpra);
  50. __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
  51. __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
  52. __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
  53. &txx9wdt_reg->tcr);
  54. __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
  55. }
  56. static void txx9wdt_stop(void)
  57. {
  58. __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
  59. __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
  60. &txx9wdt_reg->tcr);
  61. }
  62. static int txx9wdt_open(struct inode *inode, struct file *file)
  63. {
  64. if (test_and_set_bit(0, &txx9wdt_alive))
  65. return -EBUSY;
  66. if (__raw_readl(&txx9wdt_reg->tcr) & TXx9_TMTCR_TCE) {
  67. clear_bit(0, &txx9wdt_alive);
  68. return -EBUSY;
  69. }
  70. if (nowayout)
  71. __module_get(THIS_MODULE);
  72. txx9wdt_start();
  73. return nonseekable_open(inode, file);
  74. }
  75. static int txx9wdt_release(struct inode *inode, struct file *file)
  76. {
  77. if (expect_close)
  78. txx9wdt_stop();
  79. else {
  80. printk(KERN_CRIT "txx9wdt: "
  81. "Unexpected close, not stopping watchdog!\n");
  82. txx9wdt_ping();
  83. }
  84. clear_bit(0, &txx9wdt_alive);
  85. expect_close = 0;
  86. return 0;
  87. }
  88. static ssize_t txx9wdt_write(struct file *file, const char __user *data,
  89. size_t len, loff_t *ppos)
  90. {
  91. if (len) {
  92. if (!nowayout) {
  93. size_t i;
  94. expect_close = 0;
  95. for (i = 0; i != len; i++) {
  96. char c;
  97. if (get_user(c, data + i))
  98. return -EFAULT;
  99. if (c == 'V')
  100. expect_close = 1;
  101. }
  102. }
  103. txx9wdt_ping();
  104. }
  105. return len;
  106. }
  107. static int txx9wdt_ioctl(struct inode *inode, struct file *file,
  108. unsigned int cmd, unsigned long arg)
  109. {
  110. void __user *argp = (void __user *)arg;
  111. int __user *p = argp;
  112. int new_timeout;
  113. static struct watchdog_info ident = {
  114. .options = WDIOF_SETTIMEOUT |
  115. WDIOF_KEEPALIVEPING |
  116. WDIOF_MAGICCLOSE,
  117. .firmware_version = 0,
  118. .identity = "Hardware Watchdog for TXx9",
  119. };
  120. switch (cmd) {
  121. default:
  122. return -ENOTTY;
  123. case WDIOC_GETSUPPORT:
  124. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  125. case WDIOC_GETSTATUS:
  126. case WDIOC_GETBOOTSTATUS:
  127. return put_user(0, p);
  128. case WDIOC_KEEPALIVE:
  129. txx9wdt_ping();
  130. return 0;
  131. case WDIOC_SETTIMEOUT:
  132. if (get_user(new_timeout, p))
  133. return -EFAULT;
  134. if (new_timeout < 1 || new_timeout > WD_MAX_TIMEOUT)
  135. return -EINVAL;
  136. timeout = new_timeout;
  137. txx9wdt_stop();
  138. txx9wdt_start();
  139. /* Fall */
  140. case WDIOC_GETTIMEOUT:
  141. return put_user(timeout, p);
  142. }
  143. }
  144. static int txx9wdt_notify_sys(struct notifier_block *this, unsigned long code,
  145. void *unused)
  146. {
  147. if (code == SYS_DOWN || code == SYS_HALT)
  148. txx9wdt_stop();
  149. return NOTIFY_DONE;
  150. }
  151. static const struct file_operations txx9wdt_fops = {
  152. .owner = THIS_MODULE,
  153. .llseek = no_llseek,
  154. .write = txx9wdt_write,
  155. .ioctl = txx9wdt_ioctl,
  156. .open = txx9wdt_open,
  157. .release = txx9wdt_release,
  158. };
  159. static struct miscdevice txx9wdt_miscdev = {
  160. .minor = WATCHDOG_MINOR,
  161. .name = "watchdog",
  162. .fops = &txx9wdt_fops,
  163. };
  164. static struct notifier_block txx9wdt_notifier = {
  165. .notifier_call = txx9wdt_notify_sys
  166. };
  167. static int __init txx9wdt_probe(struct platform_device *dev)
  168. {
  169. struct resource *res;
  170. int ret;
  171. txx9_imclk = clk_get(NULL, "imbus_clk");
  172. if (IS_ERR(txx9_imclk)) {
  173. ret = PTR_ERR(txx9_imclk);
  174. txx9_imclk = NULL;
  175. goto exit;
  176. }
  177. ret = clk_enable(txx9_imclk);
  178. if (ret) {
  179. clk_put(txx9_imclk);
  180. txx9_imclk = NULL;
  181. goto exit;
  182. }
  183. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  184. if (!res)
  185. goto exit_busy;
  186. if (!devm_request_mem_region(&dev->dev,
  187. res->start, res->end - res->start + 1,
  188. "txx9wdt"))
  189. goto exit_busy;
  190. txx9wdt_reg = devm_ioremap(&dev->dev,
  191. res->start, res->end - res->start + 1);
  192. if (!txx9wdt_reg)
  193. goto exit_busy;
  194. ret = register_reboot_notifier(&txx9wdt_notifier);
  195. if (ret)
  196. goto exit;
  197. ret = misc_register(&txx9wdt_miscdev);
  198. if (ret) {
  199. unregister_reboot_notifier(&txx9wdt_notifier);
  200. goto exit;
  201. }
  202. printk(KERN_INFO "Hardware Watchdog Timer for TXx9: "
  203. "timeout=%d sec (max %ld) (nowayout= %d)\n",
  204. timeout, WD_MAX_TIMEOUT, nowayout);
  205. return 0;
  206. exit_busy:
  207. ret = -EBUSY;
  208. exit:
  209. if (txx9_imclk) {
  210. clk_disable(txx9_imclk);
  211. clk_put(txx9_imclk);
  212. }
  213. return ret;
  214. }
  215. static int __exit txx9wdt_remove(struct platform_device *dev)
  216. {
  217. misc_deregister(&txx9wdt_miscdev);
  218. unregister_reboot_notifier(&txx9wdt_notifier);
  219. clk_disable(txx9_imclk);
  220. clk_put(txx9_imclk);
  221. return 0;
  222. }
  223. static struct platform_driver txx9wdt_driver = {
  224. .remove = __exit_p(txx9wdt_remove),
  225. .driver = {
  226. .name = "txx9wdt",
  227. .owner = THIS_MODULE,
  228. },
  229. };
  230. static int __init watchdog_init(void)
  231. {
  232. return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe);
  233. }
  234. static void __exit watchdog_exit(void)
  235. {
  236. platform_driver_unregister(&txx9wdt_driver);
  237. }
  238. module_init(watchdog_init);
  239. module_exit(watchdog_exit);
  240. MODULE_DESCRIPTION("TXx9 Watchdog Driver");
  241. MODULE_LICENSE("GPL");
  242. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  243. MODULE_ALIAS("platform:txx9wdt");