1556a2324725029f6d164cbfcb0774a6cf4b553b
[librecmc/librecmc.git] / target / linux / ifxmips / files / drivers / watchdog / ifxmips_wdt.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  * 
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  *
16  * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
17  * Based on EP93xx wdt driver
18  */
19
20 #include <linux/module.h>
21 #include <linux/fs.h>
22 #include <linux/miscdevice.h>
23 #include <linux/miscdevice.h>
24 #include <linux/watchdog.h>
25 #include <linux/platform_device.h>
26 #include <asm/uaccess.h>
27 #include <asm-mips/ifxmips/ifxmips_cgu.h>
28 #include <asm-mips/ifxmips/ifxmips.h>
29
30 #define IFXMIPS_WDT_PW1                  0x00BE0000
31 #define IFXMIPS_WDT_PW2                  0x00DC0000
32
33 #ifndef CONFIG_WATCHDOG_NOWAYOUT
34 static int wdt_ok_to_close = 0;
35 #endif
36
37 int wdt_timeout = 30;
38
39 int
40 ifxmips_wdt_enable(unsigned int timeout)
41 {
42         u32 fpi;
43         fpi = cgu_get_io_region_clock();
44         ifxmips_w32(IFXMIPS_WDT_PW1, IFXMIPS_BIU_WDT_CR);
45         ifxmips_w32(IFXMIPS_WDT_PW2 |
46                 (0x3 << 26) | // PWL
47                 (0x3 << 24) | // CLKDIV
48                 (0x1 << 31) | // enable
49                 ((timeout * (fpi / 0x40000)) + 0x1000), // reload 
50                 IFXMIPS_BIU_WDT_CR);
51         return 0;
52 }
53
54 void
55 ifxmips_wdt_disable(void)
56 {
57 #ifndef CONFIG_WATCHDOG_NOWAYOUT
58         wdt_ok_to_close = 0;
59 #endif
60         ifxmips_w32(IFXMIPS_WDT_PW1, IFXMIPS_BIU_WDT_CR);
61         ifxmips_w32(IFXMIPS_WDT_PW2, IFXMIPS_BIU_WDT_CR);
62 }
63
64 static ssize_t
65 ifxmips_wdt_write(struct file *file, const char __user *data, size_t len,
66                  loff_t *ppos)
67 {
68         size_t i;
69
70         if(!len)
71                 return 0;
72
73 #ifndef CONFIG_WATCHDOG_NOWAYOUT
74         for(i = 0; i != len; i++)
75         {
76                 char c;
77                 if(get_user(c, data + i))
78                         return -EFAULT;
79                 if(c == 'V')
80                         wdt_ok_to_close = 1;
81         }
82 #endif
83         ifxmips_wdt_enable(wdt_timeout);
84         return len;
85 }
86
87 static struct watchdog_info ident = {
88         .options = WDIOF_MAGICCLOSE,
89         .identity = "ifxmips Watchdog",
90 };
91
92 static int
93 ifxmips_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
94                  unsigned long arg)
95 {
96         int ret = -ENOTTY;
97
98         switch(cmd)
99         {
100         case WDIOC_GETSUPPORT:
101                 ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
102                                 sizeof(ident)) ? -EFAULT : 0;
103                 break;
104
105         case WDIOC_GETTIMEOUT:
106                 ret = put_user(wdt_timeout, (int __user *)arg);
107                 break;
108
109         case WDIOC_SETTIMEOUT:
110                 ret = get_user(wdt_timeout, (int __user*)arg);
111                 break;
112
113         case WDIOC_KEEPALIVE:
114                 ifxmips_wdt_enable(wdt_timeout);
115                 ret = 0;
116                 break;
117         }
118         return ret;
119 }
120
121 static int
122 ifxmips_wdt_open(struct inode *inode, struct file *file)
123 {
124         ifxmips_wdt_enable(wdt_timeout);
125         printk("ifxmips_wdt: activated");
126         return nonseekable_open(inode, file);
127 }
128
129 static int ifxmips_wdt_release(struct inode *inode, struct file *file)
130 {
131 #ifndef CONFIG_WATCHDOG_NOWAYOUT
132         if(wdt_ok_to_close)
133                 ifxmips_wdt_disable();
134         else
135 #endif
136                 printk("ifxmips_wdt: watchdog closed without warning, rebooting system\n");
137         return 0;
138 }
139
140 static const struct file_operations ifxmips_wdt_fops = {
141         .owner          = THIS_MODULE,
142         .write          = ifxmips_wdt_write,
143         .ioctl          = ifxmips_wdt_ioctl,
144         .open           = ifxmips_wdt_open,
145         .release        = ifxmips_wdt_release,
146 };
147
148 static struct miscdevice ifxmips_wdt_miscdev = {
149         .minor          = WATCHDOG_MINOR,
150         .name           = "watchdog",
151         .fops           = &ifxmips_wdt_fops,
152 };
153
154 static int
155 ifxmips_wdt_probe(struct platform_device *dev)
156 {
157         int err;
158         err = misc_register(&ifxmips_wdt_miscdev);
159         if(err)
160                 printk("ifxmips_wdt: error creating device\n");
161         else
162                 printk("ifxmips_wdt: loaded\n");
163         return err;
164 }
165
166 static int
167 ifxmips_wdt_remove(struct platform_device *dev)
168 {
169         ifxmips_wdt_disable();
170         misc_deregister(&ifxmips_wdt_miscdev);
171         return 0;
172 }
173
174
175 static struct platform_driver ifxmips_wdt_driver = {
176         .probe = ifxmips_wdt_probe,
177         .remove = ifxmips_wdt_remove,
178         .driver = {
179                 .name = "ifxmips_wdt",
180                 .owner = THIS_MODULE,
181         },
182 };
183
184 static int __init
185 init_ifxmips_wdt(void)
186 {
187         int ret = platform_driver_register(&ifxmips_wdt_driver);
188         if(ret)
189                 printk(KERN_INFO "ifxmips_wdt: error registering platfom driver!");
190         return ret;
191 }
192
193 static void __exit
194 exit_ifxmips_wdt(void)
195 {
196         platform_driver_unregister(&ifxmips_wdt_driver);
197 }
198
199 module_init(init_ifxmips_wdt);
200 module_exit(exit_ifxmips_wdt);
201
202 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
203 MODULE_DESCRIPTION("ifxmips Watchdog");
204 MODULE_LICENSE("GPL");
205 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);