blob: c400e8781356f56b57d96e08efea121edbce0db5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jens Scharsig98250e82010-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 Scharsig98250e82010-02-03 22:47:35 +010014 */
15
16#include <common.h>
Simon Glass691d7192020-05-10 11:40:02 -060017#include <init.h>
Simon Glass10453152019-11-14 12:57:30 -070018#include <time.h>
Simon Glass401d1c42020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Jens Scharsig98250e82010-02-03 22:47:35 +010021
Jens Scharsig80733992011-02-19 06:17:02 +000022#include <asm/io.h>
Andreas Bießmanna429db72010-11-30 09:45:06 +000023#include <asm/arch/hardware.h>
Jens Scharsig98250e82010-02-03 22:47:35 +010024#include <asm/arch/at91_tc.h>
Wenyou Yangeced5a72016-02-03 10:16:49 +080025#include <asm/arch/clk.h>
Jens Scharsig98250e82010-02-03 22:47:35 +010026
Andreas Bießmanna429db72010-11-30 09:45:06 +000027DECLARE_GLOBAL_DATA_PTR;
28
Jens Scharsig98250e82010-02-03 22:47:35 +010029/* the number of clocks per CONFIG_SYS_HZ */
30#define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
31
Jens Scharsig98250e82010-02-03 22:47:35 +010032int timer_init(void)
33{
Jens Scharsig80733992011-02-19 06:17:02 +000034 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
Jens Scharsig98250e82010-02-03 22:47:35 +010035
Wenyou Yangeced5a72016-02-03 10:16:49 +080036 at91_periph_clk_enable(ATMEL_ID_TC0);
Jens Scharsig98250e82010-02-03 22:47:35 +010037
38 writel(0, &tc->bcr);
39 writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE |
40 AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr);
41
42 writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr);
43 /* set to MCLK/2 and restart the timer
44 when the value in TC_RC is reached */
45 writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr);
46
Mike Williams16263082011-07-22 04:01:30 +000047 writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interrupts */
Jens Scharsig98250e82010-02-03 22:47:35 +010048 writel(TIMER_LOAD_VAL, &tc->tc[0].rc);
49
50 writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr);
Simon Glass582601d2012-12-13 20:48:35 +000051 gd->arch.lastinc = 0;
Simon Glass66ee6922012-12-13 20:48:34 +000052 gd->arch.tbl = 0;
Jens Scharsig98250e82010-02-03 22:47:35 +010053
54 return 0;
55}
56
57/*
58 * timer without interrupts
59 */
Jens Scharsig98250e82010-02-03 22:47:35 +010060ulong get_timer_raw(void)
61{
Jens Scharsig80733992011-02-19 06:17:02 +000062 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
Jens Scharsig98250e82010-02-03 22:47:35 +010063 u32 now;
64
65 now = readl(&tc->tc[0].cv) & 0x0000ffff;
66
Simon Glass582601d2012-12-13 20:48:35 +000067 if (now >= gd->arch.lastinc) {
Jens Scharsig98250e82010-02-03 22:47:35 +010068 /* normal mode */
Simon Glass582601d2012-12-13 20:48:35 +000069 gd->arch.tbl += now - gd->arch.lastinc;
Jens Scharsig98250e82010-02-03 22:47:35 +010070 } else {
71 /* we have an overflow ... */
Simon Glass582601d2012-12-13 20:48:35 +000072 gd->arch.tbl += now + TIMER_LOAD_VAL - gd->arch.lastinc;
Jens Scharsig98250e82010-02-03 22:47:35 +010073 }
Simon Glass582601d2012-12-13 20:48:35 +000074 gd->arch.lastinc = now;
Jens Scharsig98250e82010-02-03 22:47:35 +010075
Simon Glass66ee6922012-12-13 20:48:34 +000076 return gd->arch.tbl;
Jens Scharsig98250e82010-02-03 22:47:35 +010077}
78
Patrick Delaunay6180ea72018-10-05 11:33:52 +020079static ulong get_timer_masked(void)
Jens Scharsig98250e82010-02-03 22:47:35 +010080{
81 return get_timer_raw()/TIMER_LOAD_VAL;
82}
83
Patrick Delaunay6180ea72018-10-05 11:33:52 +020084ulong get_timer(ulong base)
85{
86 return get_timer_masked() - base;
87}
88
Patrick Delaunayaa33fe82018-10-05 11:33:51 +020089void __udelay(unsigned long usec)
Jens Scharsig98250e82010-02-03 22:47:35 +010090{
91 u32 tmo;
92 u32 endtime;
93 signed long diff;
94
95 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
96 tmo *= usec;
97 tmo /= 1000;
98
99 endtime = get_timer_raw() + tmo;
100
101 do {
102 u32 now = get_timer_raw();
103 diff = endtime - now;
104 } while (diff >= 0);
105}
106
107/*
108 * This function is derived from PowerPC code (read timebase as long long).
109 * On ARM it just returns the timer value.
110 */
111unsigned long long get_ticks(void)
112{
113 return get_timer(0);
114}
115
116/*
117 * This function is derived from PowerPC code (timebase clock frequency).
118 * On ARM it returns the number of timer ticks per second.
119 */
120ulong get_tbclk(void)
121{
122 return CONFIG_SYS_HZ;
123}