1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2021, Xilinx, Inc.
6 #define LOG_CATEGORY UCLASS_RTC
13 #define RTC_SET_TM_WR 0x00
14 #define RTC_SET_TM_RD 0x04
15 #define RTC_CALIB_WR 0x08
16 #define RTC_CUR_TM 0x10
17 #define RTC_INT_STS 0x20
20 #define RTC_INT_SEC BIT(0)
21 #define RTC_BATT_EN BIT(31)
22 #define RTC_CALIB_DEF 0x198233
23 #define RTC_CALIB_MASK 0x1FFFFF
25 struct zynqmp_rtc_priv {
27 unsigned int calibval;
30 static int zynqmp_rtc_get(struct udevice *dev, struct rtc_time *tm)
32 struct zynqmp_rtc_priv *priv = dev_get_priv(dev);
34 unsigned long read_time;
36 status = readl(priv->base + RTC_INT_STS);
38 if (status & RTC_INT_SEC) {
40 * RTC has updated the CURRENT_TIME with the time written into
41 * SET_TIME_WRITE register.
43 read_time = readl(priv->base + RTC_CUR_TM);
46 * Time written in SET_TIME_WRITE has not yet updated into
47 * the seconds read register, so read the time from the
48 * SET_TIME_WRITE instead of CURRENT_TIME register.
49 * Since we add +1 sec while writing, we need to -1 sec while
52 read_time = readl(priv->base + RTC_SET_TM_RD) - 1;
55 rtc_to_tm(read_time, tm);
60 static int zynqmp_rtc_set(struct udevice *dev, const struct rtc_time *tm)
62 struct zynqmp_rtc_priv *priv = dev_get_priv(dev);
63 unsigned long new_time = 0;
67 * The value written will be updated after 1 sec into the
68 * seconds read register, so we need to program time +1 sec
69 * to get the correct time on read.
71 new_time = rtc_mktime(tm) + 1;
74 * Writing into calibration register will clear the Tick Counter and
75 * force the next second to be signaled exactly in 1 second period
77 priv->calibval &= RTC_CALIB_MASK;
78 writel(priv->calibval, (priv->base + RTC_CALIB_WR));
80 writel(new_time, priv->base + RTC_SET_TM_WR);
83 * Clear the rtc interrupt status register after setting the
84 * time. During a read_time function, the code should read the
85 * RTC_INT_STATUS register and if bit 0 is still 0, it means
86 * that one second has not elapsed yet since RTC was set and
87 * the current time should be read from SET_TIME_READ register;
88 * otherwise, CURRENT_TIME register is read to report the time
90 writel(RTC_INT_SEC, priv->base + RTC_INT_STS);
95 static int zynqmp_rtc_reset(struct udevice *dev)
97 return zynqmp_rtc_set(dev, NULL);
100 static int zynqmp_rtc_init(struct udevice *dev)
102 struct zynqmp_rtc_priv *priv = dev_get_priv(dev);
105 /* Enable RTC switch to battery when VCC_PSAUX is not available */
106 rtc_ctrl = readl(priv->base + RTC_CTRL);
107 rtc_ctrl |= RTC_BATT_EN;
108 writel(rtc_ctrl, priv->base + RTC_CTRL);
111 * Based on crystal freq of 33.330 KHz
112 * set the seconds counter and enable, set fractions counter
113 * to default value suggested as per design spec
114 * to correct RTC delay in frequency over period of time.
116 priv->calibval &= RTC_CALIB_MASK;
117 writel(priv->calibval, (priv->base + RTC_CALIB_WR));
122 static int zynqmp_rtc_probe(struct udevice *dev)
124 struct zynqmp_rtc_priv *priv = dev_get_priv(dev);
127 priv->base = dev_read_addr(dev);
128 if (priv->base == FDT_ADDR_T_NONE)
131 priv->calibval = dev_read_u32_default(dev, "calibration",
134 ret = zynqmp_rtc_init(dev);
139 static const struct rtc_ops zynqmp_rtc_ops = {
140 .get = zynqmp_rtc_get,
141 .set = zynqmp_rtc_set,
142 .reset = zynqmp_rtc_reset,
145 static const struct udevice_id zynqmp_rtc_ids[] = {
146 { .compatible = "xlnx,zynqmp-rtc" },
150 U_BOOT_DRIVER(rtc_zynqmp) = {
151 .name = "rtc-zynqmp",
153 .probe = zynqmp_rtc_probe,
154 .of_match = zynqmp_rtc_ids,
155 .ops = &zynqmp_rtc_ops,
156 .priv_auto = sizeof(struct zynqmp_rtc_priv),