1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RTC driver for the Armada 38x Marvell SoCs
5 * Copyright (C) 2015 Marvell
7 * Gregory Clement <gregory.clement@free-electrons.com>
10 #include <linux/delay.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/rtc.h>
18 #define RTC_STATUS 0x0
19 #define RTC_STATUS_ALARM1 BIT(0)
20 #define RTC_STATUS_ALARM2 BIT(1)
21 #define RTC_IRQ1_CONF 0x4
22 #define RTC_IRQ2_CONF 0x8
23 #define RTC_IRQ_AL_EN BIT(0)
24 #define RTC_IRQ_FREQ_EN BIT(1)
25 #define RTC_IRQ_FREQ_1HZ BIT(2)
27 #define RTC_CCR_MODE BIT(15)
28 #define RTC_CONF_TEST 0x1C
29 #define RTC_NOMINAL_TIMING BIT(13)
32 #define RTC_ALARM1 0x10
33 #define RTC_ALARM2 0x14
35 /* Armada38x SoC registers */
36 #define RTC_38X_BRIDGE_TIMING_CTL 0x0
37 #define RTC_38X_PERIOD_OFFS 0
38 #define RTC_38X_PERIOD_MASK (0x3FF << RTC_38X_PERIOD_OFFS)
39 #define RTC_38X_READ_DELAY_OFFS 26
40 #define RTC_38X_READ_DELAY_MASK (0x1F << RTC_38X_READ_DELAY_OFFS)
42 /* Armada 7K/8K registers */
43 #define RTC_8K_BRIDGE_TIMING_CTL0 0x0
44 #define RTC_8K_WRCLK_PERIOD_OFFS 0
45 #define RTC_8K_WRCLK_PERIOD_MASK (0xFFFF << RTC_8K_WRCLK_PERIOD_OFFS)
46 #define RTC_8K_WRCLK_SETUP_OFFS 16
47 #define RTC_8K_WRCLK_SETUP_MASK (0xFFFF << RTC_8K_WRCLK_SETUP_OFFS)
48 #define RTC_8K_BRIDGE_TIMING_CTL1 0x4
49 #define RTC_8K_READ_DELAY_OFFS 0
50 #define RTC_8K_READ_DELAY_MASK (0xFFFF << RTC_8K_READ_DELAY_OFFS)
52 #define RTC_8K_ISR 0x10
53 #define RTC_8K_IMR 0x14
54 #define RTC_8K_ALARM2 BIT(0)
56 #define SOC_RTC_INTERRUPT 0x8
57 #define SOC_RTC_ALARM1 BIT(0)
58 #define SOC_RTC_ALARM2 BIT(1)
59 #define SOC_RTC_ALARM1_MASK BIT(2)
60 #define SOC_RTC_ALARM2_MASK BIT(3)
64 struct value_to_freq {
69 struct armada38x_rtc {
70 struct rtc_device *rtc_dev;
72 void __iomem *regs_soc;
76 struct value_to_freq *val_to_freq;
77 const struct armada38x_rtc_data *data;
83 #define ALARM_REG(base, alarm) ((base) + (alarm) * sizeof(u32))
85 struct armada38x_rtc_data {
86 /* Initialize the RTC-MBUS bridge timing */
87 void (*update_mbus_timing)(struct armada38x_rtc *rtc);
88 u32 (*read_rtc_reg)(struct armada38x_rtc *rtc, u8 rtc_reg);
89 void (*clear_isr)(struct armada38x_rtc *rtc);
90 void (*unmask_interrupt)(struct armada38x_rtc *rtc);
95 * According to the datasheet, the OS should wait 5us after every
96 * register write to the RTC hard macro so that the required update
97 * can occur without holding off the system bus
98 * According to errata RES-3124064, Write to any RTC register
99 * may fail. As a workaround, before writing to RTC
100 * register, issue a dummy write of 0x0 twice to RTC Status
104 static void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
106 writel(0, rtc->regs + RTC_STATUS);
107 writel(0, rtc->regs + RTC_STATUS);
108 writel(val, rtc->regs + offset);
112 /* Update RTC-MBUS bridge timing parameters */
113 static void rtc_update_38x_mbus_timing_params(struct armada38x_rtc *rtc)
117 reg = readl(rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
118 reg &= ~RTC_38X_PERIOD_MASK;
119 reg |= 0x3FF << RTC_38X_PERIOD_OFFS; /* Maximum value */
120 reg &= ~RTC_38X_READ_DELAY_MASK;
121 reg |= 0x1F << RTC_38X_READ_DELAY_OFFS; /* Maximum value */
122 writel(reg, rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
125 static void rtc_update_8k_mbus_timing_params(struct armada38x_rtc *rtc)
129 reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
130 reg &= ~RTC_8K_WRCLK_PERIOD_MASK;
131 reg |= 0x3FF << RTC_8K_WRCLK_PERIOD_OFFS;
132 reg &= ~RTC_8K_WRCLK_SETUP_MASK;
133 reg |= 0x29 << RTC_8K_WRCLK_SETUP_OFFS;
134 writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
136 reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
137 reg &= ~RTC_8K_READ_DELAY_MASK;
138 reg |= 0x3F << RTC_8K_READ_DELAY_OFFS;
139 writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
142 static u32 read_rtc_register(struct armada38x_rtc *rtc, u8 rtc_reg)
144 return readl(rtc->regs + rtc_reg);
147 static u32 read_rtc_register_38x_wa(struct armada38x_rtc *rtc, u8 rtc_reg)
149 int i, index_max = 0, max = 0;
151 for (i = 0; i < SAMPLE_NR; i++) {
152 rtc->val_to_freq[i].value = readl(rtc->regs + rtc_reg);
153 rtc->val_to_freq[i].freq = 0;
156 for (i = 0; i < SAMPLE_NR; i++) {
158 u32 value = rtc->val_to_freq[i].value;
160 while (rtc->val_to_freq[j].freq) {
161 if (rtc->val_to_freq[j].value == value) {
162 rtc->val_to_freq[j].freq++;
168 if (!rtc->val_to_freq[j].freq) {
169 rtc->val_to_freq[j].value = value;
170 rtc->val_to_freq[j].freq = 1;
173 if (rtc->val_to_freq[j].freq > max) {
175 max = rtc->val_to_freq[j].freq;
179 * If a value already has half of the sample this is the most
180 * frequent one and we can stop the research right now
182 if (max > SAMPLE_NR / 2)
186 return rtc->val_to_freq[index_max].value;
189 static void armada38x_clear_isr(struct armada38x_rtc *rtc)
191 u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
193 writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
196 static void armada38x_unmask_interrupt(struct armada38x_rtc *rtc)
198 u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
200 writel(val | SOC_RTC_ALARM1_MASK, rtc->regs_soc + SOC_RTC_INTERRUPT);
203 static void armada8k_clear_isr(struct armada38x_rtc *rtc)
205 writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_ISR);
208 static void armada8k_unmask_interrupt(struct armada38x_rtc *rtc)
210 writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_IMR);
213 static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
215 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
216 unsigned long time, flags;
218 spin_lock_irqsave(&rtc->lock, flags);
219 time = rtc->data->read_rtc_reg(rtc, RTC_TIME);
220 spin_unlock_irqrestore(&rtc->lock, flags);
222 rtc_time64_to_tm(time, tm);
227 static void armada38x_rtc_reset(struct armada38x_rtc *rtc)
231 reg = rtc->data->read_rtc_reg(rtc, RTC_CONF_TEST);
232 /* If bits [7:0] are non-zero, assume RTC was uninitialized */
234 rtc_delayed_write(0, rtc, RTC_CONF_TEST);
235 msleep(500); /* Oscillator startup time */
236 rtc_delayed_write(0, rtc, RTC_TIME);
237 rtc_delayed_write(SOC_RTC_ALARM1 | SOC_RTC_ALARM2, rtc,
239 rtc_delayed_write(RTC_NOMINAL_TIMING, rtc, RTC_CCR);
241 rtc->initialized = true;
244 static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
246 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
247 unsigned long time, flags;
249 time = rtc_tm_to_time64(tm);
251 if (!rtc->initialized)
252 armada38x_rtc_reset(rtc);
254 spin_lock_irqsave(&rtc->lock, flags);
255 rtc_delayed_write(time, rtc, RTC_TIME);
256 spin_unlock_irqrestore(&rtc->lock, flags);
261 static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
263 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
264 unsigned long time, flags;
265 u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
266 u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
269 spin_lock_irqsave(&rtc->lock, flags);
271 time = rtc->data->read_rtc_reg(rtc, reg);
272 val = rtc->data->read_rtc_reg(rtc, reg_irq) & RTC_IRQ_AL_EN;
274 spin_unlock_irqrestore(&rtc->lock, flags);
276 alrm->enabled = val ? 1 : 0;
277 rtc_time64_to_tm(time, &alrm->time);
282 static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
284 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
285 u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
286 u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
287 unsigned long time, flags;
289 time = rtc_tm_to_time64(&alrm->time);
291 spin_lock_irqsave(&rtc->lock, flags);
293 rtc_delayed_write(time, rtc, reg);
296 rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
297 rtc->data->unmask_interrupt(rtc);
300 spin_unlock_irqrestore(&rtc->lock, flags);
305 static int armada38x_rtc_alarm_irq_enable(struct device *dev,
306 unsigned int enabled)
308 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
309 u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
312 spin_lock_irqsave(&rtc->lock, flags);
315 rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
317 rtc_delayed_write(0, rtc, reg_irq);
319 spin_unlock_irqrestore(&rtc->lock, flags);
324 static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
326 struct armada38x_rtc *rtc = data;
328 int event = RTC_IRQF | RTC_AF;
329 u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
331 dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
333 spin_lock(&rtc->lock);
335 rtc->data->clear_isr(rtc);
336 val = rtc->data->read_rtc_reg(rtc, reg_irq);
337 /* disable all the interrupts for alarm*/
338 rtc_delayed_write(0, rtc, reg_irq);
340 rtc_delayed_write(1 << rtc->data->alarm, rtc, RTC_STATUS);
342 spin_unlock(&rtc->lock);
344 if (val & RTC_IRQ_FREQ_EN) {
345 if (val & RTC_IRQ_FREQ_1HZ)
351 rtc_update_irq(rtc->rtc_dev, 1, event);
357 * The information given in the Armada 388 functional spec is complex.
358 * They give two different formulas for calculating the offset value,
359 * but when considering "Offset" as an 8-bit signed integer, they both
360 * reduce down to (we shall rename "Offset" as "val" here):
362 * val = (f_ideal / f_measured - 1) / resolution where f_ideal = 32768
364 * Converting to time, f = 1/t:
365 * val = (t_measured / t_ideal - 1) / resolution where t_ideal = 1/32768
367 * => t_measured / t_ideal = val * resolution + 1
369 * "offset" in the RTC interface is defined as:
370 * t = t0 * (1 + offset * 1e-9)
371 * where t is the desired period, t0 is the measured period with a zero
372 * offset, which is t_measured above. With t0 = t_measured and t = t_ideal,
373 * offset = (t_ideal / t_measured - 1) / 1e-9
375 * => t_ideal / t_measured = offset * 1e-9 + 1
379 * offset * 1e-9 + 1 = 1 / (val * resolution + 1)
381 * We want "resolution" to be an integer, so resolution = R * 1e-9, giving
382 * offset = 1e18 / (val * R + 1e9) - 1e9
383 * val = (1e18 / (offset + 1e9) - 1e9) / R
384 * with a common transformation:
385 * f(x) = 1e18 / (x + 1e9) - 1e9
386 * offset = f(val * R)
387 * val = f(offset) / R
389 * Armada 38x supports two modes, fine mode (954ppb) and coarse mode (3815ppb).
391 static long armada38x_ppb_convert(long ppb)
393 long div = ppb + 1000000000L;
395 return div_s64(1000000000000000000LL + div / 2, div) - 1000000000L;
398 static int armada38x_rtc_read_offset(struct device *dev, long *offset)
400 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
401 unsigned long ccr, flags;
404 spin_lock_irqsave(&rtc->lock, flags);
405 ccr = rtc->data->read_rtc_reg(rtc, RTC_CCR);
406 spin_unlock_irqrestore(&rtc->lock, flags);
408 ppb_cor = (ccr & RTC_CCR_MODE ? 3815 : 954) * (s8)ccr;
409 /* ppb_cor + 1000000000L can never be zero */
410 *offset = armada38x_ppb_convert(ppb_cor);
415 static int armada38x_rtc_set_offset(struct device *dev, long offset)
417 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
418 unsigned long ccr = 0;
422 * The maximum ppb_cor is -128 * 3815 .. 127 * 3815, but we
423 * need to clamp the input. This equates to -484270 .. 488558.
424 * Not only is this to stop out of range "off" but also to
425 * avoid the division by zero in armada38x_ppb_convert().
427 offset = clamp(offset, -484270L, 488558L);
429 ppb_cor = armada38x_ppb_convert(offset);
432 * Use low update mode where possible, which gives a better
433 * resolution of correction.
435 off = DIV_ROUND_CLOSEST(ppb_cor, 954);
436 if (off > 127 || off < -128) {
438 off = DIV_ROUND_CLOSEST(ppb_cor, 3815);
442 * Armada 388 requires a bit pattern in bits 14..8 depending on
443 * the sign bit: { 0, ~S, S, S, S, S, S }
445 ccr |= (off & 0x3fff) ^ 0x2000;
446 rtc_delayed_write(ccr, rtc, RTC_CCR);
451 static const struct rtc_class_ops armada38x_rtc_ops = {
452 .read_time = armada38x_rtc_read_time,
453 .set_time = armada38x_rtc_set_time,
454 .read_alarm = armada38x_rtc_read_alarm,
455 .set_alarm = armada38x_rtc_set_alarm,
456 .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
457 .read_offset = armada38x_rtc_read_offset,
458 .set_offset = armada38x_rtc_set_offset,
461 static const struct armada38x_rtc_data armada38x_data = {
462 .update_mbus_timing = rtc_update_38x_mbus_timing_params,
463 .read_rtc_reg = read_rtc_register_38x_wa,
464 .clear_isr = armada38x_clear_isr,
465 .unmask_interrupt = armada38x_unmask_interrupt,
469 static const struct armada38x_rtc_data armada8k_data = {
470 .update_mbus_timing = rtc_update_8k_mbus_timing_params,
471 .read_rtc_reg = read_rtc_register,
472 .clear_isr = armada8k_clear_isr,
473 .unmask_interrupt = armada8k_unmask_interrupt,
478 static const struct of_device_id armada38x_rtc_of_match_table[] = {
480 .compatible = "marvell,armada-380-rtc",
481 .data = &armada38x_data,
484 .compatible = "marvell,armada-8k-rtc",
485 .data = &armada8k_data,
489 MODULE_DEVICE_TABLE(of, armada38x_rtc_of_match_table);
492 static __init int armada38x_rtc_probe(struct platform_device *pdev)
494 struct resource *res;
495 struct armada38x_rtc *rtc;
497 rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
502 rtc->data = of_device_get_match_data(&pdev->dev);
504 rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
505 sizeof(struct value_to_freq), GFP_KERNEL);
506 if (!rtc->val_to_freq)
509 spin_lock_init(&rtc->lock);
511 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
512 rtc->regs = devm_ioremap_resource(&pdev->dev, res);
513 if (IS_ERR(rtc->regs))
514 return PTR_ERR(rtc->regs);
515 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
516 rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
517 if (IS_ERR(rtc->regs_soc))
518 return PTR_ERR(rtc->regs_soc);
520 rtc->irq = platform_get_irq(pdev, 0);
524 rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
525 if (IS_ERR(rtc->rtc_dev))
526 return PTR_ERR(rtc->rtc_dev);
528 if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
529 0, pdev->name, rtc) < 0) {
530 dev_warn(&pdev->dev, "Interrupt not available.\n");
533 platform_set_drvdata(pdev, rtc);
536 device_init_wakeup(&pdev->dev, 1);
538 clear_bit(RTC_FEATURE_ALARM, rtc->rtc_dev->features);
540 /* Update RTC-MBUS bridge timing parameters */
541 rtc->data->update_mbus_timing(rtc);
543 rtc->rtc_dev->ops = &armada38x_rtc_ops;
544 rtc->rtc_dev->range_max = U32_MAX;
546 return devm_rtc_register_device(rtc->rtc_dev);
549 #ifdef CONFIG_PM_SLEEP
550 static int armada38x_rtc_suspend(struct device *dev)
552 if (device_may_wakeup(dev)) {
553 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
555 return enable_irq_wake(rtc->irq);
561 static int armada38x_rtc_resume(struct device *dev)
563 if (device_may_wakeup(dev)) {
564 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
566 /* Update RTC-MBUS bridge timing parameters */
567 rtc->data->update_mbus_timing(rtc);
569 return disable_irq_wake(rtc->irq);
576 static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
577 armada38x_rtc_suspend, armada38x_rtc_resume);
579 static struct platform_driver armada38x_rtc_driver = {
581 .name = "armada38x-rtc",
582 .pm = &armada38x_rtc_pm_ops,
583 .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
587 module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
589 MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
590 MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
591 MODULE_LICENSE("GPL");