ep93xx_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Watchdog driver for Cirrus Logic EP93xx family of devices.
  3. *
  4. * Copyright (c) 2004 Ray Lehtiniemi
  5. * Copyright (c) 2006 Tower Technologies
  6. * Based on ep93xx driver, bits from alim7101_wdt.c
  7. *
  8. * Authors: Ray Lehtiniemi <rayl@mail.com>,
  9. * Alessandro Zummo <a.zummo@towertech.it>
  10. *
  11. * Copyright (c) 2012 H Hartley Sweeten <hsweeten@visionengravers.com>
  12. * Convert to a platform device and use the watchdog framework API
  13. *
  14. * This file is licensed under the terms of the GNU General Public
  15. * License version 2. This program is licensed "as is" without any
  16. * warranty of any kind, whether express or implied.
  17. *
  18. * This watchdog fires after 250msec, which is a too short interval
  19. * for us to rely on the user space daemon alone. So we ping the
  20. * wdt each ~200msec and eventually stop doing it if the user space
  21. * daemon dies.
  22. *
  23. * TODO:
  24. *
  25. * - Test last reset from watchdog status
  26. * - Add a few missing ioctls
  27. */
  28. #include <linux/platform_device.h>
  29. #include <linux/module.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/watchdog.h>
  32. #include <linux/timer.h>
  33. #include <linux/io.h>
  34. #define WDT_VERSION "0.4"
  35. /* default timeout (secs) */
  36. #define WDT_TIMEOUT 30
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. module_param(nowayout, bool, 0);
  39. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  40. static unsigned int timeout = WDT_TIMEOUT;
  41. module_param(timeout, uint, 0);
  42. MODULE_PARM_DESC(timeout,
  43. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  44. __MODULE_STRING(WDT_TIMEOUT) ")");
  45. static void __iomem *mmio_base;
  46. static struct timer_list timer;
  47. static unsigned long next_heartbeat;
  48. #define EP93XX_WATCHDOG 0x00
  49. #define EP93XX_WDSTATUS 0x04
  50. /* reset the wdt every ~200ms - the heartbeat of the device is 0.250 seconds*/
  51. #define WDT_INTERVAL (HZ/5)
  52. static void ep93xx_wdt_timer_ping(unsigned long data)
  53. {
  54. if (time_before(jiffies, next_heartbeat))
  55. writel(0x5555, mmio_base + EP93XX_WATCHDOG);
  56. /* Re-set the timer interval */
  57. mod_timer(&timer, jiffies + WDT_INTERVAL);
  58. }
  59. static int ep93xx_wdt_start(struct watchdog_device *wdd)
  60. {
  61. next_heartbeat = jiffies + (timeout * HZ);
  62. writel(0xaaaa, mmio_base + EP93XX_WATCHDOG);
  63. mod_timer(&timer, jiffies + WDT_INTERVAL);
  64. return 0;
  65. }
  66. static int ep93xx_wdt_stop(struct watchdog_device *wdd)
  67. {
  68. del_timer_sync(&timer);
  69. writel(0xaa55, mmio_base + EP93XX_WATCHDOG);
  70. return 0;
  71. }
  72. static int ep93xx_wdt_keepalive(struct watchdog_device *wdd)
  73. {
  74. /* user land ping */
  75. next_heartbeat = jiffies + (timeout * HZ);
  76. return 0;
  77. }
  78. static const struct watchdog_info ep93xx_wdt_ident = {
  79. .options = WDIOF_CARDRESET |
  80. WDIOF_MAGICCLOSE |
  81. WDIOF_KEEPALIVEPING,
  82. .identity = "EP93xx Watchdog",
  83. };
  84. static struct watchdog_ops ep93xx_wdt_ops = {
  85. .owner = THIS_MODULE,
  86. .start = ep93xx_wdt_start,
  87. .stop = ep93xx_wdt_stop,
  88. .ping = ep93xx_wdt_keepalive,
  89. };
  90. static struct watchdog_device ep93xx_wdt_wdd = {
  91. .info = &ep93xx_wdt_ident,
  92. .ops = &ep93xx_wdt_ops,
  93. };
  94. static int __devinit ep93xx_wdt_probe(struct platform_device *pdev)
  95. {
  96. struct resource *res;
  97. unsigned long val;
  98. int err;
  99. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  100. if (!res)
  101. return -ENXIO;
  102. if (!devm_request_mem_region(&pdev->dev, res->start,
  103. resource_size(res), pdev->name))
  104. return -EBUSY;
  105. mmio_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  106. if (!mmio_base)
  107. return -ENXIO;
  108. if (timeout < 1 || timeout > 3600) {
  109. timeout = WDT_TIMEOUT;
  110. dev_warn(&pdev->dev,
  111. "timeout value must be 1<=x<=3600, using %d\n",
  112. timeout);
  113. }
  114. val = readl(mmio_base + EP93XX_WATCHDOG);
  115. ep93xx_wdt_wdd.bootstatus = (val & 0x01) ? WDIOF_CARDRESET : 0;
  116. ep93xx_wdt_wdd.timeout = timeout;
  117. watchdog_set_nowayout(&ep93xx_wdt_wdd, nowayout);
  118. setup_timer(&timer, ep93xx_wdt_timer_ping, 1);
  119. err = watchdog_register_device(&ep93xx_wdt_wdd);
  120. if (err)
  121. return err;
  122. dev_info(&pdev->dev,
  123. "EP93XX watchdog, driver version " WDT_VERSION "%s\n",
  124. (val & 0x08) ? " (nCS1 disable detected)" : "");
  125. return 0;
  126. }
  127. static int __devexit ep93xx_wdt_remove(struct platform_device *pdev)
  128. {
  129. watchdog_unregister_device(&ep93xx_wdt_wdd);
  130. return 0;
  131. }
  132. static struct platform_driver ep93xx_wdt_driver = {
  133. .driver = {
  134. .owner = THIS_MODULE,
  135. .name = "ep93xx-wdt",
  136. },
  137. .probe = ep93xx_wdt_probe,
  138. .remove = __devexit_p(ep93xx_wdt_remove),
  139. };
  140. module_platform_driver(ep93xx_wdt_driver);
  141. MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
  142. "Alessandro Zummo <a.zummo@towertech.it>,"
  143. "H Hartley Sweeten <hsweeten@visionengravers.com>");
  144. MODULE_DESCRIPTION("EP93xx Watchdog");
  145. MODULE_LICENSE("GPL");
  146. MODULE_VERSION(WDT_VERSION);
  147. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);