rtc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Real Time Clock interface for Linux on the MVME16x
  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/mc146818rtc.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 <asm/uaccess.h>
  21. #include <asm/system.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 const unsigned char days_in_mo[] =
  30. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  31. static atomic_t rtc_ready = ATOMIC_INIT(1);
  32. static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  33. unsigned long arg)
  34. {
  35. volatile MK48T08ptr_t rtc = (MK48T08ptr_t)MVME_RTC_BASE;
  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. rtc->ctrl = RTC_READ;
  45. memset(&wtime, 0, sizeof(struct rtc_time));
  46. wtime.tm_sec = BCD2BIN(rtc->bcd_sec);
  47. wtime.tm_min = BCD2BIN(rtc->bcd_min);
  48. wtime.tm_hour = BCD2BIN(rtc->bcd_hr);
  49. wtime.tm_mday = BCD2BIN(rtc->bcd_dom);
  50. wtime.tm_mon = BCD2BIN(rtc->bcd_mth)-1;
  51. wtime.tm_year = BCD2BIN(rtc->bcd_year);
  52. if (wtime.tm_year < 70)
  53. wtime.tm_year += 100;
  54. wtime.tm_wday = BCD2BIN(rtc->bcd_dow)-1;
  55. rtc->ctrl = 0;
  56. local_irq_restore(flags);
  57. return copy_to_user(argp, &wtime, sizeof wtime) ?
  58. -EFAULT : 0;
  59. }
  60. case RTC_SET_TIME: /* Set the RTC */
  61. {
  62. struct rtc_time rtc_tm;
  63. unsigned char mon, day, hrs, min, sec, leap_yr;
  64. unsigned int yrs;
  65. if (!capable(CAP_SYS_ADMIN))
  66. return -EACCES;
  67. if (copy_from_user(&rtc_tm, argp, sizeof(struct rtc_time)))
  68. return -EFAULT;
  69. yrs = rtc_tm.tm_year;
  70. if (yrs < 1900)
  71. yrs += 1900;
  72. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  73. day = rtc_tm.tm_mday;
  74. hrs = rtc_tm.tm_hour;
  75. min = rtc_tm.tm_min;
  76. sec = rtc_tm.tm_sec;
  77. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  78. if ((mon > 12) || (day == 0))
  79. return -EINVAL;
  80. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  81. return -EINVAL;
  82. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  83. return -EINVAL;
  84. if (yrs >= 2070)
  85. return -EINVAL;
  86. local_irq_save(flags);
  87. rtc->ctrl = RTC_WRITE;
  88. rtc->bcd_sec = BIN2BCD(sec);
  89. rtc->bcd_min = BIN2BCD(min);
  90. rtc->bcd_hr = BIN2BCD(hrs);
  91. rtc->bcd_dom = BIN2BCD(day);
  92. rtc->bcd_mth = BIN2BCD(mon);
  93. rtc->bcd_year = BIN2BCD(yrs%100);
  94. rtc->ctrl = 0;
  95. local_irq_restore(flags);
  96. return 0;
  97. }
  98. default:
  99. return -EINVAL;
  100. }
  101. }
  102. /*
  103. * We enforce only one user at a time here with the open/close.
  104. * Also clear the previous interrupt data on an open, and clean
  105. * up things on a close.
  106. */
  107. static int rtc_open(struct inode *inode, struct file *file)
  108. {
  109. if( !atomic_dec_and_test(&rtc_ready) )
  110. {
  111. atomic_inc( &rtc_ready );
  112. return -EBUSY;
  113. }
  114. return 0;
  115. }
  116. static int rtc_release(struct inode *inode, struct file *file)
  117. {
  118. atomic_inc( &rtc_ready );
  119. return 0;
  120. }
  121. /*
  122. * The various file operations we support.
  123. */
  124. static const struct file_operations rtc_fops = {
  125. .ioctl = rtc_ioctl,
  126. .open = rtc_open,
  127. .release = rtc_release,
  128. };
  129. static struct miscdevice rtc_dev=
  130. {
  131. .minor = RTC_MINOR,
  132. .name = "rtc",
  133. .fops = &rtc_fops
  134. };
  135. static int __init rtc_MK48T08_init(void)
  136. {
  137. if (!MACH_IS_MVME16x)
  138. return -ENODEV;
  139. printk(KERN_INFO "MK48T08 Real Time Clock Driver v%s\n", RTC_VERSION);
  140. return misc_register(&rtc_dev);
  141. }
  142. module_init(rtc_MK48T08_init);