meson_gxbb_wdt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (c) 2016 BayLibre, SAS.
  8. * Author: Neil Armstrong <narmstrong@baylibre.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called COPYING.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright (c) 2016 BayLibre, SAS.
  27. * Author: Neil Armstrong <narmstrong@baylibre.com>
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <linux/clk.h>
  56. #include <linux/err.h>
  57. #include <linux/io.h>
  58. #include <linux/module.h>
  59. #include <linux/of.h>
  60. #include <linux/platform_device.h>
  61. #include <linux/slab.h>
  62. #include <linux/types.h>
  63. #include <linux/watchdog.h>
  64. #define DEFAULT_TIMEOUT 30 /* seconds */
  65. #define GXBB_WDT_CTRL_REG 0x0
  66. #define GXBB_WDT_TCNT_REG 0x8
  67. #define GXBB_WDT_RSET_REG 0xc
  68. #define GXBB_WDT_CTRL_CLKDIV_EN BIT(25)
  69. #define GXBB_WDT_CTRL_CLK_EN BIT(24)
  70. #define GXBB_WDT_CTRL_EE_RESET BIT(21)
  71. #define GXBB_WDT_CTRL_EN BIT(18)
  72. #define GXBB_WDT_CTRL_DIV_MASK (BIT(18) - 1)
  73. #define GXBB_WDT_TCNT_SETUP_MASK (BIT(16) - 1)
  74. #define GXBB_WDT_TCNT_CNT_SHIFT 16
  75. struct meson_gxbb_wdt {
  76. void __iomem *reg_base;
  77. struct watchdog_device wdt_dev;
  78. struct clk *clk;
  79. };
  80. static int meson_gxbb_wdt_start(struct watchdog_device *wdt_dev)
  81. {
  82. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  83. writel(readl(data->reg_base + GXBB_WDT_CTRL_REG) | GXBB_WDT_CTRL_EN,
  84. data->reg_base + GXBB_WDT_CTRL_REG);
  85. return 0;
  86. }
  87. static int meson_gxbb_wdt_stop(struct watchdog_device *wdt_dev)
  88. {
  89. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  90. writel(readl(data->reg_base + GXBB_WDT_CTRL_REG) & ~GXBB_WDT_CTRL_EN,
  91. data->reg_base + GXBB_WDT_CTRL_REG);
  92. return 0;
  93. }
  94. static int meson_gxbb_wdt_ping(struct watchdog_device *wdt_dev)
  95. {
  96. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  97. writel(0, data->reg_base + GXBB_WDT_RSET_REG);
  98. return 0;
  99. }
  100. static int meson_gxbb_wdt_set_timeout(struct watchdog_device *wdt_dev,
  101. unsigned int timeout)
  102. {
  103. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  104. unsigned long tcnt = timeout * 1000;
  105. if (tcnt > GXBB_WDT_TCNT_SETUP_MASK)
  106. tcnt = GXBB_WDT_TCNT_SETUP_MASK;
  107. wdt_dev->timeout = timeout;
  108. meson_gxbb_wdt_ping(wdt_dev);
  109. writel(tcnt, data->reg_base + GXBB_WDT_TCNT_REG);
  110. return 0;
  111. }
  112. static unsigned int meson_gxbb_wdt_get_timeleft(struct watchdog_device *wdt_dev)
  113. {
  114. struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
  115. unsigned long reg;
  116. reg = readl(data->reg_base + GXBB_WDT_TCNT_REG);
  117. return ((reg >> GXBB_WDT_TCNT_CNT_SHIFT) -
  118. (reg & GXBB_WDT_TCNT_SETUP_MASK)) / 1000;
  119. }
  120. static const struct watchdog_ops meson_gxbb_wdt_ops = {
  121. .start = meson_gxbb_wdt_start,
  122. .stop = meson_gxbb_wdt_stop,
  123. .ping = meson_gxbb_wdt_ping,
  124. .set_timeout = meson_gxbb_wdt_set_timeout,
  125. .get_timeleft = meson_gxbb_wdt_get_timeleft,
  126. };
  127. static const struct watchdog_info meson_gxbb_wdt_info = {
  128. .identity = "Meson GXBB Watchdog",
  129. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  130. };
  131. static int __maybe_unused meson_gxbb_wdt_resume(struct device *dev)
  132. {
  133. struct meson_gxbb_wdt *data = dev_get_drvdata(dev);
  134. if (watchdog_active(&data->wdt_dev))
  135. meson_gxbb_wdt_start(&data->wdt_dev);
  136. return 0;
  137. }
  138. static int __maybe_unused meson_gxbb_wdt_suspend(struct device *dev)
  139. {
  140. struct meson_gxbb_wdt *data = dev_get_drvdata(dev);
  141. if (watchdog_active(&data->wdt_dev))
  142. meson_gxbb_wdt_stop(&data->wdt_dev);
  143. return 0;
  144. }
  145. static const struct dev_pm_ops meson_gxbb_wdt_pm_ops = {
  146. SET_SYSTEM_SLEEP_PM_OPS(meson_gxbb_wdt_suspend, meson_gxbb_wdt_resume)
  147. };
  148. static const struct of_device_id meson_gxbb_wdt_dt_ids[] = {
  149. { .compatible = "amlogic,meson-gxbb-wdt", },
  150. { /* sentinel */ },
  151. };
  152. MODULE_DEVICE_TABLE(of, meson_gxbb_wdt_dt_ids);
  153. static int meson_gxbb_wdt_probe(struct platform_device *pdev)
  154. {
  155. struct meson_gxbb_wdt *data;
  156. struct resource *res;
  157. int ret;
  158. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  159. if (!data)
  160. return -ENOMEM;
  161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. data->reg_base = devm_ioremap_resource(&pdev->dev, res);
  163. if (IS_ERR(data->reg_base))
  164. return PTR_ERR(data->reg_base);
  165. data->clk = devm_clk_get(&pdev->dev, NULL);
  166. if (IS_ERR(data->clk))
  167. return PTR_ERR(data->clk);
  168. clk_prepare_enable(data->clk);
  169. platform_set_drvdata(pdev, data);
  170. data->wdt_dev.parent = &pdev->dev;
  171. data->wdt_dev.info = &meson_gxbb_wdt_info;
  172. data->wdt_dev.ops = &meson_gxbb_wdt_ops;
  173. data->wdt_dev.max_hw_heartbeat_ms = GXBB_WDT_TCNT_SETUP_MASK;
  174. data->wdt_dev.min_timeout = 1;
  175. data->wdt_dev.timeout = DEFAULT_TIMEOUT;
  176. watchdog_set_drvdata(&data->wdt_dev, data);
  177. /* Setup with 1ms timebase */
  178. writel(((clk_get_rate(data->clk) / 1000) & GXBB_WDT_CTRL_DIV_MASK) |
  179. GXBB_WDT_CTRL_EE_RESET |
  180. GXBB_WDT_CTRL_CLK_EN |
  181. GXBB_WDT_CTRL_CLKDIV_EN,
  182. data->reg_base + GXBB_WDT_CTRL_REG);
  183. meson_gxbb_wdt_set_timeout(&data->wdt_dev, data->wdt_dev.timeout);
  184. ret = watchdog_register_device(&data->wdt_dev);
  185. if (ret) {
  186. clk_disable_unprepare(data->clk);
  187. return ret;
  188. }
  189. return 0;
  190. }
  191. static int meson_gxbb_wdt_remove(struct platform_device *pdev)
  192. {
  193. struct meson_gxbb_wdt *data = platform_get_drvdata(pdev);
  194. watchdog_unregister_device(&data->wdt_dev);
  195. clk_disable_unprepare(data->clk);
  196. return 0;
  197. }
  198. static void meson_gxbb_wdt_shutdown(struct platform_device *pdev)
  199. {
  200. struct meson_gxbb_wdt *data = platform_get_drvdata(pdev);
  201. meson_gxbb_wdt_stop(&data->wdt_dev);
  202. }
  203. static struct platform_driver meson_gxbb_wdt_driver = {
  204. .probe = meson_gxbb_wdt_probe,
  205. .remove = meson_gxbb_wdt_remove,
  206. .shutdown = meson_gxbb_wdt_shutdown,
  207. .driver = {
  208. .name = "meson-gxbb-wdt",
  209. .pm = &meson_gxbb_wdt_pm_ops,
  210. .of_match_table = meson_gxbb_wdt_dt_ids,
  211. },
  212. };
  213. module_platform_driver(meson_gxbb_wdt_driver);
  214. MODULE_ALIAS("platform:meson-gxbb-wdt");
  215. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  216. MODULE_DESCRIPTION("Amlogic Meson GXBB Watchdog timer driver");
  217. MODULE_LICENSE("Dual BSD/GPL");