tqmx86_wdt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Watchdog driver for TQMx86 PLD.
  4. *
  5. * The watchdog supports power of 2 timeouts from 1 to 4096sec.
  6. * Once started, it cannot be stopped.
  7. *
  8. * Based on the vendor code written by Vadim V.Vlasov
  9. * <vvlasov@dev.rtsoft.ru>
  10. */
  11. #include <linux/io.h>
  12. #include <linux/log2.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/timer.h>
  16. #include <linux/watchdog.h>
  17. /* default timeout (secs) */
  18. #define WDT_TIMEOUT 32
  19. static unsigned int timeout;
  20. module_param(timeout, uint, 0);
  21. MODULE_PARM_DESC(timeout,
  22. "Watchdog timeout in seconds. (1<=timeout<=4096, default="
  23. __MODULE_STRING(WDT_TIMEOUT) ")");
  24. struct tqmx86_wdt {
  25. struct watchdog_device wdd;
  26. void __iomem *io_base;
  27. };
  28. #define TQMX86_WDCFG 0x00 /* Watchdog Configuration Register */
  29. #define TQMX86_WDCS 0x01 /* Watchdog Config/Status Register */
  30. static int tqmx86_wdt_start(struct watchdog_device *wdd)
  31. {
  32. struct tqmx86_wdt *priv = watchdog_get_drvdata(wdd);
  33. iowrite8(0x81, priv->io_base + TQMX86_WDCS);
  34. return 0;
  35. }
  36. static int tqmx86_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
  37. {
  38. struct tqmx86_wdt *priv = watchdog_get_drvdata(wdd);
  39. u8 val;
  40. t = roundup_pow_of_two(t);
  41. val = ilog2(t) | 0x90;
  42. val += 3; /* values 0,1,2 correspond to 0.125,0.25,0.5s timeouts */
  43. iowrite8(val, priv->io_base + TQMX86_WDCFG);
  44. wdd->timeout = t;
  45. return 0;
  46. }
  47. static const struct watchdog_info tqmx86_wdt_info = {
  48. .options = WDIOF_SETTIMEOUT |
  49. WDIOF_KEEPALIVEPING,
  50. .identity = "TQMx86 Watchdog",
  51. };
  52. static struct watchdog_ops tqmx86_wdt_ops = {
  53. .owner = THIS_MODULE,
  54. .start = tqmx86_wdt_start,
  55. .set_timeout = tqmx86_wdt_set_timeout,
  56. };
  57. static int tqmx86_wdt_probe(struct platform_device *pdev)
  58. {
  59. struct device *dev = &pdev->dev;
  60. struct tqmx86_wdt *priv;
  61. struct resource *res;
  62. int err;
  63. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  64. if (!priv)
  65. return -ENOMEM;
  66. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  67. if (!res)
  68. return -ENODEV;
  69. priv->io_base = devm_ioport_map(dev, res->start, resource_size(res));
  70. if (!priv->io_base)
  71. return -ENOMEM;
  72. watchdog_set_drvdata(&priv->wdd, priv);
  73. priv->wdd.parent = dev;
  74. priv->wdd.info = &tqmx86_wdt_info;
  75. priv->wdd.ops = &tqmx86_wdt_ops;
  76. priv->wdd.min_timeout = 1;
  77. priv->wdd.max_timeout = 4096;
  78. priv->wdd.max_hw_heartbeat_ms = 4096*1000;
  79. priv->wdd.timeout = WDT_TIMEOUT;
  80. watchdog_init_timeout(&priv->wdd, timeout, dev);
  81. watchdog_set_nowayout(&priv->wdd, WATCHDOG_NOWAYOUT);
  82. tqmx86_wdt_set_timeout(&priv->wdd, priv->wdd.timeout);
  83. err = devm_watchdog_register_device(dev, &priv->wdd);
  84. if (err)
  85. return err;
  86. dev_info(dev, "TQMx86 watchdog\n");
  87. return 0;
  88. }
  89. static struct platform_driver tqmx86_wdt_driver = {
  90. .driver = {
  91. .name = "tqmx86-wdt",
  92. },
  93. .probe = tqmx86_wdt_probe,
  94. };
  95. module_platform_driver(tqmx86_wdt_driver);
  96. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  97. MODULE_DESCRIPTION("TQMx86 Watchdog");
  98. MODULE_ALIAS("platform:tqmx86-wdt");
  99. MODULE_LICENSE("GPL");