Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 1 | /* |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 2 | * (C) Copyright 2007-2008 |
| 3 | * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org> |
| 4 | * |
| 5 | * (C) Copyright 2003 |
| 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
Nobuhiro Iwamatsu | 0121325 | 2008-07-08 12:03:24 +0900 | [diff] [blame] | 28 | #include <asm/processor.h> |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 29 | #include <asm/io.h> |
| 30 | |
| 31 | #define TMU_MAX_COUNTER (~0UL) |
| 32 | static int clk_adj = 1; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 33 | |
| 34 | static void tmu_timer_start (unsigned int timer) |
| 35 | { |
| 36 | if (timer > 2) |
| 37 | return; |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 38 | writeb(readb(TSTR) | (1 << timer), TSTR); |
| 39 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 40 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 41 | static void tmu_timer_stop (unsigned int timer) |
| 42 | { |
| 43 | if (timer > 2) |
| 44 | return; |
| 45 | writeb(readb(TSTR) & ~(1 << timer), TSTR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | int timer_init (void) |
| 49 | { |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 50 | /* Divide clock by TMU_CLK_DIVIDER */ |
| 51 | u16 bit = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 4b13718 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 52 | |
| 53 | switch (TMU_CLK_DIVIDER) { |
| 54 | case 1024: |
| 55 | bit = 4; |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 56 | break; |
| 57 | case 256: |
| 58 | bit = 3; |
| 59 | break; |
Jean-Christophe PLAGNIOL-VILLARD | 4b13718 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 60 | case 64: |
| 61 | bit = 2; |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 62 | break; |
Jean-Christophe PLAGNIOL-VILLARD | 4b13718 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 63 | case 16: |
| 64 | bit = 1; |
| 65 | break; |
| 66 | case 4: |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 67 | default: |
| 68 | bit = 0; |
| 69 | break; |
| 70 | } |
| 71 | writew(readw(TCR0) | bit, TCR0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 72 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 73 | /* Clock adjustment calc */ |
Jean-Christophe PLAGNIOL-VILLARD | 4b13718 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 74 | clk_adj = (int)(1.0 / ((1.0 / CONFIG_SYS_HZ) * 1000000)); |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 75 | if (clk_adj < 1) |
| 76 | clk_adj = 1; |
| 77 | |
| 78 | tmu_timer_stop(0); |
| 79 | tmu_timer_start(0); |
| 80 | |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | unsigned long long get_ticks (void) |
| 85 | { |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 86 | return 0 - readl(TCNT0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 89 | static unsigned long get_usec (void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 90 | { |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 91 | return (0 - readl(TCNT0)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 92 | } |
| 93 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 94 | void udelay (unsigned long usec) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 95 | { |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 96 | unsigned int start = get_usec(); |
| 97 | unsigned int end = start + (usec * clk_adj); |
| 98 | |
| 99 | while (get_usec() < end) |
| 100 | continue; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 103 | unsigned long get_timer (unsigned long base) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 104 | { |
Jean-Christophe PLAGNIOL-VILLARD | 4b13718 | 2008-12-20 15:25:22 +0100 | [diff] [blame] | 105 | /* return msec */ |
| 106 | return ((get_usec() / clk_adj) / 1000) - base; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 109 | void set_timer (unsigned long t) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 110 | { |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 111 | writel((0 - t), TCNT0); |
| 112 | } |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 113 | |
Nobuhiro Iwamatsu | ac89047 | 2008-11-20 16:44:42 +0900 | [diff] [blame] | 114 | void reset_timer (void) |
| 115 | { |
| 116 | tmu_timer_stop(0); |
| 117 | set_timer (0); |
| 118 | tmu_timer_start(0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | unsigned long get_tbclk (void) |
| 122 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 123 | return CONFIG_SYS_HZ; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 124 | } |