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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | |
Yuri Tikhonov | 787f8fc | 2008-02-21 14:23:42 +0100 | [diff] [blame] | 26 | #ifndef CONFIG_WD_PERIOD |
| 27 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default*/ |
| 28 | #endif |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 29 | |
| 30 | /* ------------------------------------------------------------------------- */ |
| 31 | |
| 32 | /* |
| 33 | * This function is intended for SHORT delays only. |
| 34 | * It will overflow at around 10 seconds @ 400MHz, |
| 35 | * or 20 seconds @ 200MHz. |
| 36 | */ |
| 37 | unsigned long usec2ticks(unsigned long usec) |
| 38 | { |
| 39 | ulong ticks; |
| 40 | |
| 41 | if (usec < 1000) { |
| 42 | ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000; |
| 43 | } else { |
| 44 | ticks = ((usec / 10) * (get_tbclk() / 100000)); |
| 45 | } |
| 46 | |
| 47 | return (ticks); |
| 48 | } |
| 49 | |
| 50 | /* ------------------------------------------------------------------------- */ |
| 51 | |
| 52 | /* |
| 53 | * We implement the delay by converting the delay (the number of |
| 54 | * microseconds to wait) into a number of time base ticks; then we |
| 55 | * watch the time base until it has incremented by that amount. |
| 56 | */ |
| 57 | void udelay(unsigned long usec) |
| 58 | { |
Yuri Tikhonov | 787f8fc | 2008-02-21 14:23:42 +0100 | [diff] [blame] | 59 | ulong ticks, kv; |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 60 | |
Yuri Tikhonov | 787f8fc | 2008-02-21 14:23:42 +0100 | [diff] [blame] | 61 | do { |
| 62 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; |
| 63 | ticks = usec2ticks (kv); |
| 64 | wait_ticks (ticks); |
| 65 | usec -= kv; |
| 66 | } while(usec); |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* ------------------------------------------------------------------------- */ |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 70 | #ifndef CONFIG_NAND_SPL |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 71 | unsigned long ticks2usec(unsigned long ticks) |
| 72 | { |
| 73 | ulong tbclk = get_tbclk(); |
| 74 | |
| 75 | /* usec = ticks * 1000000 / tbclk |
| 76 | * Multiplication would overflow at ~4.2e3 ticks, |
| 77 | * so we break it up into |
| 78 | * usec = ( ( ticks * 1000) / tbclk ) * 1000; |
| 79 | */ |
| 80 | ticks *= 1000L; |
| 81 | ticks /= tbclk; |
| 82 | ticks *= 1000L; |
| 83 | |
| 84 | return ((ulong)ticks); |
| 85 | } |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 86 | #endif |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 87 | /* ------------------------------------------------------------------------- */ |
| 88 | |
| 89 | int init_timebase (void) |
| 90 | { |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 91 | #if defined(CONFIG_5xx) || defined(CONFIG_8xx) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 92 | volatile immap_t *immap = (immap_t *) CFG_IMMR; |
| 93 | |
| 94 | /* unlock */ |
| 95 | immap->im_sitk.sitk_tbk = KAPWR_KEY; |
| 96 | #endif |
| 97 | |
| 98 | /* reset */ |
| 99 | asm ("li 3,0 ; mttbu 3 ; mttbl 3 ;"); |
| 100 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 101 | #if defined(CONFIG_5xx) || defined(CONFIG_8xx) |
wdenk | 1dda0b1 | 2002-08-27 10:52:29 +0000 | [diff] [blame] | 102 | /* enable */ |
| 103 | immap->im_sit.sit_tbscr |= TBSCR_TBE; |
| 104 | #endif |
| 105 | return (0); |
| 106 | } |
| 107 | /* ------------------------------------------------------------------------- */ |