Blame view

drivers/rtc/rtc-zynqmp.c 8.2 KB
28e7861cf   Alexandre Belloni   rtc: zynqmp: conv...
1
  // SPDX-License-Identifier: GPL-2.0
11143c19e   Suneel Garapati   rtc: add xilinx z...
2
3
4
5
6
  /*
   * Xilinx Zynq Ultrascale+ MPSoC Real Time Clock Driver
   *
   * Copyright (C) 2015 Xilinx, Inc.
   *
11143c19e   Suneel Garapati   rtc: add xilinx z...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   */
  
  #include <linux/delay.h>
  #include <linux/init.h>
  #include <linux/io.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/platform_device.h>
  #include <linux/rtc.h>
  
  /* RTC Registers */
  #define RTC_SET_TM_WR		0x00
  #define RTC_SET_TM_RD		0x04
  #define RTC_CALIB_WR		0x08
  #define RTC_CALIB_RD		0x0C
  #define RTC_CUR_TM		0x10
  #define RTC_CUR_TICK		0x14
  #define RTC_ALRM		0x18
  #define RTC_INT_STS		0x20
  #define RTC_INT_MASK		0x24
  #define RTC_INT_EN		0x28
  #define RTC_INT_DIS		0x2C
  #define RTC_CTRL		0x40
  
  #define RTC_FR_EN		BIT(20)
  #define RTC_FR_DATSHIFT		16
  #define RTC_TICK_MASK		0xFFFF
  #define RTC_INT_SEC		BIT(0)
  #define RTC_INT_ALRM		BIT(1)
  #define RTC_OSC_EN		BIT(24)
9092984f1   Anurag Kumar Vulisha   rtc: zynqmp: Enab...
37
  #define RTC_BATT_EN		BIT(31)
11143c19e   Suneel Garapati   rtc: add xilinx z...
38
39
40
  
  #define RTC_CALIB_DEF		0x198233
  #define RTC_CALIB_MASK		0x1FFFFF
4594d082d   Srinivas Neeli   rtc: zynqmp: Clea...
41
42
  #define RTC_ALRM_MASK          BIT(1)
  #define RTC_MSEC               1000
