Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2002 |
| 4 | * Lineo, Inc. <www.lineo.com> |
| 5 | * Bernhard Kuhn <bkuhn@lineo.com> |
| 6 | * |
| 7 | * (C) Copyright 2002 |
| 8 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 9 | * Marius Groeger <mgroeger@sysgo.de> |
| 10 | * |
| 11 | * (C) Copyright 2002 |
| 12 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 13 | * Alex Zuepke <azu@sysgo.de> |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 17 | #include <init.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 18 | #include <time.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 20 | |
Jens Scharsig | 58aa563 | 2011-02-19 06:17:02 +0000 | [diff] [blame] | 21 | #include <asm/io.h> |
Andreas Bießmann | 20e3029 | 2010-11-30 09:45:06 +0000 | [diff] [blame] | 22 | #include <asm/arch/hardware.h> |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 23 | #include <asm/arch/at91_tc.h> |
Wenyou Yang | 57b7f29 | 2016-02-03 10:16:49 +0800 | [diff] [blame] | 24 | #include <asm/arch/clk.h> |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 25 | |
Andreas Bießmann | 20e3029 | 2010-11-30 09:45:06 +0000 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 28 | /* the number of clocks per CONFIG_SYS_HZ */ |
| 29 | #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ) |
| 30 | |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 31 | int timer_init(void) |
| 32 | { |
Jens Scharsig | 58aa563 | 2011-02-19 06:17:02 +0000 | [diff] [blame] | 33 | at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC; |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 34 | |
Wenyou Yang | 57b7f29 | 2016-02-03 10:16:49 +0800 | [diff] [blame] | 35 | at91_periph_clk_enable(ATMEL_ID_TC0); |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 36 | |
| 37 | writel(0, &tc->bcr); |
| 38 | writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE | |
| 39 | AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr); |
| 40 | |
| 41 | writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr); |
| 42 | /* set to MCLK/2 and restart the timer |
| 43 | when the value in TC_RC is reached */ |
| 44 | writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr); |
| 45 | |
Mike Williams | bf895ad | 2011-07-22 04:01:30 +0000 | [diff] [blame] | 46 | writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interrupts */ |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 47 | writel(TIMER_LOAD_VAL, &tc->tc[0].rc); |
| 48 | |
| 49 | writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr); |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 50 | gd->arch.lastinc = 0; |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 51 | gd->arch.tbl = 0; |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * timer without interrupts |
| 58 | */ |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 59 | ulong get_timer_raw(void) |
| 60 | { |
Jens Scharsig | 58aa563 | 2011-02-19 06:17:02 +0000 | [diff] [blame] | 61 | at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC; |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 62 | u32 now; |
| 63 | |
| 64 | now = readl(&tc->tc[0].cv) & 0x0000ffff; |
| 65 | |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 66 | if (now >= gd->arch.lastinc) { |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 67 | /* normal mode */ |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 68 | gd->arch.tbl += now - gd->arch.lastinc; |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 69 | } else { |
| 70 | /* we have an overflow ... */ |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 71 | gd->arch.tbl += now + TIMER_LOAD_VAL - gd->arch.lastinc; |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 72 | } |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 73 | gd->arch.lastinc = now; |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 74 | |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 75 | return gd->arch.tbl; |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Patrick Delaunay | 9858a60 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 78 | static ulong get_timer_masked(void) |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 79 | { |
| 80 | return get_timer_raw()/TIMER_LOAD_VAL; |
| 81 | } |
| 82 | |
Patrick Delaunay | 9858a60 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 83 | ulong get_timer(ulong base) |
| 84 | { |
| 85 | return get_timer_masked() - base; |
| 86 | } |
| 87 | |
Patrick Delaunay | 94a0859 | 2018-10-05 11:33:51 +0200 | [diff] [blame] | 88 | void __udelay(unsigned long usec) |
Jens Scharsig | 9bbaae3 | 2010-02-03 22:47:35 +0100 | [diff] [blame] | 89 | { |
| 90 | u32 tmo; |
| 91 | u32 endtime; |
| 92 | signed long diff; |
| 93 | |
| 94 | tmo = CONFIG_SYS_HZ_CLOCK / 1000; |
| 95 | tmo *= usec; |
| 96 | tmo /= 1000; |
| 97 | |
| 98 | endtime = get_timer_raw() + tmo; |
| 99 | |
| 100 | do { |
| 101 | u32 now = get_timer_raw(); |
| 102 | diff = endtime - now; |
| 103 | } while (diff >= 0); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * This function is derived from PowerPC code (read timebase as long long). |
| 108 | * On ARM it just returns the timer value. |
| 109 | */ |
| 110 | unsigned long long get_ticks(void) |
| 111 | { |
| 112 | return get_timer(0); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * This function is derived from PowerPC code (timebase clock frequency). |
| 117 | * On ARM it returns the number of timer ticks per second. |
| 118 | */ |
| 119 | ulong get_tbclk(void) |
| 120 | { |
| 121 | return CONFIG_SYS_HZ; |
| 122 | } |