e06d5621e10fd770ef7a150101834218807ead31
[openwrt/staging/pepe2k.git] /
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
5
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
8 code.
9
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>
16 ---
17 drivers/watchdog/mt7621_wdt.c | 102 +++++++++++++++++++++++++++---------------
18 1 file changed, 65 insertions(+), 37 deletions(-)
19
20 --- a/drivers/watchdog/mt7621_wdt.c
21 +++ b/drivers/watchdog/mt7621_wdt.c
22 @@ -31,8 +31,11 @@
23 #define TMR1CTL_RESTART BIT(9)
24 #define TMR1CTL_PRESCALE_SHIFT 16
25
26 -static void __iomem *mt7621_wdt_base;
27 -static struct reset_control *mt7621_wdt_reset;
28 +struct mt7621_wdt_data {
29 + void __iomem *base;
30 + struct reset_control *rst;
31 + struct watchdog_device wdt;
32 +};
33
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) ")");
39
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)
42 {
43 - iowrite32(val, mt7621_wdt_base + reg);
44 + iowrite32(val, base + reg);
45 }
46
47 -static inline u32 rt_wdt_r32(unsigned reg)
48 +static inline u32 rt_wdt_r32(void __iomem *base, unsigned int reg)
49 {
50 - return ioread32(mt7621_wdt_base + reg);
51 + return ioread32(base + reg);
52 }
53
54 static int mt7621_wdt_ping(struct watchdog_device *w)
55 {
56 - rt_wdt_w32(TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
57 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
58 +
59 + rt_wdt_w32(drvdata->base, TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
60
61 return 0;
62 }
63
64 static int mt7621_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
65 {
66 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
67 +
68 w->timeout = t;
69 - rt_wdt_w32(TIMER_REG_TMR1LOAD, t * 1000);
70 + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1LOAD, t * 1000);
71 mt7621_wdt_ping(w);
72
73 return 0;
74 @@ -68,29 +75,31 @@ static int mt7621_wdt_set_timeout(struct
75
76 static int mt7621_wdt_start(struct watchdog_device *w)
77 {
78 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
79 u32 t;
80
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);
84
85 mt7621_wdt_set_timeout(w, w->timeout);
86
87 - t = rt_wdt_r32(TIMER_REG_TMR1CTL);
88 + t = rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL);
89 t |= TMR1CTL_ENABLE;
90 - rt_wdt_w32(TIMER_REG_TMR1CTL, t);
91 + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, t);
92
93 return 0;
94 }
95
96 static int mt7621_wdt_stop(struct watchdog_device *w)
97 {
98 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
99 u32 t;
100
101 mt7621_wdt_ping(w);
102
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);
108
109 return 0;
110 }
111 @@ -105,7 +114,9 @@ static int mt7621_wdt_bootcause(void)
112
113 static int mt7621_wdt_is_running(struct watchdog_device *w)
114 {
115 - return !!(rt_wdt_r32(TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
116 + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
117 +
118 + return !!(rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
119 }
120
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,
124 };
125
126 -static struct watchdog_device mt7621_wdt_dev = {
127 - .info = &mt7621_wdt_info,
128 - .ops = &mt7621_wdt_ops,
129 - .min_timeout = 1,
130 - .max_timeout = 0xfffful / 1000,
131 -};
132 -
133 static int mt7621_wdt_probe(struct platform_device *pdev)
134 {
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);
139 -
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);
143 -
144 - mt7621_wdt_dev.bootstatus = mt7621_wdt_bootcause();
145 -
146 - watchdog_init_timeout(&mt7621_wdt_dev, mt7621_wdt_dev.max_timeout,
147 - dev);
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;
152 + int err;
153 +
154 + drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
155 + if (!drvdata)
156 + return -ENOMEM;
157 +
158 + drvdata->base = devm_platform_ioremap_resource(pdev, 0);
159 + if (IS_ERR(drvdata->base))
160 + return PTR_ERR(drvdata->base);
161 +
162 + drvdata->rst = devm_reset_control_get_exclusive(dev, NULL);
163 + if (!IS_ERR(drvdata->rst))
164 + reset_control_deassert(drvdata->rst);
165 +
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;
172 +
173 + mt7621_wdt->bootstatus = mt7621_wdt_bootcause();
174 +
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);
178 +
179 + if (mt7621_wdt_is_running(mt7621_wdt)) {
180 /*
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.
186 */
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);
193 }
194
195 - return devm_watchdog_register_device(dev, &mt7621_wdt_dev);
196 + err = devm_watchdog_register_device(dev, &drvdata->wdt);
197 + if (err)
198 + return err;
199 +
200 + platform_set_drvdata(pdev, drvdata);
201 +
202 + return 0;
203 }
204
205 static void mt7621_wdt_shutdown(struct platform_device *pdev)
206 {
207 - mt7621_wdt_stop(&mt7621_wdt_dev);
208 + struct mt7621_wdt_data *drvdata = platform_get_drvdata(pdev);
209 +
210 + mt7621_wdt_stop(&drvdata->wdt);
211 }
212
213 static const struct of_device_id mt7621_wdt_match[] = {