RTC Driver for S3c2440

RTC Driver for S3c2440

Real Time Clock

RTC,实时时钟用于在系统电源关闭的情况下依靠备用电池工作,完成计时功能。

工作原理

以上是实时时钟的框架图,由XTIrtc和XTOrtc产生脉冲信号出,传给时钟分频器,得到128Hz的频率,用于产生滴答计数。当TICNT计数为0时,产生一个TIME TICK中断信号。RTCCON寄存器用来控制RTC的功能,RTCRST是重置寄存器,用来重置SEC和MIN寄存器。Leap Year Generator是润年发生器。RTCALM用来控制是否产生报警信号。

RTC设备资源

drivers/rtc/rtc-s3c.c

/* RTC */

static struct resource s3c_rtc_resource[] = {
[0] = {/* io端口资源 */
.start = S3C24XX_PA_RTC,
.end = S3C24XX_PA_RTC + 0xff,
.flags = IORESOURCE_MEM,
},
[1] = {/* Alarm中断资源 */
.start = IRQ_RTC,
.end = IRQ_RTC,
.flags = IORESOURCE_IRQ,
},
[2] = {/* TIME TICK中断资源 */
.start = IRQ_TICK,
.end = IRQ_TICK,
.flags = IORESOURCE_IRQ
}
};

struct platform_device s3c_device_rtc = {
.name = “s3c2410-rtc”,
.id = -1,
.num_resources = ARRAY_SIZE(s3c_rtc_resource),
.resource = s3c_rtc_resource,
};

EXPORT_SYMBOL(s3c_device_rtc); rtc设备资源在mach-mini2440.c中被注册到内核

加载卸载
static struct platform_driver s3c2410_rtc_driver = {
	.probe		= s3c_rtc_probe,
	.remove		= __devexit_p(s3c_rtc_remove),
	.suspend	= s3c_rtc_suspend,
	.resume		= s3c_rtc_resume,
	.driver		= {
		.name	= "s3c2410-rtc",
		.owner	= THIS_MODULE,
	},
};

static char __initdata banner[] = “S3C24XX RTC, (c) 2004,2006 Simtec Electronicsn”;

static int __init s3c_rtc_init(void)
{ printk(banner);
return platform_driver_register(&s3c2410_rtc_driver);
}

static void __exit s3c_rtc_exit(void)
{ platform_driver_unregister(&s3c2410_rtc_driver);
}

module_init(s3c_rtc_init);
module_exit(s3c_rtc_exit); 这一部分代码向平台注册了平台驱动。

RTC探测函数s3c_rtc_probe()

首先介绍一个重要的结构体 struct rtc_device
include/linux/rtc.h

struct rtc_device
{
	struct device dev;			//内嵌设备结构体
	struct module *owner;			//所属模块

int id; //设备id 
char name[RTC_DEVICE_NAME_SIZE]; //RTC设备名

const struct rtc_class_ops *ops; //类操作函数集
struct mutex ops_lock; //互斥锁

struct cdev char_dev; //内嵌字符设备
unsigned long flags; //RTC状态标志

unsigned long irq_data; //中断数据
spinlock_t irq_lock; //中断自旋锁
wait_queue_head_t irq_queue; //中断等待队列
struct fasync_struct *async_queue; //异步队列

struct rtc_task *irq_task; //RTC任务结构体
spinlock_t irq_task_lock; //中断任务自旋锁
int irq_freq; //中断频率
int max_user_freq; //最大用户频率
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
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
};

以下是s3c_rtc_probe()

static int __devinit s3c_rtc_probe(struct platform_device *pdev)
{
	struct rtc_device *rtc;
	struct resource *res;
	int ret;

pr_debug(“%s: probe=%pn”, __func__, pdev);

/* find the IRQs */

/* 获得TIME TICK 中断号 */
s3c_rtc_tickno = platform_get_irq(pdev, 1);
if (s3c_rtc_tickno < 0) {
dev_err(&pdev->dev, “no irq for rtc tickn”);
return -ENOENT;
}

/* 获得Alarm 中断号 */
s3c_rtc_alarmno = platform_get_irq(pdev, 0);
if (s3c_rtc_alarmno < 0) {
dev_err(&pdev->dev, “no irq for alarmn”);
return -ENOENT;
}

pr_debug(“s3c2410_rtc: tick irq %d, alarm irq %dn”,
s3c_rtc_tickno, s3c_rtc_alarmno);

/* get the memory region */

/* 获得io端口资源 */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
dev_err(&pdev->dev, “failed to get memory region resourcen”);
return -ENOENT;
}

