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