ftwdt010_wdt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Watchdog driver for Faraday Technology FTWDT010
  4. *
  5. * Copyright (C) 2017 Linus Walleij <[email protected]>
  6. *
  7. * Inspired by the out-of-tree drivers from OpenWRT:
  8. * Copyright (C) 2009 Paulius Zaleckas <[email protected]>
  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 void ftwdt010_enable(struct ftwdt010_wdt *gwdt,
  43. unsigned int timeout,
  44. bool need_irq)
  45. {
  46. u32 enable;
  47. writel(timeout * WDT_CLOCK, gwdt->base + FTWDT010_WDLOAD);
  48. writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
  49. /* set clock before enabling */
  50. enable = WDCR_CLOCK_5MHZ | WDCR_SYS_RST;
  51. writel(enable, gwdt->base + FTWDT010_WDCR);
  52. if (need_irq)
  53. enable |= WDCR_WDINTR;
  54. enable |= WDCR_ENABLE;
  55. writel(enable, gwdt->base + FTWDT010_WDCR);
  56. }
  57. static int ftwdt010_wdt_start(struct watchdog_device *wdd)
  58. {
  59. struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
  60. ftwdt010_enable(gwdt, wdd->timeout, gwdt->has_irq);
  61. return 0;
  62. }
  63. static int ftwdt010_wdt_stop(struct watchdog_device *wdd)
  64. {
  65. struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
  66. writel(0, gwdt->base + FTWDT010_WDCR);
  67. return 0;
  68. }
  69. static int ftwdt010_wdt_ping(struct watchdog_device *wdd)
  70. {
  71. struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
  72. writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
  73. return 0;
  74. }
  75. static int ftwdt010_wdt_set_timeout(struct watchdog_device *wdd,
  76. unsigned int timeout)
  77. {
  78. wdd->timeout = timeout;
  79. if (watchdog_active(wdd))
  80. ftwdt010_wdt_start(wdd);
  81. return 0;
  82. }
  83. static int ftwdt010_wdt_restart(struct watchdog_device *wdd,
  84. unsigned long action, void *data)
  85. {
  86. ftwdt010_enable(to_ftwdt010_wdt(wdd), 0, false);
  87. return 0;
  88. }
  89. static irqreturn_t ftwdt010_wdt_interrupt(int irq, void *data)
  90. {
  91. struct ftwdt010_wdt *gwdt = data;
  92. watchdog_notify_pretimeout(&gwdt->wdd);
  93. return IRQ_HANDLED;
  94. }
  95. static const struct watchdog_ops ftwdt010_wdt_ops = {
  96. .start = ftwdt010_wdt_start,
  97. .stop = ftwdt010_wdt_stop,
  98. .ping = ftwdt010_wdt_ping,
  99. .set_timeout = ftwdt010_wdt_set_timeout,
  100. .restart = ftwdt010_wdt_restart,
  101. .owner = THIS_MODULE,
  102. };
  103. static const struct watchdog_info ftwdt010_wdt_info = {
  104. .options = WDIOF_KEEPALIVEPING
  105. | WDIOF_MAGICCLOSE
  106. | WDIOF_SETTIMEOUT,
  107. .identity = KBUILD_MODNAME,
  108. };
  109. static int ftwdt010_wdt_probe(struct platform_device *pdev)
  110. {
  111. struct device *dev = &pdev->dev;
  112. struct ftwdt010_wdt *gwdt;
  113. unsigned int reg;
  114. int irq;
  115. int ret;
  116. gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
  117. if (!gwdt)
  118. return -ENOMEM;
  119. gwdt->base = devm_platform_ioremap_resource(pdev, 0);
  120. if (IS_ERR(gwdt->base))
  121. return PTR_ERR(gwdt->base);
  122. gwdt->dev = dev;
  123. gwdt->wdd.info = &ftwdt010_wdt_info;
  124. gwdt->wdd.ops = &ftwdt010_wdt_ops;
  125. gwdt->wdd.min_timeout = 1;
  126. gwdt->wdd.max_timeout = 0xFFFFFFFF / WDT_CLOCK;
  127. gwdt->wdd.parent = dev;
  128. /*
  129. * If 'timeout-sec' unspecified in devicetree, assume a 13 second
  130. * default.
  131. */
  132. gwdt->wdd.timeout = 13U;
  133. watchdog_init_timeout(&gwdt->wdd, 0, dev);
  134. reg = readw(gwdt->base + FTWDT010_WDCR);
  135. if (reg & WDCR_ENABLE) {
  136. /* Watchdog was enabled by the bootloader, disable it. */
  137. reg &= ~WDCR_ENABLE;
  138. writel(reg, gwdt->base + FTWDT010_WDCR);
  139. }
  140. irq = platform_get_irq(pdev, 0);
  141. if (irq > 0) {
  142. ret = devm_request_irq(dev, irq, ftwdt010_wdt_interrupt, 0,
  143. "watchdog bark", gwdt);
  144. if (ret)
  145. return ret;
  146. gwdt->has_irq = true;
  147. }
  148. ret = devm_watchdog_register_device(dev, &gwdt->wdd);
  149. if (ret)
  150. return ret;
  151. /* Set up platform driver data */
  152. platform_set_drvdata(pdev, gwdt);
  153. dev_info(dev, "FTWDT010 watchdog driver enabled\n");
  154. return 0;
  155. }
  156. static int __maybe_unused ftwdt010_wdt_suspend(struct device *dev)
  157. {
  158. struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
  159. unsigned int reg;
  160. reg = readw(gwdt->base + FTWDT010_WDCR);
  161. reg &= ~WDCR_ENABLE;
  162. writel(reg, gwdt->base + FTWDT010_WDCR);
  163. return 0;
  164. }
  165. static int __maybe_unused ftwdt010_wdt_resume(struct device *dev)
  166. {
  167. struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
  168. unsigned int reg;
  169. if (watchdog_active(&gwdt->wdd)) {
  170. reg = readw(gwdt->base + FTWDT010_WDCR);
  171. reg |= WDCR_ENABLE;
  172. writel(reg, gwdt->base + FTWDT010_WDCR);
  173. }
  174. return 0;
  175. }
  176. static const struct dev_pm_ops ftwdt010_wdt_dev_pm_ops = {
  177. SET_SYSTEM_SLEEP_PM_OPS(ftwdt010_wdt_suspend,
  178. ftwdt010_wdt_resume)
  179. };
  180. #ifdef CONFIG_OF
  181. static const struct of_device_id ftwdt010_wdt_match[] = {
  182. { .compatible = "faraday,ftwdt010" },
  183. { .compatible = "cortina,gemini-watchdog" },
  184. {},
  185. };
  186. MODULE_DEVICE_TABLE(of, ftwdt010_wdt_match);
  187. #endif
  188. static struct platform_driver ftwdt010_wdt_driver = {
  189. .probe = ftwdt010_wdt_probe,
  190. .driver = {
  191. .name = "ftwdt010-wdt",
  192. .of_match_table = of_match_ptr(ftwdt010_wdt_match),
  193. .pm = &ftwdt010_wdt_dev_pm_ops,
  194. },
  195. };
  196. module_platform_driver(ftwdt010_wdt_driver);
  197. MODULE_AUTHOR("Linus Walleij");
  198. MODULE_DESCRIPTION("Watchdog driver for Faraday Technology FTWDT010");
  199. MODULE_LICENSE("GPL");