aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-nuc900.c
diff options
context:
space:
mode:
authorWan ZongShun <mcuos.com@gmail.com>2009-12-15 19:46:17 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2009-12-16 10:20:00 -0500
commitafd49a7e92851239c3edffae730cc27080bfab78 (patch)
treec4f65a10e9dd4fc487375197c79bd0f275595857 /drivers/rtc/rtc-nuc900.c
parentd973b632a362cb4075fbe5363eba238b6f245b55 (diff)
ARM: NUC900: add RTC driver support for nuc910 and nuc920
Signed-off-by: Wan ZongShun <mcuos.com@gmail.com> Cc: Alessandro Zummo <a.zummo@towertech.it> Cc: Hu Ruihuan <specter118@gmail.com> Cc: Russell King <rmk@arm.linux.org.uk> Cc: "jack wang" <jack_wang@usish.com> Cc: David Brownell <david-b@pacbell.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/rtc/rtc-nuc900.c')
-rw-r--r--drivers/rtc/rtc-nuc900.c342
1 files changed, 342 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-nuc900.c b/drivers/rtc/rtc-nuc900.c
new file mode 100644
index 000000000000..bf59c9c586b2
--- /dev/null
+++ b/drivers/rtc/rtc-nuc900.c
@@ -0,0 +1,342 @@
1/*
2 * Copyright (c) 2008-2009 Nuvoton technology corporation.
3 *
4 * Wan ZongShun <mcuos.com@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation;version 2 of the License.
9 *
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/platform_device.h>
15#include <linux/rtc.h>
16#include <linux/delay.h>
17#include <linux/io.h>
18#include <linux/bcd.h>
19
20/* RTC Control Registers */
21#define REG_RTC_INIR 0x00
22#define REG_RTC_AER 0x04
23#define REG_RTC_FCR 0x08
24#define REG_RTC_TLR 0x0C
25#define REG_RTC_CLR 0x10
26#define REG_RTC_TSSR 0x14
27#define REG_RTC_DWR 0x18
28#define REG_RTC_TAR 0x1C
29#define REG_RTC_CAR 0x20
30#define REG_RTC_LIR 0x24
31#define REG_RTC_RIER 0x28
32#define REG_RTC_RIIR 0x2C
33#define REG_RTC_TTR 0x30
34
35#define RTCSET 0x01
36#define AERRWENB 0x10000
37#define INIRRESET 0xa5eb1357
38#define AERPOWERON 0xA965
39#define AERPOWEROFF 0x0000
40#define LEAPYEAR 0x0001
41#define TICKENB 0x80
42#define TICKINTENB 0x0002
43#define ALARMINTENB 0x0001
44#define MODE24 0x0001
45
46struct nuc900_rtc {
47 int irq_num;
48 void __iomem *rtc_reg;
49 struct rtc_device *rtcdev;
50};
51
52struct nuc900_bcd_time {
53 int bcd_sec;
54 int bcd_min;
55 int bcd_hour;
56 int bcd_mday;
57 int bcd_mon;
58 int bcd_year;
59};
60
61static irqreturn_t nuc900_rtc_interrupt(int irq, void *_rtc)
62{
63 struct nuc900_rtc *rtc = _rtc;
64 unsigned long events = 0, rtc_irq;
65
66 rtc_irq = __raw_readl(rtc->rtc_reg + REG_RTC_RIIR);
67
68 if (rtc_irq & ALARMINTENB) {
69 rtc_irq &= ~ALARMINTENB;
70 __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR);
71 events |= RTC_AF | RTC_IRQF;
72 }
73
74 if (rtc_irq & TICKINTENB) {
75 rtc_irq &= ~TICKINTENB;
76 __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR);
77 events |= RTC_UF | RTC_IRQF;
78 }
79
80 rtc_update_irq(rtc->rtcdev, 1, events);
81
82 return IRQ_HANDLED;
83}
84
85static int *check_rtc_access_enable(struct nuc900_rtc *nuc900_rtc)
86{
87 unsigned int i;
88 __raw_writel(INIRRESET, nuc900_rtc->rtc_reg + REG_RTC_INIR);
89
90 mdelay(10);
91
92 __raw_writel(AERPOWERON, nuc900_rtc->rtc_reg + REG_RTC_AER);
93
94 for (i = 0; i < 1000; i++) {
95 if (__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_AER) & AERRWENB)
96 return 0;
97 }
98
99 if ((__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_AER) & AERRWENB) == 0x0)
100 return ERR_PTR(-ENODEV);
101
102 return ERR_PTR(-EPERM);
103}
104
105static void nuc900_rtc_bcd2bin(unsigned int timereg,
106 unsigned int calreg, struct rtc_time *tm)
107{
108 tm->tm_mday = bcd2bin(calreg >> 0);
109 tm->tm_mon = bcd2bin(calreg >> 8);
110 tm->tm_year = bcd2bin(calreg >> 16) + 100;
111
112 tm->tm_sec = bcd2bin(timereg >> 0);
113 tm->tm_min = bcd2bin(timereg >> 8);
114 tm->tm_hour = bcd2bin(timereg >> 16);
115
116 rtc_valid_tm(tm);
117}
118
119static void nuc900_rtc_bin2bcd(struct rtc_time *settm,
120 struct nuc900_bcd_time *gettm)
121{
122 gettm->bcd_mday = bin2bcd(settm->tm_mday) << 0;
123 gettm->bcd_mon = bin2bcd(settm->tm_mon) << 8;
124 gettm->bcd_year = bin2bcd(settm->tm_year - 100) << 16;
125
126 gettm->bcd_sec = bin2bcd(settm->tm_sec) << 0;
127 gettm->bcd_min = bin2bcd(settm->tm_min) << 8;
128 gettm->bcd_hour = bin2bcd(settm->tm_hour) << 16;
129}
130
131static int nuc900_update_irq_enable(struct device *dev, unsigned int enabled)
132{
133 struct nuc900_rtc *rtc = dev_get_drvdata(dev);
134
135 if (enabled)
136 __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)|
137 (TICKINTENB), rtc->rtc_reg + REG_RTC_RIER);
138 else
139 __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)&
140 (~TICKINTENB), rtc->rtc_reg + REG_RTC_RIER);
141
142 return 0;
143}
144
145static int nuc900_alarm_irq_enable(struct device *dev, unsigned int enabled)
146{
147 struct nuc900_rtc *rtc = dev_get_drvdata(dev);
148
149 if (enabled)
150 __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)|
151 (ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER);
152 else
153 __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)&
154 (~ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER);
155
156 return 0;
157}
158
159static int nuc900_rtc_read_time(struct device *dev, struct rtc_time *tm)
160{
161 struct nuc900_rtc *rtc = dev_get_drvdata(dev);
162 unsigned int timeval, clrval;
163
164 timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TLR);
165 clrval = __raw_readl(rtc->rtc_reg + REG_RTC_CLR);
166
167 nuc900_rtc_bcd2bin(timeval, clrval, tm);
168
169 return 0;
170}
171
172static int nuc900_rtc_set_time(struct device *dev, struct rtc_time *tm)
173{
174 struct nuc900_rtc *rtc = dev_get_drvdata(dev);
175 struct nuc900_bcd_time gettm;
176 unsigned long val;
177 int *err;
178
179 nuc900_rtc_bin2bcd(tm, &gettm);
180
181 err = check_rtc_access_enable(rtc);
182 if (IS_ERR(err))
183 return PTR_ERR(err);
184
185 val = gettm.bcd_mday | gettm.bcd_mon | gettm.bcd_year;
186 __raw_writel(val, rtc->rtc_reg + REG_RTC_CLR);
187
188 val = gettm.bcd_sec | gettm.bcd_min | gettm.bcd_hour;
189 __raw_writel(val, rtc->rtc_reg + REG_RTC_TLR);
190
191 return 0;
192}
193
194static int nuc900_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195{
196 struct nuc900_rtc *rtc = dev_get_drvdata(dev);
197 unsigned int timeval, carval;
198
199 timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TAR);
200 carval = __raw_readl(rtc->rtc_reg + REG_RTC_CAR);
201
202 nuc900_rtc_bcd2bin(timeval, carval, &alrm->time);
203
204 return 0;
205}
206
207static int nuc900_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
208{
209 struct nuc900_rtc *rtc = dev_get_drvdata(dev);
210 struct nuc900_bcd_time tm;
211 unsigned long val;
212 int *err;
213
214 nuc900_rtc_bin2bcd(&alrm->time, &tm);
215
216 err = check_rtc_access_enable(rtc);
217 if (IS_ERR(err))
218 return PTR_ERR(err);
219
220 val = tm.bcd_mday | tm.bcd_mon | tm.bcd_year;
221 __raw_writel(val, rtc->rtc_reg + REG_RTC_CAR);
222
223 val = tm.bcd_sec | tm.bcd_min | tm.bcd_hour;
224 __raw_writel(val, rtc->rtc_reg + REG_RTC_TAR);
225
226 return 0;
227}
228
229static struct rtc_class_ops nuc900_rtc_ops = {
230 .read_time = nuc900_rtc_read_time,
231 .set_time = nuc900_rtc_set_time,
232 .read_alarm = nuc900_rtc_read_alarm,
233 .set_alarm = nuc900_rtc_set_alarm,
234 .alarm_irq_enable = nuc900_alarm_irq_enable,
235 .update_irq_enable = nuc900_update_irq_enable,
236};
237
238static int __devinit nuc900_rtc_probe(struct platform_device *pdev)
239{
240 struct resource *res;
241 struct nuc900_rtc *nuc900_rtc;
242 int err = 0;
243
244 nuc900_rtc = kzalloc(sizeof(struct nuc900_rtc), GFP_KERNEL);
245 if (!nuc900_rtc) {
246 dev_err(&pdev->dev, "kzalloc nuc900_rtc failed\n");
247 return -ENOMEM;
248 }
249 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
250 if (!res) {
251 dev_err(&pdev->dev, "platform_get_resource failed\n");
252 err = -ENXIO;
253 goto fail1;
254 }
255
256 if (!request_mem_region(res->start, resource_size(res),
257 pdev->name)) {
258 dev_err(&pdev->dev, "request_mem_region failed\n");
259 err = -EBUSY;
260 goto fail1;
261 }
262
263 nuc900_rtc->rtc_reg = ioremap(res->start, resource_size(res));
264 if (!nuc900_rtc->rtc_reg) {
265 dev_err(&pdev->dev, "ioremap rtc_reg failed\n");
266 err = -ENOMEM;
267 goto fail2;
268 }
269
270 nuc900_rtc->irq_num = platform_get_irq(pdev, 0);
271 if (request_irq(nuc900_rtc->irq_num, nuc900_rtc_interrupt,
272 IRQF_DISABLED, "nuc900rtc", nuc900_rtc)) {
273 dev_err(&pdev->dev, "NUC900 RTC request irq failed\n");
274 err = -EBUSY;
275 goto fail3;
276 }
277
278 nuc900_rtc->rtcdev = rtc_device_register(pdev->name, &pdev->dev,
279 &nuc900_rtc_ops, THIS_MODULE);
280 if (IS_ERR(nuc900_rtc->rtcdev)) {
281 dev_err(&pdev->dev, "rtc device register faild\n");
282 err = PTR_ERR(nuc900_rtc->rtcdev);
283 goto fail4;
284 }
285
286 platform_set_drvdata(pdev, nuc900_rtc);
287 __raw_writel(__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_TSSR) | MODE24,
288 nuc900_rtc->rtc_reg + REG_RTC_TSSR);
289
290 return 0;
291
292fail4: free_irq(nuc900_rtc->irq_num, nuc900_rtc);
293fail3: iounmap(nuc900_rtc->rtc_reg);
294fail2: release_mem_region(res->start, resource_size(res));
295fail1: kfree(nuc900_rtc);
296 return err;
297}
298
299static int __devexit nuc900_rtc_remove(struct platform_device *pdev)
300{
301 struct nuc900_rtc *nuc900_rtc = platform_get_drvdata(pdev);
302 struct resource *res;
303
304 rtc_device_unregister(nuc900_rtc->rtcdev);
305 free_irq(nuc900_rtc->irq_num, nuc900_rtc);
306 iounmap(nuc900_rtc->rtc_reg);
307
308 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309 release_mem_region(res->start, resource_size(res));
310
311 kfree(nuc900_rtc);
312
313 platform_set_drvdata(pdev, NULL);
314
315 return 0;
316}
317
318static struct platform_driver nuc900_rtc_driver = {
319 .remove = __devexit_p(nuc900_rtc_remove),
320 .driver = {
321 .name = "nuc900-rtc",
322 .owner = THIS_MODULE,
323 },
324};
325
326static int __init nuc900_rtc_init(void)
327{
328 return platform_driver_probe(&nuc900_rtc_driver, nuc900_rtc_probe);
329}
330
331static void __exit nuc900_rtc_exit(void)
332{
333 platform_driver_unregister(&nuc900_rtc_driver);
334}
335
336module_init(nuc900_rtc_init);
337module_exit(nuc900_rtc_exit);
338
339MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
340MODULE_DESCRIPTION("nuc910/nuc920 RTC driver");
341MODULE_LICENSE("GPL");
342MODULE_ALIAS("platform:nuc900-rtc");