blob: 1fe537e83bcb2d1f44f71458f3f8cbb0d3a6399d [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090012 */
13
14#include <common.h>
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020015#include <div64.h>
Nobuhiro Iwamatsu01213252008-07-08 12:03:24 +090016#include <asm/processor.h>
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090017#include <asm/io.h>
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090018#include <sh_tmu.h>
19
20static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090021
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090022static u16 bit;
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090023static unsigned long last_tcnt;
24static unsigned long long overflow_ticks;
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020025
Nobuhiro Iwamatsuf7be78e2012-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-VILLARD51704102009-06-04 12:06:47 +020031static inline unsigned long long tick_to_time(unsigned long long tick)
32{
33 tick *= CONFIG_SYS_HZ;
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090034 do_div(tick, get_tbclk());
Jean-Christophe PLAGNIOL-VILLARD51704102009-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 Iwamatsuf7be78e2012-08-21 13:24:43 +090041 usec *= get_tbclk();
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020042 do_div(usec, 1000000);
43
44 return usec;
45}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090046
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090047static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090048{
49 if (timer > 2)
50 return;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090051 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090052}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090053
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090054static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090055{
56 if (timer > 2)
57 return;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090058 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090059}
60
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090061int timer_init(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090062{
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090063 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090064 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090065
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090066 tmu_timer_stop(0);
67 tmu_timer_start(0);
68
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090069 last_tcnt = 0;
70 overflow_ticks = 0;
71
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090072 return 0;
73}
74
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090075unsigned long long get_ticks(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090076{
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090077 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090078
Nobuhiro Iwamatsu51fbfb92012-03-01 13:29:38 +090079 if (last_tcnt > tcnt) /* overflow */
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090080 overflow_ticks++;
Nobuhiro Iwamatsub633dd12010-06-16 08:10:21 +090081 last_tcnt = tcnt;
82
83 return (overflow_ticks << 32) | tcnt;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090084}
85
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090086void __udelay(unsigned long usec)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090087{
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020088 unsigned long long tmp;
89 ulong tmo;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090090
Jean-Christophe PLAGNIOL-VILLARD51704102009-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 Iwamatsu970dc332007-05-13 20:58:00 +090096}
97
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090098unsigned long get_timer(unsigned long base)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090099{
Jean-Christophe PLAGNIOL-VILLARD4b137182008-12-20 15:25:22 +0100100 /* return msec */
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +0200101 return tick_to_time(get_ticks()) - base;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900102}
103
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +0900104void set_timer(unsigned long t)
105{
106 writel((0 - t), &tmu->tcnt0);
107}
108
109void reset_timer(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900110{
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +0900111 tmu_timer_stop(0);
112 set_timer(0);
113 tmu_timer_start(0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900114}