Linux 2.6.39-rc2
[pohmelfs.git] / arch / unicore32 / kernel / rtc.c
blob8cad70b3302c86e08c8189e7c926b34c3ab78f2c
1 /*
2 * linux/arch/unicore32/kernel/rtc.c
4 * Code specific to PKUnity SoC and UniCore ISA
6 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
7 * Copyright (C) 2001-2010 Guan Xuetao
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/fs.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/clk.h>
23 #include <linux/log2.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26 #include <linux/io.h>
28 #include <asm/irq.h>
29 #include <mach/hardware.h>
31 static struct resource *puv3_rtc_mem;
33 static int puv3_rtc_alarmno = IRQ_RTCAlarm;
34 static int puv3_rtc_tickno = IRQ_RTC;
36 static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
38 /* IRQ Handlers */
40 static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
42 struct rtc_device *rdev = id;
44 writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
45 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
46 return IRQ_HANDLED;
49 static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
51 struct rtc_device *rdev = id;
53 writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
54 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
55 return IRQ_HANDLED;
58 /* Update control registers */
59 static void puv3_rtc_setaie(int to)
61 unsigned int tmp;
63 pr_debug("%s: aie=%d\n", __func__, to);
65 tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
67 if (to)
68 tmp |= RTC_RTSR_ALE;
70 writel(tmp, RTC_RTSR);
73 static int puv3_rtc_setpie(struct device *dev, int enabled)
75 unsigned int tmp;
77 pr_debug("%s: pie=%d\n", __func__, enabled);
79 spin_lock_irq(&puv3_rtc_pie_lock);
80 tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
82 if (enabled)
83 tmp |= RTC_RTSR_HZE;
85 writel(tmp, RTC_RTSR);
86 spin_unlock_irq(&puv3_rtc_pie_lock);
88 return 0;
91 /* Time read/write */
93 static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
95 rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
97 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
98 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
99 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
101 return 0;
104 static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
106 unsigned long rtc_count = 0;
108 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
109 tm->tm_year, tm->tm_mon, tm->tm_mday,
110 tm->tm_hour, tm->tm_min, tm->tm_sec);
112 rtc_tm_to_time(tm, &rtc_count);
113 writel(rtc_count, RTC_RCNR);
115 return 0;
118 static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
120 struct rtc_time *alm_tm = &alrm->time;
122 rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
124 alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
126 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
127 alrm->enabled,
128 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
129 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
131 return 0;
134 static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
136 struct rtc_time *tm = &alrm->time;
137 unsigned long rtcalarm_count = 0;
139 pr_debug("puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
140 alrm->enabled,
141 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
142 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
144 rtc_tm_to_time(tm, &rtcalarm_count);
145 writel(rtcalarm_count, RTC_RTAR);
147 puv3_rtc_setaie(alrm->enabled);
149 if (alrm->enabled)
150 enable_irq_wake(puv3_rtc_alarmno);
151 else
152 disable_irq_wake(puv3_rtc_alarmno);
154 return 0;
157 static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
159 seq_printf(seq, "periodic_IRQ\t: %s\n",
160 (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
161 return 0;
164 static int puv3_rtc_open(struct device *dev)
166 struct platform_device *pdev = to_platform_device(dev);
167 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
168 int ret;
170 ret = request_irq(puv3_rtc_alarmno, puv3_rtc_alarmirq,
171 IRQF_DISABLED, "pkunity-rtc alarm", rtc_dev);
173 if (ret) {
174 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
175 return ret;
178 ret = request_irq(puv3_rtc_tickno, puv3_rtc_tickirq,
179 IRQF_DISABLED, "pkunity-rtc tick", rtc_dev);
181 if (ret) {
182 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
183 goto tick_err;
186 return ret;
188 tick_err:
189 free_irq(puv3_rtc_alarmno, rtc_dev);
190 return ret;
193 static void puv3_rtc_release(struct device *dev)
195 struct platform_device *pdev = to_platform_device(dev);
196 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
198 /* do not clear AIE here, it may be needed for wake */
200 puv3_rtc_setpie(dev, 0);
201 free_irq(puv3_rtc_alarmno, rtc_dev);
202 free_irq(puv3_rtc_tickno, rtc_dev);
205 static const struct rtc_class_ops puv3_rtcops = {
206 .open = puv3_rtc_open,
207 .release = puv3_rtc_release,
208 .read_time = puv3_rtc_gettime,
209 .set_time = puv3_rtc_settime,
210 .read_alarm = puv3_rtc_getalarm,
211 .set_alarm = puv3_rtc_setalarm,
212 .proc = puv3_rtc_proc,
215 static void puv3_rtc_enable(struct platform_device *pdev, int en)
217 if (!en) {
218 writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
219 } else {
220 /* re-enable the device, and check it is ok */
222 if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
223 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
224 writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
229 static int puv3_rtc_remove(struct platform_device *dev)
231 struct rtc_device *rtc = platform_get_drvdata(dev);
233 platform_set_drvdata(dev, NULL);
234 rtc_device_unregister(rtc);
236 puv3_rtc_setpie(&dev->dev, 0);
237 puv3_rtc_setaie(0);
239 release_resource(puv3_rtc_mem);
240 kfree(puv3_rtc_mem);
242 return 0;
245 static int puv3_rtc_probe(struct platform_device *pdev)
247 struct rtc_device *rtc;
248 struct resource *res;
249 int ret;
251 pr_debug("%s: probe=%p\n", __func__, pdev);
253 /* find the IRQs */
255 puv3_rtc_tickno = platform_get_irq(pdev, 1);
256 if (puv3_rtc_tickno < 0) {
257 dev_err(&pdev->dev, "no irq for rtc tick\n");
258 return -ENOENT;
261 puv3_rtc_alarmno = platform_get_irq(pdev, 0);
262 if (puv3_rtc_alarmno < 0) {
263 dev_err(&pdev->dev, "no irq for alarm\n");
264 return -ENOENT;
267 pr_debug("PKUnity_rtc: tick irq %d, alarm irq %d\n",
268 puv3_rtc_tickno, puv3_rtc_alarmno);
270 /* get the memory region */
272 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273 if (res == NULL) {
274 dev_err(&pdev->dev, "failed to get memory region resource\n");
275 return -ENOENT;
278 puv3_rtc_mem = request_mem_region(res->start,
279 res->end-res->start+1,
280 pdev->name);
282 if (puv3_rtc_mem == NULL) {
283 dev_err(&pdev->dev, "failed to reserve memory region\n");
284 ret = -ENOENT;
285 goto err_nores;
288 puv3_rtc_enable(pdev, 1);
290 /* register RTC and exit */
292 rtc = rtc_device_register("pkunity", &pdev->dev, &puv3_rtcops,
293 THIS_MODULE);
295 if (IS_ERR(rtc)) {
296 dev_err(&pdev->dev, "cannot attach rtc\n");
297 ret = PTR_ERR(rtc);
298 goto err_nortc;
301 /* platform setup code should have handled this; sigh */
302 if (!device_can_wakeup(&pdev->dev))
303 device_init_wakeup(&pdev->dev, 1);
305 platform_set_drvdata(pdev, rtc);
306 return 0;
308 err_nortc:
309 puv3_rtc_enable(pdev, 0);
310 release_resource(puv3_rtc_mem);
312 err_nores:
313 return ret;
316 #ifdef CONFIG_PM
318 /* RTC Power management control */
320 static int ticnt_save;
322 static int puv3_rtc_suspend(struct platform_device *pdev, pm_message_t state)
324 /* save RTAR for anyone using periodic interrupts */
325 ticnt_save = readl(RTC_RTAR);
326 puv3_rtc_enable(pdev, 0);
327 return 0;
330 static int puv3_rtc_resume(struct platform_device *pdev)
332 puv3_rtc_enable(pdev, 1);
333 writel(ticnt_save, RTC_RTAR);
334 return 0;
336 #else
337 #define puv3_rtc_suspend NULL
338 #define puv3_rtc_resume NULL
339 #endif
341 static struct platform_driver puv3_rtcdrv = {
342 .probe = puv3_rtc_probe,
343 .remove = __devexit_p(puv3_rtc_remove),
344 .suspend = puv3_rtc_suspend,
345 .resume = puv3_rtc_resume,
346 .driver = {
347 .name = "PKUnity-v3-RTC",
348 .owner = THIS_MODULE,
352 static char __initdata banner[] = "PKUnity-v3 RTC, (c) 2009 PKUnity Co.\n";
354 static int __init puv3_rtc_init(void)
356 printk(banner);
357 return platform_driver_register(&puv3_rtcdrv);
360 static void __exit puv3_rtc_exit(void)
362 platform_driver_unregister(&puv3_rtcdrv);
365 module_init(puv3_rtc_init);
366 module_exit(puv3_rtc_exit);
368 MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
369 MODULE_AUTHOR("Hu Dongliang");
370 MODULE_LICENSE("GPL v2");