xref: /openbmc/linux/drivers/rtc/rtc-st-lpc.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * rtc-st-lpc.c - ST's LPC RTC, powered by the Low Power Timer
4  *
5  * Copyright (C) 2014 STMicroelectronics Limited
6  *
7  * Author: David Paris <david.paris@st.com> for STMicroelectronics
8  *         Lee Jones <lee.jones@linaro.org> for STMicroelectronics
9  *
10  * Based on the original driver written by Stuart Menefy.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/io.h>
17 #include <linux/irq.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_irq.h>
22 #include <linux/platform_device.h>
23 #include <linux/rtc.h>
24 
25 #include <dt-bindings/mfd/st-lpc.h>
26 
27 /* Low Power Timer */
28 #define LPC_LPT_LSB_OFF		0x400
29 #define LPC_LPT_MSB_OFF		0x404
30 #define LPC_LPT_START_OFF	0x408
31 
32 /* Low Power Alarm */
33 #define LPC_LPA_LSB_OFF		0x410
34 #define LPC_LPA_MSB_OFF		0x414
35 #define LPC_LPA_START_OFF	0x418
36 
37 /* LPC as WDT */
38 #define LPC_WDT_OFF		0x510
39 #define LPC_WDT_FLAG_OFF	0x514
40 
41 struct st_rtc {
42 	struct rtc_device *rtc_dev;
43 	struct rtc_wkalrm alarm;
44 	struct resource *res;
45 	struct clk *clk;
46 	unsigned long clkrate;
47 	void __iomem *ioaddr;
48 	bool irq_enabled:1;
49 	spinlock_t lock;
50 	short irq;
51 };
52 
53 static void st_rtc_set_hw_alarm(struct st_rtc *rtc,
54 				unsigned long msb, unsigned long  lsb)
55 {
56 	unsigned long flags;
57 
58 	spin_lock_irqsave(&rtc->lock, flags);
59 
60 	writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
61 
62 	writel_relaxed(msb, rtc->ioaddr + LPC_LPA_MSB_OFF);
63 	writel_relaxed(lsb, rtc->ioaddr + LPC_LPA_LSB_OFF);
64 	writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
65 
66 	writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
67 
68 	spin_unlock_irqrestore(&rtc->lock, flags);
69 }
70 
71 static irqreturn_t st_rtc_handler(int this_irq, void *data)
72 {
73 	struct st_rtc *rtc = (struct st_rtc *)data;
74 
75 	rtc_update_irq(rtc->rtc_dev, 1, RTC_AF);
76 
77 	return IRQ_HANDLED;
78 }
79 
80 static int st_rtc_read_time(struct device *dev, struct rtc_time *tm)
81 {
82 	struct st_rtc *rtc = dev_get_drvdata(dev);
83 	unsigned long lpt_lsb, lpt_msb;
84 	unsigned long long lpt;
85 	unsigned long flags;
86 
87 	spin_lock_irqsave(&rtc->lock, flags);
88 
89 	do {
90 		lpt_msb = readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF);
91 		lpt_lsb = readl_relaxed(rtc->ioaddr + LPC_LPT_LSB_OFF);
92 	} while (readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF) != lpt_msb);
93 
94 	spin_unlock_irqrestore(&rtc->lock, flags);
95 
96 	lpt = ((unsigned long long)lpt_msb << 32) | lpt_lsb;
97 	do_div(lpt, rtc->clkrate);
98 	rtc_time64_to_tm(lpt, tm);
99 
100 	return 0;
101 }
102 
103 static int st_rtc_set_time(struct device *dev, struct rtc_time *tm)
104 {
105 	struct st_rtc *rtc = dev_get_drvdata(dev);
106 	unsigned long long lpt, secs;
107 	unsigned long flags;
108 
109 	secs = rtc_tm_to_time64(tm);
110 
111 	lpt = (unsigned long long)secs * rtc->clkrate;
112 
113 	spin_lock_irqsave(&rtc->lock, flags);
114 
115 	writel_relaxed(lpt >> 32, rtc->ioaddr + LPC_LPT_MSB_OFF);
116 	writel_relaxed(lpt, rtc->ioaddr + LPC_LPT_LSB_OFF);
117 	writel_relaxed(1, rtc->ioaddr + LPC_LPT_START_OFF);
118 
119 	spin_unlock_irqrestore(&rtc->lock, flags);
120 
121 	return 0;
122 }
123 
124 static int st_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
125 {
126 	struct st_rtc *rtc = dev_get_drvdata(dev);
127 	unsigned long flags;
128 
129 	spin_lock_irqsave(&rtc->lock, flags);
130 
131 	memcpy(wkalrm, &rtc->alarm, sizeof(struct rtc_wkalrm));
132 
133 	spin_unlock_irqrestore(&rtc->lock, flags);
134 
135 	return 0;
136 }
137 
138 static int st_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
139 {
140 	struct st_rtc *rtc = dev_get_drvdata(dev);
141 
142 	if (enabled && !rtc->irq_enabled) {
143 		enable_irq(rtc->irq);
144 		rtc->irq_enabled = true;
145 	} else if (!enabled && rtc->irq_enabled) {
146 		disable_irq(rtc->irq);
147 		rtc->irq_enabled = false;
148 	}
149 
150 	return 0;
151 }
152 
153 static int st_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
154 {
155 	struct st_rtc *rtc = dev_get_drvdata(dev);
156 	struct rtc_time now;
157 	unsigned long long now_secs;
158 	unsigned long long alarm_secs;
159 	unsigned long long lpa;
160 
161 	st_rtc_read_time(dev, &now);
162 	now_secs = rtc_tm_to_time64(&now);
163 	alarm_secs = rtc_tm_to_time64(&t->time);
164 
165 	memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm));
166 
167 	/* Now many secs to fire */
168 	alarm_secs -= now_secs;
169 	lpa = (unsigned long long)alarm_secs * rtc->clkrate;
170 
171 	st_rtc_set_hw_alarm(rtc, lpa >> 32, lpa);
172 	st_rtc_alarm_irq_enable(dev, t->enabled);
173 
174 	return 0;
175 }
176 
177 static struct rtc_class_ops st_rtc_ops = {
178 	.read_time		= st_rtc_read_time,
179 	.set_time		= st_rtc_set_time,
180 	.read_alarm		= st_rtc_read_alarm,
181 	.set_alarm		= st_rtc_set_alarm,
182 	.alarm_irq_enable	= st_rtc_alarm_irq_enable,
183 };
184 
185 static int st_rtc_probe(struct platform_device *pdev)
186 {
187 	struct device_node *np = pdev->dev.of_node;
188 	struct st_rtc *rtc;
189 	struct resource *res;
190 	uint32_t mode;
191 	int ret = 0;
192 
193 	ret = of_property_read_u32(np, "st,lpc-mode", &mode);
194 	if (ret) {
195 		dev_err(&pdev->dev, "An LPC mode must be provided\n");
196 		return -EINVAL;
197 	}
198 
199 	/* LPC can either run as a Clocksource or in RTC or WDT mode */
200 	if (mode != ST_LPC_MODE_RTC)
201 		return -ENODEV;
202 
203 	rtc = devm_kzalloc(&pdev->dev, sizeof(struct st_rtc), GFP_KERNEL);
204 	if (!rtc)
205 		return -ENOMEM;
206 
207 	rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
208 	if (IS_ERR(rtc->rtc_dev))
209 		return PTR_ERR(rtc->rtc_dev);
210 
211 	spin_lock_init(&rtc->lock);
212 
213 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 	rtc->ioaddr = devm_ioremap_resource(&pdev->dev, res);
215 	if (IS_ERR(rtc->ioaddr))
216 		return PTR_ERR(rtc->ioaddr);
217 
218 	rtc->irq = irq_of_parse_and_map(np, 0);
219 	if (!rtc->irq) {
220 		dev_err(&pdev->dev, "IRQ missing or invalid\n");
221 		return -EINVAL;
222 	}
223 
224 	ret = devm_request_irq(&pdev->dev, rtc->irq, st_rtc_handler, 0,
225 			       pdev->name, rtc);
226 	if (ret) {
227 		dev_err(&pdev->dev, "Failed to request irq %i\n", rtc->irq);
228 		return ret;
229 	}
230 
231 	enable_irq_wake(rtc->irq);
232 	disable_irq(rtc->irq);
233 
234 	rtc->clk = clk_get(&pdev->dev, NULL);
235 	if (IS_ERR(rtc->clk)) {
236 		dev_err(&pdev->dev, "Unable to request clock\n");
237 		return PTR_ERR(rtc->clk);
238 	}
239 
240 	clk_prepare_enable(rtc->clk);
241 
242 	rtc->clkrate = clk_get_rate(rtc->clk);
243 	if (!rtc->clkrate) {
244 		dev_err(&pdev->dev, "Unable to fetch clock rate\n");
245 		return -EINVAL;
246 	}
247 
248 	device_set_wakeup_capable(&pdev->dev, 1);
249 
250 	platform_set_drvdata(pdev, rtc);
251 
252 	rtc->rtc_dev->ops = &st_rtc_ops;
253 	rtc->rtc_dev->range_max = U64_MAX;
254 	do_div(rtc->rtc_dev->range_max, rtc->clkrate);
255 
256 	ret = rtc_register_device(rtc->rtc_dev);
257 	if (ret) {
258 		clk_disable_unprepare(rtc->clk);
259 		return ret;
260 	}
261 
262 	return 0;
263 }
264 
265 #ifdef CONFIG_PM_SLEEP
266 static int st_rtc_suspend(struct device *dev)
267 {
268 	struct st_rtc *rtc = dev_get_drvdata(dev);
269 
270 	if (device_may_wakeup(dev))
271 		return 0;
272 
273 	writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
274 	writel_relaxed(0, rtc->ioaddr + LPC_LPA_START_OFF);
275 	writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
276 
277 	return 0;
278 }
279 
280 static int st_rtc_resume(struct device *dev)
281 {
282 	struct st_rtc *rtc = dev_get_drvdata(dev);
283 
284 	rtc_alarm_irq_enable(rtc->rtc_dev, 0);
285 
286 	/*
287 	 * clean 'rtc->alarm' to allow a new
288 	 * .set_alarm to the upper RTC layer
289 	 */
290 	memset(&rtc->alarm, 0, sizeof(struct rtc_wkalrm));
291 
292 	writel_relaxed(0, rtc->ioaddr + LPC_LPA_MSB_OFF);
293 	writel_relaxed(0, rtc->ioaddr + LPC_LPA_LSB_OFF);
294 	writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
295 	writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
296 	writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
297 
298 	return 0;
299 }
300 #endif
301 
302 static SIMPLE_DEV_PM_OPS(st_rtc_pm_ops, st_rtc_suspend, st_rtc_resume);
303 
304 static const struct of_device_id st_rtc_match[] = {
305 	{ .compatible = "st,stih407-lpc" },
306 	{}
307 };
308 MODULE_DEVICE_TABLE(of, st_rtc_match);
309 
310 static struct platform_driver st_rtc_platform_driver = {
311 	.driver = {
312 		.name = "st-lpc-rtc",
313 		.pm = &st_rtc_pm_ops,
314 		.of_match_table = st_rtc_match,
315 	},
316 	.probe = st_rtc_probe,
317 };
318 
319 module_platform_driver(st_rtc_platform_driver);
320 
321 MODULE_DESCRIPTION("STMicroelectronics LPC RTC driver");
322 MODULE_AUTHOR("David Paris <david.paris@st.com>");
323 MODULE_LICENSE("GPL");
324