bcm7038_wdt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Broadcom Corporation
  4. *
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/init.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm.h>
  13. #include <linux/watchdog.h>
  14. #define WDT_START_1 0xff00
  15. #define WDT_START_2 0x00ff
  16. #define WDT_STOP_1 0xee00
  17. #define WDT_STOP_2 0x00ee
  18. #define WDT_TIMEOUT_REG 0x0
  19. #define WDT_CMD_REG 0x4
  20. #define WDT_MIN_TIMEOUT 1 /* seconds */
  21. #define WDT_DEFAULT_TIMEOUT 30 /* seconds */
  22. #define WDT_DEFAULT_RATE 27000000
  23. struct bcm7038_watchdog {
  24. void __iomem *base;
  25. struct watchdog_device wdd;
  26. u32 rate;
  27. struct clk *clk;
  28. };
  29. static bool nowayout = WATCHDOG_NOWAYOUT;
  30. static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
  31. {
  32. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  33. u32 timeout;
  34. timeout = wdt->rate * wdog->timeout;
  35. writel(timeout, wdt->base + WDT_TIMEOUT_REG);
  36. }
  37. static int bcm7038_wdt_ping(struct watchdog_device *wdog)
  38. {
  39. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  40. writel(WDT_START_1, wdt->base + WDT_CMD_REG);
  41. writel(WDT_START_2, wdt->base + WDT_CMD_REG);
  42. return 0;
  43. }
  44. static int bcm7038_wdt_start(struct watchdog_device *wdog)
  45. {
  46. bcm7038_wdt_set_timeout_reg(wdog);
  47. bcm7038_wdt_ping(wdog);
  48. return 0;
  49. }
  50. static int bcm7038_wdt_stop(struct watchdog_device *wdog)
  51. {
  52. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  53. writel(WDT_STOP_1, wdt->base + WDT_CMD_REG);
  54. writel(WDT_STOP_2, wdt->base + WDT_CMD_REG);
  55. return 0;
  56. }
  57. static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
  58. unsigned int t)
  59. {
  60. /* Can't modify timeout value if watchdog timer is running */
  61. bcm7038_wdt_stop(wdog);
  62. wdog->timeout = t;
  63. bcm7038_wdt_start(wdog);
  64. return 0;
  65. }
  66. static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
  67. {
  68. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  69. u32 time_left;
  70. time_left = readl(wdt->base + WDT_CMD_REG);
  71. return time_left / wdt->rate;
  72. }
  73. static const struct watchdog_info bcm7038_wdt_info = {
  74. .identity = "Broadcom BCM7038 Watchdog Timer",
  75. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  76. WDIOF_MAGICCLOSE
  77. };
  78. static const struct watchdog_ops bcm7038_wdt_ops = {
  79. .owner = THIS_MODULE,
  80. .start = bcm7038_wdt_start,
  81. .stop = bcm7038_wdt_stop,
  82. .set_timeout = bcm7038_wdt_set_timeout,
  83. .get_timeleft = bcm7038_wdt_get_timeleft,
  84. };
  85. static void bcm7038_clk_disable_unprepare(void *data)
  86. {
  87. clk_disable_unprepare(data);
  88. }
  89. static int bcm7038_wdt_probe(struct platform_device *pdev)
  90. {
  91. struct device *dev = &pdev->dev;
  92. struct bcm7038_watchdog *wdt;
  93. int err;
  94. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  95. if (!wdt)
  96. return -ENOMEM;
  97. platform_set_drvdata(pdev, wdt);
  98. wdt->base = devm_platform_ioremap_resource(pdev, 0);
  99. if (IS_ERR(wdt->base))
  100. return PTR_ERR(wdt->base);
  101. wdt->clk = devm_clk_get(dev, NULL);
  102. /* If unable to get clock, use default frequency */
  103. if (!IS_ERR(wdt->clk)) {
  104. err = clk_prepare_enable(wdt->clk);
  105. if (err)
  106. return err;
  107. err = devm_add_action_or_reset(dev,
  108. bcm7038_clk_disable_unprepare,
  109. wdt->clk);
  110. if (err)
  111. return err;
  112. wdt->rate = clk_get_rate(wdt->clk);
  113. /* Prevent divide-by-zero exception */
  114. if (!wdt->rate)
  115. wdt->rate = WDT_DEFAULT_RATE;
  116. } else {
  117. wdt->rate = WDT_DEFAULT_RATE;
  118. wdt->clk = NULL;
  119. }
  120. wdt->wdd.info = &bcm7038_wdt_info;
  121. wdt->wdd.ops = &bcm7038_wdt_ops;
  122. wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
  123. wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
  124. wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
  125. wdt->wdd.parent = dev;
  126. watchdog_set_drvdata(&wdt->wdd, wdt);
  127. watchdog_stop_on_reboot(&wdt->wdd);
  128. watchdog_stop_on_unregister(&wdt->wdd);
  129. err = devm_watchdog_register_device(dev, &wdt->wdd);
  130. if (err)
  131. return err;
  132. dev_info(dev, "Registered BCM7038 Watchdog\n");
  133. return 0;
  134. }
  135. #ifdef CONFIG_PM_SLEEP
  136. static int bcm7038_wdt_suspend(struct device *dev)
  137. {
  138. struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
  139. if (watchdog_active(&wdt->wdd))
  140. return bcm7038_wdt_stop(&wdt->wdd);
  141. return 0;
  142. }
  143. static int bcm7038_wdt_resume(struct device *dev)
  144. {
  145. struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
  146. if (watchdog_active(&wdt->wdd))
  147. return bcm7038_wdt_start(&wdt->wdd);
  148. return 0;
  149. }
  150. #endif
  151. static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
  152. bcm7038_wdt_resume);
  153. static const struct of_device_id bcm7038_wdt_match[] = {
  154. { .compatible = "brcm,bcm7038-wdt" },
  155. {},
  156. };
  157. MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
  158. static struct platform_driver bcm7038_wdt_driver = {
  159. .probe = bcm7038_wdt_probe,
  160. .driver = {
  161. .name = "bcm7038-wdt",
  162. .of_match_table = bcm7038_wdt_match,
  163. .pm = &bcm7038_wdt_pm_ops,
  164. }
  165. };
  166. module_platform_driver(bcm7038_wdt_driver);
  167. module_param(nowayout, bool, 0);
  168. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  169. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  170. MODULE_LICENSE("GPL");
  171. MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
  172. MODULE_AUTHOR("Justin Chen");