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