ts72xx_wdt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Watchdog driver for Technologic Systems TS-72xx based SBCs
  4. * (TS-7200, TS-7250 and TS-7260). These boards have external
  5. * glue logic CPLD chip, which includes programmable watchdog
  6. * timer.
  7. *
  8. * Copyright (c) 2009 Mika Westerberg <mika.westerberg@iki.fi>
  9. *
  10. * This driver is based on ep93xx_wdt and wm831x_wdt drivers.
  11. *
  12. */
  13. #include <linux/platform_device.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/module.h>
  16. #include <linux/watchdog.h>
  17. #include <linux/io.h>
  18. #define TS72XX_WDT_DEFAULT_TIMEOUT 30
  19. static int timeout;
  20. module_param(timeout, int, 0);
  21. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds.");
  22. static bool nowayout = WATCHDOG_NOWAYOUT;
  23. module_param(nowayout, bool, 0);
  24. MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
  25. /* priv->control_reg */
  26. #define TS72XX_WDT_CTRL_DISABLE 0x00
  27. #define TS72XX_WDT_CTRL_250MS 0x01
  28. #define TS72XX_WDT_CTRL_500MS 0x02
  29. #define TS72XX_WDT_CTRL_1SEC 0x03
  30. #define TS72XX_WDT_CTRL_RESERVED 0x04
  31. #define TS72XX_WDT_CTRL_2SEC 0x05
  32. #define TS72XX_WDT_CTRL_4SEC 0x06
  33. #define TS72XX_WDT_CTRL_8SEC 0x07
  34. /* priv->feed_reg */
  35. #define TS72XX_WDT_FEED_VAL 0x05
  36. struct ts72xx_wdt_priv {
  37. void __iomem *control_reg;
  38. void __iomem *feed_reg;
  39. struct watchdog_device wdd;
  40. unsigned char regval;
  41. };
  42. static int ts72xx_wdt_start(struct watchdog_device *wdd)
  43. {
  44. struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
  45. writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
  46. writeb(priv->regval, priv->control_reg);
  47. return 0;
  48. }
  49. static int ts72xx_wdt_stop(struct watchdog_device *wdd)
  50. {
  51. struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
  52. writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
  53. writeb(TS72XX_WDT_CTRL_DISABLE, priv->control_reg);
  54. return 0;
  55. }
  56. static int ts72xx_wdt_ping(struct watchdog_device *wdd)
  57. {
  58. struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
  59. writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
  60. return 0;
  61. }
  62. static int ts72xx_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
  63. {
  64. struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
  65. if (to == 1) {
  66. priv->regval = TS72XX_WDT_CTRL_1SEC;
  67. } else if (to == 2) {
  68. priv->regval = TS72XX_WDT_CTRL_2SEC;
  69. } else if (to <= 4) {
  70. priv->regval = TS72XX_WDT_CTRL_4SEC;
  71. to = 4;
  72. } else {
  73. priv->regval = TS72XX_WDT_CTRL_8SEC;
  74. if (to <= 8)
  75. to = 8;
  76. }
  77. wdd->timeout = to;
  78. if (watchdog_active(wdd)) {
  79. ts72xx_wdt_stop(wdd);
  80. ts72xx_wdt_start(wdd);
  81. }
  82. return 0;
  83. }
  84. static const struct watchdog_info ts72xx_wdt_ident = {
  85. .options = WDIOF_KEEPALIVEPING |
  86. WDIOF_SETTIMEOUT |
  87. WDIOF_MAGICCLOSE,
  88. .firmware_version = 1,
  89. .identity = "TS-72XX WDT",
  90. };
  91. static const struct watchdog_ops ts72xx_wdt_ops = {
  92. .owner = THIS_MODULE,
  93. .start = ts72xx_wdt_start,
  94. .stop = ts72xx_wdt_stop,
  95. .ping = ts72xx_wdt_ping,
  96. .set_timeout = ts72xx_wdt_settimeout,
  97. };
  98. static int ts72xx_wdt_probe(struct platform_device *pdev)
  99. {
  100. struct device *dev = &pdev->dev;
  101. struct ts72xx_wdt_priv *priv;
  102. struct watchdog_device *wdd;
  103. int ret;
  104. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  105. if (!priv)
  106. return -ENOMEM;
  107. priv->control_reg = devm_platform_ioremap_resource(pdev, 0);
  108. if (IS_ERR(priv->control_reg))
  109. return PTR_ERR(priv->control_reg);
  110. priv->feed_reg = devm_platform_ioremap_resource(pdev, 1);
  111. if (IS_ERR(priv->feed_reg))
  112. return PTR_ERR(priv->feed_reg);
  113. wdd = &priv->wdd;
  114. wdd->info = &ts72xx_wdt_ident;
  115. wdd->ops = &ts72xx_wdt_ops;
  116. wdd->min_timeout = 1;
  117. wdd->max_hw_heartbeat_ms = 8000;
  118. wdd->parent = dev;
  119. watchdog_set_nowayout(wdd, nowayout);
  120. wdd->timeout = TS72XX_WDT_DEFAULT_TIMEOUT;
  121. watchdog_init_timeout(wdd, timeout, dev);
  122. watchdog_set_drvdata(wdd, priv);
  123. ret = devm_watchdog_register_device(dev, wdd);
  124. if (ret)
  125. return ret;
  126. dev_info(dev, "TS-72xx Watchdog driver\n");
  127. return 0;
  128. }
  129. static const struct of_device_id ts72xx_wdt_of_ids[] = {
  130. { .compatible = "technologic,ts7200-wdt" },
  131. { /* sentinel */ }
  132. };
  133. MODULE_DEVICE_TABLE(of, ts72xx_wdt_of_ids);
  134. static struct platform_driver ts72xx_wdt_driver = {
  135. .probe = ts72xx_wdt_probe,
  136. .driver = {
  137. .name = "ts72xx-wdt",
  138. .of_match_table = ts72xx_wdt_of_ids,
  139. },
  140. };
  141. module_platform_driver(ts72xx_wdt_driver);
  142. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  143. MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
  144. MODULE_LICENSE("GPL");
  145. MODULE_ALIAS("platform:ts72xx-wdt");