1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/rtc/rtc-pl030.c
4 *
5 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
6 */
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>
13 #include <linux/slab.h>
14
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
23 struct pl030_rtc {
24 void __iomem *base;
25 };
26
pl030_interrupt(int irq,void * dev_id)27 static irqreturn_t pl030_interrupt(int irq, void *dev_id)
28 {
29 struct pl030_rtc *rtc = dev_id;
30 writel(0, rtc->base + RTC_EOI);
31 return IRQ_HANDLED;
32 }
33
pl030_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)34 static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
35 {
36 struct pl030_rtc *rtc = dev_get_drvdata(dev);
37
38 rtc_time64_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
39 return 0;
40 }
41
pl030_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)42 static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
43 {
44 struct pl030_rtc *rtc = dev_get_drvdata(dev);
45
46 writel(rtc_tm_to_time64(&alrm->time), rtc->base + RTC_MR);
47
48 return 0;
49 }
50
pl030_read_time(struct device * dev,struct rtc_time * tm)51 static int pl030_read_time(struct device *dev, struct rtc_time *tm)
52 {
53 struct pl030_rtc *rtc = dev_get_drvdata(dev);
54
55 rtc_time64_to_tm(readl(rtc->base + RTC_DR), tm);
56
57 return 0;
58 }
59
60 /*
61 * Set the RTC time. Unfortunately, we can't accurately set
62 * the point at which the counter updates.
63 *
64 * Also, since RTC_LR is transferred to RTC_CR on next rising
65 * edge of the 1Hz clock, we must write the time one second
66 * in advance.
67 */
pl030_set_time(struct device * dev,struct rtc_time * tm)68 static int pl030_set_time(struct device *dev, struct rtc_time *tm)
69 {
70 struct pl030_rtc *rtc = dev_get_drvdata(dev);
71
72 writel(rtc_tm_to_time64(tm) + 1, rtc->base + RTC_LR);
73
74 return 0;
75 }
76
77 static const struct rtc_class_ops pl030_ops = {
78 .read_time = pl030_read_time,
79 .set_time = pl030_set_time,
80 .read_alarm = pl030_read_alarm,
81 .set_alarm = pl030_set_alarm,
82 };
83
pl030_probe(struct amba_device * dev,const struct amba_id * id)84 static int pl030_probe(struct amba_device *dev, const struct amba_id *id)
85 {
86 struct pl030_rtc *rtc;
87 int ret;
88 struct rtc_device *rtc_dev;
89
90 ret = amba_request_regions(dev, NULL);
91 if (ret)
92 goto err_req;
93
94 rtc = devm_kzalloc(&dev->dev, sizeof(*rtc), GFP_KERNEL);
95 if (!rtc) {
96 ret = -ENOMEM;
97 goto err_rtc;
98 }
99
100 rtc_dev = devm_rtc_allocate_device(&dev->dev);
101 if (IS_ERR(rtc_dev)) {
102 ret = PTR_ERR(rtc_dev);
103 goto err_rtc;
104 }
105
106 rtc_dev->ops = &pl030_ops;
107 rtc_dev->range_max = U32_MAX;
108 rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
109 if (!rtc->base) {
110 ret = -ENOMEM;
111 goto err_rtc;
112 }
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
119 ret = request_irq(dev->irq[0], pl030_interrupt, 0,
120 "rtc-pl030", rtc);
121 if (ret)
122 goto err_irq;
123
124 ret = devm_rtc_register_device(rtc_dev);
125 if (ret)
126 goto err_reg;
127
128 return 0;
129
130 err_reg:
131 free_irq(dev->irq[0], rtc);
132 err_irq:
133 iounmap(rtc->base);
134 err_rtc:
135 amba_release_regions(dev);
136 err_req:
137 return ret;
138 }
139
pl030_remove(struct amba_device * dev)140 static void pl030_remove(struct amba_device *dev)
141 {
142 struct pl030_rtc *rtc = amba_get_drvdata(dev);
143
144 writel(0, rtc->base + RTC_CR);
145
146 free_irq(dev->irq[0], rtc);
147 iounmap(rtc->base);
148 amba_release_regions(dev);
149 }
150
151 static const struct amba_id pl030_ids[] = {
152 {
153 .id = 0x00041030,
154 .mask = 0x000fffff,
155 },
156 { 0, 0 },
157 };
158
159 MODULE_DEVICE_TABLE(amba, pl030_ids);
160
161 static struct amba_driver pl030_driver = {
162 .drv = {
163 .name = "rtc-pl030",
164 },
165 .probe = pl030_probe,
166 .remove = pl030_remove,
167 .id_table = pl030_ids,
168 };
169
170 module_amba_driver(pl030_driver);
171
172 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
173 MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
174 MODULE_LICENSE("GPL");
175