blob: 8d6babfb83d6edd71785651325fbd2d44836b511 [file] [log] [blame]
Tom Rini83d290c2018-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 Glass691d7192020-05-10 11:40:02 -06008#include <init.h>
Simon Glass6887c5b2019-11-14 12:57:26 -07009#include <time.h>
Christophe Leroyba3da732017-07-06 10:33:13 +020010#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060011#include <linux/delay.h>
wdenk1dda0b12002-08-27 10:52:29 +000012
wdenk1dda0b12002-08-27 10:52:29 +000013/* ------------------------------------------------------------------------- */
14
15/*
16 * This function is intended for SHORT delays only.
17 * It will overflow at around 10 seconds @ 400MHz,
18 * or 20 seconds @ 200MHz.
19 */
20unsigned long usec2ticks(unsigned long usec)
21{
22 ulong ticks;
23
24 if (usec < 1000) {
25 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
26 } else {
27 ticks = ((usec / 10) * (get_tbclk() / 100000));
28 }
29
30 return (ticks);
31}
32
33/* ------------------------------------------------------------------------- */
34
35/*
36 * We implement the delay by converting the delay (the number of
37 * microseconds to wait) into a number of time base ticks; then we
38 * watch the time base until it has incremented by that amount.
39 */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010040void __udelay(unsigned long usec)
wdenk1dda0b12002-08-27 10:52:29 +000041{
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010042 ulong ticks = usec2ticks (usec);
43 wait_ticks (ticks);
wdenk1dda0b12002-08-27 10:52:29 +000044}
45
46/* ------------------------------------------------------------------------- */
Scott Woode4c09502008-06-30 14:13:28 -050047#ifndef CONFIG_NAND_SPL
wdenk1dda0b12002-08-27 10:52:29 +000048unsigned long ticks2usec(unsigned long ticks)
49{
50 ulong tbclk = get_tbclk();
51
52 /* usec = ticks * 1000000 / tbclk
53 * Multiplication would overflow at ~4.2e3 ticks,
54 * so we break it up into
55 * usec = ( ( ticks * 1000) / tbclk ) * 1000;
56 */
57 ticks *= 1000L;
58 ticks /= tbclk;
59 ticks *= 1000L;
60
61 return ((ulong)ticks);
62}
Scott Woode4c09502008-06-30 14:13:28 -050063#endif
wdenk1dda0b12002-08-27 10:52:29 +000064/* ------------------------------------------------------------------------- */
65
Simon Glass70e2aaf2017-03-28 10:27:24 -060066int timer_init(void)
wdenk1dda0b12002-08-27 10:52:29 +000067{
Timur Tabi96805a52010-12-14 17:18:51 -060068 unsigned long temp;
69
wdenk1dda0b12002-08-27 10:52:29 +000070 /* reset */
Christophe Leroyf0eda3c2017-07-13 15:09:50 +020071 asm volatile("li %0,0 ; mttbl %0 ; mttbu %0;"
Timur Tabi96805a52010-12-14 17:18:51 -060072 : "=&r"(temp) );
wdenk1dda0b12002-08-27 10:52:29 +000073
wdenk1dda0b12002-08-27 10:52:29 +000074 return (0);
75}
76/* ------------------------------------------------------------------------- */