blob: ebe03eba8f5ffafa19823082cfe198e8ea658cdb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Russell Kinga1909012008-04-20 12:08:36 +01002/*
3 * linux/drivers/rtc/rtc-pl030.c
4 *
5 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
Russell Kinga1909012008-04-20 12:08:36 +01006 */
7#include <linux/module.h>
8#include <linux/rtc.h>
9#include <linux/init.h>
10#include <linux/interrupt.h>
11#include <linux/amba/bus.h>
12#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Russell Kinga1909012008-04-20 12:08:36 +010014
15#define RTC_DR (0)
16#define RTC_MR (4)
17#define RTC_STAT (8)
18#define RTC_EOI (8)
19#define RTC_LR (12)
20#define RTC_CR (16)
21#define RTC_CR_MIE (1 << 0)
22
23struct pl030_rtc {
24 struct rtc_device *rtc;
25 void __iomem *base;
26};
27
28static irqreturn_t pl030_interrupt(int irq, void *dev_id)
29{
30 struct pl030_rtc *rtc = dev_id;
31 writel(0, rtc->base + RTC_EOI);
32 return IRQ_HANDLED;
33}
34
Russell Kinga1909012008-04-20 12:08:36 +010035static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
36{
37 struct pl030_rtc *rtc = dev_get_drvdata(dev);
38
Alexandre Bellonic33c4712020-03-06 01:57:30 +010039 rtc_time64_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
Russell Kinga1909012008-04-20 12:08:36 +010040 return 0;
41}
42
43static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
44{
45 struct pl030_rtc *rtc = dev_get_drvdata(dev);
Russell Kinga1909012008-04-20 12:08:36 +010046
Alexandre Bellonic33c4712020-03-06 01:57:30 +010047 writel(rtc_tm_to_time64(&alrm->time), rtc->base + RTC_MR);
48
49 return 0;
Russell Kinga1909012008-04-20 12:08:36 +010050}
51
52static int pl030_read_time(struct device *dev, struct rtc_time *tm)
53{
54 struct pl030_rtc *rtc = dev_get_drvdata(dev);
55
Alexandre Bellonic33c4712020-03-06 01:57:30 +010056 rtc_time64_to_tm(readl(rtc->base + RTC_DR), tm);
Russell Kinga1909012008-04-20 12:08:36 +010057
58 return 0;
59}
60
61/*
62 * Set the RTC time. Unfortunately, we can't accurately set
63 * the point at which the counter updates.
64 *
65 * Also, since RTC_LR is transferred to RTC_CR on next rising
66 * edge of the 1Hz clock, we must write the time one second
67 * in advance.
68 */
69static int pl030_set_time(struct device *dev, struct rtc_time *tm)
70{
71 struct pl030_rtc *rtc = dev_get_drvdata(dev);
Russell Kinga1909012008-04-20 12:08:36 +010072
Alexandre Bellonic33c4712020-03-06 01:57:30 +010073 writel(rtc_tm_to_time64(tm) + 1, rtc->base + RTC_LR);
Russell Kinga1909012008-04-20 12:08:36 +010074
Alexandre Bellonic33c4712020-03-06 01:57:30 +010075 return 0;
Russell Kinga1909012008-04-20 12:08:36 +010076}
77
78static const struct rtc_class_ops pl030_ops = {
Russell Kinga1909012008-04-20 12:08:36 +010079 .read_time = pl030_read_time,
80 .set_time = pl030_set_time,
81 .read_alarm = pl030_read_alarm,
82 .set_alarm = pl030_set_alarm,
83};
84
Russell Kingaa25afa2011-02-19 15:55:00 +000085static int pl030_probe(struct amba_device *dev, const struct amba_id *id)
Russell Kinga1909012008-04-20 12:08:36 +010086{
87 struct pl030_rtc *rtc;
88 int ret;
89
90 ret = amba_request_regions(dev, NULL);
91 if (ret)
92 goto err_req;
93
Sangjung Woo58c181c2013-11-12 15:11:02 -080094 rtc = devm_kzalloc(&dev->dev, sizeof(*rtc), GFP_KERNEL);
Russell Kinga1909012008-04-20 12:08:36 +010095 if (!rtc) {
96 ret = -ENOMEM;
97 goto err_rtc;
98 }
99
Alexandre Bellonic778ec82018-09-09 22:38:47 +0200100 rtc->rtc = devm_rtc_allocate_device(&dev->dev);
101 if (IS_ERR(rtc->rtc)) {
102 ret = PTR_ERR(rtc->rtc);
103 goto err_rtc;
104 }
105
106 rtc->rtc->ops = &pl030_ops;
Alexandre Belloni98961692020-03-06 01:57:28 +0100107 rtc->rtc->range_max = U32_MAX;
Linus Walleijdc890c22009-06-07 23:27:31 +0100108 rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kinga1909012008-04-20 12:08:36 +0100109 if (!rtc->base) {
110 ret = -ENOMEM;
Sangjung Woo58c181c2013-11-12 15:11:02 -0800111 goto err_rtc;
Russell Kinga1909012008-04-20 12:08:36 +0100112 }
113
114 __raw_writel(0, rtc->base + RTC_CR);
115 __raw_writel(0, rtc->base + RTC_EOI);
116
117 amba_set_drvdata(dev, rtc);
118
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700119 ret = request_irq(dev->irq[0], pl030_interrupt, 0,
Russell Kinga1909012008-04-20 12:08:36 +0100120 "rtc-pl030", rtc);
121 if (ret)
122 goto err_irq;
123
Alexandre Bellonic778ec82018-09-09 22:38:47 +0200124 ret = rtc_register_device(rtc->rtc);
125 if (ret)
Russell Kinga1909012008-04-20 12:08:36 +0100126 goto err_reg;
Russell Kinga1909012008-04-20 12:08:36 +0100127
128 return 0;
129
130 err_reg:
131 free_irq(dev->irq[0], rtc);
132 err_irq:
133 iounmap(rtc->base);
Russell Kinga1909012008-04-20 12:08:36 +0100134 err_rtc:
135 amba_release_regions(dev);
136 err_req:
137 return ret;
138}
139
140static int pl030_remove(struct amba_device *dev)
141{
142 struct pl030_rtc *rtc = amba_get_drvdata(dev);
143
Russell Kinga1909012008-04-20 12:08:36 +0100144 writel(0, rtc->base + RTC_CR);
145
146 free_irq(dev->irq[0], rtc);
Russell Kinga1909012008-04-20 12:08:36 +0100147 iounmap(rtc->base);
Russell Kinga1909012008-04-20 12:08:36 +0100148 amba_release_regions(dev);
149
150 return 0;
151}
152
153static struct amba_id pl030_ids[] = {
154 {
155 .id = 0x00041030,
156 .mask = 0x000fffff,
157 },
158 { 0, 0 },
159};
160
Dave Martin66bce592011-10-05 15:15:21 +0100161MODULE_DEVICE_TABLE(amba, pl030_ids);
162
Russell Kinga1909012008-04-20 12:08:36 +0100163static struct amba_driver pl030_driver = {
164 .drv = {
165 .name = "rtc-pl030",
166 },
167 .probe = pl030_probe,
168 .remove = pl030_remove,
169 .id_table = pl030_ids,
170};
171
viresh kumar9e5ed092012-03-15 10:40:38 +0100172module_amba_driver(pl030_driver);
Russell Kinga1909012008-04-20 12:08:36 +0100173
174MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
175MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
176MODULE_LICENSE("GPL");