11143c19e   Suneel Garapati   rtc: add xilinx z...
43
44
45
46
47
48
  
  struct xlnx_rtc_dev {
  	struct rtc_device	*rtc;
  	void __iomem		*reg_base;
  	int			alarm_irq;
  	int			sec_irq;
d53bf24db   Srinivas Goud   rtc: xilinx: Fix ...
49
  	unsigned int		calibval;
11143c19e   Suneel Garapati   rtc: add xilinx z...
50
51
52
53
54
55
  };
  
  static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm)
  {
  	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  	unsigned long new_time;
b62c3a115   Anurag Kumar Vulisha   rtc: zynqmp: Upda...
56
57
58
59
60
61
  	/*
  	 * The value written will be updated after 1 sec into the
  	 * seconds read register, so we need to program time +1 sec
  	 * to get the correct time on read.
  	 */
  	new_time = rtc_tm_to_time64(tm) + 1;
11143c19e   Suneel Garapati   rtc: add xilinx z...
62

58c4ed3ba   Anurag Kumar Vulisha   rtc: zynqmp: Writ...
63
64
65
66
67
68
  	/*
  	 * Writing into calibration register will clear the Tick Counter and
  	 * force the next second to be signaled exactly in 1 second period
  	 */
  	xrtcdev->calibval &= RTC_CALIB_MASK;
  	writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
11143c19e   Suneel Garapati   rtc: add xilinx z...
69
  	writel(new_time, xrtcdev->reg_base + RTC_SET_TM_WR);
b62c3a115   Anurag Kumar Vulisha   rtc: zynqmp: Upda...
70
71
72
73
74
75
76
77
78
  	/*
  	 * Clear the rtc interrupt status register after setting the
  	 * time. During a read_time function, the code should read the
  	 * RTC_INT_STATUS register and if bit 0 is still 0, it means
  	 * that one second has not elapsed yet since RTC was set and
  	 * the current time should be read from SET_TIME_READ register;
  	 * otherwise, CURRENT_TIME register is read to report the time
  	 */
  	writel(RTC_INT_SEC, xrtcdev->reg_base + RTC_INT_STS);
11143c19e   Suneel Garapati   rtc: add xilinx z...
79
80
81
82
83
  	return 0;
  }
  
  static int xlnx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  {
b62c3a115   Anurag Kumar Vulisha   rtc: zynqmp: Upda...
84
85
  	u32 status;
  	unsigned long read_time;
11143c19e   Suneel Garapati   rtc: add xilinx z...
86
  	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
b62c3a115   Anurag Kumar Vulisha   rtc: zynqmp: Upda...
87
88
89
90
91
92
93
  	status = readl(xrtcdev->reg_base + RTC_INT_STS);
  
  	if (status & RTC_INT_SEC) {
  		/*
  		 * RTC has updated the CURRENT_TIME with the time written into
  		 * SET_TIME_WRITE register.
  		 */
519d63702   Jean-Francois Dagenais   rtc: zynqmp: re-u...
94
  		read_time = readl(xrtcdev->reg_base + RTC_CUR_TM);
b62c3a115   Anurag Kumar Vulisha   rtc: zynqmp: Upda...
95
96
97
98
99
100
101
102
103
  	} else {
  		/*
  		 * Time written in SET_TIME_WRITE has not yet updated into
  		 * the seconds read register, so read the time from the
  		 * SET_TIME_WRITE instead of CURRENT_TIME register.
  		 * Since we add +1 sec while writing, we need to -1 sec while
  		 * reading.
  		 */
  		read_time = readl(xrtcdev->reg_base + RTC_SET_TM_RD) - 1;
b62c3a115   Anurag Kumar Vulisha   rtc: zynqmp: Upda...
104
  	}
519d63702   Jean-Francois Dagenais   rtc: zynqmp: re-u...
105
  	rtc_time64_to_tm(read_time, tm);
11143c19e   Suneel Garapati   rtc: add xilinx z...
106

146d21bd9   Alexandre Belloni   rtc: stop validat...
107
  	return 0;
11143c19e   Suneel Garapati   rtc: add xilinx z...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  }
  
  static int xlnx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  
  	rtc_time64_to_tm(readl(xrtcdev->reg_base + RTC_ALRM), &alrm->time);
  	alrm->enabled = readl(xrtcdev->reg_base + RTC_INT_MASK) & RTC_INT_ALRM;
  
  	return 0;
  }
  
  static int xlnx_rtc_alarm_irq_enable(struct device *dev, u32 enabled)
  {
  	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
4594d082d   Srinivas Neeli   rtc: zynqmp: Clea...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  	unsigned int status;
  	ulong timeout;
  
  	timeout = jiffies + msecs_to_jiffies(RTC_MSEC);
  
  	if (enabled) {
  		while (1) {
  			status = readl(xrtcdev->reg_base + RTC_INT_STS);
  			if (!((status & RTC_ALRM_MASK) == RTC_ALRM_MASK))
  				break;
  
  			if (time_after_eq(jiffies, timeout)) {
  				dev_err(dev, "Time out occur, while clearing alarm status bit
  ");
  				return -ETIMEDOUT;
  			}
  			writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_STS);
  		}
11143c19e   Suneel Garapati   rtc: add xilinx z...
141

11143c19e   Suneel Garapati   rtc: add xilinx z...
142
  		writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_EN);
4594d082d   Srinivas Neeli   rtc: zynqmp: Clea...
143
  	} else {
11143c19e   Suneel Garapati   rtc: add xilinx z...
144
  		writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_DIS);
4594d082d   Srinivas Neeli   rtc: zynqmp: Clea...
145
  	}
11143c19e   Suneel Garapati   rtc: add xilinx z...
146
147
148
149
150
151
152
153
154
155
  
  	return 0;
  }
  
  static int xlnx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  	unsigned long alarm_time;
  
  	alarm_time = rtc_tm_to_time64(&alrm->time);
11143c19e   Suneel Garapati   rtc: add xilinx z...
156
157
158
159
160
161
  	writel((u32)alarm_time, (xrtcdev->reg_base + RTC_ALRM));
  
  	xlnx_rtc_alarm_irq_enable(dev, alrm->enabled);
  
  	return 0;
  }
58c4ed3ba   Anurag Kumar Vulisha   rtc: zynqmp: Writ...
162
  static void xlnx_init_rtc(struct xlnx_rtc_dev *xrtcdev)
