coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
timer.c
Go to the documentation of this file.
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 */
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 
57 {
59 }
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
static void mono_time_set_usecs(struct mono_time *mt, long us)
Definition: timer.h:53
__weak void init_timer(void)
Definition: timer.c:7
void udelay(unsigned int usec)
Definition: timer.c:9
#define GCNT_CNTCR
Definition: iomap.h:50
#define GCNT_CNTCV_HI
Definition: iomap.h:54
#define GCNT_CNTCV_LO
Definition: iomap.h:53
#define GCNT_GLB_CNTCV_HI
Definition: iomap.h:52
#define GCNT_GLB_CNTCV_LO
Definition: iomap.h:51
void timer_monotonic_get(struct mono_time *mt)
Definition: timer.c:7
#define TIMER_TICKS(us)
Definition: timer.c:11
#define TIMER_USECS(ticks)
Definition: timer.c:12
static uint64_t read_gcnt_val(void)
Definition: timer.c:30
unsigned int uint32_t
Definition: stdint.h:14
unsigned long long uint64_t
Definition: stdint.h:17