blob: ce2f78de7a80b9ce2285ad7c8d4fffb02179855e [file] [log] [blame]
Yoichi Yuasa8417eb72006-04-10 22:54:47 -07001/*
2 * Driver for NEC VR4100 series Real Time Clock unit.
3 *
4 * Copyright (C) 2003-2006 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
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; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Yoichi Yuasabd0765092007-05-11 21:18:48 +090020#include <linux/err.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070021#include <linux/fs.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
Yoichi Yuasabd0765092007-05-11 21:18:48 +090024#include <linux/interrupt.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070025#include <linux/module.h>
26#include <linux/platform_device.h>
27#include <linux/rtc.h>
28#include <linux/spinlock.h>
29#include <linux/types.h>
30
31#include <asm/div64.h>
32#include <asm/io.h>
33#include <asm/uaccess.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070034
35MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
36MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
37MODULE_LICENSE("GPL");
38
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070039/* RTC 1 registers */
40#define ETIMELREG 0x00
41#define ETIMEMREG 0x02
42#define ETIMEHREG 0x04
43/* RFU */
44#define ECMPLREG 0x08
45#define ECMPMREG 0x0a
46#define ECMPHREG 0x0c
47/* RFU */
48#define RTCL1LREG 0x10
49#define RTCL1HREG 0x12
50#define RTCL1CNTLREG 0x14
51#define RTCL1CNTHREG 0x16
52#define RTCL2LREG 0x18
53#define RTCL2HREG 0x1a
54#define RTCL2CNTLREG 0x1c
55#define RTCL2CNTHREG 0x1e
56
57/* RTC 2 registers */
58#define TCLKLREG 0x00
59#define TCLKHREG 0x02
60#define TCLKCNTLREG 0x04
61#define TCLKCNTHREG 0x06
62/* RFU */
63#define RTCINTREG 0x1e
64 #define TCLOCK_INT 0x08
65 #define RTCLONG2_INT 0x04
66 #define RTCLONG1_INT 0x02
67 #define ELAPSEDTIME_INT 0x01
68
69#define RTC_FREQUENCY 32768
70#define MAX_PERIODIC_RATE 6553
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070071
72static void __iomem *rtc1_base;
73static void __iomem *rtc2_base;
74
75#define rtc1_read(offset) readw(rtc1_base + (offset))
76#define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
77
78#define rtc2_read(offset) readw(rtc2_base + (offset))
79#define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
80
81static unsigned long epoch = 1970; /* Jan 1 1970 00:00:00 */
82
Ingo Molnar34af9462006-06-27 02:53:55 -070083static DEFINE_SPINLOCK(rtc_lock);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070084static char rtc_name[] = "RTC";
85static unsigned long periodic_frequency;
86static unsigned long periodic_count;
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -070087static unsigned int alarm_enabled;
Yoichi Yuasabd0765092007-05-11 21:18:48 +090088static int aie_irq = -1;
89static int pie_irq = -1;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070090
91static inline unsigned long read_elapsed_second(void)
92{
93
94 unsigned long first_low, first_mid, first_high;
95
96 unsigned long second_low, second_mid, second_high;
97
98 do {
99 first_low = rtc1_read(ETIMELREG);
100 first_mid = rtc1_read(ETIMEMREG);
101 first_high = rtc1_read(ETIMEHREG);
102 second_low = rtc1_read(ETIMELREG);
103 second_mid = rtc1_read(ETIMEMREG);
104 second_high = rtc1_read(ETIMEHREG);
105 } while (first_low != second_low || first_mid != second_mid ||
106 first_high != second_high);
107
108 return (first_high << 17) | (first_mid << 1) | (first_low >> 15);
109}
110
111static inline void write_elapsed_second(unsigned long sec)
112{
113 spin_lock_irq(&rtc_lock);
114
115 rtc1_write(ETIMELREG, (uint16_t)(sec << 15));
116 rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1));
117 rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17));
118
119 spin_unlock_irq(&rtc_lock);
120}
121
122static void vr41xx_rtc_release(struct device *dev)
123{
124
125 spin_lock_irq(&rtc_lock);
126
127 rtc1_write(ECMPLREG, 0);
128 rtc1_write(ECMPMREG, 0);
129 rtc1_write(ECMPHREG, 0);
130 rtc1_write(RTCL1LREG, 0);
131 rtc1_write(RTCL1HREG, 0);
132
133 spin_unlock_irq(&rtc_lock);
134
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900135 disable_irq(aie_irq);
136 disable_irq(pie_irq);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700137}
138
139static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
140{
141 unsigned long epoch_sec, elapsed_sec;
142
143 epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
144 elapsed_sec = read_elapsed_second();
145
146 rtc_time_to_tm(epoch_sec + elapsed_sec, time);
147
148 return 0;
149}
150
151static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time)
152{
153 unsigned long epoch_sec, current_sec;
154
155 epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
156 current_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
157 time->tm_hour, time->tm_min, time->tm_sec);
158
159 write_elapsed_second(current_sec - epoch_sec);
160
161 return 0;
162}
163
164static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
165{
166 unsigned long low, mid, high;
167 struct rtc_time *time = &wkalrm->time;
168
169 spin_lock_irq(&rtc_lock);
170
171 low = rtc1_read(ECMPLREG);
172 mid = rtc1_read(ECMPMREG);
173 high = rtc1_read(ECMPHREG);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700174 wkalrm->enabled = alarm_enabled;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700175
176 spin_unlock_irq(&rtc_lock);
177
178 rtc_time_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
179
180 return 0;
181}
182
183static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
184{
185 unsigned long alarm_sec;
186 struct rtc_time *time = &wkalrm->time;
187
188 alarm_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
189 time->tm_hour, time->tm_min, time->tm_sec);
190
191 spin_lock_irq(&rtc_lock);
192
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700193 if (alarm_enabled)
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900194 disable_irq(aie_irq);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700195
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700196 rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
197 rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
198 rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
199
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700200 if (wkalrm->enabled)
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900201 enable_irq(aie_irq);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700202
203 alarm_enabled = wkalrm->enabled;
204
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700205 spin_unlock_irq(&rtc_lock);
206
207 return 0;
208}
209
210static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
211{
212 unsigned long count;
213
214 switch (cmd) {
215 case RTC_AIE_ON:
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700216 spin_lock_irq(&rtc_lock);
217
218 if (!alarm_enabled) {
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900219 enable_irq(aie_irq);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700220 alarm_enabled = 1;
221 }
222
223 spin_unlock_irq(&rtc_lock);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700224 break;
225 case RTC_AIE_OFF:
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700226 spin_lock_irq(&rtc_lock);
227
228 if (alarm_enabled) {
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900229 disable_irq(aie_irq);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700230 alarm_enabled = 0;
231 }
232
233 spin_unlock_irq(&rtc_lock);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700234 break;
235 case RTC_PIE_ON:
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900236 enable_irq(pie_irq);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700237 break;
238 case RTC_PIE_OFF:
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900239 disable_irq(pie_irq);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700240 break;
241 case RTC_IRQP_READ:
242 return put_user(periodic_frequency, (unsigned long __user *)arg);
243 break;
244 case RTC_IRQP_SET:
245 if (arg > MAX_PERIODIC_RATE)
246 return -EINVAL;
247
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700248 periodic_frequency = arg;
249
250 count = RTC_FREQUENCY;
251 do_div(count, arg);
252
253 periodic_count = count;
254
255 spin_lock_irq(&rtc_lock);
256
257 rtc1_write(RTCL1LREG, count);
258 rtc1_write(RTCL1HREG, count >> 16);
259
260 spin_unlock_irq(&rtc_lock);
261 break;
262 case RTC_EPOCH_READ:
263 return put_user(epoch, (unsigned long __user *)arg);
264 case RTC_EPOCH_SET:
265 /* Doesn't support before 1900 */
266 if (arg < 1900)
267 return -EINVAL;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700268 epoch = arg;
269 break;
270 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -0700271 return -ENOIOCTLCMD;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700272 }
273
274 return 0;
275}
276
David Howells7d12e782006-10-05 14:55:46 +0100277static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700278{
279 struct platform_device *pdev = (struct platform_device *)dev_id;
280 struct rtc_device *rtc = platform_get_drvdata(pdev);
281
282 rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
283
David Brownellab6a2d72007-05-08 00:33:30 -0700284 rtc_update_irq(rtc, 1, RTC_AF);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700285
286 return IRQ_HANDLED;
287}
288
David Howells7d12e782006-10-05 14:55:46 +0100289static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700290{
291 struct platform_device *pdev = (struct platform_device *)dev_id;
292 struct rtc_device *rtc = platform_get_drvdata(pdev);
293 unsigned long count = periodic_count;
294
295 rtc2_write(RTCINTREG, RTCLONG1_INT);
296
297 rtc1_write(RTCL1LREG, count);
298 rtc1_write(RTCL1HREG, count >> 16);
299
David Brownellab6a2d72007-05-08 00:33:30 -0700300 rtc_update_irq(rtc, 1, RTC_PF);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700301
302 return IRQ_HANDLED;
303}
304
David Brownellff8371a2006-09-30 23:28:17 -0700305static const struct rtc_class_ops vr41xx_rtc_ops = {
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700306 .release = vr41xx_rtc_release,
307 .ioctl = vr41xx_rtc_ioctl,
308 .read_time = vr41xx_rtc_read_time,
309 .set_time = vr41xx_rtc_set_time,
310 .read_alarm = vr41xx_rtc_read_alarm,
311 .set_alarm = vr41xx_rtc_set_alarm,
312};
313
314static int __devinit rtc_probe(struct platform_device *pdev)
315{
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900316 struct resource *res;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700317 struct rtc_device *rtc;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700318 int retval;
319
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900320 if (pdev->num_resources != 4)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700321 return -EBUSY;
322
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900323 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
324 if (!res)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700325 return -EBUSY;
326
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900327 rtc1_base = ioremap(res->start, res->end - res->start + 1);
328 if (!rtc1_base)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700329 return -EBUSY;
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900330
331 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
332 if (!res) {
333 retval = -EBUSY;
334 goto err_rtc1_iounmap;
335 }
336
337 rtc2_base = ioremap(res->start, res->end - res->start + 1);
338 if (!rtc2_base) {
339 retval = -EBUSY;
340 goto err_rtc1_iounmap;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700341 }
342
343 rtc = rtc_device_register(rtc_name, &pdev->dev, &vr41xx_rtc_ops, THIS_MODULE);
344 if (IS_ERR(rtc)) {
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900345 retval = PTR_ERR(rtc);
346 goto err_iounmap_all;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700347 }
348
349 spin_lock_irq(&rtc_lock);
350
351 rtc1_write(ECMPLREG, 0);
352 rtc1_write(ECMPMREG, 0);
353 rtc1_write(ECMPHREG, 0);
354 rtc1_write(RTCL1LREG, 0);
355 rtc1_write(RTCL1HREG, 0);
356
357 spin_unlock_irq(&rtc_lock);
358
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900359 aie_irq = platform_get_irq(pdev, 0);
360 if (aie_irq < 0 || aie_irq >= NR_IRQS) {
361 retval = -EBUSY;
362 goto err_device_unregister;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700363 }
364
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900365 retval = request_irq(aie_irq, elapsedtime_interrupt, IRQF_DISABLED,
366 "elapsed_time", pdev);
367 if (retval < 0)
368 goto err_device_unregister;
369
370 pie_irq = platform_get_irq(pdev, 1);
371 if (pie_irq < 0 || pie_irq >= NR_IRQS)
372 goto err_free_irq;
373
374 retval = request_irq(pie_irq, rtclong1_interrupt, IRQF_DISABLED,
375 "rtclong1", pdev);
376 if (retval < 0)
377 goto err_free_irq;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700378
379 platform_set_drvdata(pdev, rtc);
380
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900381 disable_irq(aie_irq);
382 disable_irq(pie_irq);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700383
384 printk(KERN_INFO "rtc: Real Time Clock of NEC VR4100 series\n");
385
386 return 0;
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900387
388err_free_irq:
389 free_irq(aie_irq, pdev);
390
391err_device_unregister:
392 rtc_device_unregister(rtc);
393
394err_iounmap_all:
395 iounmap(rtc2_base);
396 rtc2_base = NULL;
397
398err_rtc1_iounmap:
399 iounmap(rtc1_base);
400 rtc1_base = NULL;
401
402 return retval;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700403}
404
405static int __devexit rtc_remove(struct platform_device *pdev)
406{
407 struct rtc_device *rtc;
408
409 rtc = platform_get_drvdata(pdev);
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900410 if (rtc)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700411 rtc_device_unregister(rtc);
412
413 platform_set_drvdata(pdev, NULL);
414
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900415 free_irq(aie_irq, pdev);
416 free_irq(pie_irq, pdev);
417 if (rtc1_base)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700418 iounmap(rtc1_base);
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900419 if (rtc2_base)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700420 iounmap(rtc2_base);
421
422 return 0;
423}
424
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700425static struct platform_driver rtc_platform_driver = {
426 .probe = rtc_probe,
427 .remove = __devexit_p(rtc_remove),
428 .driver = {
429 .name = rtc_name,
430 .owner = THIS_MODULE,
431 },
432};
433
434static int __init vr41xx_rtc_init(void)
435{
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900436 return platform_driver_register(&rtc_platform_driver);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700437}
438
439static void __exit vr41xx_rtc_exit(void)
440{
441 platform_driver_unregister(&rtc_platform_driver);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700442}
443
444module_init(vr41xx_rtc_init);
445module_exit(vr41xx_rtc_exit);