1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
9 #include <asm/iproc-common/timer.h>
10 #include <asm/iproc-common/sysmap.h>
12 static inline uint64_t timer_global_read(void)
19 count_h = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
21 count_l = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
22 TIMER_GLB_LOW_OFFSET);
23 cur_tick = readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
25 } while (cur_tick != count_h);
27 return (cur_tick << 32) + count_l;
30 void timer_global_init(void)
32 writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_CTRL_OFFSET);
33 writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_LOW_OFFSET);
34 writel(0, IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_HI_OFFSET);
35 writel(TIMER_GLB_TIM_CTRL_TIM_EN,
36 IPROC_PERIPH_GLB_TIM_REG_BASE + TIMER_GLB_CTRL_OFFSET);
45 unsigned long get_timer(unsigned long base)
52 count = timer_global_read();
54 /* default arm clk is 1GHz, periph_clk=arm_clk/2, tick per msec */
56 tim_clk = lldiv(periph_clk,
57 (((readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
58 TIMER_GLB_CTRL_OFFSET) &
59 TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1));
61 ret = lldiv(count, (uint32_t)tim_clk);
67 void __udelay(unsigned long usec)
69 uint64_t cur_tick, end_tick;
73 /* default arm clk is 1GHz, periph_clk=arm_clk/2, tick per usec */
76 tim_clk = lldiv(periph_clk,
77 (((readl(IPROC_PERIPH_GLB_TIM_REG_BASE +
78 TIMER_GLB_CTRL_OFFSET) &
79 TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1));
81 cur_tick = timer_global_read();
88 cur_tick = timer_global_read();
90 } while (cur_tick < end_tick);
93 void timer_systick_init(uint32_t tick_ms)
95 /* Disable timer and clear interrupt status*/
96 writel(0, IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_CTRL_OFFSET);
97 writel(TIMER_PVT_TIM_INT_STATUS_SET,
98 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_STATUS_OFFSET);
99 writel((PLL_AXI_CLK/1000) * tick_ms,
100 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_LOAD_OFFSET);
101 writel(TIMER_PVT_TIM_CTRL_INT_EN |
102 TIMER_PVT_TIM_CTRL_AUTO_RELD |
103 TIMER_PVT_TIM_CTRL_TIM_EN,
104 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_CTRL_OFFSET);
107 void timer_systick_isr(void *data)
109 writel(TIMER_PVT_TIM_INT_STATUS_SET,
110 IPROC_PERIPH_PVT_TIM_REG_BASE + TIMER_PVT_STATUS_OFFSET);
114 * This function is derived from PowerPC code (read timebase as long long).
115 * On ARM it just returns the timer value in msec.
117 unsigned long long get_ticks(void)
123 * This is used in conjuction with get_ticks, which returns msec as ticks.
124 * Here we just return ticks/sec = msec/sec = 1000
126 ulong get_tbclk(void)