ep93xx_wdt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/watchdog.h>
  31. #include <linux/timer.h>
  32. #include <linux/io.h>
  33. #define WDT_VERSION "0.4"
  34. /* default timeout (secs) */
  35. #define WDT_TIMEOUT 30
  36. static bool nowayout = WATCHDOG_NOWAYOUT;
  37. module_param(nowayout, bool, 0);
  38. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  39. static unsigned int timeout = WDT_TIMEOUT;
  40. module_param(timeout, uint, 0);
  41. MODULE_PARM_DESC(timeout,
  42. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  43. __MODULE_STRING(WDT_TIMEOUT) ")");
  44. static void __iomem *mmio_base;
  45. static struct timer_list timer;
  46. static unsigned long next_heartbeat;
  47. #define EP93XX_WATCHDOG 0x00
  48. #define EP93XX_WDSTATUS 0x04
  49. /* reset the wdt every ~200ms - the heartbeat of the device is 0.250 seconds*/
  50. #define WDT_INTERVAL (HZ/5)
  51. static void ep93xx_wdt_timer_ping(unsigned long data)
  52. {
  53. if (time_before(jiffies, next_heartbeat))
  54. writel(0x5555, mmio_base + EP93XX_WATCHDOG);
  55. /* Re-set the timer interval */
  56. mod_timer(&timer, jiffies + WDT_INTERVAL);
  57. }
  58. static int ep93xx_wdt_start(struct watchdog_device *wdd)
  59. {
  60. next_heartbeat = jiffies + (timeout * HZ);
  61. writel(0xaaaa, mmio_base + EP93XX_WATCHDOG);
  62. mod_timer(&timer, jiffies + WDT_INTERVAL);
  63. return 0;
  64. }
  65. static int ep93xx_wdt_stop(struct watchdog_device *wdd)
  66. {
  67. del_timer_sync(&timer);
  68. writel(0xaa55, mmio_base + EP93XX_WATCHDOG);
  69. return 0;
  70. }
  71. static int ep93xx_wdt_keepalive(struct watchdog_device *wdd)
  72. {
  73. /* user land ping */
  74. next_heartbeat = jiffies + (timeout * HZ);
  75. return 0;
  76. }
  77. static const struct watchdog_info ep93xx_wdt_ident = {
  78. .options = WDIOF_CARDRESET |
  79. WDIOF_MAGICCLOSE |
  80. WDIOF_KEEPALIVEPING,
  81. .identity = "EP93xx Watchdog",
  82. };
  83. static struct watchdog_ops ep93xx_wdt_ops = {
  84. .owner = THIS_MODULE,
  85. .start = ep93xx_wdt_start,
  86. .stop = ep93xx_wdt_stop,
  87. .ping = ep93xx_wdt_keepalive,
  88. };
  89. static struct watchdog_device ep93xx_wdt_wdd = {
  90. .info = &ep93xx_wdt_ident,
  91. .ops = &ep93xx_wdt_ops,
  92. };
  93. static int ep93xx_wdt_probe(struct platform_device *pdev)
  94. {
  95. struct resource *res;
  96. unsigned long val;
  97. int err;
  98. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  99. mmio_base = devm_ioremap_resource(&pdev->dev, res);
  100. if (IS_ERR(mmio_base))
  101. return PTR_ERR(mmio_base);
  102. if (timeout < 1 || timeout > 3600) {
  103. timeout = WDT_TIMEOUT;
  104. dev_warn(&pdev->dev,
  105. "timeout value must be 1<=x<=3600, using %d\n",
  106. timeout);
  107. }
  108. val = readl(mmio_base + EP93XX_WATCHDOG);
  109. ep93xx_wdt_wdd.bootstatus = (val & 0x01) ? WDIOF_CARDRESET : 0;
  110. ep93xx_wdt_wdd.timeout = timeout;
  111. ep93xx_wdt_wdd.parent = &pdev->dev;
  112. watchdog_set_nowayout(&ep93xx_wdt_wdd, nowayout);
  113. setup_timer(&timer, ep93xx_wdt_timer_ping, 1);
  114. err = watchdog_register_device(&ep93xx_wdt_wdd);
  115. if (err)
  116. return err;
  117. dev_info(&pdev->dev,
  118. "EP93XX watchdog, driver version " WDT_VERSION "%s\n",
  119. (val & 0x08) ? " (nCS1 disable detected)" : "");
  120. return 0;
  121. }
  122. static int ep93xx_wdt_remove(struct platform_device *pdev)
  123. {
  124. watchdog_unregister_device(&ep93xx_wdt_wdd);
  125. return 0;
  126. }
  127. static struct platform_driver ep93xx_wdt_driver = {
  128. .driver = {
  129. .name = "ep93xx-wdt",
  130. },
  131. .probe = ep93xx_wdt_probe,
  132. .remove = ep93xx_wdt_remove,
  133. };
  134. module_platform_driver(ep93xx_wdt_driver);
  135. MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>");
  136. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  137. MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
  138. MODULE_DESCRIPTION("EP93xx Watchdog");
  139. MODULE_LICENSE("GPL");
  140. MODULE_VERSION(WDT_VERSION);