1 /* 2 * (C) Copyright 2002 3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com> 4 * Marius Groeger <mgroeger@sysgo.de> 5 * 6 * (C) Copyright 2002 7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com> 8 * Alex Zuepke <azu@sysgo.de> 9 * 10 * (C) Copyright 2002 11 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de> 12 * 13 * (C) Copyright 2009 14 * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com> 15 * 16 * (C) Copyright 2009 DENX Software Engineering 17 * Author: John Rigby <jrigby@gmail.com> 18 * Add support for MX25 19 * 20 * See file CREDITS for list of people who contributed to this 21 * project. 22 * 23 * This program is free software; you can redistribute it and/or 24 * modify it under the terms of the GNU General Public License as 25 * published by the Free Software Foundation; either version 2 of 26 * the License, or (at your option) any later version. 27 * 28 * This program is distributed in the hope that it will be useful, 29 * but WITHOUT ANY WARRANTY; without even the implied warranty of 30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 * GNU General Public License for more details. 32 * 33 * You should have received a copy of the GNU General Public License 34 * along with this program; if not, write to the Free Software 35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 36 * MA 02111-1307 USA 37 */ 38 39 #include <common.h> 40 #include <div64.h> 41 #include <asm/io.h> 42 #include <asm/arch/imx-regs.h> 43 44 static ulong timestamp; 45 static ulong lastinc; 46 47 /* 48 * "time" is measured in 1 / CONFIG_SYS_HZ seconds, 49 * "tick" is internal timer period 50 */ 51 #ifdef CONFIG_MX25_TIMER_HIGH_PRECISION 52 /* ~0.4% error - measured with stop-watch on 100s boot-delay */ 53 static inline unsigned long long tick_to_time(unsigned long long tick) 54 { 55 tick *= CONFIG_SYS_HZ; 56 do_div(tick, CONFIG_MX25_CLK32); 57 return tick; 58 } 59 60 static inline unsigned long long time_to_tick(unsigned long long time) 61 { 62 time *= CONFIG_MX25_CLK32; 63 do_div(time, CONFIG_SYS_HZ); 64 return time; 65 } 66 67 static inline unsigned long long us_to_tick(unsigned long long us) 68 { 69 us = us * CONFIG_MX25_CLK32 + 999999; 70 do_div(us, 1000000); 71 return us; 72 } 73 #else 74 /* ~2% error */ 75 #define TICK_PER_TIME ((CONFIG_MX25_CLK32 + CONFIG_SYS_HZ / 2) / \ 76 CONFIG_SYS_HZ) 77 #define US_PER_TICK (1000000 / CONFIG_MX25_CLK32) 78 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 97 98 /* nothing really to do with interrupts, just starts up a counter. */ 99 /* The 32KHz 32-bit timer overruns in 134217 seconds */ 100 int timer_init(void) 101 { 102 int i; 103 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE; 104 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; 105 106 /* setup GP Timer 1 */ 107 writel(GPT_CTRL_SWR, &gpt->ctrl); 108 109 writel(readl(&ccm->cgr1) | CCM_CGR1_GPT1, &ccm->cgr1); 110 111 for (i = 0; i < 100; i++) 112 writel(0, &gpt->ctrl); /* We have no udelay by now */ 113 writel(0, &gpt->pre); /* prescaler = 1 */ 114 /* Freerun Mode, 32KHz input */ 115 writel(readl(&gpt->ctrl) | GPT_CTRL_CLKSOURCE_32 | GPT_CTRL_FRR, 116 &gpt->ctrl); 117 writel(readl(&gpt->ctrl) | GPT_CTRL_TEN, &gpt->ctrl); 118 119 return 0; 120 } 121 122 void reset_timer_masked(void) 123 { 124 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE; 125 /* reset time */ 126 /* capture current incrementer value time */ 127 lastinc = readl(&gpt->counter); 128 timestamp = 0; /* start "advancing" time stamp from 0 */ 129 } 130 131 void reset_timer(void) 132 { 133 reset_timer_masked(); 134 } 135 136 unsigned long long get_ticks (void) 137 { 138 struct gpt_regs *gpt = (struct gpt_regs *)IMX_GPT1_BASE; 139 ulong now = readl(&gpt->counter); /* current tick value */ 140 141 if (now >= lastinc) { 142 /* 143 * normal mode (non roll) 144 * move stamp forward with absolut diff ticks 145 */ 146 timestamp += (now - lastinc); 147 } else { 148 /* we have rollover of incrementer */ 149 timestamp += (0xFFFFFFFF - lastinc) + now; 150 } 151 lastinc = now; 152 return timestamp; 153 } 154 155 ulong get_timer_masked (void) 156 { 157 /* 158 * get_ticks() returns a long long (64 bit), it wraps in 159 * 2^64 / CONFIG_MX25_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~ 160 * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in 161 * 5 * 10^6 days - long enough. 162 */ 163 return tick_to_time(get_ticks()); 164 } 165 166 ulong get_timer (ulong base) 167 { 168 return get_timer_masked () - base; 169 } 170 171 void set_timer (ulong t) 172 { 173 timestamp = time_to_tick(t); 174 } 175 176 /* delay x useconds AND preserve advance timstamp value */ 177 void __udelay (unsigned long usec) 178 { 179 unsigned long long tmp; 180 ulong tmo; 181 182 tmo = us_to_tick(usec); 183 tmp = get_ticks() + tmo; /* get current timestamp */ 184 185 while (get_ticks() < tmp) /* loop till event */ 186 /*NOP*/; 187 } 188