blob: 46f14b82f3ab6e987efef66d90181414e34d475c [file] [log] [blame]
GuanXuetao02b2ee12011-01-15 18:19:03 +08001/*
Guan Xuetao2809e802011-05-26 16:43:27 +08002 * RTC driver code specific to PKUnity SoC and UniCore ISA
GuanXuetao02b2ee12011-01-15 18:19:03 +08003 *
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
29static struct resource *puv3_rtc_mem;
30
31static int puv3_rtc_alarmno = IRQ_RTCAlarm;
32static int puv3_rtc_tickno = IRQ_RTC;
33
34static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
35
36/* IRQ Handlers */
GuanXuetao02b2ee12011-01-15 18:19:03 +080037static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
38{
39 struct rtc_device *rdev = id;
40
GuanXuetaoe5abf782011-02-26 21:21:18 +080041 writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080042 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
43 return IRQ_HANDLED;
44}
45
46static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
47{
48 struct rtc_device *rdev = id;
49
GuanXuetaoe5abf782011-02-26 21:21:18 +080050 writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080051 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
52 return IRQ_HANDLED;
53}
54
55/* Update control registers */
56static void puv3_rtc_setaie(int to)
57{
58 unsigned int tmp;
59
60 pr_debug("%s: aie=%d\n", __func__, to);
61
GuanXuetaoe5abf782011-02-26 21:21:18 +080062 tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
GuanXuetao02b2ee12011-01-15 18:19:03 +080063
64 if (to)
65 tmp |= RTC_RTSR_ALE;
66
GuanXuetaoe5abf782011-02-26 21:21:18 +080067 writel(tmp, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080068}
69
70static int puv3_rtc_setpie(struct device *dev, int enabled)
71{
72 unsigned int tmp;
73
74 pr_debug("%s: pie=%d\n", __func__, enabled);
75
76 spin_lock_irq(&puv3_rtc_pie_lock);
GuanXuetaoe5abf782011-02-26 21:21:18 +080077 tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
GuanXuetao02b2ee12011-01-15 18:19:03 +080078
79 if (enabled)
80 tmp |= RTC_RTSR_HZE;
81
GuanXuetaoe5abf782011-02-26 21:21:18 +080082 writel(tmp, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080083 spin_unlock_irq(&puv3_rtc_pie_lock);
84
85 return 0;
86}
87
GuanXuetao02b2ee12011-01-15 18:19:03 +080088/* Time read/write */
GuanXuetao02b2ee12011-01-15 18:19:03 +080089static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
90{
GuanXuetaoe5abf782011-02-26 21:21:18 +080091 rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +080092
93 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
94 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
95 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
96
97 return 0;
98}
99
100static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
101{
102 unsigned long rtc_count = 0;
103
104 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
105 tm->tm_year, tm->tm_mon, tm->tm_mday,
106 tm->tm_hour, tm->tm_min, tm->tm_sec);
107
108 rtc_tm_to_time(tm, &rtc_count);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800109 writel(rtc_count, RTC_RCNR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800110
111 return 0;
112}
113
114static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
115{
116 struct rtc_time *alm_tm = &alrm->time;
117
GuanXuetaoe5abf782011-02-26 21:21:18 +0800118 rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800119
GuanXuetaoe5abf782011-02-26 21:21:18 +0800120 alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
GuanXuetao02b2ee12011-01-15 18:19:03 +0800121
122 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
123 alrm->enabled,
124 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
125 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
126
127 return 0;
128}
129
130static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
131{
132 struct rtc_time *tm = &alrm->time;
133 unsigned long rtcalarm_count = 0;
134
135 pr_debug("puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
136 alrm->enabled,
137 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
138 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
139
140 rtc_tm_to_time(tm, &rtcalarm_count);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800141 writel(rtcalarm_count, RTC_RTAR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800142
143 puv3_rtc_setaie(alrm->enabled);
144
145 if (alrm->enabled)
146 enable_irq_wake(puv3_rtc_alarmno);
147 else
148 disable_irq_wake(puv3_rtc_alarmno);
149
150 return 0;
151}
152
153static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
154{
155 seq_printf(seq, "periodic_IRQ\t: %s\n",
GuanXuetaoe5abf782011-02-26 21:21:18 +0800156 (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
GuanXuetao02b2ee12011-01-15 18:19:03 +0800157 return 0;
158}
159
160static int puv3_rtc_open(struct device *dev)
161{
162 struct platform_device *pdev = to_platform_device(dev);
163 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
164 int ret;
165
166 ret = request_irq(puv3_rtc_alarmno, puv3_rtc_alarmirq,
167 IRQF_DISABLED, "pkunity-rtc alarm", rtc_dev);
168
169 if (ret) {
170 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
171 return ret;
172 }
173
174 ret = request_irq(puv3_rtc_tickno, puv3_rtc_tickirq,
175 IRQF_DISABLED, "pkunity-rtc tick", rtc_dev);
176
177 if (ret) {
178 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
179 goto tick_err;
180 }
181
182 return ret;
183
184 tick_err:
185 free_irq(puv3_rtc_alarmno, rtc_dev);
186 return ret;
187}
188
189static void puv3_rtc_release(struct device *dev)
190{
191 struct platform_device *pdev = to_platform_device(dev);
192 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
193
194 /* do not clear AIE here, it may be needed for wake */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800195 puv3_rtc_setpie(dev, 0);
196 free_irq(puv3_rtc_alarmno, rtc_dev);
197 free_irq(puv3_rtc_tickno, rtc_dev);
198}
199
200static const struct rtc_class_ops puv3_rtcops = {
201 .open = puv3_rtc_open,
202 .release = puv3_rtc_release,
203 .read_time = puv3_rtc_gettime,
204 .set_time = puv3_rtc_settime,
205 .read_alarm = puv3_rtc_getalarm,
206 .set_alarm = puv3_rtc_setalarm,
GuanXuetao02b2ee12011-01-15 18:19:03 +0800207 .proc = puv3_rtc_proc,
208};
209
210static void puv3_rtc_enable(struct platform_device *pdev, int en)
211{
212 if (!en) {
GuanXuetaoe5abf782011-02-26 21:21:18 +0800213 writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800214 } else {
215 /* re-enable the device, and check it is ok */
GuanXuetaoe5abf782011-02-26 21:21:18 +0800216 if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
GuanXuetao02b2ee12011-01-15 18:19:03 +0800217 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
GuanXuetaoe5abf782011-02-26 21:21:18 +0800218 writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800219 }
220 }
221}
222
223static int puv3_rtc_remove(struct platform_device *dev)
224{
225 struct rtc_device *rtc = platform_get_drvdata(dev);
226
227 platform_set_drvdata(dev, NULL);
228 rtc_device_unregister(rtc);
229
230 puv3_rtc_setpie(&dev->dev, 0);
231 puv3_rtc_setaie(0);
232
233 release_resource(puv3_rtc_mem);
234 kfree(puv3_rtc_mem);
235
236 return 0;
237}
238
239static int puv3_rtc_probe(struct platform_device *pdev)
240{
241 struct rtc_device *rtc;
242 struct resource *res;
243 int ret;
244
245 pr_debug("%s: probe=%p\n", __func__, pdev);
246
247 /* find the IRQs */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800248 puv3_rtc_tickno = platform_get_irq(pdev, 1);
249 if (puv3_rtc_tickno < 0) {
250 dev_err(&pdev->dev, "no irq for rtc tick\n");
251 return -ENOENT;
252 }
253
254 puv3_rtc_alarmno = platform_get_irq(pdev, 0);
255 if (puv3_rtc_alarmno < 0) {
256 dev_err(&pdev->dev, "no irq for alarm\n");
257 return -ENOENT;
258 }
259
260 pr_debug("PKUnity_rtc: tick irq %d, alarm irq %d\n",
261 puv3_rtc_tickno, puv3_rtc_alarmno);
262
263 /* get the memory region */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800264 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 if (res == NULL) {
266 dev_err(&pdev->dev, "failed to get memory region resource\n");
267 return -ENOENT;
268 }
269
270 puv3_rtc_mem = request_mem_region(res->start,
271 res->end-res->start+1,
272 pdev->name);
273
274 if (puv3_rtc_mem == NULL) {
275 dev_err(&pdev->dev, "failed to reserve memory region\n");
276 ret = -ENOENT;
277 goto err_nores;
278 }
279
280 puv3_rtc_enable(pdev, 1);
281
GuanXuetao02b2ee12011-01-15 18:19:03 +0800282 /* register RTC and exit */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800283 rtc = rtc_device_register("pkunity", &pdev->dev, &puv3_rtcops,
284 THIS_MODULE);
285
286 if (IS_ERR(rtc)) {
287 dev_err(&pdev->dev, "cannot attach rtc\n");
288 ret = PTR_ERR(rtc);
289 goto err_nortc;
290 }
291
292 /* platform setup code should have handled this; sigh */
293 if (!device_can_wakeup(&pdev->dev))
294 device_init_wakeup(&pdev->dev, 1);
295
296 platform_set_drvdata(pdev, rtc);
297 return 0;
298
299 err_nortc:
300 puv3_rtc_enable(pdev, 0);
301 release_resource(puv3_rtc_mem);
302
303 err_nores:
304 return ret;
305}
306
307#ifdef CONFIG_PM
308
GuanXuetao02b2ee12011-01-15 18:19:03 +0800309static int ticnt_save;
310
311static int puv3_rtc_suspend(struct platform_device *pdev, pm_message_t state)
312{
313 /* save RTAR for anyone using periodic interrupts */
GuanXuetaoe5abf782011-02-26 21:21:18 +0800314 ticnt_save = readl(RTC_RTAR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800315 puv3_rtc_enable(pdev, 0);
316 return 0;
317}
318
319static int puv3_rtc_resume(struct platform_device *pdev)
320{
321 puv3_rtc_enable(pdev, 1);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800322 writel(ticnt_save, RTC_RTAR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800323 return 0;
324}
325#else
326#define puv3_rtc_suspend NULL
327#define puv3_rtc_resume NULL
328#endif
329
330static struct platform_driver puv3_rtcdrv = {
331 .probe = puv3_rtc_probe,
332 .remove = __devexit_p(puv3_rtc_remove),
333 .suspend = puv3_rtc_suspend,
334 .resume = puv3_rtc_resume,
335 .driver = {
336 .name = "PKUnity-v3-RTC",
337 .owner = THIS_MODULE,
338 }
339};
340
341static char __initdata banner[] = "PKUnity-v3 RTC, (c) 2009 PKUnity Co.\n";
342
343static int __init puv3_rtc_init(void)
344{
345 printk(banner);
346 return platform_driver_register(&puv3_rtcdrv);
347}
348
349static void __exit puv3_rtc_exit(void)
350{
351 platform_driver_unregister(&puv3_rtcdrv);
352}
353
354module_init(puv3_rtc_init);
355module_exit(puv3_rtc_exit);
356
357MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
358MODULE_AUTHOR("Hu Dongliang");
359MODULE_LICENSE("GPL v2");