gef_wdt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * GE watchdog userspace interface
  3. *
  4. * Author: Martyn Welch <martyn.welch@ge.com>
  5. *
  6. * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * Based on: mv64x60_wdt.c (MV64X60 watchdog userspace interface)
  14. * Author: James Chapman <jchapman@katalix.com>
  15. */
  16. /* TODO:
  17. * This driver does not provide support for the hardwares capability of sending
  18. * an interrupt at a programmable threshold.
  19. *
  20. * This driver currently can only support 1 watchdog - there are 2 in the
  21. * hardware that this driver supports. Thus one could be configured as a
  22. * process-based watchdog (via /dev/watchdog), the second (using the interrupt
  23. * capabilities) a kernel-based watchdog.
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/kernel.h>
  27. #include <linux/compiler.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/watchdog.h>
  32. #include <linux/fs.h>
  33. #include <linux/of.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/io.h>
  36. #include <linux/uaccess.h>
  37. #include <sysdev/fsl_soc.h>
  38. /*
  39. * The watchdog configuration register contains a pair of 2-bit fields,
  40. * 1. a reload field, bits 27-26, which triggers a reload of
  41. * the countdown register, and
  42. * 2. an enable field, bits 25-24, which toggles between
  43. * enabling and disabling the watchdog timer.
  44. * Bit 31 is a read-only field which indicates whether the
  45. * watchdog timer is currently enabled.
  46. *
  47. * The low 24 bits contain the timer reload value.
  48. */
  49. #define GEF_WDC_ENABLE_SHIFT 24
  50. #define GEF_WDC_SERVICE_SHIFT 26
  51. #define GEF_WDC_ENABLED_SHIFT 31
  52. #define GEF_WDC_ENABLED_TRUE 1
  53. #define GEF_WDC_ENABLED_FALSE 0
  54. /* Flags bits */
  55. #define GEF_WDOG_FLAG_OPENED 0
  56. static unsigned long wdt_flags;
  57. static int wdt_status;
  58. static void __iomem *gef_wdt_regs;
  59. static int gef_wdt_timeout;
  60. static int gef_wdt_count;
  61. static unsigned int bus_clk;
  62. static char expect_close;
  63. static DEFINE_SPINLOCK(gef_wdt_spinlock);
  64. static bool nowayout = WATCHDOG_NOWAYOUT;
  65. module_param(nowayout, bool, 0);
  66. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  67. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  68. static int gef_wdt_toggle_wdc(int enabled_predicate, int field_shift)
  69. {
  70. u32 data;
  71. u32 enabled;
  72. int ret = 0;
  73. spin_lock(&gef_wdt_spinlock);
  74. data = ioread32be(gef_wdt_regs);
  75. enabled = (data >> GEF_WDC_ENABLED_SHIFT) & 1;
  76. /* only toggle the requested field if enabled state matches predicate */
  77. if ((enabled ^ enabled_predicate) == 0) {
  78. /* We write a 1, then a 2 -- to the appropriate field */
  79. data = (1 << field_shift) | gef_wdt_count;
  80. iowrite32be(data, gef_wdt_regs);
  81. data = (2 << field_shift) | gef_wdt_count;
  82. iowrite32be(data, gef_wdt_regs);
  83. ret = 1;
  84. }
  85. spin_unlock(&gef_wdt_spinlock);
  86. return ret;
  87. }
  88. static void gef_wdt_service(void)
  89. {
  90. gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
  91. GEF_WDC_SERVICE_SHIFT);
  92. }
  93. static void gef_wdt_handler_enable(void)
  94. {
  95. if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_FALSE,
  96. GEF_WDC_ENABLE_SHIFT)) {
  97. gef_wdt_service();
  98. pr_notice("watchdog activated\n");
  99. }
  100. }
  101. static void gef_wdt_handler_disable(void)
  102. {
  103. if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
  104. GEF_WDC_ENABLE_SHIFT))
  105. pr_notice("watchdog deactivated\n");
  106. }
  107. static void gef_wdt_set_timeout(unsigned int timeout)
  108. {
  109. /* maximum bus cycle count is 0xFFFFFFFF */
  110. if (timeout > 0xFFFFFFFF / bus_clk)
  111. timeout = 0xFFFFFFFF / bus_clk;
  112. /* Register only holds upper 24 bits, bit shifted into lower 24 */
  113. gef_wdt_count = (timeout * bus_clk) >> 8;
  114. gef_wdt_timeout = timeout;
  115. }
  116. static ssize_t gef_wdt_write(struct file *file, const char __user *data,
  117. size_t len, loff_t *ppos)
  118. {
  119. if (len) {
  120. if (!nowayout) {
  121. size_t i;
  122. expect_close = 0;
  123. for (i = 0; i != len; i++) {
  124. char c;
  125. if (get_user(c, data + i))
  126. return -EFAULT;
  127. if (c == 'V')
  128. expect_close = 42;
  129. }
  130. }
  131. gef_wdt_service();
  132. }
  133. return len;
  134. }
  135. static long gef_wdt_ioctl(struct file *file, unsigned int cmd,
  136. unsigned long arg)
  137. {
  138. int timeout;
  139. int options;
  140. void __user *argp = (void __user *)arg;
  141. static const struct watchdog_info info = {
  142. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  143. WDIOF_KEEPALIVEPING,
  144. .firmware_version = 0,
  145. .identity = "GE watchdog",
  146. };
  147. switch (cmd) {
  148. case WDIOC_GETSUPPORT:
  149. if (copy_to_user(argp, &info, sizeof(info)))
  150. return -EFAULT;
  151. break;
  152. case WDIOC_GETSTATUS:
  153. case WDIOC_GETBOOTSTATUS:
  154. if (put_user(wdt_status, (int __user *)argp))
  155. return -EFAULT;
  156. wdt_status &= ~WDIOF_KEEPALIVEPING;
  157. break;
  158. case WDIOC_SETOPTIONS:
  159. if (get_user(options, (int __user *)argp))
  160. return -EFAULT;
  161. if (options & WDIOS_DISABLECARD)
  162. gef_wdt_handler_disable();
  163. if (options & WDIOS_ENABLECARD)
  164. gef_wdt_handler_enable();
  165. break;
  166. case WDIOC_KEEPALIVE:
  167. gef_wdt_service();
  168. wdt_status |= WDIOF_KEEPALIVEPING;
  169. break;
  170. case WDIOC_SETTIMEOUT:
  171. if (get_user(timeout, (int __user *)argp))
  172. return -EFAULT;
  173. gef_wdt_set_timeout(timeout);
  174. /* Fall through */
  175. case WDIOC_GETTIMEOUT:
  176. if (put_user(gef_wdt_timeout, (int __user *)argp))
  177. return -EFAULT;
  178. break;
  179. default:
  180. return -ENOTTY;
  181. }
  182. return 0;
  183. }
  184. static int gef_wdt_open(struct inode *inode, struct file *file)
  185. {
  186. if (test_and_set_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags))
  187. return -EBUSY;
  188. if (nowayout)
  189. __module_get(THIS_MODULE);
  190. gef_wdt_handler_enable();
  191. return nonseekable_open(inode, file);
  192. }
  193. static int gef_wdt_release(struct inode *inode, struct file *file)
  194. {
  195. if (expect_close == 42)
  196. gef_wdt_handler_disable();
  197. else {
  198. pr_crit("unexpected close, not stopping timer!\n");
  199. gef_wdt_service();
  200. }
  201. expect_close = 0;
  202. clear_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags);
  203. return 0;
  204. }
  205. static const struct file_operations gef_wdt_fops = {
  206. .owner = THIS_MODULE,
  207. .llseek = no_llseek,
  208. .write = gef_wdt_write,
  209. .unlocked_ioctl = gef_wdt_ioctl,
  210. .open = gef_wdt_open,
  211. .release = gef_wdt_release,
  212. };
  213. static struct miscdevice gef_wdt_miscdev = {
  214. .minor = WATCHDOG_MINOR,
  215. .name = "watchdog",
  216. .fops = &gef_wdt_fops,
  217. };
  218. static int __devinit gef_wdt_probe(struct platform_device *dev)
  219. {
  220. int timeout = 10;
  221. u32 freq;
  222. bus_clk = 133; /* in MHz */
  223. freq = fsl_get_sys_freq();
  224. if (freq != -1)
  225. bus_clk = freq;
  226. /* Map devices registers into memory */
  227. gef_wdt_regs = of_iomap(dev->dev.of_node, 0);
  228. if (gef_wdt_regs == NULL)
  229. return -ENOMEM;
  230. gef_wdt_set_timeout(timeout);
  231. gef_wdt_handler_disable(); /* in case timer was already running */
  232. return misc_register(&gef_wdt_miscdev);
  233. }
  234. static int __devexit gef_wdt_remove(struct platform_device *dev)
  235. {
  236. misc_deregister(&gef_wdt_miscdev);
  237. gef_wdt_handler_disable();
  238. iounmap(gef_wdt_regs);
  239. return 0;
  240. }
  241. static const struct of_device_id gef_wdt_ids[] = {
  242. {
  243. .compatible = "gef,fpga-wdt",
  244. },
  245. {},
  246. };
  247. static struct platform_driver gef_wdt_driver = {
  248. .driver = {
  249. .name = "gef_wdt",
  250. .owner = THIS_MODULE,
  251. .of_match_table = gef_wdt_ids,
  252. },
  253. .probe = gef_wdt_probe,
  254. };
  255. static int __init gef_wdt_init(void)
  256. {
  257. pr_info("GE watchdog driver\n");
  258. return platform_driver_register(&gef_wdt_driver);
  259. }
  260. static void __exit gef_wdt_exit(void)
  261. {
  262. platform_driver_unregister(&gef_wdt_driver);
  263. }
  264. module_init(gef_wdt_init);
  265. module_exit(gef_wdt_exit);
  266. MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
  267. MODULE_DESCRIPTION("GE watchdog driver");
  268. MODULE_LICENSE("GPL");
  269. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  270. MODULE_ALIAS("platform:gef_wdt");