您的位置 首页 方案

ARMLinux驱动Watch Dog Timer(看门狗)驱动剖析

硬件平台:FL2440内核版本:2.6.28主机平台:Ubuntu11,04内核版本:2.6.39原创作品,转载请标明出处http://blog.csdn.net/yming0221/arti…

硬件渠道: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 #include #include #include #include #include #include #include #include #include interrupt.h>#include #include #include #include #include #undef S3C_VA_WATCHDOG#define S3C_VA_WATCHDOG (0)#include #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;//看门狗守时器使能、时钟除数因子128if (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写入WTDATwritel(wdt_count, wdt_base + S3C2410_WTCNT);//将wdt_count写入WTCNTwritel(wtcon, wdt_base + S3C2410_WTCON);//设置WTCONspin_unlock(&wdt_lock);}//设置WatchDog周期timeoutstatic 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 canactually 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\n", 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位为1return -EBUSY;if (nowayout)__module_get(THIS_MODULE);allow_close = CLOSE_STATE_NOT;/* start the timer */s3c2410wdt_start();//发动watchdogreturn nonseekable_open(inode, file);}static int s3c2410wdt_release(struct inode *inode, struct file *file){/**	Shut off the timer.* 	Lock it in if its a module and we set nowayout*/if (allow_close == CLOSE_STATE_ALLOW)s3c2410wdt_stop();else {dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");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时,封闭watchdogallow_close = CLOSE_STATE_ALLOW;}}//留意:这儿要手动喂狗一次?!s3c2410wdt_keepalive();//我们将allow_close设置成CLOSE_STATE_ALLOW后,当release时无法再次喂狗}return len;}#define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSEstatic 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)\n");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\n");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\n");ret = -ENOENT;goto err_req;}//地址映射,wdt_base为基地址wdt_base = ioremap(res->start, size);if (wdt_base == NULL) {dev_err(dev, "failed to ioremap() region\n");ret = -EINVAL;goto err_req;}DBG("probe: mapped wdt_base=%p\n", wdt_base);//从渠道设备中获取中止号wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);if (wdt_irq == NULL) {dev_err(dev, "no irq resource specified\n");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)\n", 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\n");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\n",CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);elsedev_info(dev, "default timer value is out of range, cannot start\n");}//注册设备ret = misc_register(&s3c2410wdt_miscdev);if (ret) {dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",WATCHDOG_MINOR, ret);goto err_clk;}if (tmr_atboot && started == 0) {dev_info(dev, "starting watchdog timer\n");s3c2410wdt_start();//发动看门狗} else if (!tmr_atboot) {/* if were 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\n",(wtcon & S3C2410_WTCON_ENABLE) ?  "" : "in",(wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",(wtcon & S3C2410_WTCON_INTEN) ? "" : "en");//设置GPB10端口为输出端口,用于点亮LED10s3c2410_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\n";//驱动装置时碑文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,然后喂狗,从头计数。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/265063.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部