rtc.c 4.1 KB

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