rtc.c 4.1 KB

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