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.
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.
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
16 * Copyright 2004 Wu Qi Ming <gokimi@msn.com>
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
20 #include <linux/version.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/timer.h>
29 #include <linux/errno.h>
30 #include <linux/proc_fs.h>
31 #include <linux/stat.h>
32 #include <linux/tty.h>
33 #include <linux/selection.h>
34 #include <linux/kmod.h>
35 #include <linux/vmalloc.h>
36 #include <linux/kdev_t.h>
37 #include <linux/ioctl.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <linux/platform_device.h>
41 #include <asm/amazon/amazon.h>
42 #include <asm/amazon/amazon_wdt.h>
44 #define DRV_NAME "AMAZON WDT:"
46 #undef AMAZON_WDT_DEBUG
48 static int amazon_wdt_isopen
= 0;
50 #ifdef AMAZON_WDT_DEBUG
51 static struct proc_dir_entry
* amazon_wdt_dir
;
54 int wdt_enable(int timeout
)
57 int reload_value
, divider
= 1;
59 ffpi
= amazon_get_fpi_hz();
61 reload_value
= 65536 - timeout
* ffpi
/ 256;
63 if (reload_value
< 0) {
65 reload_value
= 65536 - timeout
* ffpi
/ 16384;
68 if (reload_value
< 0){
69 printk(KERN_INFO DRV_NAME
"timeout too large %d\n", timeout
);
73 printk(KERN_INFO DRV_NAME
"timeout:%d reload_value: %8x\n", timeout
, reload_value
);
75 hard_psw
= (amazon_readl(AMAZON_WDT_CON0
) & 0xffffff01) +
76 (amazon_readl(AMAZON_WDT_CON1
) & 0xc) + 0xf0;
77 amazon_writel(hard_psw
, AMAZON_WDT_CON0
);
80 amazon_writel((hard_psw
& 0xff00) + (reload_value
<< 16) + 0xf2, AMAZON_WDT_CON0
);
83 amazon_writel(divider
<< 2, AMAZON_WDT_CON1
);
86 hard_psw
= (amazon_readl(AMAZON_WDT_CON0
) & 0xffffff01) +
87 (amazon_readl(AMAZON_WDT_CON1
) & 0xc) + 0xf0;
88 amazon_writel(hard_psw
, AMAZON_WDT_CON0
);
91 amazon_writel_masked(AMAZON_WDT_CON0
, 0xff, 0xf3);
96 void wdt_disable(void)
100 hard_psw
= (amazon_readl(AMAZON_WDT_CON0
) & 0xffffff01) +
101 (amazon_readl(AMAZON_WDT_CON1
) & 0xc) + 0xf0;
102 amazon_writel(hard_psw
, AMAZON_WDT_CON0
);
105 amazon_writel_masked(AMAZON_WDT_CON0
, 0xff, 0xf2);
108 amazon_writel_masked(AMAZON_WDT_CON1
, 0x8, 0x8);
111 hard_psw
=(amazon_readl(AMAZON_WDT_CON0
) & 0xffffff01) +
112 (amazon_readl(AMAZON_WDT_CON1
) & 0xc) + 0xf0;
113 amazon_writel(hard_psw
, AMAZON_WDT_CON0
);
116 amazon_writel_masked(AMAZON_WDT_CON0
, 0xff, 0xf3);
122 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
123 static long wdt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
125 static int wdt_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
129 static int timeout
=-1;
132 case AMAZON_WDT_IOC_START
:
133 printk(KERN_INFO DRV_NAME
"enable watch dog timer!\n");
134 if (copy_from_user((void*)&timeout
, (void*)arg
, sizeof (int))) {
135 printk(KERN_INFO DRV_NAME
"invalid argument\n");
137 } else if ((result
= wdt_enable(timeout
)) < 0) {
142 case AMAZON_WDT_IOC_STOP
:
143 printk(KERN_INFO DRV_NAME
"disable watch dog timer\n");
148 case AMAZON_WDT_IOC_PING
:
152 result
= wdt_enable(timeout
);
163 static ssize_t
wdt_read(struct file
*file
, char *buf
, size_t count
, loff_t
*offset
)
168 static ssize_t
wdt_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*offset
)
173 static int wdt_open(struct inode
*inode
, struct file
*file
)
175 if (amazon_wdt_isopen
== 1)
178 amazon_wdt_isopen
= 1;
179 printk(KERN_INFO DRV_NAME
"opened\n");
183 static int wdt_release(struct inode
*inode
, struct file
*file
)
185 amazon_wdt_isopen
= 0;
186 printk(KERN_INFO DRV_NAME
"closed\n");
190 #ifdef AMAZON_WDT_DEBUG
191 int wdt_register_proc_read(char *buf
, char **start
, off_t offset
,
192 int count
, int *eof
, void *data
)
195 len
+=sprintf(buf
+len
,"NMISR: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_NMISR
));
196 len
+=sprintf(buf
+len
,"RST_REQ: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_RST_REQ
));
197 len
+=sprintf(buf
+len
,"RST_SR: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_RST_SR
));
198 len
+=sprintf(buf
+len
,"WDT_CON0: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_CON0
));
199 len
+=sprintf(buf
+len
,"WDT_CON1: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_CON1
));
200 len
+=sprintf(buf
+len
,"WDT_SR: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_SR
));
206 static struct file_operations wdt_fops
= {
209 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
210 unlocked_ioctl
: wdt_ioctl
,
215 release
: wdt_release
,
218 static int __init
amazon_wdt_probe(struct platform_device
*dev
)
220 int result
= result
= register_chrdev(0, "watchdog", &wdt_fops
);
223 printk(KERN_INFO DRV_NAME
"cannot register device\n");
227 #ifdef AMAZON_WDT_DEBUG
228 amazon_wdt_dir
=proc_mkdir("amazon_wdt",NULL
);
229 create_proc_read_entry("wdt_register", 0, amazon_wdt_dir
,
230 wdt_register_proc_read
, NULL
);
234 printk(KERN_INFO DRV_NAME
"driver loaded but inactive\n");
238 static int amazon_wdt_remove(struct platform_device
*dev
)
240 unregister_chrdev(0, "watchdog");
241 #ifdef AMAZON_WDT_DEBUG
242 remove_proc_entry("wdt_register", amazon_wdt_dir
);
243 remove_proc_entry("amazon_wdt", NULL
);
245 printk(KERN_INFO DRV_NAME
"unregistered\n");
249 static struct platform_driver amazon_wdt_driver
= {
250 .probe
= amazon_wdt_probe
,
251 .remove
= amazon_wdt_remove
,
253 .name
= "amazon_wdt",
254 .owner
= THIS_MODULE
,
258 static int __init
amazon_wdt_init(void)
260 int ret
= platform_driver_register(&amazon_wdt_driver
);
262 printk(KERN_WARNING
"amazon_wdt: error registering platfom driver!\n");
266 static void __exit
amazon_wdt_exit(void)
268 platform_driver_unregister(&amazon_wdt_driver
);
271 module_init(amazon_wdt_init
);
272 module_exit(amazon_wdt_exit
);
274 MODULE_LICENSE ("GPL");
275 MODULE_AUTHOR("Infineon / John Crispin <blogic@openwrt.org>");
276 MODULE_DESCRIPTION("AMAZON WDT driver");