// SPDX-License-Identifier: GPL-2.0+ /* * "RTT as Real Time Clock" driver for AT91SAM9 SoC family * * (C) 2007 Michel Benoit * * Based on rtc-at91rm9200.c by Rick Bronson
*/
/* * This driver uses two configurable hardware resources that live in the * AT91SAM9 backup power domain (intended to be powered at all times) * to implement the Real Time Clock interfaces * * - A "Real-time Timer" (RTT) counts up in seconds from a base time. * We can't assign the counter value (CRTV) ... but we can reset it. * * - One of the "General Purpose Backup Registers" (GPBRs) holds the * base time, normally an offset from the beginning of the POSIX * epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the * local timezone's offset. * * The RTC's value is the RTT counter plus that offset. The RTC's alarm * is likewise a base (ALMV) plus that offset. * * Not all RTTs will be used as RTCs; some systems have multiple RTTs to * choose from, or a "real" RTC module. All systems have multiple GPBR * registers available, likewise usable for more than "RTC" support.
*/
/* * Read current time and date in RTC
*/ staticint at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
{ struct sam9_rtc *rtc = dev_get_drvdata(dev);
u32 secs, secs2;
u32 offset;
/* read current time offset */
offset = gpbr_readl(rtc); if (offset == 0) return -EILSEQ;
/* reread the counter to help sync the two clock domains */
secs = rtt_readl(rtc, VR);
secs2 = rtt_readl(rtc, VR); if (secs != secs2)
secs = rtt_readl(rtc, VR);
rtc_time64_to_tm(offset + secs, tm);
dev_dbg(dev, "%s: %ptR\n", __func__, tm);
return 0;
}
/* * Set current time and date in RTC
*/ staticint at91_rtc_settime(struct device *dev, struct rtc_time *tm)
{ struct sam9_rtc *rtc = dev_get_drvdata(dev);
u32 offset, alarm, mr; unsignedlong secs;
/* read current time offset */
offset = gpbr_readl(rtc);
/* store the new base time in a battery backup register */
secs += 1;
gpbr_writel(rtc, secs);
/* adjust the alarm time for the new base */
alarm = rtt_readl(rtc, AR); if (alarm != ALARM_DISABLED) { if (offset > secs) { /* time jumped backwards, increase time until alarm */
alarm += (offset - secs);
} elseif ((alarm + offset) > secs) { /* time jumped forwards, decrease time until alarm */
alarm -= (secs - offset);
} else { /* time jumped past the alarm, disable alarm */
alarm = ALARM_DISABLED;
mr &= ~AT91_RTT_ALMIEN;
}
rtt_writel(rtc, AR, alarm);
}
/* reset the timer, and re-enable interrupts */
rtt_writel(rtc, MR, mr | AT91_RTT_RTTRST);
/* Shared interrupt may be for another device. Note: reading * SR clears it, so we must only read it in this irq handler!
*/
mr = rtt_readl(rtc, MR) & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
sr = rtt_readl(rtc, SR) & (mr >> 16); if (!sr) return IRQ_NONE;
/* alarm status */ if (sr & AT91_RTT_ALMS)
rtc->events |= (RTC_AF | RTC_IRQF);
/* * IRQ handler for the RTC
*/ static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
{ struct sam9_rtc *rtc = _rtc; int ret;
spin_lock(&rtc->lock);
ret = at91_rtc_cache_events(rtc);
/* We're called in suspended state */ if (rtc->suspended) { /* Mask irqs coming from this peripheral */
rtt_writel(rtc, MR,
rtt_readl(rtc, MR) &
~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN)); /* Trigger a system wakeup */
pm_system_wakeup();
} else {
at91_rtc_flush_events(rtc);
}
/* register irq handler after we know what name we'll use */
ret = devm_request_irq(&pdev->dev, rtc->irq, at91_rtc_interrupt,
IRQF_SHARED | IRQF_COND_SUSPEND,
dev_name(&rtc->rtcdev->dev), rtc); if (ret) {
dev_dbg(&pdev->dev, "can't share IRQ %d?\n", rtc->irq); goto err_clk;
}
/* NOTE: sam9260 rev A silicon has a ROM bug which resets the * RTT on at least some reboots. If you have that chip, you must * initialize the time from some external source like a GPS, wall * clock, discrete RTC, etc
*/
if (gpbr_readl(rtc) == 0)
dev_warn(&pdev->dev, "%s: SET TIME!\n",
dev_name(&rtc->rtcdev->dev));
/* * This IRQ is shared with DBGU and other hardware which isn't * necessarily a wakeup event source.
*/
rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN); if (rtc->imr) { if (device_may_wakeup(dev) && (mr & AT91_RTT_ALMIEN)) { unsignedlong flags;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.