Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007-2011 |
| 4 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 5 | * Tom Cubie <tangliang@allwinnertech.com> |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
Andre Przywara | e42015b | 2022-07-03 00:14:24 +0100 | [diff] [blame] | 12 | #include <asm/arch/cpu.h> |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 13 | #include <asm/arch/timer.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | #define TIMER_MODE (0x0 << 7) /* continuous mode */ |
| 19 | #define TIMER_DIV (0x0 << 4) /* pre scale 1 */ |
| 20 | #define TIMER_SRC (0x1 << 2) /* osc24m */ |
| 21 | #define TIMER_RELOAD (0x1 << 1) /* reload internal value */ |
| 22 | #define TIMER_EN (0x1 << 0) /* enable timer */ |
| 23 | |
| 24 | #define TIMER_CLOCK (24 * 1000 * 1000) |
| 25 | #define COUNT_TO_USEC(x) ((x) / 24) |
| 26 | #define USEC_TO_COUNT(x) ((x) * 24) |
| 27 | #define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ) |
| 28 | #define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ) |
| 29 | |
| 30 | #define TIMER_LOAD_VAL 0xffffffff |
| 31 | |
| 32 | #define TIMER_NUM 0 /* we use timer 0 */ |
| 33 | |
| 34 | /* read the 32-bit timer */ |
| 35 | static ulong read_timer(void) |
| 36 | { |
| 37 | struct sunxi_timer_reg *timers = |
| 38 | (struct sunxi_timer_reg *)SUNXI_TIMER_BASE; |
| 39 | struct sunxi_timer *timer = &timers->timer[TIMER_NUM]; |
| 40 | |
| 41 | /* |
| 42 | * The hardware timer counts down, therefore we invert to |
| 43 | * produce an incrementing timer. |
| 44 | */ |
| 45 | return ~readl(&timer->val); |
| 46 | } |
| 47 | |
| 48 | /* init timer register */ |
| 49 | int timer_init(void) |
| 50 | { |
| 51 | struct sunxi_timer_reg *timers = |
| 52 | (struct sunxi_timer_reg *)SUNXI_TIMER_BASE; |
| 53 | struct sunxi_timer *timer = &timers->timer[TIMER_NUM]; |
Jesse Taube | c339098 | 2022-01-29 10:23:01 -0500 | [diff] [blame] | 54 | |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 55 | writel(TIMER_LOAD_VAL, &timer->inter); |
| 56 | writel(TIMER_MODE | TIMER_DIV | TIMER_SRC | TIMER_RELOAD | TIMER_EN, |
| 57 | &timer->ctl); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Patrick Delaunay | 9858a60 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 62 | static ulong get_timer_masked(void) |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 63 | { |
| 64 | /* current tick value */ |
| 65 | ulong now = TICKS_TO_HZ(read_timer()); |
| 66 | |
Jesse Taube | c339098 | 2022-01-29 10:23:01 -0500 | [diff] [blame] | 67 | if (now >= gd->arch.lastinc) { /* normal (non rollover) */ |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 68 | gd->arch.tbl += (now - gd->arch.lastinc); |
Jesse Taube | c339098 | 2022-01-29 10:23:01 -0500 | [diff] [blame] | 69 | } else { |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 70 | /* rollover */ |
| 71 | gd->arch.tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL) |
| 72 | - gd->arch.lastinc) + now; |
| 73 | } |
| 74 | gd->arch.lastinc = now; |
| 75 | |
| 76 | return gd->arch.tbl; |
| 77 | } |
| 78 | |
Jesse Taube | c339098 | 2022-01-29 10:23:01 -0500 | [diff] [blame] | 79 | /* timer without interrupts */ |
Patrick Delaunay | 9858a60 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 80 | ulong get_timer(ulong base) |
| 81 | { |
| 82 | return get_timer_masked() - base; |
| 83 | } |
| 84 | |
Ian Campbell | 49aeca3 | 2014-05-05 11:52:23 +0100 | [diff] [blame] | 85 | /* delay x useconds */ |
| 86 | void __udelay(unsigned long usec) |
| 87 | { |
| 88 | long tmo = USEC_TO_COUNT(usec); |
| 89 | ulong now, last = read_timer(); |
| 90 | |
| 91 | while (tmo > 0) { |
| 92 | now = read_timer(); |
| 93 | if (now > last) /* normal (non rollover) */ |
| 94 | tmo -= now - last; |
| 95 | else /* rollover */ |
| 96 | tmo -= TIMER_LOAD_VAL - last + now; |
| 97 | last = now; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * This function is derived from PowerPC code (read timebase as long long). |
| 103 | * On ARM it just returns the timer value. |
| 104 | */ |
| 105 | unsigned long long get_ticks(void) |
| 106 | { |
| 107 | return get_timer(0); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * This function is derived from PowerPC code (timebase clock frequency). |
| 112 | * On ARM it returns the number of timer ticks per second. |
| 113 | */ |
| 114 | ulong get_tbclk(void) |
| 115 | { |
| 116 | return CONFIG_SYS_HZ; |
| 117 | } |