11143c19e   Suneel Garapati   rtc: add xilinx z...
163
  {
9092984f1   Anurag Kumar Vulisha   rtc: zynqmp: Enab...
164
165
166
167
168
169
  	u32 rtc_ctrl;
  
  	/* Enable RTC switch to battery when VCC_PSAUX is not available */
  	rtc_ctrl = readl(xrtcdev->reg_base + RTC_CTRL);
  	rtc_ctrl |= RTC_BATT_EN;
  	writel(rtc_ctrl, xrtcdev->reg_base + RTC_CTRL);
11143c19e   Suneel Garapati   rtc: add xilinx z...
170
171
172
173
174
175
  	/*
  	 * Based on crystal freq of 33.330 KHz
  	 * set the seconds counter and enable, set fractions counter
  	 * to default value suggested as per design spec
  	 * to correct RTC delay in frequency over period of time.
  	 */
58c4ed3ba   Anurag Kumar Vulisha   rtc: zynqmp: Writ...
176
177
  	xrtcdev->calibval &= RTC_CALIB_MASK;
  	writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
11143c19e   Suneel Garapati   rtc: add xilinx z...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  }
  
  static const struct rtc_class_ops xlnx_rtc_ops = {
  	.set_time	  = xlnx_rtc_set_time,
  	.read_time	  = xlnx_rtc_read_time,
  	.read_alarm	  = xlnx_rtc_read_alarm,
  	.set_alarm	  = xlnx_rtc_set_alarm,
  	.alarm_irq_enable = xlnx_rtc_alarm_irq_enable,
  };
  
  static irqreturn_t xlnx_rtc_interrupt(int irq, void *id)
  {
  	struct xlnx_rtc_dev *xrtcdev = (struct xlnx_rtc_dev *)id;
  	unsigned int status;
  
  	status = readl(xrtcdev->reg_base + RTC_INT_STS);
  	/* Check if interrupt asserted */
  	if (!(status & (RTC_INT_SEC | RTC_INT_ALRM)))
  		return IRQ_NONE;
4594d082d   Srinivas Neeli   rtc: zynqmp: Clea...
197
198
  	/* Disable RTC_INT_ALRM interrupt only */
  	writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_DIS);
11143c19e   Suneel Garapati   rtc: add xilinx z...
199

