2 * RTC driver code specific to PKUnity SoC and UniCore ISA
4 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
5 * Copyright (C) 2001-2010 Guan Xuetao
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.
12 #include <linux/module.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>
27 #include <mach/hardware.h>
29 static struct resource
*puv3_rtc_mem
;
31 static int puv3_rtc_alarmno
= IRQ_RTCAlarm
;
32 static int puv3_rtc_tickno
= IRQ_RTC
;
34 static DEFINE_SPINLOCK(puv3_rtc_pie_lock
);
37 static irqreturn_t
puv3_rtc_alarmirq(int irq
, void *id
)
39 struct rtc_device
*rdev
= id
;
41 writel(readl(RTC_RTSR
) | RTC_RTSR_AL
, RTC_RTSR
);
42 rtc_update_irq(rdev
, 1, RTC_AF
| RTC_IRQF
);
46 static irqreturn_t
puv3_rtc_tickirq(int irq
, void *id
)
48 struct rtc_device
*rdev
= id
;
50 writel(readl(RTC_RTSR
) | RTC_RTSR_HZ
, RTC_RTSR
);
51 rtc_update_irq(rdev
, 1, RTC_PF
| RTC_IRQF
);
55 /* Update control registers */
56 static void puv3_rtc_setaie(struct device
*dev
, int to
)
60 dev_dbg(dev
, "%s: aie=%d\n", __func__
, to
);
62 tmp
= readl(RTC_RTSR
) & ~RTC_RTSR_ALE
;
67 writel(tmp
, RTC_RTSR
);
70 static int puv3_rtc_setpie(struct device
*dev
, int enabled
)
74 dev_dbg(dev
, "%s: pie=%d\n", __func__
, enabled
);
76 spin_lock_irq(&puv3_rtc_pie_lock
);
77 tmp
= readl(RTC_RTSR
) & ~RTC_RTSR_HZE
;
82 writel(tmp
, RTC_RTSR
);
83 spin_unlock_irq(&puv3_rtc_pie_lock
);
89 static int puv3_rtc_gettime(struct device
*dev
, struct rtc_time
*rtc_tm
)
91 rtc_time_to_tm(readl(RTC_RCNR
), rtc_tm
);
93 dev_dbg(dev
, "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
);
100 static int puv3_rtc_settime(struct device
*dev
, struct rtc_time
*tm
)
102 unsigned long rtc_count
= 0;
104 dev_dbg(dev
, "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
);
108 rtc_tm_to_time(tm
, &rtc_count
);
109 writel(rtc_count
, RTC_RCNR
);
114 static int puv3_rtc_getalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
116 struct rtc_time
*alm_tm
= &alrm
->time
;
118 rtc_time_to_tm(readl(RTC_RTAR
), alm_tm
);
120 alrm
->enabled
= readl(RTC_RTSR
) & RTC_RTSR_ALE
;
122 dev_dbg(dev
, "read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
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
);
130 static int puv3_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
132 struct rtc_time
*tm
= &alrm
->time
;
133 unsigned long rtcalarm_count
= 0;
135 dev_dbg(dev
, "puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
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
);
140 rtc_tm_to_time(tm
, &rtcalarm_count
);
141 writel(rtcalarm_count
, RTC_RTAR
);
143 puv3_rtc_setaie(dev
, alrm
->enabled
);
146 enable_irq_wake(puv3_rtc_alarmno
);
148 disable_irq_wake(puv3_rtc_alarmno
);
153 static int puv3_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
155 seq_printf(seq
, "periodic_IRQ\t: %s\n",
156 (readl(RTC_RTSR
) & RTC_RTSR_HZE
) ? "yes" : "no");
160 static const struct rtc_class_ops puv3_rtcops
= {
161 .read_time
= puv3_rtc_gettime
,
162 .set_time
= puv3_rtc_settime
,
163 .read_alarm
= puv3_rtc_getalarm
,
164 .set_alarm
= puv3_rtc_setalarm
,
165 .proc
= puv3_rtc_proc
,
168 static void puv3_rtc_enable(struct device
*dev
, int en
)
171 writel(readl(RTC_RTSR
) & ~RTC_RTSR_HZE
, RTC_RTSR
);
173 /* re-enable the device, and check it is ok */
174 if ((readl(RTC_RTSR
) & RTC_RTSR_HZE
) == 0) {
175 dev_info(dev
, "rtc disabled, re-enabling\n");
176 writel(readl(RTC_RTSR
) | RTC_RTSR_HZE
, RTC_RTSR
);
181 static int puv3_rtc_remove(struct platform_device
*dev
)
183 puv3_rtc_setpie(&dev
->dev
, 0);
184 puv3_rtc_setaie(&dev
->dev
, 0);
186 release_resource(puv3_rtc_mem
);
192 static int puv3_rtc_probe(struct platform_device
*pdev
)
194 struct rtc_device
*rtc
;
195 struct resource
*res
;
198 dev_dbg(&pdev
->dev
, "%s: probe=%p\n", __func__
, pdev
);
201 puv3_rtc_tickno
= platform_get_irq(pdev
, 1);
202 if (puv3_rtc_tickno
< 0) {
203 dev_err(&pdev
->dev
, "no irq for rtc tick\n");
207 puv3_rtc_alarmno
= platform_get_irq(pdev
, 0);
208 if (puv3_rtc_alarmno
< 0) {
209 dev_err(&pdev
->dev
, "no irq for alarm\n");
213 dev_dbg(&pdev
->dev
, "PKUnity_rtc: tick irq %d, alarm irq %d\n",
214 puv3_rtc_tickno
, puv3_rtc_alarmno
);
216 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
220 ret
= devm_request_irq(&pdev
->dev
, puv3_rtc_alarmno
, puv3_rtc_alarmirq
,
221 0, "pkunity-rtc alarm", rtc
);
223 dev_err(&pdev
->dev
, "IRQ%d error %d\n", puv3_rtc_alarmno
, ret
);
227 ret
= devm_request_irq(&pdev
->dev
, puv3_rtc_tickno
, puv3_rtc_tickirq
,
228 0, "pkunity-rtc tick", rtc
);
230 dev_err(&pdev
->dev
, "IRQ%d error %d\n", puv3_rtc_tickno
, ret
);
234 /* get the memory region */
235 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
237 dev_err(&pdev
->dev
, "failed to get memory region resource\n");
241 puv3_rtc_mem
= request_mem_region(res
->start
, resource_size(res
),
244 if (puv3_rtc_mem
== NULL
) {
245 dev_err(&pdev
->dev
, "failed to reserve memory region\n");
250 puv3_rtc_enable(&pdev
->dev
, 1);
252 /* register RTC and exit */
253 rtc
->ops
= &puv3_rtcops
;
254 ret
= rtc_register_device(rtc
);
256 dev_err(&pdev
->dev
, "cannot attach rtc\n");
260 /* platform setup code should have handled this; sigh */
261 if (!device_can_wakeup(&pdev
->dev
))
262 device_init_wakeup(&pdev
->dev
, 1);
264 platform_set_drvdata(pdev
, rtc
);
268 puv3_rtc_enable(&pdev
->dev
, 0);
269 release_resource(puv3_rtc_mem
);
275 #ifdef CONFIG_PM_SLEEP
276 static int ticnt_save
;
278 static int puv3_rtc_suspend(struct device
*dev
)
280 /* save RTAR for anyone using periodic interrupts */
281 ticnt_save
= readl(RTC_RTAR
);
282 puv3_rtc_enable(dev
, 0);
286 static int puv3_rtc_resume(struct device
*dev
)
288 puv3_rtc_enable(dev
, 1);
289 writel(ticnt_save
, RTC_RTAR
);
294 static SIMPLE_DEV_PM_OPS(puv3_rtc_pm_ops
, puv3_rtc_suspend
, puv3_rtc_resume
);
296 static struct platform_driver puv3_rtc_driver
= {
297 .probe
= puv3_rtc_probe
,
298 .remove
= puv3_rtc_remove
,
300 .name
= "PKUnity-v3-RTC",
301 .pm
= &puv3_rtc_pm_ops
,
305 module_platform_driver(puv3_rtc_driver
);
307 MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
308 MODULE_AUTHOR("Hu Dongliang");
309 MODULE_LICENSE("GPL v2");