Linux 4.19.133
[linux/fpc-iii.git] / drivers / watchdog / meson_gxbb_wdt.c
blob0a6672789640c2367bdbd4fa6fb66b43973ac9a4
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (c) 2016 BayLibre, SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
6 */
7 #include <linux/clk.h>
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/watchdog.h>
17 #define DEFAULT_TIMEOUT 30 /* seconds */
19 #define GXBB_WDT_CTRL_REG 0x0
20 #define GXBB_WDT_TCNT_REG 0x8
21 #define GXBB_WDT_RSET_REG 0xc
23 #define GXBB_WDT_CTRL_CLKDIV_EN BIT(25)
24 #define GXBB_WDT_CTRL_CLK_EN BIT(24)
25 #define GXBB_WDT_CTRL_EE_RESET BIT(21)
26 #define GXBB_WDT_CTRL_EN BIT(18)
27 #define GXBB_WDT_CTRL_DIV_MASK (BIT(18) - 1)
29 #define GXBB_WDT_TCNT_SETUP_MASK (BIT(16) - 1)
30 #define GXBB_WDT_TCNT_CNT_SHIFT 16
32 struct meson_gxbb_wdt {
33 void __iomem *reg_base;
34 struct watchdog_device wdt_dev;
35 struct clk *clk;
38 static int meson_gxbb_wdt_start(struct watchdog_device *wdt_dev)
40 struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
42 writel(readl(data->reg_base + GXBB_WDT_CTRL_REG) | GXBB_WDT_CTRL_EN,
43 data->reg_base + GXBB_WDT_CTRL_REG);
45 return 0;
48 static int meson_gxbb_wdt_stop(struct watchdog_device *wdt_dev)
50 struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
52 writel(readl(data->reg_base + GXBB_WDT_CTRL_REG) & ~GXBB_WDT_CTRL_EN,
53 data->reg_base + GXBB_WDT_CTRL_REG);
55 return 0;
58 static int meson_gxbb_wdt_ping(struct watchdog_device *wdt_dev)
60 struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
62 writel(0, data->reg_base + GXBB_WDT_RSET_REG);
64 return 0;
67 static int meson_gxbb_wdt_set_timeout(struct watchdog_device *wdt_dev,
68 unsigned int timeout)
70 struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
71 unsigned long tcnt = timeout * 1000;
73 if (tcnt > GXBB_WDT_TCNT_SETUP_MASK)
74 tcnt = GXBB_WDT_TCNT_SETUP_MASK;
76 wdt_dev->timeout = timeout;
78 meson_gxbb_wdt_ping(wdt_dev);
80 writel(tcnt, data->reg_base + GXBB_WDT_TCNT_REG);
82 return 0;
85 static unsigned int meson_gxbb_wdt_get_timeleft(struct watchdog_device *wdt_dev)
87 struct meson_gxbb_wdt *data = watchdog_get_drvdata(wdt_dev);
88 unsigned long reg;
90 reg = readl(data->reg_base + GXBB_WDT_TCNT_REG);
92 return ((reg & GXBB_WDT_TCNT_SETUP_MASK) -
93 (reg >> GXBB_WDT_TCNT_CNT_SHIFT)) / 1000;
96 static const struct watchdog_ops meson_gxbb_wdt_ops = {
97 .start = meson_gxbb_wdt_start,
98 .stop = meson_gxbb_wdt_stop,
99 .ping = meson_gxbb_wdt_ping,
100 .set_timeout = meson_gxbb_wdt_set_timeout,
101 .get_timeleft = meson_gxbb_wdt_get_timeleft,
104 static const struct watchdog_info meson_gxbb_wdt_info = {
105 .identity = "Meson GXBB Watchdog",
106 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
109 static int __maybe_unused meson_gxbb_wdt_resume(struct device *dev)
111 struct meson_gxbb_wdt *data = dev_get_drvdata(dev);
113 if (watchdog_active(&data->wdt_dev))
114 meson_gxbb_wdt_start(&data->wdt_dev);
116 return 0;
119 static int __maybe_unused meson_gxbb_wdt_suspend(struct device *dev)
121 struct meson_gxbb_wdt *data = dev_get_drvdata(dev);
123 if (watchdog_active(&data->wdt_dev))
124 meson_gxbb_wdt_stop(&data->wdt_dev);
126 return 0;
129 static const struct dev_pm_ops meson_gxbb_wdt_pm_ops = {
130 SET_SYSTEM_SLEEP_PM_OPS(meson_gxbb_wdt_suspend, meson_gxbb_wdt_resume)
133 static const struct of_device_id meson_gxbb_wdt_dt_ids[] = {
134 { .compatible = "amlogic,meson-gxbb-wdt", },
135 { /* sentinel */ },
137 MODULE_DEVICE_TABLE(of, meson_gxbb_wdt_dt_ids);
139 static int meson_gxbb_wdt_probe(struct platform_device *pdev)
141 struct meson_gxbb_wdt *data;
142 struct resource *res;
143 int ret;
145 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
146 if (!data)
147 return -ENOMEM;
149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150 data->reg_base = devm_ioremap_resource(&pdev->dev, res);
151 if (IS_ERR(data->reg_base))
152 return PTR_ERR(data->reg_base);
154 data->clk = devm_clk_get(&pdev->dev, NULL);
155 if (IS_ERR(data->clk))
156 return PTR_ERR(data->clk);
158 ret = clk_prepare_enable(data->clk);
159 if (ret)
160 return ret;
162 platform_set_drvdata(pdev, data);
164 data->wdt_dev.parent = &pdev->dev;
165 data->wdt_dev.info = &meson_gxbb_wdt_info;
166 data->wdt_dev.ops = &meson_gxbb_wdt_ops;
167 data->wdt_dev.max_hw_heartbeat_ms = GXBB_WDT_TCNT_SETUP_MASK;
168 data->wdt_dev.min_timeout = 1;
169 data->wdt_dev.timeout = DEFAULT_TIMEOUT;
170 watchdog_set_drvdata(&data->wdt_dev, data);
172 /* Setup with 1ms timebase */
173 writel(((clk_get_rate(data->clk) / 1000) & GXBB_WDT_CTRL_DIV_MASK) |
174 GXBB_WDT_CTRL_EE_RESET |
175 GXBB_WDT_CTRL_CLK_EN |
176 GXBB_WDT_CTRL_CLKDIV_EN,
177 data->reg_base + GXBB_WDT_CTRL_REG);
179 meson_gxbb_wdt_set_timeout(&data->wdt_dev, data->wdt_dev.timeout);
181 ret = watchdog_register_device(&data->wdt_dev);
182 if (ret) {
183 clk_disable_unprepare(data->clk);
184 return ret;
187 return 0;
190 static int meson_gxbb_wdt_remove(struct platform_device *pdev)
192 struct meson_gxbb_wdt *data = platform_get_drvdata(pdev);
194 watchdog_unregister_device(&data->wdt_dev);
196 clk_disable_unprepare(data->clk);
198 return 0;
201 static void meson_gxbb_wdt_shutdown(struct platform_device *pdev)
203 struct meson_gxbb_wdt *data = platform_get_drvdata(pdev);
205 meson_gxbb_wdt_stop(&data->wdt_dev);
208 static struct platform_driver meson_gxbb_wdt_driver = {
209 .probe = meson_gxbb_wdt_probe,
210 .remove = meson_gxbb_wdt_remove,
211 .shutdown = meson_gxbb_wdt_shutdown,
212 .driver = {
213 .name = "meson-gxbb-wdt",
214 .pm = &meson_gxbb_wdt_pm_ops,
215 .of_match_table = meson_gxbb_wdt_dt_ids,
219 module_platform_driver(meson_gxbb_wdt_driver);
221 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
222 MODULE_DESCRIPTION("Amlogic Meson GXBB Watchdog timer driver");
223 MODULE_LICENSE("Dual BSD/GPL");