blob: b702dbe2e3c71b2b86881050869d6cd925c3c17b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
4 *
5 * Copyright (c) 2000 Nils Faerber
6 *
7 * Based on rtc.c by Paul Gortmaker
8 *
9 * Original Driver by Nils Faerber <nils@kernelconcepts.de>
10 *
11 * Modifications from:
12 * CIH <cih@coventive.com>
13 * Nicolas Pitre <nico@fluxnic.net>
14 * Andrew Christian <andrew.christian@hp.com>
15 *
16 * Converted to the RTC subsystem and Driver Model
17 * by Richard Purdie <rpurdie@rpsys.net>
18 */
19
20#include <linux/platform_device.h>
21#include <linux/module.h>
22#include <linux/clk.h>
23#include <linux/rtc.h>
24#include <linux/init.h>
25#include <linux/fs.h>
26#include <linux/interrupt.h>
27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/of.h>
30#include <linux/pm.h>
31#include <linux/bitops.h>
32#include <linux/io.h>
33#include <linux/delay.h>
34#include <linux/cputype.h>
35
36#define RCNR 0x00 /* RTC Count Register */
37#define RTAR 0x04 /* RTC Alarm Register */
38#define RTSR 0x08 /* RTC Status Register */
39#define RTTR 0x0C /* RTC Timer Trim Register */
40#define RTSR_HZE BIT(3) /* HZ interrupt enable */
41#define RTSR_ALE BIT(2) /* RTC alarm interrupt enable */
42#define RTSR_HZ BIT(1) /* HZ rising-edge detected */
43#define RTSR_AL BIT(0) /* RTC alarm detected */
44
45#include "rtc-sa1100.h"
46
47#define RTC_DEF_DIVIDER (32768 - 1)
48
49#ifdef CONFIG_CPU_ASR1903
50#define SCS_RTC_DEF_DIVIDER (32765 - 1)
51#else
52#define SCS_RTC_DEF_DIVIDER (32787 - 1)
53#endif
54
55#define RTC_DEF_TRIM 0
56#define RTC_FREQ 1024
57
58static s64 base_ticks;
59
60static void __iomem *rtc_reg_base;
61int sync_time_to_soc(s64 ticks)
62{
63 s64 cur_ticks;
64
65 if (rtc_reg_base) {
66 cur_ticks = readl(rtc_reg_base + RCNR);
67 base_ticks = ticks - cur_ticks;
68 pr_info("sync ticks: %lld %lld %lld\n",
69 ticks, cur_ticks, base_ticks);
70#if 0
71 writel(ticks, rtc_reg_base + RCNR);
72 /*
73 * for soc rtc time setting, it needs some delay
74 * to ensure setting to take effective
75 */
76 udelay(200);
77#endif
78 }
79 return 0;
80}
81EXPORT_SYMBOL(sync_time_to_soc);
82
83static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
84{
85 struct sa1100_rtc *info = dev_get_drvdata(dev_id);
86 struct rtc_device *rtc = info->rtc;
87 unsigned int rtsr;
88 unsigned long events = 0;
89
90 spin_lock(&info->lock);
91
92 rtsr = readl_relaxed(info->rtsr);
93 /* clear interrupt sources */
94 writel_relaxed(0, info->rtsr);
95 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
96 * See also the comments in sa1100_rtc_probe(). */
97 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
98 /* This is the original code, before there was the if test
99 * above. This code does not clear interrupts that were not
100 * enabled. */
101 writel_relaxed((RTSR_AL | RTSR_HZ) & (rtsr >> 2), info->rtsr);
102 } else {
103 /* For some reason, it is possible to enter this routine
104 * without interruptions enabled, it has been tested with
105 * several units (Bug in SA11xx chip?).
106 *
107 * This situation leads to an infinite "loop" of interrupt
108 * routine calling and as a result the processor seems to
109 * lock on its first call to open(). */
110 writel_relaxed(RTSR_AL | RTSR_HZ, info->rtsr);
111 }
112
113 /* clear alarm interrupt if it has occurred */
114 if (rtsr & RTSR_AL)
115 rtsr &= ~RTSR_ALE;
116 writel_relaxed(rtsr & (RTSR_ALE | RTSR_HZE), info->rtsr);
117
118 /* update irq data & counter */
119 if (rtsr & RTSR_AL)
120 events |= RTC_AF | RTC_IRQF;
121 if (rtsr & RTSR_HZ)
122 events |= RTC_UF | RTC_IRQF;
123
124 rtc_update_irq(rtc, 1, events);
125
126 spin_unlock(&info->lock);
127
128 return IRQ_HANDLED;
129}
130
131static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
132{
133 u32 rtsr;
134 unsigned long flags;
135 struct sa1100_rtc *info = dev_get_drvdata(dev);
136
137 spin_lock_irqsave(&info->lock, flags);
138 rtsr = readl_relaxed(info->rtsr);
139 if (enabled)
140 rtsr |= RTSR_ALE;
141 else
142 rtsr &= ~RTSR_ALE;
143 writel_relaxed(rtsr, info->rtsr);
144 spin_unlock_irqrestore(&info->lock, flags);
145 return 0;
146}
147
148static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
149{
150 s64 ticks;
151
152 struct sa1100_rtc *info = dev_get_drvdata(dev);
153
154 ticks = readl_relaxed(info->rcnr) + base_ticks;
155 rtc_time64_to_tm(ticks, tm);
156 return 0;
157}
158
159static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
160{
161 struct sa1100_rtc *info = dev_get_drvdata(dev);
162 s64 ticks;
163
164 /* to align with rtc-88pm80x/scs */
165 if ((tm->tm_year < 70) || (tm->tm_year > 300)) {
166 pr_err("SCSRTC: Set time %d out of range. Please set time between 1970 to 2200.\n",
167 1900 + tm->tm_year);
168 return -EINVAL;
169 }
170
171 ticks = rtc_tm_to_time64(tm);
172 ticks = ticks - base_ticks;
173 BUG_ON(ticks < 0);
174 writel_relaxed((u32)ticks, info->rcnr);
175 /* stabilize the register value */
176 udelay(200);
177 return 0;
178}
179
180static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
181{
182 u32 rtsr;
183 s64 time;
184 struct sa1100_rtc *info = dev_get_drvdata(dev);
185
186 time = base_ticks + readl_relaxed(info->rtar);
187 rtc_time64_to_tm(time, &alrm->time);
188 rtsr = readl_relaxed(info->rtsr);
189 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
190 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
191 return 0;
192}
193
194static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195{
196 struct sa1100_rtc *info = dev_get_drvdata(dev);
197 s64 time;
198 int ret;
199 unsigned long flags;
200
201 spin_lock_irqsave(&info->lock, flags);
202
203 time = rtc_tm_to_time64(&alrm->time);
204 writel_relaxed(readl_relaxed(info->rtsr) &
205 (RTSR_HZE | RTSR_ALE | RTSR_AL), info->rtsr);
206 time = time - base_ticks;
207 writel_relaxed((u32)time, info->rtar);
208 BUG_ON(time < 0);
209 if (alrm->enabled)
210 writel_relaxed(readl_relaxed(info->rtsr) | RTSR_ALE, info->rtsr);
211 else
212 writel_relaxed(readl_relaxed(info->rtsr) & ~RTSR_ALE, info->rtsr);
213
214 spin_unlock_irqrestore(&info->lock, flags);
215
216 return ret;
217}
218
219static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
220{
221 struct sa1100_rtc *info = dev_get_drvdata(dev);
222
223 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", readl_relaxed(info->rttr));
224 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", readl_relaxed(info->rtsr));
225
226 return 0;
227}
228
229static const struct rtc_class_ops sa1100_rtc_ops = {
230 .read_time = sa1100_rtc_read_time,
231 .set_time = sa1100_rtc_set_time,
232 .read_alarm = sa1100_rtc_read_alarm,
233 .set_alarm = sa1100_rtc_set_alarm,
234 .proc = sa1100_rtc_proc,
235 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
236};
237
238int sa1100_rtc_init(struct platform_device *pdev, struct sa1100_rtc *info)
239{
240 int ret;
241
242 spin_lock_init(&info->lock);
243
244 info->clk = devm_clk_get(&pdev->dev, NULL);
245 if (IS_ERR(info->clk)) {
246 dev_err(&pdev->dev, "failed to find rtc clock source\n");
247 return PTR_ERR(info->clk);
248 }
249
250 ret = clk_prepare_enable(info->clk);
251 if (ret)
252 return ret;
253 /*
254 * According to the manual we should be able to let RTTR be zero
255 * and then a default diviser for a 32.768KHz clock is used.
256 * Apparently this doesn't work, at least for my SA1110 rev 5.
257 * If the clock divider is uninitialized then reset it to the
258 * default value to get the 1Hz clock.
259 */
260 if (readl_relaxed(info->rttr) == 0) {
261 writel_relaxed(RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16), info->rttr);
262 dev_warn(&pdev->dev, "warning: "
263 "initializing default clock divider/trim value\n");
264 /* The current RTC value probably doesn't make sense either */
265 writel_relaxed(0, info->rcnr);
266 } else if (cpu_is_asr1803() || cpu_is_asr1806()) {
267 writel_relaxed(SCS_RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16), info->rttr);
268 }
269
270 info->rtc->ops = &sa1100_rtc_ops;
271 info->rtc->max_user_freq = RTC_FREQ;
272
273 ret = rtc_register_device(info->rtc);
274 if (ret) {
275 clk_disable_unprepare(info->clk);
276 return ret;
277 }
278
279 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
280 * See also the comments in sa1100_rtc_interrupt().
281 *
282 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
283 * interrupt pending, even though interrupts were never enabled.
284 * In this case, this bit it must be reset before enabling
285 * interruptions to avoid a nonexistent interrupt to occur.
286 *
287 * In principle, the same problem would apply to bit 0, although it has
288 * never been observed to happen.
289 *
290 * This issue is addressed both here and in sa1100_rtc_interrupt().
291 * If the issue is not addressed here, in the times when the processor
292 * wakes up with the bit set there will be one spurious interrupt.
293 *
294 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
295 * safe side, once the condition that lead to this strange
296 * initialization is unknown and could in principle happen during
297 * normal processing.
298 *
299 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
300 * the corresponding bits in RTSR. */
301 writel_relaxed(RTSR_AL | RTSR_HZ, info->rtsr);
302
303 return 0;
304}
305EXPORT_SYMBOL_GPL(sa1100_rtc_init);
306
307static int sa1100_rtc_probe(struct platform_device *pdev)
308{
309 struct sa1100_rtc *info;
310 struct resource *iores;
311 void __iomem *base;
312 int irq_1hz, irq_alarm;
313 int ret;
314
315 irq_1hz = platform_get_irq_byname(pdev, "rtc 1Hz");
316 irq_alarm = platform_get_irq_byname(pdev, "rtc alarm");
317 if (irq_1hz < 0 || irq_alarm < 0)
318 return -ENODEV;
319
320 info = devm_kzalloc(&pdev->dev, sizeof(struct sa1100_rtc), GFP_KERNEL);
321 if (!info)
322 return -ENOMEM;
323 info->irq_1hz = irq_1hz;
324 info->irq_alarm = irq_alarm;
325
326 info->rtc = devm_rtc_allocate_device(&pdev->dev);
327 if (IS_ERR(info->rtc))
328 return PTR_ERR(info->rtc);
329
330 ret = devm_request_irq(&pdev->dev, irq_1hz, sa1100_rtc_interrupt, 0,
331 "rtc 1Hz", &pdev->dev);
332 if (ret) {
333 dev_err(&pdev->dev, "IRQ %d already in use.\n", irq_1hz);
334 return ret;
335 }
336 ret = devm_request_irq(&pdev->dev, irq_alarm, sa1100_rtc_interrupt, 0,
337 "rtc Alrm", &pdev->dev);
338 if (ret) {
339 dev_err(&pdev->dev, "IRQ %d already in use.\n", irq_alarm);
340 return ret;
341 }
342
343 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
344 base = devm_ioremap_resource(&pdev->dev, iores);
345 if (IS_ERR(base))
346 return PTR_ERR(base);
347 rtc_reg_base = base;
348
349 if (IS_ENABLED(CONFIG_ARCH_SA1100) ||
350 of_device_is_compatible(pdev->dev.of_node, "mrvl,sa1100-rtc")) {
351 info->rcnr = base + 0x04;
352 info->rtsr = base + 0x10;
353 info->rtar = base + 0x00;
354 info->rttr = base + 0x08;
355 } else {
356 info->rcnr = base + 0x0;
357 info->rtsr = base + 0x8;
358 info->rtar = base + 0x4;
359 info->rttr = base + 0xc;
360 }
361
362 platform_set_drvdata(pdev, info);
363 device_init_wakeup(&pdev->dev, 1);
364
365 return sa1100_rtc_init(pdev, info);
366}
367
368static int sa1100_rtc_remove(struct platform_device *pdev)
369{
370 unsigned long flags;
371 struct sa1100_rtc *info = platform_get_drvdata(pdev);
372
373 if (info) {
374 spin_lock_irqsave(&info->lock, flags);
375 writel_relaxed(0, info->rtsr);
376 spin_unlock_irqrestore(&info->lock, flags);
377 clk_disable_unprepare(info->clk);
378 }
379
380 return 0;
381}
382
383#ifdef CONFIG_PM_SLEEP
384static int sa1100_rtc_suspend(struct device *dev)
385{
386 struct sa1100_rtc *info = dev_get_drvdata(dev);
387 if (device_may_wakeup(dev))
388 enable_irq_wake(info->irq_alarm);
389 return 0;
390}
391
392static int sa1100_rtc_resume(struct device *dev)
393{
394 struct sa1100_rtc *info = dev_get_drvdata(dev);
395 if (device_may_wakeup(dev))
396 disable_irq_wake(info->irq_alarm);
397 return 0;
398}
399#endif
400
401static SIMPLE_DEV_PM_OPS(sa1100_rtc_pm_ops, sa1100_rtc_suspend,
402 sa1100_rtc_resume);
403
404#ifdef CONFIG_OF
405static const struct of_device_id sa1100_rtc_dt_ids[] = {
406 { .compatible = "mrvl,sa1100-rtc", },
407 { .compatible = "mrvl,mmp-rtc", },
408 {}
409};
410MODULE_DEVICE_TABLE(of, sa1100_rtc_dt_ids);
411#endif
412
413static struct platform_driver sa1100_rtc_driver = {
414 .probe = sa1100_rtc_probe,
415 .remove = sa1100_rtc_remove,
416 .driver = {
417 .name = "sa1100-rtc",
418 .pm = &sa1100_rtc_pm_ops,
419 .of_match_table = of_match_ptr(sa1100_rtc_dt_ids),
420 },
421};
422
423module_platform_driver(sa1100_rtc_driver);
424
425MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
426MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
427MODULE_LICENSE("GPL");
428MODULE_ALIAS("platform:sa1100-rtc");