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 | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 5 | * (C) Copyright 2007-2008 |
| 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> |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame^] | 33 | #include <asm/clk.h> |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 34 | #include <asm/io.h> |
| 35 | |
| 36 | #define TMU_MAX_COUNTER (~0UL) |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame^] | 37 | |
| 38 | static ulong timer_freq; |
| 39 | |
| 40 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 41 | { |
| 42 | tick *= CONFIG_SYS_HZ; |
| 43 | do_div(tick, timer_freq); |
| 44 | |
| 45 | return tick; |
| 46 | } |
| 47 | |
| 48 | static inline unsigned long long usec_to_tick(unsigned long long usec) |
| 49 | { |
| 50 | usec *= timer_freq; |
| 51 | do_div(usec, 1000000); |
| 52 | |
| 53 | return usec; |
| 54 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 55 | |
| 56 | static void tmu_timer_start (unsigned int timer) |
| 57 | { |
| 58 | if (timer > 2) |
| 59 | return; |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 60 | writeb(readb(TSTR) | (1 << timer), TSTR); |
| 61 | } |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 62 | |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 63 | static void tmu_timer_stop (unsigned int timer) |
| 64 | { |
| 65 | if (timer > 2) |
| 66 | return; |
| 67 | writeb(readb(TSTR) & ~(1 << timer), TSTR); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int timer_init (void) |
| 71 | { |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 72 | /* Divide clock by TMU_CLK_DIVIDER */ |
| 73 | u16 bit = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 1e15ff9 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 74 | |
| 75 | switch (TMU_CLK_DIVIDER) { |
| 76 | case 1024: |
| 77 | bit = 4; |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 78 | break; |
| 79 | case 256: |
| 80 | bit = 3; |
| 81 | break; |
Jean-Christophe PLAGNIOL-VILLARD | 1e15ff9 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 82 | case 64: |
| 83 | bit = 2; |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 84 | break; |
Jean-Christophe PLAGNIOL-VILLARD | 1e15ff9 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 85 | case 16: |
| 86 | bit = 1; |
| 87 | break; |
| 88 | case 4: |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 89 | default: |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | writew(readw(TCR0) | bit, TCR0); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 93 | |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame^] | 94 | /* Calc clock rate */ |
| 95 | timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2); |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 96 | |
| 97 | tmu_timer_stop(0); |
| 98 | tmu_timer_start(0); |
| 99 | |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | unsigned long long get_ticks (void) |
| 104 | { |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 105 | return 0 - readl(TCNT0); |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 108 | void udelay (unsigned long usec) |
| 109 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame^] | 110 | unsigned long long tmp; |
| 111 | ulong tmo; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 112 | |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame^] | 113 | tmo = usec_to_tick(usec); |
| 114 | tmp = get_ticks() + tmo; /* get current timestamp */ |
| 115 | |
| 116 | while (get_ticks() < tmp) /* loop till event */ |
| 117 | /*NOP*/; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 118 | } |
| 119 | |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 120 | unsigned long get_timer (unsigned long base) |
| 121 | { |
Jean-Christophe PLAGNIOL-VILLARD | 1e15ff9 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 122 | /* return msec */ |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame^] | 123 | return tick_to_time(get_ticks()) - base; |
Nobuhiro Iwamatsu | e9d5f35 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void set_timer (unsigned long t) |
| 127 | { |
| 128 | writel((0 - t), TCNT0); |
| 129 | } |
| 130 | |
| 131 | void reset_timer (void) |
| 132 | { |
| 133 | tmu_timer_stop(0); |
| 134 | set_timer (0); |
| 135 | tmu_timer_start(0); |
| 136 | } |
| 137 | |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 138 | unsigned long get_tbclk (void) |
| 139 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8dd29c8 | 2009-06-04 12:06:47 +0200 | [diff] [blame^] | 140 | return timer_freq; |
Nobuhiro Iwamatsu | 0b135cf | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 141 | } |