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> |
| 25 | #include <asm/arch/mx31-regs.h> |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 26 | #include <div64.h> |
Stefano Babic | e17e331 | 2011-02-02 00:49:36 +0000 | [diff] [blame^] | 27 | #include <watchdog.h> |
| 28 | #include <asm/io.h> |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 29 | |
| 30 | #define TIMER_BASE 0x53f90000 /* General purpose timer 1 */ |
| 31 | |
| 32 | /* General purpose timers registers */ |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 33 | #define GPTCR __REG(TIMER_BASE) /* Control register */ |
| 34 | #define GPTPR __REG(TIMER_BASE + 0x4) /* Prescaler register */ |
| 35 | #define GPTSR __REG(TIMER_BASE + 0x8) /* Status register */ |
| 36 | #define GPTCNT __REG(TIMER_BASE + 0x24) /* Counter register */ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 37 | |
| 38 | /* General purpose timers bitfields */ |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 39 | #define GPTCR_SWR (1 << 15) /* Software reset */ |
| 40 | #define GPTCR_FRR (1 << 9) /* Freerun / restart */ |
| 41 | #define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */ |
| 42 | #define GPTCR_TEN 1 /* Timer enable */ |
| 43 | |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 44 | DECLARE_GLOBAL_DATA_PTR; |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 45 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | /* "time" is measured in 1 / CONFIG_SYS_HZ seconds, "tick" is internal timer period */ |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_MX31_TIMER_HIGH_PRECISION |
| 48 | /* ~0.4% error - measured with stop-watch on 100s boot-delay */ |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 49 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 50 | { |
| 51 | tick *= CONFIG_SYS_HZ; |
| 52 | do_div(tick, CONFIG_MX31_CLK32); |
| 53 | return tick; |
| 54 | } |
| 55 | |
| 56 | static inline unsigned long long time_to_tick(unsigned long long time) |
| 57 | { |
| 58 | time *= CONFIG_MX31_CLK32; |
| 59 | do_div(time, CONFIG_SYS_HZ); |
| 60 | return time; |
| 61 | } |
| 62 | |
| 63 | static inline unsigned long long us_to_tick(unsigned long long us) |
| 64 | { |
| 65 | us = us * CONFIG_MX31_CLK32 + 999999; |
| 66 | do_div(us, 1000000); |
| 67 | return us; |
| 68 | } |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 69 | #else |
| 70 | /* ~2% error */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 71 | #define TICK_PER_TIME ((CONFIG_MX31_CLK32 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ) |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 72 | #define US_PER_TICK (1000000 / CONFIG_MX31_CLK32) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 73 | |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 74 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 75 | { |
| 76 | do_div(tick, TICK_PER_TIME); |
| 77 | return tick; |
| 78 | } |
| 79 | |
| 80 | static inline unsigned long long time_to_tick(unsigned long long time) |
| 81 | { |
| 82 | return time * TICK_PER_TIME; |
| 83 | } |
| 84 | |
| 85 | static inline unsigned long long us_to_tick(unsigned long long us) |
| 86 | { |
| 87 | us += US_PER_TICK - 1; |
| 88 | do_div(us, US_PER_TICK); |
| 89 | return us; |
| 90 | } |
| 91 | #endif |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 92 | |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 93 | /* The 32768Hz 32-bit timer overruns in 131072 seconds */ |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 94 | int timer_init (void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 95 | { |
| 96 | int i; |
| 97 | |
| 98 | /* setup GP Timer 1 */ |
| 99 | GPTCR = GPTCR_SWR; |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 100 | for (i = 0; i < 100; i++) |
| 101 | GPTCR = 0; /* We have no udelay by now */ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 102 | GPTPR = 0; /* 32Khz */ |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 103 | /* Freerun Mode, PERCLK1 input */ |
| 104 | GPTCR |= GPTCR_CLKSOURCE_32 | GPTCR_TEN; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | void reset_timer_masked (void) |
| 110 | { |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 111 | /* reset time */ |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 112 | gd->lastinc = GPTCNT; /* capture current incrementer value time */ |
| 113 | gd->tbl = 0; /* start "advancing" time stamp from 0 */ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 116 | void reset_timer(void) |
| 117 | { |
| 118 | reset_timer_masked(); |
| 119 | } |
| 120 | |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 121 | unsigned long long get_ticks (void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 122 | { |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 123 | ulong now = GPTCNT; /* current tick value */ |
| 124 | |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 125 | if (now >= gd->lastinc) /* normal mode (non roll) */ |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 126 | /* move stamp forward with absolut diff ticks */ |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 127 | gd->tbl += (now - gd->lastinc); |
Magnus Lilja | 067a4f9 | 2008-08-29 10:36:17 +0200 | [diff] [blame] | 128 | else /* we have rollover of incrementer */ |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 129 | gd->tbl += (0xFFFFFFFF - gd->lastinc) + now; |
| 130 | gd->lastinc = now; |
| 131 | return gd->tbl; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 134 | ulong get_timer_masked (void) |
| 135 | { |
| 136 | /* |
| 137 | * get_ticks() returns a long long (64 bit), it wraps in |
| 138 | * 2^64 / CONFIG_MX31_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] | 139 | * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 140 | * 5 * 10^6 days - long enough. |
| 141 | */ |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 142 | return tick_to_time(get_ticks()); |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 145 | ulong get_timer (ulong base) |
| 146 | { |
| 147 | return get_timer_masked () - base; |
| 148 | } |
| 149 | |
| 150 | void set_timer (ulong t) |
| 151 | { |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 152 | gd->tbl = time_to_tick(t); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 155 | /* delay x useconds AND preserve advance timestamp value */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 156 | void __udelay (unsigned long usec) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 157 | { |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 158 | unsigned long long tmp; |
| 159 | ulong tmo; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 160 | |
Tomohiro Masubuchi | 9a3393b | 2008-10-21 13:17:16 +0900 | [diff] [blame] | 161 | tmo = us_to_tick(usec); |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 162 | tmp = get_ticks() + tmo; /* get current timestamp */ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 163 | |
Guennadi Liakhovetski | 4771a4c | 2008-09-25 20:54:37 +0200 | [diff] [blame] | 164 | while (get_ticks() < tmp) /* loop till event */ |
| 165 | /*NOP*/; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void reset_cpu (ulong addr) |
| 169 | { |
Stefano Babic | e17e331 | 2011-02-02 00:49:36 +0000 | [diff] [blame^] | 170 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE; |
| 171 | wdog->wcr = WDOG_ENABLE; |
| 172 | while (1) |
| 173 | ; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 174 | } |
Stefano Babic | e17e331 | 2011-02-02 00:49:36 +0000 | [diff] [blame^] | 175 | |
| 176 | #ifdef CONFIG_HW_WATCHDOG |
| 177 | void mxc_hw_watchdog_enable(void) |
| 178 | { |
| 179 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE; |
| 180 | u16 secs; |
| 181 | |
| 182 | /* |
| 183 | * The timer watchdog can be set between |
| 184 | * 0.5 and 128 Seconds. If not defined |
| 185 | * in configuration file, sets 64 Seconds |
| 186 | */ |
| 187 | #ifdef CONFIG_SYS_WD_TIMER_SECS |
| 188 | secs = (CONFIG_SYS_WD_TIMER_SECS << 1) & 0xFF; |
| 189 | if (!secs) secs = 1; |
| 190 | #else |
| 191 | secs = 64; |
| 192 | #endif |
| 193 | writew(readw(&wdog->wcr) | (secs << WDOG_WT_SHIFT) | WDOG_ENABLE, |
| 194 | &wdog->wcr); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void mxc_hw_watchdog_reset(void) |
| 199 | { |
| 200 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE; |
| 201 | |
| 202 | writew(0x5555, &wdog->wsr); |
| 203 | writew(0xAAAA, &wdog->wsr); |
| 204 | } |
| 205 | #endif |