分享

Linux时间系统之RTC时间

 科技强我 2018-06-08

一、概述

不知道有没有“时间系统”的说法,我们暂且把它作为Linux中和时间相关的内容的统称吧。
Linux时间有两个,系统时间(Wall Time),RTC时间
系统时间(WT):由Linux系统软件维持的时间,比如command date
  1. $ date  
  2. 2017年 02月 25日 星期六 16:58:10 CST  

获取到的就是系统时间。
RTC时间:这个时间来自我们设备上的RTC芯片,通过command hwclock 可以读取:
  1. # hwclock -r  --> root权限才可以运行  
  2. 2017年02月25日 星期六 17时01分57秒  -0.906462 seconds  

我们通过man查看hwclock的介绍:
  1. hwclock - query or set the hardware clock (RTC)  

接下来,通过代码看下两者的关系。

二、WT时间和RTC时间

WT时间来自于RTC时间,流程是:
  1. 上电-->RTC驱动加载-->从RTC同步时间到WT时间  

上代码:
  1. hctosys.c (drivers\rtc)  
  2. static int __init rtc_hctosys(void)  
  3. {  
  4.     struct timespec tv = {  
  5.         .tv_nsec = NSEC_PER_SEC >> 1,  
  6.     };  
  7.       
  8.     err = rtc_read_time(rtc, &tm);  
  9.     err = do_settimeofday(&tv);  
  10.     dev_info(rtc->dev.parent,  
  11.         "setting system clock to "  
  12.         "%d-%02d-%02d %02d:%02d:%02d UTC (%u)\n",  
  13.         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,  
  14.         tm.tm_hour, tm.tm_min, tm.tm_sec,  
  15.         (unsigned int) tv.tv_sec);  
  16. }  
  17. late_initcall(rtc_hctosys);  

late_initcall说明系统在启动流程的后面才会调用该函数去同步时间。关于late_initcall可见最后PS。
接下来从底层到上层进行梳理。

三、RTC时间框架

框架如图:

:图中hwclock.c笔误,实为hctosys.c

  1. Hardware:提供时间信息(time&alarm),通过一定的接口(比如I2C)和
  2. RTC Driver:进行交互。Driver完成硬件的访问功能,提供访问接口,以驱动的形式驻留在系统。注册方式由
  3. class.c:文件提供。驱动注册成功后会构建rtc_device结构体表征的rtc设备,并把rtc芯片的操作方式存放到rtc设备的ops成员中
  4. interface.c:文件屏蔽硬件相关的细节,向上提供统一的获取/设置时间或Alarm的接口
  5. rtc-lib.c:文件提供通用的时间操作函数,如rtc_time_to_tm、rtc_valid_tm等
  6. rtc-dev.c:文件在/dev/目录下创建设备节点供应用层访问,如open、read、ioctl等,访问方式填充到file_operations结构体中
  7. hctosys.c/rtc-sys.c/rtc-proc.c:看名字就知道其作用

接下来从驱动一层一层看下

四、RTC驱动

驱动主要工作是填充rtc_class_ops结构体,结构体描述了RTC芯片能够提供的所有操作方式:
  1. struct rtc_class_ops {  
  2.     int (*open)(struct device *);  
  3.     void (*release)(struct device *);  
  4.     int (*ioctl)(struct device *, unsigned int, unsigned long);  
  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 *);  
  10.     int (*set_mmss)(struct device *, unsigned long secs);  
  11.     int (*read_callback)(struct device *, int data);  
  12.     int (*alarm_irq_enable)(struct device *, unsigned int enabled);  
  13. };  


实现:
  1. static const struct rtc_class_ops test_rtc_ops = {  
  2.     .read_time    = test_rtc_read_time,  
  3.     .set_time    = test_rtc_set_time,  
  4.     .read_alarm    = test_rtc_read_alarm,  
  5.     .set_alarm    = test_rtc_set_alarm,  
  6.     .ioctl         = test_rtc_ioctl,  
  7.     .proc        = test_rtc_proc  
  8. };  

注册:
  1. rtc_device_register(name, dev, &test_rtc_ops, THIS_MODULE);  

