blob: 29099612db4f4d46d243eed2546a98d997c6597f [file] [log] [blame]
wdenk1dda0b12002-08-27 10:52:29 +00001/*
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
wdenk1dda0b12002-08-27 10:52:29 +000026/* ------------------------------------------------------------------------- */
27
28/*
29 * This function is intended for SHORT delays only.
30 * It will overflow at around 10 seconds @ 400MHz,
31 * or 20 seconds @ 200MHz.
32 */
33unsigned long usec2ticks(unsigned long usec)
34{
35 ulong ticks;
36
37 if (usec < 1000) {
38 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
39 } else {
40 ticks = ((usec / 10) * (get_tbclk() / 100000));
41 }
42
43 return (ticks);
44}
45
46/* ------------------------------------------------------------------------- */
47
48/*
49 * We implement the delay by converting the delay (the number of
50 * microseconds to wait) into a number of time base ticks; then we
51 * watch the time base until it has incremented by that amount.
52 */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010053void __udelay(unsigned long usec)
wdenk1dda0b12002-08-27 10:52:29 +000054{
Ingo van Lilf0f778a2009-11-24 14:09:21 +010055 ulong ticks = usec2ticks (usec);
56 wait_ticks (ticks);
wdenk1dda0b12002-08-27 10:52:29 +000057}
58
59/* ------------------------------------------------------------------------- */
Scott Woodb71689b2008-06-30 14:13:28 -050060#ifndef CONFIG_NAND_SPL
wdenk1dda0b12002-08-27 10:52:29 +000061unsigned long ticks2usec(unsigned long ticks)
62{
63 ulong tbclk = get_tbclk();
64
65 /* usec = ticks * 1000000 / tbclk
66 * Multiplication would overflow at ~4.2e3 ticks,
67 * so we break it up into
68 * usec = ( ( ticks * 1000) / tbclk ) * 1000;
69 */
70 ticks *= 1000L;
71 ticks /= tbclk;
72 ticks *= 1000L;
73
74 return ((ulong)ticks);
75}
Scott Woodb71689b2008-06-30 14:13:28 -050076#endif
wdenk1dda0b12002-08-27 10:52:29 +000077/* ------------------------------------------------------------------------- */
78
79int init_timebase (void)
80{
wdenk359733b2003-03-31 17:27:09 +000081#if defined(CONFIG_5xx) || defined(CONFIG_8xx)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020082 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
wdenk1dda0b12002-08-27 10:52:29 +000083
84 /* unlock */
85 immap->im_sitk.sitk_tbk = KAPWR_KEY;
86#endif
87
88 /* reset */
89 asm ("li 3,0 ; mttbu 3 ; mttbl 3 ;");
90
wdenk359733b2003-03-31 17:27:09 +000091#if defined(CONFIG_5xx) || defined(CONFIG_8xx)
wdenk1dda0b12002-08-27 10:52:29 +000092 /* enable */
93 immap->im_sit.sit_tbscr |= TBSCR_TBE;
94#endif
95 return (0);
96}
97/* ------------------------------------------------------------------------- */