Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Scott Branden | 5653a82 | 2014-08-11 13:58:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Broadcom Corporation. |
Scott Branden | 5653a82 | 2014-08-11 13:58:22 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <div64.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 9 | #include <time.h> |
Scott Branden | 5653a82 | 2014-08-11 13:58:22 -0700 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/iproc-common/timer.h> |
| 12 | #include <asm/iproc-common/sysmap.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Scott Branden | 5653a82 | 2014-08-11 13:58:22 -0700 | [diff] [blame] | 14 | |
| 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 | |
| 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 | |
| 42 | int timer_init(void) |
| 43 | { |
| 44 | timer_global_init(); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | */ |
| 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 | */ |
| 129 | ulong get_tbclk(void) |
| 130 | { |
| 131 | return 1000; |
| 132 | } |