blob: 9db5135a8ffab1fb0397bb77c85d73847ce438b3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +02002/*
3 * (C) Copyright 2002
4 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5 * Marius Groeger <mgroeger@sysgo.de>
6 *
7 * (C) Copyright 2002
8 * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
9 *
10 * (C) Copyright 2003
11 * Texas Instruments, <www.ti.com>
12 * Kshitij Gupta <Kshitij@ti.com>
13 *
14 * (C) Copyright 2004
15 * ARM Ltd.
16 * Philippe Robin, <philippe.robin@arm.com>
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020017 */
18
19#include <common.h>
20#include <div64.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070021#include <time.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020023
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020024#ifdef CONFIG_ARCH_CINTEGRATOR
25#define DIV_CLOCK_INIT 1
26#define TIMER_LOAD_VAL 0xFFFFFFFFL
27#else
28#define DIV_CLOCK_INIT 256
29#define TIMER_LOAD_VAL 0x0000FFFFL
30#endif
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020031/* The Integrator/CP timer1 is clocked at 1MHz
32 * can be divided by 16 or 256
33 * and can be set up as a 32-bit timer
34 */
35/* U-Boot expects a 32 bit timer, running at CONFIG_SYS_HZ */
36/* Keep total timer count to avoid losing decrements < div_timer */
37static unsigned long long total_count = 0;
38static unsigned long long lastdec; /* Timer reading at last call */
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020039/* Divisor applied to timer clock */
40static unsigned long long div_clock = DIV_CLOCK_INIT;
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020041static unsigned long long div_timer = 1; /* Divisor to convert timer reading
42 * change to U-Boot ticks
43 */
Tom Rini6a5dccc2022-11-16 13:10:41 -050044/* CONFIG_SYS_HZ = CFG_SYS_HZ_CLOCK/(div_clock * div_timer) */
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020045static ulong timestamp; /* U-Boot ticks since startup */
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020046
Tom Rini6a5dccc2022-11-16 13:10:41 -050047#define READ_TIMER (*(volatile ulong *)(CFG_SYS_TIMERBASE+4))
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020048
49/* all function return values in U-Boot ticks i.e. (1/CONFIG_SYS_HZ) sec
50 * - unless otherwise stated
51 */
52
53/* starts up a counter
54 * - the Integrator/CP timer can be set up to issue an interrupt */
55int timer_init (void)
56{
57 /* Load timer with initial value */
Tom Rini6a5dccc2022-11-16 13:10:41 -050058 *(volatile ulong *)(CFG_SYS_TIMERBASE + 0) = TIMER_LOAD_VAL;
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020059#ifdef CONFIG_ARCH_CINTEGRATOR
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020060 /* Set timer to be
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020061 * enabled 1
62 * periodic 1
63 * no interrupts 0
64 * X 0
65 * divider 1 00 == less rounding error
66 * 32 bit 1
67 * wrapping 0
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020068 */
Tom Rini6a5dccc2022-11-16 13:10:41 -050069 *(volatile ulong *)(CFG_SYS_TIMERBASE + 8) = 0x000000C2;
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020070#else
71 /* Set timer to be
72 * enabled 1
73 * free-running 0
74 * XX 00
75 * divider 256 10
76 * XX 00
77 */
Tom Rini6a5dccc2022-11-16 13:10:41 -050078 *(volatile ulong *)(CFG_SYS_TIMERBASE + 8) = 0x00000088;
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020079#endif
80
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020081 /* init the timestamp */
82 total_count = 0ULL;
Graeme Russ944a7fe2011-07-15 02:21:14 +000083 /* capure current decrementer value */
84 lastdec = READ_TIMER;
85 /* start "advancing" time stamp from 0 */
86 timestamp = 0L;
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020087
Tom Rini6a5dccc2022-11-16 13:10:41 -050088 div_timer = CFG_SYS_HZ_CLOCK;
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +020089 do_div(div_timer, CONFIG_SYS_HZ);
90 do_div(div_timer, div_clock);
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020091
92 return (0);
93}
94
95/*
96 * timer without interrupts
97 */
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020098
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +020099/* converts the timer reading to U-Boot ticks */
100/* the timestamp is the number of ticks since reset */
Patrick Delaunay9858a602018-10-05 11:33:52 +0200101static ulong get_timer_masked (void)
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +0200102{
103 /* get current count */
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +0200104 unsigned long long now = READ_TIMER;
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +0200105
106 if(now > lastdec) {
107 /* Must have wrapped */
108 total_count += lastdec + TIMER_LOAD_VAL + 1 - now;
109 } else {
110 total_count += lastdec - now;
111 }
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +0200112 lastdec = now;
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +0200113
114 /* Reuse "now" */
115 now = total_count;
116 do_div(now, div_timer);
117 timestamp = now;
118
119 return timestamp;
120}
121
Patrick Delaunay9858a602018-10-05 11:33:52 +0200122ulong get_timer (ulong base_ticks)
123{
124 return get_timer_masked () - base_ticks;
125}
126
127/* delay usec useconds */
Simon Glassdbd79542020-05-10 11:40:11 -0600128void __udelay(unsigned long usec)
Patrick Delaunay9858a602018-10-05 11:33:52 +0200129{
130 ulong tmo, tmp;
131
132 /* Convert to U-Boot ticks */
133 tmo = usec * CONFIG_SYS_HZ;
134 tmo /= (1000000L);
135
136 tmp = get_timer_masked(); /* get current timestamp */
137 tmo += tmp; /* form target timestamp */
138
139 while (get_timer_masked () < tmo) {/* loop till event */
140 /*NOP*/;
141 }
142}
143
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +0200144/*
145 * This function is derived from PowerPC code (read timebase as long long).
146 * On ARM it just returns the timer value.
147 */
148unsigned long long get_ticks(void)
149{
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +0200150 return get_timer(0);
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +0200151}
152
153/*
154 * Return the timebase clock frequency
155 * i.e. how often the timer decrements
156 */
Simon Glassa9dc0682019-12-28 10:44:59 -0700157ulong get_tbclk(void)
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +0200158{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500159 unsigned long long tmp = CFG_SYS_HZ_CLOCK;
Jean-Christophe PLAGNIOL-VILLARD693a7ae2009-05-17 00:58:37 +0200160
161 do_div(tmp, div_clock);
162
163 return tmp;
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +0200164}