wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | |
| 29 | #include <common.h> |
| 30 | #include <clps7111.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 31 | #include <asm/proc-armv/ptrace.h> |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 32 | #include <asm/hardware.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 33 | |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 34 | #ifndef CONFIG_NETARM |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 35 | /* we always count down the max. */ |
| 36 | #define TIMER_LOAD_VAL 0xffff |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | /* macro to read the 16 bit timer */ |
| 38 | #define READ_TIMER (IO_TC1D & 0xffff) |
Gary Jennejohn | 7968bb5 | 2007-01-24 12:16:56 +0100 | [diff] [blame] | 39 | |
| 40 | #ifdef CONFIG_LPC2292 |
| 41 | #undef READ_TIMER |
| 42 | #define READ_TIMER (0xFFFFFFFF - GET32(T0TC)) |
| 43 | #endif |
| 44 | |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 45 | #else |
| 46 | #define IRQEN (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_INTR_ENABLE)) |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 47 | #define TM2CTRL (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_TIMER2_CONTROL)) |
| 48 | #define TM2STAT (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_TIMER2_STATUS)) |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 49 | #define TIMER_LOAD_VAL NETARM_GEN_TSTAT_CTC_MASK |
| 50 | #define READ_TIMER (TM2STAT & NETARM_GEN_TSTAT_CTC_MASK) |
| 51 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 53 | #ifdef CONFIG_S3C4510B |
| 54 | /* require interrupts for the S3C4510B */ |
| 55 | # ifndef CONFIG_USE_IRQ |
| 56 | # error CONFIG_USE_IRQ _must_ be defined when using CONFIG_S3C4510B |
| 57 | # else |
| 58 | static struct _irq_handler IRQ_HANDLER[N_IRQS]; |
| 59 | # endif |
| 60 | #endif /* CONFIG_S3C4510B */ |
| 61 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | #ifdef CONFIG_USE_IRQ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | void do_irq (struct pt_regs *pt_regs) |
| 64 | { |
Andreas Engel | 2d4b253 | 2008-01-14 09:06:52 +0000 | [diff] [blame] | 65 | #if defined(CONFIG_S3C4510B) |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 66 | unsigned int pending; |
| 67 | |
| 68 | while ( (pending = GET_REG( REG_INTOFFSET)) != 0x54) { /* sentinal value for no pending interrutps */ |
| 69 | IRQ_HANDLER[pending>>2].m_func( IRQ_HANDLER[pending>>2].m_data); |
| 70 | |
| 71 | /* clear pending interrupt */ |
| 72 | PUT_REG( REG_INTPEND, (1<<(pending>>2))); |
| 73 | } |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 74 | #elif defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_INTEGRATOR) |
| 75 | /* No do_irq() for IntegratorAP/CM720T as yet */ |
Gary Jennejohn | 7968bb5 | 2007-01-24 12:16:56 +0100 | [diff] [blame] | 76 | #elif defined(CONFIG_LPC2292) |
| 77 | |
| 78 | void (*pfnct)(void); |
| 79 | |
| 80 | pfnct = (void (*)(void))VICVectAddr; |
| 81 | |
| 82 | (*pfnct)(); |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 83 | #else |
| 84 | #error do_irq() not defined for this CPU type |
| 85 | #endif |
| 86 | } |
Andreas Engel | 2d4b253 | 2008-01-14 09:06:52 +0000 | [diff] [blame] | 87 | #endif |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 88 | |
| 89 | #ifdef CONFIG_S3C4510B |
| 90 | static void default_isr( void *data) { |
| 91 | printf ("default_isr(): called for IRQ %d\n", (int)data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | } |
| 93 | |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 94 | static void timer_isr( void *data) { |
| 95 | unsigned int *pTime = (unsigned int *)data; |
| 96 | |
| 97 | (*pTime)++; |
| 98 | if ( !(*pTime % (CFG_HZ/4))) { |
| 99 | /* toggle LED 0 */ |
| 100 | PUT_REG( REG_IOPDATA, GET_REG(REG_IOPDATA) ^ 0x1); |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | #endif |
| 105 | |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 106 | #if defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_INTEGRATOR) |
| 107 | /* Use IntegratorAP routines in board/integratorap.c */ |
| 108 | #else |
| 109 | |
Wolfgang Denk | 88bd743 | 2005-10-09 00:22:48 +0200 | [diff] [blame] | 110 | static ulong timestamp; |
| 111 | static ulong lastdec; |
| 112 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 113 | int interrupt_init (void) |
| 114 | { |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 115 | |
| 116 | #if defined(CONFIG_NETARM) |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 117 | /* disable all interrupts */ |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 118 | IRQEN = 0; |
| 119 | |
| 120 | /* operate timer 2 in non-prescale mode */ |
| 121 | TM2CTRL = ( NETARM_GEN_TIMER_SET_HZ(CFG_HZ) | |
| 122 | NETARM_GEN_TCTL_ENABLE | |
| 123 | NETARM_GEN_TCTL_INIT_COUNT(TIMER_LOAD_VAL)); |
| 124 | |
| 125 | /* set timer 2 counter */ |
| 126 | lastdec = TIMER_LOAD_VAL; |
Wolfgang Denk | 4dc1146 | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 127 | #elif defined(CONFIG_IMPA7) || defined(CONFIG_EP7312) || defined(CONFIG_ARMADILLO) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | /* disable all interrupts */ |
| 129 | IO_INTMR1 = 0; |
| 130 | |
| 131 | /* operate timer 1 in prescale mode */ |
| 132 | IO_SYSCON1 |= SYSCON1_TC1M; |
| 133 | |
| 134 | /* select 2kHz clock source for timer 1 */ |
| 135 | IO_SYSCON1 &= ~SYSCON1_TC1S; |
| 136 | |
| 137 | /* set timer 1 counter */ |
| 138 | lastdec = IO_TC1D = TIMER_LOAD_VAL; |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 139 | #elif defined(CONFIG_S3C4510B) |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 140 | int i; |
| 141 | |
| 142 | /* install default interrupt handlers */ |
| 143 | for ( i = 0; i < N_IRQS; i++) { |
| 144 | IRQ_HANDLER[i].m_data = (void *)i; |
| 145 | IRQ_HANDLER[i].m_func = default_isr; |
| 146 | } |
| 147 | |
| 148 | /* configure interrupts for IRQ mode */ |
| 149 | PUT_REG( REG_INTMODE, 0x0); |
| 150 | /* clear any pending interrupts */ |
| 151 | PUT_REG( REG_INTPEND, 0x1FFFFF); |
| 152 | |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 153 | lastdec = 0; |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 154 | |
| 155 | /* install interrupt handler for timer */ |
| 156 | IRQ_HANDLER[INT_TIMER0].m_data = (void *)×tamp; |
| 157 | IRQ_HANDLER[INT_TIMER0].m_func = timer_isr; |
| 158 | |
| 159 | /* configure free running timer 0 */ |
| 160 | PUT_REG( REG_TMOD, 0x0); |
| 161 | /* Stop timer 0 */ |
| 162 | CLR_REG( REG_TMOD, TM0_RUN); |
| 163 | |
| 164 | /* Configure for interval mode */ |
| 165 | CLR_REG( REG_TMOD, TM1_TOGGLE); |
| 166 | |
| 167 | /* |
| 168 | * Load Timer data register with count down value. |
| 169 | * count_down_val = CFG_SYS_CLK_FREQ/CFG_HZ |
| 170 | */ |
| 171 | PUT_REG( REG_TDATA0, (CFG_SYS_CLK_FREQ / CFG_HZ)); |
| 172 | |
| 173 | /* |
| 174 | * Enable global interrupt |
| 175 | * Enable timer0 interrupt |
| 176 | */ |
| 177 | CLR_REG( REG_INTMASK, ((1<<INT_GLOBAL) | (1<<INT_TIMER0))); |
| 178 | |
| 179 | /* Start timer */ |
| 180 | SET_REG( REG_TMOD, TM0_RUN); |
Gary Jennejohn | 7968bb5 | 2007-01-24 12:16:56 +0100 | [diff] [blame] | 181 | #elif defined(CONFIG_LPC2292) |
| 182 | PUT32(T0IR, 0); /* disable all timer0 interrupts */ |
| 183 | PUT32(T0TCR, 0); /* disable timer0 */ |
| 184 | PUT32(T0PR, CFG_SYS_CLK_FREQ / CFG_HZ); |
| 185 | PUT32(T0MCR, 0); |
| 186 | PUT32(T0TC, 0); |
| 187 | PUT32(T0TCR, 1); /* enable timer0 */ |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 188 | |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 189 | #else |
| 190 | #error No interrupt_init() defined for this CPU type |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 191 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | timestamp = 0; |
| 193 | |
| 194 | return (0); |
| 195 | } |
| 196 | |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 197 | #endif /* ! IntegratorAP */ |
| 198 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 199 | /* |
| 200 | * timer without interrupts |
| 201 | */ |
| 202 | |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 203 | |
Gary Jennejohn | 7968bb5 | 2007-01-24 12:16:56 +0100 | [diff] [blame] | 204 | #if defined(CONFIG_IMPA7) || defined(CONFIG_EP7312) || defined(CONFIG_NETARM) || defined(CONFIG_ARMADILLO) || defined(CONFIG_LPC2292) |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 205 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | void reset_timer (void) |
| 207 | { |
| 208 | reset_timer_masked (); |
| 209 | } |
| 210 | |
| 211 | ulong get_timer (ulong base) |
| 212 | { |
| 213 | return get_timer_masked () - base; |
| 214 | } |
| 215 | |
| 216 | void set_timer (ulong t) |
| 217 | { |
| 218 | timestamp = t; |
| 219 | } |
| 220 | |
| 221 | void udelay (unsigned long usec) |
| 222 | { |
| 223 | ulong tmo; |
| 224 | |
| 225 | tmo = usec / 1000; |
| 226 | tmo *= CFG_HZ; |
| 227 | tmo /= 1000; |
| 228 | |
| 229 | tmo += get_timer (0); |
| 230 | |
| 231 | while (get_timer_masked () < tmo) |
Gary Jennejohn | 7968bb5 | 2007-01-24 12:16:56 +0100 | [diff] [blame] | 232 | #ifdef CONFIG_LPC2292 |
| 233 | /* GJ - not sure whether this is really needed or a misunderstanding */ |
| 234 | __asm__ __volatile__(" nop"); |
| 235 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | /*NOP*/; |
Gary Jennejohn | 7968bb5 | 2007-01-24 12:16:56 +0100 | [diff] [blame] | 237 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void reset_timer_masked (void) |
| 241 | { |
| 242 | /* reset time */ |
| 243 | lastdec = READ_TIMER; |
| 244 | timestamp = 0; |
| 245 | } |
| 246 | |
| 247 | ulong get_timer_masked (void) |
| 248 | { |
| 249 | ulong now = READ_TIMER; |
| 250 | |
| 251 | if (lastdec >= now) { |
| 252 | /* normal mode */ |
| 253 | timestamp += lastdec - now; |
| 254 | } else { |
| 255 | /* we have an overflow ... */ |
| 256 | timestamp += lastdec + TIMER_LOAD_VAL - now; |
| 257 | } |
| 258 | lastdec = now; |
| 259 | |
| 260 | return timestamp; |
| 261 | } |
| 262 | |
| 263 | void udelay_masked (unsigned long usec) |
| 264 | { |
| 265 | ulong tmo; |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 266 | ulong endtime; |
| 267 | signed long diff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 269 | if (usec >= 1000) { |
| 270 | tmo = usec / 1000; |
| 271 | tmo *= CFG_HZ; |
| 272 | tmo /= 1000; |
| 273 | } else { |
| 274 | tmo = usec * CFG_HZ; |
| 275 | tmo /= (1000*1000); |
| 276 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 277 | |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 278 | endtime = get_timer_masked () + tmo; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 279 | |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 280 | do { |
| 281 | ulong now = get_timer_masked (); |
| 282 | diff = endtime - now; |
| 283 | } while (diff >= 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 284 | } |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 285 | |
| 286 | #elif defined(CONFIG_S3C4510B) |
| 287 | |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 288 | ulong get_timer (ulong base) |
| 289 | { |
| 290 | return timestamp - base; |
| 291 | } |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 292 | |
| 293 | void udelay (unsigned long usec) |
| 294 | { |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 295 | u32 ticks; |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 296 | |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 297 | ticks = (usec * CFG_HZ) / 1000000; |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 298 | |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 299 | ticks += get_timer (0); |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 300 | |
wdenk | 82db02f | 2004-07-11 22:19:26 +0000 | [diff] [blame] | 301 | while (get_timer (0) < ticks) |
| 302 | /*NOP*/; |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 303 | |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 306 | #elif defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_INTEGRATOR) |
| 307 | /* No timer routines for IntegratorAP/CM720T as yet */ |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 308 | #else |
| 309 | #error Timer routines not defined for this CPU type |
| 310 | #endif |