bcm7038_wdt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm.h>
  21. #include <linux/watchdog.h>
  22. #define WDT_START_1 0xff00
  23. #define WDT_START_2 0x00ff
  24. #define WDT_STOP_1 0xee00
  25. #define WDT_STOP_2 0x00ee
  26. #define WDT_TIMEOUT_REG 0x0
  27. #define WDT_CMD_REG 0x4
  28. #define WDT_MIN_TIMEOUT 1 /* seconds */
  29. #define WDT_DEFAULT_TIMEOUT 30 /* seconds */
  30. #define WDT_DEFAULT_RATE 27000000
  31. struct bcm7038_watchdog {
  32. void __iomem *base;
  33. struct watchdog_device wdd;
  34. u32 rate;
  35. struct clk *clk;
  36. };
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
  39. {
  40. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  41. u32 timeout;
  42. timeout = wdt->rate * wdog->timeout;
  43. writel(timeout, wdt->base + WDT_TIMEOUT_REG);
  44. }
  45. static int bcm7038_wdt_ping(struct watchdog_device *wdog)
  46. {
  47. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  48. writel(WDT_START_1, wdt->base + WDT_CMD_REG);
  49. writel(WDT_START_2, wdt->base + WDT_CMD_REG);
  50. return 0;
  51. }
  52. static int bcm7038_wdt_start(struct watchdog_device *wdog)
  53. {
  54. bcm7038_wdt_set_timeout_reg(wdog);
  55. bcm7038_wdt_ping(wdog);
  56. return 0;
  57. }
  58. static int bcm7038_wdt_stop(struct watchdog_device *wdog)
  59. {
  60. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  61. writel(WDT_STOP_1, wdt->base + WDT_CMD_REG);
  62. writel(WDT_STOP_2, wdt->base + WDT_CMD_REG);
  63. return 0;
  64. }
  65. static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
  66. unsigned int t)
  67. {
  68. /* Can't modify timeout value if watchdog timer is running */
  69. bcm7038_wdt_stop(wdog);
  70. wdog->timeout = t;
  71. bcm7038_wdt_start(wdog);
  72. return 0;
  73. }
  74. static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
  75. {
  76. struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
  77. u32 time_left;
  78. time_left = readl(wdt->base + WDT_CMD_REG);
  79. return time_left / wdt->rate;
  80. }
  81. static struct watchdog_info bcm7038_wdt_info = {
  82. .identity = "Broadcom BCM7038 Watchdog Timer",
  83. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  84. WDIOF_MAGICCLOSE
  85. };
  86. static struct watchdog_ops bcm7038_wdt_ops = {
  87. .owner = THIS_MODULE,
  88. .start = bcm7038_wdt_start,
  89. .stop = bcm7038_wdt_stop,
  90. .set_timeout = bcm7038_wdt_set_timeout,
  91. .get_timeleft = bcm7038_wdt_get_timeleft,
  92. };
  93. static int bcm7038_wdt_probe(struct platform_device *pdev)
  94. {
  95. struct device *dev = &pdev->dev;
  96. struct bcm7038_watchdog *wdt;
  97. struct resource *res;
  98. int err;
  99. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  100. if (!wdt)
  101. return -ENOMEM;
  102. platform_set_drvdata(pdev, wdt);
  103. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. wdt->base = devm_ioremap_resource(dev, res);
  105. if (IS_ERR(wdt->base))
  106. return PTR_ERR(wdt->base);
  107. wdt->clk = devm_clk_get(dev, NULL);
  108. /* If unable to get clock, use default frequency */
  109. if (!IS_ERR(wdt->clk)) {
  110. clk_prepare_enable(wdt->clk);
  111. wdt->rate = clk_get_rate(wdt->clk);
  112. /* Prevent divide-by-zero exception */
  113. if (!wdt->rate)
  114. wdt->rate = WDT_DEFAULT_RATE;
  115. } else {
  116. wdt->rate = WDT_DEFAULT_RATE;
  117. wdt->clk = NULL;
  118. }
  119. wdt->wdd.info = &bcm7038_wdt_info;
  120. wdt->wdd.ops = &bcm7038_wdt_ops;
  121. wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
  122. wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
  123. wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
  124. wdt->wdd.parent = dev;
  125. watchdog_set_drvdata(&wdt->wdd, wdt);
  126. err = watchdog_register_device(&wdt->wdd);
  127. if (err) {
  128. dev_err(dev, "Failed to register watchdog device\n");
  129. clk_disable_unprepare(wdt->clk);
  130. return err;
  131. }
  132. dev_info(dev, "Registered BCM7038 Watchdog\n");
  133. return 0;
  134. }
  135. static int bcm7038_wdt_remove(struct platform_device *pdev)
  136. {
  137. struct bcm7038_watchdog *wdt = platform_get_drvdata(pdev);
  138. if (!nowayout)
  139. bcm7038_wdt_stop(&wdt->wdd);
  140. watchdog_unregister_device(&wdt->wdd);
  141. clk_disable_unprepare(wdt->clk);
  142. return 0;
  143. }
  144. #ifdef CONFIG_PM_SLEEP
  145. static int bcm7038_wdt_suspend(struct device *dev)
  146. {
  147. struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
  148. if (watchdog_active(&wdt->wdd))
  149. return bcm7038_wdt_stop(&wdt->wdd);
  150. return 0;
  151. }
  152. static int bcm7038_wdt_resume(struct device *dev)
  153. {
  154. struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
  155. if (watchdog_active(&wdt->wdd))
  156. return bcm7038_wdt_start(&wdt->wdd);
  157. return 0;
  158. }
  159. #endif
  160. static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
  161. bcm7038_wdt_resume);
  162. static void bcm7038_wdt_shutdown(struct platform_device *pdev)
  163. {
  164. struct bcm7038_watchdog *wdt = platform_get_drvdata(pdev);
  165. if (watchdog_active(&wdt->wdd))
  166. bcm7038_wdt_stop(&wdt->wdd);
  167. }
  168. static const struct of_device_id bcm7038_wdt_match[] = {
  169. { .compatible = "brcm,bcm7038-wdt" },
  170. {},
  171. };
  172. static struct platform_driver bcm7038_wdt_driver = {
  173. .probe = bcm7038_wdt_probe,
  174. .remove = bcm7038_wdt_remove,
  175. .shutdown = bcm7038_wdt_shutdown,
  176. .driver = {
  177. .name = "bcm7038-wdt",
  178. .of_match_table = bcm7038_wdt_match,
  179. .pm = &bcm7038_wdt_pm_ops,
  180. }
  181. };
  182. module_platform_driver(bcm7038_wdt_driver);
  183. module_param(nowayout, bool, 0);
  184. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  185. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  186. MODULE_LICENSE("GPL v2");
  187. MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
  188. MODULE_AUTHOR("Justin Chen");