Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000, 2001 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 7 | #include <init.h> |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 8 | #include <time.h> |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 9 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 10 | #include <linux/delay.h> |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 11 | |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 12 | /* ------------------------------------------------------------------------- */ |
| 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 | */ |
| 19 | unsigned 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 Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 39 | void __udelay(unsigned long usec) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 40 | { |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 41 | ulong ticks = usec2ticks (usec); |
| 42 | wait_ticks (ticks); |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* ------------------------------------------------------------------------- */ |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 46 | #ifndef CONFIG_NAND_SPL |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 47 | unsigned 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 Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 62 | #endif |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 63 | /* ------------------------------------------------------------------------- */ |
| 64 | |
Simon Glass | 4c974d7 | 2017-03-28 10:27:24 -0600 | [diff] [blame] | 65 | int timer_init(void) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 66 | { |
Timur Tabi | 4705e00 | 2010-12-14 17:18:51 -0600 | [diff] [blame] | 67 | unsigned long temp; |
| 68 | |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 69 | /* reset */ |
Christophe Leroy | 8d65234 | 2017-07-13 15:09:50 +0200 | [diff] [blame] | 70 | asm volatile("li %0,0 ; mttbl %0 ; mttbu %0;" |
Timur Tabi | 4705e00 | 2010-12-14 17:18:51 -0600 | [diff] [blame] | 71 | : "=&r"(temp) ); |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 72 | |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 73 | return (0); |
| 74 | } |
| 75 | /* ------------------------------------------------------------------------- */ |