blob: 30aa813f96c0af5f1e0442bce281f5c206dd8e03 [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
39 rtc_time_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
40 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);
46 unsigned long time;
47 int ret;
48
49 /*
50 * At the moment, we can only deal with non-wildcarded alarm times.
51 */
52 ret = rtc_valid_tm(&alrm->time);
53 if (ret == 0)
54 ret = rtc_tm_to_time(&alrm->time, &time);
55 if (ret == 0)
56 writel(time, rtc->base + RTC_MR);
57 return ret;
58}
59
60static int pl030_read_time(struct device *dev, struct rtc_time *tm)
61{
62 struct pl030_rtc *rtc = dev_get_drvdata(dev);
63
64 rtc_time_to_tm(readl(rtc->base + RTC_DR), tm);
65
66 return 0;
67}
68
69/*
70 * Set the RTC time. Unfortunately, we can't accurately set
71 * the point at which the counter updates.
72 *
73 * Also, since RTC_LR is transferred to RTC_CR on next rising
74 * edge of the 1Hz clock, we must write the time one second
75 * in advance.
76 */
77static int pl030_set_time(struct device *dev, struct rtc_time *tm)
78{
79 struct pl030_rtc *rtc = dev_get_drvdata(dev);
80 unsigned long time;
81 int ret;
82
83 ret = rtc_tm_to_time(tm, &time);
84 if (ret == 0)
85 writel(time + 1, rtc->base + RTC_LR);
86
87 return ret;
88}
89
90static const struct rtc_class_ops pl030_ops = {
Russell Kinga1909012008-04-20 12:08:36 +010091 .read_time = pl030_read_time,
92 .set_time = pl030_set_time,
93 .read_alarm = pl030_read_alarm,
94 .set_alarm = pl030_set_alarm,
95};
96
Russell Kingaa25afa2011-02-19 15:55:00 +000097static int pl030_probe(struct amba_device *dev, const struct amba_id *id)
Russell Kinga1909012008-04-20 12:08:36 +010098{
99 struct pl030_rtc *rtc;
100 int ret;
101
102 ret = amba_request_regions(dev, NULL);
103 if (ret)
104 goto err_req;
105
Sangjung Woo58c181c2013-11-12 15:11:02 -0800106 rtc = devm_kzalloc(&dev->dev, sizeof(*rtc), GFP_KERNEL);
Russell Kinga1909012008-04-20 12:08:36 +0100107 if (!rtc) {
108 ret = -ENOMEM;
109 goto err_rtc;
110 }
111
Alexandre Bellonic778ec82018-09-09 22:38:47 +0200112 rtc->rtc = devm_rtc_allocate_device(&dev->dev);
113 if (IS_ERR(rtc->rtc)) {
114 ret = PTR_ERR(rtc->rtc);
115 goto err_rtc;
116 }
117
118 rtc->rtc->ops = &pl030_ops;
Alexandre Belloni98961692020-03-06 01:57:28 +0100119 rtc->rtc->range_max = U32_MAX;
Linus Walleijdc890c22009-06-07 23:27:31 +0100120 rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kinga1909012008-04-20 12:08:36 +0100121 if (!rtc->base) {
122 ret = -ENOMEM;
Sangjung Woo58c181c2013-11-12 15:11:02 -0800123 goto err_rtc;
Russell Kinga1909012008-04-20 12:08:36 +0100124 }
125
126 __raw_writel(0, rtc->base + RTC_CR);
127 __raw_writel(0, rtc->base + RTC_EOI);
128
129 amba_set_drvdata(dev, rtc);
130
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700131 ret = request_irq(dev->irq[0], pl030_interrupt, 0,
Russell Kinga1909012008-04-20 12:08:36 +0100132 "rtc-pl030", rtc);
133 if (ret)
134 goto err_irq;
135
Alexandre Bellonic778ec82018-09-09 22:38:47 +0200136 ret = rtc_register_device(rtc->rtc);
137 if (ret)
Russell Kinga1909012008-04-20 12:08:36 +0100138 goto err_reg;
Russell Kinga1909012008-04-20 12:08:36 +0100139
140 return 0;
141
142 err_reg:
143 free_irq(dev->irq[0], rtc);
144 err_irq:
145 iounmap(rtc->base);
Russell Kinga1909012008-04-20 12:08:36 +0100146 err_rtc:
147 amba_release_regions(dev);
148 err_req:
149 return ret;
150}
151
152static int pl030_remove(struct amba_device *dev)
153{
154 struct pl030_rtc *rtc = amba_get_drvdata(dev);
155
Russell Kinga1909012008-04-20 12:08:36 +0100156 writel(0, rtc->base + RTC_CR);
157
158 free_irq(dev->irq[0], rtc);
Russell Kinga1909012008-04-20 12:08:36 +0100159 iounmap(rtc->base);
Russell Kinga1909012008-04-20 12:08:36 +0100160 amba_release_regions(dev);
161
162 return 0;
163}
164
165static struct amba_id pl030_ids[] = {
166 {
167 .id = 0x00041030,
168 .mask = 0x000fffff,
169 },
170 { 0, 0 },
171};
172
Dave Martin66bce592011-10-05 15:15:21 +0100173MODULE_DEVICE_TABLE(amba, pl030_ids);
174
Russell Kinga1909012008-04-20 12:08:36 +0100175static struct amba_driver pl030_driver = {
176 .drv = {
177 .name = "rtc-pl030",
178 },
179 .probe = pl030_probe,
180 .remove = pl030_remove,
181 .id_table = pl030_ids,
182};
183
viresh kumar9e5ed092012-03-15 10:40:38 +0100184module_amba_driver(pl030_driver);
Russell Kinga1909012008-04-20 12:08:36 +0100185
186MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
187MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
188MODULE_LICENSE("GPL");