/* * According to HW manual (section 22.6.4. Notes on writing to and * reading from registers) after writing to count registers, alarm * registers, year alarm enable register, bits RCR2.AADJE, AADJP, * and HR24 register, we need to do 3 empty reads before being * able to fetch the registers content.
*/ for (u8 i = 0; i < 3; i++) {
readb(priv->base + RTCA3_RSECCNT + offset);
readb(priv->base + RTCA3_RMINCNT + offset);
readb(priv->base + RTCA3_RHRCNT + offset);
readb(priv->base + RTCA3_RWKCNT + offset);
readb(priv->base + RTCA3_RDAYCNT + offset);
readw(priv->base + RTCA3_RYRCNT + offset); if (!cnt)
readb(priv->base + RTCA3_RYRAREN);
}
}
/* * We cannot generate carries due to reading 64Hz counter as * the driver doesn't implement carry, thus, carries will be * generated once per seconds. Add a timeout of 5 trials here * to avoid infinite loop, if any.
*/
} while ((tmp & RTCA3_RSR_CF) && ++trials < 5);
if (enabled) { /* * AIE, CIE, PIE bit indexes in RSR corresponds with * those on RCR1. Same interrupts mask can be used.
*/
rtca3_byte_update_bits(priv, RTCA3_RSR, interrupts, 0);
val = interrupts;
} else {
val = 0;
}
/* Make sure we can read back the counters. */
rtca3_prepare_cntalrm_regs_for_read(priv, false);
/* Need to wait for 2 * 1/64 periodic interrupts to be generated. */
atomic_set(&priv->alrm_sstep, RTCA3_ALRM_SSTEP_INIT);
reinit_completion(&priv->set_alarm_completion);
/* Wait for the 2 * 1/64 periodic interrupts. */
ret = wait_for_completion_interruptible_timeout(&priv->set_alarm_completion,
msecs_to_jiffies(500)); if (ret <= 0) {
ret = -ETIMEDOUT; goto setup_failed;
}
*offset = cycles * ppb_per_cycle;
val = FIELD_GET(RTCA3_RADJ_PMADJ, radj); if (val == RTCA3_RADJ_PMADJ_SUB)
*offset = -(*offset);
return 0;
}
staticint rtca3_set_offset(struct device *dev, long offset)
{ struct rtca3_priv *priv = dev_get_drvdata(dev); int cycles, cycles10, cycles60;
u8 radj, adjp, tmp; int ret;
/* * Automatic time error adjustment could be set at intervals of 10 * or 60 seconds.
*/
cycles10 = DIV_ROUND_CLOSEST(offset, priv->ppb.ten_sec);
cycles60 = DIV_ROUND_CLOSEST(offset, priv->ppb.sixty_sec);
/* We can set b/w 1 and 63 clock cycles. */ if (cycles60 >= -RTCA3_RADJ_ADJ_MAX &&
cycles60 <= RTCA3_RADJ_ADJ_MAX) {
cycles = cycles60;
adjp = 0;
} elseif (cycles10 >= -RTCA3_RADJ_ADJ_MAX &&
cycles10 <= RTCA3_RADJ_ADJ_MAX) {
cycles = cycles10;
adjp = RTCA3_RCR2_ADJP;
} else { return -ERANGE;
}
if ((tmp & RTCA3_RCR2_ADJP) != adjp) { /* RADJ.PMADJ need to be set to zero before setting RCR2.ADJP. */
writeb(0, priv->base + RTCA3_RADJ);
ret = readb_poll_timeout_atomic(priv->base + RTCA3_RADJ, tmp, !tmp,
10, RTCA3_DEFAULT_TIMEOUT_US); if (ret) return ret;
/* * According to HW manual (section 22.4.2. Clock and count mode setting procedure) * we need to wait at least 6 cycles of the 32KHz clock after clock was enabled.
*/
usleep_range(sleep_us, sleep_us + 10);
mask = RTCA3_RCR2_START | RTCA3_RCR2_HR24;
val = readb(priv->base + RTCA3_RCR2); /* Only disable the interrupts if already started in 24 hours and calendar count mode. */ if ((val & mask) == mask) { /* Disable all interrupts. */
mask = RTCA3_RCR1_AIE | RTCA3_RCR1_CIE | RTCA3_RCR1_PIE; return rtca3_alarm_irq_set_helper(priv, mask, 0);
}
/* Reconfigure the RTC in 24 hours and calendar count mode. */
mask = RTCA3_RCR2_START | RTCA3_RCR2_CNTMD;
writeb(0, priv->base + RTCA3_RCR2);
ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, !(tmp & mask),
10, RTCA3_DEFAULT_TIMEOUT_US); if (ret) return ret;
/* * Set 24 hours mode. According to HW manual (section 22.3.19. RTC Control * Register 2) this needs to be done separate from stop operation.
*/
mask = RTCA3_RCR2_HR24;
val = RTCA3_RCR2_HR24;
writeb(val, priv->base + RTCA3_RCR2);
ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, (tmp & mask),
10, RTCA3_DEFAULT_TIMEOUT_US); if (ret) return ret;
/* * According to HW manual (section 22.6.3. Notes on writing to and reading * from registers) after reset we need to wait 6 clock cycles before * writing to RTC registers.
*/
usleep_range(sleep_us, sleep_us + 10);
/* Set no adjustment. */
writeb(0, priv->base + RTCA3_RADJ);
ret = readb_poll_timeout(priv->base + RTCA3_RADJ, tmp, !tmp, 10,
RTCA3_DEFAULT_TIMEOUT_US);
/* Start the RTC and enable automatic time error adjustment. */
mask = RTCA3_RCR2_START | RTCA3_RCR2_AADJE;
val |= RTCA3_RCR2_START | RTCA3_RCR2_AADJE;
writeb(val, priv->base + RTCA3_RCR2);
ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, ((tmp & mask) == mask),
10, RTCA3_START_TIMEOUT_US); if (ret) return ret;
/* * According to HW manual (section 22.6.4. Notes on writing to and reading * from registers) we need to wait 1/128 seconds while the clock is operating * (RCR2.START bit = 1) to be able to read the counters after a return from * reset.
*/
usleep_range(8000, 9000);
/* Set period interrupt to 1/64 seconds. It is necessary for alarm setup. */
val = FIELD_PREP(RTCA3_RCR1_PES, RTCA3_RCR1_PES_1_64_SEC);
rtca3_byte_update_bits(priv, RTCA3_RCR1, RTCA3_RCR1_PES, val); return readb_poll_timeout(priv->base + RTCA3_RCR1, tmp, ((tmp & RTCA3_RCR1_PES) == val),
10, RTCA3_DEFAULT_TIMEOUT_US);
}
irq = platform_get_irq_byname(pdev, "alarm"); if (irq < 0) return dev_err_probe(dev, irq, "Failed to get alarm IRQ!\n");
ret = devm_request_irq(dev, irq, rtca3_alarm_handler, 0, "rtca3-alarm", priv); if (ret) return dev_err_probe(dev, ret, "Failed to request alarm IRQ!\n");
priv->wakeup_irq = irq;
irq = platform_get_irq_byname(pdev, "period"); if (irq < 0) return dev_err_probe(dev, irq, "Failed to get period IRQ!\n");
ret = devm_request_irq(dev, irq, rtca3_periodic_handler, 0, "rtca3-period", priv); if (ret) return dev_err_probe(dev, ret, "Failed to request period IRQ!\n");
/* * Driver doesn't implement carry handler. Just get the IRQ here * for backward compatibility, in case carry support will be added later.
*/
irq = platform_get_irq_byname(pdev, "carry"); if (irq < 0) return dev_err_probe(dev, irq, "Failed to get carry IRQ!\n");
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); if (!priv) return -ENOMEM;
priv->base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(priv->base)) return PTR_ERR(priv->base);
ret = devm_pm_runtime_enable(dev); if (ret) return ret;
priv->rstc = devm_reset_control_get_shared(dev, NULL); if (IS_ERR(priv->rstc)) return PTR_ERR(priv->rstc);
ret = pm_runtime_resume_and_get(dev); if (ret) return ret;
ret = reset_control_deassert(priv->rstc); if (ret) {
pm_runtime_put_sync(dev); return ret;
}
dev_set_drvdata(dev, priv);
ret = devm_add_action_or_reset(dev, rtca3_action, dev); if (ret) return ret;
/* * This must be an always-on clock to keep the RTC running even after * driver is unbinded.
*/
clk = devm_clk_get_enabled(dev, "counter"); if (IS_ERR(clk)) return PTR_ERR(clk);
ret = rtc_read_alarm(rtc_dev, &alarm); if (ret) return ret;
if (!alarm.enabled) return 0;
ret = rtc_read_time(rtc_dev, &tm); if (ret) return ret;
alarm_time = rtc_tm_to_time64(&alarm.time);
now = rtc_tm_to_time64(&tm); if (alarm_time >= now) return 0;
/* * Heuristically, it has been determined that when returning from deep * sleep state the RTCA3_RSR.AF is zero even though the alarm expired. * Call again the rtc_update_irq() if alarm helper detects this.
*/
guard(spinlock_irqsave)(&priv->lock);
pending = rtca3_alarm_handler_helper(priv); if (!pending)
rtc_update_irq(priv->rtc_dev, 1, RTC_AF | RTC_IRQF);
/* * According to the HW manual (section 22.6.4 Notes on writing to * and reading from registers) we need to wait 1/128 seconds while * RCR2.START = 1 to be able to read the counters after a return from low * power consumption state.
*/
mdelay(8);
/* * The alarm cannot wake the system from deep sleep states. In case * we return from deep sleep states and the alarm expired we need * to disable it to avoid failures when setting another alarm.
*/ return rtca3_clean_alarm(priv);
}
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.