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