blob: bcfba16a08e54e1215fa728206e0995a75f2b3fd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Behmee0e49fe2008-12-14 09:47:15 +01002/*
3 * (C) Copyright 2008
4 * Texas Instruments
5 *
6 * Richard Woodruff <r-woodruff2@ti.com>
7 * Syed Moahmmed Khasim <khasim@ti.com>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Marius Groeger <mgroeger@sysgo.de>
12 * Alex Zuepke <azu@sysgo.de>
13 *
14 * (C) Copyright 2002
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +020015 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Dirk Behmee0e49fe2008-12-14 09:47:15 +010016 */
17
18#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -060019#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070020#include <time.h>
Dirk Behmee0e49fe2008-12-14 09:47:15 +010021#include <asm/io.h>
Tom Rini8eb48ff2013-03-14 11:15:25 +000022#include <asm/arch/cpu.h>
Sricharan R8bbdc3f2013-05-30 03:19:34 +000023#include <asm/arch/clock.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Dirk Behmee0e49fe2008-12-14 09:47:15 +010025
Dirk Behmeae8c7ee2010-12-11 10:50:48 -050026DECLARE_GLOBAL_DATA_PTR;
27
Dirk Behmedc7af202009-08-08 09:30:21 +020028static struct gptimer *timer_base = (struct gptimer *)CONFIG_SYS_TIMERBASE;
Patrick Delaunay9858a602018-10-05 11:33:52 +020029static ulong get_timer_masked(void);
Dirk Behmee0e49fe2008-12-14 09:47:15 +010030
Manikandan Pillaie8b16962009-04-21 17:29:05 +020031/*
32 * Nothing really to do with interrupts, just starts up a counter.
Manikandan Pillaie8b16962009-04-21 17:29:05 +020033 */
34
John Rigbyee764d12010-12-27 14:33:10 +000035#define TIMER_CLOCK (V_SCLK / (2 << CONFIG_SYS_PTV))
36#define TIMER_OVERFLOW_VAL 0xffffffff
37#define TIMER_LOAD_VAL 0
Manikandan Pillaie8b16962009-04-21 17:29:05 +020038
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020039int timer_init(void)
Dirk Behmee0e49fe2008-12-14 09:47:15 +010040{
41 /* start the counter ticking up, reload value on overflow */
42 writel(TIMER_LOAD_VAL, &timer_base->tldr);
43 /* enable timer */
Ladislav Michl993e57d2009-03-30 18:58:41 +020044 writel((CONFIG_SYS_PTV << 2) | TCLR_PRE | TCLR_AR | TCLR_ST,
Dirk Behmee0e49fe2008-12-14 09:47:15 +010045 &timer_base->tclr);
46
Dirk Behmee0e49fe2008-12-14 09:47:15 +010047 return 0;
48}
49
50/*
51 * timer without interrupts
52 */
Dirk Behmee0e49fe2008-12-14 09:47:15 +010053ulong get_timer(ulong base)
54{
55 return get_timer_masked() - base;
56}
57
Manikandan Pillaie8b16962009-04-21 17:29:05 +020058/* delay x useconds */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010059void __udelay(unsigned long usec)
Dirk Behmee0e49fe2008-12-14 09:47:15 +010060{
Manikandan Pillaie8b16962009-04-21 17:29:05 +020061 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
62 unsigned long now, last = readl(&timer_base->tcrr);
Dirk Behmee0e49fe2008-12-14 09:47:15 +010063
Manikandan Pillaie8b16962009-04-21 17:29:05 +020064 while (tmo > 0) {
65 now = readl(&timer_base->tcrr);
66 if (last > now) /* count up timer overflow */
John Rigbyee764d12010-12-27 14:33:10 +000067 tmo -= TIMER_OVERFLOW_VAL - last + now + 1;
Manikandan Pillaie8b16962009-04-21 17:29:05 +020068 else
69 tmo -= now - last;
70 last = now;
Dirk Behmee0e49fe2008-12-14 09:47:15 +010071 }
Dirk Behmee0e49fe2008-12-14 09:47:15 +010072}
73
Patrick Delaunay9858a602018-10-05 11:33:52 +020074static ulong get_timer_masked(void)
Dirk Behmee0e49fe2008-12-14 09:47:15 +010075{
Manikandan Pillaie8b16962009-04-21 17:29:05 +020076 /* current tick value */
77 ulong now = readl(&timer_base->tcrr) / (TIMER_CLOCK / CONFIG_SYS_HZ);
Dirk Behmee0e49fe2008-12-14 09:47:15 +010078
Simon Glassa848da52012-12-13 20:48:35 +000079 if (now >= gd->arch.lastinc) { /* normal mode (non roll) */
Dirk Behmee0e49fe2008-12-14 09:47:15 +010080 /* move stamp fordward with absoulte diff ticks */
Simon Glassa848da52012-12-13 20:48:35 +000081 gd->arch.tbl += (now - gd->arch.lastinc);
Simon Glass2655ee12012-12-13 20:48:34 +000082 } else { /* we have rollover of incrementer */
Daniel Gorsulowskic1f2bdd2016-06-06 09:40:11 +020083 gd->arch.tbl += ((TIMER_OVERFLOW_VAL / (TIMER_CLOCK /
Simon Glassa848da52012-12-13 20:48:35 +000084 CONFIG_SYS_HZ)) - gd->arch.lastinc) + now;
Simon Glass2655ee12012-12-13 20:48:34 +000085 }
Simon Glassa848da52012-12-13 20:48:35 +000086 gd->arch.lastinc = now;
Simon Glass2655ee12012-12-13 20:48:34 +000087 return gd->arch.tbl;
Dirk Behmee0e49fe2008-12-14 09:47:15 +010088}
89
Dirk Behmee0e49fe2008-12-14 09:47:15 +010090/*
91 * This function is derived from PowerPC code (read timebase as long long).
92 * On ARM it just returns the timer value.
93 */
94unsigned long long get_ticks(void)
95{
96 return get_timer(0);
97}
98
99/*
100 * This function is derived from PowerPC code (timebase clock frequency).
101 * On ARM it returns the number of timer ticks per second.
102 */
103ulong get_tbclk(void)
104{
Manikandan Pillaie8b16962009-04-21 17:29:05 +0200105 return CONFIG_SYS_HZ;
Dirk Behmee0e49fe2008-12-14 09:47:15 +0100106}