blob: 0a0e75e726b0e9cb244c68ec2bd10ba0a5491a0d [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
Simon Glass97589732020-05-10 11:40:02 -06007#include <init.h>
Simon Glass45c78902019-11-14 12:57:26 -07008#include <time.h>
Christophe Leroy394f9b32017-07-06 10:33:13 +02009#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
wdenk1dda0b12002-08-27 10:52:29 +000011
wdenk1dda0b12002-08-27 10:52:29 +000012/* ------------------------------------------------------------------------- */
13
14/*
15 * This function is intended for SHORT delays only.
16 * It will overflow at around 10 seconds @ 400MHz,
17 * or 20 seconds @ 200MHz.
18 */
19unsigned long usec2ticks(unsigned long usec)
20{
21 ulong ticks;
22
23 if (usec < 1000) {
24 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
25 } else {
26 ticks = ((usec / 10) * (get_tbclk() / 100000));
27 }
28
29 return (ticks);
30}
31
32/* ------------------------------------------------------------------------- */
33
34/*
35 * We implement the delay by converting the delay (the number of
36 * microseconds to wait) into a number of time base ticks; then we
37 * watch the time base until it has incremented by that amount.
38 */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010039void __udelay(unsigned long usec)
wdenk1dda0b12002-08-27 10:52:29 +000040{
Ingo van Lilf0f778a2009-11-24 14:09:21 +010041 ulong ticks = usec2ticks (usec);
42 wait_ticks (ticks);
wdenk1dda0b12002-08-27 10:52:29 +000043}
44
45/* ------------------------------------------------------------------------- */
Scott Woodb71689b2008-06-30 14:13:28 -050046#ifndef CONFIG_NAND_SPL
wdenk1dda0b12002-08-27 10:52:29 +000047unsigned long ticks2usec(unsigned long ticks)
48{
49 ulong tbclk = get_tbclk();
50
51 /* usec = ticks * 1000000 / tbclk
52 * Multiplication would overflow at ~4.2e3 ticks,
53 * so we break it up into
54 * usec = ( ( ticks * 1000) / tbclk ) * 1000;
55 */
56 ticks *= 1000L;
57 ticks /= tbclk;
58 ticks *= 1000L;
59
60 return ((ulong)ticks);
61}
Scott Woodb71689b2008-06-30 14:13:28 -050062#endif
wdenk1dda0b12002-08-27 10:52:29 +000063/* ------------------------------------------------------------------------- */
64
Simon Glass4c974d72017-03-28 10:27:24 -060065int timer_init(void)
wdenk1dda0b12002-08-27 10:52:29 +000066{
Timur Tabi4705e002010-12-14 17:18:51 -060067 unsigned long temp;
68
wdenk1dda0b12002-08-27 10:52:29 +000069 /* reset */
Christophe Leroy8d652342017-07-13 15:09:50 +020070 asm volatile("li %0,0 ; mttbl %0 ; mttbu %0;"
Timur Tabi4705e002010-12-14 17:18:51 -060071 : "=&r"(temp) );
wdenk1dda0b12002-08-27 10:52:29 +000072
wdenk1dda0b12002-08-27 10:52:29 +000073 return (0);
74}
75/* ------------------------------------------------------------------------- */