blob: e1494fa1292772945b059d94db53d93596800f46 [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>
Simon Glass45c78902019-11-14 12:57:26 -07008#include <time.h>
Christophe Leroy394f9b32017-07-06 10:33:13 +02009#include <asm/io.h>
wdenk1dda0b12002-08-27 10:52:29 +000010
wdenk1dda0b12002-08-27 10:52:29 +000011/* ------------------------------------------------------------------------- */
12
13/*
14 * This function is intended for SHORT delays only.
15 * It will overflow at around 10 seconds @ 400MHz,
16 * or 20 seconds @ 200MHz.
17 */
18unsigned long usec2ticks(unsigned long usec)
19{
20 ulong ticks;
21
22 if (usec < 1000) {
23 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
24 } else {
25 ticks = ((usec / 10) * (get_tbclk() / 100000));
26 }
27
28 return (ticks);
29}
30
31/* ------------------------------------------------------------------------- */
32
33/*
34 * We implement the delay by converting the delay (the number of
35 * microseconds to wait) into a number of time base ticks; then we
36 * watch the time base until it has incremented by that amount.
37 */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010038void __udelay(unsigned long usec)
wdenk1dda0b12002-08-27 10:52:29 +000039{
Ingo van Lilf0f778a2009-11-24 14:09:21 +010040 ulong ticks = usec2ticks (usec);
41 wait_ticks (ticks);
wdenk1dda0b12002-08-27 10:52:29 +000042}
43
44/* ------------------------------------------------------------------------- */
Scott Woodb71689b2008-06-30 14:13:28 -050045#ifndef CONFIG_NAND_SPL
wdenk1dda0b12002-08-27 10:52:29 +000046unsigned long ticks2usec(unsigned long ticks)
47{
48 ulong tbclk = get_tbclk();
49
50 /* usec = ticks * 1000000 / tbclk
51 * Multiplication would overflow at ~4.2e3 ticks,
52 * so we break it up into
53 * usec = ( ( ticks * 1000) / tbclk ) * 1000;
54 */
55 ticks *= 1000L;
56 ticks /= tbclk;
57 ticks *= 1000L;
58
59 return ((ulong)ticks);
60}
Scott Woodb71689b2008-06-30 14:13:28 -050061#endif
wdenk1dda0b12002-08-27 10:52:29 +000062/* ------------------------------------------------------------------------- */
63
Simon Glass4c974d72017-03-28 10:27:24 -060064int timer_init(void)
wdenk1dda0b12002-08-27 10:52:29 +000065{
Timur Tabi4705e002010-12-14 17:18:51 -060066 unsigned long temp;
67
wdenk1dda0b12002-08-27 10:52:29 +000068 /* reset */
Christophe Leroy8d652342017-07-13 15:09:50 +020069 asm volatile("li %0,0 ; mttbl %0 ; mttbu %0;"
Timur Tabi4705e002010-12-14 17:18:51 -060070 : "=&r"(temp) );
wdenk1dda0b12002-08-27 10:52:29 +000071
wdenk1dda0b12002-08-27 10:52:29 +000072 return (0);
73}
74/* ------------------------------------------------------------------------- */