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