ARM-Linux驅(qū)動(dòng)--Watch Dog Timer(看門(mén)狗)驅(qū)動(dòng)分析
內(nèi)核版本:2.6.28
本文引用地址:http://m.butianyuan.cn/article/201611/318869.htm主機(jī)平臺(tái):Ubuntu 11,04
內(nèi)核版本:2.6.39
1、看門(mén)狗驅(qū)動(dòng)的原理
下圖是看門(mén)狗驅(qū)動(dòng)的原理圖
可以看出,PCLK是系統(tǒng)時(shí)鐘,經(jīng)過(guò)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,稱(chēng)之“喂狗”。
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
- #undefS3C_VA_WATCHDOG
- #defineS3C_VA_WATCHDOG(0)
- #include
- #definePFX"s3c2410-wdt:"
- #defineCONFIG_S3C2410_WATCHDOG_ATBOOT(0)
- #defineCONFIG_S3C2410_WATCHDOG_DEFAULT_TIME(15)
- staticintnowayout=WATCHDOG_NOWAYOUT;
- staticinttmr_margin=CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
- staticinttmr_atboot=CONFIG_S3C2410_WATCHDOG_ATBOOT;
- staticintsoft_noboot=1;//設(shè)置默認(rèn)為執(zhí)行中斷
- staticintdebug;
- staticintcount;//用于計(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,"Watchdogtmr_margininseconds.default="
- __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME)")");
- MODULE_PARM_DESC(tmr_atboot,
- "Watchdogisstartedatboottimeifsetto1,default="
- __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
- MODULE_PARM_DESC(nowayout,"Watchdogcannotbestoppedoncestarted(default="
- __MODULE_STRING(WATCHDOG_NOWAYOUT)")");
- MODULE_PARM_DESC(soft_noboot,"Watchdogaction,setto1toignorereboots,0to
- reboot(defaultdependsonONLY_TESTING)");
- MODULE_PARM_DESC(debug,"Watchdogdebug,setto>1fordebug,(default0)");
- typedefenumclose_state
- {
- CLOSE_STATE_NOT,
- CLOSE_STATE_ALLOW=0x4021
- }close_state_t;
- staticunsignedlongopen_lock;
- staticstructdevice*wdt_dev;/*platformdeviceattachedto*/
- staticstructresource*wdt_mem;//用來(lái)保存IO端口占用的內(nèi)存資源
- staticstructresource*wdt_irq;//保存wdt中斷號(hào)
- staticstructclk*wdt_clock;//保存從平臺(tái)獲取的watchdog時(shí)鐘
- staticvoid__iomem*wdt_base;//經(jīng)過(guò)ioremap后的內(nèi)存基地址
- staticunsignedintwdt_count;//保存向WTCNT寫(xiě)的計(jì)數(shù)值
- staticclose_state_tallow_close;
- staticDEFINE_SPINLOCK(wdt_lock);//定義一個(gè)自旋鎖,用于資源的互斥訪(fǎng)問(wèn)
- /*watchdogcontrolroutines*/
- #defineDBG(msg...)do{
- if(debug)
- printk(KERN_INFOmsg);
- }while(0)
- /*functions*/
- //喂狗函數(shù),實(shí)際上是將wdt_count寫(xiě)入WTCNT寄存器
- staticvoids3c2410wdt_keepalive(void)
- {
- spin_lock(&wdt_lock);//給資源上鎖
- writel(wdt_count,wdt_base+S3C2410_WTCNT);//寫(xiě)WTCNT寄存器
- spin_unlock(&wdt_lock);//解鎖資源,下同
- }
- staticvoid__s3c2410wdt_stop(void)
- {
- unsignedlongwtcon;
- wtcon=readl(wdt_base+S3C2410_WTCON);
- wtcon&=~(S3C2410_WTCON_ENABLE|S3C2410_WTCON_RSTEN);
- writel(wtcon,wdt_base+S3C2410_WTCON);//設(shè)備看門(mén)狗使能無(wú)效、復(fù)位功能無(wú)效
- }
- //停止watchdog計(jì)時(shí)
- staticvoids3c2410wdt_stop(void)
- {
- spin_lock(&wdt_lock);
- __s3c2410wdt_stop();
- spin_unlock(&wdt_lock);
- }
- //啟動(dòng)watchdog計(jì)時(shí)
- staticvoids3c2410wdt_start(void)
- {
- unsignedlongwtcon;
- spin_lock(&wdt_lock);
- __s3c2410wdt_stop();
- wtcon=readl(wdt_base+S3C2410_WTCON);
- wtcon|=S3C2410_WTCON_ENABLE|S3C2410_WTCON_DIV128;//看門(mén)狗定時(shí)器使能、時(shí)鐘除數(shù)因子128
- if(soft_noboot)//使用參數(shù)soft_noboot來(lái)選擇看門(mén)狗到時(shí)是重啟還是執(zhí)行中斷函數(shù)
- {
- wtcon|=S3C2410_WTCON_INTEN;//中斷使能
- wtcon&=~S3C2410_WTCON_RSTEN;//復(fù)位功能無(wú)效
- }
- else
- {
- wtcon&=~S3C2410_WTCON_INTEN;//中斷不使能
- wtcon|=S3C2410_WTCON_RSTEN;//復(fù)位功能有效
- }
- writel(wdt_count,wdt_base+S3C2410_WTDAT);//將wdt_count寫(xiě)入WTDAT
- writel(wdt_count,wdt_base+S3C2410_WTCNT);//將wdt_count寫(xiě)入WTCNT
- writel(wtcon,wdt_base+S3C2410_WTCON);//設(shè)置WTCON
- spin_unlock(&wdt_lock);
- }
- //設(shè)置WatchDog周期timeout
- staticints3c2410wdt_set_heartbeat(inttimeout)
- {
- unsignedintfreq=clk_get_rate(wdt_clock);
- unsignedintcount;
- unsignedintdivisor=1;
- unsignedlongwtcon;
- if(timeout<1)
- return-EINVAL;
- freq/=128;
- count=timeout*freq;
- /*ifthecountisbiggerthanthewatchdogregister,
- thenworkoutwhatweneedtodo(andif)wecan
- actuallymakethisvalue
- */
- //如果計(jì)時(shí)的時(shí)間過(guò)大,則適當(dāng)加大預(yù)分頻值
- if(count>=0x10000)
- {
- for(divisor=1;divisor<=0x100;divisor++)
- {
- if((count/divisor)<0x10000)
- break;
- }
- //若預(yù)分頻最大仍不能滿(mǎn)足計(jì)時(shí)周期,則報(bào)錯(cuò)
- if((count/divisor)>=0x10000)
- {
- dev_err(wdt_dev,"timeout%dtoobign",timeout);
- return-EINVAL;
- }
- }
- tmr_margin=timeout;
- count/=divisor;
- wdt_count=count;
- /*updatethepre-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ù)分頻值
- return0;
- }
- /*
- */dev/watchdoghandling
- */
- staticints3c2410wdt_open(structinode*inode,structfile*file)
- {
- if(test_and_set_bit(0,&open_lock))//測(cè)試并設(shè)置open_lock第0位為1
- return-EBUSY;
- if(nowayout)
- __module_get(THIS_MODULE);
- allow_close=CLOSE_STATE_NOT;
- /*startthetimer*/
- s3c2410wdt_start();//啟動(dòng)watchdog
- returnnonseekable_open(inode,file);
- }
- staticints3c2410wdt_release(structinode*inode,structfile*file)
- {
- /*
- *Shutoffthetimer.
- *Lockitinifitsamoduleandwesetnowayout
- */
- if(allow_close==CLOSE_STATE_ALLOW)
- s3c2410wdt_stop();
- else
- {
- dev_err(wdt_dev,"Unexpectedclose,notstoppingwatchdogn");
- s3c2410wdt_keepalive();
- }
- allow_close=CLOSE_STATE_NOT;
- clear_bit(0,&open_lock);//清楚open_lock第0位
- return0;
- }
- staticssize_ts3c2410wdt_write(structfile*file,constchar__user*data,
- size_tlen,loff_t*ppos)
- {
- /*
- *Refreshthetimer.
- */
- if(len)
- {
- if(!nowayout)
- {
- size_ti;
- /*Incaseitwassetlongago*/
- allow_close=CLOSE_STATE_NOT;
- for(i=0;i!=len;i++)
- {
- charc;
- if(get_user(c,data+i))//從用戶(hù)空間copy數(shù)據(jù)
- return-EFAULT;
- if(c==V)//當(dāng)輸入V時(shí),關(guān)閉watchdog
- allow_close=CLOSE_STATE_ALLOW;
- }
- }
- //注意:這里要手動(dòng)喂狗一次?!
- s3c2410wdt_keepalive();//由于將allow_close設(shè)置成CLOSE_STATE_ALLOW后,當(dāng)release時(shí)無(wú)法再次喂狗
- }
- returnlen;
- }
- #defineOPTIONSWDIOF_SETTIMEOUT|WDIOF_KEEPALIVEPING|WDIOF_MAGICCLOSE
- staticconststructwatchdog_infos3c2410_wdt_ident=
- {
- .options=OPTIONS,
- .firmware_version=0,
- .identity="S3C2410Watchdog",
- };
- //IO控制接口函數(shù)
- staticlongs3c2410wdt_ioctl(structfile*file,unsignedintcmd,
- unsignedlongarg)
- {
- void__user*argp=(void__user*)arg;
- int__user*p=argp;
- intnew_margin;
- switch(cmd){
- caseWDIOC_GETSUPPORT:
- returncopy_to_user(argp,&s3c2410_wdt_ident,
- sizeof(s3c2410_wdt_ident))?-EFAULT:0;
- caseWDIOC_GETSTATUS:
- caseWDIOC_GETBOOTSTATUS:
- returnput_user(0,p);
- caseWDIOC_KEEPALIVE:
- s3c2410wdt_keepalive();
- return0;
- caseWDIOC_SETTIMEOUT:
- if(get_user(new_margin,p))
- return-EFAULT;
- if(s3c2410wdt_set_heartbeat(new_margin))
- return-EINVAL;
- s3c2410wdt_keepalive();
- returnput_user(tmr_margin,p);
- caseWDIOC_GETTIMEOUT:
- returnput_user(tmr_margin,p);
- default:
- return-ENOTTY;
- }
- }
- /*kernelinterface*/
- //文件操作結(jié)構(gòu)體
- staticconststructfile_operationss3c2410wdt_fops=
- {
- .owner=THIS_MODULE,
- .llseek=no_llseek,//屏蔽seek操作
- .write=s3c2410wdt_write,//寫(xiě)方法
- .unlocked_ioctl=s3c2410wdt_ioctl,//控制方法
- .open=s3c2410wdt_open,//代開(kāi)設(shè)備方法
- .release=s3c2410wdt_release,//關(guān)閉設(shè)備方法
- };
- staticstructmiscdevices3c2410wdt_miscdev=
- {
- .minor=WATCHDOG_MINOR,
- .name="watchdog",
- .fops=&s3c2410wdt_fops,
- };
- /*interrupthandlercode*/
- staticirqreturn_ts3c2410wdt_irq(intirqno,void*param)
- {
- //dev_info(wdt_dev,"watchdogtimerexpired(irq)n");
- s3c2410_gpio_setpin(S3C2410_GPB10,(count++)%2);
- s3c2410wdt_keepalive();
- returnIRQ_HANDLED;
- }
- /*deviceinterface*/
- //注冊(cè)設(shè)備時(shí)執(zhí)行
- staticints3c2410wdt_probe(structplatform_device*pdev)
- {
- structresource*res;
- structdevice*dev;
- unsignedintwtcon;
- intstarted=0;
- intret;
- intsize;
- dev=&pdev->dev;
- wdt_dev=&pdev->dev;
- /*getthememoryregionforthewatchdogtimer*/
- //獲取IO端口資源,這里IORESOURCE_MEM和平臺(tái)設(shè)備中資源的定義一致
- res=platform_get_resource(pdev,IORESOURCE_MEM,0);
- if(res==NULL){
- dev_err(dev,"nomemoryresourcespecifiedn");
- 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,"failedtogetmemoryregionn");
- ret=-ENOENT;
- gotoerr_req;
- }
- //地址映射,wdt_base為基地址
- wdt_base=ioremap(res->start,size);
- if(wdt_base==NULL){
- dev_err(dev,"failedtoioremap()regionn");
- ret=-EINVAL;
- gotoerr_req;
- }
- DBG("probe:mappedwdt_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,"noirqresourcespecifiedn");
- ret=-ENOENT;
- gotoerr_map;
- }
- //注冊(cè)中斷,中斷處理函數(shù)s3c2410wdt_irq()
- ret=request_irq(wdt_irq->start,s3c2410wdt_irq,0,pdev->name,pdev);
- if(ret!=0){
- dev_err(dev,"failedtoinstallirq(%d)n",ret);
- gotoerr_map;
- }
- //獲取系統(tǒng)時(shí)鐘
- wdt_clock=clk_get(&pdev->dev,"watchdog");
- if(IS_ERR(wdt_clock)){
- dev_err(dev,"failedtofindwatchdogclocksourcen");
- ret=PTR_ERR(wdt_clock);
- gotoerr_irq;
- }
- //使能時(shí)鐘
- clk_enable(wdt_clock);
- /*seeifwecanactuallysettherequestedtimermargin,andif
- *not,trythedefaultvalue*/
- 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_marginvalueoutofrange,default%dusedn",
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- else
- dev_info(dev,"defaulttimervalueisoutofrange,cannotstartn");
- }
- //注冊(cè)設(shè)備
- ret=misc_register(&s3c2410wdt_miscdev);
- if(ret)
- {
- dev_err(dev,"cannotregistermiscdevonminor=%d(%d)n",
- WATCHDOG_MINOR,ret);
- gotoerr_clk;
- }
- if(tmr_atboot&&started==0)
- {
- dev_info(dev,"startingwatchdogtimern");
- s3c2410wdt_start();//啟動(dòng)看門(mén)狗
- }
- elseif(!tmr_atboot)
- {
- /*ifwerenotenablingthewatchdog,thenensureitis
- *disabledifithasbeenleftrunningfromthebootloader
- *orothersource*/
- s3c2410wdt_stop();
- }
- /*printoutastatementofreadiness*/
- 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)亮LED10
- s3c2410_gpio_cfgpin(S3C2410_GPB10,S3C2410_GPB10_OUTP);
- return0;
- //出錯(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);
- returnret;
- }
- //設(shè)備移除函數(shù),釋放資源和映射
- staticints3c2410wdt_remove(structplatform_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);
- return0;
- }
- //關(guān)閉看門(mén)狗
- staticvoids3c2410wdt_shutdown(structplatform_device*dev)
- {
- s3c2410wdt_stop();
- }
- //定義平臺(tái)設(shè)備驅(qū)動(dòng)
- staticstructplatform_drivers3c2410wdt_driver={
- .probe=s3c2410wdt_probe,
- .remove=s3c2410wdt_remove,
- .shutdown=s3c2410wdt_shutdown,
- .driver={
- .owner=THIS_MODULE,
- .name="s3c2410-wdt",//該名稱(chēng)參照內(nèi)核源碼中該資源的名稱(chēng),兩者必須一致
- },
- };
- staticcharbanner[]__initdata=
- KERN_INFO"S3C2410WatchdogTimer,(c)2004SimtecElectronicsn";
- //驅(qū)動(dòng)安裝時(shí)執(zhí)行
- staticint__initwatchdog_init(void)
- {
- printk(banner);
- returnplatform_driver_register(&s3c2410wdt_driver);//注冊(cè)設(shè)備
- }
- //驅(qū)動(dòng)移除是執(zhí)行
- staticvoid__exitwatchdog_exit(void)
- {
- platform_driver_unregister(&s3c2410wdt_driver);//移除設(shè)備
- }
- module_init(watchdog_init);
- module_exit(watchdog_exit);
- MODULE_AUTHOR("YanMing");
- MODULE_DESCRIPTION("S3C2410WatchdogDeviceDriver");
- 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)論