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