rtc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/smp_lock.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/bcd.h>
  19. #include <asm/bvme6000hw.h>
  20. #include <asm/io.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/system.h>
  23. #include <asm/setup.h>
  24. /*
  25. * We sponge a minor off of the misc major. No need slurping
  26. * up another valuable major dev number for this. If you add
  27. * an ioctl, make sure you don't conflict with SPARC's RTC
  28. * ioctls.
  29. */
  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. void __user *argp = (void __user *)arg;
  41. switch (cmd) {
  42. case RTC_RD_TIME: /* Read the time/date from RTC */
  43. {
  44. local_irq_save(flags);
  45. /* Ensure clock and real-time-mode-register are accessible */
  46. msr = rtc->msr & 0xc0;
  47. rtc->msr = 0x40;
  48. memset(&wtime, 0, sizeof(struct rtc_time));
  49. do {
  50. wtime.tm_sec = bcd2bin(rtc->bcd_sec);
  51. wtime.tm_min = bcd2bin(rtc->bcd_min);
  52. wtime.tm_hour = bcd2bin(rtc->bcd_hr);
  53. wtime.tm_mday = bcd2bin(rtc->bcd_dom);
  54. wtime.tm_mon = bcd2bin(rtc->bcd_mth)-1;
  55. wtime.tm_year = bcd2bin(rtc->bcd_year);
  56. if (wtime.tm_year < 70)
  57. wtime.tm_year += 100;
  58. wtime.tm_wday = bcd2bin(rtc->bcd_dow)-1;
  59. } while (wtime.tm_sec != bcd2bin(rtc->bcd_sec));
  60. rtc->msr = msr;
  61. local_irq_restore(flags);
  62. return copy_to_user(argp, &wtime, sizeof wtime) ?
  63. -EFAULT : 0;
  64. }
  65. case RTC_SET_TIME: /* Set the RTC */
  66. {
  67. struct rtc_time rtc_tm;
  68. unsigned char mon, day, hrs, min, sec, leap_yr;
  69. unsigned int yrs;
  70. if (!capable(CAP_SYS_ADMIN))
  71. return -EACCES;
  72. if (copy_from_user(&rtc_tm, argp, 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. lock_kernel();
  122. if(rtc_status) {
  123. unlock_kernel();
  124. return -EBUSY;
  125. }
  126. rtc_status = 1;
  127. unlock_kernel();
  128. return 0;
  129. }
  130. static int rtc_release(struct inode *inode, struct file *file)
  131. {
  132. lock_kernel();
  133. rtc_status = 0;
  134. unlock_kernel();
  135. return 0;
  136. }
  137. /*
  138. * The various file operations we support.
  139. */
  140. static const struct file_operations rtc_fops = {
  141. .ioctl = rtc_ioctl,
  142. .open = rtc_open,
  143. .release = rtc_release,
  144. };
  145. static struct miscdevice rtc_dev = {
  146. .minor = RTC_MINOR,
  147. .name = "rtc",
  148. .fops = &rtc_fops
  149. };
  150. static int __init rtc_DP8570A_init(void)
  151. {
  152. if (!MACH_IS_BVME6000)
  153. return -ENODEV;
  154. printk(KERN_INFO "DP8570A Real Time Clock Driver v%s\n", RTC_VERSION);
  155. return misc_register(&rtc_dev);
  156. }
  157. module_init(rtc_DP8570A_init);