rtc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/iSeries/mf.h>
  43. #include <asm/machdep.h>
  44. extern int piranha_simulator;
  45. /*
  46. * We sponge a minor off of the misc major. No need slurping
  47. * up another valuable major dev number for this. If you add
  48. * an ioctl, make sure you don't conflict with SPARC's RTC
  49. * ioctls.
  50. */
  51. static ssize_t rtc_read(struct file *file, char __user *buf,
  52. size_t count, loff_t *ppos);
  53. static int rtc_ioctl(struct inode *inode, struct file *file,
  54. unsigned int cmd, unsigned long arg);
  55. static int rtc_read_proc(char *page, char **start, off_t off,
  56. int count, int *eof, void *data);
  57. /*
  58. * If this driver ever becomes modularised, it will be really nice
  59. * to make the epoch retain its value across module reload...
  60. */
  61. static unsigned long epoch = 1900; /* year corresponding to 0x00 */
  62. static const unsigned char days_in_mo[] =
  63. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  64. /*
  65. * Now all the various file operations that we export.
  66. */
  67. static ssize_t rtc_read(struct file *file, char __user *buf,
  68. size_t count, loff_t *ppos)
  69. {
  70. return -EIO;
  71. }
  72. static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  73. unsigned long arg)
  74. {
  75. struct rtc_time wtime;
  76. switch (cmd) {
  77. case RTC_RD_TIME: /* Read the time/date from RTC */
  78. {
  79. memset(&wtime, 0, sizeof(struct rtc_time));
  80. ppc_md.get_rtc_time(&wtime);
  81. break;
  82. }
  83. case RTC_SET_TIME: /* Set the RTC */
  84. {
  85. struct rtc_time rtc_tm;
  86. unsigned char mon, day, hrs, min, sec, leap_yr;
  87. unsigned int yrs;
  88. if (!capable(CAP_SYS_TIME))
  89. return -EACCES;
  90. if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
  91. sizeof(struct rtc_time)))
  92. return -EFAULT;
  93. yrs = rtc_tm.tm_year;
  94. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  95. day = rtc_tm.tm_mday;
  96. hrs = rtc_tm.tm_hour;
  97. min = rtc_tm.tm_min;
  98. sec = rtc_tm.tm_sec;
  99. if (yrs < 70)
  100. return -EINVAL;
  101. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  102. if ((mon > 12) || (day == 0))
  103. return -EINVAL;
  104. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  105. return -EINVAL;
  106. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  107. return -EINVAL;
  108. if ( yrs > 169 )
  109. return -EINVAL;
  110. ppc_md.set_rtc_time(&rtc_tm);
  111. return 0;
  112. }
  113. case RTC_EPOCH_READ: /* Read the epoch. */
  114. {
  115. return put_user (epoch, (unsigned long __user *)arg);
  116. }
  117. case RTC_EPOCH_SET: /* Set the epoch. */
  118. {
  119. /*
  120. * There were no RTC clocks before 1900.
  121. */
  122. if (arg < 1900)
  123. return -EINVAL;
  124. if (!capable(CAP_SYS_TIME))
  125. return -EACCES;
  126. epoch = arg;
  127. return 0;
  128. }
  129. default:
  130. return -EINVAL;
  131. }
  132. return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  133. }
  134. static int rtc_open(struct inode *inode, struct file *file)
  135. {
  136. nonseekable_open(inode, file);
  137. return 0;
  138. }
  139. static int rtc_release(struct inode *inode, struct file *file)
  140. {
  141. return 0;
  142. }
  143. /*
  144. * The various file operations we support.
  145. */
  146. static struct file_operations rtc_fops = {
  147. .owner = THIS_MODULE,
  148. .llseek = no_llseek,
  149. .read = rtc_read,
  150. .ioctl = rtc_ioctl,
  151. .open = rtc_open,
  152. .release = rtc_release,
  153. };
  154. static struct miscdevice rtc_dev = {
  155. .minor = RTC_MINOR,
  156. .name = "rtc",
  157. .fops = &rtc_fops
  158. };
  159. static int __init rtc_init(void)
  160. {
  161. int retval;
  162. retval = misc_register(&rtc_dev);
  163. if(retval < 0)
  164. return retval;
  165. #ifdef CONFIG_PROC_FS
  166. if (create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL)
  167. == NULL) {
  168. misc_deregister(&rtc_dev);
  169. return -ENOMEM;
  170. }
  171. #endif
  172. printk(KERN_INFO "i/pSeries Real Time Clock Driver v" RTC_VERSION "\n");
  173. return 0;
  174. }
  175. static void __exit rtc_exit (void)
  176. {
  177. remove_proc_entry ("driver/rtc", NULL);
  178. misc_deregister(&rtc_dev);
  179. }
  180. module_init(rtc_init);
  181. module_exit(rtc_exit);
  182. /*
  183. * Info exported via "/proc/driver/rtc".
  184. */
  185. static int rtc_proc_output (char *buf)
  186. {
  187. char *p;
  188. struct rtc_time tm;
  189. p = buf;
  190. ppc_md.get_rtc_time(&tm);
  191. /*
  192. * There is no way to tell if the luser has the RTC set for local
  193. * time or for Universal Standard Time (GMT). Probably local though.
  194. */
  195. p += sprintf(p,
  196. "rtc_time\t: %02d:%02d:%02d\n"
  197. "rtc_date\t: %04d-%02d-%02d\n"
  198. "rtc_epoch\t: %04lu\n",
  199. tm.tm_hour, tm.tm_min, tm.tm_sec,
  200. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
  201. p += sprintf(p,
  202. "DST_enable\t: no\n"
  203. "BCD\t\t: yes\n"
  204. "24hr\t\t: yes\n" );
  205. return p - buf;
  206. }
  207. static int rtc_read_proc(char *page, char **start, off_t off,
  208. int count, int *eof, void *data)
  209. {
  210. int len = rtc_proc_output (page);
  211. if (len <= off+count) *eof = 1;
  212. *start = page + off;
  213. len -= off;
  214. if (len>count) len = count;
  215. if (len<0) len = 0;
  216. return len;
  217. }
  218. #ifdef CONFIG_PPC_ISERIES
  219. /*
  220. * Get the RTC from the virtual service processor
  221. * This requires flowing LpEvents to the primary partition
  222. */
  223. void iSeries_get_rtc_time(struct rtc_time *rtc_tm)
  224. {
  225. if (piranha_simulator)
  226. return;
  227. mf_get_rtc(rtc_tm);
  228. rtc_tm->tm_mon--;
  229. }
  230. /*
  231. * Set the RTC in the virtual service processor
  232. * This requires flowing LpEvents to the primary partition
  233. */
  234. int iSeries_set_rtc_time(struct rtc_time *tm)
  235. {
  236. mf_set_rtc(tm);
  237. return 0;
  238. }
  239. void iSeries_get_boot_time(struct rtc_time *tm)
  240. {
  241. if ( piranha_simulator )
  242. return;
  243. mf_get_boot_rtc(tm);
  244. tm->tm_mon -= 1;
  245. }
  246. #endif
  247. #ifdef CONFIG_PPC_RTAS
  248. #define MAX_RTC_WAIT 5000 /* 5 sec */
  249. #define RTAS_CLOCK_BUSY (-2)
  250. void rtas_get_boot_time(struct rtc_time *rtc_tm)
  251. {
  252. int ret[8];
  253. int error, wait_time;
  254. unsigned long max_wait_tb;
  255. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  256. do {
  257. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  258. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  259. wait_time = rtas_extended_busy_delay_time(error);
  260. /* This is boot time so we spin. */
  261. udelay(wait_time*1000);
  262. error = RTAS_CLOCK_BUSY;
  263. }
  264. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  265. if (error != 0 && printk_ratelimit()) {
  266. printk(KERN_WARNING "error: reading the clock failed (%d)\n",
  267. error);
  268. return;
  269. }
  270. rtc_tm->tm_sec = ret[5];
  271. rtc_tm->tm_min = ret[4];
  272. rtc_tm->tm_hour = ret[3];
  273. rtc_tm->tm_mday = ret[2];
  274. rtc_tm->tm_mon = ret[1] - 1;
  275. rtc_tm->tm_year = ret[0] - 1900;
  276. }
  277. /* NOTE: get_rtc_time will get an error if executed in interrupt context
  278. * and if a delay is needed to read the clock. In this case we just
  279. * silently return without updating rtc_tm.
  280. */
  281. void rtas_get_rtc_time(struct rtc_time *rtc_tm)
  282. {
  283. int ret[8];
  284. int error, wait_time;
  285. unsigned long max_wait_tb;
  286. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  287. do {
  288. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  289. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  290. if (in_interrupt() && printk_ratelimit()) {
  291. printk(KERN_WARNING "error: reading clock would delay interrupt\n");
  292. return; /* delay not allowed */
  293. }
  294. wait_time = rtas_extended_busy_delay_time(error);
  295. msleep_interruptible(wait_time);
  296. error = RTAS_CLOCK_BUSY;
  297. }
  298. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  299. if (error != 0 && printk_ratelimit()) {
  300. printk(KERN_WARNING "error: reading the clock failed (%d)\n",
  301. error);
  302. return;
  303. }
  304. rtc_tm->tm_sec = ret[5];
  305. rtc_tm->tm_min = ret[4];
  306. rtc_tm->tm_hour = ret[3];
  307. rtc_tm->tm_mday = ret[2];
  308. rtc_tm->tm_mon = ret[1] - 1;
  309. rtc_tm->tm_year = ret[0] - 1900;
  310. }
  311. int rtas_set_rtc_time(struct rtc_time *tm)
  312. {
  313. int error, wait_time;
  314. unsigned long max_wait_tb;
  315. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  316. do {
  317. error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
  318. tm->tm_year + 1900, tm->tm_mon + 1,
  319. tm->tm_mday, tm->tm_hour, tm->tm_min,
  320. tm->tm_sec, 0);
  321. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  322. if (in_interrupt())
  323. return 1; /* probably decrementer */
  324. wait_time = rtas_extended_busy_delay_time(error);
  325. msleep_interruptible(wait_time);
  326. error = RTAS_CLOCK_BUSY;
  327. }
  328. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  329. if (error != 0 && printk_ratelimit())
  330. printk(KERN_WARNING "error: setting the clock failed (%d)\n",
  331. error);
  332. return 0;
  333. }
  334. #endif