#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/timer.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cpufreq.h>
#include <linux/slab.h>
#include <mach/map.h>
#undef S3C_VA_WATCHDOG
#define S3C_VA_WATCHDOG (0)
#include <plat/regs-watchdog.h>
static void __iomem *dog_addr;
#define set_time 0x01
#define stop_time 0x02
//(*(volatile unsigned long *)(adc_addr + name))
/*#define dog_con (*(volatile unsigned long *)(dog_addr + S3C2410_WTCON))
#define dog_dat (*(volatile unsigned long *)(dog_addr + S3C2410_WTDAT))
#define dog_cnt (*(volatile unsigned long *)(dog_addr + S3C2410_WTCNT))*/
static int yzh_watchdog_open(struct inode *inode, struct file *filp)
{
int temp;
int tmp;
temp = readl(dog_addr + S3C2410_WTCON);
temp |= S3C2410_WTCON_RSTEN|S3C2410_WTCON_ENABLE|S3C2410_WTCON_DIV128|S3C2410_WTCON_PRESCALE(0x55);
writel(temp,dog_addr + S3C2410_WTCON);
tmp = readl(dog_addr + S3C2410_WTCON);
tmp &= ~(S3C2410_WTCON_INTEN);
writel(tmp,dog_addr + S3C2410_WTCON);
printk("the dog has opened \n\r");
return 0;
}
int yzh_watchdog_release(struct inode *inode, struct file *filp)
{
int tmp = 0;
tmp = readl(dog_addr + S3C2410_WTCON);
tmp &= ~(S3C2410_WTCON_ENABLE);
writel(tmp,dog_addr + S3C2410_WTCON);
return 0;
}
long yzh_watchdog_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int temp = 0x55;
int tmp;
switch(cmd)
{
case set_time:
writel(temp,dog_addr + S3C2410_WTDAT);
break;
case stop_time:
tmp = readl(dog_addr + S3C2410_WTCON);
tmp &= ~(S3C2410_WTCON_ENABLE);
writel(tmp,dog_addr + S3C2410_WTCON);
break;
default:
break;
}
return 0;
}
struct file_operations yzh_watchdog_fops = {
.open = yzh_watchdog_open,
.release = yzh_watchdog_release,
.unlocked_ioctl = yzh_watchdog_unlocked_ioctl,
};
struct miscdevice yzh_watchdog_misc = {
.minor = 255,
.name = "watchdog_myself",
.fops = &yzh_watchdog_fops,
};
int yzh_watchdog_init(void)
{
int ret;
dog_addr = ioremap(S3C_PA_WDT,0x20);
ret = misc_register(&yzh_watchdog_misc);
printk("the yzh_watchdog has init %d\n\r",ret);
return ret;
}
void yzh_watchdog_exit(void)
{
int ret;
iounmap(dog_addr);
ret = misc_deregister(&yzh_watchdog_misc);
printk("the yzh_watchdog has uninstall %d\n\r",ret);
}
module_init(yzh_watchdog_init);
module_exit(yzh_watchdog_exit);
MODULE_LICENSE("GPL");
测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#define set_time 0x01
#define stop_time 0x02
int main(void)
{
int fd;
int i;
fd = open("/dev/watchdog_myself",0);
if(fd < 0)
{
printf("the dog open is fail %d\n\r",fd);
}
printf("the dog is open is ok %d\n\r",fd);
for(i=0;i<10;i++)
{
ioctl(fd,0x01,&i);
printf("tesing \n\r");
sleep(1);
}
close(fd);
return 0;
}