Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Sascha Hauer, Pengutronix |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Stefano Babic | 78129d9 | 2011-03-14 15:43:56 +0100 | [diff] [blame] | 25 | #include <asm/arch/imx-regs.h> |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 26 | #include <asm/arch/clock.h> |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 27 | #include <div64.h> |
Stefano Babic | e17e331 | 2011-02-02 00:49:36 +0000 | [diff] [blame] | 28 | #include <watchdog.h> |
| 29 | #include <asm/io.h> |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 30 | |
| 31 | #define TIMER_BASE 0x53f90000 /* General purpose timer 1 */ |
| 32 | |
| 33 | /* General purpose timers registers */ |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 34 | #define GPTCR __REG(TIMER_BASE) /* Control register */ |
| 35 | #define GPTPR __REG(TIMER_BASE + 0x4) /* Prescaler register */ |
| 36 | #define GPTSR __REG(TIMER_BASE + 0x8) /* Status register */ |
| 37 | #define GPTCNT __REG(TIMER_BASE + 0x24) /* Counter register */ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 38 | |
| 39 | /* General purpose timers bitfields */ |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 40 | #define GPTCR_SWR (1 << 15) /* Software reset */ |
| 41 | #define GPTCR_FRR (1 << 9) /* Freerun / restart */ |
| 42 | #define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */ |
| 43 | #define GPTCR_TEN 1 /* Timer enable */ |
| 44 | |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 45 | DECLARE_GLOBAL_DATA_PTR; |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 46 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 47 | /* |
| 48 | * "time" is measured in 1 / CONFIG_SYS_HZ seconds, |
| 49 | * "tick" is internal timer period |
| 50 | */ |
| 51 | |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_MX31_TIMER_HIGH_PRECISION |
| 53 | /* ~0.4% error - measured with stop-watch on 100s boot-delay */ |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 54 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 55 | { |
| 56 | tick *= CONFIG_SYS_HZ; |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 57 | do_div(tick, MXC_CLK32); |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 58 | return tick; |
| 59 | } |
| 60 | |
| 61 | static inline unsigned long long time_to_tick(unsigned long long time) |
| 62 | { |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 63 | time *= MXC_CLK32; |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 64 | do_div(time, CONFIG_SYS_HZ); |
| 65 | return time; |
| 66 | } |
| 67 | |
| 68 | static inline unsigned long long us_to_tick(unsigned long long us) |
| 69 | { |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 70 | us = us * MXC_CLK32 + 999999; |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 71 | do_div(us, 1000000); |
| 72 | return us; |
| 73 | } |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 74 | #else |
| 75 | /* ~2% error */ |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 76 | #define TICK_PER_TIME ((MXC_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ) |
| 77 | #define US_PER_TICK (1000000 / MXC_CLK32) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 78 | |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 79 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 80 | { |
| 81 | do_div(tick, TICK_PER_TIME); |
| 82 | return tick; |
| 83 | } |
| 84 | |
| 85 | static inline unsigned long long time_to_tick(unsigned long long time) |
| 86 | { |
| 87 | return time * TICK_PER_TIME; |
| 88 | } |
| 89 | |
| 90 | static inline unsigned long long us_to_tick(unsigned long long us) |
| 91 | { |
| 92 | us += US_PER_TICK - 1; |
| 93 | do_div(us, US_PER_TICK); |
| 94 | return us; |
| 95 | } |
| 96 | #endif |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 97 | |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 98 | /* The 32768Hz 32-bit timer overruns in 131072 seconds */ |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 99 | int timer_init(void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 100 | { |
| 101 | int i; |
| 102 | |
| 103 | /* setup GP Timer 1 */ |
| 104 | GPTCR = GPTCR_SWR; |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 105 | for (i = 0; i < 100; i++) |
| 106 | GPTCR = 0; /* We have no udelay by now */ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 107 | GPTPR = 0; /* 32Khz */ |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 108 | /* Freerun Mode, PERCLK1 input */ |
| 109 | GPTCR |= GPTCR_CLKSOURCE_32 | GPTCR_TEN; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 114 | unsigned long long get_ticks(void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 115 | { |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 116 | ulong now = GPTCNT; /* current tick value */ |
| 117 | |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 118 | if (now >= gd->lastinc) /* normal mode (non roll) */ |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 119 | /* move stamp forward with absolut diff ticks */ |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame^] | 120 | gd->arch.tbl += (now - gd->lastinc); |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 121 | else /* we have rollover of incrementer */ |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame^] | 122 | gd->arch.tbl += (0xFFFFFFFF - gd->lastinc) + now; |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 123 | gd->lastinc = now; |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame^] | 124 | return gd->arch.tbl; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 127 | ulong get_timer_masked(void) |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 128 | { |
| 129 | /* |
| 130 | * get_ticks() returns a long long (64 bit), it wraps in |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 131 | * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 132 | * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 133 | * 5 * 10^6 days - long enough. |
| 134 | */ |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 135 | return tick_to_time(get_ticks()); |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 138 | ulong get_timer(ulong base) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 139 | { |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 140 | return get_timer_masked() - base; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 143 | /* delay x useconds AND preserve advance timestamp value */ |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 144 | void __udelay(unsigned long usec) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 145 | { |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 146 | unsigned long long tmp; |
| 147 | ulong tmo; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 148 | |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 149 | tmo = us_to_tick(usec); |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 150 | tmp = get_ticks() + tmo; /* get current timestamp */ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 151 | |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 152 | while (get_ticks() < tmp) /* loop till event */ |
| 153 | /*NOP*/; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Stefano Babic | ecb6e6e | 2012-02-04 13:02:01 +0100 | [diff] [blame] | 156 | /* |
| 157 | * This function is derived from PowerPC code (timebase clock frequency). |
| 158 | * On ARM it returns the number of timer ticks per second. |
| 159 | */ |
| 160 | ulong get_tbclk(void) |
| 161 | { |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 162 | return MXC_CLK32; |
Stefano Babic | ecb6e6e | 2012-02-04 13:02:01 +0100 | [diff] [blame] | 163 | } |