11143c19e   Suneel Garapati   rtc: add xilinx z...
200
201
202
203
204
205
206
207
208
  	if (status & RTC_INT_ALRM)
  		rtc_update_irq(xrtcdev->rtc, 1, RTC_IRQF | RTC_AF);
  
  	return IRQ_HANDLED;
  }
  
  static int xlnx_rtc_probe(struct platform_device *pdev)
  {
  	struct xlnx_rtc_dev *xrtcdev;
11143c19e   Suneel Garapati   rtc: add xilinx z...
209
  	int ret;
11143c19e   Suneel Garapati   rtc: add xilinx z...
210
211
212
213
214
215
  
  	xrtcdev = devm_kzalloc(&pdev->dev, sizeof(*xrtcdev), GFP_KERNEL);
  	if (!xrtcdev)
  		return -ENOMEM;
  
  	platform_set_drvdata(pdev, xrtcdev);
b8541798a   Alexandre Belloni   rtc: zynqmp: fix ...
216
217
218
219
220
  	xrtcdev->rtc = devm_rtc_allocate_device(&pdev->dev);
  	if (IS_ERR(xrtcdev->rtc))
  		return PTR_ERR(xrtcdev->rtc);
  
  	xrtcdev->rtc->ops = &xlnx_rtc_ops;
3199fc3d7   Alexandre Belloni   rtc: zynqmp: let ...
221
  	xrtcdev->rtc->range_max = U32_MAX;
b8541798a   Alexandre Belloni   rtc: zynqmp: fix ...
222

09ef18bcd   YueHaibing   rtc: use devm_pla...
223
  	xrtcdev->reg_base = devm_platform_ioremap_resource(pdev, 0);
11143c19e   Suneel Garapati   rtc: add xilinx z...
224
225
226
227
  	if (IS_ERR(xrtcdev->reg_base))
  		return PTR_ERR(xrtcdev->reg_base);
  
  	xrtcdev->alarm_irq = platform_get_irq_byname(pdev, "alarm");
faac91020   Stephen Boyd   rtc: Remove dev_e...
228
  	if (xrtcdev->alarm_irq < 0)
11143c19e   Suneel Garapati   rtc: add xilinx z...
229
  		return xrtcdev->alarm_irq;
11143c19e   Suneel Garapati   rtc: add xilinx z...
230
231
232
233
234
235
236
237
238
239
  	ret = devm_request_irq(&pdev->dev, xrtcdev->alarm_irq,
  			       xlnx_rtc_interrupt, 0,
  			       dev_name(&pdev->dev), xrtcdev);
  	if (ret) {
  		dev_err(&pdev->dev, "request irq failed
  ");
  		return ret;
  	}
  
  	xrtcdev->sec_irq = platform_get_irq_byname(pdev, "sec");
faac91020   Stephen Boyd   rtc: Remove dev_e...
240
  	if (xrtcdev->sec_irq < 0)
11143c19e   Suneel Garapati   rtc: add xilinx z...
241
  		return xrtcdev->sec_irq;
11143c19e   Suneel Garapati   rtc: add xilinx z...
242
243
244
245
246
247
248
249
250
251
  	ret = devm_request_irq(&pdev->dev, xrtcdev->sec_irq,
  			       xlnx_rtc_interrupt, 0,
  			       dev_name(&pdev->dev), xrtcdev);
  	if (ret) {
  		dev_err(&pdev->dev, "request irq failed
  ");
  		return ret;
  	}
  
  	ret = of_property_read_u32(pdev->dev.of_node, "calibration",
58c4ed3ba   Anurag Kumar Vulisha   rtc: zynqmp: Writ...
252
  				   &xrtcdev->calibval);
11143c19e   Suneel Garapati   rtc: add xilinx z...
253
  	if (ret)
58c4ed3ba   Anurag Kumar Vulisha   rtc: zynqmp: Writ...
254
  		xrtcdev->calibval = RTC_CALIB_DEF;
11143c19e   Suneel Garapati   rtc: add xilinx z...
255

58c4ed3ba   Anurag Kumar Vulisha   rtc: zynqmp: Writ...
256
  	xlnx_init_rtc(xrtcdev);
11143c19e   Suneel Garapati   rtc: add xilinx z...
257
258
  
  	device_init_wakeup(&pdev->dev, 1);
b8541798a   Alexandre Belloni   rtc: zynqmp: fix ...
259
  	return rtc_register_device(xrtcdev->rtc);
11143c19e   Suneel Garapati   rtc: add xilinx z...
260
261
262
263
264
265
266
267
268
269
270
271
  }
  
  static int xlnx_rtc_remove(struct platform_device *pdev)
  {
  	xlnx_rtc_alarm_irq_enable(&pdev->dev, 0);
  	device_init_wakeup(&pdev->dev, 0);
  
  	return 0;
  }
  
  static int __maybe_unused xlnx_rtc_suspend(struct device *dev)
  {
85368bb9d   Wolfram Sang   rtc: simplify get...
272
  	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
11143c19e   Suneel Garapati   rtc: add xilinx z...
273

85368bb9d   Wolfram Sang   rtc: simplify get...
274
  	if (device_may_wakeup(dev))
11143c19e   Suneel Garapati   rtc: add xilinx z...
275
276
277
278
279
280
281
282
283
  		enable_irq_wake(xrtcdev->alarm_irq);
  	else
  		xlnx_rtc_alarm_irq_enable(dev, 0);
  
  	return 0;
  }
  
  static int __maybe_unused xlnx_rtc_resume(struct device *dev)
  {
85368bb9d   Wolfram Sang   rtc: simplify get...
284
  	struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
11143c19e   Suneel Garapati   rtc: add xilinx z...
285

85368bb9d   Wolfram Sang   rtc: simplify get...
286
  	if (device_may_wakeup(dev))
11143c19e   Suneel Garapati   rtc: add xilinx z...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  		disable_irq_wake(xrtcdev->alarm_irq);
  	else
  		xlnx_rtc_alarm_irq_enable(dev, 1);
  
  	return 0;
  }
  
  static SIMPLE_DEV_PM_OPS(xlnx_rtc_pm_ops, xlnx_rtc_suspend, xlnx_rtc_resume);
  
  static const struct of_device_id xlnx_rtc_of_match[] = {
  	{.compatible = "xlnx,zynqmp-rtc" },
  	{ }
  };
  MODULE_DEVICE_TABLE(of, xlnx_rtc_of_match);
  
  static struct platform_driver xlnx_rtc_driver = {
  	.probe		= xlnx_rtc_probe,
  	.remove		= xlnx_rtc_remove,
  	.driver		= {
  		.name	= KBUILD_MODNAME,
  		.pm	= &xlnx_rtc_pm_ops,
  		.of_match_table	= xlnx_rtc_of_match,
  	},
  };
  
  module_platform_driver(xlnx_rtc_driver);
  
  MODULE_DESCRIPTION("Xilinx Zynq MPSoC RTC driver");
  MODULE_AUTHOR("Xilinx Inc.");
  MODULE_LICENSE("GPL v2");