blob: 4249b47902bd5321bf7955b3f86bb773b8c2a305 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0
Markus Mayer6adb7302013-11-22 14:56:03 -08002/*
3 * Copyright (C) 2013 Broadcom Corporation
4 *
Markus Mayer6adb7302013-11-22 14:56:03 -08005 */
6
Markus Mayer6e2ac202014-01-06 13:56:10 -08007#include <linux/debugfs.h>
Markus Mayer6adb7302013-11-22 14:56:03 -08008#include <linux/delay.h>
9#include <linux/err.h>
10#include <linux/io.h>
11#include <linux/module.h>
12#include <linux/of_address.h>
13#include <linux/platform_device.h>
14#include <linux/watchdog.h>
15
16#define SECWDOG_CTRL_REG 0x00000000
17#define SECWDOG_COUNT_REG 0x00000004
18
19#define SECWDOG_RESERVED_MASK 0x1dffffff
20#define SECWDOG_WD_LOAD_FLAG 0x10000000
21#define SECWDOG_EN_MASK 0x08000000
22#define SECWDOG_SRSTEN_MASK 0x04000000
23#define SECWDOG_RES_MASK 0x00f00000
24#define SECWDOG_COUNT_MASK 0x000fffff
25
26#define SECWDOG_MAX_COUNT SECWDOG_COUNT_MASK
27#define SECWDOG_CLKS_SHIFT 20
28#define SECWDOG_MAX_RES 15
29#define SECWDOG_DEFAULT_RESOLUTION 4
30#define SECWDOG_MAX_TRY 1000
31
32#define SECS_TO_TICKS(x, w) ((x) << (w)->resolution)
33#define TICKS_TO_SECS(x, w) ((x) >> (w)->resolution)
34
35#define BCM_KONA_WDT_NAME "bcm_kona_wdt"
36
37struct bcm_kona_wdt {
38 void __iomem *base;
39 /*
40 * One watchdog tick is 1/(2^resolution) seconds. Resolution can take
41 * the values 0-15, meaning one tick can be 1s to 30.52us. Our default
42 * resolution of 4 means one tick is 62.5ms.
43 *
44 * The watchdog counter is 20 bits. Depending on resolution, the maximum
45 * counter value of 0xfffff expires after about 12 days (resolution 0)
46 * down to only 32s (resolution 15). The default resolution of 4 gives
47 * us a maximum of about 18 hours and 12 minutes before the watchdog
48 * times out.
49 */
50 int resolution;
51 spinlock_t lock;
Markus Mayer6e2ac202014-01-06 13:56:10 -080052#ifdef CONFIG_BCM_KONA_WDT_DEBUG
53 unsigned long busy_count;
54 struct dentry *debugfs;
55#endif
Markus Mayer6adb7302013-11-22 14:56:03 -080056};
57
Markus Mayer6e2ac202014-01-06 13:56:10 -080058static int secure_register_read(struct bcm_kona_wdt *wdt, uint32_t offset)
Markus Mayer6adb7302013-11-22 14:56:03 -080059{
60 uint32_t val;
61 unsigned count = 0;
62
63 /*
64 * If the WD_LOAD_FLAG is set, the watchdog counter field is being
65 * updated in hardware. Once the WD timer is updated in hardware, it
66 * gets cleared.
67 */
68 do {
69 if (unlikely(count > 1))
70 udelay(5);
Markus Mayer6e2ac202014-01-06 13:56:10 -080071 val = readl_relaxed(wdt->base + offset);
Markus Mayer6adb7302013-11-22 14:56:03 -080072 count++;
73 } while ((val & SECWDOG_WD_LOAD_FLAG) && count < SECWDOG_MAX_TRY);
74
Markus Mayer6e2ac202014-01-06 13:56:10 -080075#ifdef CONFIG_BCM_KONA_WDT_DEBUG
76 /* Remember the maximum number iterations due to WD_LOAD_FLAG */
77 if (count > wdt->busy_count)
78 wdt->busy_count = count;
79#endif
80
Markus Mayer6adb7302013-11-22 14:56:03 -080081 /* This is the only place we return a negative value. */
82 if (val & SECWDOG_WD_LOAD_FLAG)
83 return -ETIMEDOUT;
84
85 /* We always mask out reserved bits. */
86 val &= SECWDOG_RESERVED_MASK;
87
88 return val;
89}
90
Markus Mayer6e2ac202014-01-06 13:56:10 -080091#ifdef CONFIG_BCM_KONA_WDT_DEBUG
92
Yangtao Li57808f42018-12-01 09:51:39 -050093static int bcm_kona_show(struct seq_file *s, void *data)
Markus Mayer6e2ac202014-01-06 13:56:10 -080094{
Joe Perchese1dbde22015-02-21 18:53:48 -080095 int ctl_val, cur_val;
Markus Mayer6e2ac202014-01-06 13:56:10 -080096 unsigned long flags;
97 struct bcm_kona_wdt *wdt = s->private;
98
Joe Perchese1dbde22015-02-21 18:53:48 -080099 if (!wdt) {
100 seq_puts(s, "No device pointer\n");
101 return 0;
102 }
Markus Mayer6e2ac202014-01-06 13:56:10 -0800103
104 spin_lock_irqsave(&wdt->lock, flags);
105 ctl_val = secure_register_read(wdt, SECWDOG_CTRL_REG);
106 cur_val = secure_register_read(wdt, SECWDOG_COUNT_REG);
107 spin_unlock_irqrestore(&wdt->lock, flags);
108
109 if (ctl_val < 0 || cur_val < 0) {
Joe Perchese1dbde22015-02-21 18:53:48 -0800110 seq_puts(s, "Error accessing hardware\n");
Markus Mayer6e2ac202014-01-06 13:56:10 -0800111 } else {
112 int ctl, cur, ctl_sec, cur_sec, res;
113
114 ctl = ctl_val & SECWDOG_COUNT_MASK;
115 res = (ctl_val & SECWDOG_RES_MASK) >> SECWDOG_CLKS_SHIFT;
116 cur = cur_val & SECWDOG_COUNT_MASK;
117 ctl_sec = TICKS_TO_SECS(ctl, wdt);
118 cur_sec = TICKS_TO_SECS(cur, wdt);
Joe Perchese1dbde22015-02-21 18:53:48 -0800119 seq_printf(s,
120 "Resolution: %d / %d\n"
121 "Control: %d s / %d (%#x) ticks\n"
122 "Current: %d s / %d (%#x) ticks\n"
123 "Busy count: %lu\n",
124 res, wdt->resolution,
125 ctl_sec, ctl, ctl,
126 cur_sec, cur, cur,
127 wdt->busy_count);
Markus Mayer6e2ac202014-01-06 13:56:10 -0800128 }
129
Joe Perchese1dbde22015-02-21 18:53:48 -0800130 return 0;
Markus Mayer6e2ac202014-01-06 13:56:10 -0800131}
132
Yangtao Li57808f42018-12-01 09:51:39 -0500133DEFINE_SHOW_ATTRIBUTE(bcm_kona);
Markus Mayer6e2ac202014-01-06 13:56:10 -0800134
135static void bcm_kona_wdt_debug_init(struct platform_device *pdev)
136{
137 struct dentry *dir;
138 struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
139
140 if (!wdt)
141 return;
142
143 wdt->debugfs = NULL;
144
145 dir = debugfs_create_dir(BCM_KONA_WDT_NAME, NULL);
146 if (IS_ERR_OR_NULL(dir))
147 return;
148
149 if (debugfs_create_file("info", S_IFREG | S_IRUGO, dir, wdt,
Yangtao Li57808f42018-12-01 09:51:39 -0500150 &bcm_kona_fops))
Markus Mayer6e2ac202014-01-06 13:56:10 -0800151 wdt->debugfs = dir;
152 else
153 debugfs_remove_recursive(dir);
154}
155
156static void bcm_kona_wdt_debug_exit(struct platform_device *pdev)
157{
158 struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
159
160 if (wdt && wdt->debugfs) {
161 debugfs_remove_recursive(wdt->debugfs);
162 wdt->debugfs = NULL;
163 }
164}
165
166#else
167
168static void bcm_kona_wdt_debug_init(struct platform_device *pdev) {}
169static void bcm_kona_wdt_debug_exit(struct platform_device *pdev) {}
170
171#endif /* CONFIG_BCM_KONA_WDT_DEBUG */
172
Markus Mayer6adb7302013-11-22 14:56:03 -0800173static int bcm_kona_wdt_ctrl_reg_modify(struct bcm_kona_wdt *wdt,
174 unsigned mask, unsigned newval)
175{
176 int val;
177 unsigned long flags;
178 int ret = 0;
179
180 spin_lock_irqsave(&wdt->lock, flags);
181
Markus Mayer6e2ac202014-01-06 13:56:10 -0800182 val = secure_register_read(wdt, SECWDOG_CTRL_REG);
Markus Mayer6adb7302013-11-22 14:56:03 -0800183 if (val < 0) {
184 ret = val;
185 } else {
186 val &= ~mask;
187 val |= newval;
188 writel_relaxed(val, wdt->base + SECWDOG_CTRL_REG);
189 }
190
191 spin_unlock_irqrestore(&wdt->lock, flags);
192
193 return ret;
194}
195
196static int bcm_kona_wdt_set_resolution_reg(struct bcm_kona_wdt *wdt)
197{
198 if (wdt->resolution > SECWDOG_MAX_RES)
199 return -EINVAL;
200
201 return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_RES_MASK,
202 wdt->resolution << SECWDOG_CLKS_SHIFT);
203}
204
205static int bcm_kona_wdt_set_timeout_reg(struct watchdog_device *wdog,
206 unsigned watchdog_flags)
207{
208 struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
209
210 return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_COUNT_MASK,
211 SECS_TO_TICKS(wdog->timeout, wdt) |
212 watchdog_flags);
213}
214
215static int bcm_kona_wdt_set_timeout(struct watchdog_device *wdog,
216 unsigned int t)
217{
218 wdog->timeout = t;
219 return 0;
220}
221
222static unsigned int bcm_kona_wdt_get_timeleft(struct watchdog_device *wdog)
223{
224 struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
225 int val;
226 unsigned long flags;
227
228 spin_lock_irqsave(&wdt->lock, flags);
Markus Mayer6e2ac202014-01-06 13:56:10 -0800229 val = secure_register_read(wdt, SECWDOG_COUNT_REG);
Markus Mayer6adb7302013-11-22 14:56:03 -0800230 spin_unlock_irqrestore(&wdt->lock, flags);
231
232 if (val < 0)
233 return val;
234
235 return TICKS_TO_SECS(val & SECWDOG_COUNT_MASK, wdt);
236}
237
238static int bcm_kona_wdt_start(struct watchdog_device *wdog)
239{
240 return bcm_kona_wdt_set_timeout_reg(wdog,
241 SECWDOG_EN_MASK | SECWDOG_SRSTEN_MASK);
242}
243
244static int bcm_kona_wdt_stop(struct watchdog_device *wdog)
245{
246 struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
247
248 return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_EN_MASK |
249 SECWDOG_SRSTEN_MASK, 0);
250}
251
Bhumika Goyalb893e342017-01-28 13:11:17 +0530252static const struct watchdog_ops bcm_kona_wdt_ops = {
Markus Mayer6adb7302013-11-22 14:56:03 -0800253 .owner = THIS_MODULE,
254 .start = bcm_kona_wdt_start,
255 .stop = bcm_kona_wdt_stop,
256 .set_timeout = bcm_kona_wdt_set_timeout,
257 .get_timeleft = bcm_kona_wdt_get_timeleft,
258};
259
Bhumika Goyal6c368932016-12-26 22:35:11 +0530260static const struct watchdog_info bcm_kona_wdt_info = {
Markus Mayer6adb7302013-11-22 14:56:03 -0800261 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
262 WDIOF_KEEPALIVEPING,
263 .identity = "Broadcom Kona Watchdog Timer",
264};
265
266static struct watchdog_device bcm_kona_wdt_wdd = {
267 .info = &bcm_kona_wdt_info,
268 .ops = &bcm_kona_wdt_ops,
269 .min_timeout = 1,
270 .max_timeout = SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
271 .timeout = SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
272};
273
274static void bcm_kona_wdt_shutdown(struct platform_device *pdev)
275{
276 bcm_kona_wdt_stop(&bcm_kona_wdt_wdd);
277}
278
279static int bcm_kona_wdt_probe(struct platform_device *pdev)
280{
281 struct device *dev = &pdev->dev;
282 struct bcm_kona_wdt *wdt;
283 struct resource *res;
284 int ret;
285
286 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
287 if (!wdt)
288 return -ENOMEM;
289
Eric Anholtfedf266f2017-04-27 18:02:32 -0700290 spin_lock_init(&wdt->lock);
291
Markus Mayer6adb7302013-11-22 14:56:03 -0800292 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
293 wdt->base = devm_ioremap_resource(dev, res);
294 if (IS_ERR(wdt->base))
295 return -ENODEV;
296
297 wdt->resolution = SECWDOG_DEFAULT_RESOLUTION;
298 ret = bcm_kona_wdt_set_resolution_reg(wdt);
299 if (ret) {
300 dev_err(dev, "Failed to set resolution (error: %d)", ret);
301 return ret;
302 }
303
Markus Mayer6adb7302013-11-22 14:56:03 -0800304 platform_set_drvdata(pdev, wdt);
305 watchdog_set_drvdata(&bcm_kona_wdt_wdd, wdt);
Pratyush Anand65518812015-08-20 14:05:01 +0530306 bcm_kona_wdt_wdd.parent = &pdev->dev;
Markus Mayer6adb7302013-11-22 14:56:03 -0800307
308 ret = bcm_kona_wdt_set_timeout_reg(&bcm_kona_wdt_wdd, 0);
309 if (ret) {
310 dev_err(dev, "Failed set watchdog timeout");
311 return ret;
312 }
313
314 ret = watchdog_register_device(&bcm_kona_wdt_wdd);
315 if (ret) {
316 dev_err(dev, "Failed to register watchdog device");
317 return ret;
318 }
319
Markus Mayer6e2ac202014-01-06 13:56:10 -0800320 bcm_kona_wdt_debug_init(pdev);
Markus Mayer6adb7302013-11-22 14:56:03 -0800321 dev_dbg(dev, "Broadcom Kona Watchdog Timer");
322
323 return 0;
324}
325
326static int bcm_kona_wdt_remove(struct platform_device *pdev)
327{
Markus Mayer6e2ac202014-01-06 13:56:10 -0800328 bcm_kona_wdt_debug_exit(pdev);
Markus Mayer6adb7302013-11-22 14:56:03 -0800329 bcm_kona_wdt_shutdown(pdev);
330 watchdog_unregister_device(&bcm_kona_wdt_wdd);
331 dev_dbg(&pdev->dev, "Watchdog driver disabled");
332
333 return 0;
334}
335
336static const struct of_device_id bcm_kona_wdt_of_match[] = {
337 { .compatible = "brcm,kona-wdt", },
338 {},
339};
340MODULE_DEVICE_TABLE(of, bcm_kona_wdt_of_match);
341
342static struct platform_driver bcm_kona_wdt_driver = {
343 .driver = {
344 .name = BCM_KONA_WDT_NAME,
Markus Mayer6adb7302013-11-22 14:56:03 -0800345 .of_match_table = bcm_kona_wdt_of_match,
346 },
347 .probe = bcm_kona_wdt_probe,
348 .remove = bcm_kona_wdt_remove,
349 .shutdown = bcm_kona_wdt_shutdown,
350};
351
352module_platform_driver(bcm_kona_wdt_driver);
353
354MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME);
355MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
356MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
357MODULE_LICENSE("GPL v2");