成功的话log:

  1. [    1.531114] test_rtc_init Enter.  
  2. [    1.531126] bus: 'i2c': add driver test_rtc  
  3. [    1.531189] test_rtc_probe Enter.  
  4. [    1.533990] test_rtc_read_time Enter.  
  5. [    1.534527] test_rtc_read_time Exit.  
  6. [    1.534537] test_rtc_read_alarm Enter.  
  7. [    1.534546] test_rtc_read_alarm Exit.  
  8. [    1.534556] test_rtc_read_time Enter.  
  9. [    1.535083] test_rtc_read_time Exit.  
  10. [    1.535237] test_rtc 2-0051: rtc core: registered test_rtc as rtc0  
  11. [    1.535250] test_rtc_probe Exit.  


五、class.c和RTC驱动注册

class.c文件在RTC驱动注册之前开始得到运行:
  1. static int __init rtc_init(void)  
  2. {  
  3.     rtc_class = class_create(THIS_MODULE, "rtc");  
  4.     rtc_class->suspend = rtc_suspend;  
  5.     rtc_class->resume = rtc_resume;  
  6.     rtc_dev_init();  
  7.     rtc_sysfs_init(rtc_class);  
  8.     return 0;  
  9. }  
  10. subsys_initcall(rtc_init);  

完成:
  • 1、创建名为rtc的class
  • 2、提供PM相关接口suspend/resume
  • 3、rtc_dev_init():动态申请/dev/rtcN的设备号
  • 4、rtc_sysfs_init():rtc类具有的device_attribute属性

接着看RTC驱动注册:
  1. struct rtc_device *rtc_device_register(const charchar *name, struct device *dev,  
  2.                     const struct rtc_class_ops *ops,  
  3.                     struct module *owner)  
  4. {  
  5.     struct rtc_device *rtc;  
  6.     struct rtc_wkalrm alrm;  
  7.     int id, err;  
  8.       
  9.     // 1、Linux支持多个RTC设备,所以需要为每一个设备分配一个ID  
  10.     // 对应与/dev/rtc0,/dev/rtc1,,,/dev/rtcN  
  11.     id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);  
  12.   
  13.     // 2、创建rtc_device设备(对象)并执行初始化  
  14.     rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);  
  15.     rtc->id = id;  
  16.     rtc->ops = ops; // 2.1 对应RTC驱动填充的test_rtc_ops  
  17.     rtc->owner = owner;  
  18.     rtc->irq_freq = 1;  
  19.     rtc->max_user_freq = 64;  
  20.     rtc->dev.parent = dev;  
  21.     rtc->dev.class = rtc_class;// 2.2 rtc_init()创建的rtc_class  
  22.     rtc->dev.release = rtc_device_release;  
  23.   
  24.     // 2.3 rtc设备中相关锁、等待队列的初始化  
  25.     mutex_init(&rtc->ops_lock);  
  26.     spin_lock_init(&rtc->irq_lock);  
  27.     spin_lock_init(&rtc->irq_task_lock);  
  28.     init_waitqueue_head(&rtc->irq_queue);  
  29.   
  30.     // 2.4 Init timerqueue:我们都知道,手机等都是可以设置多个闹钟的  
  31.     timerqueue_init_head(&rtc->timerqueue);  
  32.     INIT_WORK(&rtc->irqwork, rtc_timer_do_work);  
  33.     // 2.5 Init aie timer:alarm interrupt enable,RTC闹钟中断  
  34.     rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (voidvoid *)rtc);  
  35.     // 2.6 Init uie timer:update interrupt,RTC更新中断  
  36.     rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (voidvoid *)rtc);  
  37.     /* Init pie timer:periodic interrupt,RTC周期性中断 */  
  38.     hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);  
  39.     rtc->pie_timer.function = rtc_pie_update_irq;  
  40.     rtc->pie_enabled = 0;  
  41.   
  42.     /* Check to see if there is an ALARM already set in hw */  
  43.     err = __rtc_read_alarm(rtc, &alrm);  
  44.   
  45.     // 3、如果RTC芯片中设置了有效的Alarm,则初始化:加入到rtc->timerqueue队列中  
  46.     if (!err && !rtc_valid_tm(&alrm.time))  
  47.         rtc_initialize_alarm(rtc, &alrm);  
  48.   
  49.     // 4、根据name参数设置rtc的name域  
  50.     strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);  
  51.     // 5、设置rtc的dev成员中的name域  
  52.     dev_set_name(&rtc->dev, "rtc%d", id);  
  53.   
  54.     // 6、/dev/rtc0的rtc作为字符设备进行初始化  
  55.     // rtc_dev_prepare-->cdev_init(&rtc->char_dev, &rtc_dev_fops);  
  56.     rtc_dev_prepare(rtc);  
  57.   
  58.     // 7、添加rtc设备到系统  
  59.     err = device_register(&rtc->dev);  
  60.   
  61.     // 8、rtc设备作为字符设备添加到系统  
  62.     // rtc_dev_add_devicec-->dev_add(&rtc->char_dev, rtc->dev.devt, 1)  
  63.     // 然后就存在/dev/rtc0了  
  64.     rtc_dev_add_device(rtc);  
  65.     rtc_sysfs_add_device(rtc);  
  66.     // 9、/proc/rtc  
  67.     rtc_proc_add_device(rtc);  
  68.   
  69.     dev_info(dev, "rtc core: registered %s as %s\n",  
  70.             rtc->name, dev_name(&rtc->dev));  
  71.   
  72.     return rtc;  
  73. }  

