blob: 6a98d7c4de9aa26600ad3f02597f819b4d517e68 [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>
17
Jens Scharsig80733992011-02-19 06:17:02 +000018#include <asm/io.h>
Andreas Bießmanna429db72010-11-30 09:45:06 +000019#include <asm/arch/hardware.h>
Jens Scharsig98250e82010-02-03 22:47:35 +010020#include <asm/arch/at91_tc.h>
Wenyou Yangeced5a72016-02-03 10:16:49 +080021#include <asm/arch/clk.h>
Jens Scharsig98250e82010-02-03 22:47:35 +010022
Andreas Bießmanna429db72010-11-30 09:45:06 +000023DECLARE_GLOBAL_DATA_PTR;
24
Jens Scharsig98250e82010-02-03 22:47:35 +010025/* the number of clocks per CONFIG_SYS_HZ */
26#define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
27
Jens Scharsig98250e82010-02-03 22:47:35 +010028int timer_init(void)
29{
Jens Scharsig80733992011-02-19 06:17:02 +000030 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
Jens Scharsig98250e82010-02-03 22:47:35 +010031
Wenyou Yangeced5a72016-02-03 10:16:49 +080032 at91_periph_clk_enable(ATMEL_ID_TC0);
Jens Scharsig98250e82010-02-03 22:47:35 +010033
34 writel(0, &tc->bcr);
35 writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE |
36 AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr);
37
38 writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr);
39 /* set to MCLK/2 and restart the timer
40 when the value in TC_RC is reached */
41 writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr);
42
Mike Williams16263082011-07-22 04:01:30 +000043 writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interrupts */
Jens Scharsig98250e82010-02-03 22:47:35 +010044 writel(TIMER_LOAD_VAL, &tc->tc[0].rc);
45
46 writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr);
Simon Glass582601d2012-12-13 20:48:35 +000047 gd->arch.lastinc = 0;
Simon Glass66ee6922012-12-13 20:48:34 +000048 gd->arch.tbl = 0;
Jens Scharsig98250e82010-02-03 22:47:35 +010049
50 return 0;
51}
52
53/*
54 * timer without interrupts
55 */
Jens Scharsig98250e82010-02-03 22:47:35 +010056ulong get_timer(ulong base)
57{
58 return get_timer_masked() - base;
59}
60
Jens Scharsig98250e82010-02-03 22:47:35 +010061ulong get_timer_raw(void)
62{
Jens Scharsig80733992011-02-19 06:17:02 +000063 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
Jens Scharsig98250e82010-02-03 22:47:35 +010064 u32 now;
65
66 now = readl(&tc->tc[0].cv) & 0x0000ffff;
67
Simon Glass582601d2012-12-13 20:48:35 +000068 if (now >= gd->arch.lastinc) {
Jens Scharsig98250e82010-02-03 22:47:35 +010069 /* normal mode */
Simon Glass582601d2012-12-13 20:48:35 +000070 gd->arch.tbl += now - gd->arch.lastinc;
Jens Scharsig98250e82010-02-03 22:47:35 +010071 } else {
72 /* we have an overflow ... */
Simon Glass582601d2012-12-13 20:48:35 +000073 gd->arch.tbl += now + TIMER_LOAD_VAL - gd->arch.lastinc;
Jens Scharsig98250e82010-02-03 22:47:35 +010074 }
Simon Glass582601d2012-12-13 20:48:35 +000075 gd->arch.lastinc = now;
Jens Scharsig98250e82010-02-03 22:47:35 +010076
Simon Glass66ee6922012-12-13 20:48:34 +000077 return gd->arch.tbl;
Jens Scharsig98250e82010-02-03 22:47:35 +010078}
79
80ulong get_timer_masked(void)
81{
82 return get_timer_raw()/TIMER_LOAD_VAL;
83}
84
Patrick Delaunayaa33fe82018-10-05 11:33:51 +020085void __udelay(unsigned long usec)
Jens Scharsig98250e82010-02-03 22:47:35 +010086{
87 u32 tmo;
88 u32 endtime;
89 signed long diff;
90
91 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
92 tmo *= usec;
93 tmo /= 1000;
94
95 endtime = get_timer_raw() + tmo;
96
97 do {
98 u32 now = get_timer_raw();
99 diff = endtime - now;
100 } while (diff >= 0);
101}
102
103/*
104 * This function is derived from PowerPC code (read timebase as long long).
105 * On ARM it just returns the timer value.
106 */
107unsigned long long get_ticks(void)
108{
109 return get_timer(0);
110}
111
112/*
113 * This function is derived from PowerPC code (timebase clock frequency).
114 * On ARM it returns the number of timer ticks per second.
115 */
116ulong get_tbclk(void)
117{
118 return CONFIG_SYS_HZ;
119}