2 * Broadcom BCM63xx SoC watchdog driver
4 * Copyright (C) 2007, Miguel Gaio <miguel.gaio@efixo.com>
5 * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/miscdevice.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/types.h>
24 #include <linux/uaccess.h>
25 #include <linux/watchdog.h>
26 #include <linux/timer.h>
27 #include <linux/jiffies.h>
28 #include <linux/interrupt.h>
29 #include <linux/ptrace.h>
30 #include <linux/resource.h>
31 #include <linux/platform_device.h>
33 #include <bcm63xx_cpu.h>
34 #include <bcm63xx_io.h>
35 #include <bcm63xx_regs.h>
36 #include <bcm63xx_timer.h>
38 #define PFX KBUILD_MODNAME
40 #define WDT_HZ 50000000 /* Fclk */
41 #define WDT_DEFAULT_TIME 30 /* seconds */
42 #define WDT_MAX_TIME 256 /* seconds */
46 struct timer_list timer
;
51 static int expect_close
;
53 static int wdt_time
= WDT_DEFAULT_TIME
;
54 static bool nowayout
= WATCHDOG_NOWAYOUT
;
55 module_param(nowayout
, bool, 0);
56 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
57 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
60 static void bcm63xx_wdt_hw_start(void)
62 bcm_writel(0xfffffffe, bcm63xx_wdt_device
.regs
+ WDT_DEFVAL_REG
);
63 bcm_writel(WDT_START_1
, bcm63xx_wdt_device
.regs
+ WDT_CTL_REG
);
64 bcm_writel(WDT_START_2
, bcm63xx_wdt_device
.regs
+ WDT_CTL_REG
);
67 static void bcm63xx_wdt_hw_stop(void)
69 bcm_writel(WDT_STOP_1
, bcm63xx_wdt_device
.regs
+ WDT_CTL_REG
);
70 bcm_writel(WDT_STOP_2
, bcm63xx_wdt_device
.regs
+ WDT_CTL_REG
);
73 static void bcm63xx_wdt_isr(void *data
)
75 struct pt_regs
*regs
= get_irq_regs();
77 die(PFX
" fire", regs
);
80 static void bcm63xx_timer_tick(struct timer_list
*unused
)
82 if (!atomic_dec_and_test(&bcm63xx_wdt_device
.ticks
)) {
83 bcm63xx_wdt_hw_start();
84 mod_timer(&bcm63xx_wdt_device
.timer
, jiffies
+ HZ
);
86 pr_crit("watchdog will restart system\n");
89 static void bcm63xx_wdt_pet(void)
91 atomic_set(&bcm63xx_wdt_device
.ticks
, wdt_time
);
94 static void bcm63xx_wdt_start(void)
97 bcm63xx_timer_tick(0);
100 static void bcm63xx_wdt_pause(void)
102 del_timer_sync(&bcm63xx_wdt_device
.timer
);
103 bcm63xx_wdt_hw_stop();
106 static int bcm63xx_wdt_settimeout(int new_time
)
108 if ((new_time
<= 0) || (new_time
> WDT_MAX_TIME
))
116 static int bcm63xx_wdt_open(struct inode
*inode
, struct file
*file
)
118 if (test_and_set_bit(0, &bcm63xx_wdt_device
.inuse
))
122 return nonseekable_open(inode
, file
);
125 static int bcm63xx_wdt_release(struct inode
*inode
, struct file
*file
)
127 if (expect_close
== 42)
130 pr_crit("Unexpected close, not stopping watchdog!\n");
133 clear_bit(0, &bcm63xx_wdt_device
.inuse
);
138 static ssize_t
bcm63xx_wdt_write(struct file
*file
, const char *data
,
139 size_t len
, loff_t
*ppos
)
145 /* In case it was set long ago */
148 for (i
= 0; i
!= len
; i
++) {
150 if (get_user(c
, data
+ i
))
161 static struct watchdog_info bcm63xx_wdt_info
= {
163 .options
= WDIOF_SETTIMEOUT
|
164 WDIOF_KEEPALIVEPING
|
169 static long bcm63xx_wdt_ioctl(struct file
*file
, unsigned int cmd
,
172 void __user
*argp
= (void __user
*)arg
;
173 int __user
*p
= argp
;
174 int new_value
, retval
= -EINVAL
;
177 case WDIOC_GETSUPPORT
:
178 return copy_to_user(argp
, &bcm63xx_wdt_info
,
179 sizeof(bcm63xx_wdt_info
)) ? -EFAULT
: 0;
181 case WDIOC_GETSTATUS
:
182 case WDIOC_GETBOOTSTATUS
:
183 return put_user(0, p
);
185 case WDIOC_SETOPTIONS
:
186 if (get_user(new_value
, p
))
189 if (new_value
& WDIOS_DISABLECARD
) {
193 if (new_value
& WDIOS_ENABLECARD
) {
200 case WDIOC_KEEPALIVE
:
204 case WDIOC_SETTIMEOUT
:
205 if (get_user(new_value
, p
))
208 if (bcm63xx_wdt_settimeout(new_value
))
213 case WDIOC_GETTIMEOUT
:
214 return put_user(wdt_time
, p
);
222 static const struct file_operations bcm63xx_wdt_fops
= {
223 .owner
= THIS_MODULE
,
225 .write
= bcm63xx_wdt_write
,
226 .unlocked_ioctl
= bcm63xx_wdt_ioctl
,
227 .open
= bcm63xx_wdt_open
,
228 .release
= bcm63xx_wdt_release
,
231 static struct miscdevice bcm63xx_wdt_miscdev
= {
232 .minor
= WATCHDOG_MINOR
,
234 .fops
= &bcm63xx_wdt_fops
,
238 static int bcm63xx_wdt_probe(struct platform_device
*pdev
)
243 timer_setup(&bcm63xx_wdt_device
.timer
, bcm63xx_timer_tick
, 0);
245 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
247 dev_err(&pdev
->dev
, "failed to get resources\n");
251 bcm63xx_wdt_device
.regs
= devm_ioremap_nocache(&pdev
->dev
, r
->start
,
253 if (!bcm63xx_wdt_device
.regs
) {
254 dev_err(&pdev
->dev
, "failed to remap I/O resources\n");
258 ret
= bcm63xx_timer_register(TIMER_WDT_ID
, bcm63xx_wdt_isr
, NULL
);
260 dev_err(&pdev
->dev
, "failed to register wdt timer isr\n");
264 if (bcm63xx_wdt_settimeout(wdt_time
)) {
265 bcm63xx_wdt_settimeout(WDT_DEFAULT_TIME
);
267 ": wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
271 ret
= misc_register(&bcm63xx_wdt_miscdev
);
273 dev_err(&pdev
->dev
, "failed to register watchdog device\n");
274 goto unregister_timer
;
277 dev_info(&pdev
->dev
, " started, timer margin: %d sec\n",
283 bcm63xx_timer_unregister(TIMER_WDT_ID
);
287 static int bcm63xx_wdt_remove(struct platform_device
*pdev
)
292 misc_deregister(&bcm63xx_wdt_miscdev
);
293 bcm63xx_timer_unregister(TIMER_WDT_ID
);
297 static void bcm63xx_wdt_shutdown(struct platform_device
*pdev
)
302 static struct platform_driver bcm63xx_wdt_driver
= {
303 .probe
= bcm63xx_wdt_probe
,
304 .remove
= bcm63xx_wdt_remove
,
305 .shutdown
= bcm63xx_wdt_shutdown
,
307 .name
= "bcm63xx-wdt",
311 module_platform_driver(bcm63xx_wdt_driver
);
313 MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
314 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
315 MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog");
316 MODULE_LICENSE("GPL");
317 MODULE_ALIAS("platform:bcm63xx-wdt");