aboutsummaryrefslogtreecommitdiffstats
path: root/arch/unicore32/kernel/rtc.c
diff options
context:
space:
mode:
authorGuanXuetao <gxt@mprc.pku.edu.cn>2011-01-15 05:19:03 -0500
committerGuanXuetao <gxt@mprc.pku.edu.cn>2011-03-16 21:19:10 -0400
commit02b2ee16cc31df2b23d6f6c68a597d947f6c10e8 (patch)
treeac92441c0286fc3458339589c4e9de27e6be6067 /arch/unicore32/kernel/rtc.c
parent10c9c10c31514564b09c153432a42ffaea3ce831 (diff)
unicore32 core architecture: timer and time handling
This patch implements timer and time. RTC and PWM device drivers are also here. Signed-off-by: Guan Xuetao <gxt@mprc.pku.edu.cn>
Diffstat (limited to 'arch/unicore32/kernel/rtc.c')
-rw-r--r--arch/unicore32/kernel/rtc.c380
1 files changed, 380 insertions, 0 deletions
diff --git a/arch/unicore32/kernel/rtc.c b/arch/unicore32/kernel/rtc.c
new file mode 100644
index 000000000000..5e4db4158589
--- /dev/null
+++ b/arch/unicore32/kernel/rtc.c
@@ -0,0 +1,380 @@
1/*
2 * linux/arch/unicore32/kernel/rtc.c
3 *
4 * Code specific to PKUnity SoC and UniCore ISA
5 *
6 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
7 * Copyright (C) 2001-2010 Guan Xuetao
8 *
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.
12 */
13
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>
27
28#include <asm/irq.h>
29#include <mach/hardware.h>
30
31static struct resource *puv3_rtc_mem;
32
33static int puv3_rtc_alarmno = IRQ_RTCAlarm;
34static int puv3_rtc_tickno = IRQ_RTC;
35
36static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
37
38/* IRQ Handlers */
39
40static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
41{
42 struct rtc_device *rdev = id;
43
44 RTC_RTSR |= RTC_RTSR_AL;
45 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
46 return IRQ_HANDLED;
47}
48
49static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
50{
51 struct rtc_device *rdev = id;
52
53 RTC_RTSR |= RTC_RTSR_HZ;
54 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
55 return IRQ_HANDLED;
56}
57
58/* Update control registers */
59static void puv3_rtc_setaie(int to)
60{
61 unsigned int tmp;
62
63 pr_debug("%s: aie=%d\n", __func__, to);
64
65 tmp = RTC_RTSR & ~RTC_RTSR_ALE;
66
67 if (to)
68 tmp |= RTC_RTSR_ALE;
69
70 RTC_RTSR = tmp;
71}
72
73static int puv3_rtc_setpie(struct device *dev, int enabled)
74{
75 unsigned int tmp;
76
77 pr_debug("%s: pie=%d\n", __func__, enabled);
78
79 spin_lock_irq(&puv3_rtc_pie_lock);
80 tmp = RTC_RTSR & ~RTC_RTSR_HZE;
81
82 if (enabled)
83 tmp |= RTC_RTSR_HZE;
84
85 RTC_RTSR = tmp;
86 spin_unlock_irq(&puv3_rtc_pie_lock);
87
88 return 0;
89}
90
91static int puv3_rtc_setfreq(struct device *dev, int freq)
92{
93 return 0;
94}
95
96/* Time read/write */
97
98static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
99{
100 rtc_time_to_tm(RTC_RCNR, rtc_tm);
101
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);
105
106 return 0;
107}
108
109static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
110{
111 unsigned long rtc_count = 0;
112
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);
116
117 rtc_tm_to_time(tm, &rtc_count);
118 RTC_RCNR = rtc_count;
119
120 return 0;
121}
122
123static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
124{
125 struct rtc_time *alm_tm = &alrm->time;
126
127 rtc_time_to_tm(RTC_RTAR, alm_tm);
128
129 alrm->enabled = RTC_RTSR & RTC_RTSR_ALE;
130
131 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
132 alrm->enabled,
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);
135
136 return 0;
137}
138
139static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
140{
141 struct rtc_time *tm = &alrm->time;
142 unsigned long rtcalarm_count = 0;
143
144 pr_debug("puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
145 alrm->enabled,
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);
148
149 rtc_tm_to_time(tm, &rtcalarm_count);
150 RTC_RTAR = rtcalarm_count;
151
152 puv3_rtc_setaie(alrm->enabled);
153
154 if (alrm->enabled)
155 enable_irq_wake(puv3_rtc_alarmno);
156 else
157 disable_irq_wake(puv3_rtc_alarmno);
158
159 return 0;
160}
161
162static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
163{
164 seq_printf(seq, "periodic_IRQ\t: %s\n",
165 (RTC_RTSR & RTC_RTSR_HZE) ? "yes" : "no");
166 return 0;
167}
168
169static int puv3_rtc_open(struct device *dev)
170{
171 struct platform_device *pdev = to_platform_device(dev);
172 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
173 int ret;
174
175 ret = request_irq(puv3_rtc_alarmno, puv3_rtc_alarmirq,
176 IRQF_DISABLED, "pkunity-rtc alarm", rtc_dev);
177
178 if (ret) {
179 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
180 return ret;
181 }
182
183 ret = request_irq(puv3_rtc_tickno, puv3_rtc_tickirq,
184 IRQF_DISABLED, "pkunity-rtc tick", rtc_dev);
185
186 if (ret) {
187 dev_err(dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
188 goto tick_err;
189 }
190
191 return ret;
192
193 tick_err:
194 free_irq(puv3_rtc_alarmno, rtc_dev);
195 return ret;
196}
197
198static void puv3_rtc_release(struct device *dev)
199{
200 struct platform_device *pdev = to_platform_device(dev);
201 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
202
203 /* do not clear AIE here, it may be needed for wake */
204
205 puv3_rtc_setpie(dev, 0);
206 free_irq(puv3_rtc_alarmno, rtc_dev);
207 free_irq(puv3_rtc_tickno, rtc_dev);
208}
209
210static 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,
220};
221
222static void puv3_rtc_enable(struct platform_device *pdev, int en)
223{
224 if (!en) {
225 RTC_RTSR &= ~RTC_RTSR_HZE;
226 } else {
227 /* re-enable the device, and check it is ok */
228
229 if ((RTC_RTSR & RTC_RTSR_HZE) == 0) {
230 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
231 RTC_RTSR |= RTC_RTSR_HZE;
232 }
233 }
234}
235
236static int puv3_rtc_remove(struct platform_device *dev)
237{
238 struct rtc_device *rtc = platform_get_drvdata(dev);
239
240 platform_set_drvdata(dev, NULL);
241 rtc_device_unregister(rtc);
242
243 puv3_rtc_setpie(&dev->dev, 0);
244 puv3_rtc_setaie(0);
245
246 release_resource(puv3_rtc_mem);
247 kfree(puv3_rtc_mem);
248
249 return 0;
250}
251
252static int puv3_rtc_probe(struct platform_device *pdev)
253{
254 struct rtc_device *rtc;
255 struct resource *res;
256 int ret;
257
258 pr_debug("%s: probe=%p\n", __func__, pdev);
259
260 /* find the IRQs */
261
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");
265 return -ENOENT;
266 }
267
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");
271 return -ENOENT;
272 }
273
274 pr_debug("PKUnity_rtc: tick irq %d, alarm irq %d\n",
275 puv3_rtc_tickno, puv3_rtc_alarmno);
276
277 /* get the memory region */
278
279 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
280 if (res == NULL) {
281 dev_err(&pdev->dev, "failed to get memory region resource\n");
282 return -ENOENT;
283 }
284
285 puv3_rtc_mem = request_mem_region(res->start,
286 res->end-res->start+1,
287 pdev->name);
288
289 if (puv3_rtc_mem == NULL) {
290 dev_err(&pdev->dev, "failed to reserve memory region\n");
291 ret = -ENOENT;
292 goto err_nores;
293 }
294
295 puv3_rtc_enable(pdev, 1);
296
297 puv3_rtc_setfreq(&pdev->dev, 1);
298
299 /* register RTC and exit */
300
301 rtc = rtc_device_register("pkunity", &pdev->dev, &puv3_rtcops,
302 THIS_MODULE);
303
304 if (IS_ERR(rtc)) {
305 dev_err(&pdev->dev, "cannot attach rtc\n");
306 ret = PTR_ERR(rtc);
307 goto err_nortc;
308 }
309
310 /* platform setup code should have handled this; sigh */
311 if (!device_can_wakeup(&pdev->dev))
312 device_init_wakeup(&pdev->dev, 1);
313
314 platform_set_drvdata(pdev, rtc);
315 return 0;
316
317 err_nortc:
318 puv3_rtc_enable(pdev, 0);
319 release_resource(puv3_rtc_mem);
320
321 err_nores:
322 return ret;
323}
324
325#ifdef CONFIG_PM
326
327/* RTC Power management control */
328
329static int ticnt_save;
330
331static int puv3_rtc_suspend(struct platform_device *pdev, pm_message_t state)
332{
333 /* save RTAR for anyone using periodic interrupts */
334 ticnt_save = RTC_RTAR;
335 puv3_rtc_enable(pdev, 0);
336 return 0;
337}
338
339static int puv3_rtc_resume(struct platform_device *pdev)
340{
341 puv3_rtc_enable(pdev, 1);
342 RTC_RTAR = ticnt_save;
343 return 0;
344}
345#else
346#define puv3_rtc_suspend NULL
347#define puv3_rtc_resume NULL
348#endif
349
350static 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,
355 .driver = {
356 .name = "PKUnity-v3-RTC",
357 .owner = THIS_MODULE,
358 }
359};
360
361static char __initdata banner[] = "PKUnity-v3 RTC, (c) 2009 PKUnity Co.\n";
362
363static int __init puv3_rtc_init(void)
364{
365 printk(banner);
366 return platform_driver_register(&puv3_rtcdrv);
367}
368
369static void __exit puv3_rtc_exit(void)
370{
371 platform_driver_unregister(&puv3_rtcdrv);
372}
373
374module_init(puv3_rtc_init);
375module_exit(puv3_rtc_exit);
376
377MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
378MODULE_AUTHOR("Hu Dongliang");
379MODULE_LICENSE("GPL v2");
380