rtc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* $Id: rtc.c,v 1.28 2001/10/08 22:19:51 davem Exp $
  2. *
  3. * Linux/SPARC Real Time Clock Driver
  4. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  5. *
  6. * This is a little driver that lets a user-level program access
  7. * the SPARC Mostek real time clock chip. It is no use unless you
  8. * use the modified clock utility.
  9. *
  10. * Get the modified clock utility from:
  11. * ftp://vger.kernel.org/pub/linux/Sparc/userland/clock.c
  12. */
  13. #include <linux/module.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/slab.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/poll.h>
  21. #include <linux/init.h>
  22. #include <asm/io.h>
  23. #include <asm/mostek.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/rtc.h>
  27. static int rtc_busy = 0;
  28. /* This is the structure layout used by drivers/char/rtc.c, we
  29. * support that driver's ioctls so that things are less messy in
  30. * userspace.
  31. */
  32. struct rtc_time_generic {
  33. int tm_sec;
  34. int tm_min;
  35. int tm_hour;
  36. int tm_mday;
  37. int tm_mon;
  38. int tm_year;
  39. int tm_wday;
  40. int tm_yday;
  41. int tm_isdst;
  42. };
  43. #define RTC_AIE_ON _IO('p', 0x01) /* Alarm int. enable on */
  44. #define RTC_AIE_OFF _IO('p', 0x02) /* ... off */
  45. #define RTC_UIE_ON _IO('p', 0x03) /* Update int. enable on */
  46. #define RTC_UIE_OFF _IO('p', 0x04) /* ... off */
  47. #define RTC_PIE_ON _IO('p', 0x05) /* Periodic int. enable on */
  48. #define RTC_PIE_OFF _IO('p', 0x06) /* ... off */
  49. #define RTC_WIE_ON _IO('p', 0x0f) /* Watchdog int. enable on */
  50. #define RTC_WIE_OFF _IO('p', 0x10) /* ... off */
  51. #define RTC_RD_TIME _IOR('p', 0x09, struct rtc_time_generic) /* Read RTC time */
  52. #define RTC_SET_TIME _IOW('p', 0x0a, struct rtc_time_generic) /* Set RTC time */
  53. #define RTC_ALM_SET _IOW('p', 0x07, struct rtc_time) /* Set alarm time */
  54. #define RTC_ALM_READ _IOR('p', 0x08, struct rtc_time) /* Read alarm time */
  55. #define RTC_IRQP_READ _IOR('p', 0x0b, unsigned long) /* Read IRQ rate */
  56. #define RTC_IRQP_SET _IOW('p', 0x0c, unsigned long) /* Set IRQ rate */
  57. #define RTC_EPOCH_READ _IOR('p', 0x0d, unsigned long) /* Read epoch */
  58. #define RTC_EPOCH_SET _IOW('p', 0x0e, unsigned long) /* Set epoch */
  59. #define RTC_WKALM_SET _IOW('p', 0x0f, struct rtc_wkalrm)/* Set wakeup alarm*/
  60. #define RTC_WKALM_RD _IOR('p', 0x10, struct rtc_wkalrm)/* Get wakeup alarm*/
  61. #define RTC_PLL_GET _IOR('p', 0x11, struct rtc_pll_info) /* Get PLL correction */
  62. #define RTC_PLL_SET _IOW('p', 0x12, struct rtc_pll_info) /* Set PLL correction */
  63. /* Retrieve the current date and time from the real time clock. */
  64. static void get_rtc_time(struct rtc_time *t)
  65. {
  66. void __iomem *regs = mstk48t02_regs;
  67. u8 tmp;
  68. spin_lock_irq(&mostek_lock);
  69. tmp = mostek_read(regs + MOSTEK_CREG);
  70. tmp |= MSTK_CREG_READ;
  71. mostek_write(regs + MOSTEK_CREG, tmp);
  72. t->sec = MSTK_REG_SEC(regs);
  73. t->min = MSTK_REG_MIN(regs);
  74. t->hour = MSTK_REG_HOUR(regs);
  75. t->dow = MSTK_REG_DOW(regs);
  76. t->dom = MSTK_REG_DOM(regs);
  77. t->month = MSTK_REG_MONTH(regs);
  78. t->year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
  79. tmp = mostek_read(regs + MOSTEK_CREG);
  80. tmp &= ~MSTK_CREG_READ;
  81. mostek_write(regs + MOSTEK_CREG, tmp);
  82. spin_unlock_irq(&mostek_lock);
  83. }
  84. /* Set the current date and time inthe real time clock. */
  85. void set_rtc_time(struct rtc_time *t)
  86. {
  87. void __iomem *regs = mstk48t02_regs;
  88. u8 tmp;
  89. spin_lock_irq(&mostek_lock);
  90. tmp = mostek_read(regs + MOSTEK_CREG);
  91. tmp |= MSTK_CREG_WRITE;
  92. mostek_write(regs + MOSTEK_CREG, tmp);
  93. MSTK_SET_REG_SEC(regs,t->sec);
  94. MSTK_SET_REG_MIN(regs,t->min);
  95. MSTK_SET_REG_HOUR(regs,t->hour);
  96. MSTK_SET_REG_DOW(regs,t->dow);
  97. MSTK_SET_REG_DOM(regs,t->dom);
  98. MSTK_SET_REG_MONTH(regs,t->month);
  99. MSTK_SET_REG_YEAR(regs,t->year - MSTK_YEAR_ZERO);
  100. tmp = mostek_read(regs + MOSTEK_CREG);
  101. tmp &= ~MSTK_CREG_WRITE;
  102. mostek_write(regs + MOSTEK_CREG, tmp);
  103. spin_unlock_irq(&mostek_lock);
  104. }
  105. static int put_rtc_time_generic(void __user *argp, struct rtc_time *tm)
  106. {
  107. struct rtc_time_generic __user *utm = argp;
  108. if (__put_user(tm->sec, &utm->tm_sec) ||
  109. __put_user(tm->min, &utm->tm_min) ||
  110. __put_user(tm->hour, &utm->tm_hour) ||
  111. __put_user(tm->dom, &utm->tm_mday) ||
  112. __put_user(tm->month, &utm->tm_mon) ||
  113. __put_user(tm->year, &utm->tm_year) ||
  114. __put_user(tm->dow, &utm->tm_wday) ||
  115. __put_user(0, &utm->tm_yday) ||
  116. __put_user(0, &utm->tm_isdst))
  117. return -EFAULT;
  118. return 0;
  119. }
  120. static int get_rtc_time_generic(struct rtc_time *tm, void __user *argp)
  121. {
  122. struct rtc_time_generic __user *utm = argp;
  123. if (__get_user(tm->sec, &utm->tm_sec) ||
  124. __get_user(tm->min, &utm->tm_min) ||
  125. __get_user(tm->hour, &utm->tm_hour) ||
  126. __get_user(tm->dom, &utm->tm_mday) ||
  127. __get_user(tm->month, &utm->tm_mon) ||
  128. __get_user(tm->year, &utm->tm_year) ||
  129. __get_user(tm->dow, &utm->tm_wday))
  130. return -EFAULT;
  131. return 0;
  132. }
  133. static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  134. unsigned long arg)
  135. {
  136. struct rtc_time rtc_tm;
  137. void __user *argp = (void __user *)arg;
  138. switch (cmd) {
  139. /* No interrupt support, return an error
  140. * compatible with drivers/char/rtc.c
  141. */
  142. case RTC_AIE_OFF:
  143. case RTC_AIE_ON:
  144. case RTC_PIE_OFF:
  145. case RTC_PIE_ON:
  146. case RTC_UIE_OFF:
  147. case RTC_UIE_ON:
  148. case RTC_IRQP_READ:
  149. case RTC_IRQP_SET:
  150. case RTC_EPOCH_SET:
  151. case RTC_EPOCH_READ:
  152. return -EINVAL;
  153. case RTCGET:
  154. case RTC_RD_TIME:
  155. memset(&rtc_tm, 0, sizeof(struct rtc_time));
  156. get_rtc_time(&rtc_tm);
  157. if (cmd == RTCGET) {
  158. if (copy_to_user(argp, &rtc_tm,
  159. sizeof(struct rtc_time)))
  160. return -EFAULT;
  161. } else if (put_rtc_time_generic(argp, &rtc_tm))
  162. return -EFAULT;
  163. return 0;
  164. case RTCSET:
  165. case RTC_SET_TIME:
  166. if (!capable(CAP_SYS_TIME))
  167. return -EPERM;
  168. if (cmd == RTCSET) {
  169. if (copy_from_user(&rtc_tm, argp,
  170. sizeof(struct rtc_time)))
  171. return -EFAULT;
  172. } else if (get_rtc_time_generic(&rtc_tm, argp))
  173. return -EFAULT;
  174. set_rtc_time(&rtc_tm);
  175. return 0;
  176. default:
  177. return -EINVAL;
  178. }
  179. }
  180. static int rtc_open(struct inode *inode, struct file *file)
  181. {
  182. int ret;
  183. lock_kernel();
  184. spin_lock_irq(&mostek_lock);
  185. if (rtc_busy) {
  186. ret = -EBUSY;
  187. } else {
  188. rtc_busy = 1;
  189. ret = 0;
  190. }
  191. spin_unlock_irq(&mostek_lock);
  192. unlock_kernel();
  193. return ret;
  194. }
  195. static int rtc_release(struct inode *inode, struct file *file)
  196. {
  197. rtc_busy = 0;
  198. return 0;
  199. }
  200. static const struct file_operations rtc_fops = {
  201. .owner = THIS_MODULE,
  202. .llseek = no_llseek,
  203. .ioctl = rtc_ioctl,
  204. .open = rtc_open,
  205. .release = rtc_release,
  206. };
  207. static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
  208. static int __init rtc_sun_init(void)
  209. {
  210. int error;
  211. /* It is possible we are being driven by some other RTC chip
  212. * and thus another RTC driver is handling things.
  213. */
  214. if (!mstk48t02_regs)
  215. return -ENODEV;
  216. error = misc_register(&rtc_dev);
  217. if (error) {
  218. printk(KERN_ERR "rtc: unable to get misc minor for Mostek\n");
  219. return error;
  220. }
  221. printk("rtc_sun_init: Registered Mostek RTC driver.\n");
  222. return 0;
  223. }
  224. static void __exit rtc_sun_cleanup(void)
  225. {
  226. misc_deregister(&rtc_dev);
  227. }
  228. module_init(rtc_sun_init);
  229. module_exit(rtc_sun_cleanup);
  230. MODULE_LICENSE("GPL");