1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/time.h>
4 #include <linux/timer.h>
5 #include <linux/init.h>
7 #include <linux/delay.h>
8 #include <linux/ratelimit.h>
14 #define MAX_RTC_WAIT 5000 /* 5 sec */
15 #define RTAS_CLOCK_BUSY (-2)
16 time64_t __init
rtas_get_boot_time(void)
20 unsigned int wait_time
;
23 max_wait_tb
= get_tb() + tb_ticks_per_usec
* 1000 * MAX_RTC_WAIT
;
25 error
= rtas_call(rtas_token("get-time-of-day"), 0, 8, ret
);
27 wait_time
= rtas_busy_delay_time(error
);
29 /* This is boot time so we spin. */
30 udelay(wait_time
*1000);
32 } while (wait_time
&& (get_tb() < max_wait_tb
));
35 printk_ratelimited(KERN_WARNING
36 "error: reading the clock failed (%d)\n",
41 return mktime64(ret
[0], ret
[1], ret
[2], ret
[3], ret
[4], ret
[5]);
44 /* NOTE: get_rtc_time will get an error if executed in interrupt context
45 * and if a delay is needed to read the clock. In this case we just
46 * silently return without updating rtc_tm.
48 void rtas_get_rtc_time(struct rtc_time
*rtc_tm
)
52 unsigned int wait_time
;
55 max_wait_tb
= get_tb() + tb_ticks_per_usec
* 1000 * MAX_RTC_WAIT
;
57 error
= rtas_call(rtas_token("get-time-of-day"), 0, 8, ret
);
59 wait_time
= rtas_busy_delay_time(error
);
62 memset(rtc_tm
, 0, sizeof(struct rtc_time
));
63 printk_ratelimited(KERN_WARNING
64 "error: reading clock "
65 "would delay interrupt\n");
66 return; /* delay not allowed */
70 } while (wait_time
&& (get_tb() < max_wait_tb
));
73 printk_ratelimited(KERN_WARNING
74 "error: reading the clock failed (%d)\n",
79 rtc_tm
->tm_sec
= ret
[5];
80 rtc_tm
->tm_min
= ret
[4];
81 rtc_tm
->tm_hour
= ret
[3];
82 rtc_tm
->tm_mday
= ret
[2];
83 rtc_tm
->tm_mon
= ret
[1] - 1;
84 rtc_tm
->tm_year
= ret
[0] - 1900;
87 int rtas_set_rtc_time(struct rtc_time
*tm
)
92 max_wait_tb
= get_tb() + tb_ticks_per_usec
* 1000 * MAX_RTC_WAIT
;
94 error
= rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL
,
95 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1,
96 tm
->tm_mday
, tm
->tm_hour
, tm
->tm_min
,
99 wait_time
= rtas_busy_delay_time(error
);
102 return 1; /* probably decrementer */
105 } while (wait_time
&& (get_tb() < max_wait_tb
));
108 printk_ratelimited(KERN_WARNING
109 "error: setting the clock failed (%d)\n",