1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments K3 RTC driver
5 * Copyright (C) 2021-2022 Texas Instruments Incorporated - https://www.ti.com/
9 #include <linux/delay.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/sys_soc.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 #include <linux/rtc.h>
20 #define REG_K3RTC_S_CNT_LSW 0x08
21 #define REG_K3RTC_S_CNT_MSW 0x0c
22 #define REG_K3RTC_COMP 0x10
23 #define REG_K3RTC_ON_OFF_S_CNT_LSW 0x20
24 #define REG_K3RTC_ON_OFF_S_CNT_MSW 0x24
25 #define REG_K3RTC_SCRATCH0 0x30
26 #define REG_K3RTC_SCRATCH7 0x4c
27 #define REG_K3RTC_GENERAL_CTL 0x50
28 #define REG_K3RTC_IRQSTATUS_RAW_SYS 0x54
29 #define REG_K3RTC_IRQSTATUS_SYS 0x58
30 #define REG_K3RTC_IRQENABLE_SET_SYS 0x5c
31 #define REG_K3RTC_IRQENABLE_CLR_SYS 0x60
32 #define REG_K3RTC_SYNCPEND 0x68
33 #define REG_K3RTC_KICK0 0x70
34 #define REG_K3RTC_KICK1 0x74
36 /* Freeze when lsw is read and unfreeze when msw is read */
37 #define K3RTC_CNT_FMODE_S_CNT_VALUE (0x2 << 24)
39 /* Magic values for lock/unlock */
40 #define K3RTC_KICK0_UNLOCK_VALUE 0x83e70b13
41 #define K3RTC_KICK1_UNLOCK_VALUE 0x95a4f1e0
43 /* Multiplier for ppb conversions */
44 #define K3RTC_PPB_MULT (1000000000LL)
45 /* Min and max values supported with 'offset' interface (swapped sign) */
46 #define K3RTC_MIN_OFFSET (-277761)
47 #define K3RTC_MAX_OFFSET (277778)
49 static const struct regmap_config ti_k3_rtc_regmap_config = {
50 .name = "peripheral-registers",
54 .max_register = REG_K3RTC_KICK1,
57 enum ti_k3_rtc_fields {
62 K3RTC_O32K_OSC_DEP_EN,
66 K3RTC_RELOAD_FROM_BBD,
77 K3RTC_IRQ_ENABLE_CLR_ALT,
82 static const struct reg_field ti_rtc_reg_fields[] = {
83 [K3RTC_KICK0] = REG_FIELD(REG_K3RTC_KICK0, 0, 31),
84 [K3RTC_KICK1] = REG_FIELD(REG_K3RTC_KICK1, 0, 31),
85 [K3RTC_S_CNT_LSW] = REG_FIELD(REG_K3RTC_S_CNT_LSW, 0, 31),
86 [K3RTC_S_CNT_MSW] = REG_FIELD(REG_K3RTC_S_CNT_MSW, 0, 15),
87 [K3RTC_O32K_OSC_DEP_EN] = REG_FIELD(REG_K3RTC_GENERAL_CTL, 21, 21),
88 [K3RTC_UNLOCK] = REG_FIELD(REG_K3RTC_GENERAL_CTL, 23, 23),
89 [K3RTC_CNT_FMODE] = REG_FIELD(REG_K3RTC_GENERAL_CTL, 24, 25),
90 [K3RTC_PEND] = REG_FIELD(REG_K3RTC_SYNCPEND, 0, 1),
91 [K3RTC_RELOAD_FROM_BBD] = REG_FIELD(REG_K3RTC_SYNCPEND, 31, 31),
92 [K3RTC_COMP] = REG_FIELD(REG_K3RTC_COMP, 0, 31),
94 /* We use on to off as alarm trigger */
95 [K3RTC_ALM_S_CNT_LSW] = REG_FIELD(REG_K3RTC_ON_OFF_S_CNT_LSW, 0, 31),
96 [K3RTC_ALM_S_CNT_MSW] = REG_FIELD(REG_K3RTC_ON_OFF_S_CNT_MSW, 0, 15),
97 [K3RTC_IRQ_STATUS_RAW] = REG_FIELD(REG_K3RTC_IRQSTATUS_RAW_SYS, 0, 0),
98 [K3RTC_IRQ_STATUS] = REG_FIELD(REG_K3RTC_IRQSTATUS_SYS, 0, 0),
99 [K3RTC_IRQ_ENABLE_SET] = REG_FIELD(REG_K3RTC_IRQENABLE_SET_SYS, 0, 0),
100 [K3RTC_IRQ_ENABLE_CLR] = REG_FIELD(REG_K3RTC_IRQENABLE_CLR_SYS, 0, 0),
101 /* Off to on is alternate */
102 [K3RTC_IRQ_STATUS_ALT] = REG_FIELD(REG_K3RTC_IRQSTATUS_SYS, 1, 1),
103 [K3RTC_IRQ_ENABLE_CLR_ALT] = REG_FIELD(REG_K3RTC_IRQENABLE_CLR_SYS, 1, 1),
107 * struct ti_k3_rtc - Private data for ti-k3-rtc
109 * @sync_timeout_us: data sync timeout period in uSec
110 * @rate_32k: 32k clock rate in Hz
111 * @rtc_dev: rtc device
112 * @regmap: rtc mmio regmap
113 * @r_fields: rtc register fields
118 unsigned long rate_32k;
119 struct rtc_device *rtc_dev;
120 struct regmap *regmap;
121 struct regmap_field *r_fields[K3_RTC_MAX_FIELDS];
124 static int k3rtc_field_read(struct ti_k3_rtc *priv, enum ti_k3_rtc_fields f)
129 ret = regmap_field_read(priv->r_fields[f], &val);
131 * We shouldn't be seeing regmap fail on us for mmio reads
132 * This is possible if clock context fails, but that isn't the case for us
134 if (WARN_ON_ONCE(ret))
139 static void k3rtc_field_write(struct ti_k3_rtc *priv, enum ti_k3_rtc_fields f, u32 val)
141 regmap_field_write(priv->r_fields[f], val);
145 * k3rtc_fence - Ensure a register sync took place between the two domains
146 * @priv: pointer to priv data
148 * Return: 0 if the sync took place, else returns -ETIMEDOUT
150 static int k3rtc_fence(struct ti_k3_rtc *priv)
154 ret = regmap_field_read_poll_timeout(priv->r_fields[K3RTC_PEND], ret,
155 !ret, 2, priv->sync_timeout_us);
160 static inline int k3rtc_check_unlocked(struct ti_k3_rtc *priv)
164 ret = k3rtc_field_read(priv, K3RTC_UNLOCK);
168 return (ret) ? 0 : 1;
171 static int k3rtc_unlock_rtc(struct ti_k3_rtc *priv)
175 ret = k3rtc_check_unlocked(priv);
179 k3rtc_field_write(priv, K3RTC_KICK0, K3RTC_KICK0_UNLOCK_VALUE);
180 k3rtc_field_write(priv, K3RTC_KICK1, K3RTC_KICK1_UNLOCK_VALUE);
182 /* Skip fence since we are going to check the unlock bit as fence */
183 ret = regmap_field_read_poll_timeout(priv->r_fields[K3RTC_UNLOCK], ret,
184 ret, 2, priv->sync_timeout_us);
190 * This is the list of SoCs affected by TI's i2327 errata causing the RTC
191 * state-machine to break if not unlocked fast enough during boot. These
192 * SoCs must have the bootloader unlock this device very early in the
193 * boot-flow before we (Linux) can use this device.
195 static const struct soc_device_attribute has_erratum_i2327[] = {
196 { .family = "AM62X", .revision = "SR1.0" },
200 static int k3rtc_configure(struct device *dev)
203 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
206 * HWBUG: The compare state machine is broken if the RTC module
207 * is NOT unlocked in under one second of boot - which is pretty long
208 * time from the perspective of Linux driver (module load, u-boot
209 * shell all can take much longer than this.
211 * In such occurrence, it is assumed that the RTC module is unusable
213 if (soc_device_match(has_erratum_i2327)) {
214 ret = k3rtc_check_unlocked(priv);
215 /* If there is an error OR if we are locked, return error */
218 HW_ERR "Erratum i2327 unlock QUIRK! Cannot operate!!\n");
222 /* May need to explicitly unlock first time */
223 ret = k3rtc_unlock_rtc(priv);
225 dev_err(dev, "Failed to unlock(%d)!\n", ret);
230 /* Enable Shadow register sync on 32k clock boundary */
231 k3rtc_field_write(priv, K3RTC_O32K_OSC_DEP_EN, 0x1);
234 * Wait at least clock sync time before proceeding further programming.
235 * This ensures that the 32k based sync is active.
237 usleep_range(priv->sync_timeout_us, priv->sync_timeout_us + 5);
239 /* We need to ensure fence here to make sure sync here */
240 ret = k3rtc_fence(priv);
243 "Failed fence osc_dep enable(%d) - is 32k clk working?!\n", ret);
248 * FMODE setting: Reading lower seconds will freeze value on higher
249 * seconds. This also implies that we must *ALWAYS* read lower seconds
250 * prior to reading higher seconds
252 k3rtc_field_write(priv, K3RTC_CNT_FMODE, K3RTC_CNT_FMODE_S_CNT_VALUE);
254 /* Clear any spurious IRQ sources if any */
255 k3rtc_field_write(priv, K3RTC_IRQ_STATUS_ALT, 0x1);
256 k3rtc_field_write(priv, K3RTC_IRQ_STATUS, 0x1);
257 /* Disable all IRQs */
258 k3rtc_field_write(priv, K3RTC_IRQ_ENABLE_CLR_ALT, 0x1);
259 k3rtc_field_write(priv, K3RTC_IRQ_ENABLE_CLR, 0x1);
261 /* And.. Let us Sync the writes in */
262 return k3rtc_fence(priv);
265 static int ti_k3_rtc_read_time(struct device *dev, struct rtc_time *tm)
267 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
268 u32 seconds_lo, seconds_hi;
270 seconds_lo = k3rtc_field_read(priv, K3RTC_S_CNT_LSW);
271 seconds_hi = k3rtc_field_read(priv, K3RTC_S_CNT_MSW);
273 rtc_time64_to_tm((((time64_t)seconds_hi) << 32) | (time64_t)seconds_lo, tm);
278 static int ti_k3_rtc_set_time(struct device *dev, struct rtc_time *tm)
280 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
283 seconds = rtc_tm_to_time64(tm);
286 * Read operation on LSW will freeze the RTC, so to update
287 * the time, we cannot use field operations. Just write since the
288 * reserved bits are ignored.
290 regmap_write(priv->regmap, REG_K3RTC_S_CNT_LSW, seconds);
291 regmap_write(priv->regmap, REG_K3RTC_S_CNT_MSW, seconds >> 32);
293 return k3rtc_fence(priv);
296 static int ti_k3_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
298 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
300 u32 offset = enabled ? K3RTC_IRQ_ENABLE_SET : K3RTC_IRQ_ENABLE_CLR;
302 reg = k3rtc_field_read(priv, K3RTC_IRQ_ENABLE_SET);
303 if ((enabled && reg) || (!enabled && !reg))
306 k3rtc_field_write(priv, offset, 0x1);
309 * Ensure the write sync is through - NOTE: it should be OK to have
310 * ISR to fire as we are checking sync (which should be done in a 32k
313 return k3rtc_fence(priv);
316 static int ti_k3_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
318 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
319 u32 seconds_lo, seconds_hi;
321 seconds_lo = k3rtc_field_read(priv, K3RTC_ALM_S_CNT_LSW);
322 seconds_hi = k3rtc_field_read(priv, K3RTC_ALM_S_CNT_MSW);
324 rtc_time64_to_tm((((time64_t)seconds_hi) << 32) | (time64_t)seconds_lo, &alarm->time);
326 alarm->enabled = k3rtc_field_read(priv, K3RTC_IRQ_ENABLE_SET);
331 static int ti_k3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
333 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
337 seconds = rtc_tm_to_time64(&alarm->time);
339 k3rtc_field_write(priv, K3RTC_ALM_S_CNT_LSW, seconds);
340 k3rtc_field_write(priv, K3RTC_ALM_S_CNT_MSW, (seconds >> 32));
342 /* Make sure the alarm time is synced in */
343 ret = k3rtc_fence(priv);
345 dev_err(dev, "Failed to fence(%d)! Potential config issue?\n", ret);
349 /* Alarm IRQ enable will do a sync */
350 return ti_k3_rtc_alarm_irq_enable(dev, alarm->enabled);
353 static int ti_k3_rtc_read_offset(struct device *dev, long *offset)
355 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
356 u32 ticks_per_hr = priv->rate_32k * 3600;
360 comp = k3rtc_field_read(priv, K3RTC_COMP);
362 /* Convert from RTC calibration register format to ppb format */
363 tmp = comp * (s64)K3RTC_PPB_MULT;
365 tmp -= ticks_per_hr / 2LL;
367 tmp += ticks_per_hr / 2LL;
368 tmp = div_s64(tmp, ticks_per_hr);
370 /* Offset value operates in negative way, so swap sign */
371 *offset = (long)-tmp;
376 static int ti_k3_rtc_set_offset(struct device *dev, long offset)
378 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
379 u32 ticks_per_hr = priv->rate_32k * 3600;
383 /* Make sure offset value is within supported range */
384 if (offset < K3RTC_MIN_OFFSET || offset > K3RTC_MAX_OFFSET)
387 /* Convert from ppb format to RTC calibration register format */
388 tmp = offset * (s64)ticks_per_hr;
390 tmp -= K3RTC_PPB_MULT / 2LL;
392 tmp += K3RTC_PPB_MULT / 2LL;
393 tmp = div_s64(tmp, K3RTC_PPB_MULT);
395 /* Offset value operates in negative way, so swap sign */
398 k3rtc_field_write(priv, K3RTC_COMP, comp);
400 return k3rtc_fence(priv);
403 static irqreturn_t ti_k3_rtc_interrupt(s32 irq, void *dev_id)
405 struct device *dev = dev_id;
406 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
411 * IRQ assertion can be very fast, however, the IRQ Status clear
412 * de-assert depends on 32k clock edge in the 32k domain
413 * If we clear the status prior to the first 32k clock edge,
414 * the status bit is cleared, but the IRQ stays re-asserted.
416 * To prevent this condition, we need to wait for clock sync time.
417 * We can either do that by polling the 32k observability signal for
418 * a toggle OR we could just sleep and let the processor do other
421 usleep_range(priv->sync_timeout_us, priv->sync_timeout_us + 2);
423 /* Lets make sure that this is a valid interrupt */
424 reg = k3rtc_field_read(priv, K3RTC_IRQ_STATUS);
427 u32 raw = k3rtc_field_read(priv, K3RTC_IRQ_STATUS_RAW);
431 "Erratum i2327/IRQ trig: status: 0x%08x / 0x%08x\n", reg, raw);
436 * Write 1 to clear status reg
437 * We cannot use a field operation here due to a potential race between
438 * 32k domain and vbus domain.
440 regmap_write(priv->regmap, REG_K3RTC_IRQSTATUS_SYS, 0x1);
442 /* Sync the write in */
443 ret = k3rtc_fence(priv);
445 dev_err(dev, "Failed to fence irq status clr(%d)!\n", ret);
450 * Force the 32k status to be reloaded back in to ensure status is
451 * reflected back correctly.
453 k3rtc_field_write(priv, K3RTC_RELOAD_FROM_BBD, 0x1);
455 /* Ensure the write sync is through */
456 ret = k3rtc_fence(priv);
458 dev_err(dev, "Failed to fence reload from bbd(%d)!\n", ret);
462 /* Now we ensure that the status bit is cleared */
463 ret = regmap_field_read_poll_timeout(priv->r_fields[K3RTC_IRQ_STATUS],
464 ret, !ret, 2, priv->sync_timeout_us);
466 dev_err(dev, "Time out waiting for status clear\n");
470 /* Notify RTC core on event */
471 rtc_update_irq(priv->rtc_dev, 1, RTC_IRQF | RTC_AF);
476 static const struct rtc_class_ops ti_k3_rtc_ops = {
477 .read_time = ti_k3_rtc_read_time,
478 .set_time = ti_k3_rtc_set_time,
479 .read_alarm = ti_k3_rtc_read_alarm,
480 .set_alarm = ti_k3_rtc_set_alarm,
481 .read_offset = ti_k3_rtc_read_offset,
482 .set_offset = ti_k3_rtc_set_offset,
483 .alarm_irq_enable = ti_k3_rtc_alarm_irq_enable,
486 static int ti_k3_rtc_scratch_read(void *priv_data, unsigned int offset,
487 void *val, size_t bytes)
489 struct ti_k3_rtc *priv = (struct ti_k3_rtc *)priv_data;
491 return regmap_bulk_read(priv->regmap, REG_K3RTC_SCRATCH0 + offset, val, bytes / 4);
494 static int ti_k3_rtc_scratch_write(void *priv_data, unsigned int offset,
495 void *val, size_t bytes)
497 struct ti_k3_rtc *priv = (struct ti_k3_rtc *)priv_data;
500 ret = regmap_bulk_write(priv->regmap, REG_K3RTC_SCRATCH0 + offset, val, bytes / 4);
504 return k3rtc_fence(priv);
507 static struct nvmem_config ti_k3_rtc_nvmem_config = {
508 .name = "ti_k3_rtc_scratch",
511 .size = REG_K3RTC_SCRATCH7 - REG_K3RTC_SCRATCH0 + 4,
512 .reg_read = ti_k3_rtc_scratch_read,
513 .reg_write = ti_k3_rtc_scratch_write,
516 static int k3rtc_get_32kclk(struct device *dev, struct ti_k3_rtc *priv)
520 clk = devm_clk_get_enabled(dev, "osc32k");
524 priv->rate_32k = clk_get_rate(clk);
526 /* Make sure we are exact 32k clock. Else, try to compensate delay */
527 if (priv->rate_32k != 32768)
528 dev_warn(dev, "Clock rate %ld is not 32768! Could misbehave!\n",
532 * Sync timeout should be two 32k clk sync cycles = ~61uS. We double
533 * it to comprehend intermediate bus segment and cpu frequency
536 priv->sync_timeout_us = (u32)(DIV_ROUND_UP_ULL(1000000, priv->rate_32k) * 4);
541 static int k3rtc_get_vbusclk(struct device *dev, struct ti_k3_rtc *priv)
545 /* Note: VBUS isn't a context clock, it is needed for hardware operation */
546 clk = devm_clk_get_enabled(dev, "vbus");
553 static int ti_k3_rtc_probe(struct platform_device *pdev)
555 struct device *dev = &pdev->dev;
556 struct ti_k3_rtc *priv;
557 void __iomem *rtc_base;
560 priv = devm_kzalloc(dev, sizeof(struct ti_k3_rtc), GFP_KERNEL);
564 rtc_base = devm_platform_ioremap_resource(pdev, 0);
565 if (IS_ERR(rtc_base))
566 return PTR_ERR(rtc_base);
568 priv->regmap = devm_regmap_init_mmio(dev, rtc_base, &ti_k3_rtc_regmap_config);
569 if (IS_ERR(priv->regmap))
570 return PTR_ERR(priv->regmap);
572 ret = devm_regmap_field_bulk_alloc(dev, priv->regmap, priv->r_fields,
573 ti_rtc_reg_fields, K3_RTC_MAX_FIELDS);
577 ret = k3rtc_get_32kclk(dev, priv);
580 ret = k3rtc_get_vbusclk(dev, priv);
584 ret = platform_get_irq(pdev, 0);
587 priv->irq = (unsigned int)ret;
589 priv->rtc_dev = devm_rtc_allocate_device(dev);
590 if (IS_ERR(priv->rtc_dev))
591 return PTR_ERR(priv->rtc_dev);
593 priv->rtc_dev->ops = &ti_k3_rtc_ops;
594 priv->rtc_dev->range_max = (1ULL << 48) - 1; /* 48Bit seconds */
595 ti_k3_rtc_nvmem_config.priv = priv;
597 ret = devm_request_threaded_irq(dev, priv->irq, NULL,
599 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
602 dev_err(dev, "Could not request IRQ: %d\n", ret);
606 platform_set_drvdata(pdev, priv);
608 ret = k3rtc_configure(dev);
612 if (device_property_present(dev, "wakeup-source"))
613 device_init_wakeup(dev, true);
615 device_set_wakeup_capable(dev, true);
617 ret = devm_rtc_register_device(priv->rtc_dev);
621 return devm_rtc_nvmem_register(priv->rtc_dev, &ti_k3_rtc_nvmem_config);
624 static const struct of_device_id ti_k3_rtc_of_match_table[] = {
625 {.compatible = "ti,am62-rtc" },
628 MODULE_DEVICE_TABLE(of, ti_k3_rtc_of_match_table);
630 static int __maybe_unused ti_k3_rtc_suspend(struct device *dev)
632 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
634 if (device_may_wakeup(dev))
635 return enable_irq_wake(priv->irq);
640 static int __maybe_unused ti_k3_rtc_resume(struct device *dev)
642 struct ti_k3_rtc *priv = dev_get_drvdata(dev);
644 if (device_may_wakeup(dev))
645 disable_irq_wake(priv->irq);
649 static SIMPLE_DEV_PM_OPS(ti_k3_rtc_pm_ops, ti_k3_rtc_suspend, ti_k3_rtc_resume);
651 static struct platform_driver ti_k3_rtc_driver = {
652 .probe = ti_k3_rtc_probe,
655 .of_match_table = ti_k3_rtc_of_match_table,
656 .pm = &ti_k3_rtc_pm_ops,
659 module_platform_driver(ti_k3_rtc_driver);
661 MODULE_LICENSE("GPL");
662 MODULE_DESCRIPTION("TI K3 RTC driver");
663 MODULE_AUTHOR("Nishanth Menon");