rtc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Real Time Clock interface for Linux on the MVME16x
  3. *
  4. * Based on the PC driver by Paul Gortmaker.
  5. */
  6. #define RTC_VERSION "1.00"
  7. #include <linux/types.h>
  8. #include <linux/errno.h>
  9. #include <linux/miscdevice.h>
  10. #include <linux/ioport.h>
  11. #include <linux/capability.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/init.h>
  14. #include <linux/poll.h>
  15. #include <linux/mc146818rtc.h> /* For struct rtc_time and ioctls, etc */
  16. #include <linux/bcd.h>
  17. #include <asm/mvme16xhw.h>
  18. #include <asm/io.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/setup.h>
  21. /*
  22. * We sponge a minor off of the misc major. No need slurping
  23. * up another valuable major dev number for this. If you add
  24. * an ioctl, make sure you don't conflict with SPARC's RTC
  25. * ioctls.
  26. */
  27. static const unsigned char days_in_mo[] =
  28. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  29. static atomic_t rtc_ready = ATOMIC_INIT(1);
  30. static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  31. {
  32. volatile MK48T08ptr_t rtc = (MK48T08ptr_t)MVME_RTC_BASE;
  33. unsigned long flags;
  34. struct rtc_time wtime;
  35. void __user *argp = (void __user *)arg;
  36. switch (cmd) {
  37. case RTC_RD_TIME: /* Read the time/date from RTC */
  38. {
  39. local_irq_save(flags);
  40. /* Ensure clock and real-time-mode-register are accessible */
  41. rtc->ctrl = RTC_READ;
  42. memset(&wtime, 0, sizeof(struct rtc_time));
  43. wtime.tm_sec = bcd2bin(rtc->bcd_sec);
  44. wtime.tm_min = bcd2bin(rtc->bcd_min);
  45. wtime.tm_hour = bcd2bin(rtc->bcd_hr);
  46. wtime.tm_mday = bcd2bin(rtc->bcd_dom);
  47. wtime.tm_mon = bcd2bin(rtc->bcd_mth)-1;
  48. wtime.tm_year = bcd2bin(rtc->bcd_year);
  49. if (wtime.tm_year < 70)
  50. wtime.tm_year += 100;
  51. wtime.tm_wday = bcd2bin(rtc->bcd_dow)-1;
  52. rtc->ctrl = 0;
  53. local_irq_restore(flags);
  54. return copy_to_user(argp, &wtime, sizeof wtime) ?
  55. -EFAULT : 0;
  56. }
  57. case RTC_SET_TIME: /* Set the RTC */
  58. {
  59. struct rtc_time rtc_tm;
  60. unsigned char mon, day, hrs, min, sec, leap_yr;
  61. unsigned int yrs;
  62. if (!capable(CAP_SYS_ADMIN))
  63. return -EACCES;
  64. if (copy_from_user(&rtc_tm, argp, sizeof(struct rtc_time)))
  65. return -EFAULT;
  66. yrs = rtc_tm.tm_year;
  67. if (yrs < 1900)
  68. yrs += 1900;
  69. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  70. day = rtc_tm.tm_mday;
  71. hrs = rtc_tm.tm_hour;
  72. min = rtc_tm.tm_min;
  73. sec = rtc_tm.tm_sec;
  74. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  75. if ((mon > 12) || (day == 0))
  76. return -EINVAL;
  77. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  78. return -EINVAL;
  79. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  80. return -EINVAL;
  81. if (yrs >= 2070)
  82. return -EINVAL;
  83. local_irq_save(flags);
  84. rtc->ctrl = RTC_WRITE;
  85. rtc->bcd_sec = bin2bcd(sec);
  86. rtc->bcd_min = bin2bcd(min);
  87. rtc->bcd_hr = bin2bcd(hrs);
  88. rtc->bcd_dom = bin2bcd(day);
  89. rtc->bcd_mth = bin2bcd(mon);
  90. rtc->bcd_year = bin2bcd(yrs%100);
  91. rtc->ctrl = 0;
  92. local_irq_restore(flags);
  93. return 0;
  94. }
  95. default:
  96. return -EINVAL;
  97. }
  98. }
  99. /*
  100. * We enforce only one user at a time here with the open/close.
  101. */
  102. static int rtc_open(struct inode *inode, struct file *file)
  103. {
  104. if( !atomic_dec_and_test(&rtc_ready) )
  105. {
  106. atomic_inc( &rtc_ready );
  107. return -EBUSY;
  108. }
  109. return 0;
  110. }
  111. static int rtc_release(struct inode *inode, struct file *file)
  112. {
  113. atomic_inc( &rtc_ready );
  114. return 0;
  115. }
  116. /*
  117. * The various file operations we support.
  118. */
  119. static const struct file_operations rtc_fops = {
  120. .unlocked_ioctl = rtc_ioctl,
  121. .open = rtc_open,
  122. .release = rtc_release,
  123. .llseek = noop_llseek,
  124. };
  125. static struct miscdevice rtc_dev=
  126. {
  127. .minor = RTC_MINOR,
  128. .name = "rtc",
  129. .fops = &rtc_fops
  130. };
  131. static int __init rtc_MK48T08_init(void)
  132. {
  133. if (!MACH_IS_MVME16x)
  134. return -ENODEV;
  135. printk(KERN_INFO "MK48T08 Real Time Clock Driver v%s\n", RTC_VERSION);
  136. return misc_register(&rtc_dev);
  137. }
  138. device_initcall(rtc_MK48T08_init);