有了/dev/rtc0后,应用层就可以通过open/read/ioctl操作RTC设备了,对应与内核的file_operations:
  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访问为例,抽象的实现位于interface.c文件,其实现基于class.c中创建的rtc_device设备。
实现原理,以rtc_set_time为例:
  1. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)  
  2. {  
  3.     int err;  
  4.     // 1、参数检测  
  5.     err = rtc_valid_tm(tm);  
  6.   
  7.     err = mutex_lock_interruptible(&rtc->ops_lock);  
  8.     if (err)  
  9.         return err;  
  10.   
  11.     // 2、调用rtc_device中ops结构体的函数指针  
  12.     // ops结构体的函数指针已经在RTC驱动中被赋值  
  13.     if (!rtc->ops)  
  14.         err = -ENODEV;  
  15.     else if (rtc->ops->set_time)  
  16.         err = rtc->ops->set_time(rtc->dev.parent, tm);  
  17.     mutex_unlock(&rtc->ops_lock);  
  18.     /* A timer might have just expired */  
  19.     schedule_work(&rtc->irqwork);  
  20.     return err;  
  21. }  


七、rtc在文件系统中的呈现

1、rtc在sysfs

之前曾建立过名为rtc的class:

  1. rtc_class = class_create(THIS_MODULE, "rtc");  

查看之:

  1. # ls /sys/class/rtc/                                      
  2. rtc0  
  3. # ls -l /sys/class/rtc/                                   
  4. lrwxrwxrwx root     root              2014-01-02 16:51 rtc0 -> ../../devices/ff660000.i2c/i2c-2/2-0051/rtc/rtc0  

我们系统中只有一个RTC,所以编号为rtc0。
同时发现rtc0文件为指向/sys/devices/ff660000.i2c/i2c-2/2-0051/rtc/rtc0的符号链接,RTC芯片是I2C接口,所以rtc0挂载在I2C的总线上,总线控制器地址ff660000,控制器编号为2,RTC芯片作为slave端地址为0x51。
rtc0设备属性

  1. void __init rtc_sysfs_init(struct classclass *rtc_class)  
  2. {  
  3.     rtc_class->dev_attrs = rtc_attrs;  
  4. }  
  5. static struct device_attribute rtc_attrs[] = {  
  6.     __ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL),  
  7.     __ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL),  
  8.     __ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL),  
  9.     __ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL),  
  10.     __ATTR(max_user_freq, S_IRUGO | S_IWUSR, rtc_sysfs_show_max_user_freq,  
  11.             rtc_sysfs_set_max_user_freq),  
  12.     __ATTR(hctosys, S_IRUGO, rtc_sysfs_show_hctosys, NULL),  
  13.     { },  
  14. };  


