rtc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Real Time Clock interface for Linux on the MVME16x
  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/rtc.h> /* For struct rtc_time and ioctls, etc */
  17. #include <linux/bcd.h>
  18. #include <asm/mvme16xhw.h>
  19. #include <asm/io.h>
  20. #include <linux/uaccess.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. static const unsigned char days_in_mo[] =
  29. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  30. static atomic_t rtc_ready = ATOMIC_INIT(1);
  31. static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  32. {
  33. volatile MK48T08ptr_t rtc = (MK48T08ptr_t)MVME_RTC_BASE;
  34. unsigned long flags;
  35. struct rtc_time wtime;
  36. void __user *argp = (void __user *)arg;
  37. switch (cmd) {
  38. case RTC_RD_TIME: /* Read the time/date from RTC */
  39. {
  40. local_irq_save(flags);
  41. /* Ensure clock and real-time-mode-register are accessible */
  42. rtc->ctrl = RTC_READ;
  43. memset(&wtime, 0, sizeof(struct rtc_time));
  44. wtime.tm_sec = bcd2bin(rtc->bcd_sec);
  45. wtime.tm_min = bcd2bin(rtc->bcd_min);
  46. wtime.tm_hour = bcd2bin(rtc->bcd_hr);
  47. wtime.tm_mday = bcd2bin(rtc->bcd_dom);
  48. wtime.tm_mon = bcd2bin(rtc->bcd_mth)-1;
  49. wtime.tm_year = bcd2bin(rtc->bcd_year);
  50. if (wtime.tm_year < 70)
  51. wtime.tm_year += 100;
  52. wtime.tm_wday = bcd2bin(rtc->bcd_dow)-1;
  53. rtc->ctrl = 0;
  54. local_irq_restore(flags);
  55. return copy_to_user(argp, &wtime, sizeof wtime) ?
  56. -EFAULT : 0;
  57. }
  58. case RTC_SET_TIME: /* Set the RTC */
  59. {
  60. struct rtc_time rtc_tm;
  61. unsigned char mon, day, hrs, min, sec, leap_yr;
  62. unsigned int yrs;
  63. if (!capable(CAP_SYS_ADMIN))
  64. return -EACCES;
  65. if (copy_from_user(&rtc_tm, argp, sizeof(struct rtc_time)))
  66. return -EFAULT;
  67. yrs = rtc_tm.tm_year;
  68. if (yrs < 1900)
  69. yrs += 1900;
  70. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  71. day = rtc_tm.tm_mday;
  72. hrs = rtc_tm.tm_hour;
  73. min = rtc_tm.tm_min;
  74. sec = rtc_tm.tm_sec;
  75. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  76. if ((mon > 12) || (day == 0))
  77. return -EINVAL;
  78. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  79. return -EINVAL;
  80. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  81. return -EINVAL;
  82. if (yrs >= 2070)
  83. return -EINVAL;
  84. local_irq_save(flags);
  85. rtc->ctrl = RTC_WRITE;
  86. rtc->bcd_sec = bin2bcd(sec);
  87. rtc->bcd_min = bin2bcd(min);
  88. rtc->bcd_hr = bin2bcd(hrs);
  89. rtc->bcd_dom = bin2bcd(day);
  90. rtc->bcd_mth = bin2bcd(mon);
  91. rtc->bcd_year = bin2bcd(yrs%100);
  92. rtc->ctrl = 0;
  93. local_irq_restore(flags);
  94. return 0;
  95. }
  96. default:
  97. return -EINVAL;
  98. }
  99. }
  100. /*
  101. * We enforce only one user at a time here with the open/close.
  102. */
  103. static int rtc_open(struct inode *inode, struct file *file)
  104. {
  105. if( !atomic_dec_and_test(&rtc_ready) )
  106. {
  107. atomic_inc( &rtc_ready );
  108. return -EBUSY;
  109. }
  110. return 0;
  111. }
  112. static int rtc_release(struct inode *inode, struct file *file)
  113. {
  114. atomic_inc( &rtc_ready );
  115. return 0;
  116. }
  117. /*
  118. * The various file operations we support.
  119. */
  120. static const struct file_operations rtc_fops = {
  121. .unlocked_ioctl = rtc_ioctl,
  122. .open = rtc_open,
  123. .release = rtc_release,
  124. .llseek = noop_llseek,
  125. };
  126. static struct miscdevice rtc_dev=
  127. {
  128. .minor = RTC_MINOR,
  129. .name = "rtc",
  130. .fops = &rtc_fops
  131. };
  132. static int __init rtc_MK48T08_init(void)
  133. {
  134. if (!MACH_IS_MVME16x)
  135. return -ENODEV;
  136. pr_info("MK48T08 Real Time Clock Driver v%s\n", RTC_VERSION);
  137. return misc_register(&rtc_dev);
  138. }
  139. device_initcall(rtc_MK48T08_init);