blob: 5f3609aec3c2328dd46c552e6c7f06212701ebcd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +01002/*
3 * Cirrus Logic EP93xx timer support.
4 *
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +01005 * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net>
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +01006 *
7 * Copyright (C) 2004, 2005
8 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
9 *
10 * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
11 * author unknown.
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010012 */
13
14#include <common.h>
15#include <linux/types.h>
16#include <asm/arch/ep93xx.h>
17#include <asm/io.h>
Matthias Kaehlckef56e70f2010-02-24 00:22:00 +010018#include <div64.h>
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010019
20#define TIMER_CLKSEL (1 << 3)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010021#define TIMER_ENABLE (1 << 7)
22
Matthias Kaehlcke64d667b2010-03-09 22:13:20 +010023#define TIMER_FREQ 508469 /* ticks / second */
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010024#define TIMER_MAX_VAL 0xFFFFFFFF
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010025
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010026static struct ep93xx_timer
27{
28 unsigned long long ticks;
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010029 unsigned long last_read;
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +010030} timer;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010031
Matthias Kaehlcke64d667b2010-03-09 22:13:20 +010032static inline unsigned long long usecs_to_ticks(unsigned long usecs)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010033{
Matthias Kaehlcke64d667b2010-03-09 22:13:20 +010034 unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
35 do_div(ticks, 1000 * 1000);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010036
37 return ticks;
38}
39
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010040static inline void read_timer(void)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010041{
Matthias Kaehlcke9d466562010-03-09 22:13:47 +010042 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010043 const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
44
45 if (now >= timer.last_read)
46 timer.ticks += now - timer.last_read;
47 else
48 /* an overflow occurred */
49 timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010050
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010051 timer.last_read = now;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010052}
53
54/*
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +010055 * Get the number of ticks (in CONFIG_SYS_HZ resolution)
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010056 */
57unsigned long long get_ticks(void)
58{
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010059 unsigned long long sys_ticks;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010060
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010061 read_timer();
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010062
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010063 sys_ticks = timer.ticks * CONFIG_SYS_HZ;
64 do_div(sys_ticks, TIMER_FREQ);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010065
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010066 return sys_ticks;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010067}
68
69unsigned long get_timer_masked(void)
70{
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +010071 return get_ticks();
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010072}
73
74unsigned long get_timer(unsigned long base)
75{
76 return get_timer_masked() - base;
77}
78
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010079void __udelay(unsigned long usec)
80{
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010081 unsigned long long target;
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +010082
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010083 read_timer();
84
85 target = timer.ticks + usecs_to_ticks(usec);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010086
Matthias Kaehlcke957f7b52010-03-09 22:13:33 +010087 while (timer.ticks < target)
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010088 read_timer();
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010089}
90
91int timer_init(void)
92{
Matthias Kaehlcke9d466562010-03-09 22:13:47 +010093 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010094
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010095 /* use timer 3 with 508KHz and free running, not enabled now */
Matthias Kaehlcke9d466562010-03-09 22:13:47 +010096 writel(TIMER_CLKSEL, &timer_regs->timer3.control);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +010097
Matthias Kaehlcke37911ae2010-03-09 22:13:56 +010098 /* set initial timer value */
Matthias Kaehlcke9d466562010-03-09 22:13:47 +010099 writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100100
Matthias Kaehlcke8df23b72010-02-24 00:22:09 +0100101 /* Enable the timer */
102 writel(TIMER_ENABLE | TIMER_CLKSEL,
Matthias Kaehlcke9d466562010-03-09 22:13:47 +0100103 &timer_regs->timer3.control);
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100104
Graeme Russ944a7fe2011-07-15 02:21:14 +0000105 /* Reset the timer */
106 read_timer();
107 timer.ticks = 0;
Matthias Kaehlcke195dbd12010-02-01 21:29:39 +0100108
109 return 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 */
116unsigned long get_tbclk(void)
117{
118 return CONFIG_SYS_HZ;
119}