RTC系统转
Posted sky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RTC系统转相关的知识,希望对你有一定的参考价值。
转自:http://www.cnblogs.com/muhuacat/p/5276306.html
一. RTC及驱动简介
RTC即real time clock实时时钟,主要用于为操作系统提供可靠的时间;当系统处于断电 的情况下,RTC记录操作系统时间,并可在电池供电情况下继续正常工作,当系统正常启动后,系统可从RTC读取时间信息,来确保断电后时间运行连续性。
目前,很多CPU中都已集成RTC系统,且有许多独立的外接RTC芯片可用于实现RTC功能;
在内核中RTC驱动可分为两层,一层为于硬件无关的抽象层,主要用于管理RTC设备、设备节点,属性节点注册及操作;另一层为与硬件相关的RTC低层驱动层;抽象层程序在/drivers/rtc目录下,主要涉及下面几个文件:
class.c 用于管理和注册RTC设备结构、sysfs、procfs及RTC类;
rtc-dev.c 用于注册和管理RTC设备节点,为用户空间提供devfs操作接口,主要操作有rtc_read,rtc_ioctl;
rtc-proc.c 用于管理rtc的procfs属性节点,提供一些中断状态、标志查询;
rtc-sysfs.c 用于管理rtc设备的sysfs属性,如获取RTC设备名字、日期、时间等属性信息;
interface.c 为rtc-dev.c 和RTC低层驱动提供操作接口;
RTC系统结构示意图如图1所示;
图1 RTC系统结构图
如图1所示,RTC 设备驱动通过rtc_device_register接口向linux系统注册RTC设备,并成生proc、sys目录下的属性文件;当用户通过/dev/rtcx设备节点或proc、sysfs接口来操作RTC设备时,都需要先通过interface接口才能访问真实的设备驱动。
二. 部分数据结构分析
1. RTC设备结构
struct rtc_device
{
struct device dev;
struct module *owner;
int id; 设备编号
char name[RTC_DEVICE_NAME_SIZE];
const struct rtc_class_ops *ops; rtc设备低层操作接口;
struct mutex ops_lock;
struct cdev char_dev; RTC字符型设备结构;
unsigned long flags;
unsigned long irq_data; 中断数据;
spinlock_t irq_lock;
wait_queue_head_t irq_queue; 中断数据等待队列;
struct fasync_struct *async_queue;
struct rtc_task *irq_task;
spinlock_t irq_task_lock;
int irq_freq; 中断频率;
int max_user_freq;
struct timerqueue_head timerqueue;
struct rtc_timer aie_timer; 报警中断定时器;
struct rtc_timer uie_rtctimer; 更新中断定时器;
struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */ 周期中断高精度定时器;
int pie_enabled; 周期中断使能标志;
struct work_struct irqwork;
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 内部仿真update interrupt时所需;
struct work_struct uie_task;
struct timer_list uie_timer;
/* Those fields are protected by rtc->irq_lock */
unsigned int oldsecs;
unsigned int uie_irq_active:1;
unsigned int stop_uie_polling:1;
unsigned int uie_task_active:1;
unsigned int uie_timer_active:1;
#endif
};
2. RTC设备低层操作接口
struct rtc_class_ops {
int (*open)(struct device *); 打开设备
void (*release)(struct device *); 释放设备
int (*ioctl)(struct device *, unsigned int, unsigned long);
int (*read_time)(struct device *, struct rtc_time *); 读取RTC时间;
int (*set_time)(struct device *, struct rtc_time *); 设置RTC时间;
int (*read_alarm)(struct device *, struct rtc_wkalrm *); 读取RTC报警时间;
int (*set_alarm)(struct device *, struct rtc_wkalrm *); 设置RTC报警时间;
int (*proc)(struct device *, struct seq_file *); 用于提供procfs查询rtc状态接口;
int (*set_mmss)(struct device *, unsigned long secs); 设置以S为单位RTC时间接口;
int (*read_callback)(struct device *, int data);
int (*alarm_irq_enable)(struct device *, unsigned int enabled); 中断使能接口;
};
三.RTC低层驱动实现
1. 设备驱动注册及卸载
.probe = s3c_rtc_probe,.remove = s3c_rtc_remove,.suspend = s3c_rtc_suspend,.resume = s3c_rtc_resume,.id_table = s3c_rtc_driver_ids, 用于匹配平台设备.driver = {
.name = "s3c-rtc",.owner = THIS_MODULE,
},
return platform_driver_register(&s3c_rtc_driver)
platform_drvier_unregister(&s3c_rtc_driver)
[0] = {
.start = S3C_PA_RTC, RTC寄存器起始地址空间;.end = S3C_PA_RTC + 0xff,.flags = IORESOURCE_MEM, resource为内存类型
},[1] = {
.start = IRQ_RTC_ALARM, RTC报警中断号.end = IRQ_RTC_ALARM,.flags = IORESOURCE_IRQ, resource为中断类型
},[2] = {
.start = IRQ_RTC_TIC, RTC TICK中断.end = IRQ_RTC_TIC,.flags = IORESOURCE_IRQ
}
struct platform_device s3c_device_rtc = {
.name = "s3c64xx-rtc",.id = -1,.num_resources = ARRAY_SIZE(s3c_rtc_resource),.resource = s3c_rtc_resource,
{
struct rtc_device *rtc;struct rtc_time rtc_tm;struct resource *res;int ret;pr_debug("%s: probe=%p\\n", __func__, pdev);/* 从平台设备中获取相关的设备资源*/s3c_rtc_tickno = platform_get_irq(pdev, 1);if (s3c_rtc_tickno < 0) {
dev_err(&pdev->dev, "no irq for rtc tick\\n");return -ENOENT;
}s3c_rtc_alarmno = platform_get_irq(pdev, 0);if (s3c_rtc_alarmno < 0) {
dev_err(&pdev->dev, "no irq for alarm\\n");return -ENOENT;
}/*内核中,在对内存进行操作前得先申请内存空间,通过ioremap映射完后才能使用*/res = platform_get_resource(pdev, IORESOURCE_MEM, 0);if (res == NULL) {
dev_err(&pdev->dev, "failed to get memory region resource\\n");return -ENOENT;
}s3c_rtc_mem = request_mem_region(res->start, resource_size(res), pdev->name);if (s3c_rtc_mem == NULL) {
dev_err(&pdev->dev, "failed to reserve memory region\\n");ret = -ENOENT;goto err_nores;
}s3c_rtc_base = ioremap(res->start, resource_size(res));if (s3c_rtc_base == NULL) {
dev_err(&pdev->dev, "failed ioremap()\\n");ret = -EINVAL;goto err_nomap;
}/*获取并使能RTC 系统时钟信号*/rtc_clk = clk_get(&pdev->dev, "rtc");if (IS_ERR(rtc_clk)) {
dev_err(&pdev->dev, "failed to find rtc clock source\\n");ret = PTR_ERR(rtc_clk);rtc_clk = NULL;goto err_clk;
}clk_enable(rtc_clk);/* 配置S3C RTC 控制寄存器,使能RTCEN,只有当RTCEN有效时才能配置BCD等寄存器,当系统断电前得清除RTCEN,防止对BCD寄存器进行写操作; */s3c_rtc_enable(pdev, 1);device_init_wakeup(&pdev->dev, 1);/*注册RTC设备,s3c_rtcops为需要实现的低层操作接口*/rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,THIS_MODULE);if (IS_ERR(rtc)) {
dev_err(&pdev->dev, "cannot attach rtc\\n");ret = PTR_ERR(rtc);goto err_nortc;
}s3c_rtc_cpu_type = platform_get_device_id(pdev)->driver_data;/* Check RTC Time */s3c_rtc_gettime(NULL, &rtc_tm);if (rtc_valid_tm(&rtc_tm)) {
rtc_tm.tm_year = 100;rtc_tm.tm_mon = 0;rtc_tm.tm_mday = 1;rtc_tm.tm_hour = 0;rtc_tm.tm_min = 0;rtc_tm.tm_sec = 0;s3c_rtc_settime(NULL, &rtc_tm);dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\\n");
}if (s3c_rtc_cpu_type == TYPE_S3C64XX)
rtc->max_user_freq = 32768;
else
rtc->max_user_freq = 128;
platform_set_drvdata(pdev, rtc);/*设置时钟频率为1HZ*/s3c_rtc_setfreq(&pdev->dev, 1);clk_disable(rtc_clk);return 0;
err_nortc:
s3c_rtc_enable(pdev, 0);clk_disable(rtc_clk);clk_put(rtc_clk);
iounmap(s3c_rtc_base);
err_nomap:
release_resource(s3c_rtc_mem);
err_nores:
return ret;
.open = s3c_rtc_open,.release = s3c_rtc_release,.read_time = s3c_rtc_gettime,.set_time = s3c_rtc_settime,.read_alarm = s3c_rtc_getalarm,.set_alarm = s3c_rtc_setalarm,.proc = s3c_rtc_proc,.alarm_irq_enable = s3c_rtc_setaie,
{
struct platform_device *pdev = to_platform_device(dev);struct rtc_device *rtc_dev = platform_get_drvdata(pdev);int ret;ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);if (ret) {
dev_err(dev, "IRQ%d error %d\\n", s3c_rtc_alarmno, ret);return ret;
}ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq, IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);if (ret) {dev_err(dev, "IRQ%d error %d\\n", s3c_rtc_tickno, ret);goto tick_err;}return ret;
tick_err:
free_irq(s3c_rtc_alarmno, rtc_dev);return ret;
{
struct platform_device *pdev = to_platform_device(dev);struct rtc_device *rtc_dev = platform_get_drvdata(pdev);free_irq(s3c_rtc_alarmno, rtc_dev);free_irq(s3c_rtc_tickno, rtc_dev);
{
unsigned int have_retried = 0;void __iomem *base = s3c_rtc_base;clk_enable(rtc_clk);
/*读取年、月、日、天、小时、分、秒数据*/
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);if (rtc_tm->tm_sec == 0 && !have_retried) {
have_retried = 1;goto retry_get_time;
}
/*将读取出来的数据进行BCD转二进制操作*/
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;clk_disable(rtc_clk);return rtc_valid_tm(rtc_tm);
{
void __iomem *base = s3c_rtc_base;int year = tm->tm_year - 100;clk_enable(rtc_clk);/* we get around y2k by simply not supporting it */if (year < 0 || year >= 100) {
dev_err(dev, "rtc only supports 100 years\\n");return -EINVAL;
}/*将时间数据转成BCD码形式后存入到相应寄存器*/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);clk_disable(rtc_clk);return 0;
static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
{
struct rtc_time *alm_tm = &alrm->time;void __iomem *base = s3c_rtc_base;unsigned int alm_en;clk_enable(rtc_clk);alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);alm_tm->tm_min = readb(base + S3C2410_ALMMIN);alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);alm_tm->tm_mon = readb(base + S3C2410_ALMMON);alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);alm_en = readb(base + S3C2410_RTCALM);alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;/* decode the alarm enable field */if (alm_en & S3C2410_RTCALM_SECEN)
alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
else
alm_tm->tm_sec = -1;
if (alm_en & S3C2410_RTCALM_MINEN)
alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
else
alm_tm->tm_min = -1;
if (alm_en & S3C2410_RTCALM_HOUREN)
alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
else
alm_tm->tm_hour = -1;
if (alm_en & S3C2410_RTCALM_DAYEN)
alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
else
alm_tm->tm_mday = -1;
if (alm_en & S3C2410_RTCALM_MONEN) {
alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);alm_tm->tm_mon -= 1;
} else {
alm_tm->tm_mon = -1;
}if (alm_en & S3C2410_RTCALM_YEAREN)
alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
else
alm_tm->tm_year = -1;
clk_disable(rtc_clk);return 0;
{
struct rtc_time *tm = &alrm->time;void __iomem *base = s3c_rtc_base;unsigned int alrm_en;clk_enable(rtc_clk);alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;writeb(0x00, base + S3C2410_RTCALM);if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
alrm_en |= S3C2410_RTCALM_SECEN;writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
}if (tm->tm_min < 60 && tm->tm_min >= 0) {
alrm_en |= S3C2410_RTCALM_MINEN;writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
}if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
alrm_en |= S3C2410_RTCALM_HOUREN;writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
}writeb(alrm_en, base + S3C2410_RTCALM);s3c_rtc_setaie(dev, alrm->enabled);clk_disable(rtc_clk);return 0;
{
struct rtc_device *rdev = id;clk_enable(rtc_clk);rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);if (s3c_rtc_cpu_type == TYPE_S3C64XX)writeb(S3C2410_INTP_ALM, s3c_rtc_base + S3C2410_INTP);clk_disable(rtc_clk);return IRQ_HANDLED;
3. 使用过程中出现问题及解决方法
“select() to /dev/rtc0 to wait for clock tick timed out.”
linux系统中/proc/interrupt可以用来查询中断号,中断使用次数等信息,由于S3C RTC中的中断申请是在s3c_rtc_open中,所以每次使用完就会在s3c_rtc_release中释放,所以无法在/proc/interrupt中查询到rtc相应信息,为了确定是否有中断产生,把中断申请和释放移到s3c_rtc_probe和s3c_rtc_remove函数中。更新程序后发现S3C RTC中的alarm 和tick中断相应的次数都为0,说明根本就没有产生中断。后来经过测试和分析发现,原来S3C RTC中的低层操作接口在每次设置完相应的寄存器后都会关闭RTC时钟,即clk_disable(rtc_clk);而没有RTC时钟就不会产生相应ALARM 和TICK中断,所以解决的方法就是使能RTC时钟;解决方法是在打开 RTC设备时,就一直使能时钟直到关闭设备;
以上是关于RTC系统转的主要内容,如果未能解决你的问题,请参考以下文章