1 // SPDX-License-Identifier: GPL-2.0+
3 * An RTC driver for the NVIDIA Tegra 200 series internal RTC.
5 * Copyright (c) 2010, NVIDIA Corporation.
9 #include <linux/delay.h>
10 #include <linux/init.h>
12 #include <linux/irq.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/platform_device.h>
18 #include <linux/rtc.h>
19 #include <linux/slab.h>
21 /* set to 1 = busy every eight 32kHz clocks during copy of sec+msec to AHB */
22 #define TEGRA_RTC_REG_BUSY 0x004
23 #define TEGRA_RTC_REG_SECONDS 0x008
24 /* when msec is read, the seconds are buffered into shadow seconds. */
25 #define TEGRA_RTC_REG_SHADOW_SECONDS 0x00c
26 #define TEGRA_RTC_REG_MILLI_SECONDS 0x010
27 #define TEGRA_RTC_REG_SECONDS_ALARM0 0x014
28 #define TEGRA_RTC_REG_SECONDS_ALARM1 0x018
29 #define TEGRA_RTC_REG_MILLI_SECONDS_ALARM0 0x01c
30 #define TEGRA_RTC_REG_INTR_MASK 0x028
31 /* write 1 bits to clear status bits */
32 #define TEGRA_RTC_REG_INTR_STATUS 0x02c
34 /* bits in INTR_MASK */
35 #define TEGRA_RTC_INTR_MASK_MSEC_CDN_ALARM (1<<4)
36 #define TEGRA_RTC_INTR_MASK_SEC_CDN_ALARM (1<<3)
37 #define TEGRA_RTC_INTR_MASK_MSEC_ALARM (1<<2)
38 #define TEGRA_RTC_INTR_MASK_SEC_ALARM1 (1<<1)
39 #define TEGRA_RTC_INTR_MASK_SEC_ALARM0 (1<<0)
41 /* bits in INTR_STATUS */
42 #define TEGRA_RTC_INTR_STATUS_MSEC_CDN_ALARM (1<<4)
43 #define TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM (1<<3)
44 #define TEGRA_RTC_INTR_STATUS_MSEC_ALARM (1<<2)
45 #define TEGRA_RTC_INTR_STATUS_SEC_ALARM1 (1<<1)
46 #define TEGRA_RTC_INTR_STATUS_SEC_ALARM0 (1<<0)
48 struct tegra_rtc_info {
49 struct platform_device *pdev;
50 struct rtc_device *rtc_dev;
51 void __iomem *rtc_base; /* NULL if not initialized. */
53 int tegra_rtc_irq; /* alarm and periodic irq */
54 spinlock_t tegra_rtc_lock;
57 /* RTC hardware is busy when it is updating its values over AHB once
58 * every eight 32kHz clocks (~250uS).
59 * outside of these updates the CPU is free to write.
60 * CPU is always free to read.
62 static inline u32 tegra_rtc_check_busy(struct tegra_rtc_info *info)
64 return readl(info->rtc_base + TEGRA_RTC_REG_BUSY) & 1;
67 /* Wait for hardware to be ready for writing.
68 * This function tries to maximize the amount of time before the next update.
69 * It does this by waiting for the RTC to become busy with its periodic update,
70 * then returning once the RTC first becomes not busy.
71 * This periodic update (where the seconds and milliseconds are copied to the
72 * AHB side) occurs every eight 32kHz clocks (~250uS).
73 * The behavior of this function allows us to make some assumptions without
74 * introducing a race, because 250uS is plenty of time to read/write a value.
76 static int tegra_rtc_wait_while_busy(struct device *dev)
78 struct tegra_rtc_info *info = dev_get_drvdata(dev);
80 int retries = 500; /* ~490 us is the worst case, ~250 us is best. */
82 /* first wait for the RTC to become busy. this is when it
83 * posts its updated seconds+msec registers to AHB side. */
84 while (tegra_rtc_check_busy(info)) {
90 /* now we have about 250 us to manipulate registers */
94 dev_err(dev, "write failed:retry count exceeded.\n");
98 static int tegra_rtc_read_time(struct device *dev, struct rtc_time *tm)
100 struct tegra_rtc_info *info = dev_get_drvdata(dev);
101 unsigned long sec, msec;
102 unsigned long sl_irq_flags;
104 /* RTC hardware copies seconds to shadow seconds when a read
105 * of milliseconds occurs. use a lock to keep other threads out. */
106 spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
108 msec = readl(info->rtc_base + TEGRA_RTC_REG_MILLI_SECONDS);
109 sec = readl(info->rtc_base + TEGRA_RTC_REG_SHADOW_SECONDS);
111 spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
113 rtc_time64_to_tm(sec, tm);
115 dev_vdbg(dev, "time read as %lu. %ptR\n", sec, tm);
120 static int tegra_rtc_set_time(struct device *dev, struct rtc_time *tm)
122 struct tegra_rtc_info *info = dev_get_drvdata(dev);
126 /* convert tm to seconds. */
127 sec = rtc_tm_to_time64(tm);
129 dev_vdbg(dev, "time set to %lu. %ptR\n", sec, tm);
131 /* seconds only written if wait succeeded. */
132 ret = tegra_rtc_wait_while_busy(dev);
134 writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS);
136 dev_vdbg(dev, "time read back as %d\n",
137 readl(info->rtc_base + TEGRA_RTC_REG_SECONDS));
142 static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
144 struct tegra_rtc_info *info = dev_get_drvdata(dev);
148 sec = readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
151 /* alarm is disabled. */
154 /* alarm is enabled. */
156 rtc_time64_to_tm(sec, &alarm->time);
159 tmp = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
160 alarm->pending = (tmp & TEGRA_RTC_INTR_STATUS_SEC_ALARM0) != 0;
165 static int tegra_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
167 struct tegra_rtc_info *info = dev_get_drvdata(dev);
169 unsigned long sl_irq_flags;
171 tegra_rtc_wait_while_busy(dev);
172 spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
174 /* read the original value, and OR in the flag. */
175 status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
177 status |= TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* set it */
179 status &= ~TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* clear it */
181 writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
183 spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
188 static int tegra_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
190 struct tegra_rtc_info *info = dev_get_drvdata(dev);
194 sec = rtc_tm_to_time64(&alarm->time);
198 tegra_rtc_wait_while_busy(dev);
199 writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
200 dev_vdbg(dev, "alarm read back as %d\n",
201 readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
203 /* if successfully written and alarm is enabled ... */
205 tegra_rtc_alarm_irq_enable(dev, 1);
206 dev_vdbg(dev, "alarm set as %lu. %ptR\n", sec, &alarm->time);
208 /* disable alarm if 0 or write error. */
209 dev_vdbg(dev, "alarm disabled\n");
210 tegra_rtc_alarm_irq_enable(dev, 0);
216 static int tegra_rtc_proc(struct device *dev, struct seq_file *seq)
218 if (!dev || !dev->driver)
221 seq_printf(seq, "name\t\t: %s\n", dev_name(dev));
226 static irqreturn_t tegra_rtc_irq_handler(int irq, void *data)
228 struct device *dev = data;
229 struct tegra_rtc_info *info = dev_get_drvdata(dev);
230 unsigned long events = 0;
232 unsigned long sl_irq_flags;
234 status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
236 /* clear the interrupt masks and status on any irq. */
237 tegra_rtc_wait_while_busy(dev);
238 spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
239 writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
240 writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
241 spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
245 if ((status & TEGRA_RTC_INTR_STATUS_SEC_ALARM0))
246 events |= RTC_IRQF | RTC_AF;
248 /* check if Periodic */
249 if ((status & TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM))
250 events |= RTC_IRQF | RTC_PF;
252 rtc_update_irq(info->rtc_dev, 1, events);
257 static const struct rtc_class_ops tegra_rtc_ops = {
258 .read_time = tegra_rtc_read_time,
259 .set_time = tegra_rtc_set_time,
260 .read_alarm = tegra_rtc_read_alarm,
261 .set_alarm = tegra_rtc_set_alarm,
262 .proc = tegra_rtc_proc,
263 .alarm_irq_enable = tegra_rtc_alarm_irq_enable,
266 static const struct of_device_id tegra_rtc_dt_match[] = {
267 { .compatible = "nvidia,tegra20-rtc", },
270 MODULE_DEVICE_TABLE(of, tegra_rtc_dt_match);
272 static int __init tegra_rtc_probe(struct platform_device *pdev)
274 struct tegra_rtc_info *info;
275 struct resource *res;
278 info = devm_kzalloc(&pdev->dev, sizeof(struct tegra_rtc_info),
283 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
284 info->rtc_base = devm_ioremap_resource(&pdev->dev, res);
285 if (IS_ERR(info->rtc_base))
286 return PTR_ERR(info->rtc_base);
288 ret = platform_get_irq(pdev, 0);
290 dev_err(&pdev->dev, "failed to get platform IRQ: %d\n", ret);
294 info->tegra_rtc_irq = ret;
296 info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
297 if (IS_ERR(info->rtc_dev))
298 return PTR_ERR(info->rtc_dev);
300 info->rtc_dev->ops = &tegra_rtc_ops;
301 info->rtc_dev->range_max = U32_MAX;
303 info->clk = devm_clk_get(&pdev->dev, NULL);
304 if (IS_ERR(info->clk))
305 return PTR_ERR(info->clk);
307 ret = clk_prepare_enable(info->clk);
311 /* set context info. */
313 spin_lock_init(&info->tegra_rtc_lock);
315 platform_set_drvdata(pdev, info);
317 /* clear out the hardware. */
318 writel(0, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
319 writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
320 writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
322 device_init_wakeup(&pdev->dev, 1);
324 ret = devm_request_irq(&pdev->dev, info->tegra_rtc_irq,
325 tegra_rtc_irq_handler, IRQF_TRIGGER_HIGH,
326 dev_name(&pdev->dev), &pdev->dev);
329 "Unable to request interrupt for device (err=%d).\n",
334 ret = rtc_register_device(info->rtc_dev);
336 dev_err(&pdev->dev, "Unable to register device (err=%d).\n",
341 dev_notice(&pdev->dev, "Tegra internal Real Time Clock\n");
346 clk_disable_unprepare(info->clk);
350 static int tegra_rtc_remove(struct platform_device *pdev)
352 struct tegra_rtc_info *info = platform_get_drvdata(pdev);
354 clk_disable_unprepare(info->clk);
359 #ifdef CONFIG_PM_SLEEP
360 static int tegra_rtc_suspend(struct device *dev)
362 struct tegra_rtc_info *info = dev_get_drvdata(dev);
364 tegra_rtc_wait_while_busy(dev);
366 /* only use ALARM0 as a wake source. */
367 writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
368 writel(TEGRA_RTC_INTR_STATUS_SEC_ALARM0,
369 info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
371 dev_vdbg(dev, "alarm sec = %d\n",
372 readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
374 dev_vdbg(dev, "Suspend (device_may_wakeup=%d) irq:%d\n",
375 device_may_wakeup(dev), info->tegra_rtc_irq);
377 /* leave the alarms on as a wake source. */
378 if (device_may_wakeup(dev))
379 enable_irq_wake(info->tegra_rtc_irq);
384 static int tegra_rtc_resume(struct device *dev)
386 struct tegra_rtc_info *info = dev_get_drvdata(dev);
388 dev_vdbg(dev, "Resume (device_may_wakeup=%d)\n",
389 device_may_wakeup(dev));
390 /* alarms were left on as a wake source, turn them off. */
391 if (device_may_wakeup(dev))
392 disable_irq_wake(info->tegra_rtc_irq);
398 static SIMPLE_DEV_PM_OPS(tegra_rtc_pm_ops, tegra_rtc_suspend, tegra_rtc_resume);
400 static void tegra_rtc_shutdown(struct platform_device *pdev)
402 dev_vdbg(&pdev->dev, "disabling interrupts.\n");
403 tegra_rtc_alarm_irq_enable(&pdev->dev, 0);
406 MODULE_ALIAS("platform:tegra_rtc");
407 static struct platform_driver tegra_rtc_driver = {
408 .remove = tegra_rtc_remove,
409 .shutdown = tegra_rtc_shutdown,
412 .of_match_table = tegra_rtc_dt_match,
413 .pm = &tegra_rtc_pm_ops,
417 module_platform_driver_probe(tegra_rtc_driver, tegra_rtc_probe);
420 MODULE_DESCRIPTION("driver for Tegra internal RTC");
421 MODULE_LICENSE("GPL");