adx_wdt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (C) 2008-2009 Avionic Design GmbH
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/gfp.h>
  10. #include <linux/io.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/types.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/watchdog.h>
  17. #define WATCHDOG_NAME "adx-wdt"
  18. /* register offsets */
  19. #define ADX_WDT_CONTROL 0x00
  20. #define ADX_WDT_CONTROL_ENABLE (1 << 0)
  21. #define ADX_WDT_CONTROL_nRESET (1 << 1)
  22. #define ADX_WDT_TIMEOUT 0x08
  23. static struct platform_device *adx_wdt_dev;
  24. static unsigned long driver_open;
  25. #define WDT_STATE_STOP 0
  26. #define WDT_STATE_START 1
  27. struct adx_wdt {
  28. void __iomem *base;
  29. unsigned long timeout;
  30. unsigned int state;
  31. unsigned int wake;
  32. spinlock_t lock;
  33. };
  34. static const struct watchdog_info adx_wdt_info = {
  35. .identity = "Avionic Design Xanthos Watchdog",
  36. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  37. };
  38. static void adx_wdt_start_locked(struct adx_wdt *wdt)
  39. {
  40. u32 ctrl;
  41. ctrl = readl(wdt->base + ADX_WDT_CONTROL);
  42. ctrl |= ADX_WDT_CONTROL_ENABLE;
  43. writel(ctrl, wdt->base + ADX_WDT_CONTROL);
  44. wdt->state = WDT_STATE_START;
  45. }
  46. static void adx_wdt_start(struct adx_wdt *wdt)
  47. {
  48. unsigned long flags;
  49. spin_lock_irqsave(&wdt->lock, flags);
  50. adx_wdt_start_locked(wdt);
  51. spin_unlock_irqrestore(&wdt->lock, flags);
  52. }
  53. static void adx_wdt_stop_locked(struct adx_wdt *wdt)
  54. {
  55. u32 ctrl;
  56. ctrl = readl(wdt->base + ADX_WDT_CONTROL);
  57. ctrl &= ~ADX_WDT_CONTROL_ENABLE;
  58. writel(ctrl, wdt->base + ADX_WDT_CONTROL);
  59. wdt->state = WDT_STATE_STOP;
  60. }
  61. static void adx_wdt_stop(struct adx_wdt *wdt)
  62. {
  63. unsigned long flags;
  64. spin_lock_irqsave(&wdt->lock, flags);
  65. adx_wdt_stop_locked(wdt);
  66. spin_unlock_irqrestore(&wdt->lock, flags);
  67. }
  68. static void adx_wdt_set_timeout(struct adx_wdt *wdt, unsigned long seconds)
  69. {
  70. unsigned long timeout = seconds * 1000;
  71. unsigned long flags;
  72. unsigned int state;
  73. spin_lock_irqsave(&wdt->lock, flags);
  74. state = wdt->state;
  75. adx_wdt_stop_locked(wdt);
  76. writel(timeout, wdt->base + ADX_WDT_TIMEOUT);
  77. if (state == WDT_STATE_START)
  78. adx_wdt_start_locked(wdt);
  79. wdt->timeout = timeout;
  80. spin_unlock_irqrestore(&wdt->lock, flags);
  81. }
  82. static void adx_wdt_get_timeout(struct adx_wdt *wdt, unsigned long *seconds)
  83. {
  84. *seconds = wdt->timeout / 1000;
  85. }
  86. static void adx_wdt_keepalive(struct adx_wdt *wdt)
  87. {
  88. unsigned long flags;
  89. spin_lock_irqsave(&wdt->lock, flags);
  90. writel(wdt->timeout, wdt->base + ADX_WDT_TIMEOUT);
  91. spin_unlock_irqrestore(&wdt->lock, flags);
  92. }
  93. static int adx_wdt_open(struct inode *inode, struct file *file)
  94. {
  95. struct adx_wdt *wdt = platform_get_drvdata(adx_wdt_dev);
  96. if (test_and_set_bit(0, &driver_open))
  97. return -EBUSY;
  98. file->private_data = wdt;
  99. adx_wdt_set_timeout(wdt, 30);
  100. adx_wdt_start(wdt);
  101. return nonseekable_open(inode, file);
  102. }
  103. static int adx_wdt_release(struct inode *inode, struct file *file)
  104. {
  105. struct adx_wdt *wdt = file->private_data;
  106. adx_wdt_stop(wdt);
  107. clear_bit(0, &driver_open);
  108. return 0;
  109. }
  110. static long adx_wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  111. {
  112. struct adx_wdt *wdt = file->private_data;
  113. void __user *argp = (void __user *)arg;
  114. unsigned long __user *p = argp;
  115. unsigned long seconds = 0;
  116. unsigned int options;
  117. long ret = -EINVAL;
  118. switch (cmd) {
  119. case WDIOC_GETSUPPORT:
  120. if (copy_to_user(argp, &adx_wdt_info, sizeof(adx_wdt_info)))
  121. return -EFAULT;
  122. else
  123. return 0;
  124. case WDIOC_GETSTATUS:
  125. case WDIOC_GETBOOTSTATUS:
  126. return put_user(0, p);
  127. case WDIOC_KEEPALIVE:
  128. adx_wdt_keepalive(wdt);
  129. return 0;
  130. case WDIOC_SETTIMEOUT:
  131. if (get_user(seconds, p))
  132. return -EFAULT;
  133. adx_wdt_set_timeout(wdt, seconds);
  134. /* fallthrough */
  135. case WDIOC_GETTIMEOUT:
  136. adx_wdt_get_timeout(wdt, &seconds);
  137. return put_user(seconds, p);
  138. case WDIOC_SETOPTIONS:
  139. if (copy_from_user(&options, argp, sizeof(options)))
  140. return -EFAULT;
  141. if (options & WDIOS_DISABLECARD) {
  142. adx_wdt_stop(wdt);
  143. ret = 0;
  144. }
  145. if (options & WDIOS_ENABLECARD) {
  146. adx_wdt_start(wdt);
  147. ret = 0;
  148. }
  149. return ret;
  150. default:
  151. break;
  152. }
  153. return -ENOTTY;
  154. }
  155. static ssize_t adx_wdt_write(struct file *file, const char __user *data,
  156. size_t len, loff_t *ppos)
  157. {
  158. struct adx_wdt *wdt = file->private_data;
  159. if (len)
  160. adx_wdt_keepalive(wdt);
  161. return len;
  162. }
  163. static const struct file_operations adx_wdt_fops = {
  164. .owner = THIS_MODULE,
  165. .llseek = no_llseek,
  166. .open = adx_wdt_open,
  167. .release = adx_wdt_release,
  168. .unlocked_ioctl = adx_wdt_ioctl,
  169. .write = adx_wdt_write,
  170. };
  171. static struct miscdevice adx_wdt_miscdev = {
  172. .minor = WATCHDOG_MINOR,
  173. .name = "watchdog",
  174. .fops = &adx_wdt_fops,
  175. };
  176. static int __devinit adx_wdt_probe(struct platform_device *pdev)
  177. {
  178. struct resource *res;
  179. struct adx_wdt *wdt;
  180. int ret = 0;
  181. u32 ctrl;
  182. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  183. if (!wdt) {
  184. dev_err(&pdev->dev, "cannot allocate WDT structure\n");
  185. return -ENOMEM;
  186. }
  187. spin_lock_init(&wdt->lock);
  188. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  189. if (!res) {
  190. dev_err(&pdev->dev, "cannot obtain I/O memory region\n");
  191. return -ENXIO;
  192. }
  193. res = devm_request_mem_region(&pdev->dev, res->start,
  194. resource_size(res), res->name);
  195. if (!res) {
  196. dev_err(&pdev->dev, "cannot request I/O memory region\n");
  197. return -ENXIO;
  198. }
  199. wdt->base = devm_ioremap_nocache(&pdev->dev, res->start,
  200. resource_size(res));
  201. if (!wdt->base) {
  202. dev_err(&pdev->dev, "cannot remap I/O memory region\n");
  203. return -ENXIO;
  204. }
  205. /* disable watchdog and reboot on timeout */
  206. ctrl = readl(wdt->base + ADX_WDT_CONTROL);
  207. ctrl &= ~ADX_WDT_CONTROL_ENABLE;
  208. ctrl &= ~ADX_WDT_CONTROL_nRESET;
  209. writel(ctrl, wdt->base + ADX_WDT_CONTROL);
  210. platform_set_drvdata(pdev, wdt);
  211. adx_wdt_dev = pdev;
  212. ret = misc_register(&adx_wdt_miscdev);
  213. if (ret) {
  214. dev_err(&pdev->dev, "cannot register miscdev on minor %d "
  215. "(err=%d)\n", WATCHDOG_MINOR, ret);
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. static int __devexit adx_wdt_remove(struct platform_device *pdev)
  221. {
  222. struct adx_wdt *wdt = platform_get_drvdata(pdev);
  223. misc_deregister(&adx_wdt_miscdev);
  224. adx_wdt_stop(wdt);
  225. platform_set_drvdata(pdev, NULL);
  226. return 0;
  227. }
  228. static void adx_wdt_shutdown(struct platform_device *pdev)
  229. {
  230. struct adx_wdt *wdt = platform_get_drvdata(pdev);
  231. adx_wdt_stop(wdt);
  232. }
  233. #ifdef CONFIG_PM
  234. static int adx_wdt_suspend(struct device *dev)
  235. {
  236. struct platform_device *pdev = to_platform_device(dev);
  237. struct adx_wdt *wdt = platform_get_drvdata(pdev);
  238. wdt->wake = (wdt->state == WDT_STATE_START) ? 1 : 0;
  239. adx_wdt_stop(wdt);
  240. return 0;
  241. }
  242. static int adx_wdt_resume(struct device *dev)
  243. {
  244. struct platform_device *pdev = to_platform_device(dev);
  245. struct adx_wdt *wdt = platform_get_drvdata(pdev);
  246. if (wdt->wake)
  247. adx_wdt_start(wdt);
  248. return 0;
  249. }
  250. static const struct dev_pm_ops adx_wdt_pm_ops = {
  251. .suspend = adx_wdt_suspend,
  252. .resume = adx_wdt_resume,
  253. };
  254. # define ADX_WDT_PM_OPS (&adx_wdt_pm_ops)
  255. #else
  256. # define ADX_WDT_PM_OPS NULL
  257. #endif
  258. static struct platform_driver adx_wdt_driver = {
  259. .probe = adx_wdt_probe,
  260. .remove = __devexit_p(adx_wdt_remove),
  261. .shutdown = adx_wdt_shutdown,
  262. .driver = {
  263. .name = WATCHDOG_NAME,
  264. .owner = THIS_MODULE,
  265. .pm = ADX_WDT_PM_OPS,
  266. },
  267. };
  268. static int __init adx_wdt_init(void)
  269. {
  270. return platform_driver_register(&adx_wdt_driver);
  271. }
  272. static void __exit adx_wdt_exit(void)
  273. {
  274. platform_driver_unregister(&adx_wdt_driver);
  275. }
  276. module_init(adx_wdt_init);
  277. module_exit(adx_wdt_exit);
  278. MODULE_DESCRIPTION("Avionic Design Xanthos Watchdog Driver");
  279. MODULE_LICENSE("GPL v2");
  280. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  281. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);