1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * drivers/char/watchdog/ixp4xx_wdt.c
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Watchdog driver for Intel IXP4xx network processors
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Author: Deepak Saxena <dsaxena@plexity.net>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Copyright 2004 (c) MontaVista, Software, Inc.
9*4882a593Smuzhiyun * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * This file is licensed under the terms of the GNU General Public
12*4882a593Smuzhiyun * License version 2. This program is licensed "as is" without any
13*4882a593Smuzhiyun * warranty of any kind, whether express or implied.
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/moduleparam.h>
20*4882a593Smuzhiyun #include <linux/types.h>
21*4882a593Smuzhiyun #include <linux/kernel.h>
22*4882a593Smuzhiyun #include <linux/fs.h>
23*4882a593Smuzhiyun #include <linux/miscdevice.h>
24*4882a593Smuzhiyun #include <linux/of.h>
25*4882a593Smuzhiyun #include <linux/watchdog.h>
26*4882a593Smuzhiyun #include <linux/init.h>
27*4882a593Smuzhiyun #include <linux/bitops.h>
28*4882a593Smuzhiyun #include <linux/uaccess.h>
29*4882a593Smuzhiyun #include <mach/hardware.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
32*4882a593Smuzhiyun static int heartbeat = 60; /* (secs) Default is 1 minute */
33*4882a593Smuzhiyun static unsigned long wdt_status;
34*4882a593Smuzhiyun static unsigned long boot_status;
35*4882a593Smuzhiyun static DEFINE_SPINLOCK(wdt_lock);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define WDT_IN_USE 0
40*4882a593Smuzhiyun #define WDT_OK_TO_CLOSE 1
41*4882a593Smuzhiyun
wdt_enable(void)42*4882a593Smuzhiyun static void wdt_enable(void)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun spin_lock(&wdt_lock);
45*4882a593Smuzhiyun *IXP4XX_OSWK = IXP4XX_WDT_KEY;
46*4882a593Smuzhiyun *IXP4XX_OSWE = 0;
47*4882a593Smuzhiyun *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
48*4882a593Smuzhiyun *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
49*4882a593Smuzhiyun *IXP4XX_OSWK = 0;
50*4882a593Smuzhiyun spin_unlock(&wdt_lock);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
wdt_disable(void)53*4882a593Smuzhiyun static void wdt_disable(void)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun spin_lock(&wdt_lock);
56*4882a593Smuzhiyun *IXP4XX_OSWK = IXP4XX_WDT_KEY;
57*4882a593Smuzhiyun *IXP4XX_OSWE = 0;
58*4882a593Smuzhiyun *IXP4XX_OSWK = 0;
59*4882a593Smuzhiyun spin_unlock(&wdt_lock);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
ixp4xx_wdt_open(struct inode * inode,struct file * file)62*4882a593Smuzhiyun static int ixp4xx_wdt_open(struct inode *inode, struct file *file)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun if (test_and_set_bit(WDT_IN_USE, &wdt_status))
65*4882a593Smuzhiyun return -EBUSY;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
68*4882a593Smuzhiyun wdt_enable();
69*4882a593Smuzhiyun return stream_open(inode, file);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun static ssize_t
ixp4xx_wdt_write(struct file * file,const char * data,size_t len,loff_t * ppos)73*4882a593Smuzhiyun ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun if (len) {
76*4882a593Smuzhiyun if (!nowayout) {
77*4882a593Smuzhiyun size_t i;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun for (i = 0; i != len; i++) {
82*4882a593Smuzhiyun char c;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if (get_user(c, data + i))
85*4882a593Smuzhiyun return -EFAULT;
86*4882a593Smuzhiyun if (c == 'V')
87*4882a593Smuzhiyun set_bit(WDT_OK_TO_CLOSE, &wdt_status);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun wdt_enable();
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun return len;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun static const struct watchdog_info ident = {
96*4882a593Smuzhiyun .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
97*4882a593Smuzhiyun WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
98*4882a593Smuzhiyun .identity = "IXP4xx Watchdog",
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun
ixp4xx_wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)102*4882a593Smuzhiyun static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd,
103*4882a593Smuzhiyun unsigned long arg)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun int ret = -ENOTTY;
106*4882a593Smuzhiyun int time;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun switch (cmd) {
109*4882a593Smuzhiyun case WDIOC_GETSUPPORT:
110*4882a593Smuzhiyun ret = copy_to_user((struct watchdog_info *)arg, &ident,
111*4882a593Smuzhiyun sizeof(ident)) ? -EFAULT : 0;
112*4882a593Smuzhiyun break;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun case WDIOC_GETSTATUS:
115*4882a593Smuzhiyun ret = put_user(0, (int *)arg);
116*4882a593Smuzhiyun break;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun case WDIOC_GETBOOTSTATUS:
119*4882a593Smuzhiyun ret = put_user(boot_status, (int *)arg);
120*4882a593Smuzhiyun break;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun case WDIOC_KEEPALIVE:
123*4882a593Smuzhiyun wdt_enable();
124*4882a593Smuzhiyun ret = 0;
125*4882a593Smuzhiyun break;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun case WDIOC_SETTIMEOUT:
128*4882a593Smuzhiyun ret = get_user(time, (int *)arg);
129*4882a593Smuzhiyun if (ret)
130*4882a593Smuzhiyun break;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun if (time <= 0 || time > 60) {
133*4882a593Smuzhiyun ret = -EINVAL;
134*4882a593Smuzhiyun break;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun heartbeat = time;
138*4882a593Smuzhiyun wdt_enable();
139*4882a593Smuzhiyun fallthrough;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun case WDIOC_GETTIMEOUT:
142*4882a593Smuzhiyun ret = put_user(heartbeat, (int *)arg);
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun return ret;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
ixp4xx_wdt_release(struct inode * inode,struct file * file)148*4882a593Smuzhiyun static int ixp4xx_wdt_release(struct inode *inode, struct file *file)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
151*4882a593Smuzhiyun wdt_disable();
152*4882a593Smuzhiyun else
153*4882a593Smuzhiyun pr_crit("Device closed unexpectedly - timer will not stop\n");
154*4882a593Smuzhiyun clear_bit(WDT_IN_USE, &wdt_status);
155*4882a593Smuzhiyun clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun static const struct file_operations ixp4xx_wdt_fops = {
162*4882a593Smuzhiyun .owner = THIS_MODULE,
163*4882a593Smuzhiyun .llseek = no_llseek,
164*4882a593Smuzhiyun .write = ixp4xx_wdt_write,
165*4882a593Smuzhiyun .unlocked_ioctl = ixp4xx_wdt_ioctl,
166*4882a593Smuzhiyun .compat_ioctl = compat_ptr_ioctl,
167*4882a593Smuzhiyun .open = ixp4xx_wdt_open,
168*4882a593Smuzhiyun .release = ixp4xx_wdt_release,
169*4882a593Smuzhiyun };
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun static struct miscdevice ixp4xx_wdt_miscdev = {
172*4882a593Smuzhiyun .minor = WATCHDOG_MINOR,
173*4882a593Smuzhiyun .name = "watchdog",
174*4882a593Smuzhiyun .fops = &ixp4xx_wdt_fops,
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun
ixp4xx_wdt_init(void)177*4882a593Smuzhiyun static int __init ixp4xx_wdt_init(void)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun int ret;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /*
182*4882a593Smuzhiyun * FIXME: we bail out on device tree boot but this really needs
183*4882a593Smuzhiyun * to be fixed in a nicer way: this registers the MDIO bus before
184*4882a593Smuzhiyun * even matching the driver infrastructure, we should only probe
185*4882a593Smuzhiyun * detected hardware.
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun if (of_have_populated_dt())
188*4882a593Smuzhiyun return -ENODEV;
189*4882a593Smuzhiyun if (!(read_cpuid_id() & 0xf) && !cpu_is_ixp46x()) {
190*4882a593Smuzhiyun pr_err("Rev. A0 IXP42x CPU detected - watchdog disabled\n");
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun return -ENODEV;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
195*4882a593Smuzhiyun WDIOF_CARDRESET : 0;
196*4882a593Smuzhiyun ret = misc_register(&ixp4xx_wdt_miscdev);
197*4882a593Smuzhiyun if (ret == 0)
198*4882a593Smuzhiyun pr_info("timer heartbeat %d sec\n", heartbeat);
199*4882a593Smuzhiyun return ret;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
ixp4xx_wdt_exit(void)202*4882a593Smuzhiyun static void __exit ixp4xx_wdt_exit(void)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun misc_deregister(&ixp4xx_wdt_miscdev);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun module_init(ixp4xx_wdt_init);
209*4882a593Smuzhiyun module_exit(ixp4xx_wdt_exit);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
212*4882a593Smuzhiyun MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun module_param(heartbeat, int, 0);
215*4882a593Smuzhiyun MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun module_param(nowayout, bool, 0);
218*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun MODULE_LICENSE("GPL");
221