blob: f0ce76865434ac46470ae8af2af0d1c93b5beb9d [file] [log] [blame]
Alexandre Bellonib6838272019-04-07 23:16:46 +02001// SPDX-License-Identifier: GPL-2.0+
Andrew Chewff859ba2011-03-22 16:34:55 -07002/*
3 * An RTC driver for the NVIDIA Tegra 200 series internal RTC.
4 *
5 * Copyright (c) 2010, NVIDIA Corporation.
Andrew Chewff859ba2011-03-22 16:34:55 -07006 */
Thierry Reding0ae20592017-01-12 17:07:42 +01007
Thierry Reding5fa40862017-01-12 17:07:43 +01008#include <linux/clk.h>
Andrew Chewff859ba2011-03-22 16:34:55 -07009#include <linux/delay.h>
Thierry Reding0ae20592017-01-12 17:07:42 +010010#include <linux/init.h>
11#include <linux/io.h>
12#include <linux/irq.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070015#include <linux/mod_devicetable.h>
Andrew Chewff859ba2011-03-22 16:34:55 -070016#include <linux/platform_device.h>
Laxman Dewangan3443ad02013-04-29 16:19:23 -070017#include <linux/pm.h>
Thierry Reding0ae20592017-01-12 17:07:42 +010018#include <linux/rtc.h>
19#include <linux/slab.h>
Andrew Chewff859ba2011-03-22 16:34:55 -070020
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
33
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)
40
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)
47
48struct tegra_rtc_info {
49 struct platform_device *pdev;
50 struct rtc_device *rtc_dev;
51 void __iomem *rtc_base; /* NULL if not initialized. */
Thierry Reding5fa40862017-01-12 17:07:43 +010052 struct clk *clk;
Andrew Chewff859ba2011-03-22 16:34:55 -070053 int tegra_rtc_irq; /* alarm and periodic irq */
54 spinlock_t tegra_rtc_lock;
55};
56
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.
61 */
62static inline u32 tegra_rtc_check_busy(struct tegra_rtc_info *info)
63{
64 return readl(info->rtc_base + TEGRA_RTC_REG_BUSY) & 1;
65}
66
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.
75 */
76static int tegra_rtc_wait_while_busy(struct device *dev)
77{
78 struct tegra_rtc_info *info = dev_get_drvdata(dev);
79
80 int retries = 500; /* ~490 us is the worst case, ~250 us is best. */
81
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)) {
85 if (!retries--)
86 goto retry_failed;
87 udelay(1);
88 }
89
90 /* now we have about 250 us to manipulate registers */
91 return 0;
92
93retry_failed:
94 dev_err(dev, "write failed:retry count exceeded.\n");
95 return -ETIMEDOUT;
96}
97
98static int tegra_rtc_read_time(struct device *dev, struct rtc_time *tm)
99{
100 struct tegra_rtc_info *info = dev_get_drvdata(dev);
101 unsigned long sec, msec;
102 unsigned long sl_irq_flags;
103
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);
107
108 msec = readl(info->rtc_base + TEGRA_RTC_REG_MILLI_SECONDS);
109 sec = readl(info->rtc_base + TEGRA_RTC_REG_SHADOW_SECONDS);
110
111 spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
112
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200113 rtc_time64_to_tm(sec, tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700114
Andy Shevchenkod54fb482018-12-04 23:23:27 +0200115 dev_vdbg(dev, "time read as %lu. %ptR\n", sec, tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700116
117 return 0;
118}
119
120static int tegra_rtc_set_time(struct device *dev, struct rtc_time *tm)
121{
122 struct tegra_rtc_info *info = dev_get_drvdata(dev);
123 unsigned long sec;
124 int ret;
125
126 /* convert tm to seconds. */
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200127 sec = rtc_tm_to_time64(tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700128
Andy Shevchenkod54fb482018-12-04 23:23:27 +0200129 dev_vdbg(dev, "time set to %lu. %ptR\n", sec, tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700130
131 /* seconds only written if wait succeeded. */
132 ret = tegra_rtc_wait_while_busy(dev);
133 if (!ret)
134 writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS);
135
136 dev_vdbg(dev, "time read back as %d\n",
137 readl(info->rtc_base + TEGRA_RTC_REG_SECONDS));
138
139 return ret;
140}
141
142static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
143{
144 struct tegra_rtc_info *info = dev_get_drvdata(dev);
145 unsigned long sec;
146 unsigned tmp;
147
148 sec = readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
149
150 if (sec == 0) {
151 /* alarm is disabled. */
152 alarm->enabled = 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700153 } else {
154 /* alarm is enabled. */
155 alarm->enabled = 1;
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200156 rtc_time64_to_tm(sec, &alarm->time);
Andrew Chewff859ba2011-03-22 16:34:55 -0700157 }
158
159 tmp = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
160 alarm->pending = (tmp & TEGRA_RTC_INTR_STATUS_SEC_ALARM0) != 0;
161
162 return 0;
163}
164
165static int tegra_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
166{
167 struct tegra_rtc_info *info = dev_get_drvdata(dev);
168 unsigned status;
169 unsigned long sl_irq_flags;
170
171 tegra_rtc_wait_while_busy(dev);
172 spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
173
174 /* read the original value, and OR in the flag. */
175 status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
176 if (enabled)
177 status |= TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* set it */
178 else
179 status &= ~TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* clear it */
180
181 writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
182
183 spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
184
185 return 0;
186}
187
188static int tegra_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
189{
190 struct tegra_rtc_info *info = dev_get_drvdata(dev);
191 unsigned long sec;
192
193 if (alarm->enabled)
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200194 sec = rtc_tm_to_time64(&alarm->time);
Andrew Chewff859ba2011-03-22 16:34:55 -0700195 else
196 sec = 0;
197
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));
202
203 /* if successfully written and alarm is enabled ... */
204 if (sec) {
205 tegra_rtc_alarm_irq_enable(dev, 1);
Andy Shevchenkod54fb482018-12-04 23:23:27 +0200206 dev_vdbg(dev, "alarm set as %lu. %ptR\n", sec, &alarm->time);
Andrew Chewff859ba2011-03-22 16:34:55 -0700207 } else {
208 /* disable alarm if 0 or write error. */
209 dev_vdbg(dev, "alarm disabled\n");
210 tegra_rtc_alarm_irq_enable(dev, 0);
211 }
212
213 return 0;
214}
215
216static int tegra_rtc_proc(struct device *dev, struct seq_file *seq)
217{
218 if (!dev || !dev->driver)
219 return 0;
220
Joe Perches4395eb12015-04-15 16:17:51 -0700221 seq_printf(seq, "name\t\t: %s\n", dev_name(dev));
222
223 return 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700224}
225
226static irqreturn_t tegra_rtc_irq_handler(int irq, void *data)
227{
228 struct device *dev = data;
229 struct tegra_rtc_info *info = dev_get_drvdata(dev);
230 unsigned long events = 0;
231 unsigned status;
232 unsigned long sl_irq_flags;
233
234 status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
235 if (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);
242 }
243
244 /* check if Alarm */
245 if ((status & TEGRA_RTC_INTR_STATUS_SEC_ALARM0))
246 events |= RTC_IRQF | RTC_AF;
247
248 /* check if Periodic */
249 if ((status & TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM))
250 events |= RTC_IRQF | RTC_PF;
251
252 rtc_update_irq(info->rtc_dev, 1, events);
253
254 return IRQ_HANDLED;
255}
256
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200257static const struct rtc_class_ops tegra_rtc_ops = {
Andrew Chewff859ba2011-03-22 16:34:55 -0700258 .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,
264};
265
Joseph Lo2d79cf82013-01-04 15:34:45 -0800266static const struct of_device_id tegra_rtc_dt_match[] = {
267 { .compatible = "nvidia,tegra20-rtc", },
268 {}
269};
270MODULE_DEVICE_TABLE(of, tegra_rtc_dt_match);
271
Jingoo Han51b38c62013-04-29 16:18:27 -0700272static int __init tegra_rtc_probe(struct platform_device *pdev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700273{
274 struct tegra_rtc_info *info;
275 struct resource *res;
276 int ret;
277
Hannu Heikkinen621bae72012-05-29 15:07:40 -0700278 info = devm_kzalloc(&pdev->dev, sizeof(struct tegra_rtc_info),
279 GFP_KERNEL);
Andrew Chewff859ba2011-03-22 16:34:55 -0700280 if (!info)
281 return -ENOMEM;
282
283 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding8cbce1e2013-01-21 11:09:17 +0100284 info->rtc_base = devm_ioremap_resource(&pdev->dev, res);
285 if (IS_ERR(info->rtc_base))
286 return PTR_ERR(info->rtc_base);
Andrew Chewff859ba2011-03-22 16:34:55 -0700287
Thierry Redingfe0b5ce2018-09-21 12:12:09 +0200288 ret = platform_get_irq(pdev, 0);
289 if (ret <= 0) {
290 dev_err(&pdev->dev, "failed to get platform IRQ: %d\n", ret);
291 return ret;
292 }
293
294 info->tegra_rtc_irq = ret;
Andrew Chewff859ba2011-03-22 16:34:55 -0700295
Alexandre Bellonie1089802019-04-07 23:16:44 +0200296 info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
297 if (IS_ERR(info->rtc_dev))
298 return PTR_ERR(info->rtc_dev);
299
300 info->rtc_dev->ops = &tegra_rtc_ops;
301 info->rtc_dev->range_max = U32_MAX;
302
Thierry Reding5fa40862017-01-12 17:07:43 +0100303 info->clk = devm_clk_get(&pdev->dev, NULL);
304 if (IS_ERR(info->clk))
305 return PTR_ERR(info->clk);
306
307 ret = clk_prepare_enable(info->clk);
308 if (ret < 0)
309 return ret;
310
Andrew Chewff859ba2011-03-22 16:34:55 -0700311 /* set context info. */
312 info->pdev = pdev;
Uwe Kleine-Könige57ee012011-07-25 17:13:34 -0700313 spin_lock_init(&info->tegra_rtc_lock);
Andrew Chewff859ba2011-03-22 16:34:55 -0700314
315 platform_set_drvdata(pdev, info);
316
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);
321
322 device_init_wakeup(&pdev->dev, 1);
323
Hannu Heikkinen621bae72012-05-29 15:07:40 -0700324 ret = devm_request_irq(&pdev->dev, info->tegra_rtc_irq,
325 tegra_rtc_irq_handler, IRQF_TRIGGER_HIGH,
Laxman Dewangan57bff982013-04-29 16:19:24 -0700326 dev_name(&pdev->dev), &pdev->dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700327 if (ret) {
328 dev_err(&pdev->dev,
329 "Unable to request interrupt for device (err=%d).\n",
330 ret);
Thierry Reding5fa40862017-01-12 17:07:43 +0100331 goto disable_clk;
Andrew Chewff859ba2011-03-22 16:34:55 -0700332 }
333
Alexandre Bellonie1089802019-04-07 23:16:44 +0200334 ret = rtc_register_device(info->rtc_dev);
335 if (ret) {
336 dev_err(&pdev->dev, "Unable to register device (err=%d).\n",
337 ret);
338 goto disable_clk;
339 }
340
Andrew Chewff859ba2011-03-22 16:34:55 -0700341 dev_notice(&pdev->dev, "Tegra internal Real Time Clock\n");
342
343 return 0;
Thierry Reding5fa40862017-01-12 17:07:43 +0100344
345disable_clk:
346 clk_disable_unprepare(info->clk);
347 return ret;
348}
349
350static int tegra_rtc_remove(struct platform_device *pdev)
351{
352 struct tegra_rtc_info *info = platform_get_drvdata(pdev);
353
354 clk_disable_unprepare(info->clk);
355
356 return 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700357}
358
Laxman Dewangan38a62762013-04-29 16:19:21 -0700359#ifdef CONFIG_PM_SLEEP
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700360static int tegra_rtc_suspend(struct device *dev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700361{
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700362 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700363
364 tegra_rtc_wait_while_busy(dev);
365
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);
370
371 dev_vdbg(dev, "alarm sec = %d\n",
372 readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
373
374 dev_vdbg(dev, "Suspend (device_may_wakeup=%d) irq:%d\n",
375 device_may_wakeup(dev), info->tegra_rtc_irq);
376
377 /* leave the alarms on as a wake source. */
378 if (device_may_wakeup(dev))
379 enable_irq_wake(info->tegra_rtc_irq);
380
381 return 0;
382}
383
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700384static int tegra_rtc_resume(struct device *dev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700385{
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700386 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700387
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);
393
394 return 0;
395}
396#endif
397
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700398static SIMPLE_DEV_PM_OPS(tegra_rtc_pm_ops, tegra_rtc_suspend, tegra_rtc_resume);
399
Andrew Chewff859ba2011-03-22 16:34:55 -0700400static void tegra_rtc_shutdown(struct platform_device *pdev)
401{
402 dev_vdbg(&pdev->dev, "disabling interrupts.\n");
403 tegra_rtc_alarm_irq_enable(&pdev->dev, 0);
404}
405
406MODULE_ALIAS("platform:tegra_rtc");
407static struct platform_driver tegra_rtc_driver = {
Thierry Reding5fa40862017-01-12 17:07:43 +0100408 .remove = tegra_rtc_remove,
Andrew Chewff859ba2011-03-22 16:34:55 -0700409 .shutdown = tegra_rtc_shutdown,
410 .driver = {
411 .name = "tegra_rtc",
Joseph Lo2d79cf82013-01-04 15:34:45 -0800412 .of_match_table = tegra_rtc_dt_match,
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700413 .pm = &tegra_rtc_pm_ops,
Andrew Chewff859ba2011-03-22 16:34:55 -0700414 },
Andrew Chewff859ba2011-03-22 16:34:55 -0700415};
416
Jingoo Han0e2c4812013-04-29 16:18:53 -0700417module_platform_driver_probe(tegra_rtc_driver, tegra_rtc_probe);
Andrew Chewff859ba2011-03-22 16:34:55 -0700418
419MODULE_AUTHOR("Jon Mayo <jmayo@nvidia.com>");
420MODULE_DESCRIPTION("driver for Tegra internal RTC");
421MODULE_LICENSE("GPL");