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>
 
  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>
 
  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
 
  58 #define ETIMELREG               0x00
 
  59 #define ETIMEMREG               0x02
 
  60 #define ETIMEHREG               0x04
 
  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
 
  78 #define TCLKCNTLREG             0x04
 
  79 #define TCLKCNTHREG             0x06
 
  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;
 
 116 static rtc_status_t rtc_status;
 
 120         FUNCTION_RTC_CONTROL,
 
 123 struct resource rtc_resource[2] = {
 
 125                 .flags  = IORESOURCE_MEM,       },
 
 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;
 
 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;
 
 219         if (count != sizeof(unsigned int) && count != sizeof(unsigned long))
 
 222         add_wait_queue(&rtc_wait, &wait);
 
 225                 __set_current_state(TASK_INTERRUPTIBLE);
 
 227                 spin_lock_irq(&rtc_lock);
 
 228                 irq_data = rtc_irq_data;
 
 230                 spin_unlock_irq(&rtc_lock);
 
 235                 if (file->f_flags & O_NONBLOCK) {
 
 240                 if (signal_pending(current)) {
 
 241                         retval = -ERESTARTSYS;
 
 247                 if (count == sizeof(unsigned int)) {
 
 248                         retval = put_user(irq_data, (unsigned int __user *)buf);
 
 250                                 retval = sizeof(unsigned int);
 
 252                         retval = put_user(irq_data, (unsigned long __user *)buf);
 
 254                                 retval = sizeof(unsigned long);
 
 259         __set_current_state(TASK_RUNNING);
 
 260         remove_wait_queue(&rtc_wait, &wait);
 
 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;
 
 275 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, rtc_callfrom_t from)
 
 277         struct rtc_time time;
 
 282                 enable_irq(ELAPSEDTIME_IRQ);
 
 285                 disable_irq(ELAPSEDTIME_IRQ);
 
 288                 enable_irq(RTCLONG1_IRQ);
 
 291                 disable_irq(RTCLONG1_IRQ);
 
 294                 if (copy_from_user(&time, (struct rtc_time __user *)arg,
 
 295                                    sizeof(struct rtc_time)))
 
 301                 memset(&time, 0, sizeof(struct rtc_time));
 
 305                 memset(&time, 0, sizeof(struct rtc_time));
 
 307                 if (copy_to_user((void __user *)arg, &time, sizeof(struct rtc_time)))
 
 311                 if (capable(CAP_SYS_TIME) == 0)
 
 314                 if (copy_from_user(&time, (struct rtc_time __user *)arg,
 
 315                                    sizeof(struct rtc_time)))
 
 321                 return put_user(periodic_frequency, (unsigned long __user *)arg);
 
 324                 if (arg > MAX_PERIODIC_RATE)
 
 327                 if (from == FUNCTION_RTC_IOCTL && arg > MAX_USER_PERIODIC_RATE &&
 
 328                     capable(CAP_SYS_RESOURCE) == 0)
 
 331                 periodic_frequency = arg;
 
 333                 count = RTC_FREQUENCY;
 
 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);
 
 346                 return put_user(epoch, (unsigned long __user *)arg);
 
 348                 /* Doesn't support before 1900 */
 
 352                 if (capable(CAP_SYS_TIME) == 0)
 
 364 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 
 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);
 
 379         rtc_status = RTC_OPEN;
 
 382         spin_unlock_irq(&rtc_lock);
 
 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);
 
 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,
 
 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);
 
 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);
 
 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);
 
 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);
 
 475 int rtc_register(rtc_task_t *task)
 
 477         if (task == NULL || task->func == NULL)
 
 480         spin_lock_irq(&rtc_lock);
 
 481         if (rtc_status == RTC_OPEN) {
 
 482                 spin_unlock_irq(&rtc_lock);
 
 486         spin_lock(&rtc_task_lock);
 
 487         if (rtc_callback != NULL) {
 
 488                 spin_unlock(&rtc_task_lock);
 
 489                 spin_unlock_irq(&rtc_task_lock);
 
 494         spin_unlock(&rtc_task_lock);
 
 496         rtc_status = RTC_OPEN;
 
 498         spin_unlock_irq(&rtc_lock);
 
 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);
 
 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);
 
 527         spin_unlock_irq(&rtc_task_lock);
 
 529         disable_irq(ELAPSEDTIME_IRQ);
 
 530         disable_irq(RTCLONG1_IRQ);
 
 535 EXPORT_SYMBOL_GPL(rtc_unregister);
 
 537 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
 
 541         spin_lock_irq(&rtc_task_lock);
 
 543         if (rtc_callback != task)
 
 546                 rtc_do_ioctl(cmd, arg, FUNCTION_RTC_CONTROL);
 
 548         spin_unlock_irq(&rtc_task_lock);
 
 553 EXPORT_SYMBOL_GPL(rtc_control);
 
 555 static struct miscdevice rtc_miscdevice = {
 
 561 static int rtc_probe(struct platform_device *pdev)
 
 566         if (pdev->num_resources != 2)
 
 569         rtc1_base = ioremap(pdev->resource[0].start, RTC1_SIZE);
 
 570         if (rtc1_base == NULL)
 
 573         rtc2_base = ioremap(pdev->resource[1].start, RTC2_SIZE);
 
 574         if (rtc2_base == NULL) {
 
 580         retval = misc_register(&rtc_miscdevice);
 
 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;
 
 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);
 
 609                 retval = request_irq(irq, rtclong1_interrupt, SA_INTERRUPT,
 
 614                 printk(KERN_ERR "rtc: IRQ%d is busy\n", irq);
 
 615                 if (irq == RTCLONG1_IRQ)
 
 616                         free_irq(ELAPSEDTIME_IRQ, NULL);
 
 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");
 
 634 static int rtc_remove(struct platform_device *dev)
 
 638         retval = misc_deregister(&rtc_miscdevice);
 
 642         free_irq(ELAPSEDTIME_IRQ, NULL);
 
 643         free_irq(RTCLONG1_IRQ, NULL);
 
 644         if (rtc1_base != NULL)
 
 646         if (rtc2_base != NULL)
 
 652 static struct platform_device *rtc_platform_device;
 
 654 static struct platform_driver rtc_device_driver = {
 
 656         .remove         = rtc_remove,
 
 662 static int __devinit vr41xx_rtc_init(void)
 
 666         switch (current_cpu_data.cputype) {
 
 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;
 
 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;
 
 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);
 
 694                 platform_device_unregister(rtc_platform_device);
 
 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);