blob: 00230c3298013dfdefe186f44f3e30379298dfb7 [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
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090039static u16 bit;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090040static unsigned long last_tcnt;
41static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020042
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090043unsigned long get_tbclk(void)
44{
45 return get_tmu0_clk_rate() >> ((bit + 1) * 2);
46}
47
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020048static inline unsigned long long tick_to_time(unsigned long long tick)
49{
50 tick *= CONFIG_SYS_HZ;
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090051 do_div(tick, get_tbclk());
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020052
53 return tick;
54}
55
56static inline unsigned long long usec_to_tick(unsigned long long usec)
57{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090058 usec *= get_tbclk();
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020059 do_div(usec, 1000000);
60
61 return usec;
62}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090063
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090064static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090065{
66 if (timer > 2)
67 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090068 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090069}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090070
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090071static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090072{
73 if (timer > 2)
74 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090075 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090076}
77
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090078int timer_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090079{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090080 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090081 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090082
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090083 tmu_timer_stop(0);
84 tmu_timer_start(0);
85
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090086 last_tcnt = 0;
87 overflow_ticks = 0;
88
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090089 return 0;
90}
91
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090092unsigned long long get_ticks(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090093{
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090094 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090095
Nobuhiro Iwamatsu78df8c62012-03-01 13:29:38 +090096 if (last_tcnt > tcnt) /* overflow */
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090097 overflow_ticks++;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090098 last_tcnt = tcnt;
99
100 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900101}
102
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900103void __udelay(unsigned long usec)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900104{
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200105 unsigned long long tmp;
106 ulong tmo;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900107
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200108 tmo = usec_to_tick(usec);
109 tmp = get_ticks() + tmo; /* get current timestamp */
110
111 while (get_ticks() < tmp) /* loop till event */
112 /*NOP*/;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900113}
114
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900115unsigned long get_timer(unsigned long base)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900116{
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +0100117 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200118 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900119}
120
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900121void set_timer(unsigned long t)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900122{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900123 writel((0 - t), &tmu->tcnt0);
124}
125
126void reset_timer(void)
127{
128 tmu_timer_stop(0);
129 set_timer(0);
130 tmu_timer_start(0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900131}