txx9wdt.c 6.6 KB

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