2007-05-13 11:58:00 +00:00
|
|
|
/*
|
2009-06-04 10:06:47 +00:00
|
|
|
* (C) Copyright 2009
|
|
|
|
* Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
|
|
|
|
*
|
2012-08-21 04:14:46 +00:00
|
|
|
* (C) Copyright 2007-2012
|
2008-11-20 07:44:42 +00:00
|
|
|
* Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2003
|
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
2007-05-13 11:58:00 +00:00
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2007-05-13 11:58:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2009-06-04 10:06:47 +00:00
|
|
|
#include <div64.h>
|
2008-07-08 03:03:24 +00:00
|
|
|
#include <asm/processor.h>
|
2008-11-20 07:44:42 +00:00
|
|
|
#include <asm/io.h>
|
2012-08-21 04:14:46 +00:00
|
|
|
#include <sh_tmu.h>
|
|
|
|
|
|
|
|
static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
|
2008-11-20 07:44:42 +00:00
|
|
|
|
2012-08-21 04:24:43 +00:00
|
|
|
static u16 bit;
|
2010-06-15 23:10:21 +00:00
|
|
|
static unsigned long last_tcnt;
|
|
|
|
static unsigned long long overflow_ticks;
|
2009-06-04 10:06:47 +00:00
|
|
|
|
2012-08-21 04:24:43 +00:00
|
|
|
unsigned long get_tbclk(void)
|
|
|
|
{
|
|
|
|
return get_tmu0_clk_rate() >> ((bit + 1) * 2);
|
|
|
|
}
|
|
|
|
|
2009-06-04 10:06:47 +00:00
|
|
|
static inline unsigned long long tick_to_time(unsigned long long tick)
|
|
|
|
{
|
|
|
|
tick *= CONFIG_SYS_HZ;
|
2012-08-21 04:24:43 +00:00
|
|
|
do_div(tick, get_tbclk());
|
2009-06-04 10:06:47 +00:00
|
|
|
|
|
|
|
return tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long long usec_to_tick(unsigned long long usec)
|
|
|
|
{
|
2012-08-21 04:24:43 +00:00
|
|
|
usec *= get_tbclk();
|
2009-06-04 10:06:47 +00:00
|
|
|
do_div(usec, 1000000);
|
|
|
|
|
|
|
|
return usec;
|
|
|
|
}
|
2007-05-13 11:58:00 +00:00
|
|
|
|
2012-08-21 04:14:46 +00:00
|
|
|
static void tmu_timer_start(unsigned int timer)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
|
|
|
if (timer > 2)
|
|
|
|
return;
|
2012-08-21 04:14:46 +00:00
|
|
|
writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
|
2008-11-20 07:44:42 +00:00
|
|
|
}
|
2007-05-13 11:58:00 +00:00
|
|
|
|
2012-08-21 04:14:46 +00:00
|
|
|
static void tmu_timer_stop(unsigned int timer)
|
2008-11-20 07:44:42 +00:00
|
|
|
{
|
|
|
|
if (timer > 2)
|
|
|
|
return;
|
2012-08-21 04:14:46 +00:00
|
|
|
writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 04:14:46 +00:00
|
|
|
int timer_init(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
2012-08-21 04:24:43 +00:00
|
|
|
bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
|
2012-08-21 04:14:46 +00:00
|
|
|
writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
|
2008-11-20 07:44:42 +00:00
|
|
|
|
|
|
|
tmu_timer_stop(0);
|
|
|
|
tmu_timer_start(0);
|
2007-05-13 11:58:00 +00:00
|
|
|
|
2010-06-15 23:10:21 +00:00
|
|
|
last_tcnt = 0;
|
|
|
|
overflow_ticks = 0;
|
|
|
|
|
2007-05-13 11:58:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-21 04:14:46 +00:00
|
|
|
unsigned long long get_ticks(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
2012-08-21 04:14:46 +00:00
|
|
|
unsigned long tcnt = 0 - readl(&tmu->tcnt0);
|
2010-06-15 23:10:21 +00:00
|
|
|
|
2012-03-01 04:29:38 +00:00
|
|
|
if (last_tcnt > tcnt) /* overflow */
|
2010-06-15 23:10:21 +00:00
|
|
|
overflow_ticks++;
|
|
|
|
last_tcnt = tcnt;
|
|
|
|
|
|
|
|
return (overflow_ticks << 32) | tcnt;
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 04:14:46 +00:00
|
|
|
void __udelay(unsigned long usec)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
2009-06-04 10:06:47 +00:00
|
|
|
unsigned long long tmp;
|
|
|
|
ulong tmo;
|
|
|
|
|
|
|
|
tmo = usec_to_tick(usec);
|
|
|
|
tmp = get_ticks() + tmo; /* get current timestamp */
|
2008-11-20 07:44:42 +00:00
|
|
|
|
2009-06-04 10:06:47 +00:00
|
|
|
while (get_ticks() < tmp) /* loop till event */
|
|
|
|
/*NOP*/;
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 04:14:46 +00:00
|
|
|
unsigned long get_timer(unsigned long base)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
2008-12-20 14:25:22 +00:00
|
|
|
/* return msec */
|
2009-06-04 10:06:47 +00:00
|
|
|
return tick_to_time(get_ticks()) - base;
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 04:24:43 +00:00
|
|
|
void set_timer(unsigned long t)
|
|
|
|
{
|
|
|
|
writel((0 - t), &tmu->tcnt0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_timer(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
2012-08-21 04:24:43 +00:00
|
|
|
tmu_timer_stop(0);
|
|
|
|
set_timer(0);
|
|
|
|
tmu_timer_start(0);
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|