ip27-rtc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/types.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/ioport.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/rtc.h>
  32. #include <linux/init.h>
  33. #include <linux/poll.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/smp_lock.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. static int rtc_ioctl(struct inode *inode, struct file *file,
  45.      unsigned int cmd, unsigned long arg);
  46. static int rtc_read_proc(char *page, char **start, off_t off,
  47.                          int count, int *eof, void *data);
  48. static void get_rtc_time(struct rtc_time *rtc_tm);
  49. /*
  50.  * Bits in rtc_status. (6 bits of room for future expansion)
  51.  */
  52. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  53. #define RTC_TIMER_ON 0x02 /* missed irq timer active */
  54. static unsigned char rtc_status; /* bitmapped status byte. */
  55. static spinlock_t rtc_status_lock = SPIN_LOCK_UNLOCKED;
  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 int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  66.      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. unsigned long flags;
  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. save_flags(flags);
  104. cli();
  105. if (yrs > 169) {
  106. restore_flags(flags);
  107. return -EINVAL;
  108. }
  109. if (yrs >= 100)
  110. yrs -= 100;
  111. BIN_TO_BCD(sec);
  112. BIN_TO_BCD(min);
  113. BIN_TO_BCD(hrs);
  114. BIN_TO_BCD(day);
  115. BIN_TO_BCD(mon);
  116. BIN_TO_BCD(yrs);
  117. rtc->control &= ~M48T35_RTC_SET;
  118. rtc->year = yrs;
  119. rtc->month = mon;
  120. rtc->date = day;
  121. rtc->hour = hrs;
  122. rtc->min = min;
  123. rtc->sec = sec;
  124. rtc->control &= ~M48T35_RTC_SET;
  125. restore_flags(flags);
  126. return 0;
  127. }
  128. default:
  129. return -EINVAL;
  130. }
  131. return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  132. }
  133. /*
  134.  * We enforce only one user at a time here with the open/close.
  135.  * Also clear the previous interrupt data on an open, and clean
  136.  * up things on a close.
  137.  */
  138. static int rtc_open(struct inode *inode, struct file *file)
  139. {
  140. spin_lock(rtc_status_lock);
  141. if (rtc_status & RTC_IS_OPEN) {
  142. spin_unlock(rtc_status_lock);
  143. return -EBUSY;
  144. }
  145. rtc_status |= RTC_IS_OPEN;
  146. spin_unlock(rtc_status_lock);
  147. return 0;
  148. }
  149. static int rtc_release(struct inode *inode, struct file *file)
  150. {
  151. /*
  152.  * Turn off all interrupts once the device is no longer
  153.  * in use, and clear the data.
  154.  */
  155. spin_lock(rtc_status_lock);
  156. rtc_status &= ~RTC_IS_OPEN;
  157. spin_unlock(rtc_status_lock);
  158. return 0;
  159. }
  160. /*
  161.  * The various file operations we support.
  162.  */
  163. static struct file_operations rtc_fops = {
  164. owner: THIS_MODULE,
  165. ioctl: rtc_ioctl,
  166. open: rtc_open,
  167. release: rtc_release,
  168. };
  169. static struct miscdevice rtc_dev=
  170. {
  171. RTC_MINOR,
  172. "rtc",
  173. &rtc_fops
  174. };
  175. static int __init rtc_init(void)
  176. {
  177. unsigned long flags;
  178. nasid_t nid;
  179. nid = get_nasid();
  180. rtc = (struct m48t35_rtc *)
  181.     KL_CONFIG_CH_CONS_INFO(nid)->memory_base + IOC3_BYTEBUS_DEV0;
  182. printk(KERN_INFO "Real Time Clock Driver v%sn", RTC_VERSION);
  183. if (misc_register(&rtc_dev))
  184. return -ENODEV;
  185. create_proc_read_entry ("rtc", 0, NULL, rtc_read_proc, NULL);
  186. save_flags(flags);
  187. cli();
  188. restore_flags(flags);
  189. rtc_freq = 1024;
  190. return 0;
  191. }
  192. static void __exit rtc_exit (void)
  193. {
  194. /* interrupts and timer disabled at this point by rtc_release */
  195. remove_proc_entry ("rtc", NULL);
  196. misc_deregister(&rtc_dev);
  197. }
  198. module_init(rtc_init);
  199. module_exit(rtc_exit);
  200. EXPORT_NO_SYMBOLS;
  201. /*
  202.  * Info exported via "/proc/rtc".
  203.  */
  204. static int rtc_get_status(char *buf)
  205. {
  206. char *p;
  207. struct rtc_time tm;
  208. /*
  209.  * Just emulate the standard /proc/rtc
  210.  */
  211. p = buf;
  212. get_rtc_time(&tm);
  213. /*
  214.  * There is no way to tell if the luser has the RTC set for local
  215.  * time or for Universal Standard Time (GMT). Probably local though.
  216.  */
  217. p += sprintf(p,
  218.      "rtc_timet: %02d:%02d:%02dn"
  219.      "rtc_datet: %04d-%02d-%02dn"
  220.        "rtc_epocht: %04lun"
  221.      "24hrtt: yesn",
  222.      tm.tm_hour, tm.tm_min, tm.tm_sec,
  223.      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
  224. return  p - buf;
  225. }
  226. static int rtc_read_proc(char *page, char **start, off_t off,
  227.                                  int count, int *eof, void *data)
  228. {
  229.         int len = rtc_get_status(page);
  230.         if (len <= off+count) *eof = 1;
  231.         *start = page + off;
  232.         len -= off;
  233.         if (len>count) len = count;
  234.         if (len<0) len = 0;
  235.         return len;
  236. }
  237. static void get_rtc_time(struct rtc_time *rtc_tm)
  238. {
  239. unsigned long flags;
  240. /*
  241.  * Do we need to wait for the last update to finish?
  242.  */
  243. /*
  244.  * Only the values that we read from the RTC are set. We leave
  245.  * tm_wday, tm_yday and tm_isdst untouched. Even though the
  246.  * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  247.  * by the RTC when initially set to a non-zero value.
  248.  */
  249. save_flags(flags);
  250. cli();
  251.         rtc->control |= M48T35_RTC_READ;
  252. rtc_tm->tm_sec = rtc->sec;
  253. rtc_tm->tm_min = rtc->min;
  254. rtc_tm->tm_hour = rtc->hour;
  255. rtc_tm->tm_mday = rtc->date;
  256. rtc_tm->tm_mon = rtc->month;
  257. rtc_tm->tm_year = rtc->year;
  258. rtc->control &= ~M48T35_RTC_READ;
  259. restore_flags(flags);
  260. BCD_TO_BIN(rtc_tm->tm_sec);
  261. BCD_TO_BIN(rtc_tm->tm_min);
  262. BCD_TO_BIN(rtc_tm->tm_hour);
  263. BCD_TO_BIN(rtc_tm->tm_mday);
  264. BCD_TO_BIN(rtc_tm->tm_mon);
  265. BCD_TO_BIN(rtc_tm->tm_year);
  266. /*
  267.  * Account for differences between how the RTC uses the values
  268.  * and how they are defined in a struct rtc_time;
  269.  */
  270. if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
  271. rtc_tm->tm_year += 100;
  272. rtc_tm->tm_mon--;
  273. }