rtc.c 9.4 KB

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