of_xilinx_wdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/init.h>
  27. #include <linux/ioport.h>
  28. #include <linux/watchdog.h>
  29. #include <linux/io.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/of_address.h>
  34. /* Register offsets for the Wdt device */
  35. #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
  36. #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
  37. #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
  38. /* Control/Status Register Masks */
  39. #define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
  40. #define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
  41. #define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
  42. /* Control/Status Register 0/1 bits */
  43. #define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
  44. /* SelfTest constants */
  45. #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
  46. #define XWT_TIMER_FAILED 0xFFFFFFFF
  47. #define WATCHDOG_NAME "Xilinx Watchdog"
  48. #define PFX WATCHDOG_NAME ": "
  49. struct xwdt_device {
  50. struct resource res;
  51. void __iomem *base;
  52. u32 nowayout;
  53. u32 wdt_interval;
  54. u32 boot_status;
  55. };
  56. static struct xwdt_device xdev;
  57. static u32 timeout;
  58. static u32 control_status_reg;
  59. static u8 expect_close;
  60. static u8 no_timeout;
  61. static unsigned long driver_open;
  62. static DEFINE_SPINLOCK(spinlock);
  63. static void xwdt_start(void)
  64. {
  65. spin_lock(&spinlock);
  66. /* Clean previous status and enable the watchdog timer */
  67. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  68. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  69. iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
  70. xdev.base + XWT_TWCSR0_OFFSET);
  71. iowrite32(XWT_CSRX_EWDT2_MASK, xdev.base + XWT_TWCSR1_OFFSET);
  72. spin_unlock(&spinlock);
  73. }
  74. static void xwdt_stop(void)
  75. {
  76. spin_lock(&spinlock);
  77. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  78. iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
  79. xdev.base + XWT_TWCSR0_OFFSET);
  80. iowrite32(0, xdev.base + XWT_TWCSR1_OFFSET);
  81. spin_unlock(&spinlock);
  82. printk(KERN_INFO PFX "Stopped!\n");
  83. }
  84. static void xwdt_keepalive(void)
  85. {
  86. spin_lock(&spinlock);
  87. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  88. control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
  89. iowrite32(control_status_reg, xdev.base + XWT_TWCSR0_OFFSET);
  90. spin_unlock(&spinlock);
  91. }
  92. static void xwdt_get_status(int *status)
  93. {
  94. int new_status;
  95. spin_lock(&spinlock);
  96. control_status_reg = ioread32(xdev.base + XWT_TWCSR0_OFFSET);
  97. new_status = ((control_status_reg &
  98. (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK)) != 0);
  99. spin_unlock(&spinlock);
  100. *status = 0;
  101. if (new_status & 1)
  102. *status |= WDIOF_CARDRESET;
  103. }
  104. static u32 xwdt_selftest(void)
  105. {
  106. int i;
  107. u32 timer_value1;
  108. u32 timer_value2;
  109. spin_lock(&spinlock);
  110. timer_value1 = ioread32(xdev.base + XWT_TBR_OFFSET);
  111. timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
  112. for (i = 0;
  113. ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
  114. (timer_value2 == timer_value1)); i++) {
  115. timer_value2 = ioread32(xdev.base + XWT_TBR_OFFSET);
  116. }
  117. spin_unlock(&spinlock);
  118. if (timer_value2 != timer_value1)
  119. return ~XWT_TIMER_FAILED;
  120. else
  121. return XWT_TIMER_FAILED;
  122. }
  123. static int xwdt_open(struct inode *inode, struct file *file)
  124. {
  125. /* Only one process can handle the wdt at a time */
  126. if (test_and_set_bit(0, &driver_open))
  127. return -EBUSY;
  128. /* Make sure that the module are always loaded...*/
  129. if (xdev.nowayout)
  130. __module_get(THIS_MODULE);
  131. xwdt_start();
  132. printk(KERN_INFO PFX "Started...\n");
  133. return nonseekable_open(inode, file);
  134. }
  135. static int xwdt_release(struct inode *inode, struct file *file)
  136. {
  137. if (expect_close == 42) {
  138. xwdt_stop();
  139. } else {
  140. printk(KERN_CRIT PFX
  141. "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. printk(KERN_WARNING PFX
  245. "The watchdog clock frequency cannot be obtained!\n");
  246. no_timeout = 1;
  247. }
  248. rc = of_address_to_resource(pdev->dev.of_node, 0, &xdev.res);
  249. if (rc) {
  250. printk(KERN_WARNING PFX "invalid address!\n");
  251. return rc;
  252. }
  253. tmptr = (u32 *)of_get_property(pdev->dev.of_node,
  254. "xlnx,wdt-interval", NULL);
  255. if (tmptr == NULL) {
  256. printk(KERN_WARNING PFX "Parameter \"xlnx,wdt-interval\""
  257. " not found in device tree!\n");
  258. no_timeout = 1;
  259. } else {
  260. xdev.wdt_interval = *tmptr;
  261. }
  262. tmptr = (u32 *)of_get_property(pdev->dev.of_node,
  263. "xlnx,wdt-enable-once", NULL);
  264. if (tmptr == NULL) {
  265. printk(KERN_WARNING PFX "Parameter \"xlnx,wdt-enable-once\""
  266. " not found in device tree!\n");
  267. xdev.nowayout = WATCHDOG_NOWAYOUT;
  268. }
  269. /*
  270. * Twice of the 2^wdt_interval / freq because the first wdt overflow is
  271. * ignored (interrupt), reset is only generated at second wdt overflow
  272. */
  273. if (!no_timeout)
  274. timeout = 2 * ((1<<xdev.wdt_interval) / *pfreq);
  275. if (!request_mem_region(xdev.res.start,
  276. xdev.res.end - xdev.res.start + 1, WATCHDOG_NAME)) {
  277. rc = -ENXIO;
  278. printk(KERN_ERR PFX "memory request failure!\n");
  279. goto err_out;
  280. }
  281. xdev.base = ioremap(xdev.res.start, xdev.res.end - xdev.res.start + 1);
  282. if (xdev.base == NULL) {
  283. rc = -ENOMEM;
  284. printk(KERN_ERR PFX "ioremap failure!\n");
  285. goto release_mem;
  286. }
  287. rc = xwdt_selftest();
  288. if (rc == XWT_TIMER_FAILED) {
  289. printk(KERN_ERR PFX "SelfTest routine error!\n");
  290. goto unmap_io;
  291. }
  292. xwdt_get_status(&xdev.boot_status);
  293. rc = misc_register(&xwdt_miscdev);
  294. if (rc) {
  295. printk(KERN_ERR PFX
  296. "cannot register miscdev on minor=%d (err=%d)\n",
  297. xwdt_miscdev.minor, rc);
  298. goto unmap_io;
  299. }
  300. if (no_timeout)
  301. printk(KERN_INFO PFX
  302. "driver loaded (timeout=? sec, nowayout=%d)\n",
  303. xdev.nowayout);
  304. else
  305. printk(KERN_INFO PFX
  306. "driver loaded (timeout=%d sec, nowayout=%d)\n",
  307. timeout, xdev.nowayout);
  308. expect_close = 0;
  309. clear_bit(0, &driver_open);
  310. return 0;
  311. unmap_io:
  312. iounmap(xdev.base);
  313. release_mem:
  314. release_mem_region(xdev.res.start, resource_size(&xdev.res));
  315. err_out:
  316. return rc;
  317. }
  318. static int __devexit xwdt_remove(struct platform_device *dev)
  319. {
  320. misc_deregister(&xwdt_miscdev);
  321. iounmap(xdev.base);
  322. release_mem_region(xdev.res.start, resource_size(&xdev.res));
  323. return 0;
  324. }
  325. /* Match table for of_platform binding */
  326. static struct of_device_id __devinitdata xwdt_of_match[] = {
  327. { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
  328. {},
  329. };
  330. MODULE_DEVICE_TABLE(of, xwdt_of_match);
  331. static struct platform_driver xwdt_driver = {
  332. .probe = xwdt_probe,
  333. .remove = __devexit_p(xwdt_remove),
  334. .driver = {
  335. .owner = THIS_MODULE,
  336. .name = WATCHDOG_NAME,
  337. .of_match_table = xwdt_of_match,
  338. },
  339. };
  340. static int __init xwdt_init(void)
  341. {
  342. return platform_driver_register(&xwdt_driver);
  343. }
  344. static void __exit xwdt_exit(void)
  345. {
  346. platform_driver_unregister(&xwdt_driver);
  347. }
  348. module_init(xwdt_init);
  349. module_exit(xwdt_exit);
  350. MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
  351. MODULE_DESCRIPTION("Xilinx Watchdog driver");
  352. MODULE_LICENSE("GPL");
  353. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);