| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#define __STDC_FORMAT_MACROS |
| 35 |
|
| 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 <commonlib/bsd/gcd.h> |
| 42 |
#include <inttypes.h> |
| 43 |
|
| 44 |
extern u32 cpu_khz; |
| 45 |
|
| 46 |
static struct { |
| 47 |
u64 ticks; |
| 48 |
time_t secs; |
| 49 |
suseconds_t usecs; |
| 50 |
} clock; |
| 51 |
|
| 52 |
static void update_clock(void) |
| 53 |
{ |
| 54 |
u64 delta = timer_raw_value() - clock.ticks; |
| 55 |
int secs; |
| 56 |
static uint64_t ticks_per_sec = 0; |
| 57 |
static uint64_t ticks_per_usec = 0; |
| 58 |
if (!ticks_per_sec) { |
| 59 |
ticks_per_sec = timer_hz(); |
| 60 |
ticks_per_usec = timer_hz() / 1000000; |
| 61 |
} |
| 62 |
|
| 63 |
clock.ticks += delta; |
| 64 |
|
| 65 |
secs = (int) (delta / ticks_per_sec); |
| 66 |
clock.secs += secs; |
| 67 |
delta -= (secs * ticks_per_sec); |
| 68 |
clock.usecs += (int)(delta / ticks_per_usec); |
| 69 |
|
| 70 |
if (clock.usecs > 1000000) { |
| 71 |
clock.usecs -= 1000000; |
| 72 |
clock.secs++; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#if CONFIG(LP_NVRAM) |
| 77 |
|
| 78 |
static unsigned int day_of_year(int mon, int day, int year) |
| 79 |
{ |
| 80 |
static u8 mdays[12] = { 31, 28, 31, 30, 31, 30, |
| 81 |
31, 31, 30, 31, 30, 31 }; |
| 82 |
|
| 83 |
int i, ret = 0; |
| 84 |
|
| 85 |
for(i = 0; i < mon; i++) { |
| 86 |
ret += mdays[i]; |
| 87 |
|
| 88 |
if (i == 1 && (year % 4)) |
| 89 |
ret++; |
| 90 |
} |
| 91 |
|
| 92 |
return (ret + day); |
| 93 |
} |
| 94 |
|
| 95 |
static void gettimeofday_init(void) |
| 96 |
{ |
| 97 |
int days, delta; |
| 98 |
struct tm tm; |
| 99 |
|
| 100 |
rtc_read_clock(&tm); |
| 101 |
clock.ticks = rdtsc(); |
| 102 |
|
| 103 |
|
| 104 |
days = day_of_year(tm.tm_mon, tm.tm_mday, tm.tm_year + 1900); |
| 105 |
|
| 106 |
delta = tm.tm_year - 70; |
| 107 |
|
| 108 |
days += (delta * 365); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if (delta > 2) |
| 113 |
days += (delta - 2) / 4; |
| 114 |
|
| 115 |
clock.secs = (days * 86400) + (tm.tm_hour * 3600) + |
| 116 |
(tm.tm_min * 60) + tm.tm_sec; |
| 117 |
} |
| 118 |
#else |
| 119 |
static void gettimeofday_init(void) |
| 120 |
{ |
| 121 |
|
| 122 |
clock.ticks = timer_raw_value(); |
| 123 |
} |
| 124 |
#endif |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
time_t time(time_t *tp) |
| 133 |
{ |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if (!clock.ticks) |
| 139 |
gettimeofday_init(); |
| 140 |
|
| 141 |
update_clock(); |
| 142 |
|
| 143 |
if (tp) |
| 144 |
*tp = clock.secs; |
| 145 |
|
| 146 |
return clock.secs; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
int gettimeofday(struct timeval *tv, void *tz) |
| 157 |
{ |
| 158 |
tv->tv_sec = time(NULL); |
| 159 |
tv->tv_usec = clock.usecs; |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
#if !CONFIG(LP_ARCH_HAS_NDELAY) |
| 165 |
void ndelay(uint64_t ns) |
| 166 |
{ |
| 167 |
uint64_t delta = ns * timer_hz() / NSECS_PER_SEC; |
| 168 |
uint64_t start = timer_raw_value(); |
| 169 |
while (timer_raw_value() - start < delta) ; |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
u64 timer_us(u64 base) |
| 174 |
{ |
| 175 |
static u64 hz; |
| 176 |
static u32 mult = USECS_PER_SEC; |
| 177 |
u32 div; |
| 178 |
|
| 179 |
|
| 180 |
if (hz == 0) { |
| 181 |
hz = timer_hz(); |
| 182 |
if (hz < mult) { |
| 183 |
printf("Timer frequency %" PRIu64 " is too low, " |
| 184 |
"must be at least 1MHz.\n", hz); |
| 185 |
halt(); |
| 186 |
} |
| 187 |
div = gcd(hz, mult); |
| 188 |
hz /= div; |
| 189 |
mult /= div; |
| 190 |
} |
| 191 |
|
| 192 |
return (mult * timer_raw_value()) / hz - base; |
| 193 |
} |