e1000: Add enabled Jumbo frame support for 82573L
[linux-2.6/libata-dev.git] / drivers / char / vr41xx_rtc.c
blobbc1b4a15212c2d939e4ed4508dcf5c70eacfe405
1 /*
2 * Driver for NEC VR4100 series Real Time Clock unit.
4 * Copyright (C) 2003-2005 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
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.
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.
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
20 #include <linux/platform_device.h>
21 #include <linux/fs.h>
22 #include <linux/init.h>
23 #include <linux/ioport.h>
24 #include <linux/irq.h>
25 #include <linux/mc146818rtc.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/poll.h>
29 #include <linux/rtc.h>
30 #include <linux/spinlock.h>
31 #include <linux/types.h>
32 #include <linux/wait.h>
34 #include <asm/div64.h>
35 #include <asm/io.h>
36 #include <asm/time.h>
37 #include <asm/uaccess.h>
38 #include <asm/vr41xx/vr41xx.h>
40 MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
41 MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
42 MODULE_LICENSE("GPL");
44 #define RTC1_TYPE1_START 0x0b0000c0UL
45 #define RTC1_TYPE1_END 0x0b0000dfUL
46 #define RTC2_TYPE1_START 0x0b0001c0UL
47 #define RTC2_TYPE1_END 0x0b0001dfUL
49 #define RTC1_TYPE2_START 0x0f000100UL
50 #define RTC1_TYPE2_END 0x0f00011fUL
51 #define RTC2_TYPE2_START 0x0f000120UL
52 #define RTC2_TYPE2_END 0x0f00013fUL
54 #define RTC1_SIZE 0x20
55 #define RTC2_SIZE 0x20
57 /* RTC 1 registers */
58 #define ETIMELREG 0x00
59 #define ETIMEMREG 0x02
60 #define ETIMEHREG 0x04
61 /* RFU */
62 #define ECMPLREG 0x08
63 #define ECMPMREG 0x0a
64 #define ECMPHREG 0x0c
65 /* RFU */
66 #define RTCL1LREG 0x10
67 #define RTCL1HREG 0x12
68 #define RTCL1CNTLREG 0x14
69 #define RTCL1CNTHREG 0x16
70 #define RTCL2LREG 0x18
71 #define RTCL2HREG 0x1a
72 #define RTCL2CNTLREG 0x1c
73 #define RTCL2CNTHREG 0x1e
75 /* RTC 2 registers */
76 #define TCLKLREG 0x00
77 #define TCLKHREG 0x02
78 #define TCLKCNTLREG 0x04
79 #define TCLKCNTHREG 0x06
80 /* RFU */
81 #define RTCINTREG 0x1e
82 #define TCLOCK_INT 0x08
83 #define RTCLONG2_INT 0x04
84 #define RTCLONG1_INT 0x02
85 #define ELAPSEDTIME_INT 0x01
87 #define RTC_FREQUENCY 32768
88 #define MAX_PERIODIC_RATE 6553
89 #define MAX_USER_PERIODIC_RATE 64
91 static void __iomem *rtc1_base;
92 static void __iomem *rtc2_base;
94 #define rtc1_read(offset) readw(rtc1_base + (offset))
95 #define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
97 #define rtc2_read(offset) readw(rtc2_base + (offset))
98 #define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
100 static unsigned long epoch = 1970; /* Jan 1 1970 00:00:00 */
102 static spinlock_t rtc_task_lock;
103 static wait_queue_head_t rtc_wait;
104 static unsigned long rtc_irq_data;
105 static struct fasync_struct *rtc_async_queue;
106 static rtc_task_t *rtc_callback;
107 static char rtc_name[] = "RTC";
108 static unsigned long periodic_frequency;
109 static unsigned long periodic_count;
111 typedef enum {
112 RTC_RELEASE,
113 RTC_OPEN,
114 } rtc_status_t;
116 static rtc_status_t rtc_status;
118 typedef enum {
119 FUNCTION_RTC_IOCTL,
120 FUNCTION_RTC_CONTROL,
121 } rtc_callfrom_t;
123 struct resource rtc_resource[2] = {
124 { .name = rtc_name,
125 .flags = IORESOURCE_MEM, },
126 { .name = rtc_name,
127 .flags = IORESOURCE_MEM, },
130 static inline unsigned long read_elapsed_second(void)
132 unsigned long first_low, first_mid, first_high;
133 unsigned long second_low, second_mid, second_high;
135 do {
136 first_low = rtc1_read(ETIMELREG);
137 first_mid = rtc1_read(ETIMEMREG);
138 first_high = rtc1_read(ETIMEHREG);
139 second_low = rtc1_read(ETIMELREG);
140 second_mid = rtc1_read(ETIMEMREG);
141 second_high = rtc1_read(ETIMEHREG);
142 } while (first_low != second_low || first_mid != second_mid ||
143 first_high != second_high);
145 return (first_high << 17) | (first_mid << 1) | (first_low >> 15);
148 static inline void write_elapsed_second(unsigned long sec)
150 spin_lock_irq(&rtc_lock);
152 rtc1_write(ETIMELREG, (uint16_t)(sec << 15));
153 rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1));
154 rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17));
156 spin_unlock_irq(&rtc_lock);
159 static void set_alarm(struct rtc_time *time)
161 unsigned long alarm_sec;
163 alarm_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
164 time->tm_hour, time->tm_min, time->tm_sec);
166 spin_lock_irq(&rtc_lock);
168 rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
169 rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
170 rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
172 spin_unlock_irq(&rtc_lock);
175 static void read_alarm(struct rtc_time *time)
177 unsigned long low, mid, high;
179 spin_lock_irq(&rtc_lock);
181 low = rtc1_read(ECMPLREG);
182 mid = rtc1_read(ECMPMREG);
183 high = rtc1_read(ECMPHREG);
185 spin_unlock_irq(&rtc_lock);
187 to_tm((high << 17) | (mid << 1) | (low >> 15), time);
188 time->tm_year -= 1900;
191 static void read_time(struct rtc_time *time)
193 unsigned long epoch_sec, elapsed_sec;
195 epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
196 elapsed_sec = read_elapsed_second();
198 to_tm(epoch_sec + elapsed_sec, time);
199 time->tm_year -= 1900;
202 static void set_time(struct rtc_time *time)
204 unsigned long epoch_sec, current_sec;
206 epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
207 current_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
208 time->tm_hour, time->tm_min, time->tm_sec);
210 write_elapsed_second(current_sec - epoch_sec);
213 static ssize_t rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
215 DECLARE_WAITQUEUE(wait, current);
216 unsigned long irq_data;
217 int retval = 0;
219 if (count != sizeof(unsigned int) && count != sizeof(unsigned long))
220 return -EINVAL;
222 add_wait_queue(&rtc_wait, &wait);
224 do {
225 __set_current_state(TASK_INTERRUPTIBLE);
227 spin_lock_irq(&rtc_lock);
228 irq_data = rtc_irq_data;
229 rtc_irq_data = 0;
230 spin_unlock_irq(&rtc_lock);
232 if (irq_data != 0)
233 break;
235 if (file->f_flags & O_NONBLOCK) {
236 retval = -EAGAIN;
237 break;
240 if (signal_pending(current)) {
241 retval = -ERESTARTSYS;
242 break;
244 } while (1);
246 if (retval == 0) {
247 if (count == sizeof(unsigned int)) {
248 retval = put_user(irq_data, (unsigned int __user *)buf);
249 if (retval == 0)
250 retval = sizeof(unsigned int);
251 } else {
252 retval = put_user(irq_data, (unsigned long __user *)buf);
253 if (retval == 0)
254 retval = sizeof(unsigned long);
259 __set_current_state(TASK_RUNNING);
260 remove_wait_queue(&rtc_wait, &wait);
262 return retval;
265 static unsigned int rtc_poll(struct file *file, struct poll_table_struct *table)
267 poll_wait(file, &rtc_wait, table);
269 if (rtc_irq_data != 0)
270 return POLLIN | POLLRDNORM;
272 return 0;
275 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, rtc_callfrom_t from)
277 struct rtc_time time;
278 unsigned long count;
280 switch (cmd) {
281 case RTC_AIE_ON:
282 enable_irq(ELAPSEDTIME_IRQ);
283 break;
284 case RTC_AIE_OFF:
285 disable_irq(ELAPSEDTIME_IRQ);
286 break;
287 case RTC_PIE_ON:
288 enable_irq(RTCLONG1_IRQ);
289 break;
290 case RTC_PIE_OFF:
291 disable_irq(RTCLONG1_IRQ);
292 break;
293 case RTC_ALM_SET:
294 if (copy_from_user(&time, (struct rtc_time __user *)arg,
295 sizeof(struct rtc_time)))
296 return -EFAULT;
298 set_alarm(&time);
299 break;
300 case RTC_ALM_READ:
301 memset(&time, 0, sizeof(struct rtc_time));
302 read_alarm(&time);
303 break;
304 case RTC_RD_TIME:
305 memset(&time, 0, sizeof(struct rtc_time));
306 read_time(&time);
307 if (copy_to_user((void __user *)arg, &time, sizeof(struct rtc_time)))
308 return -EFAULT;
309 break;
310 case RTC_SET_TIME:
311 if (capable(CAP_SYS_TIME) == 0)
312 return -EACCES;
314 if (copy_from_user(&time, (struct rtc_time __user *)arg,
315 sizeof(struct rtc_time)))
316 return -EFAULT;
318 set_time(&time);
319 break;
320 case RTC_IRQP_READ:
321 return put_user(periodic_frequency, (unsigned long __user *)arg);
322 break;
323 case RTC_IRQP_SET:
324 if (arg > MAX_PERIODIC_RATE)
325 return -EINVAL;
327 if (from == FUNCTION_RTC_IOCTL && arg > MAX_USER_PERIODIC_RATE &&
328 capable(CAP_SYS_RESOURCE) == 0)
329 return -EACCES;
331 periodic_frequency = arg;
333 count = RTC_FREQUENCY;
334 do_div(count, arg);
336 periodic_count = count;
338 spin_lock_irq(&rtc_lock);
340 rtc1_write(RTCL1LREG, count);
341 rtc1_write(RTCL1HREG, count >> 16);
343 spin_unlock_irq(&rtc_lock);
344 break;
345 case RTC_EPOCH_READ:
346 return put_user(epoch, (unsigned long __user *)arg);
347 case RTC_EPOCH_SET:
348 /* Doesn't support before 1900 */
349 if (arg < 1900)
350 return -EINVAL;
352 if (capable(CAP_SYS_TIME) == 0)
353 return -EACCES;
355 epoch = arg;
356 break;
357 default:
358 return -EINVAL;
361 return 0;
364 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
365 unsigned long arg)
367 return rtc_do_ioctl(cmd, arg, FUNCTION_RTC_IOCTL);
370 static int rtc_open(struct inode *inode, struct file *file)
372 spin_lock_irq(&rtc_lock);
374 if (rtc_status == RTC_OPEN) {
375 spin_unlock_irq(&rtc_lock);
376 return -EBUSY;
379 rtc_status = RTC_OPEN;
380 rtc_irq_data = 0;
382 spin_unlock_irq(&rtc_lock);
384 return 0;
387 static int rtc_release(struct inode *inode, struct file *file)
389 if (file->f_flags & FASYNC)
390 (void)fasync_helper(-1, file, 0, &rtc_async_queue);
392 spin_lock_irq(&rtc_lock);
394 rtc1_write(ECMPLREG, 0);
395 rtc1_write(ECMPMREG, 0);
396 rtc1_write(ECMPHREG, 0);
397 rtc1_write(RTCL1LREG, 0);
398 rtc1_write(RTCL1HREG, 0);
400 rtc_status = RTC_RELEASE;
402 spin_unlock_irq(&rtc_lock);
404 disable_irq(ELAPSEDTIME_IRQ);
405 disable_irq(RTCLONG1_IRQ);
407 return 0;
410 static int rtc_fasync(int fd, struct file *file, int on)
412 return fasync_helper(fd, file, on, &rtc_async_queue);
415 static struct file_operations rtc_fops = {
416 .owner = THIS_MODULE,
417 .llseek = no_llseek,
418 .read = rtc_read,
419 .poll = rtc_poll,
420 .ioctl = rtc_ioctl,
421 .open = rtc_open,
422 .release = rtc_release,
423 .fasync = rtc_fasync,
426 static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id, struct pt_regs *regs)
428 spin_lock(&rtc_lock);
429 rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
431 rtc_irq_data += 0x100;
432 rtc_irq_data &= ~0xff;
433 rtc_irq_data |= RTC_AF;
434 spin_unlock(&rtc_lock);
436 spin_lock(&rtc_lock);
437 if (rtc_callback)
438 rtc_callback->func(rtc_callback->private_data);
439 spin_unlock(&rtc_lock);
441 wake_up_interruptible(&rtc_wait);
443 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
445 return IRQ_HANDLED;
448 static irqreturn_t rtclong1_interrupt(int irq, void *dev_id, struct pt_regs *regs)
450 unsigned long count = periodic_count;
452 spin_lock(&rtc_lock);
453 rtc2_write(RTCINTREG, RTCLONG1_INT);
455 rtc1_write(RTCL1LREG, count);
456 rtc1_write(RTCL1HREG, count >> 16);
458 rtc_irq_data += 0x100;
459 rtc_irq_data &= ~0xff;
460 rtc_irq_data |= RTC_PF;
461 spin_unlock(&rtc_lock);
463 spin_lock(&rtc_task_lock);
464 if (rtc_callback)
465 rtc_callback->func(rtc_callback->private_data);
466 spin_unlock(&rtc_task_lock);
468 wake_up_interruptible(&rtc_wait);
470 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
472 return IRQ_HANDLED;
475 int rtc_register(rtc_task_t *task)
477 if (task == NULL || task->func == NULL)
478 return -EINVAL;
480 spin_lock_irq(&rtc_lock);
481 if (rtc_status == RTC_OPEN) {
482 spin_unlock_irq(&rtc_lock);
483 return -EBUSY;
486 spin_lock(&rtc_task_lock);
487 if (rtc_callback != NULL) {
488 spin_unlock(&rtc_task_lock);
489 spin_unlock_irq(&rtc_task_lock);
490 return -EBUSY;
493 rtc_callback = task;
494 spin_unlock(&rtc_task_lock);
496 rtc_status = RTC_OPEN;
498 spin_unlock_irq(&rtc_lock);
500 return 0;
503 EXPORT_SYMBOL_GPL(rtc_register);
505 int rtc_unregister(rtc_task_t *task)
507 spin_lock_irq(&rtc_task_lock);
508 if (task == NULL || rtc_callback != task) {
509 spin_unlock_irq(&rtc_task_lock);
510 return -ENXIO;
513 spin_lock(&rtc_lock);
515 rtc1_write(ECMPLREG, 0);
516 rtc1_write(ECMPMREG, 0);
517 rtc1_write(ECMPHREG, 0);
518 rtc1_write(RTCL1LREG, 0);
519 rtc1_write(RTCL1HREG, 0);
521 rtc_status = RTC_RELEASE;
523 spin_unlock(&rtc_lock);
525 rtc_callback = NULL;
527 spin_unlock_irq(&rtc_task_lock);
529 disable_irq(ELAPSEDTIME_IRQ);
530 disable_irq(RTCLONG1_IRQ);
532 return 0;
535 EXPORT_SYMBOL_GPL(rtc_unregister);
537 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
539 int retval = 0;
541 spin_lock_irq(&rtc_task_lock);
543 if (rtc_callback != task)
544 retval = -ENXIO;
545 else
546 rtc_do_ioctl(cmd, arg, FUNCTION_RTC_CONTROL);
548 spin_unlock_irq(&rtc_task_lock);
550 return retval;
553 EXPORT_SYMBOL_GPL(rtc_control);
555 static struct miscdevice rtc_miscdevice = {
556 .minor = RTC_MINOR,
557 .name = rtc_name,
558 .fops = &rtc_fops,
561 static int rtc_probe(struct platform_device *pdev)
563 unsigned int irq;
564 int retval;
566 if (pdev->num_resources != 2)
567 return -EBUSY;
569 rtc1_base = ioremap(pdev->resource[0].start, RTC1_SIZE);
570 if (rtc1_base == NULL)
571 return -EBUSY;
573 rtc2_base = ioremap(pdev->resource[1].start, RTC2_SIZE);
574 if (rtc2_base == NULL) {
575 iounmap(rtc1_base);
576 rtc1_base = NULL;
577 return -EBUSY;
580 retval = misc_register(&rtc_miscdevice);
581 if (retval < 0) {
582 iounmap(rtc1_base);
583 iounmap(rtc2_base);
584 rtc1_base = NULL;
585 rtc2_base = NULL;
586 return retval;
589 spin_lock_irq(&rtc_lock);
591 rtc1_write(ECMPLREG, 0);
592 rtc1_write(ECMPMREG, 0);
593 rtc1_write(ECMPHREG, 0);
594 rtc1_write(RTCL1LREG, 0);
595 rtc1_write(RTCL1HREG, 0);
597 rtc_status = RTC_RELEASE;
598 rtc_irq_data = 0;
600 spin_unlock_irq(&rtc_lock);
602 init_waitqueue_head(&rtc_wait);
604 irq = ELAPSEDTIME_IRQ;
605 retval = request_irq(irq, elapsedtime_interrupt, SA_INTERRUPT,
606 "elapsed_time", NULL);
607 if (retval == 0) {
608 irq = RTCLONG1_IRQ;
609 retval = request_irq(irq, rtclong1_interrupt, SA_INTERRUPT,
610 "rtclong1", NULL);
613 if (retval < 0) {
614 printk(KERN_ERR "rtc: IRQ%d is busy\n", irq);
615 if (irq == RTCLONG1_IRQ)
616 free_irq(ELAPSEDTIME_IRQ, NULL);
617 iounmap(rtc1_base);
618 iounmap(rtc2_base);
619 rtc1_base = NULL;
620 rtc2_base = NULL;
621 return retval;
624 disable_irq(ELAPSEDTIME_IRQ);
625 disable_irq(RTCLONG1_IRQ);
627 spin_lock_init(&rtc_task_lock);
629 printk(KERN_INFO "rtc: Real Time Clock of NEC VR4100 series\n");
631 return 0;
634 static int rtc_remove(struct platform_device *dev)
636 int retval;
638 retval = misc_deregister(&rtc_miscdevice);
639 if (retval < 0)
640 return retval;
642 free_irq(ELAPSEDTIME_IRQ, NULL);
643 free_irq(RTCLONG1_IRQ, NULL);
644 if (rtc1_base != NULL)
645 iounmap(rtc1_base);
646 if (rtc2_base != NULL)
647 iounmap(rtc2_base);
649 return 0;
652 static struct platform_device *rtc_platform_device;
654 static struct platform_driver rtc_device_driver = {
655 .probe = rtc_probe,
656 .remove = rtc_remove,
657 .driver = {
658 .name = rtc_name,
662 static int __devinit vr41xx_rtc_init(void)
664 int retval;
666 switch (current_cpu_data.cputype) {
667 case CPU_VR4111:
668 case CPU_VR4121:
669 rtc_resource[0].start = RTC1_TYPE1_START;
670 rtc_resource[0].end = RTC1_TYPE1_END;
671 rtc_resource[1].start = RTC2_TYPE1_START;
672 rtc_resource[1].end = RTC2_TYPE1_END;
673 break;
674 case CPU_VR4122:
675 case CPU_VR4131:
676 case CPU_VR4133:
677 rtc_resource[0].start = RTC1_TYPE2_START;
678 rtc_resource[0].end = RTC1_TYPE2_END;
679 rtc_resource[1].start = RTC2_TYPE2_START;
680 rtc_resource[1].end = RTC2_TYPE2_END;
681 break;
682 default:
683 return -ENODEV;
684 break;
687 rtc_platform_device = platform_device_register_simple("RTC", -1,
688 rtc_resource, ARRAY_SIZE(rtc_resource));
689 if (IS_ERR(rtc_platform_device))
690 return PTR_ERR(rtc_platform_device);
692 retval = platform_driver_register(&rtc_device_driver);
693 if (retval < 0)
694 platform_device_unregister(rtc_platform_device);
696 return retval;
699 static void __devexit vr41xx_rtc_exit(void)
701 platform_driver_unregister(&rtc_device_driver);
703 platform_device_unregister(rtc_platform_device);
706 module_init(vr41xx_rtc_init);
707 module_exit(vr41xx_rtc_exit);