ip27-rtc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
  3. *
  4. * Real Time Clock interface for Linux
  5. *
  6. * TODO: Implement periodic interrupts.
  7. *
  8. * Copyright (C) 2000 Silicon Graphics, Inc.
  9. * Written by Ulf Carlsson (ulfc@engr.sgi.com)
  10. *
  11. * Based on code written by Paul Gortmaker.
  12. *
  13. * This driver allows use of the real time clock (built into
  14. * nearly all computers) from user space. It exports the /dev/rtc
  15. * interface supporting various ioctl() and also the /proc/rtc
  16. * pseudo-file for status information.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. */
  24. #define RTC_VERSION "1.09b"
  25. #include <linux/bcd.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/types.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/ioport.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/rtc.h>
  34. #include <linux/init.h>
  35. #include <linux/poll.h>
  36. #include <linux/proc_fs.h>
  37. #include <asm/m48t35.h>
  38. #include <asm/sn/ioc3.h>
  39. #include <asm/io.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/system.h>
  42. #include <asm/sn/klconfig.h>
  43. #include <asm/sn/sn0/ip27.h>
  44. #include <asm/sn/sn0/hub.h>
  45. #include <asm/sn/sn_private.h>
  46. static long rtc_ioctl(struct file *filp, unsigned int cmd,
  47. unsigned long arg);
  48. static int rtc_read_proc(char *page, char **start, off_t off,
  49. int count, int *eof, void *data);
  50. static void get_rtc_time(struct rtc_time *rtc_tm);
  51. /*
  52. * Bits in rtc_status. (6 bits of room for future expansion)
  53. */
  54. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  55. #define RTC_TIMER_ON 0x02 /* missed irq timer active */
  56. static unsigned char rtc_status; /* bitmapped status byte. */
  57. static unsigned long rtc_freq; /* Current periodic IRQ rate */
  58. static struct m48t35_rtc *rtc;
  59. /*
  60. * If this driver ever becomes modularised, it will be really nice
  61. * to make the epoch retain its value across module reload...
  62. */
  63. static unsigned long epoch = 1970; /* year corresponding to 0x00 */
  64. static const unsigned char days_in_mo[] =
  65. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  66. static long rtc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  67. {
  68. struct rtc_time wtime;
  69. switch (cmd) {
  70. case RTC_RD_TIME: /* Read the time/date from RTC */
  71. {
  72. get_rtc_time(&wtime);
  73. break;
  74. }
  75. case RTC_SET_TIME: /* Set the RTC */
  76. {
  77. struct rtc_time rtc_tm;
  78. unsigned char mon, day, hrs, min, sec, leap_yr;
  79. unsigned int yrs;
  80. if (!capable(CAP_SYS_TIME))
  81. return -EACCES;
  82. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
  83. sizeof(struct rtc_time)))
  84. return -EFAULT;
  85. yrs = rtc_tm.tm_year + 1900;
  86. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  87. day = rtc_tm.tm_mday;
  88. hrs = rtc_tm.tm_hour;
  89. min = rtc_tm.tm_min;
  90. sec = rtc_tm.tm_sec;
  91. if (yrs < 1970)
  92. return -EINVAL;
  93. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  94. if ((mon > 12) || (day == 0))
  95. return -EINVAL;
  96. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  97. return -EINVAL;
  98. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  99. return -EINVAL;
  100. if ((yrs -= epoch) > 255) /* They are unsigned */
  101. return -EINVAL;
  102. if (yrs > 169)
  103. return -EINVAL;
  104. if (yrs >= 100)
  105. yrs -= 100;
  106. sec = BIN2BCD(sec);
  107. min = BIN2BCD(min);
  108. hrs = BIN2BCD(hrs);
  109. day = BIN2BCD(day);
  110. mon = BIN2BCD(mon);
  111. yrs = BIN2BCD(yrs);
  112. spin_lock_irq(&rtc_lock);
  113. rtc->control |= M48T35_RTC_SET;
  114. rtc->year = yrs;
  115. rtc->month = mon;
  116. rtc->date = day;
  117. rtc->hour = hrs;
  118. rtc->min = min;
  119. rtc->sec = sec;
  120. rtc->control &= ~M48T35_RTC_SET;
  121. spin_unlock_irq(&rtc_lock);
  122. return 0;
  123. }
  124. default:
  125. return -EINVAL;
  126. }
  127. return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  128. }
  129. /*
  130. * We enforce only one user at a time here with the open/close.
  131. * Also clear the previous interrupt data on an open, and clean
  132. * up things on a close.
  133. */
  134. static int rtc_open(struct inode *inode, struct file *file)
  135. {
  136. lock_kernel();
  137. spin_lock_irq(&rtc_lock);
  138. if (rtc_status & RTC_IS_OPEN) {
  139. spin_unlock_irq(&rtc_lock);
  140. unlock_kernel();
  141. return -EBUSY;
  142. }
  143. rtc_status |= RTC_IS_OPEN;
  144. spin_unlock_irq(&rtc_lock);
  145. unlock_kernel();
  146. return 0;
  147. }
  148. static int rtc_release(struct inode *inode, struct file *file)
  149. {
  150. /*
  151. * Turn off all interrupts once the device is no longer
  152. * in use, and clear the data.
  153. */
  154. spin_lock_irq(&rtc_lock);
  155. rtc_status &= ~RTC_IS_OPEN;
  156. spin_unlock_irq(&rtc_lock);
  157. return 0;
  158. }
  159. /*
  160. * The various file operations we support.
  161. */
  162. static const struct file_operations rtc_fops = {
  163. .owner = THIS_MODULE,
  164. .unlocked_ioctl = rtc_ioctl,
  165. .open = rtc_open,
  166. .release = rtc_release,
  167. };
  168. static struct miscdevice rtc_dev=
  169. {
  170. RTC_MINOR,
  171. "rtc",
  172. &rtc_fops
  173. };
  174. static int __init rtc_init(void)
  175. {
  176. rtc = (struct m48t35_rtc *)
  177. (KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base + IOC3_BYTEBUS_DEV0);
  178. printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION);
  179. if (misc_register(&rtc_dev)) {
  180. printk(KERN_ERR "rtc: cannot register misc device.\n");
  181. return -ENODEV;
  182. }
  183. if (!create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)) {
  184. printk(KERN_ERR "rtc: cannot create /proc/rtc.\n");
  185. misc_deregister(&rtc_dev);
  186. return -ENOENT;
  187. }
  188. rtc_freq = 1024;
  189. return 0;
  190. }
  191. static void __exit rtc_exit (void)
  192. {
  193. /* interrupts and timer disabled at this point by rtc_release */
  194. remove_proc_entry ("rtc", NULL);
  195. misc_deregister(&rtc_dev);
  196. }
  197. module_init(rtc_init);
  198. module_exit(rtc_exit);
  199. /*
  200. * Info exported via "/proc/rtc".
  201. */
  202. static int rtc_get_status(char *buf)
  203. {
  204. char *p;
  205. struct rtc_time tm;
  206. /*
  207. * Just emulate the standard /proc/rtc
  208. */
  209. p = buf;
  210. get_rtc_time(&tm);
  211. /*
  212. * There is no way to tell if the luser has the RTC set for local
  213. * time or for Universal Standard Time (GMT). Probably local though.
  214. */
  215. p += sprintf(p,
  216. "rtc_time\t: %02d:%02d:%02d\n"
  217. "rtc_date\t: %04d-%02d-%02d\n"
  218. "rtc_epoch\t: %04lu\n"
  219. "24hr\t\t: yes\n",
  220. tm.tm_hour, tm.tm_min, tm.tm_sec,
  221. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
  222. return p - buf;
  223. }
  224. static int rtc_read_proc(char *page, char **start, off_t off,
  225. int count, int *eof, void *data)
  226. {
  227. int len = rtc_get_status(page);
  228. if (len <= off+count) *eof = 1;
  229. *start = page + off;
  230. len -= off;
  231. if (len>count) len = count;
  232. if (len<0) len = 0;
  233. return len;
  234. }
  235. static void get_rtc_time(struct rtc_time *rtc_tm)
  236. {
  237. /*
  238. * Do we need to wait for the last update to finish?
  239. */
  240. /*
  241. * Only the values that we read from the RTC are set. We leave
  242. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  243. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  244. * by the RTC when initially set to a non-zero value.
  245. */
  246. spin_lock_irq(&rtc_lock);
  247. rtc->control |= M48T35_RTC_READ;
  248. rtc_tm->tm_sec = rtc->sec;
  249. rtc_tm->tm_min = rtc->min;
  250. rtc_tm->tm_hour = rtc->hour;
  251. rtc_tm->tm_mday = rtc->date;
  252. rtc_tm->tm_mon = rtc->month;
  253. rtc_tm->tm_year = rtc->year;
  254. rtc->control &= ~M48T35_RTC_READ;
  255. spin_unlock_irq(&rtc_lock);
  256. rtc_tm->tm_sec = BCD2BIN(rtc_tm->tm_sec);
  257. rtc_tm->tm_min = BCD2BIN(rtc_tm->tm_min);
  258. rtc_tm->tm_hour = BCD2BIN(rtc_tm->tm_hour);
  259. rtc_tm->tm_mday = BCD2BIN(rtc_tm->tm_mday);
  260. rtc_tm->tm_mon = BCD2BIN(rtc_tm->tm_mon);
  261. rtc_tm->tm_year = BCD2BIN(rtc_tm->tm_year);
  262. /*
  263. * Account for differences between how the RTC uses the values
  264. * and how they are defined in a struct rtc_time;
  265. */
  266. if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
  267. rtc_tm->tm_year += 100;
  268. rtc_tm->tm_mon--;
  269. }