硬件渠道:FL2440
内核版别:2.6.28
主机渠道:Ubuntu 11,04
内核版别:2.6.39
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
- #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;//设置默以为碑文中止
- staticintdebug;
- staticintcount;//用于计数,操控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;//用来保存IO端口占用的内存资源
- staticstructresource*wdt_irq;//保存wdt中止号
- staticstructclk*wdt_clock;//保存从渠道获取的watchdog时钟
- staticvoid__iomem*wdt_base;//通过ioremap后的内存基地址
- staticunsignedintwdt_count;//保存向WTCNT写的计数值
- staticclose_state_tallow_close;
- staticDEFINE_SPINLOCK(wdt_lock);//界说一个自旋锁,用于资源的互斥拜访
- /*watchdogcontrolroutines*/
- #defineDBG(msg…)do{\
- if(debug)\
- printk(KERN_INFOmsg);\
- }while(0)
- /*functions*/
- //喂狗函数,实际上是将wdt_count写入WTCNT寄存器
- staticvoids3c2410wdt_keepalive(void)
- {
- spin_lock(&wdt_lock);//给资源上锁
- writel(wdt_count,wdt_base+S3C2410_WTCNT);//写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);//设备看门狗使能无效、复位功用无效
- }
- //中止watchdog计时
- staticvoids3c2410wdt_stop(void)
- {
- spin_lock(&wdt_lock);
- __s3c2410wdt_stop();
- spin_unlock(&wdt_lock);
- }
- //发动watchdog计时
- staticvoids3c2410wdt_start(void)
- {
- unsignedlongwtcon;
- 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
- 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
- */
- //假如计时的时刻过大,则恰当加大预分频值
- if(count>=0x10000)
- {
- for(divisor=1;divisor<=0x100;divisor++)
- {
- if((count/divisor)<0x10000)
- break;
- }
- //若预分频最大仍不能分量计时周期,则报错
- if((count/divisor)>=0x10000)
- {
- dev_err(wdt_dev,”timeout%dtoobig\n”,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);//设置预分频值
- return0;
- }
- /*
- */dev/watchdoghandling
- */
- staticints3c2410wdt_open(structinode*inode,structfile*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;
- /*startthetimer*/
- s3c2410wdt_start();//发动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,notstoppingwatchdog\n”);
- 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))//从用户空间copy数据
- return-EFAULT;
- if(c==V)//当输入V时,封闭watchdog
- allow_close=CLOSE_STATE_ALLOW;
- }
- }
- //留意:这儿要手动喂狗一次?!
- s3c2410wdt_keepalive();//我们将allow_close设置成CLOSE_STATE_ALLOW后,当release时无法再次喂狗
- }
- returnlen;
- }
- #defineOPTIONSWDIOF_SETTIMEOUT|WDIOF_KEEPALIVEPING|WDIOF_MAGICCLOSE
- staticconststructwatchdog_infos3c2410_wdt_ident=
- {
- .options=OPTIONS,
- .firmware_version=0,
- .identity=”S3C2410Watchdog”,
- };
- //IO操控接口函数
- 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*/
- //文件操作结构体
- staticconststructfile_operationss3c2410wdt_fops=
- {
- .owner=THIS_MODULE,
- .llseek=no_llseek,//屏蔽seek操作
- .write=s3c2410wdt_write,//写办法
- .unlocked_ioctl=s3c2410wdt_ioctl,//操控办法
- .open=s3c2410wdt_open,//代开设备办法
- .release=s3c2410wdt_release,//封闭设备办法
- };
- 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*/
- //注册设备时碑文
- 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和渠道设备中资源的界说共同
- res=platform_get_resource(pdev,IORESOURCE_MEM,0);
- if(res==NULL){
- dev_err(dev,”nomemoryresourcespecified\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,”failedtogetmemoryregion\n”);
- ret=-ENOENT;
- gotoerr_req;
- }
- //地址映射,wdt_base为基地址
- wdt_base=ioremap(res->start,size);
- if(wdt_base==NULL){
- dev_err(dev,”failedtoioremap()region\n”);
- ret=-EINVAL;
- gotoerr_req;
- }
- DBG(“probe:mappedwdt_base=%p\n”,wdt_base);
- //从渠道设备中获取中止号
- wdt_irq=platform_get_resource(pdev,IORESOURCE_IRQ,0);
- if(wdt_irq==NULL){
- dev_err(dev,”noirqresourcespecified\n”);
- ret=-ENOENT;
- gotoerr_map;
- }
- //注册中止,中止处理函数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;
- }
- //获取体系时钟
- wdt_clock=clk_get(&pdev->dev,”watchdog”);
- if(IS_ERR(wdt_clock)){
- dev_err(dev,”failedtofindwatchdogclocksource\n”);
- ret=PTR_ERR(wdt_clock);
- gotoerr_irq;
- }
- //使能时钟
- clk_enable(wdt_clock);
- /*seeifwecanactuallysettherequestedtimermargin,andif
- *not,trythedefaultvalue*/
- if(s3c2410wdt_set_heartbeat(tmr_margin))
- {//这儿第一次设置计时周期为tmr_margin,假如设置不成功,则从头设置为默许值
- started=s3c2410wdt_set_heartbeat(
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- if(started==0)
- dev_info(dev,
- “tmr_marginvalueoutofrange,default%dused\n”,
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- else
- dev_info(dev,”defaulttimervalueisoutofrange,cannotstart\n”);
- }
- //注册设备
- 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,”startingwatchdogtimer\n”);
- s3c2410wdt_start();//发动看门狗
- }
- elseif(!tmr_atboot)
- {
- /*ifwerenotenablingthewatchdog,thenensureitis
- *disabledifithasbeenleftrunningfromthebootloader
- *orothersource*/
- s3c2410wdt_stop();
- }
- /*printoutastatementofreadiness*/
- 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端口为输出端口,用于点亮LED10
- s3c2410_gpio_cfgpin(S3C2410_GPB10,S3C2410_GPB10_OUTP);
- return0;
- //犯错跳转表,反常处理
- 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;
- }
- //设备移除函数,开释资源和映射
- 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;
- }
- //封闭看门狗
- staticvoids3c2410wdt_shutdown(structplatform_device*dev)
- {
- s3c2410wdt_stop();
- }
- //界说渠道设备驱动
- staticstructplatform_drivers3c2410wdt_driver={
- .probe=s3c2410wdt_probe,
- .remove=s3c2410wdt_remove,
- .shutdown=s3c2410wdt_shutdown,
- .driver={
- .owner=THIS_MODULE,
- .name=”s3c2410-wdt”,//该称号参照内核源码中该资源的称号,两者有必要共同
- },
- };
- staticcharbanner[]__initdata=
- KERN_INFO”S3C2410WatchdogTimer,(c)2004SimtecElectronics\n”;
- //驱动装置时碑文
- staticint__initwatchdog_init(void)
- {
- printk(banner);
- returnplatform_driver_register(&s3c2410wdt_driver);//注册设备
- }
- //驱动移除是碑文
- staticvoid__exitwatchdog_exit(void)
- {
- platform_driver_unregister(&s3c2410wdt_driver);//移除设备
- }
- module_init(watchdog_init);
- module_exit(watchdog_exit);
- MODULE_AUTHOR(“YanMing”);
- MODULE_DESCRIPTION(“S3C2410WatchdogDeviceDriver”);
- MODULE_L%&&&&&%ENSE(“GPL”);
- MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
设置默许不是重启机器,而是碑文中止函数,当不喂狗,计数器减到0,点亮LED,然后喂狗,从头计数。