wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 1 | /* |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 2 | * (C) Copyright 2003 Josef Baumgartner <josef.baumgartner@telex.de> |
| 3 | * |
| 4 | * (C) Copyright 2000 |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 28 | #include <asm/mcftimer.h> |
| 29 | |
Zachary P. Landau | 0bba862 | 2006-01-26 17:35:56 -0500 | [diff] [blame] | 30 | #ifdef CONFIG_M5271 |
| 31 | #include <asm/m5271.h> |
| 32 | #include <asm/immap_5271.h> |
| 33 | #endif |
| 34 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 35 | #ifdef CONFIG_M5272 |
| 36 | #include <asm/m5272.h> |
| 37 | #include <asm/immap_5272.h> |
| 38 | #endif |
| 39 | |
| 40 | #ifdef CONFIG_M5282 |
| 41 | #include <asm/m5282.h> |
| 42 | #endif |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 43 | |
stroese | 8960792 | 2004-12-16 17:56:09 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_M5249 |
| 45 | #include <asm/m5249.h> |
| 46 | #include <asm/immap_5249.h> |
| 47 | #endif |
| 48 | |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 49 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 50 | static ulong timestamp; |
Zachary P. Landau | 0bba862 | 2006-01-26 17:35:56 -0500 | [diff] [blame] | 51 | #if defined(CONFIG_M5282) || defined(CONFIG_M5271) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 52 | static unsigned short lastinc; |
| 53 | #endif |
| 54 | |
| 55 | |
| 56 | #if defined(CONFIG_M5272) |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 57 | /* |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 58 | * We use timer 3 which is running with a period of 1 us |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 59 | */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 60 | void udelay(unsigned long usec) |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 61 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 62 | volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE3); |
| 63 | uint start, now, tmp; |
| 64 | |
| 65 | while (usec > 0) { |
| 66 | if (usec > 65000) |
| 67 | tmp = 65000; |
| 68 | else |
| 69 | tmp = usec; |
| 70 | usec = usec - tmp; |
| 71 | |
| 72 | /* Set up TIMER 3 as timebase clock */ |
| 73 | timerp->timer_tmr = MCFTIMER_TMR_DISABLE; |
| 74 | timerp->timer_tcn = 0; |
| 75 | /* set period to 1 us */ |
| 76 | timerp->timer_tmr = (((CFG_CLK / 1000000) - 1) << 8) | MCFTIMER_TMR_CLK1 | |
| 77 | MCFTIMER_TMR_FREERUN | MCFTIMER_TMR_ENABLE; |
| 78 | |
| 79 | start = now = timerp->timer_tcn; |
| 80 | while (now < start + tmp) |
| 81 | now = timerp->timer_tcn; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void mcf_timer_interrupt (void * not_used){ |
| 86 | volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4); |
| 87 | volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1); |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 88 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 89 | /* check for timer 4 interrupts */ |
| 90 | if ((intp->int_isr & 0x01000000) != 0) { |
| 91 | return; |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 92 | } |
| 93 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 94 | /* reset timer */ |
| 95 | timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF; |
| 96 | timestamp ++; |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 97 | } |
| 98 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 99 | void timer_init (void) { |
| 100 | volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE4); |
| 101 | volatile intctrl_t *intp = (intctrl_t *) (CFG_MBAR + MCFSIM_ICR1); |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 102 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 103 | timestamp = 0; |
| 104 | |
| 105 | /* Set up TIMER 4 as clock */ |
| 106 | timerp->timer_tmr = MCFTIMER_TMR_DISABLE; |
| 107 | |
| 108 | /* initialize and enable timer 4 interrupt */ |
| 109 | irq_install_handler (72, mcf_timer_interrupt, 0); |
| 110 | intp->int_icr1 |= 0x0000000d; |
| 111 | |
| 112 | timerp->timer_tcn = 0; |
| 113 | timerp->timer_trr = 1000; /* Interrupt every ms */ |
| 114 | /* set a period of 1us, set timer mode to restart and enable timer and interrupt */ |
| 115 | timerp->timer_tmr = (((CFG_CLK / 1000000) - 1) << 8) | MCFTIMER_TMR_CLK1 | |
| 116 | MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE; |
| 117 | } |
| 118 | |
| 119 | void reset_timer (void) |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 120 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 121 | timestamp = 0; |
| 122 | } |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 123 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 124 | ulong get_timer (ulong base) |
| 125 | { |
| 126 | return (timestamp - base); |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 127 | } |
| 128 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 129 | void set_timer (ulong t) |
| 130 | { |
| 131 | timestamp = t; |
| 132 | } |
| 133 | #endif |
| 134 | |
Zachary P. Landau | 0bba862 | 2006-01-26 17:35:56 -0500 | [diff] [blame] | 135 | #if defined(CONFIG_M5282) || defined(CONFIG_M5271) |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 136 | |
| 137 | void udelay(unsigned long usec) |
| 138 | { |
wdenk | 6f1ff38 | 2005-04-03 23:35:57 +0000 | [diff] [blame] | 139 | volatile unsigned short *timerp; |
| 140 | uint tmp; |
| 141 | |
| 142 | timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE3); |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 143 | |
wdenk | 6f1ff38 | 2005-04-03 23:35:57 +0000 | [diff] [blame] | 144 | while (usec > 0) { |
| 145 | if (usec > 65000) |
| 146 | tmp = 65000; |
| 147 | else |
| 148 | tmp = usec; |
| 149 | usec = usec - tmp; |
| 150 | |
| 151 | /* Set up TIMER 3 as timebase clock */ |
| 152 | timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW; |
| 153 | timerp[MCFTIMER_PMR] = 0; |
| 154 | /* set period to 1 us */ |
| 155 | timerp[MCFTIMER_PCSR] = |
Bartlomiej Sieka | 8ff81c6 | 2006-12-20 00:27:32 +0100 | [diff] [blame] | 156 | #ifdef CONFIG_M5271 |
| 157 | (6 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW; |
| 158 | #else /* !CONFIG_M5271 */ |
wdenk | 6f1ff38 | 2005-04-03 23:35:57 +0000 | [diff] [blame] | 159 | (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW; |
Bartlomiej Sieka | 8ff81c6 | 2006-12-20 00:27:32 +0100 | [diff] [blame] | 160 | #endif /* CONFIG_M5271 */ |
wdenk | 6f1ff38 | 2005-04-03 23:35:57 +0000 | [diff] [blame] | 161 | |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 162 | timerp[MCFTIMER_PMR] = tmp; |
wdenk | 6f1ff38 | 2005-04-03 23:35:57 +0000 | [diff] [blame] | 163 | while (timerp[MCFTIMER_PCNTR] > 0); |
| 164 | } |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 165 | } |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 166 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 167 | void timer_init (void) |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 168 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 169 | volatile unsigned short *timerp; |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 170 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 171 | timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4); |
| 172 | timestamp = 0; |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 173 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 174 | /* Set up TIMER 4 as poll clock */ |
| 175 | timerp[MCFTIMER_PCSR] = MCFTIMER_PCSR_OVW; |
| 176 | timerp[MCFTIMER_PMR] = lastinc = 0; |
| 177 | timerp[MCFTIMER_PCSR] = |
Bartlomiej Sieka | 8ff81c6 | 2006-12-20 00:27:32 +0100 | [diff] [blame] | 178 | #ifdef CONFIG_M5271 |
| 179 | (6 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW; |
| 180 | #else /* !CONFIG_M5271 */ |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 181 | (5 << 8) | MCFTIMER_PCSR_EN | MCFTIMER_PCSR_OVW; |
Bartlomiej Sieka | 8ff81c6 | 2006-12-20 00:27:32 +0100 | [diff] [blame] | 182 | #endif /* CONFIG_M5271 */ |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 183 | } |
| 184 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 185 | void set_timer (ulong t) |
| 186 | { |
| 187 | volatile unsigned short *timerp; |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 188 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 189 | timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4); |
| 190 | timestamp = 0; |
| 191 | timerp[MCFTIMER_PMR] = lastinc = 0; |
| 192 | } |
| 193 | |
| 194 | ulong get_timer (ulong base) |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 195 | { |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 196 | unsigned short now, diff; |
| 197 | volatile unsigned short *timerp; |
| 198 | |
| 199 | timerp = (volatile unsigned short *) (CFG_MBAR + MCFTIMER_BASE4); |
| 200 | now = timerp[MCFTIMER_PCNTR]; |
| 201 | diff = -(now - lastinc); |
| 202 | |
| 203 | timestamp += diff; |
| 204 | lastinc = now; |
| 205 | return timestamp - base; |
wdenk | abf7a7c | 2003-12-08 01:34:36 +0000 | [diff] [blame] | 206 | } |
| 207 | |
wdenk | e65527f | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 208 | void wait_ticks (unsigned long ticks) |
| 209 | { |
| 210 | set_timer (0); |
| 211 | while (get_timer (0) < ticks); |
| 212 | } |
| 213 | #endif |
wdenk | d11115a | 2004-06-09 15:24:18 +0000 | [diff] [blame] | 214 | |
| 215 | |
stroese | 8960792 | 2004-12-16 17:56:09 +0000 | [diff] [blame] | 216 | #if defined(CONFIG_M5249) |
| 217 | /* |
| 218 | * We use timer 1 which is running with a period of 1 us |
| 219 | */ |
| 220 | void udelay(unsigned long usec) |
| 221 | { |
| 222 | volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE1); |
| 223 | uint start, now, tmp; |
| 224 | |
| 225 | while (usec > 0) { |
| 226 | if (usec > 65000) |
| 227 | tmp = 65000; |
| 228 | else |
| 229 | tmp = usec; |
| 230 | usec = usec - tmp; |
| 231 | |
| 232 | /* Set up TIMER 1 as timebase clock */ |
| 233 | timerp->timer_tmr = MCFTIMER_TMR_DISABLE; |
| 234 | timerp->timer_tcn = 0; |
| 235 | /* set period to 1 us */ |
| 236 | /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */ |
| 237 | timerp->timer_tmr = (((CFG_CLK / 2000000) - 1) << 8) | MCFTIMER_TMR_CLK1 | |
| 238 | MCFTIMER_TMR_FREERUN | MCFTIMER_TMR_ENABLE; |
| 239 | |
| 240 | start = now = timerp->timer_tcn; |
| 241 | while (now < start + tmp) |
| 242 | now = timerp->timer_tcn; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void mcf_timer_interrupt (void * not_used){ |
| 247 | volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2); |
| 248 | |
| 249 | /* check for timer 2 interrupts */ |
| 250 | if ((mbar_readLong(MCFSIM_IPR) & 0x00000400) == 0) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | /* reset timer */ |
| 255 | timerp->timer_ter = MCFTIMER_TER_CAP | MCFTIMER_TER_REF; |
| 256 | timestamp ++; |
| 257 | } |
| 258 | |
| 259 | void timer_init (void) { |
| 260 | volatile timer_t *timerp = (timer_t *) (CFG_MBAR + MCFTIMER_BASE2); |
| 261 | |
| 262 | timestamp = 0; |
| 263 | |
| 264 | /* Set up TIMER 2 as clock */ |
| 265 | timerp->timer_tmr = MCFTIMER_TMR_DISABLE; |
| 266 | |
| 267 | /* initialize and enable timer 2 interrupt */ |
| 268 | irq_install_handler (31, mcf_timer_interrupt, 0); |
| 269 | mbar_writeLong(MCFSIM_IMR, mbar_readLong(MCFSIM_IMR) & ~0x00000400); |
| 270 | mbar_writeByte(MCFSIM_TIMER2ICR, MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL7 | MCFSIM_ICR_PRI3); |
| 271 | |
| 272 | timerp->timer_tcn = 0; |
| 273 | timerp->timer_trr = 1000; /* Interrupt every ms */ |
| 274 | /* set a period of 1us, set timer mode to restart and enable timer and interrupt */ |
| 275 | /* on m5249 the system clock is (cpu_clk / 2) -> divide by 2000000 */ |
| 276 | timerp->timer_tmr = (((CFG_CLK / 2000000) - 1) << 8) | MCFTIMER_TMR_CLK1 | |
| 277 | MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENORI | MCFTIMER_TMR_ENABLE; |
| 278 | } |
| 279 | |
| 280 | void reset_timer (void) |
| 281 | { |
| 282 | timestamp = 0; |
| 283 | } |
| 284 | |
| 285 | ulong get_timer (ulong base) |
| 286 | { |
| 287 | return (timestamp - base); |
| 288 | } |
| 289 | |
| 290 | void set_timer (ulong t) |
| 291 | { |
| 292 | timestamp = t; |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | |
wdenk | d11115a | 2004-06-09 15:24:18 +0000 | [diff] [blame] | 297 | /* |
| 298 | * This function is derived from PowerPC code (read timebase as long long). |
| 299 | * On M68K it just returns the timer value. |
| 300 | */ |
| 301 | unsigned long long get_ticks(void) |
| 302 | { |
| 303 | return get_timer(0); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * This function is derived from PowerPC code (timebase clock frequency). |
| 308 | * On M68K it returns the number of timer ticks per second. |
| 309 | */ |
| 310 | ulong get_tbclk (void) |
| 311 | { |
| 312 | ulong tbclk; |
| 313 | tbclk = CFG_HZ; |
| 314 | return tbclk; |
| 315 | } |