blob: a28239b8b0c0bc4eb11b87d502edc8b8294d724d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +11002#include <linux/kernel.h>
3#include <linux/time.h>
4#include <linux/timer.h>
5#include <linux/init.h>
6#include <linux/rtc.h>
7#include <linux/delay.h>
Christian Dietrich9a8f99f2011-06-04 05:35:47 +00008#include <linux/ratelimit.h>
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +11009#include <asm/prom.h>
10#include <asm/rtas.h>
11#include <asm/time.h>
12
13
14#define MAX_RTC_WAIT 5000 /* 5 sec */
15#define RTAS_CLOCK_BUSY (-2)
Arnd Bergmann5bfd6432018-04-23 10:36:40 +020016time64_t __init rtas_get_boot_time(void)
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110017{
18 int ret[8];
John Rose507279d2006-06-05 16:31:48 -050019 int error;
20 unsigned int wait_time;
Paul Mackerras49e16b72005-11-18 15:52:38 +110021 u64 max_wait_tb;
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110022
23 max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
24 do {
25 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
John Rose507279d2006-06-05 16:31:48 -050026
27 wait_time = rtas_busy_delay_time(error);
28 if (wait_time) {
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110029 /* This is boot time so we spin. */
30 udelay(wait_time*1000);
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110031 }
John Rose507279d2006-06-05 16:31:48 -050032 } while (wait_time && (get_tb() < max_wait_tb));
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110033
Christian Dietrich9a8f99f2011-06-04 05:35:47 +000034 if (error != 0) {
35 printk_ratelimited(KERN_WARNING
36 "error: reading the clock failed (%d)\n",
37 error);
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110038 return 0;
39 }
40
Arnd Bergmann5bfd6432018-04-23 10:36:40 +020041 return mktime64(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]);
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110042}
43
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.
47 */
48void rtas_get_rtc_time(struct rtc_time *rtc_tm)
49{
50 int ret[8];
John Rose507279d2006-06-05 16:31:48 -050051 int error;
52 unsigned int wait_time;
Paul Mackerras49e16b72005-11-18 15:52:38 +110053 u64 max_wait_tb;
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110054
55 max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
56 do {
57 error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
John Rose507279d2006-06-05 16:31:48 -050058
59 wait_time = rtas_busy_delay_time(error);
60 if (wait_time) {
Christian Dietrich9a8f99f2011-06-04 05:35:47 +000061 if (in_interrupt()) {
Michael Neuling0e8ed472006-03-14 17:11:51 +110062 memset(rtc_tm, 0, sizeof(struct rtc_time));
Christian Dietrich9a8f99f2011-06-04 05:35:47 +000063 printk_ratelimited(KERN_WARNING
64 "error: reading clock "
65 "would delay interrupt\n");
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110066 return; /* delay not allowed */
67 }
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110068 msleep(wait_time);
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110069 }
John Rose507279d2006-06-05 16:31:48 -050070 } while (wait_time && (get_tb() < max_wait_tb));
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110071
Christian Dietrich9a8f99f2011-06-04 05:35:47 +000072 if (error != 0) {
73 printk_ratelimited(KERN_WARNING
74 "error: reading the clock failed (%d)\n",
75 error);
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110076 return;
77 }
78
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;
85}
86
87int rtas_set_rtc_time(struct rtc_time *tm)
88{
89 int error, wait_time;
Paul Mackerras49e16b72005-11-18 15:52:38 +110090 u64 max_wait_tb;
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +110091
92 max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
93 do {
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,
97 tm->tm_sec, 0);
John Rose507279d2006-06-05 16:31:48 -050098
99 wait_time = rtas_busy_delay_time(error);
100 if (wait_time) {
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +1100101 if (in_interrupt())
102 return 1; /* probably decrementer */
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +1100103 msleep(wait_time);
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +1100104 }
John Rose507279d2006-06-05 16:31:48 -0500105 } while (wait_time && (get_tb() < max_wait_tb));
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +1100106
Christian Dietrich9a8f99f2011-06-04 05:35:47 +0000107 if (error != 0)
108 printk_ratelimited(KERN_WARNING
109 "error: setting the clock failed (%d)\n",
110 error);
Benjamin Herrenschmidt31362542005-11-14 15:49:48 +1100111
112 return 0;
113}