xref: /rk3399_rockchip-uboot/arch/sh/lib/time.c (revision 73f35e0b156c771333d449ca86b99600d094d37f)
1 /*
2  * (C) Copyright 2009
3  * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
4  *
5  * (C) Copyright 2007-2012
6  * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
7  *
8  * (C) Copyright 2003
9  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10  *
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27  * MA 02111-1307 USA
28  */
29 
30 #include <common.h>
31 #include <div64.h>
32 #include <asm/processor.h>
33 #include <asm/clk.h>
34 #include <asm/io.h>
35 #include <sh_tmu.h>
36 
37 static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
38 
39 #define TMU_MAX_COUNTER (~0UL)
40 
41 static ulong timer_freq;
42 static unsigned long last_tcnt;
43 static unsigned long long overflow_ticks;
44 
45 static inline unsigned long long tick_to_time(unsigned long long tick)
46 {
47 	tick *= CONFIG_SYS_HZ;
48 	do_div(tick, timer_freq);
49 
50 	return tick;
51 }
52 
53 static inline unsigned long long usec_to_tick(unsigned long long usec)
54 {
55 	usec *= timer_freq;
56 	do_div(usec, 1000000);
57 
58 	return usec;
59 }
60 
61 static void tmu_timer_start(unsigned int timer)
62 {
63 	if (timer > 2)
64 		return;
65 	writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
66 }
67 
68 static void tmu_timer_stop(unsigned int timer)
69 {
70 	if (timer > 2)
71 		return;
72 	writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
73 }
74 
75 int timer_init(void)
76 {
77 	/* Divide clock by CONFIG_SYS_TMU_CLK_DIV */
78 	u16 bit = 0;
79 
80 	switch (CONFIG_SYS_TMU_CLK_DIV) {
81 	case 1024:
82 		bit = 4;
83 		break;
84 	case 256:
85 		bit = 3;
86 		break;
87 	case 64:
88 		bit = 2;
89 		break;
90 	case 16:
91 		bit = 1;
92 		break;
93 	case 4:
94 	default:
95 		break;
96 	}
97 	writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
98 
99 	/* Calc clock rate */
100 	timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2);
101 
102 	tmu_timer_stop(0);
103 	tmu_timer_start(0);
104 
105 	last_tcnt = 0;
106 	overflow_ticks = 0;
107 
108 	return 0;
109 }
110 
111 unsigned long long get_ticks(void)
112 {
113 	unsigned long tcnt = 0 - readl(&tmu->tcnt0);
114 
115 	if (last_tcnt > tcnt) /* overflow */
116 		overflow_ticks++;
117 	last_tcnt = tcnt;
118 
119 	return (overflow_ticks << 32) | tcnt;
120 }
121 
122 void __udelay(unsigned long usec)
123 {
124 	unsigned long long tmp;
125 	ulong tmo;
126 
127 	tmo = usec_to_tick(usec);
128 	tmp = get_ticks() + tmo;	/* get current timestamp */
129 
130 	while (get_ticks() < tmp)	/* loop till event */
131 		 /*NOP*/;
132 }
133 
134 unsigned long get_timer(unsigned long base)
135 {
136 	/* return msec */
137 	return tick_to_time(get_ticks()) - base;
138 }
139 
140 unsigned long get_tbclk(void)
141 {
142 	return timer_freq;
143 }
144