2 * Watchdog driver for Freescale STMP37XX/STMP378X
4 * Author: Vitaly Wool <vital@embeddedalley.com>
6 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
7 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/kernel.h>
15 #include <linux/miscdevice.h>
16 #include <linux/watchdog.h>
17 #include <linux/platform_device.h>
18 #include <linux/spinlock.h>
19 #include <linux/uaccess.h>
20 #include <linux/module.h>
22 #include <mach/platform.h>
23 #include <mach/regs-rtc.h>
25 #define DEFAULT_HEARTBEAT 19
26 #define MAX_HEARTBEAT (0x10000000 >> 6)
28 /* missing bitmask in headers */
29 #define BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER 0x80000000
32 #define WDT_OK_TO_CLOSE 1
34 #define WDOG_COUNTER_RATE 1000 /* 1 kHz clock */
36 static DEFINE_SPINLOCK(stmp3xxx_wdt_io_lock
);
37 static unsigned long wdt_status
;
38 static const bool nowayout
= WATCHDOG_NOWAYOUT
;
39 static int heartbeat
= DEFAULT_HEARTBEAT
;
40 static unsigned long boot_status
;
42 static void wdt_enable(u32 value
)
44 spin_lock(&stmp3xxx_wdt_io_lock
);
45 __raw_writel(value
, REGS_RTC_BASE
+ HW_RTC_WATCHDOG
);
46 stmp3xxx_setl(BM_RTC_CTRL_WATCHDOGEN
, REGS_RTC_BASE
+ HW_RTC_CTRL
);
47 stmp3xxx_setl(BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER
,
48 REGS_RTC_BASE
+ HW_RTC_PERSISTENT1
);
49 spin_unlock(&stmp3xxx_wdt_io_lock
);
52 static void wdt_disable(void)
54 spin_lock(&stmp3xxx_wdt_io_lock
);
55 stmp3xxx_clearl(BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER
,
56 REGS_RTC_BASE
+ HW_RTC_PERSISTENT1
);
57 stmp3xxx_clearl(BM_RTC_CTRL_WATCHDOGEN
, REGS_RTC_BASE
+ HW_RTC_CTRL
);
58 spin_unlock(&stmp3xxx_wdt_io_lock
);
61 static void wdt_ping(void)
63 wdt_enable(heartbeat
* WDOG_COUNTER_RATE
);
66 static int stmp3xxx_wdt_open(struct inode
*inode
, struct file
*file
)
68 if (test_and_set_bit(WDT_IN_USE
, &wdt_status
))
71 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
74 return nonseekable_open(inode
, file
);
77 static ssize_t
stmp3xxx_wdt_write(struct file
*file
, const char __user
*data
,
78 size_t len
, loff_t
*ppos
)
84 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
86 for (i
= 0; i
!= len
; i
++) {
89 if (get_user(c
, data
+ i
))
92 set_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
101 static const struct watchdog_info ident
= {
102 .options
= WDIOF_CARDRESET
|
106 .identity
= "STMP3XXX Watchdog",
109 static long stmp3xxx_wdt_ioctl(struct file
*file
, unsigned int cmd
,
112 void __user
*argp
= (void __user
*)arg
;
113 int __user
*p
= argp
;
114 int new_heartbeat
, opts
;
118 case WDIOC_GETSUPPORT
:
119 ret
= copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
122 case WDIOC_GETSTATUS
:
123 ret
= put_user(0, p
);
126 case WDIOC_GETBOOTSTATUS
:
127 ret
= put_user(boot_status
, p
);
130 case WDIOC_SETOPTIONS
:
131 if (get_user(opts
, p
)) {
135 if (opts
& WDIOS_DISABLECARD
)
137 else if (opts
& WDIOS_ENABLECARD
)
140 pr_debug("%s: unknown option 0x%x\n", __func__
, opts
);
147 case WDIOC_KEEPALIVE
:
152 case WDIOC_SETTIMEOUT
:
153 if (get_user(new_heartbeat
, p
)) {
157 if (new_heartbeat
<= 0 || new_heartbeat
> MAX_HEARTBEAT
) {
162 heartbeat
= new_heartbeat
;
166 case WDIOC_GETTIMEOUT
:
167 ret
= put_user(heartbeat
, p
);
173 static int stmp3xxx_wdt_release(struct inode
*inode
, struct file
*file
)
178 if (!test_bit(WDT_OK_TO_CLOSE
, &wdt_status
)) {
180 pr_debug("%s: Device closed unexpectedly\n", __func__
);
184 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
187 clear_bit(WDT_IN_USE
, &wdt_status
);
192 static const struct file_operations stmp3xxx_wdt_fops
= {
193 .owner
= THIS_MODULE
,
195 .write
= stmp3xxx_wdt_write
,
196 .unlocked_ioctl
= stmp3xxx_wdt_ioctl
,
197 .open
= stmp3xxx_wdt_open
,
198 .release
= stmp3xxx_wdt_release
,
201 static struct miscdevice stmp3xxx_wdt_miscdev
= {
202 .minor
= WATCHDOG_MINOR
,
204 .fops
= &stmp3xxx_wdt_fops
,
207 static int __devinit
stmp3xxx_wdt_probe(struct platform_device
*pdev
)
211 if (heartbeat
< 1 || heartbeat
> MAX_HEARTBEAT
)
212 heartbeat
= DEFAULT_HEARTBEAT
;
214 boot_status
= __raw_readl(REGS_RTC_BASE
+ HW_RTC_PERSISTENT1
) &
215 BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER
;
216 boot_status
= !!boot_status
;
217 stmp3xxx_clearl(BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER
,
218 REGS_RTC_BASE
+ HW_RTC_PERSISTENT1
);
219 wdt_disable(); /* disable for now */
221 ret
= misc_register(&stmp3xxx_wdt_miscdev
);
223 dev_err(&pdev
->dev
, "cannot register misc device\n");
227 pr_info("initialized, heartbeat %d sec\n", heartbeat
);
232 static int __devexit
stmp3xxx_wdt_remove(struct platform_device
*pdev
)
234 misc_deregister(&stmp3xxx_wdt_miscdev
);
239 static int wdt_suspended
;
240 static u32 wdt_saved_time
;
242 static int stmp3xxx_wdt_suspend(struct platform_device
*pdev
,
245 if (__raw_readl(REGS_RTC_BASE
+ HW_RTC_CTRL
) &
246 BM_RTC_CTRL_WATCHDOGEN
) {
248 wdt_saved_time
= __raw_readl(REGS_RTC_BASE
+ HW_RTC_WATCHDOG
);
254 static int stmp3xxx_wdt_resume(struct platform_device
*pdev
)
257 wdt_enable(wdt_saved_time
);
263 #define stmp3xxx_wdt_suspend NULL
264 #define stmp3xxx_wdt_resume NULL
267 static struct platform_driver platform_wdt_driver
= {
269 .name
= "stmp3xxx_wdt",
271 .probe
= stmp3xxx_wdt_probe
,
272 .remove
= __devexit_p(stmp3xxx_wdt_remove
),
273 .suspend
= stmp3xxx_wdt_suspend
,
274 .resume
= stmp3xxx_wdt_resume
,
277 module_platform_driver(platform_wdt_driver
);
279 MODULE_DESCRIPTION("STMP3XXX Watchdog Driver");
280 MODULE_LICENSE("GPL");
282 module_param(heartbeat
, int, 0);
283 MODULE_PARM_DESC(heartbeat
,
284 "Watchdog heartbeat period in seconds from 1 to "
285 __MODULE_STRING(MAX_HEARTBEAT
) ", default "
286 __MODULE_STRING(DEFAULT_HEARTBEAT
));
288 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);