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 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include <common.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 31 | #include <div64.h> |
Nobuhiro Iwamatsu | 9e23fe0 | 2008-07-08 12:03:24 +0900 | [diff] [blame] | 32 | #include <asm/processor.h> |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 33 | #include <asm/io.h> |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 34 | #include <sh_tmu.h> |
| 35 | |
| 36 | static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE; |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 37 | |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 38 | static u16 bit; |
Nobuhiro Iwamatsu | 61973af | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 39 | static unsigned long last_tcnt; |
| 40 | static unsigned long long overflow_ticks; |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 41 | |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 42 | unsigned long get_tbclk(void) |
| 43 | { |
| 44 | return get_tmu0_clk_rate() >> ((bit + 1) * 2); |
| 45 | } |
| 46 | |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 47 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 48 | { |
| 49 | tick *= CONFIG_SYS_HZ; |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 50 | do_div(tick, get_tbclk()); |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 51 | |
| 52 | return tick; |
| 53 | } |
| 54 | |
| 55 | static inline unsigned long long usec_to_tick(unsigned long long usec) |
| 56 | { |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 57 | usec *= get_tbclk(); |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 58 | do_div(usec, 1000000); |
| 59 | |
| 60 | return usec; |
| 61 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 62 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 63 | static void tmu_timer_start(unsigned int timer) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 64 | { |
| 65 | if (timer > 2) |
| 66 | return; |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 67 | writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 68 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 69 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 70 | static void tmu_timer_stop(unsigned int timer) |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 71 | { |
| 72 | if (timer > 2) |
| 73 | return; |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 74 | writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 77 | int timer_init(void) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 78 | { |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 79 | bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1; |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 80 | writew(readw(&tmu->tcr0) | bit, &tmu->tcr0); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 81 | |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 82 | tmu_timer_stop(0); |
| 83 | tmu_timer_start(0); |
| 84 | |
Nobuhiro Iwamatsu | 61973af | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 85 | last_tcnt = 0; |
| 86 | overflow_ticks = 0; |
| 87 | |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 91 | unsigned long long get_ticks(void) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 92 | { |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 93 | unsigned long tcnt = 0 - readl(&tmu->tcnt0); |
Nobuhiro Iwamatsu | 61973af | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 94 | |
Nobuhiro Iwamatsu | 78df8c6 | 2012-03-01 13:29:38 +0900 | [diff] [blame] | 95 | if (last_tcnt > tcnt) /* overflow */ |
Nobuhiro Iwamatsu | 61973af | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 96 | overflow_ticks++; |
Nobuhiro Iwamatsu | 61973af | 2010-06-16 08:10:21 +0900 | [diff] [blame] | 97 | last_tcnt = tcnt; |
| 98 | |
| 99 | return (overflow_ticks << 32) | tcnt; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 102 | void __udelay(unsigned long usec) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 103 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 104 | unsigned long long tmp; |
| 105 | ulong tmo; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 106 | |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 107 | tmo = usec_to_tick(usec); |
| 108 | tmp = get_ticks() + tmo; /* get current timestamp */ |
| 109 | |
| 110 | while (get_ticks() < tmp) /* loop till event */ |
| 111 | /*NOP*/; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Nobuhiro Iwamatsu | 73f35e0 | 2012-08-21 13:14:46 +0900 | [diff] [blame] | 114 | unsigned long get_timer(unsigned long base) |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 115 | { |
Jean-Christophe PLAGNIOL-VILLARD | 1e15ff9 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 116 | /* return msec */ |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame] | 117 | return tick_to_time(get_ticks()) - base; |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 118 | } |
| 119 | |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 120 | void set_timer(unsigned long t) |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 121 | { |
Nobuhiro Iwamatsu | d443042 | 2012-08-21 13:24:43 +0900 | [diff] [blame] | 122 | writel((0 - t), &tmu->tcnt0); |
| 123 | } |
| 124 | |
| 125 | void reset_timer(void) |
| 126 | { |
| 127 | tmu_timer_stop(0); |
| 128 | set_timer(0); |
| 129 | tmu_timer_start(0); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 130 | } |