mb/google/brya/var/omnigul: Modify NVMe and UFS Storage support
[coreboot.git] / payloads / libpayload / libc / time.c
blob6780008d4ce234c595546b370e5de8fd72ea1e68
1 /*
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 /**
30 * @file libc/time.c
31 * General time functions
34 #define __STDC_FORMAT_MACROS
36 #include <libpayload-config.h>
37 #include <libpayload.h>
38 #if CONFIG(LP_ARCH_X86) && CONFIG(LP_NVRAM)
39 #include <arch/rdtsc.h>
40 #endif
41 #include <inttypes.h>
43 extern u32 cpu_khz;
45 static struct {
46 u64 ticks;
47 time_t secs;
48 suseconds_t usecs;
49 } clock;
51 static void update_clock(void)
53 u64 delta = timer_raw_value() - clock.ticks;
54 int secs;
55 static uint64_t ticks_per_sec = 0;
56 static uint64_t ticks_per_usec = 0;
57 if (!ticks_per_sec) {
58 ticks_per_sec = timer_hz();
59 ticks_per_usec = timer_hz() / 1000000;
62 clock.ticks += delta;
64 secs = (int) (delta / ticks_per_sec);
65 clock.secs += secs;
66 delta -= (secs * ticks_per_sec);
67 clock.usecs += (int)(delta / ticks_per_usec);
69 if (clock.usecs > 1000000) {
70 clock.usecs -= 1000000;
71 clock.secs++;
75 #if CONFIG(LP_NVRAM)
77 static unsigned int day_of_year(int mon, int day, int year)
79 static u8 mdays[12] = { 31, 28, 31, 30, 31, 30,
80 31, 31, 30, 31, 30, 31 };
82 int i, ret = 0;
84 for(i = 0; i < mon; i++) {
85 ret += mdays[i];
87 if (i == 1 && (year % 4))
88 ret++;
91 return (ret + day);
94 static void gettimeofday_init(void)
96 int days, delta;
97 struct tm tm;
99 rtc_read_clock(&tm);
100 clock.ticks = rdtsc();
102 /* Calculate the number of days in the year so far */
103 days = day_of_year(tm.tm_mon, tm.tm_mday, tm.tm_year + 1900);
105 delta = tm.tm_year - 70;
107 days += (delta * 365);
109 /* Figure leap years */
111 if (delta > 2)
112 days += (delta - 2) / 4;
114 clock.secs = (days * 86400) + (tm.tm_hour * 3600) +
115 (tm.tm_min * 60) + tm.tm_sec;
117 #else
118 static void gettimeofday_init(void)
120 /* Record the number of ticks */
121 clock.ticks = timer_raw_value();
123 #endif
126 * Return the current time expressed as seconds from 00:00:00 UTC, 1 Jan 1970.
128 * @param tp When not NULL, set this to the current time in seconds.
129 * @return The current time in seconds.
131 time_t time(time_t *tp)
134 * Call the gtod init when we need it - this keeps the code from
135 * being included in the binary if we don't need it.
137 if (!clock.ticks)
138 gettimeofday_init();
140 update_clock();
142 if (tp)
143 *tp = clock.secs;
145 return clock.secs;
149 * Return the current time broken into a timeval structure.
151 * @param tv A pointer to a timeval structure.
152 * @param tz Added for compatibility - not used.
153 * @return 0 for success (this function cannot return non-zero currently).
155 int gettimeofday(struct timeval *tv, void *tz)
157 tv->tv_sec = time(NULL);
158 tv->tv_usec = clock.usecs;
160 return 0;
163 __attribute__((weak))
164 void arch_ndelay(uint64_t ns)
166 uint64_t delta = ns * timer_hz() / NSECS_PER_SEC;
167 uint64_t start = timer_raw_value();
168 while (timer_raw_value() - start < delta) ;
171 u64 timer_us(u64 base)
173 static u64 hz;
175 // Only check timer_hz once. Assume it doesn't change.
176 if (hz == 0) {
177 hz = timer_hz();
178 if (hz < 1000000) {
179 printf("Timer frequency %" PRIu64 " is too low, "
180 "must be at least 1MHz.\n", hz);
181 halt();
185 return (1000000 * timer_raw_value()) / hz - base;