1433d6423SLionel Sambuc /* Some utility functions around the free running clock on ARM. The clock is
2433d6423SLionel Sambuc * 32-bits wide, but we provide 64-bit wrapper functions to make it look
3433d6423SLionel Sambuc * similar to the read_tsc functions. On hardware we could actually make use
4433d6423SLionel Sambuc * of the timer overflow counter, but emulator doesn't emulate it. */
5433d6423SLionel Sambuc
6433d6423SLionel Sambuc #include <minix/minlib.h>
7433d6423SLionel Sambuc #include <minix/sysutil.h>
8433d6423SLionel Sambuc #include <sys/errno.h>
9433d6423SLionel Sambuc #include <sys/types.h>
10594df55eSDavid van Moolenbroek #include <lib.h>
11433d6423SLionel Sambuc #include <assert.h>
12433d6423SLionel Sambuc
13433d6423SLionel Sambuc #define MICROHZ 1000000ULL /* number of micros per second */
14433d6423SLionel Sambuc #define MICROSPERTICK(h) (MICROHZ/(h)) /* number of micros per HZ tick */
15433d6423SLionel Sambuc
16433d6423SLionel Sambuc static u64_t Hz;
17433d6423SLionel Sambuc
18433d6423SLionel Sambuc int
micro_delay(u32_t micros)19433d6423SLionel Sambuc micro_delay(u32_t micros)
20433d6423SLionel Sambuc {
21594df55eSDavid van Moolenbroek struct minix_kerninfo *minix_kerninfo;
22433d6423SLionel Sambuc u64_t start, delta, delta_end;
23433d6423SLionel Sambuc
24433d6423SLionel Sambuc Hz = sys_hz();
25594df55eSDavid van Moolenbroek minix_kerninfo = get_minix_kerninfo();
26433d6423SLionel Sambuc
27433d6423SLionel Sambuc /* Start of delay. */
28433d6423SLionel Sambuc read_frclock_64(&start);
29*26f5c8f8SDavid van Moolenbroek assert(minix_kerninfo->arm_frclock);
30*26f5c8f8SDavid van Moolenbroek assert(minix_kerninfo->arm_frclock->hz);
31*26f5c8f8SDavid van Moolenbroek delta_end = (minix_kerninfo->arm_frclock->hz * micros) / MICROHZ;
32433d6423SLionel Sambuc
33433d6423SLionel Sambuc /* If we have to wait for at least one HZ tick, use the regular
34433d6423SLionel Sambuc * tickdelay first. Round downwards on purpose, so the average
35433d6423SLionel Sambuc * half-tick we wait short (depending on where in the current tick
36433d6423SLionel Sambuc * we call tickdelay). We can correct for both overhead of tickdelay
37433d6423SLionel Sambuc * itself and the short wait in the busywait later.
38433d6423SLionel Sambuc */
39433d6423SLionel Sambuc if (micros >= MICROSPERTICK(Hz))
40433d6423SLionel Sambuc tickdelay(micros*Hz/MICROHZ);
41433d6423SLionel Sambuc
42433d6423SLionel Sambuc /* Wait (the rest) of the delay time using busywait. */
43433d6423SLionel Sambuc do {
44433d6423SLionel Sambuc read_frclock_64(&delta);
45433d6423SLionel Sambuc } while (delta_frclock_64(start, delta) < delta_end);
46433d6423SLionel Sambuc
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc return 0;
49433d6423SLionel Sambuc }
50433d6423SLionel Sambuc
frclock_64_to_micros(u64_t tsc)51433d6423SLionel Sambuc u32_t frclock_64_to_micros(u64_t tsc)
52433d6423SLionel Sambuc {
53594df55eSDavid van Moolenbroek return (u32_t)
54*26f5c8f8SDavid van Moolenbroek (tsc / (get_minix_kerninfo()->arm_frclock->hz / MICROHZ));
55433d6423SLionel Sambuc }
56433d6423SLionel Sambuc
57433d6423SLionel Sambuc void
read_frclock(u32_t * frclk)58433d6423SLionel Sambuc read_frclock(u32_t *frclk)
59433d6423SLionel Sambuc {
60594df55eSDavid van Moolenbroek struct minix_kerninfo *minix_kerninfo = get_minix_kerninfo();
61594df55eSDavid van Moolenbroek
62433d6423SLionel Sambuc assert(frclk);
63*26f5c8f8SDavid van Moolenbroek assert(minix_kerninfo->arm_frclock);
64*26f5c8f8SDavid van Moolenbroek assert(minix_kerninfo->arm_frclock->tcrr);
65594df55eSDavid van Moolenbroek *frclk = *(volatile u32_t *)((u8_t *)
66*26f5c8f8SDavid van Moolenbroek minix_kerninfo->arm_frclock->tcrr);
67433d6423SLionel Sambuc }
68433d6423SLionel Sambuc
69433d6423SLionel Sambuc u32_t
delta_frclock(u32_t base,u32_t cur)70433d6423SLionel Sambuc delta_frclock(u32_t base, u32_t cur)
71433d6423SLionel Sambuc {
72433d6423SLionel Sambuc u32_t delta;
73433d6423SLionel Sambuc
74433d6423SLionel Sambuc if (cur < base) {
75433d6423SLionel Sambuc /* We have wrapped around, so delta is base to wrapping point
76433d6423SLionel Sambuc * plus starting point (0) to cur. This supports wrapping once
77433d6423SLionel Sambuc * only. */
78433d6423SLionel Sambuc delta = (UINT_MAX - base) + cur;
79433d6423SLionel Sambuc } else {
80433d6423SLionel Sambuc delta = cur - base;
81433d6423SLionel Sambuc }
82433d6423SLionel Sambuc
83433d6423SLionel Sambuc return delta;
84433d6423SLionel Sambuc }
85433d6423SLionel Sambuc
86433d6423SLionel Sambuc void
read_frclock_64(u64_t * frclk)87433d6423SLionel Sambuc read_frclock_64(u64_t *frclk)
88433d6423SLionel Sambuc {
89433d6423SLionel Sambuc read_frclock((u32_t *) frclk);
90433d6423SLionel Sambuc }
91433d6423SLionel Sambuc
92433d6423SLionel Sambuc u64_t
delta_frclock_64(u64_t base,u64_t cur)93433d6423SLionel Sambuc delta_frclock_64(u64_t base, u64_t cur)
94433d6423SLionel Sambuc {
95433d6423SLionel Sambuc return (u64_t) delta_frclock((u32_t) base, (u32_t) cur);
96433d6423SLionel Sambuc }
97433d6423SLionel Sambuc
98