blob: 1fe537e83bcb2d1f44f71458f3f8cbb0d3a6399d [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
20static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090021
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090022static u16 bit;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090023static unsigned long last_tcnt;
24static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020025
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090026unsigned long get_tbclk(void)
27{
28 return get_tmu0_clk_rate() >> ((bit + 1) * 2);
29}
30
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020031static inline unsigned long long tick_to_time(unsigned long long tick)
32{
33 tick *= CONFIG_SYS_HZ;
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090034 do_div(tick, get_tbclk());
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020035
36 return tick;
37}
38
39static inline unsigned long long usec_to_tick(unsigned long long usec)
40{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090041 usec *= get_tbclk();
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020042 do_div(usec, 1000000);
43
44 return usec;
45}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090046
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090047static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090048{
49 if (timer > 2)
50 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090051 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090052}
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090053
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090054static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090055{
56 if (timer > 2)
57 return;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090058 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090059}
60
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090061int timer_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090062{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +090063 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090064 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090065
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090066 tmu_timer_stop(0);
67 tmu_timer_start(0);
68
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090069 last_tcnt = 0;
70 overflow_ticks = 0;
71
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090072 return 0;
73}
74
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090075unsigned long long get_ticks(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090076{
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090077 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090078
Nobuhiro Iwamatsu78df8c62012-03-01 13:29:38 +090079 if (last_tcnt > tcnt) /* overflow */
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090080 overflow_ticks++;
Nobuhiro Iwamatsu61973af2010-06-16 08:10:21 +090081 last_tcnt = tcnt;
82
83 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090084}
85
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090086void __udelay(unsigned long usec)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090087{
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020088 unsigned long long tmp;
89 ulong tmo;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090090
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +020091 tmo = usec_to_tick(usec);
92 tmp = get_ticks() + tmo; /* get current timestamp */
93
94 while (get_ticks() < tmp) /* loop till event */
95 /*NOP*/;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090096}
97
Nobuhiro Iwamatsu73f35e02012-08-21 13:14:46 +090098unsigned long get_timer(unsigned long base)
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +090099{
Jean-Christophe PLAGNIOL-VILLARD1e15ff92008-12-20 15:25:22 +0100100 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD8dd29c82009-06-04 12:06:47 +0200101 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsue9d5f352008-11-20 16:44:42 +0900102}
103
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900104void set_timer(unsigned long t)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900105{
Nobuhiro Iwamatsud4430422012-08-21 13:24:43 +0900106 writel((0 - t), &tmu->tcnt0);
107}
108
109void reset_timer(void)
110{
111 tmu_timer_stop(0);
112 set_timer(0);
113 tmu_timer_start(0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900114}