1 /* Copyright 2013-2014 IBM Corp.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 * implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <skiboot.h>
18 #include <opal.h>
19 #include <mem_region.h>
20 #include <device.h>
21 #include <timebase.h>
22 #include <time-utils.h>
23 #include <lock.h>
24
25 /* timebase when tm_offset was assigned */
26 static unsigned long tb_synctime;
27
28 /*
29 * Absolute time that was last assigned.
30 * Current rtc value is calculated from this.
31 */
32 static struct tm tm_offset;
33
34 /* protects tm_offset & tb_synctime */
35 static struct lock emulation_lock;
36
fake_rtc_write(uint32_t ymd,uint64_t hmsm)37 static int64_t fake_rtc_write(uint32_t ymd, uint64_t hmsm)
38 {
39
40 lock(&emulation_lock);
41
42 datetime_to_tm(ymd, hmsm, &tm_offset);
43 tb_synctime = mftb();
44
45 unlock(&emulation_lock);
46
47 return OPAL_SUCCESS;
48 }
49
fake_rtc_read(uint32_t * ymd,uint64_t * hmsm)50 static int64_t fake_rtc_read(uint32_t *ymd, uint64_t *hmsm)
51 {
52
53 time_t sec;
54 struct tm tm_calculated;
55
56 if (!ymd || !hmsm)
57 return OPAL_PARAMETER;
58
59 /* Compute the emulated clock value */
60 lock(&emulation_lock);
61
62 sec = tb_to_secs(mftb() - tb_synctime) + mktime(&tm_offset);
63 gmtime_r(&sec, &tm_calculated);
64 tm_to_datetime(&tm_calculated, ymd, hmsm);
65
66 unlock(&emulation_lock);
67
68 return OPAL_SUCCESS;
69 }
70
fake_rtc_init(void)71 void fake_rtc_init(void)
72 {
73 struct mem_region *rtc_region = NULL;
74 uint32_t *rtc = NULL, *fake_ymd;
75 uint64_t *fake_hmsm;
76 struct dt_node *np;
77
78 /* Read initial values from reserved memory */
79 rtc_region = find_mem_region("ibm,fake-rtc");
80
81 /* Should we register anyway? */
82 if (!rtc_region) {
83 prlog(PR_TRACE, "No initial RTC value found\n");
84 return;
85 }
86
87 init_lock(&emulation_lock);
88
89 /* Fetch the initial rtc values */
90 rtc = (uint32_t *) rtc_region->start;
91
92 fake_ymd = rtc;
93 fake_hmsm = ((uint64_t *) &rtc[1]);
94
95 fake_rtc_write(*fake_ymd, *fake_hmsm);
96
97 /* Register opal calls */
98 opal_register(OPAL_RTC_READ, fake_rtc_read, 2);
99 opal_register(OPAL_RTC_WRITE, fake_rtc_write, 2);
100
101 /* add the fake rtc dt node */
102 np = dt_new(opal_node, "rtc");
103 dt_add_property_strings(np, "compatible", "ibm,opal-rtc");
104
105 prlog(PR_TRACE, "Init fake RTC to Date:%d-%d-%d Time:%d-%d-%d\n",
106 tm_offset.tm_mon, tm_offset.tm_mday, tm_offset.tm_year,
107 tm_offset.tm_hour, tm_offset.tm_min, tm_offset.tm_sec);
108 }
109