blob: 48404727c3dcd91f61625cc39eec709da0f0d905 [file] [log] [blame]
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09001/*
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +02002 * (C) Copyright 2009
3 * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
4 *
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +09005 * (C) Copyright 2007-2012
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +09006 * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
7 *
8 * (C) Copyright 2003
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090010 *
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>
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020031#include <div64.h>
Nobuhiro Iwamatsu9e23fe02008-07-08 12:03:24 +090032#include <asm/processor.h>
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020033#include <asm/clk.h>
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090034#include <asm/io.h>
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090035#include <sh_tmu.h>
36
37static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090038
39#define TMU_MAX_COUNTER (~0UL)
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020040
41static ulong timer_freq;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090042static unsigned long last_tcnt;
43static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020044
45static 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
53static 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}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090060
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090061static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090062{
63 if (timer > 2)
64 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090065 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090066}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090067
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090068static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090069{
70 if (timer > 2)
71 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090072 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090073}
74
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090075int timer_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090076{
Jean-Christophe PLAGNIOL-VILLARDbe45c632009-06-04 12:06:48 +020077 /* Divide clock by CONFIG_SYS_TMU_CLK_DIV */
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090078 u16 bit = 0;
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +010079
Jean-Christophe PLAGNIOL-VILLARDbe45c632009-06-04 12:06:48 +020080 switch (CONFIG_SYS_TMU_CLK_DIV) {
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +010081 case 1024:
82 bit = 4;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090083 break;
84 case 256:
85 bit = 3;
86 break;
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +010087 case 64:
88 bit = 2;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090089 break;
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +010090 case 16:
91 bit = 1;
92 break;
93 case 4:
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090094 default:
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090095 break;
96 }
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090097 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090098
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020099 /* Calc clock rate */
100 timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2);
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900101
102 tmu_timer_stop(0);
103 tmu_timer_start(0);
104
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +0900105 last_tcnt = 0;
106 overflow_ticks = 0;
107
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900108 return 0;
109}
110
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900111unsigned long long get_ticks(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900112{
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900113 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +0900114
Nobuhiro Iwamatsu78df8c62012-03-01 13:29:38 +0900115 if (last_tcnt > tcnt) /* overflow */
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +0900116 overflow_ticks++;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +0900117 last_tcnt = tcnt;
118
119 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900120}
121
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900122void __udelay(unsigned long usec)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900123{
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200124 unsigned long long tmp;
125 ulong tmo;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900126
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200127 tmo = usec_to_tick(usec);
128 tmp = get_ticks() + tmo; /* get current timestamp */
129
130 while (get_ticks() < tmp) /* loop till event */
131 /*NOP*/;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900132}
133
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900134unsigned long get_timer(unsigned long base)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900135{
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +0100136 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200137 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900138}
139
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900140unsigned long get_tbclk(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900141{
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200142 return timer_freq;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900143}