blob: 570a71509d2a9b0070494de4419b3b6df862a51a [file] [log] [blame]
Benjamin Gaignard49620a22017-11-30 09:48:04 +01001// SPDX-License-Identifier: GPL-2.0
Yannick Fertre4332d112017-04-06 14:19:25 +02002/*
3 * Driver for STM32 Independent Watchdog
4 *
Benjamin Gaignard49620a22017-11-30 09:48:04 +01005 * Copyright (C) STMicroelectronics 2017
6 * Author: Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
Yannick Fertre4332d112017-04-06 14:19:25 +02007 *
8 * This driver is based on tegra_wdt.c
9 *
Yannick Fertre4332d112017-04-06 14:19:25 +020010 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
Yannick Fertre4332d112017-04-06 14:19:25 +020014#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/iopoll.h>
Ludovic Barrec2cf4662018-06-25 17:43:00 +020017#include <linux/kernel.h>
18#include <linux/module.h>
Yannick Fertre4332d112017-04-06 14:19:25 +020019#include <linux/of.h>
Ludovic Barrec2cf4662018-06-25 17:43:00 +020020#include <linux/of_device.h>
Yannick Fertre4332d112017-04-06 14:19:25 +020021#include <linux/platform_device.h>
22#include <linux/watchdog.h>
23
24/* IWDG registers */
25#define IWDG_KR 0x00 /* Key register */
26#define IWDG_PR 0x04 /* Prescaler Register */
27#define IWDG_RLR 0x08 /* ReLoad Register */
28#define IWDG_SR 0x0C /* Status Register */
29#define IWDG_WINR 0x10 /* Windows Register */
30
31/* IWDG_KR register bit mask */
32#define KR_KEY_RELOAD 0xAAAA /* reload counter enable */
33#define KR_KEY_ENABLE 0xCCCC /* peripheral enable */
34#define KR_KEY_EWA 0x5555 /* write access enable */
35#define KR_KEY_DWA 0x0000 /* write access disable */
36
Ludovic Barree9974162019-05-03 15:48:26 +020037/* IWDG_PR register */
38#define PR_SHIFT 2
39#define PR_MIN BIT(PR_SHIFT)
Yannick Fertre4332d112017-04-06 14:19:25 +020040
41/* IWDG_RLR register values */
Ludovic Barree9974162019-05-03 15:48:26 +020042#define RLR_MIN 0x2 /* min value recommended */
43#define RLR_MAX GENMASK(11, 0) /* max value of reload register */
Yannick Fertre4332d112017-04-06 14:19:25 +020044
45/* IWDG_SR register bit mask */
Ludovic Barree9974162019-05-03 15:48:26 +020046#define SR_PVU BIT(0) /* Watchdog prescaler value update */
47#define SR_RVU BIT(1) /* Watchdog counter reload value update */
Yannick Fertre4332d112017-04-06 14:19:25 +020048
49/* set timeout to 100000 us */
50#define TIMEOUT_US 100000
51#define SLEEP_US 1000
52
Ludovic Barree9974162019-05-03 15:48:26 +020053struct stm32_iwdg_data {
54 bool has_pclk;
55 u32 max_prescaler;
56};
57
58static const struct stm32_iwdg_data stm32_iwdg_data = {
59 .has_pclk = false,
60 .max_prescaler = 256,
61};
62
63static const struct stm32_iwdg_data stm32mp1_iwdg_data = {
64 .has_pclk = true,
65 .max_prescaler = 1024,
66};
Ludovic Barrec2cf4662018-06-25 17:43:00 +020067
Yannick Fertre4332d112017-04-06 14:19:25 +020068struct stm32_iwdg {
69 struct watchdog_device wdd;
Ludovic Barree9974162019-05-03 15:48:26 +020070 const struct stm32_iwdg_data *data;
Yannick Fertre4332d112017-04-06 14:19:25 +020071 void __iomem *regs;
Ludovic Barrec2cf4662018-06-25 17:43:00 +020072 struct clk *clk_lsi;
73 struct clk *clk_pclk;
Yannick Fertre4332d112017-04-06 14:19:25 +020074 unsigned int rate;
75};
76
77static inline u32 reg_read(void __iomem *base, u32 reg)
78{
79 return readl_relaxed(base + reg);
80}
81
82static inline void reg_write(void __iomem *base, u32 reg, u32 val)
83{
84 writel_relaxed(val, base + reg);
85}
86
87static int stm32_iwdg_start(struct watchdog_device *wdd)
88{
89 struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
Ludovic Barree9974162019-05-03 15:48:26 +020090 u32 tout, presc, iwdg_rlr, iwdg_pr, iwdg_sr;
Yannick Fertre4332d112017-04-06 14:19:25 +020091 int ret;
92
93 dev_dbg(wdd->parent, "%s\n", __func__);
94
Ludovic Barree9974162019-05-03 15:48:26 +020095 tout = clamp_t(unsigned int, wdd->timeout,
96 wdd->min_timeout, wdd->max_hw_heartbeat_ms / 1000);
97
98 presc = DIV_ROUND_UP(tout * wdt->rate, RLR_MAX + 1);
99
100 /* The prescaler is align on power of 2 and start at 2 ^ PR_SHIFT. */
101 presc = roundup_pow_of_two(presc);
102 iwdg_pr = presc <= 1 << PR_SHIFT ? 0 : ilog2(presc) - PR_SHIFT;
103 iwdg_rlr = ((tout * wdt->rate) / presc) - 1;
Yannick Fertre4332d112017-04-06 14:19:25 +0200104
105 /* enable write access */
106 reg_write(wdt->regs, IWDG_KR, KR_KEY_EWA);
107
108 /* set prescaler & reload registers */
Ludovic Barree9974162019-05-03 15:48:26 +0200109 reg_write(wdt->regs, IWDG_PR, iwdg_pr);
110 reg_write(wdt->regs, IWDG_RLR, iwdg_rlr);
Yannick Fertre4332d112017-04-06 14:19:25 +0200111 reg_write(wdt->regs, IWDG_KR, KR_KEY_ENABLE);
112
113 /* wait for the registers to be updated (max 100ms) */
Ludovic Barree9974162019-05-03 15:48:26 +0200114 ret = readl_relaxed_poll_timeout(wdt->regs + IWDG_SR, iwdg_sr,
115 !(iwdg_sr & (SR_PVU | SR_RVU)),
Yannick Fertre4332d112017-04-06 14:19:25 +0200116 SLEEP_US, TIMEOUT_US);
117 if (ret) {
Ludovic Barree9974162019-05-03 15:48:26 +0200118 dev_err(wdd->parent, "Fail to set prescaler, reload regs\n");
Yannick Fertre4332d112017-04-06 14:19:25 +0200119 return ret;
120 }
121
122 /* reload watchdog */
123 reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
124
125 return 0;
126}
127
128static int stm32_iwdg_ping(struct watchdog_device *wdd)
129{
130 struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
131
132 dev_dbg(wdd->parent, "%s\n", __func__);
133
134 /* reload watchdog */
135 reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
136
137 return 0;
138}
139
140static int stm32_iwdg_set_timeout(struct watchdog_device *wdd,
141 unsigned int timeout)
142{
143 dev_dbg(wdd->parent, "%s timeout: %d sec\n", __func__, timeout);
144
145 wdd->timeout = timeout;
146
147 if (watchdog_active(wdd))
148 return stm32_iwdg_start(wdd);
149
150 return 0;
151}
152
Guenter Roeck1f533052019-04-10 09:27:54 -0700153static void stm32_clk_disable_unprepare(void *data)
154{
155 clk_disable_unprepare(data);
156}
157
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200158static int stm32_iwdg_clk_init(struct platform_device *pdev,
159 struct stm32_iwdg *wdt)
160{
Guenter Roeck1f533052019-04-10 09:27:54 -0700161 struct device *dev = &pdev->dev;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200162 u32 ret;
163
Guenter Roeck1f533052019-04-10 09:27:54 -0700164 wdt->clk_lsi = devm_clk_get(dev, "lsi");
Etienne Carriere7c7164f2020-11-06 15:23:27 +0100165 if (IS_ERR(wdt->clk_lsi))
166 return dev_err_probe(dev, PTR_ERR(wdt->clk_lsi), "Unable to get lsi clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200167
168 /* optional peripheral clock */
Ludovic Barree9974162019-05-03 15:48:26 +0200169 if (wdt->data->has_pclk) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700170 wdt->clk_pclk = devm_clk_get(dev, "pclk");
Etienne Carriere7c7164f2020-11-06 15:23:27 +0100171 if (IS_ERR(wdt->clk_pclk))
172 return dev_err_probe(dev, PTR_ERR(wdt->clk_pclk),
173 "Unable to get pclk clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200174
175 ret = clk_prepare_enable(wdt->clk_pclk);
176 if (ret) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700177 dev_err(dev, "Unable to prepare pclk clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200178 return ret;
179 }
Guenter Roeck1f533052019-04-10 09:27:54 -0700180 ret = devm_add_action_or_reset(dev,
181 stm32_clk_disable_unprepare,
182 wdt->clk_pclk);
183 if (ret)
184 return ret;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200185 }
186
187 ret = clk_prepare_enable(wdt->clk_lsi);
188 if (ret) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700189 dev_err(dev, "Unable to prepare lsi clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200190 return ret;
191 }
Guenter Roeck1f533052019-04-10 09:27:54 -0700192 ret = devm_add_action_or_reset(dev, stm32_clk_disable_unprepare,
193 wdt->clk_lsi);
194 if (ret)
195 return ret;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200196
197 wdt->rate = clk_get_rate(wdt->clk_lsi);
198
199 return 0;
200}
201
Yannick Fertre4332d112017-04-06 14:19:25 +0200202static const struct watchdog_info stm32_iwdg_info = {
203 .options = WDIOF_SETTIMEOUT |
204 WDIOF_MAGICCLOSE |
205 WDIOF_KEEPALIVEPING,
206 .identity = "STM32 Independent Watchdog",
207};
208
Gustavo A. R. Silvad7b16e72017-07-07 19:28:57 -0500209static const struct watchdog_ops stm32_iwdg_ops = {
Yannick Fertre4332d112017-04-06 14:19:25 +0200210 .owner = THIS_MODULE,
211 .start = stm32_iwdg_start,
212 .ping = stm32_iwdg_ping,
213 .set_timeout = stm32_iwdg_set_timeout,
214};
215
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200216static const struct of_device_id stm32_iwdg_of_match[] = {
Ludovic Barree9974162019-05-03 15:48:26 +0200217 { .compatible = "st,stm32-iwdg", .data = &stm32_iwdg_data },
218 { .compatible = "st,stm32mp1-iwdg", .data = &stm32mp1_iwdg_data },
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200219 { /* end node */ }
220};
221MODULE_DEVICE_TABLE(of, stm32_iwdg_of_match);
222
Yannick Fertre4332d112017-04-06 14:19:25 +0200223static int stm32_iwdg_probe(struct platform_device *pdev)
224{
Guenter Roeck1f533052019-04-10 09:27:54 -0700225 struct device *dev = &pdev->dev;
Yannick Fertre4332d112017-04-06 14:19:25 +0200226 struct watchdog_device *wdd;
227 struct stm32_iwdg *wdt;
Yannick Fertre4332d112017-04-06 14:19:25 +0200228 int ret;
229
Guenter Roeck1f533052019-04-10 09:27:54 -0700230 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200231 if (!wdt)
232 return -ENOMEM;
233
Ludovic Barree9974162019-05-03 15:48:26 +0200234 wdt->data = of_device_get_match_data(&pdev->dev);
235 if (!wdt->data)
236 return -ENODEV;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200237
Yannick Fertre4332d112017-04-06 14:19:25 +0200238 /* This is the timer base. */
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700239 wdt->regs = devm_platform_ioremap_resource(pdev, 0);
Tang Bin004920d2021-08-14 22:27:41 +0800240 if (IS_ERR(wdt->regs))
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200241 return PTR_ERR(wdt->regs);
Yannick Fertre4332d112017-04-06 14:19:25 +0200242
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200243 ret = stm32_iwdg_clk_init(pdev, wdt);
244 if (ret)
Yannick Fertre4332d112017-04-06 14:19:25 +0200245 return ret;
Yannick Fertre4332d112017-04-06 14:19:25 +0200246
247 /* Initialize struct watchdog_device. */
248 wdd = &wdt->wdd;
Ludovic Barree9974162019-05-03 15:48:26 +0200249 wdd->parent = dev;
Yannick Fertre4332d112017-04-06 14:19:25 +0200250 wdd->info = &stm32_iwdg_info;
251 wdd->ops = &stm32_iwdg_ops;
Ludovic Barree9974162019-05-03 15:48:26 +0200252 wdd->min_timeout = DIV_ROUND_UP((RLR_MIN + 1) * PR_MIN, wdt->rate);
253 wdd->max_hw_heartbeat_ms = ((RLR_MAX + 1) * wdt->data->max_prescaler *
254 1000) / wdt->rate;
Yannick Fertre4332d112017-04-06 14:19:25 +0200255
256 watchdog_set_drvdata(wdd, wdt);
257 watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
Wolfram Sang6781ce22019-04-19 20:15:59 +0200258 watchdog_init_timeout(wdd, 0, dev);
Yannick Fertre4332d112017-04-06 14:19:25 +0200259
Christophe Roullier85fdc632019-11-22 14:22:46 +0100260 /*
261 * In case of CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED is set
262 * (Means U-Boot/bootloaders leaves the watchdog running)
263 * When we get here we should make a decision to prevent
264 * any side effects before user space daemon will take care of it.
265 * The best option, taking into consideration that there is no
266 * way to read values back from hardware, is to enforce watchdog
267 * being run with deterministic values.
268 */
269 if (IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED)) {
270 ret = stm32_iwdg_start(wdd);
271 if (ret)
272 return ret;
273
274 /* Make sure the watchdog is serviced */
275 set_bit(WDOG_HW_RUNNING, &wdd->status);
276 }
277
Guenter Roeck1f533052019-04-10 09:27:54 -0700278 ret = devm_watchdog_register_device(dev, wdd);
Wolfram Sang71777442019-05-18 23:27:56 +0200279 if (ret)
Guenter Roeck1f533052019-04-10 09:27:54 -0700280 return ret;
Yannick Fertre4332d112017-04-06 14:19:25 +0200281
282 platform_set_drvdata(pdev, wdt);
283
284 return 0;
Yannick Fertre4332d112017-04-06 14:19:25 +0200285}
286
Yannick Fertre4332d112017-04-06 14:19:25 +0200287static struct platform_driver stm32_iwdg_driver = {
288 .probe = stm32_iwdg_probe,
Yannick Fertre4332d112017-04-06 14:19:25 +0200289 .driver = {
290 .name = "iwdg",
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200291 .of_match_table = of_match_ptr(stm32_iwdg_of_match),
Yannick Fertre4332d112017-04-06 14:19:25 +0200292 },
293};
294module_platform_driver(stm32_iwdg_driver);
295
296MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
297MODULE_DESCRIPTION("STMicroelectronics STM32 Independent Watchdog Driver");
298MODULE_LICENSE("GPL v2");