blob: d970a1e4f02c480dd3b665b185bed700f6c4bc1f [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>
Nobuhiro Iwamatsu01213252008-07-08 12:03:24 +090015#include <asm/processor.h>
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090016#include <asm/io.h>
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090017#include <sh_tmu.h>
18
Nobuhiro Iwamatsucc18f8d2013-07-23 13:57:24 +090019#define TCR_TPSC 0x07
20
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090021static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090022
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090023unsigned long get_tbclk(void)
24{
Nobuhiro Iwamatsu45477822013-08-20 14:33:15 +090025 u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
26 return get_tmu0_clk_rate() >> ((tmu_bit + 1) * 2);
Nobuhiro Iwamatsuf7be78e2012-08-21 13:24:43 +090027}
28
Rob Herring7a626f02013-10-04 10:22:42 -050029unsigned long timer_read_counter(void)
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020030{
Rob Herring7a626f02013-10-04 10:22:42 -050031 return ~readl(&tmu->tcnt0);
Jean-Christophe PLAGNIOL-VILLARD51704102009-06-04 12:06:47 +020032}
33
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090034static void tmu_timer_start(unsigned int timer)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090035{
36 if (timer > 2)
37 return;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090038 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090039}
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090040
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090041static void tmu_timer_stop(unsigned int timer)
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090042{
43 if (timer > 2)
44 return;
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090045 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090046}
47
Nobuhiro Iwamatsue763f1a2012-08-21 13:14:46 +090048int timer_init(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090049{
Nobuhiro Iwamatsu45477822013-08-20 14:33:15 +090050 u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
51 writew((readw(&tmu->tcr0) & ~TCR_TPSC) | tmu_bit, &tmu->tcr0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090052
Nobuhiro Iwamatsuac890472008-11-20 16:44:42 +090053 tmu_timer_stop(0);
54 tmu_timer_start(0);
55
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090056 return 0;
57}
58