gef_wdt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include <linux/kernel.h>
  26. #include <linux/compiler.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/fs.h>
  32. #include <linux/of.h>
  33. #include <linux/of_platform.h>
  34. #include <linux/io.h>
  35. #include <linux/uaccess.h>
  36. #include <sysdev/fsl_soc.h>
  37. /*
  38. * The watchdog configuration register contains a pair of 2-bit fields,
  39. * 1. a reload field, bits 27-26, which triggers a reload of
  40. * the countdown register, and
  41. * 2. an enable field, bits 25-24, which toggles between
  42. * enabling and disabling the watchdog timer.
  43. * Bit 31 is a read-only field which indicates whether the
  44. * watchdog timer is currently enabled.
  45. *
  46. * The low 24 bits contain the timer reload value.
  47. */
  48. #define GEF_WDC_ENABLE_SHIFT 24
  49. #define GEF_WDC_SERVICE_SHIFT 26
  50. #define GEF_WDC_ENABLED_SHIFT 31
  51. #define GEF_WDC_ENABLED_TRUE 1
  52. #define GEF_WDC_ENABLED_FALSE 0
  53. /* Flags bits */
  54. #define GEF_WDOG_FLAG_OPENED 0
  55. static unsigned long wdt_flags;
  56. static int wdt_status;
  57. static void __iomem *gef_wdt_regs;
  58. static int gef_wdt_timeout;
  59. static int gef_wdt_count;
  60. static unsigned int bus_clk;
  61. static char expect_close;
  62. static DEFINE_SPINLOCK(gef_wdt_spinlock);
  63. static int nowayout = WATCHDOG_NOWAYOUT;
  64. module_param(nowayout, int, 0);
  65. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  66. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  67. static int gef_wdt_toggle_wdc(int enabled_predicate, int field_shift)
  68. {
  69. u32 data;
  70. u32 enabled;
  71. int ret = 0;
  72. spin_lock(&gef_wdt_spinlock);
  73. data = ioread32be(gef_wdt_regs);
  74. enabled = (data >> GEF_WDC_ENABLED_SHIFT) & 1;
  75. /* only toggle the requested field if enabled state matches predicate */
  76. if ((enabled ^ enabled_predicate) == 0) {
  77. /* We write a 1, then a 2 -- to the appropriate field */
  78. data = (1 << field_shift) | gef_wdt_count;
  79. iowrite32be(data, gef_wdt_regs);
  80. data = (2 << field_shift) | gef_wdt_count;
  81. iowrite32be(data, gef_wdt_regs);
  82. ret = 1;
  83. }
  84. spin_unlock(&gef_wdt_spinlock);
  85. return ret;
  86. }
  87. static void gef_wdt_service(void)
  88. {
  89. gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
  90. GEF_WDC_SERVICE_SHIFT);
  91. }
  92. static void gef_wdt_handler_enable(void)
  93. {
  94. if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_FALSE,
  95. GEF_WDC_ENABLE_SHIFT)) {
  96. gef_wdt_service();
  97. printk(KERN_NOTICE "gef_wdt: watchdog activated\n");
  98. }
  99. }
  100. static void gef_wdt_handler_disable(void)
  101. {
  102. if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
  103. GEF_WDC_ENABLE_SHIFT))
  104. printk(KERN_NOTICE "gef_wdt: watchdog deactivated\n");
  105. }
  106. static void gef_wdt_set_timeout(unsigned int timeout)
  107. {
  108. /* maximum bus cycle count is 0xFFFFFFFF */
  109. if (timeout > 0xFFFFFFFF / bus_clk)
  110. timeout = 0xFFFFFFFF / bus_clk;
  111. /* Register only holds upper 24 bits, bit shifted into lower 24 */
  112. gef_wdt_count = (timeout * bus_clk) >> 8;
  113. gef_wdt_timeout = timeout;
  114. }
  115. static ssize_t gef_wdt_write(struct file *file, const char __user *data,
  116. size_t len, loff_t *ppos)
  117. {
  118. if (len) {
  119. if (!nowayout) {
  120. size_t i;
  121. expect_close = 0;
  122. for (i = 0; i != len; i++) {
  123. char c;
  124. if (get_user(c, data + i))
  125. return -EFAULT;
  126. if (c == 'V')
  127. expect_close = 42;
  128. }
  129. }
  130. gef_wdt_service();
  131. }
  132. return len;
  133. }
  134. static long gef_wdt_ioctl(struct file *file, unsigned int cmd,
  135. unsigned long arg)
  136. {
  137. int timeout;
  138. int options;
  139. void __user *argp = (void __user *)arg;
  140. static const struct watchdog_info info = {
  141. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  142. WDIOF_KEEPALIVEPING,
  143. .firmware_version = 0,
  144. .identity = "GE watchdog",
  145. };
  146. switch (cmd) {
  147. case WDIOC_GETSUPPORT:
  148. if (copy_to_user(argp, &info, sizeof(info)))
  149. return -EFAULT;
  150. break;
  151. case WDIOC_GETSTATUS:
  152. case WDIOC_GETBOOTSTATUS:
  153. if (put_user(wdt_status, (int __user *)argp))
  154. return -EFAULT;
  155. wdt_status &= ~WDIOF_KEEPALIVEPING;
  156. break;
  157. case WDIOC_SETOPTIONS:
  158. if (get_user(options, (int __user *)argp))
  159. return -EFAULT;
  160. if (options & WDIOS_DISABLECARD)
  161. gef_wdt_handler_disable();
  162. if (options & WDIOS_ENABLECARD)
  163. gef_wdt_handler_enable();
  164. break;
  165. case WDIOC_KEEPALIVE:
  166. gef_wdt_service();
  167. wdt_status |= WDIOF_KEEPALIVEPING;
  168. break;
  169. case WDIOC_SETTIMEOUT:
  170. if (get_user(timeout, (int __user *)argp))
  171. return -EFAULT;
  172. gef_wdt_set_timeout(timeout);
  173. /* Fall through */
  174. case WDIOC_GETTIMEOUT:
  175. if (put_user(gef_wdt_timeout, (int __user *)argp))
  176. return -EFAULT;
  177. break;
  178. default:
  179. return -ENOTTY;
  180. }
  181. return 0;
  182. }
  183. static int gef_wdt_open(struct inode *inode, struct file *file)
  184. {
  185. if (test_and_set_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags))
  186. return -EBUSY;
  187. if (nowayout)
  188. __module_get(THIS_MODULE);
  189. gef_wdt_handler_enable();
  190. return nonseekable_open(inode, file);
  191. }
  192. static int gef_wdt_release(struct inode *inode, struct file *file)
  193. {
  194. if (expect_close == 42)
  195. gef_wdt_handler_disable();
  196. else {
  197. printk(KERN_CRIT
  198. "gef_wdt: 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. const struct of_device_id *match)
  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 __devexit 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. static struct of_platform_driver gef_wdt_driver = {
  249. .driver = {
  250. .name = "gef_wdt",
  251. .owner = THIS_MODULE,
  252. .of_match_table = gef_wdt_ids,
  253. },
  254. .probe = gef_wdt_probe,
  255. };
  256. static int __init gef_wdt_init(void)
  257. {
  258. printk(KERN_INFO "GE watchdog driver\n");
  259. return of_register_platform_driver(&gef_wdt_driver);
  260. }
  261. static void __exit gef_wdt_exit(void)
  262. {
  263. of_unregister_platform_driver(&gef_wdt_driver);
  264. }
  265. module_init(gef_wdt_init);
  266. module_exit(gef_wdt_exit);
  267. MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
  268. MODULE_DESCRIPTION("GE watchdog driver");
  269. MODULE_LICENSE("GPL");
  270. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  271. MODULE_ALIAS("platform: gef_wdt");