RTC频率设置函数s3c_rtc_setfreq()

/* 申请io内存区域 */
s3c_rtc_mem = request_mem_region(res->start,
res->end-res->start+1,
pdev->name);

if (s3c_rtc_mem == NULL) {
dev_err(&pdev->dev, “failed to reserve memory regionn”);
ret = -ENOENT;
goto err_nores;
}

/* 将物理地址映射到虚拟地址 */
s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
if (s3c_rtc_base == NULL) {
dev_err(&pdev->dev, “failed ioremap()n”);
ret = -EINVAL;
goto err_nomap;
}

/* check to see if everything is setup correctly */

/* 使能rtc */
s3c_rtc_enable(pdev, 1);

pr_debug(“s3c2410_rtc: RTCCON=%02xn”,
readb(s3c_rtc_base + S3C2410_RTCCON));

/* 设置rtc频率为1Hz */
s3c_rtc_setfreq(&pdev->dev, 1);

/* 让驱动支持电源管理,arg2=1表示设备可以唤醒 */
device_init_wakeup(&pdev->dev, 1);

/* register RTC and exit */

rtc = rtc_device_register(“s3c”, &pdev->dev, &s3c_rtcops,
THIS_MODULE);

if (IS_ERR(rtc)) {
dev_err(&pdev->dev, “cannot attach rtcn”);
ret = PTR_ERR(rtc);
goto err_nortc;
}

rtc->max_user_freq = 128;

/* 保存rtc结构体 */
platform_set_drvdata(pdev, rtc);
return 0;

err_nortc:
s3c_rtc_enable(pdev, 0);
iounmap(s3c_rtc_base);

err_nomap:
release_resource(s3c_rtc_mem);

err_nores:
return ret;
}

RTC使能函数s3c_rtc_enable()

static void s3c_rtc_enable(struct platform_device *pdev, int en)
{
	void __iomem *base = s3c_rtc_base;
	unsigned int tmp;

if (s3c_rtc_base == NULL)
return;

if (!en) {
/* 禁止RTC */
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-enable the device, and check it is ok */

/* 使能RTC */
if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
dev_info(&pdev->dev, “rtc disabled, re-enablingn”);

tmp = readb(base + S3C2410_RTCCON);
writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
}

/* 使用合并的BCD码 */
if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
dev_info(&pdev->dev, “removing RTCCON_CNTSELn”);

tmp = readb(base + S3C2410_RTCCON);
writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
}

/* 禁止RTC重置位 */
if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
dev_info(&pdev->dev, “removing RTCCON_CLKRSTn”);

tmp = readb(base + S3C2410_RTCCON);
writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
}
}
}

RTC频率设置函数s3c_rtc_setfreq()

/* 设置TIME TICK频率 */
static int s3c_rtc_setfreq(struct device *dev, int freq)
{
	unsigned int tmp;

if (!is_power_of_2(freq))
return -EINVAL;

/* 使用自旋锁保护临界资源 */
spin_lock_irq(&s3c_rtc_pie_lock);

tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
/* n=(128 / freq )-1 由datasheet给出 */
tmp |= (128 / freq)-1;

writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
spin_unlock_irq(&s3c_rtc_pie_lock);

return 0;
}

RTC卸载函数s3c_rtc_remove()
static int __devexit s3c_rtc_remove(struct platform_device *dev)
{
	struct rtc_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);

return 0;
}

文件系统接口rtc设备文件操作集struct rtc_class_ops
struct rtc_class_ops {
	int (*open)(struct device *);						//设备打开函数
	void (*release)(struct device *);					//设备释放函数
	int (*ioctl)(struct device *, unsigned int, unsigned long);		//ioctl
	int (*read_time)(struct device *, struct rtc_time *);			//读取时间函数
	int (*set_time)(struct device *, struct rtc_time *);			//设置时间函数
	int (*read_alarm)(struct device *, struct rtc_wkalrm *);		//读取alarm函数
	int (*set_alarm)(struct device *, struct rtc_wkalrm *);			//设置alarm函数
	int (*proc)(struct device *, struct seq_file *);			//proc文件接口
	int (*set_mmss)(struct device *, unsigned long secs);			
	int (*irq_set_state)(struct device *, int enabled);			//中断状态设置函数
	int (*irq_set_freq)(struct device *, int freq);				//中断频率设置函数,最大不超过64
	int (*read_callback)(struct device *, int data);			
	int (*alarm_irq_enable)(struct device *, unsigned int enabled);		//alarm中断使能函数
	int (*update_irq_enable)(struct device *, unsigned int enabled);	//更新中断使能状态
};

