from:http://blog.csdn.net/geekcome/article/details/6595265
硬件平台:FL2440
内核版本:2.6.28
主机平台:Ubuntu 11,04
内核版本:2.6.39
原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/6595265
1、看门狗驱动的原理
下图是看门狗驱动的原理图
可以看出,PCLK是系统时钟,经过8位的预分频,然后再被分频(16、32、64、128)然后产生计数脉冲,进行计数,当计数器WTCNT加到0或减到0,然后产生中断,或引起系统复位。所以要隔一段时间,重置WTCNT的值,防止WTCNT减到0,称之“喂狗”。
2、驱动分析
下面是自己的驱动分析,如有理解错误,请指正
注,为了尽量是驱动容易理解,这个驱动暂时将有关电源管理的功能删除了,等理解透彻再完善
- #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 <mach/regs-gpio.h>
- #include <mach/map.h>
- #undef S3C_VA_WATCHDOG
- #define S3C_VA_WATCHDOG (0)
- #include <asm/plat-s3c/regs-watchdog.h>
- #define PFX "s3c2410-wdt: "
- #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
- #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
- static int nowayout = WATCHDOG_NOWAYOUT;
- static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
- static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
- static int soft_noboot = 1; //设置默认为执行中断
- static int debug;
- static int count;//用于计数,控制LED灯的亮灭
- module_param(tmr_margin, int, 0);
- module_param(tmr_atboot, int, 0);
- module_param(nowayout, int, 0);
- module_param(soft_noboot, int, 0);
- module_param(debug, int, 0);
- MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default="
- __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
- MODULE_PARM_DESC(tmr_atboot,
- "Watchdog is started at boot time if set to 1, default="
- __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
- MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
- __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
- MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to
- reboot (default depends on ONLY_TESTING)");
- MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
- typedef enum close_state
- {
- CLOSE_STATE_NOT,
- CLOSE_STATE_ALLOW = 0x4021
- } close_state_t;
- static unsigned long open_lock;
- static struct device *wdt_dev; /* platform device attached to */
- static struct resource *wdt_mem;//用来保存IO端口占用的内存资源
- static struct resource *wdt_irq;//保存wdt中断号
- static struct clk *wdt_clock;//保存从平台获取的watchdog时钟
- static void __iomem *wdt_base;//经过ioremap后的内存基地址
- static unsigned int wdt_count;//保存向WTCNT写的计数值
- static close_state_t allow_close;
- static DEFINE_SPINLOCK(wdt_lock);//定义一个自旋锁,用于资源的互斥访问
- /* watchdog control routines */
- #define DBG(msg...) do {
- if (debug)
- printk(KERN_INFO msg);
- } while (0)
- /* functions */
- //喂狗函数,实际上是将wdt_count写入WTCNT寄存器
- static void s3c2410wdt_keepalive(void)
- {
- spin_lock(&wdt_lock);//给资源上锁
- writel(wdt_count, wdt_base + S3C2410_WTCNT);//写WTCNT寄存器
- spin_unlock(&wdt_lock);//解锁资源,下同
- }
- static void __s3c2410wdt_stop(void)
- {
- unsigned long wtcon;
- wtcon = readl(wdt_base + S3C2410_WTCON);
- wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
- writel(wtcon, wdt_base + S3C2410_WTCON);//设备看门狗使能无效、复位功能无效
- }
- //停止watchdog计时
- static void s3c2410wdt_stop(void)
- {
- spin_lock(&wdt_lock);
- __s3c2410wdt_stop();
- spin_unlock(&wdt_lock);
- }
- //启动watchdog计时
- static void s3c2410wdt_start(void)
- {
- unsigned long wtcon;
- spin_lock(&wdt_lock);
- __s3c2410wdt_stop();
- wtcon = readl(wdt_base + S3C2410_WTCON);
- wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;//看门狗定时器使能、时钟除数因子128
- if (soft_noboot) //使用参数soft_noboot来选择看门狗到时是重启还是执行中断函数
- {
- wtcon |= S3C2410_WTCON_INTEN;//中断使能
- wtcon &= ~S3C2410_WTCON_RSTEN;//复位功能无效
- }
- else
- {
- wtcon &= ~S3C2410_WTCON_INTEN;//中断不使能
- wtcon |= S3C2410_WTCON_RSTEN;//复位功能有效
- }
- writel(wdt_count, wdt_base + S3C2410_WTDAT);//将wdt_count写入WTDAT
- writel(wdt_count, wdt_base + S3C2410_WTCNT);//将wdt_count写入WTCNT
- writel(wtcon, wdt_base + S3C2410_WTCON);//设置WTCON
- spin_unlock(&wdt_lock);
- }
- //设置WatchDog周期timeout
- static int s3c2410wdt_set_heartbeat(int timeout)
- {
- unsigned int freq = clk_get_rate(wdt_clock);
- unsigned int count;
- unsigned int divisor = 1;
- unsigned long wtcon;
- if (timeout < 1)
- return -EINVAL;
- freq /= 128;
- count = timeout * freq;
- /* if the count is bigger than the watchdog register,
- then work out what we need to do (and if) we can
- actually make this value
- */
- //如果计时的时间过大,则适当加大预分频值
- if (count >= 0x10000)
- {
- for (divisor = 1; divisor <= 0x100; divisor++)
- {
- if ((count / divisor) < 0x10000)
- break;
- }
- //若预分频最大仍不能满足计时周期,则报错
- if ((count / divisor) >= 0x10000)
- {
- dev_err(wdt_dev, "timeout %d too big ", timeout);
- return -EINVAL;
- }
- }
- tmr_margin = timeout;
- count /= divisor;
- wdt_count = count;
- /* update the pre-scaler */
- wtcon = readl(wdt_base + S3C2410_WTCON);
- wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
- wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
- writel(count, wdt_base + S3C2410_WTDAT);//是指WTDAT寄存器
- writel(wtcon, wdt_base + S3C2410_WTCON);//设置预分频值
- return 0;
- }
- /*
- * /dev/watchdog handling
- */
- static int s3c2410wdt_open(struct inode *inode, struct file *file)
- {
- if (test_and_set_bit(0, &open_lock))//测试并设置open_lock第0位为1
- return -EBUSY;
- if (nowayout)
- __module_get(THIS_MODULE);
- allow_close = CLOSE_STATE_NOT;
- /* start the timer */
- s3c2410wdt_start();//启动watchdog
- return nonseekable_open(inode, file);
- }
- static int s3c2410wdt_release(struct inode *inode, struct file *file)
- {
- /*
- * Shut off the timer.
- * Lock it in if it's a module and we set nowayout
- */
- if (allow_close == CLOSE_STATE_ALLOW)
- s3c2410wdt_stop();
- else
- {
- dev_err(wdt_dev, "Unexpected close, not stopping watchdog ");
- s3c2410wdt_keepalive();
- }
- allow_close = CLOSE_STATE_NOT;
- clear_bit(0, &open_lock);//清楚open_lock第0位
- return 0;
- }
- static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
- size_t len, loff_t *ppos)
- {
- /*
- * Refresh the timer.
- */
- if (len)
- {
- if (!nowayout)
- {
- size_t i;
- /* In case it was set long ago */
- allow_close = CLOSE_STATE_NOT;
- for (i = 0; i != len; i++)
- {
- char c;
- if (get_user(c, data + i))//从用户空间copy数据
- return -EFAULT;
- if (c == 'V')//当输入V时,关闭watchdog
- allow_close = CLOSE_STATE_ALLOW;
- }
- }
- //注意:这里要手动喂狗一次?!
- s3c2410wdt_keepalive();//由于将allow_close设置成CLOSE_STATE_ALLOW后,当release时无法再次喂狗
- }
- return len;
- }
- #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
- static const struct watchdog_info s3c2410_wdt_ident =
- {
- .options = OPTIONS,
- .firmware_version = 0,
- .identity = "S3C2410 Watchdog",
- };
- //IO控制接口函数
- static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
- unsigned long arg)
- {
- void __user *argp = (void __user *)arg;
- int __user *p = argp;
- int new_margin;
- switch (cmd) {
- case WDIOC_GETSUPPORT:
- return copy_to_user(argp, &s3c2410_wdt_ident,
- sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
- case WDIOC_GETSTATUS:
- case WDIOC_GETBOOTSTATUS:
- return put_user(0, p);
- case WDIOC_KEEPALIVE:
- s3c2410wdt_keepalive();
- return 0;
- case WDIOC_SETTIMEOUT:
- if (get_user(new_margin, p))
- return -EFAULT;
- if (s3c2410wdt_set_heartbeat(new_margin))
- return -EINVAL;
- s3c2410wdt_keepalive();
- return put_user(tmr_margin, p);
- case WDIOC_GETTIMEOUT:
- return put_user(tmr_margin, p);
- default:
- return -ENOTTY;
- }
- }
- /* kernel interface */
- //文件操作结构体
- static const struct file_operations s3c2410wdt_fops =
- {
- .owner = THIS_MODULE,
- .llseek = no_llseek,//屏蔽seek操作
- .write = s3c2410wdt_write,//写方法
- .unlocked_ioctl = s3c2410wdt_ioctl,//控制方法
- .open = s3c2410wdt_open,//代开设备方法
- .release = s3c2410wdt_release,//关闭设备方法
- };
- static struct miscdevice s3c2410wdt_miscdev =
- {
- .minor = WATCHDOG_MINOR,
- .name = "watchdog",
- .fops = &s3c2410wdt_fops,
- };
- /* interrupt handler code */
- static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
- {
- //dev_info(wdt_dev, "watchdog timer expired (irq) ");
- s3c2410_gpio_setpin(S3C2410_GPB10,(count++)%2);
- s3c2410wdt_keepalive();
- return IRQ_HANDLED;
- }
- /* device interface */
- //注册设备时执行
- static int s3c2410wdt_probe(struct platform_device *pdev)
- {
- struct resource *res;
- struct device *dev;
- unsigned int wtcon;
- int started = 0;
- int ret;
- int size;
- dev = &pdev->dev;
- wdt_dev = &pdev->dev;
- /* get the memory region for the watchdog timer */
- //获取IO端口资源,这里 IORESOURCE_MEM和平台设备中资源的定义一致
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (res == NULL) {
- dev_err(dev, "no memory resource specified ");
- return -ENOENT;
- }
- size = (res->end - res->start) + 1;
- //申请内存空间
- wdt_mem = request_mem_region(res->start, size, pdev->name);
- if (wdt_mem == NULL) {
- dev_err(dev, "failed to get memory region ");
- ret = -ENOENT;
- goto err_req;
- }
- //地址映射,wdt_base为基地址
- wdt_base = ioremap(res->start, size);
- if (wdt_base == NULL) {
- dev_err(dev, "failed to ioremap() region ");
- ret = -EINVAL;
- goto err_req;
- }
- DBG("probe: mapped wdt_base=%p ", wdt_base);
- //从平台设备中获取中断号
- wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
- if (wdt_irq == NULL) {
- dev_err(dev, "no irq resource specified ");
- ret = -ENOENT;
- goto err_map;
- }
- //注册中断,中断处理函数s3c2410wdt_irq()
- ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
- if (ret != 0) {
- dev_err(dev, "failed to install irq (%d) ", ret);
- goto err_map;
- }
- //获取系统时钟
- wdt_clock = clk_get(&pdev->dev, "watchdog");
- if (IS_ERR(wdt_clock)) {
- dev_err(dev, "failed to find watchdog clock source ");
- ret = PTR_ERR(wdt_clock);
- goto err_irq;
- }
- //使能时钟
- clk_enable(wdt_clock);
- /* see if we can actually set the requested timer margin, and if
- * not, try the default value */
- if (s3c2410wdt_set_heartbeat(tmr_margin))
- {//这里第一次设置计时周期为tmr_margin,如果设置不成功,则重新设置为默认值
- started = s3c2410wdt_set_heartbeat(
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- if (started == 0)
- dev_info(dev,
- "tmr_margin value out of range, default %d used ",
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- else
- dev_info(dev, "default timer value is out of range, cannot start ");
- }
- //注册设备
- ret = misc_register(&s3c2410wdt_miscdev);
- if (ret)
- {
- dev_err(dev, "cannot register miscdev on minor=%d (%d) ",
- WATCHDOG_MINOR, ret);
- goto err_clk;
- }
- if (tmr_atboot && started == 0)
- {
- dev_info(dev, "starting watchdog timer ");
- s3c2410wdt_start();//启动看门狗
- }
- else if (!tmr_atboot)
- {
- /* if we're not enabling the watchdog, then ensure it is
- * disabled if it has been left running from the bootloader
- * or other source */
- s3c2410wdt_stop();
- }
- /* print out a statement of readiness */
- wtcon = readl(wdt_base + S3C2410_WTCON);
- dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled ",
- (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
- (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
- (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
- //设置GPB10端口为输出端口,用于点亮LED10
- s3c2410_gpio_cfgpin(S3C2410_GPB10,S3C2410_GPB10_OUTP);
- return 0;
- //出错跳转表,异常处理
- err_clk:
- clk_disable(wdt_clock);
- clk_put(wdt_clock);
- err_irq:
- free_irq(wdt_irq->start, pdev);
- err_map:
- iounmap(wdt_base);
- err_req:
- release_resource(wdt_mem);
- kfree(wdt_mem);
- return ret;
- }
- //设备移除函数,释放资源和映射
- static int s3c2410wdt_remove(struct platform_device *dev)
- {
- release_resource(wdt_mem);
- kfree(wdt_mem);
- wdt_mem = NULL;
- free_irq(wdt_irq->start, dev);
- wdt_irq = NULL;
- clk_disable(wdt_clock);
- clk_put(wdt_clock);
- wdt_clock = NULL;
- iounmap(wdt_base);
- misc_deregister(&s3c2410wdt_miscdev);
- return 0;
- }
- //关闭看门狗
- static void s3c2410wdt_shutdown(struct platform_device *dev)
- {
- s3c2410wdt_stop();
- }
- //定义平台设备驱动
- static struct platform_driver s3c2410wdt_driver = {
- .probe = s3c2410wdt_probe,
- .remove = s3c2410wdt_remove,
- .shutdown = s3c2410wdt_shutdown,
- .driver = {
- .owner = THIS_MODULE,
- .name = "s3c2410-wdt",//该名称参照内核源码中该资源的名称,两者必须一致
- },
- };
- static char banner[] __initdata =
- KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics ";
- //驱动安装时执行
- static int __init watchdog_init(void)
- {
- printk(banner);
- return platform_driver_register(&s3c2410wdt_driver);//注册设备
- }
- //驱动移除是执行
- static void __exit watchdog_exit(void)
- {
- platform_driver_unregister(&s3c2410wdt_driver);//移除设备
- }
- module_init(watchdog_init);
- module_exit(watchdog_exit);
- MODULE_AUTHOR("Yan Ming");
- MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
- MODULE_LICENSE("GPL");
- MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
设置默认不是重启机器,而是执行中断函数,当不喂狗,计数器减到0,点亮LED,然后喂狗,重新计数。