硬件渠道:FL2440(S3C2440)
内核版别:Linux 2.6.28
主机渠道:Ubuntu 11.04
内核版别:Linux 2.6.39
穿插编译器版别:arm-linux-gcc 3.4.1
原创著作,转载请标明出处http://blog.csdn.net/yming0221/article/details/6584285
1、实时时钟概述
实时时钟(RTC)单元能够在断电的状况下运用怯懦电池持续计时作业。RTC运用STRB/LDRB ARM操作传输二进制码十进制数的8位数据给CPU。其间的数据包含秒、分、时、日期、天、月、年的时刻信息。能够碑文报警功用。
2、实时时钟操作
下面是RTC模块的电路图
3、RTC寄存器介绍
实时时钟操控寄存器(RTCCON)-REAL TIME CLOCK CONTROL REGISTER
节拍时刻计数寄存器(TICNT)-TICK TIME COUNT REGISTER
RTC报警操控寄存器(RTCALM)-RTC ALARM CONTROL REGISTER
报警秒数寄存器(ALMSEC)-ALARM SECOND DATA REGISTER
报警分钟计数寄存器(ALMMIN)-ALARM MIN DATA REGISTER
报警小时数据寄存器(ALMHOUR)-ALARM HOUR DATA REGISTER
报警日期数据寄存器(ALMDATE)-ALARM DATE DATA REGISTER
报警月数数据寄存器(ALMMON)-ALARM MON DATA REGISTER
报警年数数据寄存器(ALMYEAR)-ALARM YEAR DATA REGISTER
BCD数据寄存器的格局和报警寄存器结构相同,仅仅对应的地址不同。
BCD秒寄存器(BCDSEC)-BCD SECOND REGISTER 地址:0x57000070(L)0x57000073(B)
BCD分寄存器(BCDMIN)-BCD MINUTE REGISTER 地址:0x57000074(L)0x57000077(B)
BCD小时寄存器(BCDHOUR)-BCD HOUR REGISTER 地址:0x57000078(L)0x5700007B(B)
BCD日期寄存器(BCDDATE)-BCD DATE REGISTER 地址:0x5700007C(L)0x5700007F(B)
BCD日寄存器(BCDDAY)-BCD DAY REGISTER 地址:0x57000080(L)0x57000083(B)
BCD月寄存器(BCDMON)-BCD MONTH REGISTER 地址:0x57000084(L)0x57000087(B)
BCD年寄存器(BCDYEAR)-BCD YEAR REGISTER 地址:0x57000088(L)0x5700008B(B)
4、驱动实例剖析
为了使驱动更简单了解,现在这个RTC驱动只完结了计时功用,没有增加相应的报警功用,也没有增加电源办理的功用,短少的功用往后完善。
下面先整体了解驱动:
首先是RTC驱动的结构体,在/include/linux/platform_device.h中,如下
structplatform_driver{ - int(*probe)(structplatform_device*);
- int(*remove)(structplatform_device*);
- void(*shutdown)(structplatform_device*);
- int(*suspend)(structplatform_device*,pm_message_tstate);
- int(*suspend_late)(structplatform_device*,pm_message_tstate);
- int(*resume_early)(structplatform_device*);
- int(*resume)(structplatform_device*);
- structpm_ext_ops*pm;
- structdevice_driverdriver;
- };
驱动中界说对应的结构体
- staticstructplatform_drivers3c2410_rtc_driver={
- .probe=s3c_rtc_probe,//RTC勘探函数
- .remove=__devexit_p(s3c_rtc_remove),//RTC移除函数
- .driver={
- .name=”s3c2410-rtc”,
- .owner=THIS_MODULE,
- },
- };
下面是驱动中驱动的初始化和退出函数
- staticint__inits3c_rtc_init(void)
- {
- printk(banner);
- returnplatform_driver_register(&s3c2410_rtc_driver);
- }
- staticvoid__exits3c_rtc_exit(void)
- {
- platform_driver_unregister(&s3c2410_rtc_driver);
- }
platform_driver_register()和platform_driver_unregister()函数在/drivers/base/platform.c中完结的。
能够看出,platform_driver_register()函数的效果便是为platform_driver中的driver中的probe、remove等供给接口函数
- intplatform_driver_register(structplatform_driver*drv)
- {
- drv->driver.bus=&platform_bus_type;
- if(drv->probe)
- drv->driver.probe=platform_drv_probe;
- if(drv->remove)
- drv->driver.remove=platform_drv_remove;
- if(drv->shutdown)
- drv->driver.shutdown=platform_drv_shutdown;
- if(drv->suspend)
- drv->driver.suspend=platform_drv_suspend;
- if(drv->resume)
- drv->driver.resume=platform_drv_resume;
- if(drv->pm)
- drv->driver.pm=&drv->pm->base;
- returndriver_register(&drv->driver);//注册老的驱动
- }
- voidplatform_driver_unregister(structplatform_driver*drv)
- {
- driver_unregister(&drv->driver);
- }
接下来是RTC渠道驱动勘探函数s3c_rtc_probe,下面函数界说的时分运用了__devinit的效果是使编译器优化代码,将其放在和是的内存方位,削减内存占用和进步内核功率。
probe函数接收到plarform_device这个参数后,就需求从中提取出需求的信息。它一般会经过调用内核供给的platform_get_resource和platform_get_irq等函数来取得相关信息。如经过platform_get_resource取得设备的开始地址后,能够对其进行request_mem_region和ioremap等操作,以便应用程序对其进行操作。经过platform_get_irq得到设备的中止号今后,就能够调用request_irq函数来向体系请求中止。这些操作在设备驱动程序中一般都要完结。
- staticint__devinits3c_rtc_probe(structplatform_device*pdev)
- {
- structrtc_device*rtc;//界说rtc_device结构体,界说在/include/linux/rtc.h
- structresource*res;//界说资源结构体,界说在/include/linux/ioport.h
- intret;
- pr_debug(“%s:probe=%p\n”,__func__,pdev);
- /*findtheIRQs*/
- s3c_rtc_tickno=platform_get_irq(pdev,1);//在体系界说的渠道设备中获取中止号
- if(s3c_rtc_tickno<0){//反常处理
- dev_err(&pdev->dev,”noirqforrtctick\n”);
- return-ENOENT;
- }
- /*getthememoryregion*/
- res=platform_get_resource(pdev,IORESOURCE_MEM,0);//获取RTC渠道运用的IO资源
- if(res==NULL){
- dev_err(&pdev->dev,”failedtogetmemoryregionresource\n”);
- return-ENOENT;
- }
- //请求内存区域,res是structresource类型,见本函数后边
- s3c_rtc_mem=request_mem_region(res->start,
- res->end-res->start+1,
- pdev->name);
- if(s3c_rtc_mem==NULL){//请求内存犯错
- dev_err(&pdev->dev,”failedtoreservememoryregion\n”);
- ret=-ENOENT;
- gotoerr_nores;
- }
- //将寄存器地址映射成虚拟地址,以便拜访
- s3c_rtc_base=ioremap(res->start,res->end-res->start+1);
- if(s3c_rtc_base==NULL){
- dev_err(&pdev->dev,”failedioremap()\n”);
- ret=-EINVAL;
- gotoerr_nomap;
- }
- /*checktoseeifeverythingissetupcorrectly*/
- s3c_rtc_enable(pdev,1);//对RTCCON寄存器设置,概况见下面的函数完结
- pr_debug(“s3c2410_rtc:RTCCON=%02x\n”,
- readb(s3c_rtc_base+S3C2410_RTCCON));
- s3c_rtc_setfreq(&pdev->dev,1);//概况见下面的函数完结
- /*registerRTCandexit*/
- rtc=rtc_device_register(“s3c”,&pdev->dev,&s3c_rtcops,
- THIS_MODULE);//注册RTC为RTC设备,其间s3c_rtcops界说见下
- if(IS_ERR(rtc)){
- dev_err(&pdev->dev,”cannotattachrtc\n”);
- ret=PTR_ERR(rtc);
- gotoerr_nortc;
- }
- rtc->max_user_freq=128;//设置RTC节拍时刻计数寄存器TICNT的节拍时刻计数值的用户最大相对值
- //将RTC类的设备数据传递给体系设备,在/include/linux/platform_device.h中
- //#defineplatform_set_drvdata(_dev,data)
dev_set_drvdata(&(_dev)->dev,(data)),该函数在/include/linux/device.h中界说,见本函数下面
- platform_set_drvdata(pdev,rtc);
- return0;
- //反常处理
- err_nortc:
- s3c_rtc_enable(pdev,0);
- iounmap(s3c_rtc_base);
- err_nomap:
- release_resource(s3c_rtc_mem);
- err_nores:
- returnret;
- }
下面是/include/linux/ioport.h中struct resource结构体界说
- structresource{
- resource_size_tstart;
- resource_size_tend;
- constchar*name;
- unsignedlongflags;
- structresource*parent,*sibling,*child;
- };
这是dev_set_drvdata()的函数界说:
- staticinlinevoiddev_set_drvdata(structdevice*dev,void*data)
- {
- dev->driver_data=data;
- }
接下来是在s3c_rtc_probe()函数用到的两个函数s3c_rtc_enable()和s3c_rtc_setfreq()
- staticvoids3c_rtc_enable(structplatform_device*pdev,inten)
- {
- void__iomem*base=s3c_rtc_base;//__iomem的效果便是为了使编译器更好的优化编译
- unsignedinttmp;
- if(s3c_rtc_base==NULL)
- return;
- //en作为参数传递过来假如en==0,封闭电源前的状况
- if(!en){
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp&~S3C2410_RTCCON_RTCEN,base+S3C2410_RTCCON);//设置RTCCON寄存器,屏蔽RTC使能,能够参阅数据手册中寄存器的相关界说
- tmp=readb(base+S3C2410_TICNT);
- writeb(tmp&~S3C2410_TICNT_ENABLE,base+S3C2410_TICNT);//设置TICNT寄存器,屏蔽节拍时刻中止使能
- }else{
- /*re-enablethedevice,andcheckitisok*/
- //en!=0的状况,一共体系复位,从头使能RTC驱动
- if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_RTCEN)==0){//RTCCON第0位为0,将其设置为1,从头使能
- dev_info(&pdev->dev,”rtcdisabled,re-enabling\n”);
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp|S3C2410_RTCCON_RTCEN,base+S3C2410_RTCCON);
- }
- if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_CNTSEL)){
- dev_info(&pdev->dev,”removingRTCCON_CNTSEL\n”);
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp&~S3C2410_RTCCON_CNTSEL,base+S3C2410_RTCCON);//设置RTCCON第2位为0,设置BCD计数为混合BCD计数
- }
- if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_CLKRST)){
- dev_info(&pdev->dev,”removingRTCCON_CLKRST\n”);
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp&~S3C2410_RTCCON_CLKRST,base+S3C2410_RTCCON);//RTC时钟计数器复位
- }
- }
- }
- staticints3c_rtc_setfreq(structdevice*dev,intfreq)//设定节拍时刻计数值
- {
- unsignedinttmp;
- spin_lock_irq(&s3c_rtc_pie_lock);//获取自旋锁,对资源互斥拜访
- tmp=readb(s3c_rtc_base+S3C2410_TICNT)&S3C2410_TICNT_ENABLE;//节拍时刻使能有用
- tmp|=(128/freq)-1;
- writeb(tmp,s3c_rtc_base+S3C2410_TICNT);
- spin_unlock_irq(&s3c_rtc_pie_lock);//解锁
- return0;
- }
接下来是RTC设备类的操作。
下面是rtc_class_ops是RTC设备类在RTC驱动中心部分中界说的对RTC设备类进行操作的结构体,相似字符设备在驱动中的file_operations对字符设备进行操作的意思。该结构体被界说在rtc.h中,对RTC的操作首要有翻开、封闭、设置或获取时刻、设置或获取报警、设置节拍时刻计数值等等,该结构体内接口函数的完结都在下面
- staticconststructrtc_class_opss3c_rtcops={
- .open=s3c_rtc_open,
- .release=s3c_rtc_release,
- .read_time=s3c_rtc_gettime,
- .set_time=s3c_rtc_settime,
- .irq_set_freq=s3c_rtc_setfreq,
- .irq_set_state=s3c_rtc_setpie,
- };
RTC翻开设备函数s3c_rtc_open()
- staticints3c_rtc_open(structdevice*dev)
- {
- structplatform_device*pdev=to_platform_device(dev);//从渠道设备中获取RTC设备类的数据
- structrtc_device*rtc_dev=platform_get_drvdata(pdev);
- intret;
- ret=request_irq(s3c_rtc_tickno,s3c_rtc_tickirq,
- IRQF_DISABLED,”s3c2410-rtctick”,rtc_dev);//请求中止
- if(ret){
- dev_err(dev,”IRQ%derror%d\n”,s3c_rtc_tickno,ret);
- gototick_err;
- }
- tick_err:
- returnret;
- }
RTC TICK节拍时刻中止服务程序
- staticirqreturn_ts3c_rtc_tickirq(intirq,void*id)
- {
- structrtc_device*rdev=id;
- rtc_update_irq(rdev,1,RTC_PF|RTC_IRQF);
- returnIRQ_HANDLED;
- }
RTC封闭设备函数s3c_rtc_release()
- staticvoids3c_rtc_release(structdevice*dev)
- {
- structplatform_device*pdev=to_platform_device(dev);//从渠道设备中获取RTC设备类的数据
- structrtc_device*rtc_dev=platform_get_drvdata(pdev);
- /*donotclearAIEhere,itmaybeneededforwake*/
- s3c_rtc_setpie(dev,0);//函数界说见下面
- free_irq(s3c_rtc_tickno,rtc_dev);
- }
s3c_rtc_setpie()函数,该函数首要效果便是依据参数设置TICNT寄存器的最高位,参数为0,制止使能,参数为1,使能
- staticints3c_rtc_setpie(structdevice*dev,intenabled)
- {
- unsignedinttmp;
- pr_debug(“%s:pie=%d\n”,__func__,enabled);
- spin_lock_irq(&s3c_rtc_pie_lock);
- tmp=readb(s3c_rtc_base+S3C2410_TICNT)&~S3C2410_TICNT_ENABLE;//读取TICNT的值并将最高位清0
- if(enabled)
- tmp|=S3C2410_TICNT_ENABLE;
- writeb(tmp,s3c_rtc_base+S3C2410_TICNT);//写入核算后新的值
- spin_unlock_irq(&s3c_rtc_pie_lock);
- return0;
- }
下面两个函数是设置和读取BCD寄存器的时刻,逻辑很简单,仅仅读取和设置相应寄存器的值
- staticints3c_rtc_gettime(structdevice*dev,structrtc_time*rtc_tm)
- {
- unsignedinthave_retried=0;
- void__iomem*base=s3c_rtc_base;
- retry_get_time:
- rtc_tm->tm_min=readb(base+S3C2410_RTCMIN);
- rtc_tm->tm_hour=readb(base+S3C2410_RTCHOUR);
- rtc_tm->tm_mday=readb(base+S3C2410_RTCDATE);
- rtc_tm->tm_mon=readb(base+S3C2410_RTCMON);
- rtc_tm->tm_year=readb(base+S3C2410_RTCYEAR);
- rtc_tm->tm_sec=readb(base+S3C2410_RTCSEC);
- /*theonlywaytoworkoutwetherthesystemwasmid-update
- *whenwereaditistocheckthesecondcounter,andifit
- *iszero,thenwere-trytheentireread
- */
- if(rtc_tm->tm_sec==0&&!have_retried){
- have_retried=1;
- gotoretry_get_time;
- }
- pr_debug(“readtime%02x.%02x.%02x%02x/%02x/%02x\n”,
- rtc_tm->tm_year,rtc_tm->tm_mon,rtc_tm->tm_mday,
- rtc_tm->tm_hour,rtc_tm->tm_min,rtc_tm->tm_sec);
- rtc_tm->tm_sec=bcd2bin(rtc_tm->tm_sec);
- rtc_tm->tm_min=bcd2bin(rtc_tm->tm_min);
- rtc_tm->tm_hour=bcd2bin(rtc_tm->tm_hour);
- rtc_tm->tm_mday=bcd2bin(rtc_tm->tm_mday);
- rtc_tm->tm_mon=bcd2bin(rtc_tm->tm_mon);
- rtc_tm->tm_year=bcd2bin(rtc_tm->tm_year);
- rtc_tm->tm_year+=100;
- rtc_tm->tm_mon-=1;
- return0;
- }
- staticints3c_rtc_settime(structdevice*dev,structrtc_time*tm)
- {
- void__iomem*base=s3c_rtc_base;
- intyear=tm->tm_year-100;
- pr_debug(“settime%02d.%02d.%02d%02d/%02d/%02d\n”,
- tm->tm_year,tm->tm_mon,tm->tm_mday,
- tm->tm_hour,tm->tm_min,tm->tm_sec);
- /*wegetaroundy2kbysimplynotsupportingit*/
- if(year<0||year>=100){
- dev_err(dev,”rtconlysupports100years\n”);
- return-EINVAL;
- }
- writeb(bin2bcd(tm->tm_sec),base+S3C2410_RTCSEC);
- writeb(bin2bcd(tm->tm_min),base+S3C2410_RTCMIN);
- writeb(bin2bcd(tm->tm_hour),base+S3C2410_RTCHOUR);
- writeb(bin2bcd(tm->tm_mday),base+S3C2410_RTCDATE);
- writeb(bin2bcd(tm->tm_mon+1),base+S3C2410_RTCMON);
- writeb(bin2bcd(year),base+S3C2410_RTCYEAR);
- return0;
- }
到这儿RTC驱动的计时功用完结,报警功用还没有完结。下面是这个驱动源代码
- #include
- #include
- #include
- #include
- #include
- #include
interrupt.h> - #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- staticstructresource*s3c_rtc_mem;
- staticvoid__iomem*s3c_rtc_base;
- staticints3c_rtc_tickno=NO_IRQ;
- staticDEFINE_SPINLOCK(s3c_rtc_pie_lock);
- staticirqreturn_ts3c_rtc_tickirq(intirq,void*id)
- {
- structrtc_device*rdev=id;
- rtc_update_irq(rdev,1,RTC_PF|RTC_IRQF);
- returnIRQ_HANDLED;
- }
- /*Updatecontrolregisters*/
- staticvoids3c_rtc_setaie(intto)
- {
- unsignedinttmp;
- pr_debug(“%s:aie=%d\n”,__func__,to);
- tmp=readb(s3c_rtc_base+S3C2410_RTCALM)&~S3C2410_RTCALM_ALMEN;
- if(to)
- tmp|=S3C2410_RTCALM_ALMEN;
- writeb(tmp,s3c_rtc_base+S3C2410_RTCALM);
- }
- staticints3c_rtc_setpie(structdevice*dev,intenabled)
- {
- unsignedinttmp;
- pr_debug(“%s:pie=%d\n”,__func__,enabled);
- spin_lock_irq(&s3c_rtc_pie_lock);
- tmp=readb(s3c_rtc_base+S3C2410_TICNT)&~S3C2410_TICNT_ENABLE;
- if(enabled)
- tmp|=S3C2410_TICNT_ENABLE;
- writeb(tmp,s3c_rtc_base+S3C2410_TICNT);
- spin_unlock_irq(&s3c_rtc_pie_lock);
- return0;
- }
- staticints3c_rtc_setfreq(structdevice*dev,intfreq)
- {
- unsignedinttmp;
- spin_lock_irq(&s3c_rtc_pie_lock);
- tmp=readb(s3c_rtc_base+S3C2410_TICNT)&S3C2410_TICNT_ENABLE;
- tmp|=(128/freq)-1;
- writeb(tmp,s3c_rtc_base+S3C2410_TICNT);
- spin_unlock_irq(&s3c_rtc_pie_lock);
- return0;
- }
- /*Timeread/write*/
- staticints3c_rtc_gettime(structdevice*dev,structrtc_time*rtc_tm)
- {
- unsignedinthave_retried=0;
- void__iomem*base=s3c_rtc_base;
- retry_get_time:
- rtc_tm->tm_min=readb(base+S3C2410_RTCMIN);
- rtc_tm->tm_hour=readb(base+S3C2410_RTCHOUR);
- rtc_tm->tm_mday=readb(base+S3C2410_RTCDATE);
- rtc_tm->tm_mon=readb(base+S3C2410_RTCMON);
- rtc_tm->tm_year=readb(base+S3C2410_RTCYEAR);
- rtc_tm->tm_sec=readb(base+S3C2410_RTCSEC);
- /*theonlywaytoworkoutwetherthesystemwasmid-update
- *whenwereaditistocheckthesecondcounter,andifit
- *iszero,thenwere-trytheentireread
- */
- if(rtc_tm->tm_sec==0&&!have_retried){
- have_retried=1;
- gotoretry_get_time;
- }
- pr_debug(“readtime%02x.%02x.%02x%02x/%02x/%02x\n”,
- rtc_tm->tm_year,rtc_tm->tm_mon,rtc_tm->tm_mday,
- rtc_tm->tm_hour,rtc_tm->tm_min,rtc_tm->tm_sec);
- rtc_tm->tm_sec=bcd2bin(rtc_tm->tm_sec);
- rtc_tm->tm_min=bcd2bin(rtc_tm->tm_min);
- rtc_tm->tm_hour=bcd2bin(rtc_tm->tm_hour);
- rtc_tm->tm_mday=bcd2bin(rtc_tm->tm_mday);
- rtc_tm->tm_mon=bcd2bin(rtc_tm->tm_mon);
- rtc_tm->tm_year=bcd2bin(rtc_tm->tm_year);
- rtc_tm->tm_year+=100;
- rtc_tm->tm_mon-=1;
- return0;
- }
- staticints3c_rtc_settime(structdevice*dev,structrtc_time*tm)
- {
- void__iomem*base=s3c_rtc_base;
- intyear=tm->tm_year-100;
- pr_debug(“settime%02d.%02d.%02d%02d/%02d/%02d\n”,
- tm->tm_year,tm->tm_mon,tm->tm_mday,
- tm->tm_hour,tm->tm_min,tm->tm_sec);
- /*wegetaroundy2kbysimplynotsupportingit*/
- if(year<0||year>=100){
- dev_err(dev,”rtconlysupports100years\n”);
- return-EINVAL;
- }
- writeb(bin2bcd(tm->tm_sec),base+S3C2410_RTCSEC);
- writeb(bin2bcd(tm->tm_min),base+S3C2410_RTCMIN);
- writeb(bin2bcd(tm->tm_hour),base+S3C2410_RTCHOUR);
- writeb(bin2bcd(tm->tm_mday),base+S3C2410_RTCDATE);
- writeb(bin2bcd(tm->tm_mon+1),base+S3C2410_RTCMON);
- writeb(bin2bcd(year),base+S3C2410_RTCYEAR);
- return0;
- }
- staticints3c_rtc_open(structdevice*dev)
- {
- structplatform_device*pdev=to_platform_device(dev);
- structrtc_device*rtc_dev=platform_get_drvdata(pdev);
- intret;
- ret=request_irq(s3c_rtc_tickno,s3c_rtc_tickirq,
- IRQF_DISABLED,”s3c2410-rtctick”,rtc_dev);
- if(ret){
- dev_err(dev,”IRQ%derror%d\n”,s3c_rtc_tickno,ret);
- gototick_err;
- }
- tick_err:
- returnret;
- }
- staticvoids3c_rtc_release(structdevice*dev)
- {
- structplatform_device*pdev=to_platform_device(dev);
- structrtc_device*rtc_dev=platform_get_drvdata(pdev);
- /*donotclearAIEhere,itmaybeneededforwake*/
- s3c_rtc_setpie(dev,0);
- free_irq(s3c_rtc_tickno,rtc_dev);
- }
- staticconststructrtc_class_opss3c_rtcops={
- .open=s3c_rtc_open,
- .release=s3c_rtc_release,
- .read_time=s3c_rtc_gettime,
- .set_time=s3c_rtc_settime,
- .irq_set_freq=s3c_rtc_setfreq,
- .irq_set_state=s3c_rtc_setpie,
- };
- staticvoids3c_rtc_enable(structplatform_device*pdev,inten)
- {
- void__iomem*base=s3c_rtc_base;
- unsignedinttmp;
- if(s3c_rtc_base==NULL)
- return;
- if(!en){
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp&~S3C2410_RTCCON_RTCEN,base+S3C2410_RTCCON);
- tmp=readb(base+S3C2410_TICNT);
- writeb(tmp&~S3C2410_TICNT_ENABLE,base+S3C2410_TICNT);
- }else{
- /*re-enablethedevice,andcheckitisok*/
- if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_RTCEN)==0){
- dev_info(&pdev->dev,”rtcdisabled,re-enabling\n”);
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp|S3C2410_RTCCON_RTCEN,base+S3C2410_RTCCON);
- }
- if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_CNTSEL)){
- dev_info(&pdev->dev,”removingRTCCON_CNTSEL\n”);
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp&~S3C2410_RTCCON_CNTSEL,base+S3C2410_RTCCON);
- }
- if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_CLKRST)){
- dev_info(&pdev->dev,”removingRTCCON_CLKRST\n”);
- tmp=readb(base+S3C2410_RTCCON);
- writeb(tmp&~S3C2410_RTCCON_CLKRST,base+S3C2410_RTCCON);
- }
- }
- }
- staticint__devexits3c_rtc_remove(structplatform_device*dev)
- {
- structrtc_device*rtc=platform_get_drvdata(dev);
- platform_set_drvdata(dev,NULL);
- rtc_device_unregister(rtc);
- s3c_rtc_setpie(&dev->dev,0);
- s3c_rtc_setaie(0);
- iounmap(s3c_rtc_base);
- release_resource(s3c_rtc_mem);
- kfree(s3c_rtc_mem);
- return0;
- }
- staticint__devinits3c_rtc_probe(structplatform_device*pdev)
- {
- structrtc_device*rtc;
- structresource*res;
- intret;
- pr_debug(“%s:probe=%p\n”,__func__,pdev);
- /*findtheIRQs*/
- s3c_rtc_tickno=platform_get_irq(pdev,1);
- if(s3c_rtc_tickno<0){
- dev_err(&pdev->dev,”noirqforrtctick\n”);
- return-ENOENT;
- }
- /*getthememoryregion*/
- res=platform_get_resource(pdev,IORESOURCE_MEM,0);
- if(res==NULL){
- dev_err(&pdev->dev,”failedtogetmemoryregionresource\n”);
- return-ENOENT;
- }
- s3c_rtc_mem=request_mem_region(res->start,
- res->end-res->start+1,
- pdev->name);
- if(s3c_rtc_mem==NULL){
- dev_err(&pdev->dev,”failedtoreservememoryregion\n”);
- ret=-ENOENT;
- gotoerr_nores;
- }
- s3c_rtc_base=ioremap(res->start,res->end-res->start+1);
- if(s3c_rtc_base==NULL){
- dev_err(&pdev->dev,”failedioremap()\n”);
- ret=-EINVAL;
- gotoerr_nomap;
- }
- /*checktoseeifeverythingissetupcorrectly*/
- s3c_rtc_enable(pdev,1);
- pr_debug(“s3c2410_rtc:RTCCON=%02x\n”,
- readb(s3c_rtc_base+S3C2410_RTCCON));
- s3c_rtc_setfreq(&pdev->dev,1);
- /*registerRTCandexit*/
- rtc=rtc_device_register(“s3c”,&pdev->dev,&s3c_rtcops,
- THIS_MODULE);
- if(IS_ERR(rtc)){
- dev_err(&pdev->dev,”cannotattachrtc\n”);
- ret=PTR_ERR(rtc);
- gotoerr_nortc;
- }
- rtc->max_user_freq=128;
- platform_set_drvdata(pdev,rtc);
- return0;
- err_nortc:
- s3c_rtc_enable(pdev,0);
- iounmap(s3c_rtc_base);
- err_nomap:
- release_resource(s3c_rtc_mem);
- err_nores:
- returnret;
- }
- staticstructplatform_drivers3c2410_rtc_driver={
- .probe=s3c_rtc_probe,
- .remove=__devexit_p(s3c_rtc_remove),
- .driver={
- .name=”s3c2410-rtc”,
- .owner=THIS_MODULE,
- },
- };
- staticchar__initdatabanner[]=”S3C24XXRTC,(c)2004,2006SimtecElectronics\n”;
- staticint__inits3c_rtc_init(void)
- {
- printk(banner);
- returnplatform_driver_register(&s3c2410_rtc_driver);
- }
- staticvoid__exits3c_rtc_exit(void)
- {
- platform_driver_unregister(&s3c2410_rtc_driver);
- }
- module_init(s3c_rtc_init);
- module_exit(s3c_rtc_exit);
- MODULE_DESCRIPTION(“Mys3c2440RTCDriver”);
- MODULE_AUTHOR(“YanMing-yming0221@gmail.com”);
- MODULE_L%&&&&&%ENSE(“GPL”);
- MODULE_ALIAS(“platform:s3c2410-rtc”);
Makefile文件
- obj-m:=rtc.o
- KERNELDIR?=/arm/linux-2.6.28.7-2440
- PWD:=$(shellpwd)
- default:
- $(MAKE)-C$(KERNELDIR)M=$(PWD)modules
- clean:
- rm-f*.o*.ko*.order*.symvers
make后在目录下生成rtc.ko驱动,使用NFS挂在到方针板,insmod rtc.ko驱动就能够加载,碑文hwclock指令,检查是否能够读取硬件的RTC。