blob: 68756a40eb36f0fe1ff64fd13f3a872e62a53279 [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
37/* IWDG_PR register bit values */
38#define PR_4 0x00 /* prescaler set to 4 */
39#define PR_8 0x01 /* prescaler set to 8 */
40#define PR_16 0x02 /* prescaler set to 16 */
41#define PR_32 0x03 /* prescaler set to 32 */
42#define PR_64 0x04 /* prescaler set to 64 */
43#define PR_128 0x05 /* prescaler set to 128 */
44#define PR_256 0x06 /* prescaler set to 256 */
45
46/* IWDG_RLR register values */
47#define RLR_MIN 0x07C /* min value supported by reload register */
48#define RLR_MAX 0xFFF /* max value supported by reload register */
49
50/* IWDG_SR register bit mask */
51#define FLAG_PVU BIT(0) /* Watchdog prescaler value update */
52#define FLAG_RVU BIT(1) /* Watchdog counter reload value update */
53
54/* set timeout to 100000 us */
55#define TIMEOUT_US 100000
56#define SLEEP_US 1000
57
Ludovic Barrec2cf4662018-06-25 17:43:00 +020058#define HAS_PCLK true
59
Yannick Fertre4332d112017-04-06 14:19:25 +020060struct stm32_iwdg {
61 struct watchdog_device wdd;
62 void __iomem *regs;
Ludovic Barrec2cf4662018-06-25 17:43:00 +020063 struct clk *clk_lsi;
64 struct clk *clk_pclk;
Yannick Fertre4332d112017-04-06 14:19:25 +020065 unsigned int rate;
Ludovic Barrec2cf4662018-06-25 17:43:00 +020066 bool has_pclk;
Yannick Fertre4332d112017-04-06 14:19:25 +020067};
68
69static inline u32 reg_read(void __iomem *base, u32 reg)
70{
71 return readl_relaxed(base + reg);
72}
73
74static inline void reg_write(void __iomem *base, u32 reg, u32 val)
75{
76 writel_relaxed(val, base + reg);
77}
78
79static int stm32_iwdg_start(struct watchdog_device *wdd)
80{
81 struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
82 u32 val = FLAG_PVU | FLAG_RVU;
83 u32 reload;
84 int ret;
85
86 dev_dbg(wdd->parent, "%s\n", __func__);
87
88 /* prescaler fixed to 256 */
89 reload = clamp_t(unsigned int, ((wdd->timeout * wdt->rate) / 256) - 1,
90 RLR_MIN, RLR_MAX);
91
92 /* enable write access */
93 reg_write(wdt->regs, IWDG_KR, KR_KEY_EWA);
94
95 /* set prescaler & reload registers */
96 reg_write(wdt->regs, IWDG_PR, PR_256); /* prescaler fix to 256 */
97 reg_write(wdt->regs, IWDG_RLR, reload);
98 reg_write(wdt->regs, IWDG_KR, KR_KEY_ENABLE);
99
100 /* wait for the registers to be updated (max 100ms) */
101 ret = readl_relaxed_poll_timeout(wdt->regs + IWDG_SR, val,
102 !(val & (FLAG_PVU | FLAG_RVU)),
103 SLEEP_US, TIMEOUT_US);
104 if (ret) {
105 dev_err(wdd->parent,
106 "Fail to set prescaler or reload registers\n");
107 return ret;
108 }
109
110 /* reload watchdog */
111 reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
112
113 return 0;
114}
115
116static int stm32_iwdg_ping(struct watchdog_device *wdd)
117{
118 struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
119
120 dev_dbg(wdd->parent, "%s\n", __func__);
121
122 /* reload watchdog */
123 reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
124
125 return 0;
126}
127
128static int stm32_iwdg_set_timeout(struct watchdog_device *wdd,
129 unsigned int timeout)
130{
131 dev_dbg(wdd->parent, "%s timeout: %d sec\n", __func__, timeout);
132
133 wdd->timeout = timeout;
134
135 if (watchdog_active(wdd))
136 return stm32_iwdg_start(wdd);
137
138 return 0;
139}
140
Guenter Roeck1f533052019-04-10 09:27:54 -0700141static void stm32_clk_disable_unprepare(void *data)
142{
143 clk_disable_unprepare(data);
144}
145
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200146static int stm32_iwdg_clk_init(struct platform_device *pdev,
147 struct stm32_iwdg *wdt)
148{
Guenter Roeck1f533052019-04-10 09:27:54 -0700149 struct device *dev = &pdev->dev;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200150 u32 ret;
151
Guenter Roeck1f533052019-04-10 09:27:54 -0700152 wdt->clk_lsi = devm_clk_get(dev, "lsi");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200153 if (IS_ERR(wdt->clk_lsi)) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700154 dev_err(dev, "Unable to get lsi clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200155 return PTR_ERR(wdt->clk_lsi);
156 }
157
158 /* optional peripheral clock */
159 if (wdt->has_pclk) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700160 wdt->clk_pclk = devm_clk_get(dev, "pclk");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200161 if (IS_ERR(wdt->clk_pclk)) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700162 dev_err(dev, "Unable to get pclk clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200163 return PTR_ERR(wdt->clk_pclk);
164 }
165
166 ret = clk_prepare_enable(wdt->clk_pclk);
167 if (ret) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700168 dev_err(dev, "Unable to prepare pclk clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200169 return ret;
170 }
Guenter Roeck1f533052019-04-10 09:27:54 -0700171 ret = devm_add_action_or_reset(dev,
172 stm32_clk_disable_unprepare,
173 wdt->clk_pclk);
174 if (ret)
175 return ret;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200176 }
177
178 ret = clk_prepare_enable(wdt->clk_lsi);
179 if (ret) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700180 dev_err(dev, "Unable to prepare lsi clock\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200181 return ret;
182 }
Guenter Roeck1f533052019-04-10 09:27:54 -0700183 ret = devm_add_action_or_reset(dev, stm32_clk_disable_unprepare,
184 wdt->clk_lsi);
185 if (ret)
186 return ret;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200187
188 wdt->rate = clk_get_rate(wdt->clk_lsi);
189
190 return 0;
191}
192
Yannick Fertre4332d112017-04-06 14:19:25 +0200193static const struct watchdog_info stm32_iwdg_info = {
194 .options = WDIOF_SETTIMEOUT |
195 WDIOF_MAGICCLOSE |
196 WDIOF_KEEPALIVEPING,
197 .identity = "STM32 Independent Watchdog",
198};
199
Gustavo A. R. Silvad7b16e72017-07-07 19:28:57 -0500200static const struct watchdog_ops stm32_iwdg_ops = {
Yannick Fertre4332d112017-04-06 14:19:25 +0200201 .owner = THIS_MODULE,
202 .start = stm32_iwdg_start,
203 .ping = stm32_iwdg_ping,
204 .set_timeout = stm32_iwdg_set_timeout,
205};
206
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200207static const struct of_device_id stm32_iwdg_of_match[] = {
208 { .compatible = "st,stm32-iwdg", .data = (void *)!HAS_PCLK },
209 { .compatible = "st,stm32mp1-iwdg", .data = (void *)HAS_PCLK },
210 { /* end node */ }
211};
212MODULE_DEVICE_TABLE(of, stm32_iwdg_of_match);
213
Yannick Fertre4332d112017-04-06 14:19:25 +0200214static int stm32_iwdg_probe(struct platform_device *pdev)
215{
Guenter Roeck1f533052019-04-10 09:27:54 -0700216 struct device *dev = &pdev->dev;
Yannick Fertre4332d112017-04-06 14:19:25 +0200217 struct watchdog_device *wdd;
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200218 const struct of_device_id *match;
Yannick Fertre4332d112017-04-06 14:19:25 +0200219 struct stm32_iwdg *wdt;
Yannick Fertre4332d112017-04-06 14:19:25 +0200220 int ret;
221
Guenter Roeck1f533052019-04-10 09:27:54 -0700222 match = of_match_device(stm32_iwdg_of_match, dev);
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200223 if (!match)
224 return -ENODEV;
225
Guenter Roeck1f533052019-04-10 09:27:54 -0700226 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200227 if (!wdt)
228 return -ENOMEM;
229
230 wdt->has_pclk = match->data;
231
Yannick Fertre4332d112017-04-06 14:19:25 +0200232 /* This is the timer base. */
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700233 wdt->regs = devm_platform_ioremap_resource(pdev, 0);
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200234 if (IS_ERR(wdt->regs)) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700235 dev_err(dev, "Could not get resource\n");
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200236 return PTR_ERR(wdt->regs);
Yannick Fertre4332d112017-04-06 14:19:25 +0200237 }
238
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200239 ret = stm32_iwdg_clk_init(pdev, wdt);
240 if (ret)
Yannick Fertre4332d112017-04-06 14:19:25 +0200241 return ret;
Yannick Fertre4332d112017-04-06 14:19:25 +0200242
243 /* Initialize struct watchdog_device. */
244 wdd = &wdt->wdd;
245 wdd->info = &stm32_iwdg_info;
246 wdd->ops = &stm32_iwdg_ops;
247 wdd->min_timeout = ((RLR_MIN + 1) * 256) / wdt->rate;
248 wdd->max_hw_heartbeat_ms = ((RLR_MAX + 1) * 256 * 1000) / wdt->rate;
Guenter Roeck1f533052019-04-10 09:27:54 -0700249 wdd->parent = dev;
Yannick Fertre4332d112017-04-06 14:19:25 +0200250
251 watchdog_set_drvdata(wdd, wdt);
252 watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
253
Guenter Roeck1f533052019-04-10 09:27:54 -0700254 ret = watchdog_init_timeout(wdd, 0, dev);
Yannick Fertre4332d112017-04-06 14:19:25 +0200255 if (ret)
Guenter Roeck1f533052019-04-10 09:27:54 -0700256 dev_warn(dev, "unable to set timeout value, using default\n");
Yannick Fertre4332d112017-04-06 14:19:25 +0200257
Guenter Roeck1f533052019-04-10 09:27:54 -0700258 ret = devm_watchdog_register_device(dev, wdd);
Yannick Fertre4332d112017-04-06 14:19:25 +0200259 if (ret) {
Guenter Roeck1f533052019-04-10 09:27:54 -0700260 dev_err(dev, "failed to register watchdog device\n");
261 return ret;
Yannick Fertre4332d112017-04-06 14:19:25 +0200262 }
263
264 platform_set_drvdata(pdev, wdt);
265
266 return 0;
Yannick Fertre4332d112017-04-06 14:19:25 +0200267}
268
Yannick Fertre4332d112017-04-06 14:19:25 +0200269static struct platform_driver stm32_iwdg_driver = {
270 .probe = stm32_iwdg_probe,
Yannick Fertre4332d112017-04-06 14:19:25 +0200271 .driver = {
272 .name = "iwdg",
Ludovic Barrec2cf4662018-06-25 17:43:00 +0200273 .of_match_table = of_match_ptr(stm32_iwdg_of_match),
Yannick Fertre4332d112017-04-06 14:19:25 +0200274 },
275};
276module_platform_driver(stm32_iwdg_driver);
277
278MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
279MODULE_DESCRIPTION("STMicroelectronics STM32 Independent Watchdog Driver");
280MODULE_LICENSE("GPL v2");