rtc.c 9.5 KB

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