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