rtc.c 6.9 KB

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