1 // SPDX-License-Identifier: GPL-2.0+
3 * RTC driver for the Armada 38x Marvell SoCs
7 * Based on Linux' driver by Gregory Clement and Marvell
12 #include <linux/delay.h>
15 #define RTC_STATUS 0x0
17 #define RTC_CONF_TEST 0x1C
19 /* Armada38x SoC registers */
20 #define RTC_38X_BRIDGE_TIMING_CTL 0x0
21 #define RTC_38X_PERIOD_OFFS 0
22 #define RTC_38X_PERIOD_MASK (0x3FF << RTC_38X_PERIOD_OFFS)
23 #define RTC_38X_READ_DELAY_OFFS 26
24 #define RTC_38X_READ_DELAY_MASK (0x1F << RTC_38X_READ_DELAY_OFFS)
28 struct armada38x_rtc {
30 void __iomem *regs_soc;
34 * According to Erratum RES-3124064 we have to do some configuration in MBUS.
35 * To read an RTC register we need to read it 100 times and return the most
37 * To write an RTC register we need to write 2x zero into STATUS register,
38 * followed by the proper write. Linux adds an 5 us delay after this, so we do
41 static void update_38x_mbus_timing_params(struct armada38x_rtc *rtc)
45 reg = readl(rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
46 reg &= ~RTC_38X_PERIOD_MASK;
47 reg |= 0x3FF << RTC_38X_PERIOD_OFFS; /* Maximum value */
48 reg &= ~RTC_38X_READ_DELAY_MASK;
49 reg |= 0x1F << RTC_38X_READ_DELAY_OFFS; /* Maximum value */
50 writel(reg, rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
53 static void armada38x_rtc_write(u32 val, struct armada38x_rtc *rtc, u8 reg)
55 writel(0, rtc->regs + RTC_STATUS);
56 writel(0, rtc->regs + RTC_STATUS);
57 writel(val, rtc->regs + reg);
61 static u32 armada38x_rtc_read(struct armada38x_rtc *rtc, u8 reg)
63 u8 counts[SAMPLE_NR], max_idx;
64 u32 samples[SAMPLE_NR], max;
67 for (i = 0, last = 0; i < SAMPLE_NR; ++i) {
68 u32 sample = readl(rtc->regs + reg);
70 /* find if this value was already read */
71 for (j = 0; j < last; ++j) {
72 if (samples[j] == sample)
77 /* if yes, increment count */
81 samples[last] = sample;
87 /* finally find the sample that was read the most */
91 for (i = 0; i < last; ++i) {
92 if (counts[i] > max) {
98 return samples[max_idx];
101 static int armada38x_rtc_get(struct udevice *dev, struct rtc_time *tm)
103 struct armada38x_rtc *rtc = dev_get_priv(dev);
106 time = armada38x_rtc_read(rtc, RTC_TIME);
113 static int armada38x_rtc_reset(struct udevice *dev)
115 struct armada38x_rtc *rtc = dev_get_priv(dev);
118 reg = armada38x_rtc_read(rtc, RTC_CONF_TEST);
121 armada38x_rtc_write(0, rtc, RTC_CONF_TEST);
123 armada38x_rtc_write(0, rtc, RTC_TIME);
124 armada38x_rtc_write(BIT(0) | BIT(1), 0, RTC_STATUS);
130 static int armada38x_rtc_set(struct udevice *dev, const struct rtc_time *tm)
132 struct armada38x_rtc *rtc = dev_get_priv(dev);
135 time = rtc_mktime(tm);
138 printf("%s: requested time to set will overflow\n", dev->name);
140 armada38x_rtc_reset(dev);
141 armada38x_rtc_write(time, rtc, RTC_TIME);
146 static int armada38x_probe(struct udevice *dev)
148 struct armada38x_rtc *rtc = dev_get_priv(dev);
150 rtc->regs = dev_remap_addr_name(dev, "rtc");
154 rtc->regs_soc = dev_remap_addr_name(dev, "rtc-soc");
158 update_38x_mbus_timing_params(rtc);
162 printf("%s: io address missing\n", dev->name);
166 static const struct rtc_ops armada38x_rtc_ops = {
167 .get = armada38x_rtc_get,
168 .set = armada38x_rtc_set,
169 .reset = armada38x_rtc_reset,
172 static const struct udevice_id armada38x_rtc_ids[] = {
173 { .compatible = "marvell,armada-380-rtc", .data = 0 },
177 U_BOOT_DRIVER(rtc_armada38x) = {
178 .name = "rtc-armada38x",
180 .of_match = armada38x_rtc_ids,
181 .probe = armada38x_probe,
182 .priv_auto = sizeof(struct armada38x_rtc),
183 .ops = &armada38x_rtc_ops,