rtc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Real Time Clock interface for PPC64.
  3. *
  4. * Based on rtc.c by Paul Gortmaker
  5. *
  6. * This driver allows use of the real time clock
  7. * from user space. It exports the /dev/rtc
  8. * interface supporting various ioctl() and also the
  9. * /proc/driver/rtc pseudo-file for status information.
  10. *
  11. * Interface does not support RTC interrupts nor an alarm.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. *
  18. * 1.0 Mike Corrigan: IBM iSeries rtc support
  19. * 1.1 Dave Engebretsen: IBM pSeries rtc support
  20. */
  21. #define RTC_VERSION "1.1"
  22. #include <linux/config.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/types.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/ioport.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/mc146818rtc.h>
  30. #include <linux/init.h>
  31. #include <linux/poll.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/bcd.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/delay.h>
  37. #include <asm/io.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/system.h>
  40. #include <asm/time.h>
  41. #include <asm/rtas.h>
  42. #include <asm/machdep.h>
  43. /*
  44. * We sponge a minor off of the misc major. No need slurping
  45. * up another valuable major dev number for this. If you add
  46. * an ioctl, make sure you don't conflict with SPARC's RTC
  47. * ioctls.
  48. */
  49. static ssize_t rtc_read(struct file *file, char __user *buf,
  50. size_t count, loff_t *ppos);
  51. static int rtc_ioctl(struct inode *inode, struct file *file,
  52. unsigned int cmd, unsigned long arg);
  53. static int rtc_read_proc(char *page, char **start, off_t off,
  54. int count, int *eof, void *data);
  55. /*
  56. * If this driver ever becomes modularised, it will be really nice
  57. * to make the epoch retain its value across module reload...
  58. */
  59. static unsigned long epoch = 1900; /* year corresponding to 0x00 */
  60. static const unsigned char days_in_mo[] =
  61. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  62. /*
  63. * Now all the various file operations that we export.
  64. */
  65. static ssize_t rtc_read(struct file *file, char __user *buf,
  66. size_t count, loff_t *ppos)
  67. {
  68. return -EIO;
  69. }
  70. static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  71. unsigned long arg)
  72. {
  73. struct rtc_time wtime;
  74. switch (cmd) {
  75. case RTC_RD_TIME: /* Read the time/date from RTC */
  76. {
  77. memset(&wtime, 0, sizeof(struct rtc_time));
  78. ppc_md.get_rtc_time(&wtime);
  79. break;
  80. }
  81. case RTC_SET_TIME: /* Set the RTC */
  82. {
  83. struct rtc_time rtc_tm;
  84. unsigned char mon, day, hrs, min, sec, leap_yr;
  85. unsigned int yrs;
  86. if (!capable(CAP_SYS_TIME))
  87. return -EACCES;
  88. if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
  89. sizeof(struct rtc_time)))
  90. return -EFAULT;
  91. yrs = rtc_tm.tm_year;
  92. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  93. day = rtc_tm.tm_mday;
  94. hrs = rtc_tm.tm_hour;
  95. min = rtc_tm.tm_min;
  96. sec = rtc_tm.tm_sec;
  97. if (yrs < 70)
  98. return -EINVAL;
  99. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  100. if ((mon > 12) || (day == 0))
  101. return -EINVAL;
  102. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  103. return -EINVAL;
  104. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  105. return -EINVAL;
  106. if ( yrs > 169 )
  107. return -EINVAL;
  108. ppc_md.set_rtc_time(&rtc_tm);
  109. return 0;
  110. }
  111. case RTC_EPOCH_READ: /* Read the epoch. */
  112. {
  113. return put_user (epoch, (unsigned long __user *)arg);
  114. }
  115. case RTC_EPOCH_SET: /* Set the epoch. */
  116. {
  117. /*
  118. * There were no RTC clocks before 1900.
  119. */
  120. if (arg < 1900)
  121. return -EINVAL;
  122. if (!capable(CAP_SYS_TIME))
  123. return -EACCES;
  124. epoch = arg;
  125. return 0;
  126. }
  127. default:
  128. return -EINVAL;
  129. }
  130. return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  131. }
  132. static int rtc_open(struct inode *inode, struct file *file)
  133. {
  134. nonseekable_open(inode, file);
  135. return 0;
  136. }
  137. static int rtc_release(struct inode *inode, struct file *file)
  138. {
  139. return 0;
  140. }
  141. /*
  142. * The various file operations we support.
  143. */
  144. static struct file_operations rtc_fops = {
  145. .owner = THIS_MODULE,
  146. .llseek = no_llseek,
  147. .read = rtc_read,
  148. .ioctl = rtc_ioctl,
  149. .open = rtc_open,
  150. .release = rtc_release,
  151. };
  152. static struct miscdevice rtc_dev = {
  153. .minor = RTC_MINOR,
  154. .name = "rtc",
  155. .fops = &rtc_fops
  156. };
  157. static int __init rtc_init(void)
  158. {
  159. int retval;
  160. retval = misc_register(&rtc_dev);
  161. if(retval < 0)
  162. return retval;
  163. #ifdef CONFIG_PROC_FS
  164. if (create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)
  165. == NULL) {
  166. misc_deregister(&rtc_dev);
  167. return -ENOMEM;
  168. }
  169. #endif
  170. printk(KERN_INFO "i/pSeries Real Time Clock Driver v" RTC_VERSION "\n");
  171. return 0;
  172. }
  173. static void __exit rtc_exit (void)
  174. {
  175. remove_proc_entry ("driver/rtc", NULL);
  176. misc_deregister(&rtc_dev);
  177. }
  178. module_init(rtc_init);
  179. module_exit(rtc_exit);
  180. /*
  181. * Info exported via "/proc/driver/rtc".
  182. */
  183. static int rtc_proc_output (char *buf)
  184. {
  185. char *p;
  186. struct rtc_time tm;
  187. p = buf;
  188. ppc_md.get_rtc_time(&tm);
  189. /*
  190. * There is no way to tell if the luser has the RTC set for local
  191. * time or for Universal Standard Time (GMT). Probably local though.
  192. */
  193. p += sprintf(p,
  194. "rtc_time\t: %02d:%02d:%02d\n"
  195. "rtc_date\t: %04d-%02d-%02d\n"
  196. "rtc_epoch\t: %04lu\n",
  197. tm.tm_hour, tm.tm_min, tm.tm_sec,
  198. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
  199. p += sprintf(p,
  200. "DST_enable\t: no\n"
  201. "BCD\t\t: yes\n"
  202. "24hr\t\t: yes\n" );
  203. return p - buf;
  204. }
  205. static int rtc_read_proc(char *page, char **start, off_t off,
  206. int count, int *eof, void *data)
  207. {
  208. int len = rtc_proc_output (page);
  209. if (len <= off+count) *eof = 1;
  210. *start = page + off;
  211. len -= off;
  212. if (len>count) len = count;
  213. if (len<0) len = 0;
  214. return len;
  215. }
  216. #ifdef CONFIG_PPC_RTAS
  217. #define MAX_RTC_WAIT 5000 /* 5 sec */
  218. #define RTAS_CLOCK_BUSY (-2)
  219. unsigned long rtas_get_boot_time(void)
  220. {
  221. int ret[8];
  222. int error, wait_time;
  223. unsigned long max_wait_tb;
  224. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  225. do {
  226. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  227. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  228. wait_time = rtas_extended_busy_delay_time(error);
  229. /* This is boot time so we spin. */
  230. udelay(wait_time*1000);
  231. error = RTAS_CLOCK_BUSY;
  232. }
  233. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  234. if (error != 0 && printk_ratelimit()) {
  235. printk(KERN_WARNING "error: reading the clock failed (%d)\n",
  236. error);
  237. return 0;
  238. }
  239. return mktime(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]);
  240. }
  241. /* NOTE: get_rtc_time will get an error if executed in interrupt context
  242. * and if a delay is needed to read the clock. In this case we just
  243. * silently return without updating rtc_tm.
  244. */
  245. void rtas_get_rtc_time(struct rtc_time *rtc_tm)
  246. {
  247. int ret[8];
  248. int error, wait_time;
  249. unsigned long max_wait_tb;
  250. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  251. do {
  252. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  253. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  254. if (in_interrupt() && printk_ratelimit()) {
  255. printk(KERN_WARNING "error: reading clock would delay interrupt\n");
  256. return; /* delay not allowed */
  257. }
  258. wait_time = rtas_extended_busy_delay_time(error);
  259. msleep_interruptible(wait_time);
  260. error = RTAS_CLOCK_BUSY;
  261. }
  262. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  263. if (error != 0 && printk_ratelimit()) {
  264. printk(KERN_WARNING "error: reading the clock failed (%d)\n",
  265. error);
  266. return;
  267. }
  268. rtc_tm->tm_sec = ret[5];
  269. rtc_tm->tm_min = ret[4];
  270. rtc_tm->tm_hour = ret[3];
  271. rtc_tm->tm_mday = ret[2];
  272. rtc_tm->tm_mon = ret[1] - 1;
  273. rtc_tm->tm_year = ret[0] - 1900;
  274. }
  275. int rtas_set_rtc_time(struct rtc_time *tm)
  276. {
  277. int error, wait_time;
  278. unsigned long max_wait_tb;
  279. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  280. do {
  281. error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
  282. tm->tm_year + 1900, tm->tm_mon + 1,
  283. tm->tm_mday, tm->tm_hour, tm->tm_min,
  284. tm->tm_sec, 0);
  285. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  286. if (in_interrupt())
  287. return 1; /* probably decrementer */
  288. wait_time = rtas_extended_busy_delay_time(error);
  289. msleep_interruptible(wait_time);
  290. error = RTAS_CLOCK_BUSY;
  291. }
  292. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  293. if (error != 0 && printk_ratelimit())
  294. printk(KERN_WARNING "error: setting the clock failed (%d)\n",
  295. error);
  296. return 0;
  297. }
  298. #endif