ftwdt010_wdt.c 5.5 KB

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