Skip to contentSkip to conversation

RTC: Initialize kernel state from RTC

RFC PATCH New John Stultz 1 participant 1 message Feb 22, 2011 Real time clock (RTC)

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 RTC: Initialize kernel state from RTC

Just wanted to send this out for initial comment. It would be great if someone with RTC hardware that preserves alarm state across resets could test this. I've tested with cmos and we correctly preserve the alarm time value, but because of the hardware the alarm is not enabled after reset.

If there's no objections, I'll try to squeeze this fix into 2.6.38 (although its frustratingly large for this late in the cycle, so it might have to wait until 2.6.38.1).

thanks
-john

Mark Brown pointed out a coner case: that RTC alarms should be allowed to be persistent across reboots if the hardware supported it.

The rework of the generic layer to virtualize the RTC alarm virtualized much of the alarm handling, and removed the code used to read the alarm time from the hardware.

Mark noted if we want the alarm to be persistent across reboots, we need to re-read the alarm value into the virtualized generic layer at boot up, so that the generic layer properly exposes that value.

This patch restores much of the earlier removed rtc_read_alarm code and wires it in so that we set the kernel's alarm value to what we find in the hardware at boot time.

NOTE: Not all hardware supports persistent RTC alarm state across system reset. rtc-cmos for example will keep the alarm time, but disables the AIE mode irq. Applications should not expect the RTC alarm to be valid after a system reset. We will preserve what we can, to represent the hardware state at boot, but its not guarenteed.

Further, in the future, with multiplexed RTC alarms, the soonest alarm to fire may not be the one set via the /dev/rt ioctls. So an application may set the alarm with RTC_ALM_SET, but after a reset find that RTC_ALM_READ returns an earlier time. Again, we preserve what we can, but applications should not expect the RTC alarm state to persist across a system reset.

Big thanks to Mark for pointing out the issue! Thanks also to Marcelo for helping think through the solution.

CC Mark Brown <[email protected]>
CC Marcelo Roberto Jimenez <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Alessandro Zummo <[email protected]>
---
 drivers/rtc/class.c     |    7 ++
 drivers/rtc/interface.c |  180 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/rtc.h     |    1 +
 3 files changed, 188 insertions(+), 0 deletions(-)
drivers/rtc/class.c +7 -0
index c404b61..09b4437 100644
@@ -117,6 +117,7 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
 					struct module *owner)
 {
 	struct rtc_device *rtc;
+	struct rtc_wkalrm alrm;
 	int id, err;
 
 	if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
@@ -166,6 +167,12 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
 	rtc->pie_timer.function = rtc_pie_update_irq;
 	rtc->pie_enabled = 0;
 
+	/* Check to see if there is an ALARM already set in hw */
+	err = __rtc_read_alarm(rtc, &alrm);
+
+	if (!err && !rtc_valid_tm(&alrm.time))
+		rtc_set_alarm(rtc, &alrm);
+
 	strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
 	dev_set_name(&rtc->dev, "rtc%d", id);
 
drivers/rtc/interface.c +180 -0

186 lines not on this page · raw message

include/linux/rtc.h +1 -0
index 89c3e51..db3832d 100644
@@ -227,6 +227,7 @@ extern void rtc_device_unregister(struct rtc_device *rtc);
 extern int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm);
 extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
 extern int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs);
+int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
 extern int rtc_read_alarm(struct rtc_device *rtc,
 			struct rtc_wkalrm *alrm);
 extern int rtc_set_alarm(struct rtc_device *rtc,
Signature · 1 line
1.7.3.2.146.gca209