ftwdt010_wdt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Watchdog driver for Faraday Technology FTWDT010
  4. *
  5. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  6. *
  7. * Inspired by the out-of-tree drivers from OpenWRT:
  8. * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/watchdog.h>
  20. #define FTWDT010_WDCOUNTER 0x0
  21. #define FTWDT010_WDLOAD 0x4
  22. #define FTWDT010_WDRESTART 0x8
  23. #define FTWDT010_WDCR 0xC
  24. #define WDRESTART_MAGIC 0x5AB9
  25. #define WDCR_CLOCK_5MHZ BIT(4)
  26. #define WDCR_WDEXT BIT(3)
  27. #define WDCR_WDINTR BIT(2)
  28. #define WDCR_SYS_RST BIT(1)
  29. #define WDCR_ENABLE BIT(0)
  30. #define WDT_CLOCK 5000000 /* 5 MHz */
  31. struct ftwdt010_wdt {
  32. struct watchdog_device wdd;
  33. struct device *dev;
  34. void __iomem *base;
  35. bool has_irq;
  36. };
  37. static inline
  38. struct ftwdt010_wdt *to_ftwdt010_wdt(struct watchdog_device *wdd)
  39. {
  40. return container_of(wdd, struct ftwdt010_wdt, wdd);
  41. }
  42. static int ftwdt010_wdt_start(struct watchdog_device *wdd)
  43. {
  44. struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
  45. u32 enable;
  46. writel(wdd->timeout * WDT_CLOCK, gwdt->base + FTWDT010_WDLOAD);
  47. writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
  48. /* set clock before enabling */
  49. enable = WDCR_CLOCK_5MHZ | WDCR_SYS_RST;
  50. writel(enable, gwdt->base + FTWDT010_WDCR);
  51. if (gwdt->has_irq)
  52. enable |= WDCR_WDINTR;
  53. enable |= WDCR_ENABLE;
  54. writel(enable, gwdt->base + FTWDT010_WDCR);
  55. return 0;
  56. }
  57. static int ftwdt010_wdt_stop(struct watchdog_device *wdd)
  58. {
  59. struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
  60. writel(0, gwdt->base + FTWDT010_WDCR);
  61. return 0;
  62. }
  63. static int ftwdt010_wdt_ping(struct watchdog_device *wdd)
  64. {
  65. struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
  66. writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
  67. return 0;
  68. }
  69. static int ftwdt010_wdt_set_timeout(struct watchdog_device *wdd,
  70. unsigned int timeout)
  71. {
  72. wdd->timeout = timeout;
  73. if (watchdog_active(wdd))
  74. ftwdt010_wdt_start(wdd);
  75. return 0;
  76. }
  77. static irqreturn_t ftwdt010_wdt_interrupt(int irq, void *data)
  78. {
  79. struct ftwdt010_wdt *gwdt = data;
  80. watchdog_notify_pretimeout(&gwdt->wdd);
  81. return IRQ_HANDLED;
  82. }
  83. static const struct watchdog_ops ftwdt010_wdt_ops = {
  84. .start = ftwdt010_wdt_start,
  85. .stop = ftwdt010_wdt_stop,
  86. .ping = ftwdt010_wdt_ping,
  87. .set_timeout = ftwdt010_wdt_set_timeout,
  88. .owner = THIS_MODULE,
  89. };
  90. static const struct watchdog_info ftwdt010_wdt_info = {
  91. .options = WDIOF_KEEPALIVEPING
  92. | WDIOF_MAGICCLOSE
  93. | WDIOF_SETTIMEOUT,
  94. .identity = KBUILD_MODNAME,
  95. };
  96. static int ftwdt010_wdt_probe(struct platform_device *pdev)
  97. {
  98. struct device *dev = &pdev->dev;
  99. struct ftwdt010_wdt *gwdt;
  100. unsigned int reg;
  101. int irq;
  102. int ret;
  103. gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
  104. if (!gwdt)
  105. return -ENOMEM;
  106. gwdt->base = devm_platform_ioremap_resource(pdev, 0);
  107. if (IS_ERR(gwdt->base))
  108. return PTR_ERR(gwdt->base);
  109. gwdt->dev = dev;
  110. gwdt->wdd.info = &ftwdt010_wdt_info;
  111. gwdt->wdd.ops = &ftwdt010_wdt_ops;
  112. gwdt->wdd.min_timeout = 1;
  113. gwdt->wdd.max_timeout = 0xFFFFFFFF / WDT_CLOCK;
  114. gwdt->wdd.parent = dev;
  115. /*
  116. * If 'timeout-sec' unspecified in devicetree, assume a 13 second
  117. * default.
  118. */
  119. gwdt->wdd.timeout = 13U;
  120. watchdog_init_timeout(&gwdt->wdd, 0, dev);
  121. reg = readw(gwdt->base + FTWDT010_WDCR);
  122. if (reg & WDCR_ENABLE) {
  123. /* Watchdog was enabled by the bootloader, disable it. */
  124. reg &= ~WDCR_ENABLE;
  125. writel(reg, gwdt->base + FTWDT010_WDCR);
  126. }
  127. irq = platform_get_irq(pdev, 0);
  128. if (irq) {
  129. ret = devm_request_irq(dev, irq, ftwdt010_wdt_interrupt, 0,
  130. "watchdog bark", gwdt);
  131. if (ret)
  132. return ret;
  133. gwdt->has_irq = true;
  134. }
  135. ret = devm_watchdog_register_device(dev, &gwdt->wdd);
  136. if (ret)
  137. return ret;
  138. /* Set up platform driver data */
  139. platform_set_drvdata(pdev, gwdt);
  140. dev_info(dev, "FTWDT010 watchdog driver enabled\n");
  141. return 0;
  142. }
  143. static int __maybe_unused ftwdt010_wdt_suspend(struct device *dev)
  144. {
  145. struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
  146. unsigned int reg;
  147. reg = readw(gwdt->base + FTWDT010_WDCR);
  148. reg &= ~WDCR_ENABLE;
  149. writel(reg, gwdt->base + FTWDT010_WDCR);
  150. return 0;
  151. }
  152. static int __maybe_unused ftwdt010_wdt_resume(struct device *dev)
  153. {
  154. struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
  155. unsigned int reg;
  156. if (watchdog_active(&gwdt->wdd)) {
  157. reg = readw(gwdt->base + FTWDT010_WDCR);
  158. reg |= WDCR_ENABLE;
  159. writel(reg, gwdt->base + FTWDT010_WDCR);
  160. }
  161. return 0;
  162. }
  163. static const struct dev_pm_ops ftwdt010_wdt_dev_pm_ops = {
  164. SET_SYSTEM_SLEEP_PM_OPS(ftwdt010_wdt_suspend,
  165. ftwdt010_wdt_resume)
  166. };
  167. #ifdef CONFIG_OF
  168. static const struct of_device_id ftwdt010_wdt_match[] = {
  169. { .compatible = "faraday,ftwdt010" },
  170. { .compatible = "cortina,gemini-watchdog" },
  171. {},
  172. };
  173. MODULE_DEVICE_TABLE(of, ftwdt010_wdt_match);
  174. #endif
  175. static struct platform_driver ftwdt010_wdt_driver = {
  176. .probe = ftwdt010_wdt_probe,
  177. .driver = {
  178. .name = "ftwdt010-wdt",
  179. .of_match_table = of_match_ptr(ftwdt010_wdt_match),
  180. .pm = &ftwdt010_wdt_dev_pm_ops,
  181. },
  182. };
  183. module_platform_driver(ftwdt010_wdt_driver);
  184. MODULE_AUTHOR("Linus Walleij");
  185. MODULE_DESCRIPTION("Watchdog driver for Faraday Technology FTWDT010");
  186. MODULE_LICENSE("GPL");