blob: 8f83d467920b692d8cce071cfd37b650b02205f7 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090012 */
13
14#include <common.h>
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020015#include <div64.h>
Nobuhiro Iwamatsu9e23fe02008-07-08 12:03:24 +090016#include <asm/processor.h>
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090017#include <asm/io.h>
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090018#include <sh_tmu.h>
19
Nobuhiro Iwamatsu861bd4b2013-07-23 13:57:24 +090020#define TCR_TPSC 0x07
21
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090022static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090023
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090024static u16 bit;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090025static unsigned long last_tcnt;
26static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020027
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090028unsigned long get_tbclk(void)
29{
30 return get_tmu0_clk_rate() >> ((bit + 1) * 2);
31}
32
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020033static inline unsigned long long tick_to_time(unsigned long long tick)
34{
35 tick *= CONFIG_SYS_HZ;
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090036 do_div(tick, get_tbclk());
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020037
38 return tick;
39}
40
41static inline unsigned long long usec_to_tick(unsigned long long usec)
42{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090043 usec *= get_tbclk();
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020044 do_div(usec, 1000000);
45
46 return usec;
47}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090048
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090049static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090050{
51 if (timer > 2)
52 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090053 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090054}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090055
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090056static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090057{
58 if (timer > 2)
59 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090060 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090061}
62
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090063int timer_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090064{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090065 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
Nobuhiro Iwamatsu861bd4b2013-07-23 13:57:24 +090066 writew((readw(&tmu->tcr0) & ~TCR_TPSC) | bit, &tmu->tcr0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090067
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090068 tmu_timer_stop(0);
69 tmu_timer_start(0);
70
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090071 last_tcnt = 0;
72 overflow_ticks = 0;
73
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090074 return 0;
75}
76
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090077unsigned long long get_ticks(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090078{
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090079 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090080
Nobuhiro Iwamatsu78df8c62012-03-01 13:29:38 +090081 if (last_tcnt > tcnt) /* overflow */
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090082 overflow_ticks++;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090083 last_tcnt = tcnt;
84
85 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090086}
87
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090088void __udelay(unsigned long usec)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090089{
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020090 unsigned long long tmp;
91 ulong tmo;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090092
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020093 tmo = usec_to_tick(usec);
94 tmp = get_ticks() + tmo; /* get current timestamp */
95
96 while (get_ticks() < tmp) /* loop till event */
97 /*NOP*/;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090098}
99
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +0900100unsigned long get_timer(unsigned long base)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900101{
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +0100102 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200103 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900104}
105
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900106void set_timer(unsigned long t)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900107{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900108 writel((0 - t), &tmu->tcnt0);
109}
110
111void reset_timer(void)
112{
113 tmu_timer_stop(0);
114 set_timer(0);
115 tmu_timer_start(0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900116}