blob: 2cc61ddcbb55cc3e1ed19b8a68805b5bb17120a7 [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>
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090033#include <asm/io.h>
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090034#include <sh_tmu.h>
35
36static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090037
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090038static u16 bit;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090039static unsigned long last_tcnt;
40static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020041
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090042unsigned long get_tbclk(void)
43{
44 return get_tmu0_clk_rate() >> ((bit + 1) * 2);
45}
46
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020047static inline unsigned long long tick_to_time(unsigned long long tick)
48{
49 tick *= CONFIG_SYS_HZ;
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090050 do_div(tick, get_tbclk());
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020051
52 return tick;
53}
54
55static inline unsigned long long usec_to_tick(unsigned long long usec)
56{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090057 usec *= get_tbclk();
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020058 do_div(usec, 1000000);
59
60 return usec;
61}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090062
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090063static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090064{
65 if (timer > 2)
66 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090067 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090068}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090069
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090070static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090071{
72 if (timer > 2)
73 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090074 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090075}
76
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090077int timer_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090078{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090079 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090080 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090081
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090082 tmu_timer_stop(0);
83 tmu_timer_start(0);
84
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090085 last_tcnt = 0;
86 overflow_ticks = 0;
87
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090088 return 0;
89}
90
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090091unsigned long long get_ticks(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090092{
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090093 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090094
Nobuhiro Iwamatsu78df8c62012-03-01 13:29:38 +090095 if (last_tcnt > tcnt) /* overflow */
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090096 overflow_ticks++;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090097 last_tcnt = tcnt;
98
99 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900100}
101
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900102void __udelay(unsigned long usec)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900103{
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200104 unsigned long long tmp;
105 ulong tmo;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900106
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200107 tmo = usec_to_tick(usec);
108 tmp = get_ticks() + tmo; /* get current timestamp */
109
110 while (get_ticks() < tmp) /* loop till event */
111 /*NOP*/;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900112}
113
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900114unsigned long get_timer(unsigned long base)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900115{
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +0100116 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200117 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900118}
119
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900120void set_timer(unsigned long t)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900121{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900122 writel((0 - t), &tmu->tcnt0);
123}
124
125void reset_timer(void)
126{
127 tmu_timer_stop(0);
128 set_timer(0);
129 tmu_timer_start(0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900130}