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 | |
Scott Branden | 5653a82 | 2014-08-11 13:58:22 -0700 | [diff] [blame] | 6 | #include <div64.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 7 | #include <init.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 8 | #include <time.h> |
Scott Branden | 5653a82 | 2014-08-11 13:58:22 -0700 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <asm/iproc-common/timer.h> |
| 11 | #include <asm/iproc-common/sysmap.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 12 | #include <linux/delay.h> |
Scott Branden | 5653a82 | 2014-08-11 13:58:22 -0700 | [diff] [blame] | 13 | |
| 14 | static 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 | |
| 32 | void 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 | |
| 41 | int timer_init(void) |
| 42 | { |
| 43 | timer_global_init(); |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | unsigned 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 | |
| 69 | void __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 | |
| 95 | void 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 | |
| 109 | void 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 | */ |
| 119 | unsigned 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 | */ |
| 128 | ulong get_tbclk(void) |
| 129 | { |
| 130 | return 1000; |
| 131 | } |