blob: 3aef9538b4a06ec6d78c09aff4d885f4079d6e48 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jens Scharsig9bbaae32010-02-03 22:47:35 +01002/*
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 Scharsig9bbaae32010-02-03 22:47:35 +010014 */
15
16#include <common.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070017#include <time.h>
Jens Scharsig9bbaae32010-02-03 22:47:35 +010018
Jens Scharsig58aa5632011-02-19 06:17:02 +000019#include <asm/io.h>
Andreas Bießmann20e30292010-11-30 09:45:06 +000020#include <asm/arch/hardware.h>
Jens Scharsig9bbaae32010-02-03 22:47:35 +010021#include <asm/arch/at91_tc.h>
Wenyou Yang57b7f292016-02-03 10:16:49 +080022#include <asm/arch/clk.h>
Jens Scharsig9bbaae32010-02-03 22:47:35 +010023
Andreas Bießmann20e30292010-11-30 09:45:06 +000024DECLARE_GLOBAL_DATA_PTR;
25
Jens Scharsig9bbaae32010-02-03 22:47:35 +010026/* the number of clocks per CONFIG_SYS_HZ */
27#define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
28
Jens Scharsig9bbaae32010-02-03 22:47:35 +010029int timer_init(void)
30{
Jens Scharsig58aa5632011-02-19 06:17:02 +000031 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
Jens Scharsig9bbaae32010-02-03 22:47:35 +010032
Wenyou Yang57b7f292016-02-03 10:16:49 +080033 at91_periph_clk_enable(ATMEL_ID_TC0);
Jens Scharsig9bbaae32010-02-03 22:47:35 +010034
35 writel(0, &tc->bcr);
36 writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE |
37 AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr);
38
39 writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr);
40 /* set to MCLK/2 and restart the timer
41 when the value in TC_RC is reached */
42 writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr);
43
Mike Williamsbf895ad2011-07-22 04:01:30 +000044 writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interrupts */
Jens Scharsig9bbaae32010-02-03 22:47:35 +010045 writel(TIMER_LOAD_VAL, &tc->tc[0].rc);
46
47 writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr);
Simon Glassa848da52012-12-13 20:48:35 +000048 gd->arch.lastinc = 0;
Simon Glass2655ee12012-12-13 20:48:34 +000049 gd->arch.tbl = 0;
Jens Scharsig9bbaae32010-02-03 22:47:35 +010050
51 return 0;
52}
53
54/*
55 * timer without interrupts
56 */
Jens Scharsig9bbaae32010-02-03 22:47:35 +010057ulong get_timer_raw(void)
58{
Jens Scharsig58aa5632011-02-19 06:17:02 +000059 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
Jens Scharsig9bbaae32010-02-03 22:47:35 +010060 u32 now;
61
62 now = readl(&tc->tc[0].cv) & 0x0000ffff;
63
Simon Glassa848da52012-12-13 20:48:35 +000064 if (now >= gd->arch.lastinc) {
Jens Scharsig9bbaae32010-02-03 22:47:35 +010065 /* normal mode */
Simon Glassa848da52012-12-13 20:48:35 +000066 gd->arch.tbl += now - gd->arch.lastinc;
Jens Scharsig9bbaae32010-02-03 22:47:35 +010067 } else {
68 /* we have an overflow ... */
Simon Glassa848da52012-12-13 20:48:35 +000069 gd->arch.tbl += now + TIMER_LOAD_VAL - gd->arch.lastinc;
Jens Scharsig9bbaae32010-02-03 22:47:35 +010070 }
Simon Glassa848da52012-12-13 20:48:35 +000071 gd->arch.lastinc = now;
Jens Scharsig9bbaae32010-02-03 22:47:35 +010072
Simon Glass2655ee12012-12-13 20:48:34 +000073 return gd->arch.tbl;
Jens Scharsig9bbaae32010-02-03 22:47:35 +010074}
75
Patrick Delaunay9858a602018-10-05 11:33:52 +020076static ulong get_timer_masked(void)
Jens Scharsig9bbaae32010-02-03 22:47:35 +010077{
78 return get_timer_raw()/TIMER_LOAD_VAL;
79}
80
Patrick Delaunay9858a602018-10-05 11:33:52 +020081ulong get_timer(ulong base)
82{
83 return get_timer_masked() - base;
84}
85
Patrick Delaunay94a08592018-10-05 11:33:51 +020086void __udelay(unsigned long usec)
Jens Scharsig9bbaae32010-02-03 22:47:35 +010087{
88 u32 tmo;
89 u32 endtime;
90 signed long diff;
91
92 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
93 tmo *= usec;
94 tmo /= 1000;
95
96 endtime = get_timer_raw() + tmo;
97
98 do {
99 u32 now = get_timer_raw();
100 diff = endtime - now;
101 } while (diff >= 0);
102}
103
104/*
105 * This function is derived from PowerPC code (read timebase as long long).
106 * On ARM it just returns the timer value.
107 */
108unsigned long long get_ticks(void)
109{
110 return get_timer(0);
111}
112
113/*
114 * This function is derived from PowerPC code (timebase clock frequency).
115 * On ARM it returns the number of timer ticks per second.
116 */
117ulong get_tbclk(void)
118{
119 return CONFIG_SYS_HZ;
120}