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/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/watchdog.h>
22 #include <dt-bindings/mfd/st-lpc.h>
25 #define LPC_LPA_LSB_OFF 0x410
26 #define LPC_LPA_START_OFF 0x418
29 #define LPC_WDT_OFF 0x510
31 static struct watchdog_device st_wdog_dev
;
33 struct st_wdog_syscfg
{
34 unsigned int reset_type_reg
;
35 unsigned int reset_type_mask
;
36 unsigned int enable_reg
;
37 unsigned int enable_mask
;
43 struct regmap
*regmap
;
44 const struct st_wdog_syscfg
*syscfg
;
46 unsigned long clkrate
;
50 static struct st_wdog_syscfg stih407_syscfg
= {
52 .enable_mask
= BIT(19),
55 static const struct of_device_id st_wdog_match
[] = {
57 .compatible
= "st,stih407-lpc",
58 .data
= &stih407_syscfg
,
62 MODULE_DEVICE_TABLE(of
, st_wdog_match
);
64 static void st_wdog_setup(struct st_wdog
*st_wdog
, bool enable
)
66 /* Type of watchdog reset - 0: Cold 1: Warm */
67 if (st_wdog
->syscfg
->reset_type_reg
)
68 regmap_update_bits(st_wdog
->regmap
,
69 st_wdog
->syscfg
->reset_type_reg
,
70 st_wdog
->syscfg
->reset_type_mask
,
73 /* Mask/unmask watchdog reset */
74 regmap_update_bits(st_wdog
->regmap
,
75 st_wdog
->syscfg
->enable_reg
,
76 st_wdog
->syscfg
->enable_mask
,
77 enable
? 0 : st_wdog
->syscfg
->enable_mask
);
80 static void st_wdog_load_timer(struct st_wdog
*st_wdog
, unsigned int timeout
)
82 unsigned long clkrate
= st_wdog
->clkrate
;
84 writel_relaxed(timeout
* clkrate
, st_wdog
->base
+ LPC_LPA_LSB_OFF
);
85 writel_relaxed(1, st_wdog
->base
+ LPC_LPA_START_OFF
);
88 static int st_wdog_start(struct watchdog_device
*wdd
)
90 struct st_wdog
*st_wdog
= watchdog_get_drvdata(wdd
);
92 writel_relaxed(1, st_wdog
->base
+ LPC_WDT_OFF
);
97 static int st_wdog_stop(struct watchdog_device
*wdd
)
99 struct st_wdog
*st_wdog
= watchdog_get_drvdata(wdd
);
101 writel_relaxed(0, st_wdog
->base
+ LPC_WDT_OFF
);
106 static int st_wdog_set_timeout(struct watchdog_device
*wdd
,
107 unsigned int timeout
)
109 struct st_wdog
*st_wdog
= watchdog_get_drvdata(wdd
);
111 wdd
->timeout
= timeout
;
112 st_wdog_load_timer(st_wdog
, timeout
);
117 static int st_wdog_keepalive(struct watchdog_device
*wdd
)
119 struct st_wdog
*st_wdog
= watchdog_get_drvdata(wdd
);
121 st_wdog_load_timer(st_wdog
, wdd
->timeout
);
126 static const struct watchdog_info st_wdog_info
= {
127 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
128 .identity
= "ST LPC WDT",
131 static const struct watchdog_ops st_wdog_ops
= {
132 .owner
= THIS_MODULE
,
133 .start
= st_wdog_start
,
134 .stop
= st_wdog_stop
,
135 .ping
= st_wdog_keepalive
,
136 .set_timeout
= st_wdog_set_timeout
,
139 static struct watchdog_device st_wdog_dev
= {
140 .info
= &st_wdog_info
,
144 static void st_clk_disable_unprepare(void *data
)
146 clk_disable_unprepare(data
);
149 static int st_wdog_probe(struct platform_device
*pdev
)
151 struct device
*dev
= &pdev
->dev
;
152 struct device_node
*np
= dev
->of_node
;
153 struct st_wdog
*st_wdog
;
154 struct regmap
*regmap
;
160 ret
= of_property_read_u32(np
, "st,lpc-mode", &mode
);
162 dev_err(dev
, "An LPC mode must be provided\n");
166 /* LPC can either run as a Clocksource or in RTC or WDT mode */
167 if (mode
!= ST_LPC_MODE_WDT
)
170 st_wdog
= devm_kzalloc(dev
, sizeof(*st_wdog
), GFP_KERNEL
);
174 st_wdog
->syscfg
= (struct st_wdog_syscfg
*)device_get_match_data(dev
);
176 base
= devm_platform_ioremap_resource(pdev
, 0);
178 return PTR_ERR(base
);
180 regmap
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
181 if (IS_ERR(regmap
)) {
182 dev_err(dev
, "No syscfg phandle specified\n");
183 return PTR_ERR(regmap
);
186 clk
= devm_clk_get(dev
, NULL
);
188 dev_err(dev
, "Unable to request clock\n");
193 st_wdog
->base
= base
;
195 st_wdog
->regmap
= regmap
;
196 st_wdog
->warm_reset
= of_property_read_bool(np
, "st,warm_reset");
197 st_wdog
->clkrate
= clk_get_rate(st_wdog
->clk
);
199 if (!st_wdog
->clkrate
) {
200 dev_err(dev
, "Unable to fetch clock rate\n");
203 st_wdog_dev
.max_timeout
= 0xFFFFFFFF / st_wdog
->clkrate
;
204 st_wdog_dev
.parent
= dev
;
206 ret
= clk_prepare_enable(clk
);
208 dev_err(dev
, "Unable to enable clock\n");
211 ret
= devm_add_action_or_reset(dev
, st_clk_disable_unprepare
, clk
);
215 watchdog_set_drvdata(&st_wdog_dev
, st_wdog
);
216 watchdog_set_nowayout(&st_wdog_dev
, WATCHDOG_NOWAYOUT
);
218 /* Init Watchdog timeout with value in DT */
219 ret
= watchdog_init_timeout(&st_wdog_dev
, 0, dev
);
223 ret
= devm_watchdog_register_device(dev
, &st_wdog_dev
);
227 st_wdog_setup(st_wdog
, true);
229 dev_info(dev
, "LPC Watchdog driver registered, reset type is %s",
230 st_wdog
->warm_reset
? "warm" : "cold");
235 static void st_wdog_remove(struct platform_device
*pdev
)
237 struct st_wdog
*st_wdog
= watchdog_get_drvdata(&st_wdog_dev
);
239 st_wdog_setup(st_wdog
, false);
242 static int st_wdog_suspend(struct device
*dev
)
244 struct st_wdog
*st_wdog
= watchdog_get_drvdata(&st_wdog_dev
);
246 if (watchdog_active(&st_wdog_dev
))
247 st_wdog_stop(&st_wdog_dev
);
249 st_wdog_setup(st_wdog
, false);
251 clk_disable(st_wdog
->clk
);
256 static int st_wdog_resume(struct device
*dev
)
258 struct st_wdog
*st_wdog
= watchdog_get_drvdata(&st_wdog_dev
);
261 ret
= clk_enable(st_wdog
->clk
);
263 dev_err(dev
, "Unable to re-enable clock\n");
264 watchdog_unregister_device(&st_wdog_dev
);
265 clk_unprepare(st_wdog
->clk
);
269 st_wdog_setup(st_wdog
, true);
271 if (watchdog_active(&st_wdog_dev
)) {
272 st_wdog_load_timer(st_wdog
, st_wdog_dev
.timeout
);
273 st_wdog_start(&st_wdog_dev
);
279 static DEFINE_SIMPLE_DEV_PM_OPS(st_wdog_pm_ops
,
280 st_wdog_suspend
, st_wdog_resume
);
282 static struct platform_driver st_wdog_driver
= {
284 .name
= "st-lpc-wdt",
285 .pm
= pm_sleep_ptr(&st_wdog_pm_ops
),
286 .of_match_table
= st_wdog_match
,
288 .probe
= st_wdog_probe
,
289 .remove
= st_wdog_remove
,
291 module_platform_driver(st_wdog_driver
);
293 MODULE_AUTHOR("David Paris <david.paris@st.com>");
294 MODULE_DESCRIPTION("ST LPC Watchdog Driver");
295 MODULE_LICENSE("GPL");