rtc.c 4.3 KB

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