1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Xilinx Zynq Ultrascale+ MPSoC Real Time Clock Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2015 Xilinx, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/io.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/rtc.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /* RTC Registers */
18*4882a593Smuzhiyun #define RTC_SET_TM_WR 0x00
19*4882a593Smuzhiyun #define RTC_SET_TM_RD 0x04
20*4882a593Smuzhiyun #define RTC_CALIB_WR 0x08
21*4882a593Smuzhiyun #define RTC_CALIB_RD 0x0C
22*4882a593Smuzhiyun #define RTC_CUR_TM 0x10
23*4882a593Smuzhiyun #define RTC_CUR_TICK 0x14
24*4882a593Smuzhiyun #define RTC_ALRM 0x18
25*4882a593Smuzhiyun #define RTC_INT_STS 0x20
26*4882a593Smuzhiyun #define RTC_INT_MASK 0x24
27*4882a593Smuzhiyun #define RTC_INT_EN 0x28
28*4882a593Smuzhiyun #define RTC_INT_DIS 0x2C
29*4882a593Smuzhiyun #define RTC_CTRL 0x40
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define RTC_FR_EN BIT(20)
32*4882a593Smuzhiyun #define RTC_FR_DATSHIFT 16
33*4882a593Smuzhiyun #define RTC_TICK_MASK 0xFFFF
34*4882a593Smuzhiyun #define RTC_INT_SEC BIT(0)
35*4882a593Smuzhiyun #define RTC_INT_ALRM BIT(1)
36*4882a593Smuzhiyun #define RTC_OSC_EN BIT(24)
37*4882a593Smuzhiyun #define RTC_BATT_EN BIT(31)
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define RTC_CALIB_DEF 0x198233
40*4882a593Smuzhiyun #define RTC_CALIB_MASK 0x1FFFFF
41*4882a593Smuzhiyun #define RTC_ALRM_MASK BIT(1)
42*4882a593Smuzhiyun #define RTC_MSEC 1000
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct xlnx_rtc_dev {
45*4882a593Smuzhiyun struct rtc_device *rtc;
46*4882a593Smuzhiyun void __iomem *reg_base;
47*4882a593Smuzhiyun int alarm_irq;
48*4882a593Smuzhiyun int sec_irq;
49*4882a593Smuzhiyun unsigned int calibval;
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
xlnx_rtc_set_time(struct device * dev,struct rtc_time * tm)52*4882a593Smuzhiyun static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
55*4882a593Smuzhiyun unsigned long new_time;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * The value written will be updated after 1 sec into the
59*4882a593Smuzhiyun * seconds read register, so we need to program time +1 sec
60*4882a593Smuzhiyun * to get the correct time on read.
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun new_time = rtc_tm_to_time64(tm) + 1;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun * Writing into calibration register will clear the Tick Counter and
66*4882a593Smuzhiyun * force the next second to be signaled exactly in 1 second period
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun xrtcdev->calibval &= RTC_CALIB_MASK;
69*4882a593Smuzhiyun writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun writel(new_time, xrtcdev->reg_base + RTC_SET_TM_WR);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun * Clear the rtc interrupt status register after setting the
75*4882a593Smuzhiyun * time. During a read_time function, the code should read the
76*4882a593Smuzhiyun * RTC_INT_STATUS register and if bit 0 is still 0, it means
77*4882a593Smuzhiyun * that one second has not elapsed yet since RTC was set and
78*4882a593Smuzhiyun * the current time should be read from SET_TIME_READ register;
79*4882a593Smuzhiyun * otherwise, CURRENT_TIME register is read to report the time
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun writel(RTC_INT_SEC, xrtcdev->reg_base + RTC_INT_STS);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun return 0;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
xlnx_rtc_read_time(struct device * dev,struct rtc_time * tm)86*4882a593Smuzhiyun static int xlnx_rtc_read_time(struct device *dev, struct rtc_time *tm)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun u32 status;
89*4882a593Smuzhiyun unsigned long read_time;
90*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun status = readl(xrtcdev->reg_base + RTC_INT_STS);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if (status & RTC_INT_SEC) {
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun * RTC has updated the CURRENT_TIME with the time written into
97*4882a593Smuzhiyun * SET_TIME_WRITE register.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun read_time = readl(xrtcdev->reg_base + RTC_CUR_TM);
100*4882a593Smuzhiyun } else {
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * Time written in SET_TIME_WRITE has not yet updated into
103*4882a593Smuzhiyun * the seconds read register, so read the time from the
104*4882a593Smuzhiyun * SET_TIME_WRITE instead of CURRENT_TIME register.
105*4882a593Smuzhiyun * Since we add +1 sec while writing, we need to -1 sec while
106*4882a593Smuzhiyun * reading.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun read_time = readl(xrtcdev->reg_base + RTC_SET_TM_RD) - 1;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun rtc_time64_to_tm(read_time, tm);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
xlnx_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)115*4882a593Smuzhiyun static int xlnx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun rtc_time64_to_tm(readl(xrtcdev->reg_base + RTC_ALRM), &alrm->time);
120*4882a593Smuzhiyun alrm->enabled = readl(xrtcdev->reg_base + RTC_INT_MASK) & RTC_INT_ALRM;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
xlnx_rtc_alarm_irq_enable(struct device * dev,u32 enabled)125*4882a593Smuzhiyun static int xlnx_rtc_alarm_irq_enable(struct device *dev, u32 enabled)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
128*4882a593Smuzhiyun unsigned int status;
129*4882a593Smuzhiyun ulong timeout;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun timeout = jiffies + msecs_to_jiffies(RTC_MSEC);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (enabled) {
134*4882a593Smuzhiyun while (1) {
135*4882a593Smuzhiyun status = readl(xrtcdev->reg_base + RTC_INT_STS);
136*4882a593Smuzhiyun if (!((status & RTC_ALRM_MASK) == RTC_ALRM_MASK))
137*4882a593Smuzhiyun break;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if (time_after_eq(jiffies, timeout)) {
140*4882a593Smuzhiyun dev_err(dev, "Time out occur, while clearing alarm status bit\n");
141*4882a593Smuzhiyun return -ETIMEDOUT;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_STS);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_EN);
147*4882a593Smuzhiyun } else {
148*4882a593Smuzhiyun writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_DIS);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun return 0;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
xlnx_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)154*4882a593Smuzhiyun static int xlnx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
157*4882a593Smuzhiyun unsigned long alarm_time;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun alarm_time = rtc_tm_to_time64(&alrm->time);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun writel((u32)alarm_time, (xrtcdev->reg_base + RTC_ALRM));
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun xlnx_rtc_alarm_irq_enable(dev, alrm->enabled);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun return 0;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
xlnx_init_rtc(struct xlnx_rtc_dev * xrtcdev)168*4882a593Smuzhiyun static void xlnx_init_rtc(struct xlnx_rtc_dev *xrtcdev)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun u32 rtc_ctrl;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* Enable RTC switch to battery when VCC_PSAUX is not available */
173*4882a593Smuzhiyun rtc_ctrl = readl(xrtcdev->reg_base + RTC_CTRL);
174*4882a593Smuzhiyun rtc_ctrl |= RTC_BATT_EN;
175*4882a593Smuzhiyun writel(rtc_ctrl, xrtcdev->reg_base + RTC_CTRL);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * Based on crystal freq of 33.330 KHz
179*4882a593Smuzhiyun * set the seconds counter and enable, set fractions counter
180*4882a593Smuzhiyun * to default value suggested as per design spec
181*4882a593Smuzhiyun * to correct RTC delay in frequency over period of time.
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun xrtcdev->calibval &= RTC_CALIB_MASK;
184*4882a593Smuzhiyun writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun static const struct rtc_class_ops xlnx_rtc_ops = {
188*4882a593Smuzhiyun .set_time = xlnx_rtc_set_time,
189*4882a593Smuzhiyun .read_time = xlnx_rtc_read_time,
190*4882a593Smuzhiyun .read_alarm = xlnx_rtc_read_alarm,
191*4882a593Smuzhiyun .set_alarm = xlnx_rtc_set_alarm,
192*4882a593Smuzhiyun .alarm_irq_enable = xlnx_rtc_alarm_irq_enable,
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun
xlnx_rtc_interrupt(int irq,void * id)195*4882a593Smuzhiyun static irqreturn_t xlnx_rtc_interrupt(int irq, void *id)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = (struct xlnx_rtc_dev *)id;
198*4882a593Smuzhiyun unsigned int status;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun status = readl(xrtcdev->reg_base + RTC_INT_STS);
201*4882a593Smuzhiyun /* Check if interrupt asserted */
202*4882a593Smuzhiyun if (!(status & (RTC_INT_SEC | RTC_INT_ALRM)))
203*4882a593Smuzhiyun return IRQ_NONE;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /* Disable RTC_INT_ALRM interrupt only */
206*4882a593Smuzhiyun writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_DIS);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (status & RTC_INT_ALRM)
209*4882a593Smuzhiyun rtc_update_irq(xrtcdev->rtc, 1, RTC_IRQF | RTC_AF);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun return IRQ_HANDLED;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
xlnx_rtc_probe(struct platform_device * pdev)214*4882a593Smuzhiyun static int xlnx_rtc_probe(struct platform_device *pdev)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev;
217*4882a593Smuzhiyun int ret;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun xrtcdev = devm_kzalloc(&pdev->dev, sizeof(*xrtcdev), GFP_KERNEL);
220*4882a593Smuzhiyun if (!xrtcdev)
221*4882a593Smuzhiyun return -ENOMEM;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun platform_set_drvdata(pdev, xrtcdev);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun xrtcdev->rtc = devm_rtc_allocate_device(&pdev->dev);
226*4882a593Smuzhiyun if (IS_ERR(xrtcdev->rtc))
227*4882a593Smuzhiyun return PTR_ERR(xrtcdev->rtc);
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun xrtcdev->rtc->ops = &xlnx_rtc_ops;
230*4882a593Smuzhiyun xrtcdev->rtc->range_max = U32_MAX;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun xrtcdev->reg_base = devm_platform_ioremap_resource(pdev, 0);
233*4882a593Smuzhiyun if (IS_ERR(xrtcdev->reg_base))
234*4882a593Smuzhiyun return PTR_ERR(xrtcdev->reg_base);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun xrtcdev->alarm_irq = platform_get_irq_byname(pdev, "alarm");
237*4882a593Smuzhiyun if (xrtcdev->alarm_irq < 0)
238*4882a593Smuzhiyun return xrtcdev->alarm_irq;
239*4882a593Smuzhiyun ret = devm_request_irq(&pdev->dev, xrtcdev->alarm_irq,
240*4882a593Smuzhiyun xlnx_rtc_interrupt, 0,
241*4882a593Smuzhiyun dev_name(&pdev->dev), xrtcdev);
242*4882a593Smuzhiyun if (ret) {
243*4882a593Smuzhiyun dev_err(&pdev->dev, "request irq failed\n");
244*4882a593Smuzhiyun return ret;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun xrtcdev->sec_irq = platform_get_irq_byname(pdev, "sec");
248*4882a593Smuzhiyun if (xrtcdev->sec_irq < 0)
249*4882a593Smuzhiyun return xrtcdev->sec_irq;
250*4882a593Smuzhiyun ret = devm_request_irq(&pdev->dev, xrtcdev->sec_irq,
251*4882a593Smuzhiyun xlnx_rtc_interrupt, 0,
252*4882a593Smuzhiyun dev_name(&pdev->dev), xrtcdev);
253*4882a593Smuzhiyun if (ret) {
254*4882a593Smuzhiyun dev_err(&pdev->dev, "request irq failed\n");
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun ret = of_property_read_u32(pdev->dev.of_node, "calibration",
259*4882a593Smuzhiyun &xrtcdev->calibval);
260*4882a593Smuzhiyun if (ret)
261*4882a593Smuzhiyun xrtcdev->calibval = RTC_CALIB_DEF;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun xlnx_init_rtc(xrtcdev);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun device_init_wakeup(&pdev->dev, 1);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun return rtc_register_device(xrtcdev->rtc);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
xlnx_rtc_remove(struct platform_device * pdev)270*4882a593Smuzhiyun static int xlnx_rtc_remove(struct platform_device *pdev)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun xlnx_rtc_alarm_irq_enable(&pdev->dev, 0);
273*4882a593Smuzhiyun device_init_wakeup(&pdev->dev, 0);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun return 0;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
xlnx_rtc_suspend(struct device * dev)278*4882a593Smuzhiyun static int __maybe_unused xlnx_rtc_suspend(struct device *dev)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun if (device_may_wakeup(dev))
283*4882a593Smuzhiyun enable_irq_wake(xrtcdev->alarm_irq);
284*4882a593Smuzhiyun else
285*4882a593Smuzhiyun xlnx_rtc_alarm_irq_enable(dev, 0);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun return 0;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
xlnx_rtc_resume(struct device * dev)290*4882a593Smuzhiyun static int __maybe_unused xlnx_rtc_resume(struct device *dev)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun if (device_may_wakeup(dev))
295*4882a593Smuzhiyun disable_irq_wake(xrtcdev->alarm_irq);
296*4882a593Smuzhiyun else
297*4882a593Smuzhiyun xlnx_rtc_alarm_irq_enable(dev, 1);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun return 0;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(xlnx_rtc_pm_ops, xlnx_rtc_suspend, xlnx_rtc_resume);
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun static const struct of_device_id xlnx_rtc_of_match[] = {
305*4882a593Smuzhiyun {.compatible = "xlnx,zynqmp-rtc" },
306*4882a593Smuzhiyun { }
307*4882a593Smuzhiyun };
308*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, xlnx_rtc_of_match);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun static struct platform_driver xlnx_rtc_driver = {
311*4882a593Smuzhiyun .probe = xlnx_rtc_probe,
312*4882a593Smuzhiyun .remove = xlnx_rtc_remove,
313*4882a593Smuzhiyun .driver = {
314*4882a593Smuzhiyun .name = KBUILD_MODNAME,
315*4882a593Smuzhiyun .pm = &xlnx_rtc_pm_ops,
316*4882a593Smuzhiyun .of_match_table = xlnx_rtc_of_match,
317*4882a593Smuzhiyun },
318*4882a593Smuzhiyun };
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun module_platform_driver(xlnx_rtc_driver);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun MODULE_DESCRIPTION("Xilinx Zynq MPSoC RTC driver");
323*4882a593Smuzhiyun MODULE_AUTHOR("Xilinx Inc.");
324*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
325