of_xilinx_wdt.c 9.7 KB

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