Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 1 | /* |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 2 | * (C) Copyright 2009 |
| 3 | * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> |
| 4 | * |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 5 | * (C) Copyright 2007-2012 |
Nobuhiro Iwamatsu | e9d5f35 | 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 | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 10 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Nobuhiro Iwamatsu | 9e23fe0 | 2008-07-08 12:03:24 +0900 | [diff] [blame] | 15 | #include <asm/processor.h> |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 16 | #include <asm/io.h> |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 17 | #include <sh_tmu.h> |
| 18 | |
Nobuhiro Iwamatsu | 861bd4b | 2013-07-23 13:57:24 +0900 | [diff] [blame] | 19 | #define TCR_TPSC 0x07 |
| 20 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 21 | static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE; |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 22 | |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 23 | unsigned long get_tbclk(void) |
| 24 | { |
Nobuhiro Iwamatsu | b8f1608 | 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 | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 27 | } |
| 28 | |
Rob Herring | 1b5cf95 | 2013-10-04 10:22:42 -0500 | [diff] [blame] | 29 | unsigned long timer_read_counter(void) |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 30 | { |
Rob Herring | 1b5cf95 | 2013-10-04 10:22:42 -0500 | [diff] [blame] | 31 | return ~readl(&tmu->tcnt0); |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 32 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 33 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 34 | static void tmu_timer_start(unsigned int timer) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 35 | { |
| 36 | if (timer > 2) |
| 37 | return; |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 38 | writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 39 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 40 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 41 | static void tmu_timer_stop(unsigned int timer) |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 42 | { |
| 43 | if (timer > 2) |
| 44 | return; |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 45 | writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 48 | int timer_init(void) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 49 | { |
Nobuhiro Iwamatsu | b8f1608 | 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 | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 52 | |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 53 | tmu_timer_stop(0); |
| 54 | tmu_timer_start(0); |
| 55 | |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |