1 From 783c7cb4659b53b5e1b809dac5e8cdf250145919 Mon Sep 17 00:00:00 2001
2 From: Sergio Paracuellos <sergio.paracuellos@gmail.com>
3 Date: Tue, 14 Feb 2023 11:39:35 +0100
4 Subject: [PATCH 1/2] watchdog: mt7621-wdt: avoid static global declarations
6 Instead of using static global definitions in driver code, refactor code
7 introducing a new watchdog driver data structure and use it along the
10 Reviewed-by: Guenter Roeck <linux@roeck-us.net>
11 Signed-off-by: Sergio Paracuellos <sergio.paracuellos@gmail.com>
12 Link: https://lore.kernel.org/r/20230214103936.1061078-5-sergio.paracuellos@gmail.com
13 [groeck: unsigned -> unsigned int]
14 Signed-off-by: Guenter Roeck <linux@roeck-us.net>
15 Signed-off-by: Wim Van Sebroeck <wim@linux-watchdog.org>
17 drivers/watchdog/mt7621_wdt.c | 102 +++++++++++++++++++++++++++---------------
18 1 file changed, 65 insertions(+), 37 deletions(-)
20 --- a/drivers/watchdog/mt7621_wdt.c
21 +++ b/drivers/watchdog/mt7621_wdt.c
23 #define TMR1CTL_RESTART BIT(9)
24 #define TMR1CTL_PRESCALE_SHIFT 16
26 -static void __iomem *mt7621_wdt_base;
27 -static struct reset_control *mt7621_wdt_reset;
28 +struct mt7621_wdt_data {
30 + struct reset_control *rst;
31 + struct watchdog_device wdt;
34 static bool nowayout = WATCHDOG_NOWAYOUT;
35 module_param(nowayout, bool, 0);
36 @@ -40,27 +43,31 @@ MODULE_PARM_DESC(nowayout,
37 "Watchdog cannot be stopped once started (default="
38 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
40 -static inline void rt_wdt_w32(unsigned reg, u32 val)
41 +static inline void rt_wdt_w32(void __iomem *base, unsigned int reg, u32 val)
43 - iowrite32(val, mt7621_wdt_base + reg);
44 + iowrite32(val, base + reg);
47 -static inline u32 rt_wdt_r32(unsigned reg)
48 +static inline u32 rt_wdt_r32(void __iomem *base, unsigned int reg)
50 - return ioread32(mt7621_wdt_base + reg);
51 + return ioread32(base + reg);
54 static int mt7621_wdt_ping(struct watchdog_device *w)
56 - rt_wdt_w32(TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
57 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
59 + rt_wdt_w32(drvdata->base, TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
64 static int mt7621_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
66 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
69 - rt_wdt_w32(TIMER_REG_TMR1LOAD, t * 1000);
70 + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1LOAD, t * 1000);
74 @@ -68,29 +75,31 @@ static int mt7621_wdt_set_timeout(struct
76 static int mt7621_wdt_start(struct watchdog_device *w)
78 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
81 /* set the prescaler to 1ms == 1000us */
82 - rt_wdt_w32(TIMER_REG_TMR1CTL, 1000 << TMR1CTL_PRESCALE_SHIFT);
83 + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, 1000 << TMR1CTL_PRESCALE_SHIFT);
85 mt7621_wdt_set_timeout(w, w->timeout);
87 - t = rt_wdt_r32(TIMER_REG_TMR1CTL);
88 + t = rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL);
90 - rt_wdt_w32(TIMER_REG_TMR1CTL, t);
91 + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, t);
96 static int mt7621_wdt_stop(struct watchdog_device *w)
98 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
103 - t = rt_wdt_r32(TIMER_REG_TMR1CTL);
104 + t = rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL);
105 t &= ~TMR1CTL_ENABLE;
106 - rt_wdt_w32(TIMER_REG_TMR1CTL, t);
107 + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, t);
111 @@ -105,7 +114,9 @@ static int mt7621_wdt_bootcause(void)
113 static int mt7621_wdt_is_running(struct watchdog_device *w)
115 - return !!(rt_wdt_r32(TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
116 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
118 + return !!(rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
121 static const struct watchdog_info mt7621_wdt_info = {
122 @@ -121,30 +132,39 @@ static const struct watchdog_ops mt7621_
123 .set_timeout = mt7621_wdt_set_timeout,
126 -static struct watchdog_device mt7621_wdt_dev = {
127 - .info = &mt7621_wdt_info,
128 - .ops = &mt7621_wdt_ops,
130 - .max_timeout = 0xfffful / 1000,
133 static int mt7621_wdt_probe(struct platform_device *pdev)
135 struct device *dev = &pdev->dev;
136 - mt7621_wdt_base = devm_platform_ioremap_resource(pdev, 0);
137 - if (IS_ERR(mt7621_wdt_base))
138 - return PTR_ERR(mt7621_wdt_base);
140 - mt7621_wdt_reset = devm_reset_control_get_exclusive(dev, NULL);
141 - if (!IS_ERR(mt7621_wdt_reset))
142 - reset_control_deassert(mt7621_wdt_reset);
144 - mt7621_wdt_dev.bootstatus = mt7621_wdt_bootcause();
146 - watchdog_init_timeout(&mt7621_wdt_dev, mt7621_wdt_dev.max_timeout,
148 - watchdog_set_nowayout(&mt7621_wdt_dev, nowayout);
149 - if (mt7621_wdt_is_running(&mt7621_wdt_dev)) {
150 + struct watchdog_device *mt7621_wdt;
151 + struct mt7621_wdt_data *drvdata;
154 + drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
158 + drvdata->base = devm_platform_ioremap_resource(pdev, 0);
159 + if (IS_ERR(drvdata->base))
160 + return PTR_ERR(drvdata->base);
162 + drvdata->rst = devm_reset_control_get_exclusive(dev, NULL);
163 + if (!IS_ERR(drvdata->rst))
164 + reset_control_deassert(drvdata->rst);
166 + mt7621_wdt = &drvdata->wdt;
167 + mt7621_wdt->info = &mt7621_wdt_info;
168 + mt7621_wdt->ops = &mt7621_wdt_ops;
169 + mt7621_wdt->min_timeout = 1;
170 + mt7621_wdt->max_timeout = 0xfffful / 1000;
171 + mt7621_wdt->parent = dev;
173 + mt7621_wdt->bootstatus = mt7621_wdt_bootcause();
175 + watchdog_init_timeout(mt7621_wdt, mt7621_wdt->max_timeout, dev);
176 + watchdog_set_nowayout(mt7621_wdt, nowayout);
177 + watchdog_set_drvdata(mt7621_wdt, drvdata);
179 + if (mt7621_wdt_is_running(mt7621_wdt)) {
181 * Make sure to apply timeout from watchdog core, taking
182 * the prescaler of this driver here into account (the
183 @@ -154,17 +174,25 @@ static int mt7621_wdt_probe(struct platf
184 * we first disable the watchdog, set the new prescaler
185 * and timeout, and then re-enable the watchdog.
187 - mt7621_wdt_stop(&mt7621_wdt_dev);
188 - mt7621_wdt_start(&mt7621_wdt_dev);
189 - set_bit(WDOG_HW_RUNNING, &mt7621_wdt_dev.status);
190 + mt7621_wdt_stop(mt7621_wdt);
191 + mt7621_wdt_start(mt7621_wdt);
192 + set_bit(WDOG_HW_RUNNING, &mt7621_wdt->status);
195 - return devm_watchdog_register_device(dev, &mt7621_wdt_dev);
196 + err = devm_watchdog_register_device(dev, &drvdata->wdt);
200 + platform_set_drvdata(pdev, drvdata);
205 static void mt7621_wdt_shutdown(struct platform_device *pdev)
207 - mt7621_wdt_stop(&mt7621_wdt_dev);
208 + struct mt7621_wdt_data *drvdata = platform_get_drvdata(pdev);
210 + mt7621_wdt_stop(&drvdata->wdt);
213 static const struct of_device_id mt7621_wdt_match[] = {