wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000, 2001 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 9 | #include <asm/io.h> |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 10 | |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 11 | /* ------------------------------------------------------------------------- */ |
| 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 | */ |
| 18 | unsigned 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 Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 38 | void __udelay(unsigned long usec) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 39 | { |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 40 | ulong ticks = usec2ticks (usec); |
| 41 | wait_ticks (ticks); |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* ------------------------------------------------------------------------- */ |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 45 | #ifndef CONFIG_NAND_SPL |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 46 | unsigned 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 Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 61 | #endif |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 62 | /* ------------------------------------------------------------------------- */ |
| 63 | |
Simon Glass | 4c974d7 | 2017-03-28 10:27:24 -0600 | [diff] [blame] | 64 | int timer_init(void) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 65 | { |
Timur Tabi | 4705e00 | 2010-12-14 17:18:51 -0600 | [diff] [blame] | 66 | unsigned long temp; |
| 67 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 68 | #if defined(CONFIG_8xx) |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 69 | immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 70 | |
| 71 | /* unlock */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 72 | out_be32(&immap->im_sitk.sitk_tbk, KAPWR_KEY); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 73 | #endif |
| 74 | |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 75 | /* reset */ |
Timur Tabi | 4705e00 | 2010-12-14 17:18:51 -0600 | [diff] [blame] | 76 | asm volatile("li %0,0 ; mttbu %0 ; mttbl %0;" |
| 77 | : "=&r"(temp) ); |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 78 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 79 | #if defined(CONFIG_8xx) |
| 80 | /* enable */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 81 | setbits_be16(&immap->im_sit.sit_tbscr, TBSCR_TBE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 82 | #endif |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 83 | return (0); |
| 84 | } |
| 85 | /* ------------------------------------------------------------------------- */ |