blob: 2cc61ddcbb55cc3e1ed19b8a68805b5bb17120a7 [file] [log] [blame]
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09001/*
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +02002 * (C) Copyright 2009
3 * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
4 *
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +09005 * (C) Copyright 2007-2012
Nobuhiro Iwamatsuac890472008-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 Iwamatsu970dc332007-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-VILLARD51704102009-06-04 12:06:47 +020031#include <div64.h>
Nobuhiro Iwamatsu01213252008-07-08 12:03:24 +090032#include <asm/processor.h>
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090033#include <asm/io.h>
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090034#include <sh_tmu.h>
35
36static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090037
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090038static u16 bit;
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090039static unsigned long last_tcnt;
40static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020041
Nobuhiro Iwamatsuf7be78e2012-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-VILLARD51704102009-06-04 12:06:47 +020047static inline unsigned long long tick_to_time(unsigned long long tick)
48{
49 tick *= CONFIG_SYS_HZ;
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090050 do_div(tick, get_tbclk());
Jean-Christophe PLAGNIOL-VILLARD51704102009-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 Iwamatsuf7be78e2012-08-21 13:24:43 +090057 usec *= get_tbclk();
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020058 do_div(usec, 1000000);
59
60 return usec;
61}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090062
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090063static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090064{
65 if (timer > 2)
66 return;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090067 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090068}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090069
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090070static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090071{
72 if (timer > 2)
73 return;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090074 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090075}
76
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090077int timer_init(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090078{
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090079 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090080 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090081
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090082 tmu_timer_stop(0);
83 tmu_timer_start(0);
84
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090085 last_tcnt = 0;
86 overflow_ticks = 0;
87
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090088 return 0;
89}
90
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090091unsigned long long get_ticks(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090092{
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090093 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090094
Nobuhiro Iwamatsu51fbfb92012-03-01 13:29:38 +090095 if (last_tcnt > tcnt) /* overflow */
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090096 overflow_ticks++;
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090097 last_tcnt = tcnt;
98
99 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900100}
101
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +0900102void __udelay(unsigned long usec)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900103{
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +0200104 unsigned long long tmp;
105 ulong tmo;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +0900106
Jean-Christophe PLAGNIOL-VILLARD51704102009-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 Iwamatsu970dc332007-05-13 20:58:00 +0900112}
113
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +0900114unsigned long get_timer(unsigned long base)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900115{
Jean-Christophe PLAGNIOL-VILLARD4b137182008-12-20 15:25:22 +0100116 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +0200117 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900118}
119
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +0900120void set_timer(unsigned long t)
121{
122 writel((0 - t), &tmu->tcnt0);
123}
124
125void reset_timer(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900126{
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +0900127 tmu_timer_stop(0);
128 set_timer(0);
129 tmu_timer_start(0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900130}