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