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> |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 15 | #include <div64.h> |
Nobuhiro Iwamatsu | 0121325 | 2008-07-08 12:03:24 +0900 | [diff] [blame] | 16 | #include <asm/processor.h> |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 17 | #include <asm/io.h> |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 18 | #include <sh_tmu.h> |
| 19 | |
Nobuhiro Iwamatsu | cc18f8d | 2013-07-23 13:57:24 +0900 | [diff] [blame] | 20 | #define TCR_TPSC 0x07 |
| 21 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 22 | static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE; |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 23 | |
Nobuhiro Iwamatsu | b633dd1 | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 24 | static unsigned long last_tcnt; |
| 25 | static unsigned long long overflow_ticks; |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 26 | |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 27 | unsigned long get_tbclk(void) |
| 28 | { |
Nobuhiro Iwamatsu | 4547782 | 2013-08-20 14:33:15 +0900 | [diff] [blame] | 29 | u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1; |
| 30 | return get_tmu0_clk_rate() >> ((tmu_bit + 1) * 2); |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 31 | } |
| 32 | |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 33 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 34 | { |
| 35 | tick *= CONFIG_SYS_HZ; |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 36 | do_div(tick, get_tbclk()); |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 37 | |
| 38 | return tick; |
| 39 | } |
| 40 | |
| 41 | static inline unsigned long long usec_to_tick(unsigned long long usec) |
| 42 | { |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 43 | usec *= get_tbclk(); |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 44 | do_div(usec, 1000000); |
| 45 | |
| 46 | return usec; |
| 47 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 48 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 49 | static void tmu_timer_start(unsigned int timer) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 50 | { |
| 51 | if (timer > 2) |
| 52 | return; |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 53 | writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 54 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 55 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 56 | static void tmu_timer_stop(unsigned int timer) |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 57 | { |
| 58 | if (timer > 2) |
| 59 | return; |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 60 | writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 63 | int timer_init(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 64 | { |
Nobuhiro Iwamatsu | 4547782 | 2013-08-20 14:33:15 +0900 | [diff] [blame] | 65 | u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1; |
| 66 | writew((readw(&tmu->tcr0) & ~TCR_TPSC) | tmu_bit, &tmu->tcr0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 67 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 68 | tmu_timer_stop(0); |
| 69 | tmu_timer_start(0); |
| 70 | |
Nobuhiro Iwamatsu | b633dd1 | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 71 | last_tcnt = 0; |
| 72 | overflow_ticks = 0; |
| 73 | |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 77 | unsigned long long get_ticks(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 78 | { |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 79 | unsigned long tcnt = 0 - readl(&tmu->tcnt0); |
Nobuhiro Iwamatsu | b633dd1 | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 80 | |
Nobuhiro Iwamatsu | 51fbfb9 | 2012-03-01 13:29:38 +0900 | [diff] [blame] | 81 | if (last_tcnt > tcnt) /* overflow */ |
Nobuhiro Iwamatsu | b633dd1 | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 82 | overflow_ticks++; |
Nobuhiro Iwamatsu | b633dd1 | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 83 | last_tcnt = tcnt; |
| 84 | |
| 85 | return (overflow_ticks << 32) | tcnt; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 88 | void __udelay(unsigned long usec) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 89 | { |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 90 | unsigned long long tmp; |
| 91 | ulong tmo; |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 92 | |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 93 | tmo = usec_to_tick(usec); |
| 94 | tmp = get_ticks() + tmo; /* get current timestamp */ |
| 95 | |
| 96 | while (get_ticks() < tmp) /* loop till event */ |
| 97 | /*NOP*/; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Nobuhiro Iwamatsu | e763f1a | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 100 | unsigned long get_timer(unsigned long base) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 101 | { |
Jean-Christophe PLAGNIOL-VILLARD | 4b13718 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 102 | /* return msec */ |
Jean-Christophe PLAGNIOL-VILLARD | 5170410 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 103 | return tick_to_time(get_ticks()) - base; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 106 | void set_timer(unsigned long t) |
| 107 | { |
| 108 | writel((0 - t), &tmu->tcnt0); |
| 109 | } |
| 110 | |
| 111 | void reset_timer(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 112 | { |
Nobuhiro Iwamatsu | f7be78e | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 113 | tmu_timer_stop(0); |
| 114 | set_timer(0); |
| 115 | tmu_timer_start(0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 116 | } |