Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
02b2ee16 | 2 | /* |
2809e80b | 3 | * RTC driver code specific to PKUnity SoC and UniCore ISA |
02b2ee16 G |
4 | * |
5 | * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn> | |
6 | * Copyright (C) 2001-2010 Guan Xuetao | |
02b2ee16 G |
7 | */ |
8 | ||
9 | #include <linux/module.h> | |
10 | #include <linux/fs.h> | |
11 | #include <linux/string.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/platform_device.h> | |
14 | #include <linux/interrupt.h> | |
15 | #include <linux/rtc.h> | |
16 | #include <linux/bcd.h> | |
17 | #include <linux/clk.h> | |
18 | #include <linux/log2.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/uaccess.h> | |
21 | #include <linux/io.h> | |
22 | ||
23 | #include <asm/irq.h> | |
24 | #include <mach/hardware.h> | |
25 | ||
26 | static struct resource *puv3_rtc_mem; | |
27 | ||
28 | static int puv3_rtc_alarmno = IRQ_RTCAlarm; | |
29 | static int puv3_rtc_tickno = IRQ_RTC; | |
30 | ||
31 | static DEFINE_SPINLOCK(puv3_rtc_pie_lock); | |
32 | ||
33 | /* IRQ Handlers */ | |
02b2ee16 G |
34 | static irqreturn_t puv3_rtc_alarmirq(int irq, void *id) |
35 | { | |
36 | struct rtc_device *rdev = id; | |
37 | ||
e5abf78b | 38 | writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR); |
02b2ee16 G |
39 | rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF); |
40 | return IRQ_HANDLED; | |
41 | } | |
42 | ||
43 | static irqreturn_t puv3_rtc_tickirq(int irq, void *id) | |
44 | { | |
45 | struct rtc_device *rdev = id; | |
46 | ||
e5abf78b | 47 | writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR); |
02b2ee16 G |
48 | rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF); |
49 | return IRQ_HANDLED; | |
50 | } | |
51 | ||
52 | /* Update control registers */ | |
1fbc4c4d | 53 | static void puv3_rtc_setaie(struct device *dev, int to) |
02b2ee16 G |
54 | { |
55 | unsigned int tmp; | |
56 | ||
1fbc4c4d | 57 | dev_dbg(dev, "%s: aie=%d\n", __func__, to); |
02b2ee16 | 58 | |
e5abf78b | 59 | tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE; |
02b2ee16 G |
60 | |
61 | if (to) | |
62 | tmp |= RTC_RTSR_ALE; | |
63 | ||
e5abf78b | 64 | writel(tmp, RTC_RTSR); |
02b2ee16 G |
65 | } |
66 | ||
67 | static int puv3_rtc_setpie(struct device *dev, int enabled) | |
68 | { | |
69 | unsigned int tmp; | |
70 | ||
c863810c | 71 | dev_dbg(dev, "%s: pie=%d\n", __func__, enabled); |
02b2ee16 G |
72 | |
73 | spin_lock_irq(&puv3_rtc_pie_lock); | |
e5abf78b | 74 | tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE; |
02b2ee16 G |
75 | |
76 | if (enabled) | |
77 | tmp |= RTC_RTSR_HZE; | |
78 | ||
e5abf78b | 79 | writel(tmp, RTC_RTSR); |
02b2ee16 G |
80 | spin_unlock_irq(&puv3_rtc_pie_lock); |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
02b2ee16 | 85 | /* Time read/write */ |
02b2ee16 G |
86 | static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm) |
87 | { | |
e5abf78b | 88 | rtc_time_to_tm(readl(RTC_RCNR), rtc_tm); |
02b2ee16 | 89 | |
b2db0a29 | 90 | dev_dbg(dev, "read time %ptRr\n", rtc_tm); |
02b2ee16 G |
91 | |
92 | return 0; | |
93 | } | |
94 | ||
95 | static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm) | |
96 | { | |
97 | unsigned long rtc_count = 0; | |
98 | ||
b2db0a29 | 99 | dev_dbg(dev, "set time %ptRr\n", tm); |
02b2ee16 G |
100 | |
101 | rtc_tm_to_time(tm, &rtc_count); | |
e5abf78b | 102 | writel(rtc_count, RTC_RCNR); |
02b2ee16 G |
103 | |
104 | return 0; | |
105 | } | |
106 | ||
107 | static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm) | |
108 | { | |
109 | struct rtc_time *alm_tm = &alrm->time; | |
110 | ||
e5abf78b | 111 | rtc_time_to_tm(readl(RTC_RTAR), alm_tm); |
02b2ee16 | 112 | |
e5abf78b | 113 | alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE; |
02b2ee16 | 114 | |
b2db0a29 | 115 | dev_dbg(dev, "read alarm: %d, %ptRr\n", alrm->enabled, alm_tm); |
02b2ee16 G |
116 | |
117 | return 0; | |
118 | } | |
119 | ||
120 | static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) | |
121 | { | |
122 | struct rtc_time *tm = &alrm->time; | |
123 | unsigned long rtcalarm_count = 0; | |
124 | ||
b2db0a29 | 125 | dev_dbg(dev, "set alarm: %d, %ptRr\n", alrm->enabled, tm); |
02b2ee16 G |
126 | |
127 | rtc_tm_to_time(tm, &rtcalarm_count); | |
e5abf78b | 128 | writel(rtcalarm_count, RTC_RTAR); |
02b2ee16 | 129 | |
73fa5406 | 130 | puv3_rtc_setaie(dev, alrm->enabled); |
02b2ee16 G |
131 | |
132 | if (alrm->enabled) | |
133 | enable_irq_wake(puv3_rtc_alarmno); | |
134 | else | |
135 | disable_irq_wake(puv3_rtc_alarmno); | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | static int puv3_rtc_proc(struct device *dev, struct seq_file *seq) | |
141 | { | |
142 | seq_printf(seq, "periodic_IRQ\t: %s\n", | |
e5abf78b | 143 | (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no"); |
02b2ee16 G |
144 | return 0; |
145 | } | |
146 | ||
02b2ee16 | 147 | static const struct rtc_class_ops puv3_rtcops = { |
02b2ee16 G |
148 | .read_time = puv3_rtc_gettime, |
149 | .set_time = puv3_rtc_settime, | |
150 | .read_alarm = puv3_rtc_getalarm, | |
151 | .set_alarm = puv3_rtc_setalarm, | |
02b2ee16 G |
152 | .proc = puv3_rtc_proc, |
153 | }; | |
154 | ||
5936fdb9 | 155 | static void puv3_rtc_enable(struct device *dev, int en) |
02b2ee16 G |
156 | { |
157 | if (!en) { | |
e5abf78b | 158 | writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR); |
02b2ee16 G |
159 | } else { |
160 | /* re-enable the device, and check it is ok */ | |
e5abf78b | 161 | if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) { |
5936fdb9 | 162 | dev_info(dev, "rtc disabled, re-enabling\n"); |
e5abf78b | 163 | writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR); |
02b2ee16 G |
164 | } |
165 | } | |
166 | } | |
167 | ||
5a167f45 | 168 | static int puv3_rtc_remove(struct platform_device *dev) |
02b2ee16 | 169 | { |
02b2ee16 | 170 | puv3_rtc_setpie(&dev->dev, 0); |
1fbc4c4d | 171 | puv3_rtc_setaie(&dev->dev, 0); |
02b2ee16 G |
172 | |
173 | release_resource(puv3_rtc_mem); | |
174 | kfree(puv3_rtc_mem); | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
5a167f45 | 179 | static int puv3_rtc_probe(struct platform_device *pdev) |
02b2ee16 G |
180 | { |
181 | struct rtc_device *rtc; | |
182 | struct resource *res; | |
183 | int ret; | |
184 | ||
1fbc4c4d | 185 | dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev); |
02b2ee16 G |
186 | |
187 | /* find the IRQs */ | |
02b2ee16 G |
188 | puv3_rtc_tickno = platform_get_irq(pdev, 1); |
189 | if (puv3_rtc_tickno < 0) { | |
190 | dev_err(&pdev->dev, "no irq for rtc tick\n"); | |
191 | return -ENOENT; | |
192 | } | |
193 | ||
194 | puv3_rtc_alarmno = platform_get_irq(pdev, 0); | |
195 | if (puv3_rtc_alarmno < 0) { | |
196 | dev_err(&pdev->dev, "no irq for alarm\n"); | |
197 | return -ENOENT; | |
198 | } | |
199 | ||
1fbc4c4d | 200 | dev_dbg(&pdev->dev, "PKUnity_rtc: tick irq %d, alarm irq %d\n", |
02b2ee16 G |
201 | puv3_rtc_tickno, puv3_rtc_alarmno); |
202 | ||
5539ba54 AB |
203 | rtc = devm_rtc_allocate_device(&pdev->dev); |
204 | if (IS_ERR(rtc)) | |
205 | return PTR_ERR(rtc); | |
206 | ||
60d34552 AB |
207 | ret = devm_request_irq(&pdev->dev, puv3_rtc_alarmno, puv3_rtc_alarmirq, |
208 | 0, "pkunity-rtc alarm", rtc); | |
209 | if (ret) { | |
210 | dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret); | |
211 | return ret; | |
212 | } | |
213 | ||
214 | ret = devm_request_irq(&pdev->dev, puv3_rtc_tickno, puv3_rtc_tickirq, | |
215 | 0, "pkunity-rtc tick", rtc); | |
216 | if (ret) { | |
217 | dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret); | |
218 | return ret; | |
219 | } | |
220 | ||
02b2ee16 | 221 | /* get the memory region */ |
02b2ee16 G |
222 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
223 | if (res == NULL) { | |
224 | dev_err(&pdev->dev, "failed to get memory region resource\n"); | |
225 | return -ENOENT; | |
226 | } | |
227 | ||
28f65c11 JP |
228 | puv3_rtc_mem = request_mem_region(res->start, resource_size(res), |
229 | pdev->name); | |
02b2ee16 G |
230 | |
231 | if (puv3_rtc_mem == NULL) { | |
232 | dev_err(&pdev->dev, "failed to reserve memory region\n"); | |
233 | ret = -ENOENT; | |
234 | goto err_nores; | |
235 | } | |
236 | ||
5936fdb9 | 237 | puv3_rtc_enable(&pdev->dev, 1); |
02b2ee16 | 238 | |
02b2ee16 | 239 | /* register RTC and exit */ |
5539ba54 AB |
240 | rtc->ops = &puv3_rtcops; |
241 | ret = rtc_register_device(rtc); | |
242 | if (ret) { | |
02b2ee16 | 243 | dev_err(&pdev->dev, "cannot attach rtc\n"); |
02b2ee16 G |
244 | goto err_nortc; |
245 | } | |
246 | ||
247 | /* platform setup code should have handled this; sigh */ | |
248 | if (!device_can_wakeup(&pdev->dev)) | |
249 | device_init_wakeup(&pdev->dev, 1); | |
250 | ||
251 | platform_set_drvdata(pdev, rtc); | |
252 | return 0; | |
253 | ||
254 | err_nortc: | |
5936fdb9 | 255 | puv3_rtc_enable(&pdev->dev, 0); |
02b2ee16 G |
256 | release_resource(puv3_rtc_mem); |
257 | ||
258 | err_nores: | |
259 | return ret; | |
260 | } | |
261 | ||
5936fdb9 | 262 | #ifdef CONFIG_PM_SLEEP |
02b2ee16 G |
263 | static int ticnt_save; |
264 | ||
5936fdb9 | 265 | static int puv3_rtc_suspend(struct device *dev) |
02b2ee16 G |
266 | { |
267 | /* save RTAR for anyone using periodic interrupts */ | |
e5abf78b | 268 | ticnt_save = readl(RTC_RTAR); |
5936fdb9 | 269 | puv3_rtc_enable(dev, 0); |
02b2ee16 G |
270 | return 0; |
271 | } | |
272 | ||
5936fdb9 | 273 | static int puv3_rtc_resume(struct device *dev) |
02b2ee16 | 274 | { |
5936fdb9 | 275 | puv3_rtc_enable(dev, 1); |
e5abf78b | 276 | writel(ticnt_save, RTC_RTAR); |
02b2ee16 G |
277 | return 0; |
278 | } | |
02b2ee16 G |
279 | #endif |
280 | ||
5936fdb9 JH |
281 | static SIMPLE_DEV_PM_OPS(puv3_rtc_pm_ops, puv3_rtc_suspend, puv3_rtc_resume); |
282 | ||
b3a0aa3a | 283 | static struct platform_driver puv3_rtc_driver = { |
02b2ee16 | 284 | .probe = puv3_rtc_probe, |
5a167f45 | 285 | .remove = puv3_rtc_remove, |
02b2ee16 G |
286 | .driver = { |
287 | .name = "PKUnity-v3-RTC", | |
5936fdb9 | 288 | .pm = &puv3_rtc_pm_ops, |
02b2ee16 G |
289 | } |
290 | }; | |
291 | ||
b3a0aa3a | 292 | module_platform_driver(puv3_rtc_driver); |
02b2ee16 G |
293 | |
294 | MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip"); | |
295 | MODULE_AUTHOR("Hu Dongliang"); | |
296 | MODULE_LICENSE("GPL v2"); |