1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2014 STMicroelectronics -- All Rights Reserved
7 * Author: David Paris <david.paris@st.com> for STMicroelectronics
8 * Lee Jones <lee.jones@linaro.org> for STMicroelectronics
11 #include <linux/clk.h>
12 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/watchdog.h>
23 #include <dt-bindings/mfd/st-lpc.h>
26 #define LPC_LPA_LSB_OFF 0x410
27 #define LPC_LPA_START_OFF 0x418
30 #define LPC_WDT_OFF 0x510
32 static struct watchdog_device st_wdog_dev;
34 struct st_wdog_syscfg {
35 unsigned int reset_type_reg;
36 unsigned int reset_type_mask;
37 unsigned int enable_reg;
38 unsigned int enable_mask;
44 struct regmap *regmap;
45 struct st_wdog_syscfg *syscfg;
47 unsigned long clkrate;
51 static struct st_wdog_syscfg stih407_syscfg = {
53 .enable_mask = BIT(19),
56 static const struct of_device_id st_wdog_match[] = {
58 .compatible = "st,stih407-lpc",
59 .data = &stih407_syscfg,
63 MODULE_DEVICE_TABLE(of, st_wdog_match);
65 static void st_wdog_setup(struct st_wdog *st_wdog, bool enable)
67 /* Type of watchdog reset - 0: Cold 1: Warm */
68 if (st_wdog->syscfg->reset_type_reg)
69 regmap_update_bits(st_wdog->regmap,
70 st_wdog->syscfg->reset_type_reg,
71 st_wdog->syscfg->reset_type_mask,
74 /* Mask/unmask watchdog reset */
75 regmap_update_bits(st_wdog->regmap,
76 st_wdog->syscfg->enable_reg,
77 st_wdog->syscfg->enable_mask,
78 enable ? 0 : st_wdog->syscfg->enable_mask);
81 static void st_wdog_load_timer(struct st_wdog *st_wdog, unsigned int timeout)
83 unsigned long clkrate = st_wdog->clkrate;
85 writel_relaxed(timeout * clkrate, st_wdog->base + LPC_LPA_LSB_OFF);
86 writel_relaxed(1, st_wdog->base + LPC_LPA_START_OFF);
89 static int st_wdog_start(struct watchdog_device *wdd)
91 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
93 writel_relaxed(1, st_wdog->base + LPC_WDT_OFF);
98 static int st_wdog_stop(struct watchdog_device *wdd)
100 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
102 writel_relaxed(0, st_wdog->base + LPC_WDT_OFF);
107 static int st_wdog_set_timeout(struct watchdog_device *wdd,
108 unsigned int timeout)
110 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
112 wdd->timeout = timeout;
113 st_wdog_load_timer(st_wdog, timeout);
118 static int st_wdog_keepalive(struct watchdog_device *wdd)
120 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
122 st_wdog_load_timer(st_wdog, wdd->timeout);
127 static const struct watchdog_info st_wdog_info = {
128 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
129 .identity = "ST LPC WDT",
132 static const struct watchdog_ops st_wdog_ops = {
133 .owner = THIS_MODULE,
134 .start = st_wdog_start,
135 .stop = st_wdog_stop,
136 .ping = st_wdog_keepalive,
137 .set_timeout = st_wdog_set_timeout,
140 static struct watchdog_device st_wdog_dev = {
141 .info = &st_wdog_info,
145 static void st_clk_disable_unprepare(void *data)
147 clk_disable_unprepare(data);
150 static int st_wdog_probe(struct platform_device *pdev)
152 struct device *dev = &pdev->dev;
153 const struct of_device_id *match;
154 struct device_node *np = dev->of_node;
155 struct st_wdog *st_wdog;
156 struct regmap *regmap;
162 ret = of_property_read_u32(np, "st,lpc-mode", &mode);
164 dev_err(dev, "An LPC mode must be provided\n");
168 /* LPC can either run as a Clocksource or in RTC or WDT mode */
169 if (mode != ST_LPC_MODE_WDT)
172 st_wdog = devm_kzalloc(dev, sizeof(*st_wdog), GFP_KERNEL);
176 match = of_match_device(st_wdog_match, dev);
178 dev_err(dev, "Couldn't match device\n");
181 st_wdog->syscfg = (struct st_wdog_syscfg *)match->data;
183 base = devm_platform_ioremap_resource(pdev, 0);
185 return PTR_ERR(base);
187 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
188 if (IS_ERR(regmap)) {
189 dev_err(dev, "No syscfg phandle specified\n");
190 return PTR_ERR(regmap);
193 clk = devm_clk_get(dev, NULL);
195 dev_err(dev, "Unable to request clock\n");
200 st_wdog->base = base;
202 st_wdog->regmap = regmap;
203 st_wdog->warm_reset = of_property_read_bool(np, "st,warm_reset");
204 st_wdog->clkrate = clk_get_rate(st_wdog->clk);
206 if (!st_wdog->clkrate) {
207 dev_err(dev, "Unable to fetch clock rate\n");
210 st_wdog_dev.max_timeout = 0xFFFFFFFF / st_wdog->clkrate;
211 st_wdog_dev.parent = dev;
213 ret = clk_prepare_enable(clk);
215 dev_err(dev, "Unable to enable clock\n");
218 ret = devm_add_action_or_reset(dev, st_clk_disable_unprepare, clk);
222 watchdog_set_drvdata(&st_wdog_dev, st_wdog);
223 watchdog_set_nowayout(&st_wdog_dev, WATCHDOG_NOWAYOUT);
225 /* Init Watchdog timeout with value in DT */
226 ret = watchdog_init_timeout(&st_wdog_dev, 0, dev);
230 ret = devm_watchdog_register_device(dev, &st_wdog_dev);
234 st_wdog_setup(st_wdog, true);
236 dev_info(dev, "LPC Watchdog driver registered, reset type is %s",
237 st_wdog->warm_reset ? "warm" : "cold");
242 static int st_wdog_remove(struct platform_device *pdev)
244 struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
246 st_wdog_setup(st_wdog, false);
251 #ifdef CONFIG_PM_SLEEP
252 static int st_wdog_suspend(struct device *dev)
254 struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
256 if (watchdog_active(&st_wdog_dev))
257 st_wdog_stop(&st_wdog_dev);
259 st_wdog_setup(st_wdog, false);
261 clk_disable(st_wdog->clk);
266 static int st_wdog_resume(struct device *dev)
268 struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
271 ret = clk_enable(st_wdog->clk);
273 dev_err(dev, "Unable to re-enable clock\n");
274 watchdog_unregister_device(&st_wdog_dev);
275 clk_unprepare(st_wdog->clk);
279 st_wdog_setup(st_wdog, true);
281 if (watchdog_active(&st_wdog_dev)) {
282 st_wdog_load_timer(st_wdog, st_wdog_dev.timeout);
283 st_wdog_start(&st_wdog_dev);
290 static SIMPLE_DEV_PM_OPS(st_wdog_pm_ops,
294 static struct platform_driver st_wdog_driver = {
296 .name = "st-lpc-wdt",
297 .pm = &st_wdog_pm_ops,
298 .of_match_table = st_wdog_match,
300 .probe = st_wdog_probe,
301 .remove = st_wdog_remove,
303 module_platform_driver(st_wdog_driver);
305 MODULE_AUTHOR("David Paris <david.paris@st.com>");
306 MODULE_DESCRIPTION("ST LPC Watchdog Driver");
307 MODULE_LICENSE("GPL");