blob: 14ab6559c7486c57a4b1475fe2e57313e8ba6d90 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Lee Jonesf27925a2015-04-09 15:47:31 +01002/*
3 * ST's LPC Watchdog
4 *
5 * Copyright (C) 2014 STMicroelectronics -- All Rights Reserved
6 *
7 * Author: David Paris <david.paris@st.com> for STMicroelectronics
8 * Lee Jones <lee.jones@linaro.org> for STMicroelectronics
Lee Jonesf27925a2015-04-09 15:47:31 +01009 */
10
11#include <linux/clk.h>
12#include <linux/init.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/mfd/syscon.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/of_platform.h>
19#include <linux/platform_device.h>
20#include <linux/regmap.h>
21#include <linux/watchdog.h>
22
23#include <dt-bindings/mfd/st-lpc.h>
24
25/* Low Power Alarm */
26#define LPC_LPA_LSB_OFF 0x410
27#define LPC_LPA_START_OFF 0x418
28
29/* LPC as WDT */
30#define LPC_WDT_OFF 0x510
31
32static struct watchdog_device st_wdog_dev;
33
34struct 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;
39};
40
41struct st_wdog {
42 void __iomem *base;
43 struct device *dev;
44 struct regmap *regmap;
45 struct st_wdog_syscfg *syscfg;
46 struct clk *clk;
47 unsigned long clkrate;
48 bool warm_reset;
49};
50
Lee Jonesf27925a2015-04-09 15:47:31 +010051static struct st_wdog_syscfg stih407_syscfg = {
52 .enable_reg = 0x204,
53 .enable_mask = BIT(19),
54};
55
56static const struct of_device_id st_wdog_match[] = {
57 {
58 .compatible = "st,stih407-lpc",
59 .data = &stih407_syscfg,
60 },
Lee Jonesf27925a2015-04-09 15:47:31 +010061 {},
62};
63MODULE_DEVICE_TABLE(of, st_wdog_match);
64
65static void st_wdog_setup(struct st_wdog *st_wdog, bool enable)
66{
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,
72 st_wdog->warm_reset);
73
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);
79}
80
81static void st_wdog_load_timer(struct st_wdog *st_wdog, unsigned int timeout)
82{
83 unsigned long clkrate = st_wdog->clkrate;
84
85 writel_relaxed(timeout * clkrate, st_wdog->base + LPC_LPA_LSB_OFF);
86 writel_relaxed(1, st_wdog->base + LPC_LPA_START_OFF);
87}
88
89static int st_wdog_start(struct watchdog_device *wdd)
90{
91 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
92
93 writel_relaxed(1, st_wdog->base + LPC_WDT_OFF);
94
95 return 0;
96}
97
98static int st_wdog_stop(struct watchdog_device *wdd)
99{
100 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
101
102 writel_relaxed(0, st_wdog->base + LPC_WDT_OFF);
103
104 return 0;
105}
106
107static int st_wdog_set_timeout(struct watchdog_device *wdd,
108 unsigned int timeout)
109{
110 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
111
112 wdd->timeout = timeout;
113 st_wdog_load_timer(st_wdog, timeout);
114
115 return 0;
116}
117
118static int st_wdog_keepalive(struct watchdog_device *wdd)
119{
120 struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);
121
122 st_wdog_load_timer(st_wdog, wdd->timeout);
123
124 return 0;
125}
126
127static const struct watchdog_info st_wdog_info = {
128 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
129 .identity = "ST LPC WDT",
130};
131
132static 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,
138};
139
140static struct watchdog_device st_wdog_dev = {
141 .info = &st_wdog_info,
142 .ops = &st_wdog_ops,
143};
144
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700145static void st_clk_disable_unprepare(void *data)
146{
147 clk_disable_unprepare(data);
148}
149
Lee Jonesf27925a2015-04-09 15:47:31 +0100150static int st_wdog_probe(struct platform_device *pdev)
151{
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700152 struct device *dev = &pdev->dev;
Lee Jonesf27925a2015-04-09 15:47:31 +0100153 const struct of_device_id *match;
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700154 struct device_node *np = dev->of_node;
Lee Jonesf27925a2015-04-09 15:47:31 +0100155 struct st_wdog *st_wdog;
156 struct regmap *regmap;
Lee Jonesf27925a2015-04-09 15:47:31 +0100157 struct clk *clk;
158 void __iomem *base;
159 uint32_t mode;
160 int ret;
161
162 ret = of_property_read_u32(np, "st,lpc-mode", &mode);
163 if (ret) {
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700164 dev_err(dev, "An LPC mode must be provided\n");
Lee Jonesf27925a2015-04-09 15:47:31 +0100165 return -EINVAL;
166 }
167
Lee Jones79cb0972015-05-12 13:58:13 +0100168 /* LPC can either run as a Clocksource or in RTC or WDT mode */
Lee Jonesf27925a2015-04-09 15:47:31 +0100169 if (mode != ST_LPC_MODE_WDT)
170 return -ENODEV;
171
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700172 st_wdog = devm_kzalloc(dev, sizeof(*st_wdog), GFP_KERNEL);
Lee Jonesf27925a2015-04-09 15:47:31 +0100173 if (!st_wdog)
174 return -ENOMEM;
175
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700176 match = of_match_device(st_wdog_match, dev);
Lee Jonesf27925a2015-04-09 15:47:31 +0100177 if (!match) {
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700178 dev_err(dev, "Couldn't match device\n");
Lee Jonesf27925a2015-04-09 15:47:31 +0100179 return -ENODEV;
180 }
181 st_wdog->syscfg = (struct st_wdog_syscfg *)match->data;
182
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700183 base = devm_platform_ioremap_resource(pdev, 0);
Lee Jonesf27925a2015-04-09 15:47:31 +0100184 if (IS_ERR(base))
185 return PTR_ERR(base);
186
187 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
188 if (IS_ERR(regmap)) {
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700189 dev_err(dev, "No syscfg phandle specified\n");
Lee Jonesf27925a2015-04-09 15:47:31 +0100190 return PTR_ERR(regmap);
191 }
192
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700193 clk = devm_clk_get(dev, NULL);
Lee Jonesf27925a2015-04-09 15:47:31 +0100194 if (IS_ERR(clk)) {
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700195 dev_err(dev, "Unable to request clock\n");
Lee Jonesf27925a2015-04-09 15:47:31 +0100196 return PTR_ERR(clk);
197 }
198
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700199 st_wdog->dev = dev;
Lee Jonesf27925a2015-04-09 15:47:31 +0100200 st_wdog->base = base;
201 st_wdog->clk = clk;
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);
205
206 if (!st_wdog->clkrate) {
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700207 dev_err(dev, "Unable to fetch clock rate\n");
Lee Jonesf27925a2015-04-09 15:47:31 +0100208 return -EINVAL;
209 }
210 st_wdog_dev.max_timeout = 0xFFFFFFFF / st_wdog->clkrate;
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700211 st_wdog_dev.parent = dev;
Lee Jonesf27925a2015-04-09 15:47:31 +0100212
213 ret = clk_prepare_enable(clk);
214 if (ret) {
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700215 dev_err(dev, "Unable to enable clock\n");
Lee Jonesf27925a2015-04-09 15:47:31 +0100216 return ret;
217 }
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700218 ret = devm_add_action_or_reset(dev, st_clk_disable_unprepare, clk);
219 if (ret)
220 return ret;
Lee Jonesf27925a2015-04-09 15:47:31 +0100221
222 watchdog_set_drvdata(&st_wdog_dev, st_wdog);
223 watchdog_set_nowayout(&st_wdog_dev, WATCHDOG_NOWAYOUT);
224
225 /* Init Watchdog timeout with value in DT */
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700226 ret = watchdog_init_timeout(&st_wdog_dev, 0, dev);
Wolfram Sangb4214182019-04-19 20:15:58 +0200227 if (ret)
Lee Jonesf27925a2015-04-09 15:47:31 +0100228 return ret;
Lee Jonesf27925a2015-04-09 15:47:31 +0100229
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700230 ret = devm_watchdog_register_device(dev, &st_wdog_dev);
Wolfram Sang7283b212019-05-18 23:27:55 +0200231 if (ret)
Lee Jonesf27925a2015-04-09 15:47:31 +0100232 return ret;
Lee Jonesf27925a2015-04-09 15:47:31 +0100233
234 st_wdog_setup(st_wdog, true);
235
Guenter Roeckcfe9ee32019-04-09 10:23:57 -0700236 dev_info(dev, "LPC Watchdog driver registered, reset type is %s",
Lee Jonesf27925a2015-04-09 15:47:31 +0100237 st_wdog->warm_reset ? "warm" : "cold");
238
239 return ret;
240}
241
242static int st_wdog_remove(struct platform_device *pdev)
243{
244 struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
245
246 st_wdog_setup(st_wdog, false);
Lee Jonesf27925a2015-04-09 15:47:31 +0100247
248 return 0;
249}
250
251#ifdef CONFIG_PM_SLEEP
252static int st_wdog_suspend(struct device *dev)
253{
254 struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
255
256 if (watchdog_active(&st_wdog_dev))
257 st_wdog_stop(&st_wdog_dev);
258
259 st_wdog_setup(st_wdog, false);
260
261 clk_disable(st_wdog->clk);
262
263 return 0;
264}
265
266static int st_wdog_resume(struct device *dev)
267{
268 struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
269 int ret;
270
271 ret = clk_enable(st_wdog->clk);
272 if (ret) {
273 dev_err(dev, "Unable to re-enable clock\n");
274 watchdog_unregister_device(&st_wdog_dev);
275 clk_unprepare(st_wdog->clk);
276 return ret;
277 }
278
279 st_wdog_setup(st_wdog, true);
280
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);
284 }
285
286 return 0;
287}
288#endif
289
290static SIMPLE_DEV_PM_OPS(st_wdog_pm_ops,
291 st_wdog_suspend,
292 st_wdog_resume);
293
294static struct platform_driver st_wdog_driver = {
295 .driver = {
296 .name = "st-lpc-wdt",
297 .pm = &st_wdog_pm_ops,
298 .of_match_table = st_wdog_match,
299 },
300 .probe = st_wdog_probe,
301 .remove = st_wdog_remove,
302};
303module_platform_driver(st_wdog_driver);
304
305MODULE_AUTHOR("David Paris <david.paris@st.com>");
306MODULE_DESCRIPTION("ST LPC Watchdog Driver");
307MODULE_LICENSE("GPL");