1 #include <linux/kernel.h>
2 #include <linux/time.h>
3 #include <linux/timer.h>
4 #include <linux/init.h>
6 #include <linux/delay.h>
7 #include <linux/ratelimit.h>
13 #define MAX_RTC_WAIT 5000 /* 5 sec */
14 #define RTAS_CLOCK_BUSY (-2)
15 unsigned long __init rtas_get_boot_time(void)
19 unsigned int wait_time;
22 max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
24 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
26 wait_time = rtas_busy_delay_time(error);
28 /* This is boot time so we spin. */
29 udelay(wait_time*1000);
31 } while (wait_time && (get_tb() < max_wait_tb));
34 printk_ratelimited(KERN_WARNING
35 "error: reading the clock failed (%d)\n",
40 return mktime(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]);
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.
47 void rtas_get_rtc_time(struct rtc_time *rtc_tm)
51 unsigned int wait_time;
54 max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
56 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
58 wait_time = rtas_busy_delay_time(error);
61 memset(rtc_tm, 0, sizeof(struct rtc_time));
62 printk_ratelimited(KERN_WARNING
63 "error: reading clock "
64 "would delay interrupt\n");
65 return; /* delay not allowed */
69 } while (wait_time && (get_tb() < max_wait_tb));
72 printk_ratelimited(KERN_WARNING
73 "error: reading the clock failed (%d)\n",
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;
86 int rtas_set_rtc_time(struct rtc_time *tm)
91 max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
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,
98 wait_time = rtas_busy_delay_time(error);
101 return 1; /* probably decrementer */
104 } while (wait_time && (get_tb() < max_wait_tb));
107 printk_ratelimited(KERN_WARNING
108 "error: setting the clock failed (%d)\n",