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>
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>
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);
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);
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);
58 /* Update control registers */
59 static void puv3_rtc_setaie(int to)
63 pr_debug("%s: aie=%d\n", __func__, to);
65 tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
70 writel(tmp, RTC_RTSR);
73 static int puv3_rtc_setpie(struct device *dev, int enabled)
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;
85 writel(tmp, RTC_RTSR);
86 spin_unlock_irq(&puv3_rtc_pie_lock);
91 static int puv3_rtc_setfreq(struct device *dev, int freq)
98 static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
100 rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
102 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
103 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
104 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
109 static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
111 unsigned long rtc_count = 0;
113 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
114 tm->tm_year, tm->tm_mon, tm->tm_mday,
115 tm->tm_hour, tm->tm_min, tm->tm_sec);
117 rtc_tm_to_time(tm, &rtc_count);
118 writel(rtc_count, RTC_RCNR);
123 static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
125 struct rtc_time *alm_tm = &alrm->time;
127 rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
129 alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
131 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
133 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
134 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
139 static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
141 struct rtc_time *tm = &alrm->time;
142 unsigned long rtcalarm_count = 0;
144 pr_debug("puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
146 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
147 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
149 rtc_tm_to_time(tm, &rtcalarm_count);
150 writel(rtcalarm_count, RTC_RTAR);
152 puv3_rtc_setaie(alrm->enabled);
155 enable_irq_wake(puv3_rtc_alarmno);
157 disable_irq_wake(puv3_rtc_alarmno);
162 static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
164 seq_printf(seq, "periodic_IRQ\t: %s\n",
165 (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
169 static int puv3_rtc_open(struct device *dev)
171 struct platform_device *pdev = to_platform_device(dev);
172 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
175 ret = request_irq(puv3_rtc_alarmno, puv3_rtc_alarmirq,
176 IRQF_DISABLED, "pkunity-rtc alarm", rtc_dev);
179 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
183 ret = request_irq(puv3_rtc_tickno, puv3_rtc_tickirq,
184 IRQF_DISABLED, "pkunity-rtc tick", rtc_dev);
187 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
194 free_irq(puv3_rtc_alarmno, rtc_dev);
198 static void puv3_rtc_release(struct device *dev)
200 struct platform_device *pdev = to_platform_device(dev);
201 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
203 /* do not clear AIE here, it may be needed for wake */
205 puv3_rtc_setpie(dev, 0);
206 free_irq(puv3_rtc_alarmno, rtc_dev);
207 free_irq(puv3_rtc_tickno, rtc_dev);
210 static const struct rtc_class_ops puv3_rtcops = {
211 .open = puv3_rtc_open,
212 .release = puv3_rtc_release,
213 .read_time = puv3_rtc_gettime,
214 .set_time = puv3_rtc_settime,
215 .read_alarm = puv3_rtc_getalarm,
216 .set_alarm = puv3_rtc_setalarm,
217 .irq_set_freq = puv3_rtc_setfreq,
218 .irq_set_state = puv3_rtc_setpie,
219 .proc = puv3_rtc_proc,
222 static void puv3_rtc_enable(struct platform_device *pdev, int en)
225 writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
227 /* re-enable the device, and check it is ok */
229 if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
230 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
231 writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
236 static int puv3_rtc_remove(struct platform_device *dev)
238 struct rtc_device *rtc = platform_get_drvdata(dev);
240 platform_set_drvdata(dev, NULL);
241 rtc_device_unregister(rtc);
243 puv3_rtc_setpie(&dev->dev, 0);
246 release_resource(puv3_rtc_mem);
252 static int puv3_rtc_probe(struct platform_device *pdev)
254 struct rtc_device *rtc;
255 struct resource *res;
258 pr_debug("%s: probe=%p\n", __func__, pdev);
262 puv3_rtc_tickno = platform_get_irq(pdev, 1);
263 if (puv3_rtc_tickno < 0) {
264 dev_err(&pdev->dev, "no irq for rtc tick\n");
268 puv3_rtc_alarmno = platform_get_irq(pdev, 0);
269 if (puv3_rtc_alarmno < 0) {
270 dev_err(&pdev->dev, "no irq for alarm\n");
274 pr_debug("PKUnity_rtc: tick irq %d, alarm irq %d\n",
275 puv3_rtc_tickno, puv3_rtc_alarmno);
277 /* get the memory region */
279 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
281 dev_err(&pdev->dev, "failed to get memory region resource\n");
285 puv3_rtc_mem = request_mem_region(res->start,
286 res->end-res->start+1,
289 if (puv3_rtc_mem == NULL) {
290 dev_err(&pdev->dev, "failed to reserve memory region\n");
295 puv3_rtc_enable(pdev, 1);
297 puv3_rtc_setfreq(&pdev->dev, 1);
299 /* register RTC and exit */
301 rtc = rtc_device_register("pkunity", &pdev->dev, &puv3_rtcops,
305 dev_err(&pdev->dev, "cannot attach rtc\n");
310 /* platform setup code should have handled this; sigh */
311 if (!device_can_wakeup(&pdev->dev))
312 device_init_wakeup(&pdev->dev, 1);
314 platform_set_drvdata(pdev, rtc);
318 puv3_rtc_enable(pdev, 0);
319 release_resource(puv3_rtc_mem);
327 /* RTC Power management control */
329 static int ticnt_save;
331 static int puv3_rtc_suspend(struct platform_device *pdev, pm_message_t state)
333 /* save RTAR for anyone using periodic interrupts */
334 ticnt_save = readl(RTC_RTAR);
335 puv3_rtc_enable(pdev, 0);
339 static int puv3_rtc_resume(struct platform_device *pdev)
341 puv3_rtc_enable(pdev, 1);
342 writel(ticnt_save, RTC_RTAR);
346 #define puv3_rtc_suspend NULL
347 #define puv3_rtc_resume NULL
350 static struct platform_driver puv3_rtcdrv = {
351 .probe = puv3_rtc_probe,
352 .remove = __devexit_p(puv3_rtc_remove),
353 .suspend = puv3_rtc_suspend,
354 .resume = puv3_rtc_resume,
356 .name = "PKUnity-v3-RTC",
357 .owner = THIS_MODULE,
361 static char __initdata banner[] = "PKUnity-v3 RTC, (c) 2009 PKUnity Co.\n";
363 static int __init puv3_rtc_init(void)
366 return platform_driver_register(&puv3_rtcdrv);
369 static void __exit puv3_rtc_exit(void)
371 platform_driver_unregister(&puv3_rtcdrv);
374 module_init(puv3_rtc_init);
375 module_exit(puv3_rtc_exit);
377 MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
378 MODULE_AUTHOR("Hu Dongliang");
379 MODULE_LICENSE("GPL v2");