ip27-rtc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/types.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/ioport.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/rtc.h>
  33. #include <linux/init.h>
  34. #include <linux/poll.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/smp_lock.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 int rtc_ioctl(struct inode *inode, struct file *file,
  47. unsigned int cmd, 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 int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  67. unsigned long arg)
  68. {
  69. struct rtc_time wtime;
  70. switch (cmd) {
  71. case RTC_RD_TIME: /* Read the time/date from RTC */
  72. {
  73. get_rtc_time(&wtime);
  74. break;
  75. }
  76. case RTC_SET_TIME: /* Set the RTC */
  77. {
  78. struct rtc_time rtc_tm;
  79. unsigned char mon, day, hrs, min, sec, leap_yr;
  80. unsigned int yrs;
  81. if (!capable(CAP_SYS_TIME))
  82. return -EACCES;
  83. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
  84. sizeof(struct rtc_time)))
  85. return -EFAULT;
  86. yrs = rtc_tm.tm_year + 1900;
  87. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  88. day = rtc_tm.tm_mday;
  89. hrs = rtc_tm.tm_hour;
  90. min = rtc_tm.tm_min;
  91. sec = rtc_tm.tm_sec;
  92. if (yrs < 1970)
  93. return -EINVAL;
  94. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  95. if ((mon > 12) || (day == 0))
  96. return -EINVAL;
  97. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  98. return -EINVAL;
  99. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  100. return -EINVAL;
  101. if ((yrs -= epoch) > 255) /* They are unsigned */
  102. return -EINVAL;
  103. if (yrs > 169)
  104. return -EINVAL;
  105. if (yrs >= 100)
  106. yrs -= 100;
  107. sec = BIN2BCD(sec);
  108. min = BIN2BCD(min);
  109. hrs = BIN2BCD(hrs);
  110. day = BIN2BCD(day);
  111. mon = BIN2BCD(mon);
  112. yrs = BIN2BCD(yrs);
  113. spin_lock_irq(&rtc_lock);
  114. rtc->control |= M48T35_RTC_SET;
  115. rtc->year = yrs;
  116. rtc->month = mon;
  117. rtc->date = day;
  118. rtc->hour = hrs;
  119. rtc->min = min;
  120. rtc->sec = sec;
  121. rtc->control &= ~M48T35_RTC_SET;
  122. spin_unlock_irq(&rtc_lock);
  123. return 0;
  124. }
  125. default:
  126. return -EINVAL;
  127. }
  128. return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  129. }
  130. /*
  131. * We enforce only one user at a time here with the open/close.
  132. * Also clear the previous interrupt data on an open, and clean
  133. * up things on a close.
  134. */
  135. static int rtc_open(struct inode *inode, struct file *file)
  136. {
  137. spin_lock_irq(&rtc_lock);
  138. if (rtc_status & RTC_IS_OPEN) {
  139. spin_unlock_irq(&rtc_lock);
  140. return -EBUSY;
  141. }
  142. rtc_status |= RTC_IS_OPEN;
  143. spin_unlock_irq(&rtc_lock);
  144. return 0;
  145. }
  146. static int rtc_release(struct inode *inode, struct file *file)
  147. {
  148. /*
  149. * Turn off all interrupts once the device is no longer
  150. * in use, and clear the data.
  151. */
  152. spin_lock_irq(&rtc_lock);
  153. rtc_status &= ~RTC_IS_OPEN;
  154. spin_unlock_irq(&rtc_lock);
  155. return 0;
  156. }
  157. /*
  158. * The various file operations we support.
  159. */
  160. static struct file_operations rtc_fops = {
  161. .owner = THIS_MODULE,
  162. .ioctl = rtc_ioctl,
  163. .open = rtc_open,
  164. .release = rtc_release,
  165. };
  166. static struct miscdevice rtc_dev=
  167. {
  168. RTC_MINOR,
  169. "rtc",
  170. &rtc_fops
  171. };
  172. static int __init rtc_init(void)
  173. {
  174. rtc = (struct m48t35_rtc *)
  175. (KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base + IOC3_BYTEBUS_DEV0);
  176. printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION);
  177. if (misc_register(&rtc_dev)) {
  178. printk(KERN_ERR "rtc: cannot register misc device.\n");
  179. return -ENODEV;
  180. }
  181. if (!create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)) {
  182. printk(KERN_ERR "rtc: cannot create /proc/rtc.\n");
  183. misc_deregister(&rtc_dev);
  184. return -ENOENT;
  185. }
  186. rtc_freq = 1024;
  187. return 0;
  188. }
  189. static void __exit rtc_exit (void)
  190. {
  191. /* interrupts and timer disabled at this point by rtc_release */
  192. remove_proc_entry ("rtc", NULL);
  193. misc_deregister(&rtc_dev);
  194. }
  195. module_init(rtc_init);
  196. module_exit(rtc_exit);
  197. /*
  198. * Info exported via "/proc/rtc".
  199. */
  200. static int rtc_get_status(char *buf)
  201. {
  202. char *p;
  203. struct rtc_time tm;
  204. /*
  205. * Just emulate the standard /proc/rtc
  206. */
  207. p = buf;
  208. get_rtc_time(&tm);
  209. /*
  210. * There is no way to tell if the luser has the RTC set for local
  211. * time or for Universal Standard Time (GMT). Probably local though.
  212. */
  213. p += sprintf(p,
  214. "rtc_time\t: %02d:%02d:%02d\n"
  215. "rtc_date\t: %04d-%02d-%02d\n"
  216. "rtc_epoch\t: %04lu\n"
  217. "24hr\t\t: yes\n",
  218. tm.tm_hour, tm.tm_min, tm.tm_sec,
  219. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
  220. return p - buf;
  221. }
  222. static int rtc_read_proc(char *page, char **start, off_t off,
  223. int count, int *eof, void *data)
  224. {
  225. int len = rtc_get_status(page);
  226. if (len <= off+count) *eof = 1;
  227. *start = page + off;
  228. len -= off;
  229. if (len>count) len = count;
  230. if (len<0) len = 0;
  231. return len;
  232. }
  233. static void get_rtc_time(struct rtc_time *rtc_tm)
  234. {
  235. /*
  236. * Do we need to wait for the last update to finish?
  237. */
  238. /*
  239. * Only the values that we read from the RTC are set. We leave
  240. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  241. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  242. * by the RTC when initially set to a non-zero value.
  243. */
  244. spin_lock_irq(&rtc_lock);
  245. rtc->control |= M48T35_RTC_READ;
  246. rtc_tm->tm_sec = rtc->sec;
  247. rtc_tm->tm_min = rtc->min;
  248. rtc_tm->tm_hour = rtc->hour;
  249. rtc_tm->tm_mday = rtc->date;
  250. rtc_tm->tm_mon = rtc->month;
  251. rtc_tm->tm_year = rtc->year;
  252. rtc->control &= ~M48T35_RTC_READ;
  253. spin_unlock_irq(&rtc_lock);
  254. rtc_tm->tm_sec = BCD2BIN(rtc_tm->tm_sec);
  255. rtc_tm->tm_min = BCD2BIN(rtc_tm->tm_min);
  256. rtc_tm->tm_hour = BCD2BIN(rtc_tm->tm_hour);
  257. rtc_tm->tm_mday = BCD2BIN(rtc_tm->tm_mday);
  258. rtc_tm->tm_mon = BCD2BIN(rtc_tm->tm_mon);
  259. rtc_tm->tm_year = BCD2BIN(rtc_tm->tm_year);
  260. /*
  261. * Account for differences between how the RTC uses the values
  262. * and how they are defined in a struct rtc_time;
  263. */
  264. if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
  265. rtc_tm->tm_year += 100;
  266. rtc_tm->tm_mon--;
  267. }