1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2014 Broadcom Corporation.
4 */
5
6 #include <common.h>
7 #include <div64.h>
8 #include <init.h>
9 #include <time.h>
10 #include <asm/io.h>
11 #include <asm/iproc-common/timer.h>
12 #include <asm/iproc-common/sysmap.h>
13 #include <linux/delay.h>
14
timer_global_read(void)15 static inline uint64_t timer_global_read(void)
16 {
17 uint64_t cur_tick;
18 uint32_t count_h;
19 uint32_t count_l;
20
21 do {
22 count_h = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
23 TIMER_GLB_HI_OFFSET);
24 count_l = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
25 TIMER_GLB_LOW_OFFSET);
26 cur_tick = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
27 TIMER_GLB_HI_OFFSET);
28 } while (cur_tick != count_h);
29
30 return (cur_tick << 32) + count_l;
31 }
32
timer_global_init(void)33 void timer_global_init(void)
34 {
35 writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_CTRL_OFFSET);
36 writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_LOW_OFFSET);
37 writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_HI_OFFSET);
38 writel(TIMER_GLB_TIM_CTRL_TIM_EN,
39 IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_CTRL_OFFSET);
40 }
41
timer_init(void)42 int timer_init(void)
43 {
44 timer_global_init();
45 return 0;
46 }
47
get_timer(unsigned long base)48 unsigned long get_timer(unsigned long base)
49 {
50 uint64_t count;
51 uint64_t ret;
52 uint64_t tim_clk;
53 uint64_t periph_clk;
54
55 count = timer_global_read();
56
57 /* default arm clk is 1GHz, periph_clk=arm_clk/2, tick per msec */
58 periph_clk = 500000;
59 tim_clk = lldiv(periph_clk,
60 (((readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
61 TIMER_GLB_CTRL_OFFSET) &
62 TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1));
63
64 ret = lldiv(count, (uint32_t)tim_clk);
65
66 /* returns msec */
67 return ret - base;
68 }
69
__udelay(unsigned long usec)70 void __udelay(unsigned long usec)
71 {
72 uint64_t cur_tick, end_tick;
73 uint64_t tim_clk;
74 uint64_t periph_clk;
75
76 /* default arm clk is 1GHz, periph_clk=arm_clk/2, tick per usec */
77 periph_clk = 500;
78
79 tim_clk = lldiv(periph_clk,
80 (((readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
81 TIMER_GLB_CTRL_OFFSET) &
82 TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1));
83
84 cur_tick = timer_global_read();
85
86 end_tick = tim_clk;
87 end_tick *= usec;
88 end_tick += cur_tick;
89
90 do {
91 cur_tick = timer_global_read();
92
93 } while (cur_tick < end_tick);
94 }
95
timer_systick_init(uint32_t tick_ms)96 void timer_systick_init(uint32_t tick_ms)
97 {
98 /* Disable timer and clear interrupt status*/
99 writel(0, IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_CTRL_OFFSET);
100 writel(TIMER_PVT_TIM_INT_STATUS_SET,
101 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_STATUS_OFFSET);
102 writel((PLL_AXI_CLK/1000) * tick_ms,
103 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_LOAD_OFFSET);
104 writel(TIMER_PVT_TIM_CTRL_INT_EN |
105 TIMER_PVT_TIM_CTRL_AUTO_RELD |
106 TIMER_PVT_TIM_CTRL_TIM_EN,
107 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_CTRL_OFFSET);
108 }
109
timer_systick_isr(void * data)110 void timer_systick_isr(void *data)
111 {
112 writel(TIMER_PVT_TIM_INT_STATUS_SET,
113 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_STATUS_OFFSET);
114 }
115
116 /*
117 * This function is derived from PowerPC code (read timebase as long long).
118 * On ARM it just returns the timer value in msec.
119 */
get_ticks(void)120 unsigned long long get_ticks(void)
121 {
122 return get_timer(0);
123 }
124
125 /*
126 * This is used in conjuction with get_ticks, which returns msec as ticks.
127 * Here we just return ticks/sec = msec/sec = 1000
128 */
get_tbclk(void)129 ulong get_tbclk(void)
130 {
131 return 1000;
132 }
133