rtc.c 4.2 KB

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