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 <delay.h>
4 #include <soc/iomap.h>
5 #include <soc/ipq_timer.h>
6 #include <timer.h>
7 
8 /*
9  * DGT runs at 25 MHz / 4, or 6.25 ticks per microsecond
10  */
11 #define DGT_MHZ_NUM 25
12 #define DGT_MHZ_DEN 4
13 
14 #define TIMER_TICKS(us) ((DGT_MHZ_NUM*(us) + (DGT_MHZ_DEN - 1)) / DGT_MHZ_DEN)
15 #define TIMER_USECS(ticks) (DGT_MHZ_DEN*(ticks) / DGT_MHZ_NUM)
16 
17 /* Clock divider values for the timer. */
18 #define DGT_CLK_DIV_1 0
19 #define DGT_CLK_DIV_2 1
20 #define DGT_CLK_DIV_3 2
21 #define DGT_CLK_DIV_4 3
22 
23 /**
24  * init_timer - initialize timer
25  */
26 void init_timer(void)
27 {
28  /* disable timer */
29  writel_i(0, DGT_ENABLE);
30 
31  /* DGT uses TCXO source which is 25MHz.
32  * The timer should run at 1/4th the frequency of TCXO
33  * according to clock plan.
34  * Set clock divider to 4.
35  */
37 
38  /* Enable timer */
39  writel_i(0, DGT_CLEAR);
41 }
42 
43 /**
44  * udelay - generates micro second delay.
45  * @param usec: delay duration in microseconds
46  */
47 void udelay(unsigned int usec)
48 {
49  uint32_t now;
50  uint32_t last;
51  uint32_t ticks;
52  uint32_t curr_ticks = 0;
53 
54  /* Calculate number of ticks required. */
55  ticks = TIMER_TICKS(usec);
56 
57  /* Obtain the current timer value. */
58  last = readl_i(DGT_COUNT_VAL);
59 
60  /* Loop until the right number of ticks. */
61  while (curr_ticks < ticks) {
62  now = readl_i(DGT_COUNT_VAL);
63  curr_ticks += now - last;
64  last = now;
65  }
66 }
67 
69 {
71 }
static void mono_time_set_usecs(struct mono_time *mt, long us)
Definition: timer.h:53
#define DGT_ENABLE_EN
Definition: ipq_timer.h:8
__weak void init_timer(void)
Definition: timer.c:7
void udelay(unsigned int usec)
Definition: timer.c:9
#define readl_i(a)
Definition: iomap.h:15
#define writel_i(v, a)
Definition: iomap.h:16
#define DGT_COUNT_VAL
Definition: iomap.h:46
#define DGT_ENABLE
Definition: iomap.h:47
#define DGT_CLK_CTL
Definition: iomap.h:49
#define DGT_CLEAR
Definition: iomap.h:48
void timer_monotonic_get(struct mono_time *mt)
Definition: timer.c:7
#define TIMER_TICKS(us)
Definition: timer.c:14
#define TIMER_USECS(ticks)
Definition: timer.c:15
#define DGT_CLK_DIV_4
Definition: timer.c:21
unsigned int uint32_t
Definition: stdint.h:14