ARMLinux驅(qū)動(dòng)Watch Dog Timer(看門狗)驅(qū)動(dòng)分析
內(nèi)核版本:2.6.28
本文引用地址:http://m.butianyuan.cn/article/201611/318175.htm主機(jī)平臺(tái):Ubuntu 11,04
內(nèi)核版本:2.6.39
原創(chuàng)作品,轉(zhuǎn)載請(qǐng)標(biāo)明出處http://blog.csdn.net/yming0221/article/details/6595265
1、看門狗驅(qū)動(dòng)的原理
下圖是看門狗驅(qū)動(dòng)的原理圖
可以看出,PCLK是系統(tǒng)時(shí)鐘,經(jīng)過8位的預(yù)分頻,然后再被分頻(16、32、64、128)然后產(chǎn)生計(jì)數(shù)脈沖,進(jìn)行計(jì)數(shù),當(dāng)計(jì)數(shù)器WTCNT加到0或減到0,然后產(chǎn)生中斷,或引起系統(tǒng)復(fù)位。所以要隔一段時(shí)間,重置WTCNT的值,防止WTCNT減到0,稱之“喂狗”。
2、驅(qū)動(dòng)分析
下面是自己的驅(qū)動(dòng)分析,如有理解錯(cuò)誤,請(qǐng)指正
注,為了盡量是驅(qū)動(dòng)容易理解,這個(gè)驅(qū)動(dòng)暫時(shí)將有關(guān)電源管理的功能刪除了,等理解透徹再完善
#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; //設(shè)置默認(rèn)為執(zhí)行中斷static int debug;static int count;//用于計(jì)數(shù),控制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端口占用的內(nèi)存資源static struct resource *wdt_irq;//保存wdt中斷號(hào)static struct clk *wdt_clock;//保存從平臺(tái)獲取的watchdog時(shí)鐘static void __iomem *wdt_base;//經(jīng)過ioremap后的內(nèi)存基地址static unsigned int wdt_count;//保存向WTCNT寫的計(jì)數(shù)值static close_state_t allow_close;static DEFINE_SPINLOCK(wdt_lock);//定義一個(gè)自旋鎖,用于資源的互斥訪問/* watchdog control routines */#define DBG(msg...) do { if (debug) printk(KERN_INFO msg); } while (0)/* functions *///喂狗函數(shù),實(shí)際上是將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);//設(shè)備看門狗使能無效、復(fù)位功能無效}//停止watchdog計(jì)時(shí)static void s3c2410wdt_stop(void){spin_lock(&wdt_lock);__s3c2410wdt_stop();spin_unlock(&wdt_lock);}//啟動(dòng)watchdog計(jì)時(shí)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;//看門狗定時(shí)器使能、時(shí)鐘除數(shù)因子128if (soft_noboot) //使用參數(shù)soft_noboot來選擇看門狗到時(shí)是重啟還是執(zhí)行中斷函數(shù){wtcon |= S3C2410_WTCON_INTEN;//中斷使能wtcon &= ~S3C2410_WTCON_RSTEN;//復(fù)位功能無效} else {wtcon &= ~S3C2410_WTCON_INTEN;//中斷不使能wtcon |= S3C2410_WTCON_RSTEN;//復(fù)位功能有效}writel(wdt_count, wdt_base + S3C2410_WTDAT);//將wdt_count寫入WTDATwritel(wdt_count, wdt_base + S3C2410_WTCNT);//將wdt_count寫入WTCNTwritel(wtcon, wdt_base + S3C2410_WTCON);//設(shè)置WTCONspin_unlock(&wdt_lock);}//設(shè)置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*///如果計(jì)時(shí)的時(shí)間過大,則適當(dāng)加大預(yù)分頻值if (count >= 0x10000) {for (divisor = 1; divisor <= 0x100; divisor++) {if ((count / divisor) < 0x10000)break;}//若預(yù)分頻最大仍不能滿足計(jì)時(shí)周期,則報(bào)錯(cuò)if ((count / divisor) >= 0x10000) {dev_err(wdt_dev, "timeout %d too bign", 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);//設(shè)置預(yù)分頻值return 0;}/** /dev/watchdog handling*/static int s3c2410wdt_open(struct inode *inode, struct file *file){if (test_and_set_bit(0, &open_lock))//測(cè)試并設(shè)置open_lock第0位為1return -EBUSY;if (nowayout)__module_get(THIS_MODULE);allow_close = CLOSE_STATE_NOT;/* start the timer */s3c2410wdt_start();//啟動(dòng)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 watchdogn");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數(shù)據(jù)return -EFAULT;if (c == V)//當(dāng)輸入V時(shí),關(guān)閉watchdogallow_close = CLOSE_STATE_ALLOW;}}//注意:這里要手動(dòng)喂狗一次?!s3c2410wdt_keepalive();//由于將allow_close設(shè)置成CLOSE_STATE_ALLOW后,當(dāng)release時(shí)無法再次喂狗}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控制接口函數(shù)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 *///文件操作結(jié)構(gòu)體static const struct file_operations s3c2410wdt_fops = {.owner = THIS_MODULE,.llseek = no_llseek,//屏蔽seek操作.write = s3c2410wdt_write,//寫方法.unlocked_ioctl = s3c2410wdt_ioctl,//控制方法.open = s3c2410wdt_open,//代開設(shè)備方法.release = s3c2410wdt_release,//關(guān)閉設(shè)備方法};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 *///注冊(cè)設(shè)備時(shí)執(zhí)行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和平臺(tái)設(shè)備中資源的定義一致res = platform_get_resource(pdev, IORESOURCE_MEM, 0);if (res == NULL) {dev_err(dev, "no memory resource specifiedn");return -ENOENT;}size = (res->end - res->start) + 1;//申請(qǐng)內(nèi)存空間wdt_mem = request_mem_region(res->start, size, pdev->name);if (wdt_mem == NULL) {dev_err(dev, "failed to get memory regionn");ret = -ENOENT;goto err_req;}//地址映射,wdt_base為基地址wdt_base = ioremap(res->start, size);if (wdt_base == NULL) {dev_err(dev, "failed to ioremap() regionn");ret = -EINVAL;goto err_req;}DBG("probe: mapped wdt_base=%pn", wdt_base);//從平臺(tái)設(shè)備中獲取中斷號(hào)wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);if (wdt_irq == NULL) {dev_err(dev, "no irq resource specifiedn");ret = -ENOENT;goto err_map;}//注冊(cè)中斷,中斷處理函數(shù)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;}//獲取系統(tǒng)時(shí)鐘wdt_clock = clk_get(&pdev->dev, "watchdog");if (IS_ERR(wdt_clock)) {dev_err(dev, "failed to find watchdog clock sourcen");ret = PTR_ERR(wdt_clock);goto err_irq;}//使能時(shí)鐘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)) {//這里第一次設(shè)置計(jì)時(shí)周期為tmr_margin,如果設(shè)置不成功,則重新設(shè)置為默認(rèn)值started = s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);if (started == 0)dev_info(dev,"tmr_margin value out of range, default %d usedn",CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);elsedev_info(dev, "default timer value is out of range, cannot startn");}//注冊(cè)設(shè)備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 timern");s3c2410wdt_start();//啟動(dòng)看門狗} 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 %sabledn",(wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",(wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",(wtcon & S3C2410_WTCON_INTEN) ? "" : "en");//設(shè)置GPB10端口為輸出端口,用于點(diǎn)亮LED10s3c2410_gpio_cfgpin(S3C2410_GPB10,S3C2410_GPB10_OUTP);return 0;//出錯(cuò)跳轉(zhuǎn)表,異常處理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;}//設(shè)備移除函數(shù),釋放資源和映射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;}//關(guān)閉看門狗static void s3c2410wdt_shutdown(struct platform_device *dev){s3c2410wdt_stop();}//定義平臺(tái)設(shè)備驅(qū)動(dòng)static struct platform_driver s3c2410wdt_driver = {.probe = s3c2410wdt_probe,.remove = s3c2410wdt_remove,.shutdown = s3c2410wdt_shutdown,.driver = {.owner = THIS_MODULE,.name = "s3c2410-wdt",//該名稱參照內(nèi)核源碼中該資源的名稱,兩者必須一致},};static char banner[] __initdata =KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronicsn";//驅(qū)動(dòng)安裝時(shí)執(zhí)行static int __init watchdog_init(void){printk(banner);return platform_driver_register(&s3c2410wdt_driver);//注冊(cè)設(shè)備}//驅(qū)動(dòng)移除是執(zhí)行static void __exit watchdog_exit(void){platform_driver_unregister(&s3c2410wdt_driver);//移除設(shè)備}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);
設(shè)置默認(rèn)不是重啟機(jī)器,而是執(zhí)行中斷函數(shù),當(dāng)不喂狗,計(jì)數(shù)器減到0,點(diǎn)亮LED,然后喂狗,重新計(jì)數(shù)。
評(píng)論