/arch/ppc64/kernel/rtc.c

https://bitbucket.org/evzijst/gittest · C · 440 lines · 305 code · 71 blank · 64 comment · 57 complexity · 77b7782d1d310f8bcab4e351b6ac8dfa MD5 · raw file

  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. unsigned long time;
  243. static unsigned long lastsec = 1;
  244. u32 dataWord1 = *((u32 *)(&xSpCommArea.xBcdTimeAtIplStart));
  245. u32 dataWord2 = *(((u32 *)&(xSpCommArea.xBcdTimeAtIplStart)) + 1);
  246. int year = 1970;
  247. int year1 = ( dataWord1 >> 24 ) & 0x000000FF;
  248. int year2 = ( dataWord1 >> 16 ) & 0x000000FF;
  249. int sec = ( dataWord1 >> 8 ) & 0x000000FF;
  250. int min = dataWord1 & 0x000000FF;
  251. int hour = ( dataWord2 >> 24 ) & 0x000000FF;
  252. int day = ( dataWord2 >> 8 ) & 0x000000FF;
  253. int mon = dataWord2 & 0x000000FF;
  254. if ( piranha_simulator )
  255. return;
  256. BCD_TO_BIN(sec);
  257. BCD_TO_BIN(min);
  258. BCD_TO_BIN(hour);
  259. BCD_TO_BIN(day);
  260. BCD_TO_BIN(mon);
  261. BCD_TO_BIN(year1);
  262. BCD_TO_BIN(year2);
  263. year = year1 * 100 + year2;
  264. time = mktime(year, mon, day, hour, min, sec);
  265. time += ( jiffies / HZ );
  266. /* Now THIS is a nasty hack!
  267. * It ensures that the first two calls get different answers.
  268. * That way the loop in init_time (time.c) will not think
  269. * the clock is stuck.
  270. */
  271. if ( lastsec ) {
  272. time -= lastsec;
  273. --lastsec;
  274. }
  275. to_tm(time, tm);
  276. tm->tm_year -= 1900;
  277. tm->tm_mon -= 1;
  278. }
  279. #endif
  280. #ifdef CONFIG_PPC_RTAS
  281. #define MAX_RTC_WAIT 5000 /* 5 sec */
  282. #define RTAS_CLOCK_BUSY (-2)
  283. void pSeries_get_boot_time(struct rtc_time *rtc_tm)
  284. {
  285. int ret[8];
  286. int error, wait_time;
  287. unsigned long max_wait_tb;
  288. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  289. do {
  290. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  291. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  292. wait_time = rtas_extended_busy_delay_time(error);
  293. /* This is boot time so we spin. */
  294. udelay(wait_time*1000);
  295. error = RTAS_CLOCK_BUSY;
  296. }
  297. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  298. if (error != 0 && printk_ratelimit()) {
  299. printk(KERN_WARNING "error: reading the clock failed (%d)\n",
  300. error);
  301. return;
  302. }
  303. rtc_tm->tm_sec = ret[5];
  304. rtc_tm->tm_min = ret[4];
  305. rtc_tm->tm_hour = ret[3];
  306. rtc_tm->tm_mday = ret[2];
  307. rtc_tm->tm_mon = ret[1] - 1;
  308. rtc_tm->tm_year = ret[0] - 1900;
  309. }
  310. /* NOTE: get_rtc_time will get an error if executed in interrupt context
  311. * and if a delay is needed to read the clock. In this case we just
  312. * silently return without updating rtc_tm.
  313. */
  314. void pSeries_get_rtc_time(struct rtc_time *rtc_tm)
  315. {
  316. int ret[8];
  317. int error, wait_time;
  318. unsigned long max_wait_tb;
  319. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  320. do {
  321. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  322. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  323. if (in_interrupt() && printk_ratelimit()) {
  324. printk(KERN_WARNING "error: reading clock would delay interrupt\n");
  325. return; /* delay not allowed */
  326. }
  327. wait_time = rtas_extended_busy_delay_time(error);
  328. set_current_state(TASK_INTERRUPTIBLE);
  329. schedule_timeout(wait_time);
  330. error = RTAS_CLOCK_BUSY;
  331. }
  332. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  333. if (error != 0 && printk_ratelimit()) {
  334. printk(KERN_WARNING "error: reading the clock failed (%d)\n",
  335. error);
  336. return;
  337. }
  338. rtc_tm->tm_sec = ret[5];
  339. rtc_tm->tm_min = ret[4];
  340. rtc_tm->tm_hour = ret[3];
  341. rtc_tm->tm_mday = ret[2];
  342. rtc_tm->tm_mon = ret[1] - 1;
  343. rtc_tm->tm_year = ret[0] - 1900;
  344. }
  345. int pSeries_set_rtc_time(struct rtc_time *tm)
  346. {
  347. int error, wait_time;
  348. unsigned long max_wait_tb;
  349. max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  350. do {
  351. error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
  352. tm->tm_year + 1900, tm->tm_mon + 1,
  353. tm->tm_mday, tm->tm_hour, tm->tm_min,
  354. tm->tm_sec, 0);
  355. if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
  356. if (in_interrupt())
  357. return 1; /* probably decrementer */
  358. wait_time = rtas_extended_busy_delay_time(error);
  359. set_current_state(TASK_INTERRUPTIBLE);
  360. schedule_timeout(wait_time);
  361. error = RTAS_CLOCK_BUSY;
  362. }
  363. } while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
  364. if (error != 0 && printk_ratelimit())
  365. printk(KERN_WARNING "error: setting the clock failed (%d)\n",
  366. error);
  367. return 0;
  368. }
  369. #endif