RTC驱动中只实现了部分接口

struct rtc_class_ops {
	int (*open)(struct device *);						//设备打开函数
	void (*release)(struct device *);					//设备释放函数
	int (*ioctl)(struct device *, unsigned int, unsigned long);		//ioctl
	int (*read_time)(struct device *, struct rtc_time *);			//读取时间函数
	int (*set_time)(struct device *, struct rtc_time *);			//设置时间函数
	int (*read_alarm)(struct device *, struct rtc_wkalrm *);		//读取alarm函数
	int (*set_alarm)(struct device *, struct rtc_wkalrm *);			//设置alarm函数
	int (*proc)(struct device *, struct seq_file *);			//proc文件接口
	int (*set_mmss)(struct device *, unsigned long secs);			
	int (*irq_set_state)(struct device *, int enabled);			//中断状态设置函数
	int (*irq_set_freq)(struct device *, int freq);				//中断频率设置函数,最大不超过64
	int (*read_callback)(struct device *, int data);			
	int (*alarm_irq_enable)(struct device *, unsigned int enabled);		//alarm中断使能函数
	int (*update_irq_enable)(struct device *, unsigned int enabled);	//更新中断使能状态
};

RTC打开函数s3c_rtc_open()

static int s3c_rtc_open(struct device *dev)
{
	/* 获得platform_device结构体指针 */
	struct platform_device *pdev = to_platform_device(dev);
	struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
	int ret;

/* 申请ALMINT,设置s3c_rtc_alarmirq为中断处理函数,IRQF_DISABLED相当于SA_INTERRUPT,表示快速中断处理历程
* 接收中断后,当前处理器将不再处理其他中断 */
ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
IRQF_DISABLED, “s3c2410-rtc alarm”, rtc_dev);

if (ret) {
dev_err(dev, “IRQ%d error %dn”, s3c_rtc_alarmno, ret);
return ret;
}

/* 申请TIME TICK中断,设置s3c_rtc_tickirq为中断处理函数*/
ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
IRQF_DISABLED, “s3c2410-rtc tick”, rtc_dev);

if (ret) {
dev_err(dev, “IRQ%d error %dn”, s3c_rtc_tickno, ret);
goto tick_err;
}

return ret;

tick_err:
free_irq(s3c_rtc_alarmno, rtc_dev);
return ret;
}

RTC关闭函数s3c_rtc_release()

static void s3c_rtc_release(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct rtc_device *rtc_dev = platform_get_drvdata(pdev);

/* do not clear AIE here, it may be needed for wake */

s3c_rtc_setpie(dev, 0);
/* 释放中断线 */
free_irq(s3c_rtc_alarmno, rtc_dev);
free_irq(s3c_rtc_tickno, rtc_dev);
}

RTC读取时间函数s3c_rtc_gettime()函数中涉及到struct rtc_time结构体,这个结构体用于保存读取到的时间
struct rtc_time {
	int tm_sec;
	int tm_min;
	int tm_hour;
	int tm_mday;
	int tm_mon;
	int tm_year;
	int tm_wday;
	int tm_yday;
	int tm_isdst;
};

由于从寄存器中读取的数值是BCD编码,需要转换为二进制码

/* 将BCD码转换为二进制码 */
unsigned bcd2bin(unsigned char val)
{
	return (val & 0x0f) + (val >> 4) * 10;
}
EXPORT_SYMBOL(bcd2bin);
/* 将二进制码转换为BCD码 */
unsigned char bin2bcd(unsigned val)
{
	return ((val / 10) << 4) + val % 10;
}
EXPORT_SYMBOL(bin2bcd);
static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
{
	unsigned int have_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);

/* the only way to work out wether the system was mid-update
* when we read it is to check the second counter, and if it
* is zero, then we re-try the entire read
*/

/* 如果秒计时器为0,表示已经过去一分钟,其他寄存器的值都可能
* 进1位,所以需要重新读取寄存器 */
if (rtc_tm->tm_sec == 0 && !have_retried) {
have_retried = 1;
goto retry_get_time;
}

