gef_wdt.c 7.3 KB

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