zoukankan      html  css  js  c++  java
  • Linux驱动修炼之道-RTC子系统框架与源码分析【转】

    转自:http://helloyesyes.iteye.com/blog/1072433

    努力成为linux kernel hacker的人李万鹏原创作品,为梦而战。转载请标明出处

    http://blog.csdn.net/woshixingaaa/archive/2011/05/21/6436215.aspx

    RTC(实时时钟)是一种典型的字符设备,作为一种字符设备驱动,RTC需要有file_operations中接口函数的实现,如open(),release(),read(),poll(),ioctl()等,而典型的ioctl包括RTC_SET_TIME,RTC_ALM_READ,RTC_ALM_SET,RTC_IRQP_SET,RTC_IRQP_READ等,这些对于所有的RTC是通用的,只有底层的具体实现是设备相关的。如下图可以清楚看出RTC子系统的框架。


    下面介绍几个重要的数据结构:

    rtc_device用来描述rtc设备:

    C-sharp代码 
    1. struct rtc_device  
    2. {  
    3.     struct device dev;  
    4.     struct module *owner;  
    5.     int id;                                   //RTC设备的次设备号  
    6.     char name[RTC_DEVICE_NAME_SIZE];  
    7.     const struct rtc_class_ops *ops;  
    8.     struct mutex ops_lock;  
    9.     struct cdev char_dev;  
    10.     unsigned long flags;  
    11.     unsigned long irq_data;  
    12.     spinlock_t irq_lock;  
    13.     wait_queue_head_t irq_queue;  
    14.     struct fasync_struct *async_queue;  
    15.     struct rtc_task *irq_task;  
    16.     spinlock_t irq_task_lock;  
    17.     int irq_freq;  
    18.     int max_user_freq;  
    19. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL  
    20.     struct work_struct uie_task;  
    21.     struct timer_list uie_timer;  
    22.     /* Those fields are protected by rtc->irq_lock */  
    23.     unsigned int oldsecs;  
    24.     unsigned int uie_irq_active:1;  
    25.     unsigned int stop_uie_polling:1;  
    26.     unsigned int uie_task_active:1;  
    27.     unsigned int uie_timer_active:1;  
    28. #endif  
    29. };  

    rtc_time用于get time/set time:

    C-sharp代码 
    1. struct rtc_time {  
    2.     int tm_sec;  
    3.     int tm_min;  
    4.     int tm_hour;  
    5.     int tm_mday;  
    6.     int tm_mon;  
    7.     int tm_year;  
    8.     int tm_wday;  
    9.     int tm_yday;  
    10.     int tm_isdst;  
    11. };  

    描述报警状态的结构:

    C-sharp代码 
    1. struct rtc_wkalrm {  
    2.     unsigned char enabled;  /* 0 = alarm disabled, 1 = alarm enabled */  
    3.     unsigned char pending;  /* 0 = alarm not pending, 1 = alarm pending */  
    4.     struct rtc_time time;   /* time the alarm is set to */  
    5. };  
    C-sharp代码 
    1. struct rtc_class_ops {  
    2.     int (*open)(struct device *);     //打开设备时的回调函数,这个函数应该初始化硬件并申请资源  
    3.     void (*release)(struct device *); //这个函数是设备关闭时被调用的,应该注销申请的资源  
    4.     int (*ioctl)(struct device *, unsigned int, unsigned long); //ioctl函数,对想让RTC自己实现的命令应返回ENOIOCTLCMD  
    5.     int (*read_time)(struct device *, struct rtc_time *);       //读取时间  
    6.     int (*set_time)(struct device *, struct rtc_time *);        //设置时间  
    7.     int (*read_alarm)(struct device *, struct rtc_wkalrm *);    //读取下一次定时中断的时间  
    8.     int (*set_alarm)(struct device *, struct rtc_wkalrm *);     //设置下一次定时中断的时间  
    9.     int (*proc)(struct device *, struct seq_file *);            //procfs接口  
    10.     int (*set_mmss)(struct device *, unsigned long secs);       //将传入的参数secs转换为struct rtc_time然后调用set_time函数。程序员可以不实现这个函数,但  
    11. 前提是定义好了read_time/set_time,因为RTC框架需要用这两个函数来实现这个功能。  
    12.     int (*irq_set_state)(struct device *, int enabled);         //周期采样中断的开关,根据enabled的值来设置  
    13.     int (*irq_set_freq)(struct device *, int freq);             //设置周期中断的频率  
    14.     int (*read_callback)(struct device *, int data);            ///用户空间获得数据后会传入读取的数据,并用这个函数返回的数据更新数据。  
    15.     int (*alarm_irq_enable)(struct device *, unsigned int enabled);  //alarm中断使能开关,根据enabled的值来设置  
    16.     int (*update_irq_enable)(struct device *, unsigned int enabled); //更新中断使能开关,根据enabled的值来设置  
    17. };  

    现在来看看rtc子系统是怎么注册上的:

    C-sharp代码 
    1. static int __init rtc_init(void)  
    2. {  
    3.     rtc_class = class_create(THIS_MODULE, "rtc");  
    4.     if (IS_ERR(rtc_class)) {  
    5.         printk(KERN_ERR "%s: couldn't create class ", __FILE__);  
    6.         return PTR_ERR(rtc_class);  
    7.     }  
    8.     rtc_class->suspend = rtc_suspend;  
    9.     rtc_class->resume = rtc_resume;  
    10.     rtc_dev_init();  
    11.     rtc_sysfs_init(rtc_class);  
    12.     return 0;  
    13. }  
    14. void __init rtc_dev_init(void)  
    15. {  
    16.     int err;  
    17.     err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");  
    18.     if (err < 0)  
    19.         printk(KERN_ERR "%s: failed to allocate char dev region ",  
    20.             __FILE__);  
    21. }  

    在class.c文件函数rtc_init中生成rtc类,然后调用rtc-dev.c文件中的rtc_dev_init分配设备号。
    在rtc-dev.c中声明了file_operations,因为rtc也是一个字符设备:

    C-sharp代码 
    1. static const struct file_operations rtc_dev_fops = {  
    2.     .owner      = THIS_MODULE,  
    3.     .llseek     = no_llseek,  
    4.     .read       = rtc_dev_read,  
    5.     .poll       = rtc_dev_poll,  
    6.     .unlocked_ioctl = rtc_dev_ioctl,  
    7.     .open       = rtc_dev_open,  
    8.     .release    = rtc_dev_release,  
    9.     .fasync     = rtc_dev_fasync,  
    10. };  

    下面来分析rtc-s3c.c源码:
    首先看模块的注册和撤销:

    C-sharp代码 
    1. static int __init s3c_rtc_init(void)  
    2. {  
    3.     printk(banner);  
    4.     return platform_driver_register(&s3c2410_rtc_driver);  
    5. }  
    6. static void __exit s3c_rtc_exit(void)  
    7. {  
    8.     platform_driver_unregister(&s3c2410_rtc_driver);  
    9. }  

    从上边的代码可以看出rtc driver作为platform_driver注册进内核,挂在platform_bus上。

    C-sharp代码 
    1. static struct platform_driver s3c2410_rtc_driver = {  
    2.     .probe      = s3c_rtc_probe,                //rtc探测函数  
    3.     .remove     = __devexit_p(s3c_rtc_remove),  //rtc移除函数  
    4.     .suspend    = s3c_rtc_suspend,              //rtc挂起函数  
    5.     .resume     = s3c_rtc_resume,               //rtc恢复函数  
    6.     .driver     = {  
    7.         .name   = "s3c2410-rtc",                //注意这里的名字一定要和系统中定义平台设备的地方一致,这样才能把平台设备和平台驱动关联起来  
    8.         .owner  = THIS_MODULE,  
    9.     },  
    10. };  

    在arch/arm/plat-s3c24xx/devs.c中定义了rtc的platform_device:

    C-sharp代码 
    1. /* RTC */  
    2. static struct resource s3c_rtc_resource[] = {            //定义了rtc平台设备会使用的资源  
    3.     [0] = {                                          //IO端口资源范围  
    4.         .start = S3C24XX_PA_RTC,  
    5.         .end   = S3C24XX_PA_RTC + 0xff,  
    6.         .flags = IORESOURCE_MEM,  
    7.     },  
    8.     [1] = {                                          //RTC报警中断资源  
    9.         .start = IRQ_RTC,  
    10.         .end   = IRQ_RTC,  
    11.         .flags = IORESOURCE_IRQ,  
    12.     },  
    13.     [2] = {                                          //TICK节拍时间中断资源  
    14.         .start = IRQ_TICK,  
    15.         .end   = IRQ_TICK,  
    16.         .flags = IORESOURCE_IRQ  
    17.     }  
    18. };  
    19. struct platform_device s3c_device_rtc = {                //定义了平台设备  
    20.     .name         = "s3c2410-rtc",               //设备名  
    21.     .id       = -1,  
    22.     .num_resources    = ARRAY_SIZE(s3c_rtc_resource),   //资源数量  
    23.     .resource     = s3c_rtc_resource,               //引用上面定义的资源  
    24. };  

    平台驱动中定义了probe函数,下面来看他的实现:

    C-sharp代码 
    1. static int __devinit s3c_rtc_probe(struct platform_device *pdev)  
    2. {  
    3.     struct rtc_device *rtc;  
    4.     struct resource *res;  
    5.     int ret;  
    6.     pr_debug("%s: probe=%p ", __func__, pdev);  
    7.     /* find the IRQs */  
    8.     /*获得IRQ资源中的第二个,即TICK节拍时间中断号*/  
    9.     s3c_rtc_tickno = platform_get_irq(pdev, 1);  
    10.     if (s3c_rtc_tickno < 0) {  
    11.         dev_err(&pdev->dev, "no irq for rtc tick ");  
    12.         return -ENOENT;  
    13.     }  
    14.     /*获取IRQ资源中的第一个,即RTC报警中断*/  
    15.     s3c_rtc_alarmno = platform_get_irq(pdev, 0);  
    16.     if (s3c_rtc_alarmno < 0) {  
    17.         dev_err(&pdev->dev, "no irq for alarm ");  
    18.         return -ENOENT;  
    19.     }  
    20.     pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d ",  
    21.          s3c_rtc_tickno, s3c_rtc_alarmno);  
    22.     /* get the memory region */  
    23.     /*获取RTC平台设备所使用的IO端口资源*/  
    24.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
    25.     if (res == NULL) {  
    26.         dev_err(&pdev->dev, "failed to get memory region resource ");  
    27.         return -ENOENT;  
    28.     }  
    29.     /*申请IO端口资源所占用的IO空间*/  
    30.     s3c_rtc_mem = request_mem_region(res->start,  
    31.                      res->end-res->start+1,  
    32.                      pdev->name);  
    33.     if (s3c_rtc_mem == NULL) {  
    34.         dev_err(&pdev->dev, "failed to reserve memory region ");  
    35.         ret = -ENOENT;  
    36.         goto err_nores;  
    37.     }  
    38.     /*将IO端口占用的IO空间映射到虚拟地址,s3c_rtc_base是这段虚拟地址的起始地址*/  
    39.     s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);  
    40.     if (s3c_rtc_base == NULL) {  
    41.         dev_err(&pdev->dev, "failed ioremap() ");  
    42.         ret = -EINVAL;  
    43.         goto err_nomap;  
    44.     }  
    45.     /* check to see if everything is setup correctly */  
    46.     /*对RTCCON第0位进行操作,使能RTC*/  
    47.     s3c_rtc_enable(pdev, 1);  
    48.     pr_debug("s3c2410_rtc: RTCCON=%02x ",  
    49.          readb(s3c_rtc_base + S3C2410_RTCCON));  
    50.     /*对TICNT第7位进行操作,使能节拍时间计数寄存器*/  
    51.     s3c_rtc_setfreq(&pdev->dev, 1);  
    52.     /*让电源管理支持唤醒功能*/  
    53.     device_init_wakeup(&pdev->dev, 1);  
    54.     /* register RTC and exit */  
    55.     /*注册rtc设备,名为"s3c",与s3c_rtcops这个rtc_class_ops进行关联*/  
    56.     rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,  
    57.                   THIS_MODULE);  
    58.     if (IS_ERR(rtc)) {  
    59.         dev_err(&pdev->dev, "cannot attach rtc ");  
    60.         ret = PTR_ERR(rtc);  
    61.         goto err_nortc;  
    62.     }  
    63.     /**/  
    64.     rtc->max_user_freq = 128;  
    65.     /*将rtc这个rtc_device存放在&pdev->dev->driver_data*/  
    66.     platform_set_drvdata(pdev, rtc);  
    67.     return 0;  
    68.  err_nortc:  
    69.     s3c_rtc_enable(pdev, 0);  
    70.     iounmap(s3c_rtc_base);  
    71.  err_nomap:  
    72.     release_resource(s3c_rtc_mem);  
    73.  err_nores:  
    74.     return ret;  
    75. }  

    函数rtc_device_register在文件class.c中实现:

    C-sharp代码 
    1. struct rtc_device *rtc_device_register(const char *name, struct device *dev,  
    2.                     const struct rtc_class_ops *ops,  
    3.                     struct module *owner)  
    4. {  
    5.     struct rtc_device *rtc;  
    6.     int id, err;  
    7.     /*为idr(rtc_idr)分配内存*/  
    8.     if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {  
    9.         err = -ENOMEM;  
    10.         goto exit;  
    11.     }  
    12.     mutex_lock(&idr_lock);  
    13.     /*分配ID号存于id中,该ID号最终将作为该RTC设备的次设备号*/  
    14.     err = idr_get_new(&rtc_idr, NULL, &id);  
    15.     mutex_unlock(&idr_lock);  
    16.     if (err < 0)  
    17.         goto exit;  
    18.     id = id & MAX_ID_MASK;  
    19.     /*为RTC结构分配内存*/  
    20.     rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);  
    21.     if (rtc == NULL) {  
    22.         err = -ENOMEM;  
    23.         goto exit_idr;  
    24.     }  
    25.     rtc->id = id;  
    26.     /*指向原始操作函数集*/  
    27.     rtc->ops = ops;    
    28.     rtc->owner = owner;  
    29.     rtc->max_user_freq = 64;  
    30.     rtc->dev.parent = dev;  
    31.     rtc->dev.class = rtc_class;  
    32.     rtc->dev.release = rtc_device_release;  
    33.     mutex_init(&rtc->ops_lock);  
    34.     spin_lock_init(&rtc->irq_lock);  
    35.     spin_lock_init(&rtc->irq_task_lock);  
    36.     init_waitqueue_head(&rtc->irq_queue);  
    37.     strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);  
    38.     dev_set_name(&rtc->dev, "rtc%d", id);  
    39.     /*rtc->dev.devt = MKDEV(MAJOR(rtc_devt),rtc->id); cdev_init(&rtc->char_dev,&rtc_dev_fops);其中rtc_devt是从调用alloc_chrdev_region时获得的*/  
    40.     rtc_dev_prepare(rtc);  
    41.     /*注册该RTC设备rtc->dev*/  
    42.     err = device_register(&rtc->dev);  
    43.     if (err)  
    44.         goto exit_kfree;  
    45.     /*cdev_add(&rtc->chr_dev,rtc->dev.devt,1);将rtc->chrdev注册到系统中*/  
    46.     rtc_dev_add_device(rtc);  
    47.     /*在/sys下添加属性文件*/  
    48.     rtc_sysfs_add_device(rtc);  
    49.     /*在/proc中创建入口项"driver/rtc"*/  
    50.     rtc_proc_add_device(rtc);  
    51.     dev_info(dev, "rtc core: registered %s as %s ",  
    52.             rtc->name, dev_name(&rtc->dev));  
    53.     return rtc;  
    54. exit_kfree:  
    55.     kfree(rtc);  
    56. exit_idr:  
    57.     mutex_lock(&idr_lock);  
    58.     idr_remove(&rtc_idr, id);  
    59.     mutex_unlock(&idr_lock);  
    60. exit:  
    61.     dev_err(dev, "rtc core: unable to register %s, err = %d ",  
    62.             name, err);  
    63.     return ERR_PTR(err);  
    64. }  

    下边是s3c_rtc_enable函数的实现:

    C-sharp代码 
    1. static void s3c_rtc_enable(struct platform_device *pdev, int en)  
    2. {  
    3.     void __iomem *base = s3c_rtc_base;  
    4.     unsigned int tmp;  
    5.     if (s3c_rtc_base == NULL)  
    6.         return;  
    7.     /*如果禁止,就disable RTCCON与TICNT*/  
    8.     if (!en) {  
    9.         tmp = readb(base + S3C2410_RTCCON);  
    10.         writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);  
    11.         tmp = readb(base + S3C2410_TICNT);  
    12.         writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);  
    13.     } else {  
    14.         /* re-enable the device, and check it is ok */  
    15.         /*如果RTCCON没有使能,则使能之*/  
    16.         if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){  
    17.             dev_info(&pdev->dev, "rtc disabled, re-enabling ");  
    18.       
    19.             tmp = readb(base + S3C2410_RTCCON);  
    20.             writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);  
    21.         }  
    22.         /*如果BCD的计数选择位为1,则置位0,即Merge BCD counts*/  
    23.         if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){  
    24.             dev_info(&pdev->dev, "removing RTCCON_CNTSEL ");  
    25.             tmp = readb(base + S3C2410_RTCCON);  
    26.             writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);  
    27.         }  
    28.         /*如果BCD的时钟选择为1,则置位0,即XTAL 1/215 divided clock*/  
    29.         if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){  
    30.             dev_info(&pdev->dev, "removing RTCCON_CLKRST ");  
    31.             tmp = readb(base + S3C2410_RTCCON);  
    32.             writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);  
    33.         }  
    34.     }  
    35. }  
    36. static int __devexit s3c_rtc_remove(struct platform_device *dev)  
    37. {  
    38.     /*从系统平台设备中获取RTC设备类的数据*/  
    39.     struct rtc_device *rtc = platform_get_drvdata(dev);  
    40.      /*清空平台设备中RTC驱动数据*/  
    41.     platform_set_drvdata(dev, NULL);  
    42.     /*注销RTC设备类*/  
    43.     rtc_device_unregister(rtc);  
    44.     /*禁止RTC节拍时间计数寄存器TICNT的使能功能*/  
    45.     s3c_rtc_setpie(&dev->dev, 0);  
    46.     /*禁止RTC报警控制寄存器RTCALM的全局报警使能功能*/  
    47.     s3c_rtc_setaie(0);  
    48.     /*释放RTC虚拟地址映射空间*/  
    49.     iounmap(s3c_rtc_base);  
    50.     /*释放获取的RTC平台设备的资源*/  
    51.     release_resource(s3c_rtc_mem);  
    52.     /*销毁保存RTC平台设备的资源内存空间*/  
    53.     kfree(s3c_rtc_mem);  
    54.     return 0;  
    55. }  

    这里是电源管理部分,在挂起时保存TICNT的值,并禁止RTCCON,TICNT;在休眠的时候开启RTCCON,并恢复TICNT的值。

    C-sharp代码 
    1. #ifdef CONFIG_PM  
    2. /* RTC Power management control */  
    3. static int ticnt_save;  
    4. static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)  
    5. {  
    6.     /* save TICNT for anyone using periodic interrupts */  
    7.     ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);  
    8.     s3c_rtc_enable(pdev, 0);  
    9.     return 0;  
    10. }  
    11. static int s3c_rtc_resume(struct platform_device *pdev)  
    12. {  
    13.     s3c_rtc_enable(pdev, 1);  
    14.     writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);  
    15.     return 0;  
    16. }  
    17. #else  
    18. #define s3c_rtc_suspend NULL  
    19. #define s3c_rtc_resume  NULL  
    20. #endif  

    s3c_rtcops是RTC设备在RTC核心部分注册的对RTC设备进行操作的结构体,类似字符设备在驱动中的file_operations对字符设备进行操作的意思。

    C-sharp代码 
    1. static const struct rtc_class_ops s3c_rtcops = {  
    2.     .open       = s3c_rtc_open,  
    3.     .release    = s3c_rtc_release,  
    4.     .read_time  = s3c_rtc_gettime,  
    5.     .set_time   = s3c_rtc_settime,  
    6.     .read_alarm = s3c_rtc_getalarm,  
    7.     .set_alarm  = s3c_rtc_setalarm,  
    8.     .irq_set_freq   = s3c_rtc_setfreq,  
    9.     .irq_set_state  = s3c_rtc_setpie,  
    10.     .proc           = s3c_rtc_proc,  
    11. };  

    这两个是下边会用到的中断处理函数,产生一个时钟中断的时候就更新一下rtc_irq_data的值,也就是说只有当产生一个时钟中断(也就是一个滴答tick)才返回给用户一个时间。

    C-sharp代码 
    1. static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)  
    2. {  
    3.     struct rtc_device *rdev = id;  
    4.     rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);  
    5.     return IRQ_HANDLED;  
    6. }  
    7. static irqreturn_t s3c_rtc_tickirq(int irq, void *id)  
    8. {  
    9.     struct rtc_device *rdev = id;  
    10.     rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);  
    11.     return IRQ_HANDLED;  
    12. }  

    首先来看打开和关闭函数:

    C-sharp代码 
    1. static int s3c_rtc_open(struct device *dev)  
    2. {  
    3.     /*获得平台设备,从平台设备pdev->dev->driver_data获取rtc_device*/  
    4.     struct platform_device *pdev = to_platform_device(dev);  
    5.     struct rtc_device *rtc_dev = platform_get_drvdata(pdev);  
    6.     int ret;  
    7.     /*注册RTC报警中断的中断处理函数*/  
    8.     ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,  
    9.               IRQF_DISABLED,  "s3c2410-rtc alarm", rtc_dev);  
    10.     if (ret) {  
    11.         dev_err(dev, "IRQ%d error %d ", s3c_rtc_alarmno, ret);  
    12.         return ret;  
    13.     }  
    14.     /*注册TICK节拍时间中断的中断处理函数*/  
    15.     ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,  
    16.               IRQF_DISABLED,  "s3c2410-rtc tick", rtc_dev);  
    17.     if (ret) {  
    18.         dev_err(dev, "IRQ%d error %d ", s3c_rtc_tickno, ret);  
    19.         goto tick_err;  
    20.     }  
    21.     return ret;  
    22.  tick_err:  
    23.     free_irq(s3c_rtc_alarmno, rtc_dev);  
    24.     return ret;  
    25. }  

    RTC设备类关闭接口函数:

    C-sharp代码 
    1. static void s3c_rtc_release(struct device *dev)  
    2. {     
    3.     struct platform_device *pdev = to_platform_device(dev);  
    4.     struct rtc_device *rtc_dev = platform_get_drvdata(pdev);  
    5.     /* do not clear AIE here, it may be needed for wake */  
    6.     s3c_rtc_setpie(dev, 0);  
    7.     free_irq(s3c_rtc_alarmno, rtc_dev);  
    8.     free_irq(s3c_rtc_tickno, rtc_dev);  
    9. }  

    更新RTCALM寄存器的状态,是否使能:

    C-sharp代码 
    1. static void s3c_rtc_setaie(int to)  
    2. {  
    3.     unsigned int tmp;  
    4.     pr_debug("%s: aie=%d ", __func__, to);  
    5.     tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;  
    6.     if (to)  
    7.         tmp |= S3C2410_RTCALM_ALMEN;  
    8.     writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);  
    9. }  

    更新TICNT寄存器的状态,是否使能:

    C-sharp代码 
    1. static int s3c_rtc_setpie(struct device *dev, int enabled)  
    2. {  
    3.     unsigned int tmp;  
    4.     pr_debug("%s: pie=%d ", __func__, enabled);  
    5.     spin_lock_irq(&s3c_rtc_pie_lock);  
    6.     tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;  
    7.     if (enabled)  
    8.         tmp |= S3C2410_TICNT_ENABLE;  
    9.     writeb(tmp, s3c_rtc_base + S3C2410_TICNT);  
    10.     spin_unlock_irq(&s3c_rtc_pie_lock);  
    11.     return 0;  
    12. }  

    更新TICNT节拍时间计数的值:

    C-sharp代码 
    1. static int s3c_rtc_setfreq(struct device *dev, int freq)  
    2. {  
    3.     unsigned int tmp;  
    4.     if (!is_power_of_2(freq))  
    5.         return -EINVAL;  
    6.     spin_lock_irq(&s3c_rtc_pie_lock);  
    7.     tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;  
    8.     tmp |= (128 / freq)-1;  
    9.     writeb(tmp, s3c_rtc_base + S3C2410_TICNT);  
    10.     spin_unlock_irq(&s3c_rtc_pie_lock);  
    11.     return 0;  
    12. }  
    13. /* Time read/write */  
    14. static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)  
    15. {  
    16.     unsigned int have_retried = 0;  
    17.     /*获得rtc IO端口寄存器的虚拟地址的起始地址*/  
    18.     void __iomem *base = s3c_rtc_base;  
    19.  retry_get_time:  
    20.     /*读取RTC中BCD数中的:分、时、日期、月、年、秒,放到rtc_time rtc_tm中*/  
    21.     rtc_tm->tm_min  = readb(base + S3C2410_RTCMIN);  
    22.     rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);  
    23.     rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);  
    24.     rtc_tm->tm_mon  = readb(base + S3C2410_RTCMON);  
    25.     rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);  
    26.     rtc_tm->tm_sec  = readb(base + S3C2410_RTCSEC);  
    27.     /* the only way to work out wether the system was mid-update 
    28.      * when we read it is to check the second counter, and if it 
    29.      * is zero, then we re-try the entire read 
    30.      */  
    31.     /*如果到达0秒就检查一下,因为年月日时分可能会有加1操作,比如此时是一年的最后天的最后一分一秒,则年月日时分秒都会改变*/  
    32.     if (rtc_tm->tm_sec == 0 && !have_retried) {  
    33.         have_retried = 1;  
    34.         goto retry_get_time;  
    35.     }  
    36.     pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x ",  
    37.          rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,  
    38.          rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);  
    39.     /*使用readb读取寄存器的值得到的是bcd格式,必须转换成bin格式再保存*/  
    40.     rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);  
    41.     rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);  
    42.     rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);  
    43.     rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);  
    44.     rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);  
    45.     rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);  
    46.     rtc_tm->tm_year += 100;  
    47.     rtc_tm->tm_mon -= 1;  
    48.     return 0;  
    49. }  
    50. static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)  
    51. {  
    52.     void __iomem *base = s3c_rtc_base;  
    53.     int year = tm->tm_year - 100;  
    54.     pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d ",  
    55.          tm->tm_year, tm->tm_mon, tm->tm_mday,  
    56.          tm->tm_hour, tm->tm_min, tm->tm_sec);  
    57.     /* we get around y2k by simply not supporting it */  
    58.     /*RTC时钟的范围是00~99,由BCDYEAR寄存器的0~7位存储*/  
    59.     if (year < 0 || year >= 100) {  
    60.         dev_err(dev, "rtc only supports 100 years ");  
    61.         return -EINVAL;  
    62.     }  
    63.     /*将上面保存到RTC核心定义的时间结构体中的时间日期值写入对应的寄存器中*/  
    64.     writeb(bin2bcd(tm->tm_sec),  base + S3C2410_RTCSEC);  
    65.     writeb(bin2bcd(tm->tm_min),  base + S3C2410_RTCMIN);  
    66.     writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);  
    67.     writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);  
    68.     writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);  
    69.     writeb(bin2bcd(year), base + S3C2410_RTCYEAR);  
    70.     return 0;  
    71. }  

    获取报警时间的值:

    C-sharp代码 
    1. static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)  
    2. {  
    3.     struct rtc_time *alm_tm = &alrm->time;  
    4.     void __iomem *base = s3c_rtc_base;  
    5.     unsigned int alm_en;  
    6.     /*从RTC的报警寄存器中读取*/  
    7.     alm_tm->tm_sec  = readb(base + S3C2410_ALMSEC);  
    8.     alm_tm->tm_min  = readb(base + S3C2410_ALMMIN);  
    9.     alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);  
    10.     alm_tm->tm_mon  = readb(base + S3C2410_ALMMON);  
    11.     alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);  
    12.     alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);  
    13.       
    14.     alm_en = readb(base + S3C2410_RTCALM);  
    15.     /*根据RTCALM寄存器的报警全局使能位来设置报警状态结构rtc_wkalrm*/  
    16.     alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;  
    17.     pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x ",  
    18.          alm_en,  
    19.          alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,  
    20.          alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);  
    21.   
    22.     /* decode the alarm enable field */  
    23.     /*如果RTCALM寄存器的秒使能,则将rtc_wkalrm中存放的秒数据由BCD格式转换为BIN格式,否则设置为0xff*/  
    24.     if (alm_en & S3C2410_RTCALM_SECEN)  
    25.         alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);  
    26.     else  
    27.         alm_tm->tm_sec = 0xff;  
    28.     /*如果RTCALM寄存器的分钟使能,则将rtc_wkalrm中存放的分钟数据由BCD格式转换为BIN格式,否则设置为0xff*/  
    29.     if (alm_en & S3C2410_RTCALM_MINEN)  
    30.         alm_tm->tm_min = bcd2bin(alm_tm->tm_min);  
    31.     else  
    32.         alm_tm->tm_min = 0xff;  
    33.     /*如果RTCALM寄存器的小时使能,则将rtc_wkalrm中存放的小时数据由BCD格式转换为BIN格式,否则设置为0xff*/  
    34.     if (alm_en & S3C2410_RTCALM_HOUREN)  
    35.         alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);  
    36.     else  
    37.         alm_tm->tm_hour = 0xff;  
    38.     /*如果RTCALM寄存器的日使能,则将rtc_wkalrm中存放的日数据由BCD格式转换为BIN格式,否则设置为0xff*/  
    39.     if (alm_en & S3C2410_RTCALM_DAYEN)  
    40.         alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);  
    41.     else  
    42.         alm_tm->tm_mday = 0xff;  
    43.     /*如果RTCALM寄存器的月使能,则将rtc_wkalrm中存放的月数据由BCD格式转换为BIN格式,否则设置为0xff*/  
    44.     if (alm_en & S3C2410_RTCALM_MONEN) {  
    45.         alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);  
    46.         alm_tm->tm_mon -= 1;  
    47.     } else {  
    48.         alm_tm->tm_mon = 0xff;  
    49.     }  
    50.     /*如果RTCALM寄存器的年使能,则将rtc_wkalrm中存放的年数据由BCD格式转换为BIN格式,否则设置为0xff*/  
    51.     if (alm_en & S3C2410_RTCALM_YEAREN)  
    52.         alm_tm->tm_year = bcd2bin(alm_tm->tm_year);  
    53.     else  
    54.         alm_tm->tm_year = 0xffff;  
    55.     return 0;  
    56. }  

    设置报警时间的值:

    C-sharp代码 
    1. static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)  
    2. {  
    3.     struct rtc_time *tm = &alrm->time;  
    4.     void __iomem *base = s3c_rtc_base;  
    5.     unsigned int alrm_en;  
    6.     pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x ",  
    7.          alrm->enabled,  
    8.          tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,  
    9.          tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);  
    10.     /*读取RTCALM寄存器的全局使能位,关闭所有报警使能*/  
    11.     alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;  
    12.     writeb(0x00, base + S3C2410_RTCALM);  
    13.     /*如果秒时间在合理范围内,则使能秒报警位,将报警状态寄存器中封装的time的秒位由BIN格式转换为BCD,写入秒报警寄存器中*/  
    14.     if (tm->tm_sec < 60 && tm->tm_sec >= 0) {  
    15.         alrm_en |= S3C2410_RTCALM_SECEN;  
    16.         writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);  
    17.     }  
    18.     /*如果分钟时间在合理范围内,则使能分钟报警位,将报警状态寄存器中封装的time的分钟位由BIN格式转换为BCD,写入分钟报警寄存器中*/  
    19.     if (tm->tm_min < 60 && tm->tm_min >= 0) {  
    20.         alrm_en |= S3C2410_RTCALM_MINEN;  
    21.         writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);  
    22.     }  
    23.     /*如果小时时间在合理范围内,则使能小时报警位,将报警状态寄存器中封装的time的小时位由BIN格式转换为BCD,写入小时报警寄存器中*/  
    24.     if (tm->tm_hour < 24 && tm->tm_hour >= 0) {  
    25.         alrm_en |= S3C2410_RTCALM_HOUREN;  
    26.         writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);  
    27.     }  
    28.     pr_debug("setting S3C2410_RTCALM to %08x ", alrm_en);  
    29.     /*使能RTCALM寄存器全局报警位*/  
    30.     writeb(alrm_en, base + S3C2410_RTCALM);  
    31.     /**/  
    32.     s3c_rtc_setaie(alrm->enabled);  
    33.     /*根据全局报警使能的状态来决定是唤醒RTC报警中断还是睡眠RTC报警中断*/  
    34.     if (alrm->enabled)  
    35.         enable_irq_wake(s3c_rtc_alarmno);  
    36.     else  
    37.         disable_irq_wake(s3c_rtc_alarmno);  
    38.     return 0;  
    39. }  

    下面来分析一下是怎样获取和设置时间的:
    通过用户空间的ioctl,在rtc-dev.c中实现了rtc_dev_ioctl,其中获取和设置时间如下:

    C-sharp代码 
    1. case RTC_RD_TIME:  
    2.         mutex_unlock(&rtc->ops_lock);  
    3.         err = rtc_read_time(rtc, &tm);  
    4.         if (err < 0)  
    5.             return err;  
    6.         if (copy_to_user(uarg, &tm, sizeof(tm)))  
    7.             err = -EFAULT;  
    8.         return err;  
    9.     case RTC_SET_TIME:  
    10.         mutex_unlock(&rtc->ops_lock);  
    11.         if (copy_from_user(&tm, uarg, sizeof(tm)))  
    12.             return -EFAULT;  
    13.         return rtc_set_time(rtc, &tm);  

    通过copy_to_user和copy_from_user实现时间在内核空间与用户空间的传递。这里调用到的rtc_read_time和rtc_set_time在interface.c中实现:

    C-sharp代码 
    1. int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)  
    2. {  
    3.     int err;  
    4.     err = mutex_lock_interruptible(&rtc->ops_lock);  
    5.     if (err)  
    6.         return err;  
    7.     if (!rtc->ops)  
    8.         err = -ENODEV;  
    9.     else if (!rtc->ops->read_time)  
    10.         err = -EINVAL;  
    11.     else {  
    12.         memset(tm, 0, sizeof(struct rtc_time));  
    13.         err = rtc->ops->read_time(rtc->dev.parent, tm);  
    14.     }  
    15.     mutex_unlock(&rtc->ops_lock);  
    16.     return err;  
    17. }  
    18. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)  
    19. {  
    20.     int err;  
    21.     err = rtc_valid_tm(tm);  
    22.     if (err != 0)  
    23.         return err;  
    24.     err = mutex_lock_interruptible(&rtc->ops_lock);  
    25.     if (err)  
    26.         return err;  
    27.     if (!rtc->ops)  
    28.         err = -ENODEV;  
    29.     else if (rtc->ops->set_time)  
    30.         err = rtc->ops->set_time(rtc->dev.parent, tm);  
    31.     else if (rtc->ops->set_mmss) {  
    32.         unsigned long secs;  
    33.         err = rtc_tm_to_time(tm, &secs);  
    34.         if (err == 0)  
    35.             err = rtc->ops->set_mmss(rtc->dev.parent, secs);  
    36.     } else  
    37.         err = -EINVAL;  
    38.     mutex_unlock(&rtc->ops_lock);  
    39.     return err;  
    40. }  

    可以看出他们调用了具体RTC设备驱动中的read_time和set_time函数,对应了s3c2410中的s3c_rtc_gettime和s3c_rtc_settime,这里使用的rtc_tm_to_time函数实现在rtclib.c中,/drivers/rtc/interface.c定义了可供其它模块访问的接口。

  • 相关阅读:
    Big Number
    Who will be punished
    find your present (2)
    Being a Good Boy in Spring Festival
    day4__列表的初识(列表的创建、增删改查、元组、range)
    day3、基础___(基础数字类型、字符串索引与切片、str常用操作方法)
    day2、基础__(while循环、格式化输出、运算符、初始编码)
    day1: 基础 __ (变量、常量、注释、数据类型、input、 if)
    十八、FTP配置
    十七、交换机配置管理IP和telnet登陆设置
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/5294484.html
Copyright © 2011-2022 走看看