查看之:

  1. ls -l /sys/class/rtc/rtc0/                              
  2. -r--r--r-- root     root         4096 2014-01-02 16:51 date  
  3. -r--r--r-- root     root         4096 2014-01-02 16:51 dev  
  4. lrwxrwxrwx root     root              2014-01-02 16:51 device -> ../../../2-0051  
  5. -r--r--r-- root     root         4096 2014-01-02 16:51 hctosys  
  6. -rw-r--r-- root     root         4096 2014-01-02 16:51 max_user_freq  
  7. -r--r--r-- root     root         4096 2014-01-02 16:51 name  
  8. drwxr-xr-x root     root              2014-01-02 16:48 power  
  9. -r--r--r-- root     root         4096 2014-01-02 16:51 since_epoch  
  10. lrwxrwxrwx root     root              2014-01-02 16:51 subsystem -> ../../../../../../class/rtc  
  11. -r--r--r-- root     root         4096 2014-01-02 16:51 time  
  12. -rw-r--r-- root     root         4096 2014-01-02 16:48 uevent  
  13. -rw-r--r-- root     root         4096 2014-01-02 16:51 wakealarm  

2、rtc在proc

之前曾rtc0设备加入到了/proc

  1. rtc_device_register  
  2. --->rtc_proc_add_device(rtc);  
  3.   
  4. void rtc_proc_add_device(struct rtc_device *rtc)  
  5. {  
  6.     if (is_rtc_hctosys(rtc))  
  7.         proc_create_data("driver/rtc", 0, NULL, &rtc_proc_fops, rtc);  
  8. }  

查看之:

  1. # cat /proc/driver/rtc                                         
  2. rtc_time    : 17:19:53  
  3. rtc_date    : 2014-01-02  
  4. alrm_time   : 00:00:00  
  5. alrm_date   : 1970-01-01  
  6. alarm_IRQ   : no  
  7. alrm_pending    : no  
  8. update IRQ enabled  : no  
  9. periodic IRQ enabled    : no  
  10. periodic IRQ frequency  : 1  
  11. max user IRQ frequency  : 64  
  12. 24hr        : yes  

信息来源:

  1. rtc_proc_fops  
  2.     -->rtc_proc_open  
  3.         -->rtc_proc_show  



PS
关于late_initcall

kernel中__init类型函数都位于.init.text段中,对应的在.initcall.init段中保存相应的函数指针。系统在启动过程中,根据定义在段中的等级值(0~7)从低到高依次执行。定义:

  1. init.h (include\linux)  
  2.   
  3. #define pure_initcall(fn)        __define_initcall(fn, 0)  
  4.   
  5. #define core_initcall(fn)        __define_initcall(fn, 1)  
  6. #define core_initcall_sync(fn)        __define_initcall(fn, 1s)  
  7. #define postcore_initcall(fn)        __define_initcall(fn, 2)  
  8. #define postcore_initcall_sync(fn)    __define_initcall(fn, 2s)  
  9. #define arch_initcall(fn)        __define_initcall(fn, 3)  
  10. #define arch_initcall_sync(fn)        __define_initcall(fn, 3s)  
  11. #define subsys_initcall(fn)        __define_initcall(fn, 4)  
  12. #define subsys_initcall_sync(fn)    __define_initcall(fn, 4s)  
  13. #define fs_initcall(fn)            __define_initcall(fn, 5)  
  14. #define fs_initcall_sync(fn)        __define_initcall(fn, 5s)  
  15. #define rootfs_initcall(fn)        __define_initcall(fn, rootfs)  
  16. #define device_initcall(fn)        __define_initcall(fn, 6)  
  17. #define device_initcall_sync(fn)    __define_initcall(fn, 6s)  
  18. #define late_initcall(fn)        __define_initcall(fn, 7)  
  19. #define late_initcall_sync(fn)        __define_initcall(fn, 7s)  

http://blog.csdn.net/u013686019/article/details/57126940

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多