rtc.c 3.9 KB

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