pr_debug(“read time %02x.%02x.%02x %02x/%02x/%02xn”,
rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);

/* 将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;

return 0;
}

读取时间函数

static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
{
	unsigned int have_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);

/* the only way to work out wether the system was mid-update
* when we read it is to check the second counter, and if it
* is zero, then we re-try the entire read
*/

/* 如果秒计时器为0,表示已经过去一分钟,其他寄存器的值都可能
* 进1位,所以需要重新读取寄存器 */
if (rtc_tm->tm_sec == 0 && !have_retried) {
have_retried = 1;
goto retry_get_time;
}

pr_debug(“read time %02x.%02x.%02x %02x/%02x/%02xn”,
rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);

/* 将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;

return 0;
}

设置时间函数s3c_rtc_settime()

/* 设置时间 */
static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
{
	void __iomem *base = s3c_rtc_base;
	int year = tm->tm_year - 100;

pr_debug(“set time %02d.%02d.%02d %02d/%02d/%02dn”,
tm->tm_year, tm->tm_mon, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);

/* we get around y2k by simply not supporting it */

if (year < 0 || year >= 100) {
dev_err(dev, “rtc only supports 100 yearsn”);
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);

return 0;
} 由于存储器中存储的时间比实际时间少100年,所以要减去100年

RTC alarm 读取函数这个函数很简单,没做注释
/* 获得rtc_alarm设定函数 */
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;

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;

pr_debug(“read alarm %02x %02x.%02x.%02x %02x/%02x/%02xn”,
alm_en,
alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);

/* 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 = 0xff;

if (alm_en & S3C2410_RTCALM_MINEN)
alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
else
alm_tm->tm_min = 0xff;

if (alm_en & S3C2410_RTCALM_HOUREN)
alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
else
alm_tm->tm_hour = 0xff;

if (alm_en & S3C2410_RTCALM_DAYEN)
alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
else
alm_tm->tm_mday = 0xff;

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 = 0xff;
}

if (alm_en & S3C2410_RTCALM_YEAREN)
alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
else
alm_tm->tm_year = 0xffff;

return 0;
}

RTC Alarm设置函数s3c_rtc_setalarm()

/* 设置Alarm函数 */
static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct rtc_time *tm = &alrm->time;
	void __iomem *base = s3c_rtc_base;
	unsigned int alrm_en;

pr_debug(“s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02xn”,
alrm->enabled,
tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);

/* 读取全局Alarm使能位 */
alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
/* 清除Alarm设置 */
writeb(0x00, base + S3C2410_RTCALM);

/* 使能并设置alarm时间 */
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);
}

pr_debug(“setting S3C2410_RTCALM to %08xn”, alrm_en);

/* 写回 */
writeb(alrm_en, base + S3C2410_RTCALM);

s3c_rtc_setaie(alrm->enabled);

if (alrm->enabled)
/* 使能中断唤醒 */
enable_irq_wake(s3c_rtc_alarmno);
else
disable_irq_wake(s3c_rtc_alarmno);

return 0;
}

RTC proc接口s3c_rtc_proc()可以从proc文件读取rtc是否支持脉冲中断。
static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
{
	unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);

seq_printf(seq, “periodic_IRQt: %sn”,
(ticnt & S3C2410_TICNT_ENABLE) ? “yes” : “no” );
return 0;
}

中断相关函数

RTC 时钟脉冲中断使能函数s3c_rtc_setpie()

enabled 参数为1时表示允许脉冲中断

/* 设置TIME_TICK中断 */
static int s3c_rtc_setpie(struct device *dev, int enabled)
{
	unsigned int tmp;

pr_debug(“%s: pie=%dn”, __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);

return 0;
}

设置Alarm中断函数s3c_rtc_setaie()

to为1时表示使能alarm中断

/* 设置Alarm中断 */
static void s3c_rtc_setaie(int to)
{
	unsigned int tmp;

pr_debug(“%s: aie=%dn”, __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);
}

中断处理函数产生一个时钟中断的时候就更新一下rtc_irq_data的值,也就是说只有当产生一个时钟中断才返回给用户一个时间
 
static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
{
	struct rtc_device *rdev = id;

rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
return IRQ_HANDLED;
}

static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
{ struct rtc_device *rdev = id;

rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
return IRQ_HANDLED;
}


你可能感兴趣的:(arm-linux驱动研究)