rtc.c 3.9 KB

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