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> |
| 9 | |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 10 | /* ------------------------------------------------------------------------- */ |
| 11 | |
| 12 | /* |
| 13 | * This function is intended for SHORT delays only. |
| 14 | * It will overflow at around 10 seconds @ 400MHz, |
| 15 | * or 20 seconds @ 200MHz. |
| 16 | */ |
| 17 | unsigned long usec2ticks(unsigned long usec) |
| 18 | { |
| 19 | ulong ticks; |
| 20 | |
| 21 | if (usec < 1000) { |
| 22 | ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000; |
| 23 | } else { |
| 24 | ticks = ((usec / 10) * (get_tbclk() / 100000)); |
| 25 | } |
| 26 | |
| 27 | return (ticks); |
| 28 | } |
| 29 | |
| 30 | /* ------------------------------------------------------------------------- */ |
| 31 | |
| 32 | /* |
| 33 | * We implement the delay by converting the delay (the number of |
| 34 | * microseconds to wait) into a number of time base ticks; then we |
| 35 | * watch the time base until it has incremented by that amount. |
| 36 | */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 37 | void __udelay(unsigned long usec) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 38 | { |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 39 | ulong ticks = usec2ticks (usec); |
| 40 | wait_ticks (ticks); |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* ------------------------------------------------------------------------- */ |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 44 | #ifndef CONFIG_NAND_SPL |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 45 | unsigned long ticks2usec(unsigned long ticks) |
| 46 | { |
| 47 | ulong tbclk = get_tbclk(); |
| 48 | |
| 49 | /* usec = ticks * 1000000 / tbclk |
| 50 | * Multiplication would overflow at ~4.2e3 ticks, |
| 51 | * so we break it up into |
| 52 | * usec = ( ( ticks * 1000) / tbclk ) * 1000; |
| 53 | */ |
| 54 | ticks *= 1000L; |
| 55 | ticks /= tbclk; |
| 56 | ticks *= 1000L; |
| 57 | |
| 58 | return ((ulong)ticks); |
| 59 | } |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 60 | #endif |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 61 | /* ------------------------------------------------------------------------- */ |
| 62 | |
Simon Glass | 4c974d7 | 2017-03-28 10:27:24 -0600 | [diff] [blame] | 63 | int timer_init(void) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 64 | { |
Timur Tabi | 4705e00 | 2010-12-14 17:18:51 -0600 | [diff] [blame] | 65 | unsigned long temp; |
| 66 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 67 | #if defined(CONFIG_5xx) || defined(CONFIG_8xx) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 68 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 69 | |
| 70 | /* unlock */ |
| 71 | immap->im_sitk.sitk_tbk = KAPWR_KEY; |
| 72 | #endif |
| 73 | |
| 74 | /* reset */ |
Timur Tabi | 4705e00 | 2010-12-14 17:18:51 -0600 | [diff] [blame] | 75 | asm volatile("li %0,0 ; mttbu %0 ; mttbl %0;" |
| 76 | : "=&r"(temp) ); |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 77 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 78 | #if defined(CONFIG_5xx) || defined(CONFIG_8xx) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 79 | /* enable */ |
| 80 | immap->im_sit.sit_tbscr |= TBSCR_TBE; |
| 81 | #endif |
| 82 | return (0); |
| 83 | } |
| 84 | /* ------------------------------------------------------------------------- */ |