xref: /linux/drivers/watchdog/st_lpc_wdt.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
12e62c498SMarcus Folkesson // SPDX-License-Identifier: GPL-2.0+
2f27925a6SLee Jones /*
3f27925a6SLee Jones  * ST's LPC Watchdog
4f27925a6SLee Jones  *
5f27925a6SLee Jones  * Copyright (C) 2014 STMicroelectronics -- All Rights Reserved
6f27925a6SLee Jones  *
7f27925a6SLee Jones  * Author: David Paris <david.paris@st.com> for STMicroelectronics
8f27925a6SLee Jones  *         Lee Jones <lee.jones@linaro.org> for STMicroelectronics
9f27925a6SLee Jones  */
10f27925a6SLee Jones 
11f27925a6SLee Jones #include <linux/clk.h>
12f27925a6SLee Jones #include <linux/init.h>
13f27925a6SLee Jones #include <linux/io.h>
14f27925a6SLee Jones #include <linux/kernel.h>
15f27925a6SLee Jones #include <linux/mfd/syscon.h>
16f27925a6SLee Jones #include <linux/module.h>
17f27925a6SLee Jones #include <linux/of.h>
18f27925a6SLee Jones #include <linux/platform_device.h>
19f27925a6SLee Jones #include <linux/regmap.h>
20f27925a6SLee Jones #include <linux/watchdog.h>
21f27925a6SLee Jones 
22f27925a6SLee Jones #include <dt-bindings/mfd/st-lpc.h>
23f27925a6SLee Jones 
24f27925a6SLee Jones /* Low Power Alarm */
25f27925a6SLee Jones #define LPC_LPA_LSB_OFF			0x410
26f27925a6SLee Jones #define LPC_LPA_START_OFF		0x418
27f27925a6SLee Jones 
28f27925a6SLee Jones /* LPC as WDT */
29f27925a6SLee Jones #define LPC_WDT_OFF			0x510
30f27925a6SLee Jones 
31f27925a6SLee Jones static struct watchdog_device st_wdog_dev;
32f27925a6SLee Jones 
33f27925a6SLee Jones struct st_wdog_syscfg {
34f27925a6SLee Jones 	unsigned int reset_type_reg;
35f27925a6SLee Jones 	unsigned int reset_type_mask;
36f27925a6SLee Jones 	unsigned int enable_reg;
37f27925a6SLee Jones 	unsigned int enable_mask;
38f27925a6SLee Jones };
39f27925a6SLee Jones 
40f27925a6SLee Jones struct st_wdog {
41f27925a6SLee Jones 	void __iomem *base;
42f27925a6SLee Jones 	struct device *dev;
43f27925a6SLee Jones 	struct regmap *regmap;
44*c7e2f4e6SRob Herring 	const struct st_wdog_syscfg *syscfg;
45f27925a6SLee Jones 	struct clk *clk;
46f27925a6SLee Jones 	unsigned long clkrate;
47f27925a6SLee Jones 	bool warm_reset;
48f27925a6SLee Jones };
49f27925a6SLee Jones 
50f27925a6SLee Jones static struct st_wdog_syscfg stih407_syscfg = {
51f27925a6SLee Jones 	.enable_reg		= 0x204,
52f27925a6SLee Jones 	.enable_mask		= BIT(19),
53f27925a6SLee Jones };
54f27925a6SLee Jones 
55f27925a6SLee Jones static const struct of_device_id st_wdog_match[] = {
56f27925a6SLee Jones 	{
57f27925a6SLee Jones 		.compatible = "st,stih407-lpc",
58f27925a6SLee Jones 		.data = &stih407_syscfg,
59f27925a6SLee Jones 	},
60f27925a6SLee Jones 	{},
61f27925a6SLee Jones };
62f27925a6SLee Jones MODULE_DEVICE_TABLE(of, st_wdog_match);
63f27925a6SLee Jones 
st_wdog_setup(struct st_wdog * st_wdog,bool enable)64f27925a6SLee Jones static void st_wdog_setup(struct st_wdog *st_wdog, bool enable)
65f27925a6SLee Jones {
66f27925a6SLee Jones 	/* Type of watchdog reset - 0: Cold 1: Warm */
67f27925a6SLee Jones 	if (st_wdog->syscfg->reset_type_reg)
68f27925a6SLee Jones 		regmap_update_bits(st_wdog->regmap,
69f27925a6SLee Jones 				   st_wdog->syscfg->reset_type_reg,
70f27925a6SLee Jones 				   st_wdog->syscfg->reset_type_mask,
71f27925a6SLee Jones 				   st_wdog->warm_reset);
72f27925a6SLee Jones 
73f27925a6SLee Jones 	/* Mask/unmask watchdog reset */
74f27925a6SLee Jones 	regmap_update_bits(st_wdog->regmap,
75f27925a6SLee Jones 			   st_wdog->syscfg->enable_reg,
76f27925a6SLee Jones 			   st_wdog->syscfg->enable_mask,
77f27925a6SLee Jones 			   enable ? 0 : st_wdog->syscfg->enable_mask);
78f27925a6SLee Jones }
79f27925a6SLee Jones 
st_wdog_load_timer(struct st_wdog * st_wdog,unsigned int timeout)80f27925a6SLee Jones static void st_wdog_load_timer(struct st_wdog *st_wdog, unsigned int timeout)
81f27925a6SLee Jones {
82f27925a6SLee Jones 	unsigned long clkrate = st_wdog->clkrate;
83f27925a6SLee Jones 
84f27925a6SLee Jones 	writel_relaxed(timeout * clkrate, st_wdog->base + LPC_LPA_LSB_OFF);
85f27925a6SLee Jones 	writel_relaxed(1, st_wdog->base + LPC_LPA_START_OFF);
86f27925a6SLee Jones }
87f27925a6SLee Jones 
st_wdog_start(struct watchdog_device * wdd)88f27925a6SLee Jones static int st_wdog_start(struct watchdog_device *wdd)
89f27925a6SLee Jones {
90f27925a6SLee Jones 	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
91f27925a6SLee Jones 
92f27925a6SLee Jones 	writel_relaxed(1, st_wdog->base + LPC_WDT_OFF);
93f27925a6SLee Jones 
94f27925a6SLee Jones 	return 0;
95f27925a6SLee Jones }
96f27925a6SLee Jones 
st_wdog_stop(struct watchdog_device * wdd)97f27925a6SLee Jones static int st_wdog_stop(struct watchdog_device *wdd)
98f27925a6SLee Jones {
99f27925a6SLee Jones 	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
100f27925a6SLee Jones 
101f27925a6SLee Jones 	writel_relaxed(0, st_wdog->base + LPC_WDT_OFF);
102f27925a6SLee Jones 
103f27925a6SLee Jones 	return 0;
104f27925a6SLee Jones }
105f27925a6SLee Jones 
st_wdog_set_timeout(struct watchdog_device * wdd,unsigned int timeout)106f27925a6SLee Jones static int st_wdog_set_timeout(struct watchdog_device *wdd,
107f27925a6SLee Jones 			       unsigned int timeout)
108f27925a6SLee Jones {
109f27925a6SLee Jones 	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
110f27925a6SLee Jones 
111f27925a6SLee Jones 	wdd->timeout = timeout;
112f27925a6SLee Jones 	st_wdog_load_timer(st_wdog, timeout);
113f27925a6SLee Jones 
114f27925a6SLee Jones 	return 0;
115f27925a6SLee Jones }
116f27925a6SLee Jones 
st_wdog_keepalive(struct watchdog_device * wdd)117f27925a6SLee Jones static int st_wdog_keepalive(struct watchdog_device *wdd)
118f27925a6SLee Jones {
119f27925a6SLee Jones 	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
120f27925a6SLee Jones 
121f27925a6SLee Jones 	st_wdog_load_timer(st_wdog, wdd->timeout);
122f27925a6SLee Jones 
123f27925a6SLee Jones 	return 0;
124f27925a6SLee Jones }
125f27925a6SLee Jones 
126f27925a6SLee Jones static const struct watchdog_info st_wdog_info = {
127f27925a6SLee Jones 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
128f27925a6SLee Jones 	.identity = "ST LPC WDT",
129f27925a6SLee Jones };
130f27925a6SLee Jones 
131f27925a6SLee Jones static const struct watchdog_ops st_wdog_ops = {
132f27925a6SLee Jones 	.owner		= THIS_MODULE,
133f27925a6SLee Jones 	.start		= st_wdog_start,
134f27925a6SLee Jones 	.stop		= st_wdog_stop,
135f27925a6SLee Jones 	.ping		= st_wdog_keepalive,
136f27925a6SLee Jones 	.set_timeout	= st_wdog_set_timeout,
137f27925a6SLee Jones };
138f27925a6SLee Jones 
139f27925a6SLee Jones static struct watchdog_device st_wdog_dev = {
140f27925a6SLee Jones 	.info		= &st_wdog_info,
141f27925a6SLee Jones 	.ops		= &st_wdog_ops,
142f27925a6SLee Jones };
143f27925a6SLee Jones 
st_clk_disable_unprepare(void * data)144cfe9ee3aSGuenter Roeck static void st_clk_disable_unprepare(void *data)
145cfe9ee3aSGuenter Roeck {
146cfe9ee3aSGuenter Roeck 	clk_disable_unprepare(data);
147cfe9ee3aSGuenter Roeck }
148cfe9ee3aSGuenter Roeck 
st_wdog_probe(struct platform_device * pdev)149f27925a6SLee Jones static int st_wdog_probe(struct platform_device *pdev)
150f27925a6SLee Jones {
151cfe9ee3aSGuenter Roeck 	struct device *dev = &pdev->dev;
152cfe9ee3aSGuenter Roeck 	struct device_node *np = dev->of_node;
153f27925a6SLee Jones 	struct st_wdog *st_wdog;
154f27925a6SLee Jones 	struct regmap *regmap;
155f27925a6SLee Jones 	struct clk *clk;
156f27925a6SLee Jones 	void __iomem *base;
157f27925a6SLee Jones 	uint32_t mode;
158f27925a6SLee Jones 	int ret;
159f27925a6SLee Jones 
160f27925a6SLee Jones 	ret = of_property_read_u32(np, "st,lpc-mode", &mode);
161f27925a6SLee Jones 	if (ret) {
162cfe9ee3aSGuenter Roeck 		dev_err(dev, "An LPC mode must be provided\n");
163f27925a6SLee Jones 		return -EINVAL;
164f27925a6SLee Jones 	}
165f27925a6SLee Jones 
16679cb0976SLee Jones 	/* LPC can either run as a Clocksource or in RTC or WDT mode */
167f27925a6SLee Jones 	if (mode != ST_LPC_MODE_WDT)
168f27925a6SLee Jones 		return -ENODEV;
169f27925a6SLee Jones 
170cfe9ee3aSGuenter Roeck 	st_wdog = devm_kzalloc(dev, sizeof(*st_wdog), GFP_KERNEL);
171f27925a6SLee Jones 	if (!st_wdog)
172f27925a6SLee Jones 		return -ENOMEM;
173f27925a6SLee Jones 
174*c7e2f4e6SRob Herring 	st_wdog->syscfg	= (struct st_wdog_syscfg *)device_get_match_data(dev);
175f27925a6SLee Jones 
1760f0a6a28SGuenter Roeck 	base = devm_platform_ioremap_resource(pdev, 0);
177f27925a6SLee Jones 	if (IS_ERR(base))
178f27925a6SLee Jones 		return PTR_ERR(base);
179f27925a6SLee Jones 
180f27925a6SLee Jones 	regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
181f27925a6SLee Jones 	if (IS_ERR(regmap)) {
182cfe9ee3aSGuenter Roeck 		dev_err(dev, "No syscfg phandle specified\n");
183f27925a6SLee Jones 		return PTR_ERR(regmap);
184f27925a6SLee Jones 	}
185f27925a6SLee Jones 
186cfe9ee3aSGuenter Roeck 	clk = devm_clk_get(dev, NULL);
187f27925a6SLee Jones 	if (IS_ERR(clk)) {
188cfe9ee3aSGuenter Roeck 		dev_err(dev, "Unable to request clock\n");
189f27925a6SLee Jones 		return PTR_ERR(clk);
190f27925a6SLee Jones 	}
191f27925a6SLee Jones 
192cfe9ee3aSGuenter Roeck 	st_wdog->dev		= dev;
193f27925a6SLee Jones 	st_wdog->base		= base;
194f27925a6SLee Jones 	st_wdog->clk		= clk;
195f27925a6SLee Jones 	st_wdog->regmap		= regmap;
196f27925a6SLee Jones 	st_wdog->warm_reset	= of_property_read_bool(np, "st,warm_reset");
197f27925a6SLee Jones 	st_wdog->clkrate	= clk_get_rate(st_wdog->clk);
198f27925a6SLee Jones 
199f27925a6SLee Jones 	if (!st_wdog->clkrate) {
200cfe9ee3aSGuenter Roeck 		dev_err(dev, "Unable to fetch clock rate\n");
201f27925a6SLee Jones 		return -EINVAL;
202f27925a6SLee Jones 	}
203f27925a6SLee Jones 	st_wdog_dev.max_timeout = 0xFFFFFFFF / st_wdog->clkrate;
204cfe9ee3aSGuenter Roeck 	st_wdog_dev.parent = dev;
205f27925a6SLee Jones 
206f27925a6SLee Jones 	ret = clk_prepare_enable(clk);
207f27925a6SLee Jones 	if (ret) {
208cfe9ee3aSGuenter Roeck 		dev_err(dev, "Unable to enable clock\n");
209f27925a6SLee Jones 		return ret;
210f27925a6SLee Jones 	}
211cfe9ee3aSGuenter Roeck 	ret = devm_add_action_or_reset(dev, st_clk_disable_unprepare, clk);
212cfe9ee3aSGuenter Roeck 	if (ret)
213cfe9ee3aSGuenter Roeck 		return ret;
214f27925a6SLee Jones 
215f27925a6SLee Jones 	watchdog_set_drvdata(&st_wdog_dev, st_wdog);
216f27925a6SLee Jones 	watchdog_set_nowayout(&st_wdog_dev, WATCHDOG_NOWAYOUT);
217f27925a6SLee Jones 
218f27925a6SLee Jones 	/* Init Watchdog timeout with value in DT */
219cfe9ee3aSGuenter Roeck 	ret = watchdog_init_timeout(&st_wdog_dev, 0, dev);
220b4214185SWolfram Sang 	if (ret)
221f27925a6SLee Jones 		return ret;
222f27925a6SLee Jones 
223cfe9ee3aSGuenter Roeck 	ret = devm_watchdog_register_device(dev, &st_wdog_dev);
2247283b217SWolfram Sang 	if (ret)
225f27925a6SLee Jones 		return ret;
226f27925a6SLee Jones 
227f27925a6SLee Jones 	st_wdog_setup(st_wdog, true);
228f27925a6SLee Jones 
229cfe9ee3aSGuenter Roeck 	dev_info(dev, "LPC Watchdog driver registered, reset type is %s",
230f27925a6SLee Jones 		 st_wdog->warm_reset ? "warm" : "cold");
231f27925a6SLee Jones 
232f27925a6SLee Jones 	return ret;
233f27925a6SLee Jones }
234f27925a6SLee Jones 
st_wdog_remove(struct platform_device * pdev)235e7a84d45SUwe Kleine-König static void st_wdog_remove(struct platform_device *pdev)
236f27925a6SLee Jones {
237f27925a6SLee Jones 	struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
238f27925a6SLee Jones 
239f27925a6SLee Jones 	st_wdog_setup(st_wdog, false);
240f27925a6SLee Jones }
241f27925a6SLee Jones 
st_wdog_suspend(struct device * dev)242f27925a6SLee Jones static int st_wdog_suspend(struct device *dev)
243f27925a6SLee Jones {
244f27925a6SLee Jones 	struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
245f27925a6SLee Jones 
246f27925a6SLee Jones 	if (watchdog_active(&st_wdog_dev))
247f27925a6SLee Jones 		st_wdog_stop(&st_wdog_dev);
248f27925a6SLee Jones 
249f27925a6SLee Jones 	st_wdog_setup(st_wdog, false);
250f27925a6SLee Jones 
251f27925a6SLee Jones 	clk_disable(st_wdog->clk);
252f27925a6SLee Jones 
253f27925a6SLee Jones 	return 0;
254f27925a6SLee Jones }
255f27925a6SLee Jones 
st_wdog_resume(struct device * dev)256f27925a6SLee Jones static int st_wdog_resume(struct device *dev)
257f27925a6SLee Jones {
258f27925a6SLee Jones 	struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
259f27925a6SLee Jones 	int ret;
260f27925a6SLee Jones 
261f27925a6SLee Jones 	ret = clk_enable(st_wdog->clk);
262f27925a6SLee Jones 	if (ret) {
263f27925a6SLee Jones 		dev_err(dev, "Unable to re-enable clock\n");
264f27925a6SLee Jones 		watchdog_unregister_device(&st_wdog_dev);
265f27925a6SLee Jones 		clk_unprepare(st_wdog->clk);
266f27925a6SLee Jones 		return ret;
267f27925a6SLee Jones 	}
268f27925a6SLee Jones 
269f27925a6SLee Jones 	st_wdog_setup(st_wdog, true);
270f27925a6SLee Jones 
271f27925a6SLee Jones 	if (watchdog_active(&st_wdog_dev)) {
272f27925a6SLee Jones 		st_wdog_load_timer(st_wdog, st_wdog_dev.timeout);
273f27925a6SLee Jones 		st_wdog_start(&st_wdog_dev);
274f27925a6SLee Jones 	}
275f27925a6SLee Jones 
276f27925a6SLee Jones 	return 0;
277f27925a6SLee Jones }
278f27925a6SLee Jones 
27929958ab7SPaul Cercueil static DEFINE_SIMPLE_DEV_PM_OPS(st_wdog_pm_ops,
28029958ab7SPaul Cercueil 				st_wdog_suspend, st_wdog_resume);
281f27925a6SLee Jones 
282f27925a6SLee Jones static struct platform_driver st_wdog_driver = {
283f27925a6SLee Jones 	.driver	= {
284f27925a6SLee Jones 		.name = "st-lpc-wdt",
28529958ab7SPaul Cercueil 		.pm = pm_sleep_ptr(&st_wdog_pm_ops),
286f27925a6SLee Jones 		.of_match_table = st_wdog_match,
287f27925a6SLee Jones 	},
288f27925a6SLee Jones 	.probe = st_wdog_probe,
289e7a84d45SUwe Kleine-König 	.remove_new = st_wdog_remove,
290f27925a6SLee Jones };
291f27925a6SLee Jones module_platform_driver(st_wdog_driver);
292f27925a6SLee Jones 
293f27925a6SLee Jones MODULE_AUTHOR("David Paris <david.paris@st.com>");
294f27925a6SLee Jones MODULE_DESCRIPTION("ST LPC Watchdog Driver");
295f27925a6SLee Jones MODULE_LICENSE("GPL");
296