ip27-rtc.c 7.5 KB

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