Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 2 | /* |
Albert ARIBAUD | 57b4bce | 2011-04-22 19:41:02 +0200 | [diff] [blame] | 3 | * Copyright (C) 2010 Albert ARIBAUD <albert.u.boot@aribaud.net> |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 4 | * |
| 5 | * Based on original Kirkwood support which is |
| 6 | * Copyright (C) Marvell International Ltd. and its affiliates |
| 7 | * Written-by: Prafulla Wadaskar <prafulla@marvell.com> |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 11 | #include <init.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 12 | #include <time.h> |
Lei Wen | 5ff8b35 | 2011-10-24 16:27:32 +0000 | [diff] [blame] | 13 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 15 | |
| 16 | #define UBOOT_CNTR 0 /* counter to use for uboot timer */ |
| 17 | |
| 18 | /* Timer reload and current value registers */ |
| 19 | struct orion5x_tmr_val { |
| 20 | u32 reload; /* Timer reload reg */ |
| 21 | u32 val; /* Timer value reg */ |
| 22 | }; |
| 23 | |
| 24 | /* Timer registers */ |
| 25 | struct orion5x_tmr_registers { |
| 26 | u32 ctrl; /* Timer control reg */ |
| 27 | u32 pad[3]; |
| 28 | struct orion5x_tmr_val tmr[2]; |
| 29 | u32 wdt_reload; |
| 30 | u32 wdt_val; |
| 31 | }; |
| 32 | |
| 33 | struct orion5x_tmr_registers *orion5x_tmr_regs = |
| 34 | (struct orion5x_tmr_registers *)ORION5X_TIMER_BASE; |
| 35 | |
| 36 | /* |
| 37 | * ARM Timers Registers Map |
| 38 | */ |
| 39 | #define CNTMR_CTRL_REG (&orion5x_tmr_regs->ctrl) |
| 40 | #define CNTMR_RELOAD_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].reload) |
| 41 | #define CNTMR_VAL_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].val) |
| 42 | |
| 43 | /* |
| 44 | * ARM Timers Control Register |
| 45 | * CPU_TIMERS_CTRL_REG (CTCR) |
| 46 | */ |
| 47 | #define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2) |
| 48 | #define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS) |
| 49 | #define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr)) |
| 50 | #define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr)) |
| 51 | |
| 52 | #define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1) |
| 53 | #define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1) |
| 54 | #define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr)) |
| 55 | #define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr)) |
| 56 | |
| 57 | /* |
| 58 | * ARM Timer\Watchdog Reload Register |
| 59 | * CNTMR_RELOAD_REG (TRR) |
| 60 | */ |
| 61 | #define TRG_ARM_TIMER_REL_OFFS 0 |
| 62 | #define TRG_ARM_TIMER_REL_MASK 0xffffffff |
| 63 | |
| 64 | /* |
| 65 | * ARM Timer\Watchdog Register |
| 66 | * CNTMR_VAL_REG (TVRG) |
| 67 | */ |
| 68 | #define TVR_ARM_TIMER_OFFS 0 |
| 69 | #define TVR_ARM_TIMER_MASK 0xffffffff |
| 70 | #define TVR_ARM_TIMER_MAX 0xffffffff |
| 71 | #define TIMER_LOAD_VAL 0xffffffff |
| 72 | |
| 73 | static inline ulong read_timer(void) |
| 74 | { |
| 75 | return readl(CNTMR_VAL_REG(UBOOT_CNTR)) |
| 76 | / (CONFIG_SYS_TCLK / 1000); |
| 77 | } |
| 78 | |
Heiko Schocher | c9ac3ba | 2011-01-20 22:56:39 +0000 | [diff] [blame] | 79 | DECLARE_GLOBAL_DATA_PTR; |
| 80 | |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 81 | #define timestamp gd->arch.tbl |
Simon Glass | 582601d | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 82 | #define lastdec gd->arch.lastinc |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 83 | |
Patrick Delaunay | 6180ea7 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 84 | static ulong get_timer_masked(void) |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 85 | { |
| 86 | ulong now = read_timer(); |
| 87 | |
| 88 | if (lastdec >= now) { |
| 89 | /* normal mode */ |
| 90 | timestamp += lastdec - now; |
| 91 | } else { |
| 92 | /* we have an overflow ... */ |
| 93 | timestamp += lastdec + |
| 94 | (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now; |
| 95 | } |
| 96 | lastdec = now; |
| 97 | |
| 98 | return timestamp; |
| 99 | } |
| 100 | |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 101 | ulong get_timer(ulong base) |
| 102 | { |
| 103 | return get_timer_masked() - base; |
| 104 | } |
| 105 | |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 106 | static inline ulong uboot_cntr_val(void) |
| 107 | { |
| 108 | return readl(CNTMR_VAL_REG(UBOOT_CNTR)); |
| 109 | } |
| 110 | |
| 111 | void __udelay(unsigned long usec) |
| 112 | { |
| 113 | uint current; |
| 114 | ulong delayticks; |
| 115 | |
| 116 | current = uboot_cntr_val(); |
| 117 | delayticks = (usec * (CONFIG_SYS_TCLK / 1000000)); |
| 118 | |
| 119 | if (current < delayticks) { |
| 120 | delayticks -= current; |
| 121 | while (uboot_cntr_val() < current) |
| 122 | ; |
| 123 | while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val()) |
| 124 | ; |
| 125 | } else { |
| 126 | while (uboot_cntr_val() > (current - delayticks)) |
| 127 | ; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * init the counter |
| 133 | */ |
| 134 | int timer_init(void) |
| 135 | { |
| 136 | unsigned int cntmrctrl; |
| 137 | |
| 138 | /* load value into timer */ |
| 139 | writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR)); |
| 140 | writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR)); |
| 141 | |
| 142 | /* enable timer in auto reload mode */ |
| 143 | cntmrctrl = readl(CNTMR_CTRL_REG); |
| 144 | cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR); |
| 145 | cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR); |
| 146 | writel(cntmrctrl, CNTMR_CTRL_REG); |
Albert Aribaud | d778a2f | 2010-09-23 21:49:23 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | } |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 149 | |
Albert Aribaud | d778a2f | 2010-09-23 21:49:23 +0200 | [diff] [blame] | 150 | void timer_init_r(void) |
| 151 | { |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 152 | /* init the timestamp and lastdec value */ |
Graeme Russ | 17659d7 | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 153 | lastdec = read_timer(); |
| 154 | timestamp = 0; |
Albert Aribaud | 0c61e6f | 2010-06-17 19:36:07 +0530 | [diff] [blame] | 155 | } |
Prafulla Wadaskar | 96f5c4b | 2012-02-08 14:15:53 +0530 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * This function is derived from PowerPC code (read timebase as long long). |
| 159 | * On ARM it just returns the timer value. |
| 160 | */ |
| 161 | unsigned long long get_ticks(void) |
| 162 | { |
| 163 | return get_timer(0); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * This function is derived from PowerPC code (timebase clock frequency). |
| 168 | * On ARM it returns the number of timer ticks per second. |
| 169 | */ |
Simon Glass | 049f8d6 | 2019-12-28 10:44:59 -0700 | [diff] [blame] | 170 | ulong get_tbclk(void) |
Prafulla Wadaskar | 96f5c4b | 2012-02-08 14:15:53 +0530 | [diff] [blame] | 171 | { |
| 172 | return (ulong)CONFIG_SYS_HZ; |
| 173 | } |