2 * Copyright (C) 2013 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/debugfs.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/platform_device.h>
21 #include <linux/watchdog.h>
23 #define SECWDOG_CTRL_REG 0x00000000
24 #define SECWDOG_COUNT_REG 0x00000004
26 #define SECWDOG_RESERVED_MASK 0x1dffffff
27 #define SECWDOG_WD_LOAD_FLAG 0x10000000
28 #define SECWDOG_EN_MASK 0x08000000
29 #define SECWDOG_SRSTEN_MASK 0x04000000
30 #define SECWDOG_RES_MASK 0x00f00000
31 #define SECWDOG_COUNT_MASK 0x000fffff
33 #define SECWDOG_MAX_COUNT SECWDOG_COUNT_MASK
34 #define SECWDOG_CLKS_SHIFT 20
35 #define SECWDOG_MAX_RES 15
36 #define SECWDOG_DEFAULT_RESOLUTION 4
37 #define SECWDOG_MAX_TRY 1000
39 #define SECS_TO_TICKS(x, w) ((x) << (w)->resolution)
40 #define TICKS_TO_SECS(x, w) ((x) >> (w)->resolution)
42 #define BCM_KONA_WDT_NAME "bcm_kona_wdt"
47 * One watchdog tick is 1/(2^resolution) seconds. Resolution can take
48 * the values 0-15, meaning one tick can be 1s to 30.52us. Our default
49 * resolution of 4 means one tick is 62.5ms.
51 * The watchdog counter is 20 bits. Depending on resolution, the maximum
52 * counter value of 0xfffff expires after about 12 days (resolution 0)
53 * down to only 32s (resolution 15). The default resolution of 4 gives
54 * us a maximum of about 18 hours and 12 minutes before the watchdog
59 #ifdef CONFIG_BCM_KONA_WDT_DEBUG
60 unsigned long busy_count
;
61 struct dentry
*debugfs
;
65 static int secure_register_read(struct bcm_kona_wdt
*wdt
, uint32_t offset
)
71 * If the WD_LOAD_FLAG is set, the watchdog counter field is being
72 * updated in hardware. Once the WD timer is updated in hardware, it
76 if (unlikely(count
> 1))
78 val
= readl_relaxed(wdt
->base
+ offset
);
80 } while ((val
& SECWDOG_WD_LOAD_FLAG
) && count
< SECWDOG_MAX_TRY
);
82 #ifdef CONFIG_BCM_KONA_WDT_DEBUG
83 /* Remember the maximum number iterations due to WD_LOAD_FLAG */
84 if (count
> wdt
->busy_count
)
85 wdt
->busy_count
= count
;
88 /* This is the only place we return a negative value. */
89 if (val
& SECWDOG_WD_LOAD_FLAG
)
92 /* We always mask out reserved bits. */
93 val
&= SECWDOG_RESERVED_MASK
;
98 #ifdef CONFIG_BCM_KONA_WDT_DEBUG
100 static int bcm_kona_wdt_dbg_show(struct seq_file
*s
, void *data
)
102 int ctl_val
, cur_val
;
104 struct bcm_kona_wdt
*wdt
= s
->private;
107 seq_puts(s
, "No device pointer\n");
111 spin_lock_irqsave(&wdt
->lock
, flags
);
112 ctl_val
= secure_register_read(wdt
, SECWDOG_CTRL_REG
);
113 cur_val
= secure_register_read(wdt
, SECWDOG_COUNT_REG
);
114 spin_unlock_irqrestore(&wdt
->lock
, flags
);
116 if (ctl_val
< 0 || cur_val
< 0) {
117 seq_puts(s
, "Error accessing hardware\n");
119 int ctl
, cur
, ctl_sec
, cur_sec
, res
;
121 ctl
= ctl_val
& SECWDOG_COUNT_MASK
;
122 res
= (ctl_val
& SECWDOG_RES_MASK
) >> SECWDOG_CLKS_SHIFT
;
123 cur
= cur_val
& SECWDOG_COUNT_MASK
;
124 ctl_sec
= TICKS_TO_SECS(ctl
, wdt
);
125 cur_sec
= TICKS_TO_SECS(cur
, wdt
);
127 "Resolution: %d / %d\n"
128 "Control: %d s / %d (%#x) ticks\n"
129 "Current: %d s / %d (%#x) ticks\n"
131 res
, wdt
->resolution
,
140 static int bcm_kona_dbg_open(struct inode
*inode
, struct file
*file
)
142 return single_open(file
, bcm_kona_wdt_dbg_show
, inode
->i_private
);
145 static const struct file_operations bcm_kona_dbg_operations
= {
146 .open
= bcm_kona_dbg_open
,
149 .release
= single_release
,
152 static void bcm_kona_wdt_debug_init(struct platform_device
*pdev
)
155 struct bcm_kona_wdt
*wdt
= platform_get_drvdata(pdev
);
162 dir
= debugfs_create_dir(BCM_KONA_WDT_NAME
, NULL
);
163 if (IS_ERR_OR_NULL(dir
))
166 if (debugfs_create_file("info", S_IFREG
| S_IRUGO
, dir
, wdt
,
167 &bcm_kona_dbg_operations
))
170 debugfs_remove_recursive(dir
);
173 static void bcm_kona_wdt_debug_exit(struct platform_device
*pdev
)
175 struct bcm_kona_wdt
*wdt
= platform_get_drvdata(pdev
);
177 if (wdt
&& wdt
->debugfs
) {
178 debugfs_remove_recursive(wdt
->debugfs
);
185 static void bcm_kona_wdt_debug_init(struct platform_device
*pdev
) {}
186 static void bcm_kona_wdt_debug_exit(struct platform_device
*pdev
) {}
188 #endif /* CONFIG_BCM_KONA_WDT_DEBUG */
190 static int bcm_kona_wdt_ctrl_reg_modify(struct bcm_kona_wdt
*wdt
,
191 unsigned mask
, unsigned newval
)
197 spin_lock_irqsave(&wdt
->lock
, flags
);
199 val
= secure_register_read(wdt
, SECWDOG_CTRL_REG
);
205 writel_relaxed(val
, wdt
->base
+ SECWDOG_CTRL_REG
);
208 spin_unlock_irqrestore(&wdt
->lock
, flags
);
213 static int bcm_kona_wdt_set_resolution_reg(struct bcm_kona_wdt
*wdt
)
215 if (wdt
->resolution
> SECWDOG_MAX_RES
)
218 return bcm_kona_wdt_ctrl_reg_modify(wdt
, SECWDOG_RES_MASK
,
219 wdt
->resolution
<< SECWDOG_CLKS_SHIFT
);
222 static int bcm_kona_wdt_set_timeout_reg(struct watchdog_device
*wdog
,
223 unsigned watchdog_flags
)
225 struct bcm_kona_wdt
*wdt
= watchdog_get_drvdata(wdog
);
227 return bcm_kona_wdt_ctrl_reg_modify(wdt
, SECWDOG_COUNT_MASK
,
228 SECS_TO_TICKS(wdog
->timeout
, wdt
) |
232 static int bcm_kona_wdt_set_timeout(struct watchdog_device
*wdog
,
239 static unsigned int bcm_kona_wdt_get_timeleft(struct watchdog_device
*wdog
)
241 struct bcm_kona_wdt
*wdt
= watchdog_get_drvdata(wdog
);
245 spin_lock_irqsave(&wdt
->lock
, flags
);
246 val
= secure_register_read(wdt
, SECWDOG_COUNT_REG
);
247 spin_unlock_irqrestore(&wdt
->lock
, flags
);
252 return TICKS_TO_SECS(val
& SECWDOG_COUNT_MASK
, wdt
);
255 static int bcm_kona_wdt_start(struct watchdog_device
*wdog
)
257 return bcm_kona_wdt_set_timeout_reg(wdog
,
258 SECWDOG_EN_MASK
| SECWDOG_SRSTEN_MASK
);
261 static int bcm_kona_wdt_stop(struct watchdog_device
*wdog
)
263 struct bcm_kona_wdt
*wdt
= watchdog_get_drvdata(wdog
);
265 return bcm_kona_wdt_ctrl_reg_modify(wdt
, SECWDOG_EN_MASK
|
266 SECWDOG_SRSTEN_MASK
, 0);
269 static const struct watchdog_ops bcm_kona_wdt_ops
= {
270 .owner
= THIS_MODULE
,
271 .start
= bcm_kona_wdt_start
,
272 .stop
= bcm_kona_wdt_stop
,
273 .set_timeout
= bcm_kona_wdt_set_timeout
,
274 .get_timeleft
= bcm_kona_wdt_get_timeleft
,
277 static const struct watchdog_info bcm_kona_wdt_info
= {
278 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
|
280 .identity
= "Broadcom Kona Watchdog Timer",
283 static struct watchdog_device bcm_kona_wdt_wdd
= {
284 .info
= &bcm_kona_wdt_info
,
285 .ops
= &bcm_kona_wdt_ops
,
287 .max_timeout
= SECWDOG_MAX_COUNT
>> SECWDOG_DEFAULT_RESOLUTION
,
288 .timeout
= SECWDOG_MAX_COUNT
>> SECWDOG_DEFAULT_RESOLUTION
,
291 static void bcm_kona_wdt_shutdown(struct platform_device
*pdev
)
293 bcm_kona_wdt_stop(&bcm_kona_wdt_wdd
);
296 static int bcm_kona_wdt_probe(struct platform_device
*pdev
)
298 struct device
*dev
= &pdev
->dev
;
299 struct bcm_kona_wdt
*wdt
;
300 struct resource
*res
;
303 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
307 spin_lock_init(&wdt
->lock
);
309 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
310 wdt
->base
= devm_ioremap_resource(dev
, res
);
311 if (IS_ERR(wdt
->base
))
314 wdt
->resolution
= SECWDOG_DEFAULT_RESOLUTION
;
315 ret
= bcm_kona_wdt_set_resolution_reg(wdt
);
317 dev_err(dev
, "Failed to set resolution (error: %d)", ret
);
321 platform_set_drvdata(pdev
, wdt
);
322 watchdog_set_drvdata(&bcm_kona_wdt_wdd
, wdt
);
323 bcm_kona_wdt_wdd
.parent
= &pdev
->dev
;
325 ret
= bcm_kona_wdt_set_timeout_reg(&bcm_kona_wdt_wdd
, 0);
327 dev_err(dev
, "Failed set watchdog timeout");
331 ret
= watchdog_register_device(&bcm_kona_wdt_wdd
);
333 dev_err(dev
, "Failed to register watchdog device");
337 bcm_kona_wdt_debug_init(pdev
);
338 dev_dbg(dev
, "Broadcom Kona Watchdog Timer");
343 static int bcm_kona_wdt_remove(struct platform_device
*pdev
)
345 bcm_kona_wdt_debug_exit(pdev
);
346 bcm_kona_wdt_shutdown(pdev
);
347 watchdog_unregister_device(&bcm_kona_wdt_wdd
);
348 dev_dbg(&pdev
->dev
, "Watchdog driver disabled");
353 static const struct of_device_id bcm_kona_wdt_of_match
[] = {
354 { .compatible
= "brcm,kona-wdt", },
357 MODULE_DEVICE_TABLE(of
, bcm_kona_wdt_of_match
);
359 static struct platform_driver bcm_kona_wdt_driver
= {
361 .name
= BCM_KONA_WDT_NAME
,
362 .of_match_table
= bcm_kona_wdt_of_match
,
364 .probe
= bcm_kona_wdt_probe
,
365 .remove
= bcm_kona_wdt_remove
,
366 .shutdown
= bcm_kona_wdt_shutdown
,
369 module_platform_driver(bcm_kona_wdt_driver
);
371 MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME
);
372 MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
373 MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
374 MODULE_LICENSE("GPL v2");