Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a190901c RK |
2 | /* |
3 | * linux/drivers/rtc/rtc-pl030.c | |
4 | * | |
5 | * Copyright (C) 2000-2001 Deep Blue Solutions Ltd. | |
a190901c RK |
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> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
a190901c RK |
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 | struct rtc_device *rtc; | |
25 | void __iomem *base; | |
26 | }; | |
27 | ||
28 | static 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 | ||
a190901c RK |
35 | static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
36 | { | |
37 | struct pl030_rtc *rtc = dev_get_drvdata(dev); | |
38 | ||
c33c4713 | 39 | rtc_time64_to_tm(readl(rtc->base + RTC_MR), &alrm->time); |
a190901c RK |
40 | return 0; |
41 | } | |
42 | ||
43 | static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | |
44 | { | |
45 | struct pl030_rtc *rtc = dev_get_drvdata(dev); | |
a190901c | 46 | |
c33c4713 AB |
47 | writel(rtc_tm_to_time64(&alrm->time), rtc->base + RTC_MR); |
48 | ||
49 | return 0; | |
a190901c RK |
50 | } |
51 | ||
52 | static int pl030_read_time(struct device *dev, struct rtc_time *tm) | |
53 | { | |
54 | struct pl030_rtc *rtc = dev_get_drvdata(dev); | |
55 | ||
c33c4713 | 56 | rtc_time64_to_tm(readl(rtc->base + RTC_DR), tm); |
a190901c RK |
57 | |
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 | */ | |
69 | static int pl030_set_time(struct device *dev, struct rtc_time *tm) | |
70 | { | |
71 | struct pl030_rtc *rtc = dev_get_drvdata(dev); | |
a190901c | 72 | |
c33c4713 | 73 | writel(rtc_tm_to_time64(tm) + 1, rtc->base + RTC_LR); |
a190901c | 74 | |
c33c4713 | 75 | return 0; |
a190901c RK |
76 | } |
77 | ||
78 | static const struct rtc_class_ops pl030_ops = { | |
a190901c RK |
79 | .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 | ||
aa25afad | 85 | static int pl030_probe(struct amba_device *dev, const struct amba_id *id) |
a190901c RK |
86 | { |
87 | struct pl030_rtc *rtc; | |
88 | int ret; | |
89 | ||
90 | ret = amba_request_regions(dev, NULL); | |
91 | if (ret) | |
92 | goto err_req; | |
93 | ||
58c181c8 | 94 | rtc = devm_kzalloc(&dev->dev, sizeof(*rtc), GFP_KERNEL); |
a190901c RK |
95 | if (!rtc) { |
96 | ret = -ENOMEM; | |
97 | goto err_rtc; | |
98 | } | |
99 | ||
c778ec85 AB |
100 | 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; | |
9896169a | 107 | rtc->rtc->range_max = U32_MAX; |
dc890c2d | 108 | rtc->base = ioremap(dev->res.start, resource_size(&dev->res)); |
a190901c RK |
109 | if (!rtc->base) { |
110 | ret = -ENOMEM; | |
58c181c8 | 111 | goto err_rtc; |
a190901c RK |
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 | ||
2f6e5f94 | 119 | ret = request_irq(dev->irq[0], pl030_interrupt, 0, |
a190901c RK |
120 | "rtc-pl030", rtc); |
121 | if (ret) | |
122 | goto err_irq; | |
123 | ||
fdcfd854 | 124 | ret = devm_rtc_register_device(rtc->rtc); |
c778ec85 | 125 | if (ret) |
a190901c | 126 | goto err_reg; |
a190901c RK |
127 | |
128 | return 0; | |
129 | ||
130 | err_reg: | |
131 | free_irq(dev->irq[0], rtc); | |
132 | err_irq: | |
133 | iounmap(rtc->base); | |
a190901c RK |
134 | err_rtc: |
135 | amba_release_regions(dev); | |
136 | err_req: | |
137 | return ret; | |
138 | } | |
139 | ||
3fd269e7 | 140 | static void pl030_remove(struct amba_device *dev) |
a190901c RK |
141 | { |
142 | struct pl030_rtc *rtc = amba_get_drvdata(dev); | |
143 | ||
a190901c RK |
144 | writel(0, rtc->base + RTC_CR); |
145 | ||
146 | free_irq(dev->irq[0], rtc); | |
a190901c | 147 | iounmap(rtc->base); |
a190901c | 148 | amba_release_regions(dev); |
a190901c RK |
149 | } |
150 | ||
151 | static struct amba_id pl030_ids[] = { | |
152 | { | |
153 | .id = 0x00041030, | |
154 | .mask = 0x000fffff, | |
155 | }, | |
156 | { 0, 0 }, | |
157 | }; | |
158 | ||
66bce591 DM |
159 | MODULE_DEVICE_TABLE(amba, pl030_ids); |
160 | ||
a190901c RK |
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 | ||
9e5ed094 | 170 | module_amba_driver(pl030_driver); |
a190901c RK |
171 | |
172 | MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); | |
173 | MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver"); | |
174 | MODULE_LICENSE("GPL"); |