PageRenderTime 32ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/m68k/mvme16x/rtc.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 164 lines | 120 code | 26 blank | 18 comment | 19 complexity | bb9bd97fd73e32fe97fc2112f123f590 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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/ioport.h>
  11. #include <linux/capability.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/bcd.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. 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. };
  125. static struct miscdevice rtc_dev=
  126. {
  127. .minor = RTC_MINOR,
  128. .name = "rtc",
  129. .fops = &rtc_fops
  130. };
  131. static int __init rtc_MK48T08_init(void)
  132. {
  133. if (!MACH_IS_MVME16x)
  134. return -ENODEV;
  135. printk(KERN_INFO "MK48T08 Real Time Clock Driver v%s\n", RTC_VERSION);
  136. return misc_register(&rtc_dev);
  137. }
  138. module_init(rtc_MK48T08_init);