Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 1 | /* |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 2 | * (C) Copyright 2009 |
| 3 | * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> |
| 4 | * |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 5 | * (C) Copyright 2007-2012 |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 6 | * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org> |
| 7 | * |
| 8 | * (C) Copyright 2003 |
| 9 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 10 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Nobuhiro Iwamatsu | 0121325 | 2008-07-08 12:03:24 +0900 | [diff] [blame] | 15 | #include <asm/processor.h> |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 16 | #include <asm/io.h> |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 17 | #include <sh_tmu.h> |
| 18 | |
Nobuhiro Iwamatsu | cc18f8d | 2013-07-23 13:57:24 +0900 | [diff] [blame] | 19 | #define TCR_TPSC 0x07 |
| 20 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 21 | static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE; |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 22 | |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 23 | unsigned long get_tbclk(void) |
| 24 | { |
Nobuhiro Iwamatsu | 4547782 | 2013-08-20 14:33:15 +0900 | [diff] [blame] | 25 | u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1; |
| 26 | return get_tmu0_clk_rate() >> ((tmu_bit + 1) * 2); |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 27 | } |
| 28 | |
Rob Herring | 7a626f0 | 2013-10-04 10:22:42 -0500 | [diff] [blame] | 29 | unsigned long timer_read_counter(void) |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 30 | { |
Rob Herring | 7a626f0 | 2013-10-04 10:22:42 -0500 | [diff] [blame] | 31 | return ~readl(&tmu->tcnt0); |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 34 | static void tmu_timer_start(unsigned int timer) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 35 | { |
| 36 | if (timer > 2) |
| 37 | return; |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 38 | writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 39 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 40 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 41 | static void tmu_timer_stop(unsigned int timer) |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 42 | { |
| 43 | if (timer > 2) |
| 44 | return; |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 45 | writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 48 | int timer_init(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 49 | { |
Nobuhiro Iwamatsu | 4547782 | 2013-08-20 14:33:15 +0900 | [diff] [blame] | 50 | u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1; |
| 51 | writew((readw(&tmu->tcr0) & ~TCR_TPSC) | tmu_bit, &tmu->tcr0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 52 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 53 | tmu_timer_stop(0); |
| 54 | tmu_timer_start(0); |
| 55 | |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |