xref: /rk3399_rockchip-uboot/arch/powerpc/lib/time.c (revision a47a12becf66f02a56da91c161e2edb625e9f20c)
1*a47a12beSStefan Roese /*
2*a47a12beSStefan Roese  * (C) Copyright 2000, 2001
3*a47a12beSStefan Roese  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*a47a12beSStefan Roese  *
5*a47a12beSStefan Roese  * See file CREDITS for list of people who contributed to this
6*a47a12beSStefan Roese  * project.
7*a47a12beSStefan Roese  *
8*a47a12beSStefan Roese  * This program is free software; you can redistribute it and/or
9*a47a12beSStefan Roese  * modify it under the terms of the GNU General Public License as
10*a47a12beSStefan Roese  * published by the Free Software Foundation; either version 2 of
11*a47a12beSStefan Roese  * the License, or (at your option) any later version.
12*a47a12beSStefan Roese  *
13*a47a12beSStefan Roese  * This program is distributed in the hope that it will be useful,
14*a47a12beSStefan Roese  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a47a12beSStefan Roese  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*a47a12beSStefan Roese  * GNU General Public License for more details.
17*a47a12beSStefan Roese  *
18*a47a12beSStefan Roese  * You should have received a copy of the GNU General Public License
19*a47a12beSStefan Roese  * along with this program; if not, write to the Free Software
20*a47a12beSStefan Roese  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21*a47a12beSStefan Roese  * MA 02111-1307 USA
22*a47a12beSStefan Roese  */
23*a47a12beSStefan Roese 
24*a47a12beSStefan Roese #include <common.h>
25*a47a12beSStefan Roese 
26*a47a12beSStefan Roese /* ------------------------------------------------------------------------- */
27*a47a12beSStefan Roese 
28*a47a12beSStefan Roese /*
29*a47a12beSStefan Roese  * This function is intended for SHORT delays only.
30*a47a12beSStefan Roese  * It will overflow at around 10 seconds @ 400MHz,
31*a47a12beSStefan Roese  * or 20 seconds @ 200MHz.
32*a47a12beSStefan Roese  */
33*a47a12beSStefan Roese unsigned long usec2ticks(unsigned long usec)
34*a47a12beSStefan Roese {
35*a47a12beSStefan Roese 	ulong ticks;
36*a47a12beSStefan Roese 
37*a47a12beSStefan Roese 	if (usec < 1000) {
38*a47a12beSStefan Roese 		ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
39*a47a12beSStefan Roese 	} else {
40*a47a12beSStefan Roese 		ticks = ((usec / 10) * (get_tbclk() / 100000));
41*a47a12beSStefan Roese 	}
42*a47a12beSStefan Roese 
43*a47a12beSStefan Roese 	return (ticks);
44*a47a12beSStefan Roese }
45*a47a12beSStefan Roese 
46*a47a12beSStefan Roese /* ------------------------------------------------------------------------- */
47*a47a12beSStefan Roese 
48*a47a12beSStefan Roese /*
49*a47a12beSStefan Roese  * We implement the delay by converting the delay (the number of
50*a47a12beSStefan Roese  * microseconds to wait) into a number of time base ticks; then we
51*a47a12beSStefan Roese  * watch the time base until it has incremented by that amount.
52*a47a12beSStefan Roese  */
53*a47a12beSStefan Roese void __udelay(unsigned long usec)
54*a47a12beSStefan Roese {
55*a47a12beSStefan Roese 	ulong ticks = usec2ticks (usec);
56*a47a12beSStefan Roese 	wait_ticks (ticks);
57*a47a12beSStefan Roese }
58*a47a12beSStefan Roese 
59*a47a12beSStefan Roese /* ------------------------------------------------------------------------- */
60*a47a12beSStefan Roese #ifndef CONFIG_NAND_SPL
61*a47a12beSStefan Roese unsigned long ticks2usec(unsigned long ticks)
62*a47a12beSStefan Roese {
63*a47a12beSStefan Roese 	ulong tbclk = get_tbclk();
64*a47a12beSStefan Roese 
65*a47a12beSStefan Roese 	/* usec = ticks * 1000000 / tbclk
66*a47a12beSStefan Roese 	 * Multiplication would overflow at ~4.2e3 ticks,
67*a47a12beSStefan Roese 	 * so we break it up into
68*a47a12beSStefan Roese 	 * usec = ( ( ticks * 1000) / tbclk ) * 1000;
69*a47a12beSStefan Roese 	 */
70*a47a12beSStefan Roese 	ticks *= 1000L;
71*a47a12beSStefan Roese 	ticks /= tbclk;
72*a47a12beSStefan Roese 	ticks *= 1000L;
73*a47a12beSStefan Roese 
74*a47a12beSStefan Roese 	return ((ulong)ticks);
75*a47a12beSStefan Roese }
76*a47a12beSStefan Roese #endif
77*a47a12beSStefan Roese /* ------------------------------------------------------------------------- */
78*a47a12beSStefan Roese 
79*a47a12beSStefan Roese int init_timebase (void)
80*a47a12beSStefan Roese {
81*a47a12beSStefan Roese #if defined(CONFIG_5xx) || defined(CONFIG_8xx)
82*a47a12beSStefan Roese 	volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
83*a47a12beSStefan Roese 
84*a47a12beSStefan Roese 	/* unlock */
85*a47a12beSStefan Roese 	immap->im_sitk.sitk_tbk = KAPWR_KEY;
86*a47a12beSStefan Roese #endif
87*a47a12beSStefan Roese 
88*a47a12beSStefan Roese 	/* reset */
89*a47a12beSStefan Roese 	asm ("li 3,0 ; mttbu 3 ; mttbl 3 ;");
90*a47a12beSStefan Roese 
91*a47a12beSStefan Roese #if defined(CONFIG_5xx) || defined(CONFIG_8xx)
92*a47a12beSStefan Roese 	/* enable */
93*a47a12beSStefan Roese 	immap->im_sit.sit_tbscr |= TBSCR_TBE;
94*a47a12beSStefan Roese #endif
95*a47a12beSStefan Roese 	return (0);
96*a47a12beSStefan Roese }
97*a47a12beSStefan Roese /* ------------------------------------------------------------------------- */
98