of_xilinx_wdt.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
  3. *
  4. * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/fs.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/init.h>
  18. #include <linux/ioport.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/io.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_address.h>
  25. /* Register offsets for the Wdt device */
  26. #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
  27. #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
  28. #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
  29. /* Control/Status Register Masks */
  30. #define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
  31. #define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
  32. #define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
  33. /* Control/Status Register 0/1 bits */
  34. #define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
  35. /* SelfTest constants */
  36. #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
  37. #define XWT_TIMER_FAILED 0xFFFFFFFF
  38. #define WATCHDOG_NAME "Xilinx Watchdog"
  39. #define PFX WATCHDOG_NAME ": "
  40. struct xwdt_device {
  41. struct resource res;
  42. void __iomem *base;
  43. u32 nowayout;
  44. u32 wdt_interval;
  45. u32 boot_status;
  46. };
  47. static struct xwdt_device xdev;
  48. static u32 timeout;
  49. static u32 control_status_reg;
  50. static u8 expect_close;
  51. static u8 no_timeout;
  52. static unsigned long driver_open;
  53. static DEFINE_SPINLOCK(spinlock);
  54. static void xwdt_start(void)
  55. {
  56. spin_lock(&spinlock);
  57. /* Clean previous status and enable the watchdog timer */
  58. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  59. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  60. iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
  61. xdev.base + XWT_TWCSR0_OFFSET);
  62. iowrite32(XWT_CSRX_EWDT2_MASK, xdev.base + XWT_TWCSR1_OFFSET);
  63. spin_unlock(&spinlock);
  64. }
  65. static void xwdt_stop(void)
  66. {
  67. spin_lock(&spinlock);
  68. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  69. iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
  70. xdev.base + XWT_TWCSR0_OFFSET);
  71. iowrite32(0, xdev.base + XWT_TWCSR1_OFFSET);
  72. spin_unlock(&spinlock);
  73. pr_info("Stopped!\n");
  74. }
  75. static void xwdt_keepalive(void)
  76. {
  77. spin_lock(&spinlock);
  78. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  79. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  80. iowrite32(control_status_reg, xdev.base + XWT_TWCSR0_OFFSET);
  81. spin_unlock(&spinlock);
  82. }
  83. static void xwdt_get_status(int *status)
  84. {
  85. int new_status;
  86. spin_lock(&spinlock);
  87. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  88. new_status = ((control_status_reg &
  89. (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK)) != 0);
  90. spin_unlock(&spinlock);
  91. *status = 0;
  92. if (new_status & 1)
  93. *status |= WDIOF_CARDRESET;
  94. }
  95. static u32 xwdt_selftest(void)
  96. {
  97. int i;
  98. u32 timer_value1;
  99. u32 timer_value2;
  100. spin_lock(&spinlock);
  101. timer_value1 = ioread32(xdev.base + XWT_TBR_OFFSET);
  102. timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
  103. for (i = 0;
  104. ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
  105. (timer_value2 == timer_value1)); i++) {
  106. timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
  107. }
  108. spin_unlock(&spinlock);
  109. if (timer_value2 != timer_value1)
  110. return ~XWT_TIMER_FAILED;
  111. else
  112. return XWT_TIMER_FAILED;
  113. }
  114. static int xwdt_open(struct inode *inode, struct file *file)
  115. {
  116. /* Only one process can handle the wdt at a time */
  117. if (test_and_set_bit(0, &driver_open))
  118. return -EBUSY;
  119. /* Make sure that the module are always loaded...*/
  120. if (xdev.nowayout)
  121. __module_get(THIS_MODULE);
  122. xwdt_start();
  123. pr_info("Started...\n");
  124. return nonseekable_open(inode, file);
  125. }
  126. static int xwdt_release(struct inode *inode, struct file *file)
  127. {
  128. if (expect_close == 42) {
  129. xwdt_stop();
  130. } else {
  131. pr_crit("Unexpected close, not stopping watchdog!\n");
  132. xwdt_keepalive();
  133. }
  134. clear_bit(0, &driver_open);
  135. expect_close = 0;
  136. return 0;
  137. }
  138. /*
  139. * xwdt_write:
  140. * @file: file handle to the watchdog
  141. * @buf: buffer to write (unused as data does not matter here
  142. * @count: count of bytes
  143. * @ppos: pointer to the position to write. No seeks allowed
  144. *
  145. * A write to a watchdog device is defined as a keepalive signal. Any
  146. * write of data will do, as we don't define content meaning.
  147. */
  148. static ssize_t xwdt_write(struct file *file, const char __user *buf,
  149. size_t len, loff_t *ppos)
  150. {
  151. if (len) {
  152. if (!xdev.nowayout) {
  153. size_t i;
  154. /* In case it was set long ago */
  155. expect_close = 0;
  156. for (i = 0; i != len; i++) {
  157. char c;
  158. if (get_user(c, buf + i))
  159. return -EFAULT;
  160. if (c == 'V')
  161. expect_close = 42;
  162. }
  163. }
  164. xwdt_keepalive();
  165. }
  166. return len;
  167. }
  168. static const struct watchdog_info ident = {
  169. .options = WDIOF_MAGICCLOSE |
  170. WDIOF_KEEPALIVEPING,
  171. .firmware_version = 1,
  172. .identity = WATCHDOG_NAME,
  173. };
  174. /*
  175. * xwdt_ioctl:
  176. * @file: file handle to the device
  177. * @cmd: watchdog command
  178. * @arg: argument pointer
  179. *
  180. * The watchdog API defines a common set of functions for all watchdogs
  181. * according to their available features.
  182. */
  183. static long xwdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  184. {
  185. int status;
  186. union {
  187. struct watchdog_info __user *ident;
  188. int __user *i;
  189. } uarg;
  190. uarg.i = (int __user *)arg;
  191. switch (cmd) {
  192. case WDIOC_GETSUPPORT:
  193. return copy_to_user(uarg.ident, &ident,
  194. sizeof(ident)) ? -EFAULT : 0;
  195. case WDIOC_GETBOOTSTATUS:
  196. return put_user(xdev.boot_status, uarg.i);
  197. case WDIOC_GETSTATUS:
  198. xwdt_get_status(&status);
  199. return put_user(status, uarg.i);
  200. case WDIOC_KEEPALIVE:
  201. xwdt_keepalive();
  202. return 0;
  203. case WDIOC_GETTIMEOUT:
  204. if (no_timeout)
  205. return -ENOTTY;
  206. else
  207. return put_user(timeout, uarg.i);
  208. default:
  209. return -ENOTTY;
  210. }
  211. }
  212. static const struct file_operations xwdt_fops = {
  213. .owner = THIS_MODULE,
  214. .llseek = no_llseek,
  215. .write = xwdt_write,
  216. .open = xwdt_open,
  217. .release = xwdt_release,
  218. .unlocked_ioctl = xwdt_ioctl,
  219. };
  220. static struct miscdevice xwdt_miscdev = {
  221. .minor = WATCHDOG_MINOR,
  222. .name = "watchdog",
  223. .fops = &xwdt_fops,
  224. };
  225. static int xwdt_probe(struct platform_device *pdev)
  226. {
  227. int rc;
  228. u32 *tmptr;
  229. u32 *pfreq;
  230. no_timeout = 0;
  231. pfreq = (u32 *)of_get_property(pdev->dev.of_node,
  232. "clock-frequency", NULL);
  233. if (pfreq == NULL) {
  234. pr_warn("The watchdog clock frequency cannot be obtained!\n");
  235. no_timeout = 1;
  236. }
  237. rc = of_address_to_resource(pdev->dev.of_node, 0, &xdev.res);
  238. if (rc) {
  239. pr_warn("invalid address!\n");
  240. return rc;
  241. }
  242. tmptr = (u32 *)of_get_property(pdev->dev.of_node,
  243. "xlnx,wdt-interval", NULL);
  244. if (tmptr == NULL) {
  245. pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n");
  246. no_timeout = 1;
  247. } else {
  248. xdev.wdt_interval = *tmptr;
  249. }
  250. tmptr = (u32 *)of_get_property(pdev->dev.of_node,
  251. "xlnx,wdt-enable-once", NULL);
  252. if (tmptr == NULL) {
  253. pr_warn("Parameter \"xlnx,wdt-enable-once\" not found in device tree!\n");
  254. xdev.nowayout = WATCHDOG_NOWAYOUT;
  255. }
  256. /*
  257. * Twice of the 2^wdt_interval / freq because the first wdt overflow is
  258. * ignored (interrupt), reset is only generated at second wdt overflow
  259. */
  260. if (!no_timeout)
  261. timeout = 2 * ((1<<xdev.wdt_interval) / *pfreq);
  262. if (!request_mem_region(xdev.res.start,
  263. xdev.res.end - xdev.res.start + 1, WATCHDOG_NAME)) {
  264. rc = -ENXIO;
  265. pr_err("memory request failure!\n");
  266. goto err_out;
  267. }
  268. xdev.base = ioremap(xdev.res.start, xdev.res.end - xdev.res.start + 1);
  269. if (xdev.base == NULL) {
  270. rc = -ENOMEM;
  271. pr_err("ioremap failure!\n");
  272. goto release_mem;
  273. }
  274. rc = xwdt_selftest();
  275. if (rc == XWT_TIMER_FAILED) {
  276. pr_err("SelfTest routine error!\n");
  277. goto unmap_io;
  278. }
  279. xwdt_get_status(&xdev.boot_status);
  280. rc = misc_register(&xwdt_miscdev);
  281. if (rc) {
  282. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  283. xwdt_miscdev.minor, rc);
  284. goto unmap_io;
  285. }
  286. if (no_timeout)
  287. pr_info("driver loaded (timeout=? sec, nowayout=%d)\n",
  288. xdev.nowayout);
  289. else
  290. pr_info("driver loaded (timeout=%d sec, nowayout=%d)\n",
  291. timeout, xdev.nowayout);
  292. expect_close = 0;
  293. clear_bit(0, &driver_open);
  294. return 0;
  295. unmap_io:
  296. iounmap(xdev.base);
  297. release_mem:
  298. release_mem_region(xdev.res.start, resource_size(&xdev.res));
  299. err_out:
  300. return rc;
  301. }
  302. static int xwdt_remove(struct platform_device *dev)
  303. {
  304. misc_deregister(&xwdt_miscdev);
  305. iounmap(xdev.base);
  306. release_mem_region(xdev.res.start, resource_size(&xdev.res));
  307. return 0;
  308. }
  309. /* Match table for of_platform binding */
  310. static struct of_device_id xwdt_of_match[] = {
  311. { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
  312. { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
  313. {},
  314. };
  315. MODULE_DEVICE_TABLE(of, xwdt_of_match);
  316. static struct platform_driver xwdt_driver = {
  317. .probe = xwdt_probe,
  318. .remove = xwdt_remove,
  319. .driver = {
  320. .owner = THIS_MODULE,
  321. .name = WATCHDOG_NAME,
  322. .of_match_table = xwdt_of_match,
  323. },
  324. };
  325. module_platform_driver(xwdt_driver);
  326. MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
  327. MODULE_DESCRIPTION("Xilinx Watchdog driver");
  328. MODULE_LICENSE("GPL v2");
  329. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);