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