| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 1 | #include <linux/kernel.h> | 
|  | 2 | #include <linux/time.h> | 
|  | 3 | #include <linux/timer.h> | 
|  | 4 | #include <linux/init.h> | 
|  | 5 | #include <linux/rtc.h> | 
|  | 6 | #include <linux/delay.h> | 
| Christian Dietrich | 9a8f99f | 2011-06-04 05:35:47 +0000 | [diff] [blame] | 7 | #include <linux/ratelimit.h> | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 8 | #include <asm/prom.h> | 
|  | 9 | #include <asm/rtas.h> | 
|  | 10 | #include <asm/time.h> | 
|  | 11 |  | 
|  | 12 |  | 
|  | 13 | #define MAX_RTC_WAIT 5000	/* 5 sec */ | 
|  | 14 | #define RTAS_CLOCK_BUSY (-2) | 
|  | 15 | unsigned long __init rtas_get_boot_time(void) | 
|  | 16 | { | 
|  | 17 | int ret[8]; | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 18 | int error; | 
|  | 19 | unsigned int wait_time; | 
| Paul Mackerras | 49e16b7 | 2005-11-18 15:52:38 +1100 | [diff] [blame] | 20 | u64 max_wait_tb; | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 21 |  | 
|  | 22 | max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT; | 
|  | 23 | do { | 
|  | 24 | error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret); | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 25 |  | 
|  | 26 | wait_time = rtas_busy_delay_time(error); | 
|  | 27 | if (wait_time) { | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 28 | /* This is boot time so we spin. */ | 
|  | 29 | udelay(wait_time*1000); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 30 | } | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 31 | } while (wait_time && (get_tb() < max_wait_tb)); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 32 |  | 
| Christian Dietrich | 9a8f99f | 2011-06-04 05:35:47 +0000 | [diff] [blame] | 33 | if (error != 0) { | 
|  | 34 | printk_ratelimited(KERN_WARNING | 
|  | 35 | "error: reading the clock failed (%d)\n", | 
|  | 36 | error); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 37 | return 0; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | return mktime(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]); | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | /* NOTE: get_rtc_time will get an error if executed in interrupt context | 
|  | 44 | * and if a delay is needed to read the clock.  In this case we just | 
|  | 45 | * silently return without updating rtc_tm. | 
|  | 46 | */ | 
|  | 47 | void rtas_get_rtc_time(struct rtc_time *rtc_tm) | 
|  | 48 | { | 
|  | 49 | int ret[8]; | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 50 | int error; | 
|  | 51 | unsigned int wait_time; | 
| Paul Mackerras | 49e16b7 | 2005-11-18 15:52:38 +1100 | [diff] [blame] | 52 | u64 max_wait_tb; | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 53 |  | 
|  | 54 | max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT; | 
|  | 55 | do { | 
|  | 56 | error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret); | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 57 |  | 
|  | 58 | wait_time = rtas_busy_delay_time(error); | 
|  | 59 | if (wait_time) { | 
| Christian Dietrich | 9a8f99f | 2011-06-04 05:35:47 +0000 | [diff] [blame] | 60 | if (in_interrupt()) { | 
| Michael Neuling | 0e8ed47 | 2006-03-14 17:11:51 +1100 | [diff] [blame] | 61 | memset(rtc_tm, 0, sizeof(struct rtc_time)); | 
| Christian Dietrich | 9a8f99f | 2011-06-04 05:35:47 +0000 | [diff] [blame] | 62 | printk_ratelimited(KERN_WARNING | 
|  | 63 | "error: reading clock " | 
|  | 64 | "would delay interrupt\n"); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 65 | return;	/* delay not allowed */ | 
|  | 66 | } | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 67 | msleep(wait_time); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 68 | } | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 69 | } while (wait_time && (get_tb() < max_wait_tb)); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 70 |  | 
| Christian Dietrich | 9a8f99f | 2011-06-04 05:35:47 +0000 | [diff] [blame] | 71 | if (error != 0) { | 
|  | 72 | printk_ratelimited(KERN_WARNING | 
|  | 73 | "error: reading the clock failed (%d)\n", | 
|  | 74 | error); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 75 | return; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | rtc_tm->tm_sec = ret[5]; | 
|  | 79 | rtc_tm->tm_min = ret[4]; | 
|  | 80 | rtc_tm->tm_hour = ret[3]; | 
|  | 81 | rtc_tm->tm_mday = ret[2]; | 
|  | 82 | rtc_tm->tm_mon = ret[1] - 1; | 
|  | 83 | rtc_tm->tm_year = ret[0] - 1900; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | int rtas_set_rtc_time(struct rtc_time *tm) | 
|  | 87 | { | 
|  | 88 | int error, wait_time; | 
| Paul Mackerras | 49e16b7 | 2005-11-18 15:52:38 +1100 | [diff] [blame] | 89 | u64 max_wait_tb; | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 90 |  | 
|  | 91 | max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT; | 
|  | 92 | do { | 
|  | 93 | error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL, | 
|  | 94 | tm->tm_year + 1900, tm->tm_mon + 1, | 
|  | 95 | tm->tm_mday, tm->tm_hour, tm->tm_min, | 
|  | 96 | tm->tm_sec, 0); | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 97 |  | 
|  | 98 | wait_time = rtas_busy_delay_time(error); | 
|  | 99 | if (wait_time) { | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 100 | if (in_interrupt()) | 
|  | 101 | return 1;	/* probably decrementer */ | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 102 | msleep(wait_time); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 103 | } | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 104 | } while (wait_time && (get_tb() < max_wait_tb)); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 105 |  | 
| Christian Dietrich | 9a8f99f | 2011-06-04 05:35:47 +0000 | [diff] [blame] | 106 | if (error != 0) | 
|  | 107 | printk_ratelimited(KERN_WARNING | 
|  | 108 | "error: setting the clock failed (%d)\n", | 
|  | 109 | error); | 
| Benjamin Herrenschmidt | 3136254 | 2005-11-14 15:49:48 +1100 | [diff] [blame] | 110 |  | 
|  | 111 | return 0; | 
|  | 112 | } |