Debian Codegraph @

src/soc/qualcomm/ipq40xx/timer.c

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-3-Clause */
2
3 #include <device/mmio.h>
4 #include <delay.h>
5 #include <soc/iomap.h>
6 #include <soc/ipq_timer.h>
7 #include <timer.h>
8
9 #define GCNT_FREQ_MHZ 48
10
11 #define TIMER_TICKS(us) (GCNT_FREQ_MHZ * (us))
12 #define TIMER_USECS(ticks) ((ticks) / GCNT_FREQ_MHZ)
13
14 /**
15 * init_timer - initialize timer
16 */
17 void init_timer(void)
18 {
19 /* disable timer */
20 write32(GCNT_CNTCR, 0);
21
22 /* Reset the counters to zero */
23 write32(GCNT_GLB_CNTCV_LO, 0);
24 write32(GCNT_GLB_CNTCV_HI, 0);
25
26 /* Enable timer */
27 write32(GCNT_CNTCR, 1);
28 }
29
30 static inline uint64_t read_gcnt_val(void)
31 {
32 uint32_t hi, lo;
33
34 do {
35 hi = read32(GCNT_CNTCV_HI);
36 lo = read32(GCNT_CNTCV_LO);
37 } while (hi != read32(GCNT_CNTCV_HI));
38
39 return ((((uint64_t)hi) << 32) | lo);
40 }
41
42 /**
43 * udelay - generates micro second delay.
44 * @param usec: delay duration in microseconds
45 */
46 void udelay(unsigned int usec)
47 {
48 uint64_t expire;
49
50 expire = read_gcnt_val() + TIMER_TICKS(usec);
51
52 while (expire >= read_gcnt_val())
53 ;
54 }
55
56 void timer_monotonic_get(struct mono_time *mt)
57 {
58 mono_time_set_usecs(mt, TIMER_USECS(read_gcnt_val()));
59 }