blob: a22a73abfa5a7dbc2f040854b5b45af0567ecfe7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk1dda0b12002-08-27 10:52:29 +00002/*
3 * (C) Copyright 2000, 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk1dda0b12002-08-27 10:52:29 +00005 */
6
7#include <common.h>
Christophe Leroy394f9b32017-07-06 10:33:13 +02008#include <asm/io.h>
wdenk1dda0b12002-08-27 10:52:29 +00009
wdenk1dda0b12002-08-27 10:52:29 +000010/* ------------------------------------------------------------------------- */
11
12/*
13 * This function is intended for SHORT delays only.
14 * It will overflow at around 10 seconds @ 400MHz,
15 * or 20 seconds @ 200MHz.
16 */
17unsigned long usec2ticks(unsigned long usec)
18{
19 ulong ticks;
20
21 if (usec < 1000) {
22 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
23 } else {
24 ticks = ((usec / 10) * (get_tbclk() / 100000));
25 }
26
27 return (ticks);
28}
29
30/* ------------------------------------------------------------------------- */
31
32/*
33 * We implement the delay by converting the delay (the number of
34 * microseconds to wait) into a number of time base ticks; then we
35 * watch the time base until it has incremented by that amount.
36 */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010037void __udelay(unsigned long usec)
wdenk1dda0b12002-08-27 10:52:29 +000038{
Ingo van Lilf0f778a2009-11-24 14:09:21 +010039 ulong ticks = usec2ticks (usec);
40 wait_ticks (ticks);
wdenk1dda0b12002-08-27 10:52:29 +000041}
42
43/* ------------------------------------------------------------------------- */
Scott Woodb71689b2008-06-30 14:13:28 -050044#ifndef CONFIG_NAND_SPL
wdenk1dda0b12002-08-27 10:52:29 +000045unsigned long ticks2usec(unsigned long ticks)
46{
47 ulong tbclk = get_tbclk();
48
49 /* usec = ticks * 1000000 / tbclk
50 * Multiplication would overflow at ~4.2e3 ticks,
51 * so we break it up into
52 * usec = ( ( ticks * 1000) / tbclk ) * 1000;
53 */
54 ticks *= 1000L;
55 ticks /= tbclk;
56 ticks *= 1000L;
57
58 return ((ulong)ticks);
59}
Scott Woodb71689b2008-06-30 14:13:28 -050060#endif
wdenk1dda0b12002-08-27 10:52:29 +000061/* ------------------------------------------------------------------------- */
62
Simon Glass4c974d72017-03-28 10:27:24 -060063int timer_init(void)
wdenk1dda0b12002-08-27 10:52:29 +000064{
Timur Tabi4705e002010-12-14 17:18:51 -060065 unsigned long temp;
66
wdenk1dda0b12002-08-27 10:52:29 +000067 /* reset */
Christophe Leroy8d652342017-07-13 15:09:50 +020068 asm volatile("li %0,0 ; mttbl %0 ; mttbu %0;"
Timur Tabi4705e002010-12-14 17:18:51 -060069 : "=&r"(temp) );
wdenk1dda0b12002-08-27 10:52:29 +000070
wdenk1dda0b12002-08-27 10:52:29 +000071 return (0);
72}
73/* ------------------------------------------------------------------------- */