blob: a4255a44c00138ba5e4746bad00b5a580349bf3a [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>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Scott Branden5653a822014-08-11 13:58:22 -070014
15static 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
33void 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
42int timer_init(void)
43{
44 timer_global_init();
45 return 0;
46}
47
48unsigned 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
70void __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
96void 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
110void 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 */
120unsigned 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 */
129ulong get_tbclk(void)
130{
131 return 1000;
132}