enable start-stop-daemon by default, i want to use this to clean up a few init script...
[openwrt/openwrt.git] / target / linux / amazon-2.6 / files / drivers / char / watchdog / amazon_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 2004 Wu Qi Ming <gokimi@msn.com>
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
18 */
19
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/timer.h>
27 #include <linux/fs.h>
28 #include <linux/errno.h>
29 #include <linux/proc_fs.h>
30 #include <linux/stat.h>
31 #include <linux/tty.h>
32 #include <linux/selection.h>
33 #include <linux/kmod.h>
34 #include <linux/vmalloc.h>
35 #include <linux/kdev_t.h>
36 #include <linux/ioctl.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <asm/amazon/amazon.h>
40 #include <asm/amazon/amazon_wdt.h>
41
42 #define DRV_NAME "AMAZON WDT:"
43
44 #undef AMAZON_WDT_DEBUG
45
46 extern unsigned int amazon_get_fpi_hz(void);
47 static int amazon_wdt_isopen = 0;
48
49 #ifdef AMAZON_WDT_DEBUG
50 static struct proc_dir_entry* amazon_wdt_dir;
51 #endif
52
53 int wdt_enable(int timeout)
54 {
55 u32 hard_psw, ffpi;
56 int reload_value, divider = 1;
57
58 ffpi = amazon_get_fpi_hz();
59
60 reload_value = 65536 - timeout * ffpi / 256;
61
62 if (reload_value < 0) {
63 divider = 0;
64 reload_value = 65536 - timeout * ffpi / 16384;
65 }
66
67 if (reload_value < 0){
68 printk(KERN_INFO DRV_NAME "timeout too large %d\n", timeout);
69 return -EINVAL;
70 }
71
72 printk(KERN_INFO DRV_NAME "timeout:%d reload_value: %8x\n", timeout, reload_value);
73
74 hard_psw = (amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) +
75 (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;
76 amazon_writel(hard_psw, AMAZON_WDT_CON0);
77 wmb();
78
79 amazon_writel((hard_psw & 0xff00) + (reload_value << 16) + 0xf2, AMAZON_WDT_CON0);
80 wmb();
81
82 amazon_writel(divider << 2, AMAZON_WDT_CON1);
83 wmb();
84
85 hard_psw = (amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) +
86 (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;
87 amazon_writel(hard_psw, AMAZON_WDT_CON0);
88 wmb();
89
90 amazon_writel_masked(AMAZON_WDT_CON0, 0xff, 0xf3);
91 wmb();
92 return 0;
93 }
94
95 void wdt_disable(void)
96 {
97 u32 hard_psw = 0;
98
99 hard_psw = (amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) +
100 (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;
101 amazon_writel(hard_psw, AMAZON_WDT_CON0);
102 wmb();
103
104 amazon_writel_masked(AMAZON_WDT_CON0, 0xff, 0xf2);
105 wmb();
106
107 amazon_writel_masked(AMAZON_WDT_CON1, 0x8, 0x8);
108 wmb();
109
110 hard_psw=(amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) +
111 (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;
112 amazon_writel(hard_psw, AMAZON_WDT_CON0);
113 wmb();
114
115 amazon_writel_masked(AMAZON_WDT_CON0, 0xff, 0xf3);
116 wmb();
117
118 return;
119 }
120
121 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
122 {
123 int result=0;
124 static int timeout=-1;
125
126 switch(cmd){
127 case AMAZON_WDT_IOC_START:
128 printk(KERN_INFO DRV_NAME "enable watch dog timer!\n");
129 if (copy_from_user((void*)&timeout, (void*)arg, sizeof (int))) {
130 printk(KERN_INFO DRV_NAME "invalid argument\n");
131 result=-EINVAL;
132 } else if ((result = wdt_enable(timeout)) < 0) {
133 timeout = -1;
134 }
135 break;
136
137 case AMAZON_WDT_IOC_STOP:
138 printk(KERN_INFO DRV_NAME "disable watch dog timer\n");
139 timeout = -1;
140 wdt_disable();
141 break;
142
143 case AMAZON_WDT_IOC_PING:
144 if (timeout < 0) {
145 result = -EIO;
146 } else {
147 result = wdt_enable(timeout);
148 }
149 break;
150
151 default:
152 result=-EINVAL;
153 break;
154 }
155 return result;
156 }
157
158 static ssize_t wdt_read(struct file *file, char *buf, size_t count, loff_t *offset)
159 {
160 return 0;
161 }
162
163 static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *offset)
164 {
165 return count;
166 }
167
168 static int wdt_open(struct inode *inode, struct file *file)
169 {
170 if (amazon_wdt_isopen == 1)
171 return -EBUSY;
172
173 amazon_wdt_isopen = 1;
174 printk(KERN_INFO DRV_NAME "opened\n");
175 return 0;
176 }
177
178 static int wdt_release(struct inode *inode, struct file *file)
179 {
180 amazon_wdt_isopen = 0;
181 printk(KERN_INFO DRV_NAME "closed\n");
182 return 0;
183 }
184
185 #ifdef AMAZON_WDT_DEBUG
186 int wdt_register_proc_read(char *buf, char **start, off_t offset,
187 int count, int *eof, void *data)
188 {
189 int len=0;
190 len+=sprintf(buf+len,"NMISR: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_NMISR));
191 len+=sprintf(buf+len,"RST_REQ: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_RST_REQ));
192 len+=sprintf(buf+len,"RST_SR: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_RST_SR));
193 len+=sprintf(buf+len,"WDT_CON0: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_CON0));
194 len+=sprintf(buf+len,"WDT_CON1: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_CON1));
195 len+=sprintf(buf+len,"WDT_SR: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_SR));
196 *eof = 1;
197 return len;
198 }
199 #endif
200
201 static struct file_operations wdt_fops = {
202 read: wdt_read,
203 write: wdt_write,
204 ioctl: wdt_ioctl,
205 open: wdt_open,
206 release: wdt_release,
207 };
208
209 int __init amazon_wdt_init_module(void)
210 {
211 int result = result = register_chrdev(0, "watchdog", &wdt_fops);
212
213 if (result < 0) {
214 printk(KERN_INFO DRV_NAME "cannot register device\n");
215 return result;
216 }
217
218 #ifdef AMAZON_WDT_DEBUG
219 amazon_wdt_dir=proc_mkdir("amazon_wdt",NULL);
220 create_proc_read_entry("wdt_register", 0, amazon_wdt_dir,
221 wdt_register_proc_read, NULL);
222 #endif
223
224 amazon_wdt_isopen=0;
225 printk(KERN_INFO DRV_NAME "driver loaded but inactive");
226 return 0;
227 }
228
229 void amazon_wdt_cleanup_module(void)
230 {
231 unregister_chrdev(0, "watchdog");
232 #ifdef AMAZON_WDT_DEBUG
233 remove_proc_entry("wdt_register", amazon_wdt_dir);
234 remove_proc_entry("amazon_wdt", NULL);
235 #endif
236 printk(KERN_INFO DRV_NAME "unregistered");
237 return;
238 }
239
240 MODULE_LICENSE ("GPL");
241 MODULE_AUTHOR("Infineon / John Crispin <blogic@openwrt.org>");
242 MODULE_DESCRIPTION("AMAZON WDT driver");
243
244 module_init(amazon_wdt_init_module);
245 module_exit(amazon_wdt_cleanup_module);
246