[acc25ee] | 1 | /* |
---|
| 2 | * Clock Tick Device Driver |
---|
| 3 | * |
---|
| 4 | * This routine utilizes the Decrementer Register common to the PPC family. |
---|
| 5 | * |
---|
| 6 | * The tick frequency is directly programmed to the configured number of |
---|
| 7 | * microseconds per tick. |
---|
| 8 | * |
---|
[f7588eba] | 9 | * COPYRIGHT (c) 1989-2007. |
---|
[acc25ee] | 10 | * On-Line Applications Research Corporation (OAR). |
---|
| 11 | * |
---|
| 12 | * The license and distribution terms for this file may in |
---|
| 13 | * the file LICENSE in this distribution or at |
---|
[21e1c44] | 14 | * http://www.rtems.com/license/LICENSE. |
---|
[acc25ee] | 15 | * |
---|
| 16 | * Modified to support the MPC750. |
---|
| 17 | * Modifications Copyright (c) 1999 Eric Valette valette@crf.canon.fr |
---|
| 18 | * |
---|
| 19 | * $Id$ |
---|
| 20 | */ |
---|
| 21 | |
---|
[a73a977] | 22 | #include <rtems/system.h> |
---|
[acc25ee] | 23 | #include <rtems.h> |
---|
| 24 | #include <rtems/libio.h> |
---|
| 25 | #include <stdlib.h> /* for atexit() */ |
---|
| 26 | #include <assert.h> |
---|
| 27 | #include <libcpu/c_clock.h> |
---|
[3fa48ee] | 28 | #include <libcpu/cpuIdent.h> |
---|
| 29 | #include <libcpu/spr.h> |
---|
[9c325fb2] | 30 | #include <rtems/bspIo.h> /* for printk() */ |
---|
| 31 | |
---|
| 32 | #include <bspopts.h> /* for CLOCK_DRIVER_USE_FAST_IDLE */ |
---|
[acc25ee] | 33 | |
---|
[3fa48ee] | 34 | SPR_RW(BOOKE_TCR) |
---|
| 35 | SPR_RW(BOOKE_TSR) |
---|
| 36 | SPR_RW(BOOKE_DECAR) |
---|
| 37 | SPR_RW(DEC) |
---|
| 38 | |
---|
[cebb89b] | 39 | extern int BSP_connect_clock_handler (void); |
---|
| 40 | |
---|
[acc25ee] | 41 | /* |
---|
| 42 | * Clock ticks since initialization |
---|
| 43 | */ |
---|
| 44 | |
---|
[66c373bf] | 45 | volatile uint32_t Clock_driver_ticks; |
---|
[acc25ee] | 46 | |
---|
| 47 | /* |
---|
| 48 | * This is the value programmed into the count down timer. |
---|
| 49 | */ |
---|
| 50 | |
---|
[66c373bf] | 51 | uint32_t Clock_Decrementer_value; |
---|
[acc25ee] | 52 | |
---|
| 53 | /* |
---|
| 54 | * These are set by clock driver during its init |
---|
| 55 | */ |
---|
| 56 | |
---|
| 57 | rtems_device_major_number rtems_clock_major = ~0; |
---|
| 58 | rtems_device_minor_number rtems_clock_minor; |
---|
| 59 | |
---|
| 60 | void clockOff(void* unused) |
---|
| 61 | { |
---|
[3fa48ee] | 62 | rtems_interrupt_level l; |
---|
| 63 | |
---|
| 64 | if ( ppc_cpu_is_bookE() ) { |
---|
| 65 | rtems_interrupt_disable(l); |
---|
| 66 | _write_BOOKE_TCR(_read_BOOKE_TCR() & ~BOOKE_TCR_DIE); |
---|
| 67 | rtems_interrupt_enable(l); |
---|
| 68 | } else { |
---|
[0dd1d44] | 69 | /* |
---|
| 70 | * Nothing to do as we cannot disable all interrupts and |
---|
| 71 | * the decrementer interrupt enable is MSR_EE |
---|
| 72 | */ |
---|
[3fa48ee] | 73 | } |
---|
[acc25ee] | 74 | } |
---|
[3fa48ee] | 75 | |
---|
[acc25ee] | 76 | void clockOn(void* unused) |
---|
| 77 | { |
---|
[3fa48ee] | 78 | rtems_interrupt_level l; |
---|
| 79 | |
---|
[acc25ee] | 80 | PPC_Set_decrementer( Clock_Decrementer_value ); |
---|
[3fa48ee] | 81 | |
---|
| 82 | if ( ppc_cpu_is_bookE() ) { |
---|
| 83 | _write_BOOKE_DECAR( Clock_Decrementer_value ); |
---|
| 84 | |
---|
| 85 | rtems_interrupt_disable(l); |
---|
| 86 | |
---|
| 87 | /* clear pending/stale irq */ |
---|
| 88 | _write_BOOKE_TSR( BOOKE_TSR_DIS ); |
---|
| 89 | /* enable */ |
---|
| 90 | _write_BOOKE_TCR( _read_BOOKE_TCR() | BOOKE_TCR_DIE ); |
---|
| 91 | |
---|
| 92 | rtems_interrupt_enable(l); |
---|
| 93 | |
---|
| 94 | } |
---|
[acc25ee] | 95 | } |
---|
| 96 | |
---|
[56758965] | 97 | static void clockHandler(void) |
---|
| 98 | { |
---|
[9c325fb2] | 99 | |
---|
| 100 | #if defined(CLOCK_DRIVER_USE_FAST_IDLE) |
---|
| 101 | do { |
---|
| 102 | rtems_clock_tick(); |
---|
| 103 | } while ( _Thread_Executing == _Thread_Idle && |
---|
| 104 | _Thread_Heir == _Thread_Executing); |
---|
| 105 | |
---|
| 106 | #else |
---|
[56758965] | 107 | rtems_clock_tick(); |
---|
[9c325fb2] | 108 | #endif |
---|
[56758965] | 109 | } |
---|
| 110 | |
---|
| 111 | static void (*clock_handler)(void); |
---|
| 112 | |
---|
[acc25ee] | 113 | /* |
---|
| 114 | * Clock_isr |
---|
| 115 | * |
---|
| 116 | * This is the clock tick interrupt handler. |
---|
| 117 | * |
---|
| 118 | * Input parameters: |
---|
| 119 | * vector - vector number |
---|
| 120 | * |
---|
| 121 | * Output parameters: NONE |
---|
| 122 | * |
---|
| 123 | * Return values: NONE |
---|
| 124 | * |
---|
| 125 | */ |
---|
[d3d9ef37] | 126 | void clockIsr(void *unused) |
---|
[acc25ee] | 127 | { |
---|
[c984fb3] | 128 | int decr; |
---|
[acc25ee] | 129 | /* |
---|
| 130 | * The driver has seen another tick. |
---|
| 131 | */ |
---|
[c984fb3] | 132 | do { |
---|
[9c325fb2] | 133 | register uint32_t flags; |
---|
| 134 | rtems_interrupt_disable(flags); |
---|
| 135 | asm volatile ( |
---|
| 136 | "mfdec %0; add %0, %0, %1; mtdec %0" |
---|
| 137 | : "=&r"(decr) |
---|
| 138 | : "r"(Clock_Decrementer_value)); |
---|
| 139 | rtems_interrupt_enable(flags); |
---|
[acc25ee] | 140 | |
---|
[9c325fb2] | 141 | Clock_driver_ticks += 1; |
---|
[acc25ee] | 142 | |
---|
[9c325fb2] | 143 | /* |
---|
| 144 | * Real Time Clock counter/timer is set to automatically reload. |
---|
| 145 | */ |
---|
| 146 | clock_handler(); |
---|
[c984fb3] | 147 | } while ( decr < 0 ); |
---|
[acc25ee] | 148 | } |
---|
| 149 | |
---|
[3fa48ee] | 150 | /* |
---|
| 151 | * Clock_isr_bookE |
---|
| 152 | * |
---|
| 153 | * This is the clock tick interrupt handler |
---|
| 154 | * for bookE CPUs. For efficiency reasons we |
---|
| 155 | * provide a separate handler rather than |
---|
| 156 | * checking the CPU type each time. |
---|
| 157 | * |
---|
| 158 | * Input parameters: |
---|
| 159 | * vector - vector number |
---|
| 160 | * |
---|
| 161 | * Output parameters: NONE |
---|
| 162 | * |
---|
| 163 | * Return values: NONE |
---|
| 164 | * |
---|
| 165 | */ |
---|
| 166 | void clockIsrBookE(void *unused) |
---|
| 167 | { |
---|
| 168 | /* Note: TSR bit has already been cleared in the exception handler */ |
---|
| 169 | |
---|
| 170 | /* |
---|
| 171 | * The driver has seen another tick. |
---|
| 172 | */ |
---|
| 173 | |
---|
| 174 | Clock_driver_ticks += 1; |
---|
| 175 | |
---|
| 176 | /* |
---|
| 177 | * Real Time Clock counter/timer is set to automatically reload. |
---|
| 178 | */ |
---|
| 179 | clock_handler(); |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
[acc25ee] | 183 | int clockIsOn(void* unused) |
---|
| 184 | { |
---|
[3fa48ee] | 185 | uint32_t msr_value; |
---|
[acc25ee] | 186 | |
---|
[9c325fb2] | 187 | _CPU_MSR_GET( msr_value ); |
---|
[3fa48ee] | 188 | |
---|
[9c325fb2] | 189 | if ( ppc_cpu_is_bookE() && ! (_read_BOOKE_TCR() & BOOKE_TCR_DIE) ) |
---|
| 190 | msr_value = 0; |
---|
[3fa48ee] | 191 | |
---|
[9c325fb2] | 192 | if (msr_value & MSR_EE) return 1; |
---|
[3fa48ee] | 193 | |
---|
[9c325fb2] | 194 | return 0; |
---|
[acc25ee] | 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | /* |
---|
| 199 | * Clock_exit |
---|
| 200 | * |
---|
| 201 | * This routine allows the clock driver to exit by masking the interrupt and |
---|
| 202 | * disabling the clock's counter. |
---|
| 203 | * |
---|
| 204 | * Input parameters: NONE |
---|
| 205 | * |
---|
| 206 | * Output parameters: NONE |
---|
| 207 | * |
---|
| 208 | * Return values: NONE |
---|
| 209 | * |
---|
| 210 | */ |
---|
| 211 | |
---|
| 212 | void Clock_exit( void ) |
---|
| 213 | { |
---|
[0dd1d44] | 214 | (void) BSP_disconnect_clock_handler (); |
---|
[acc25ee] | 215 | } |
---|
| 216 | |
---|
[f7588eba] | 217 | uint32_t Clock_driver_nanoseconds_since_last_tick(void) |
---|
| 218 | { |
---|
| 219 | uint32_t clicks, tmp; |
---|
| 220 | |
---|
| 221 | PPC_Get_decrementer( clicks ); |
---|
| 222 | |
---|
[a205b96b] | 223 | /* |
---|
| 224 | * Multiply by 1000 here separately from below so we do not overflow |
---|
| 225 | * and get a negative value. |
---|
| 226 | */ |
---|
| 227 | tmp = (Clock_Decrementer_value - clicks) * 1000; |
---|
[f7588eba] | 228 | tmp /= (BSP_bus_frequency/BSP_time_base_divisor); |
---|
| 229 | |
---|
[a205b96b] | 230 | return tmp * 1000; |
---|
[f7588eba] | 231 | } |
---|
| 232 | |
---|
[acc25ee] | 233 | /* |
---|
| 234 | * Clock_initialize |
---|
| 235 | * |
---|
| 236 | * This routine initializes the clock driver. |
---|
| 237 | * |
---|
| 238 | * Input parameters: |
---|
| 239 | * major - clock device major number |
---|
| 240 | * minor - clock device minor number |
---|
| 241 | * parg - pointer to optional device driver arguments |
---|
| 242 | * |
---|
| 243 | * Output parameters: NONE |
---|
| 244 | * |
---|
| 245 | * Return values: |
---|
| 246 | * rtems_device_driver status code |
---|
| 247 | */ |
---|
| 248 | rtems_device_driver Clock_initialize( |
---|
| 249 | rtems_device_major_number major, |
---|
| 250 | rtems_device_minor_number minor, |
---|
| 251 | void *pargp |
---|
| 252 | ) |
---|
| 253 | { |
---|
[3fa48ee] | 254 | rtems_interrupt_level l,tcr; |
---|
| 255 | |
---|
[95273a6] | 256 | Clock_Decrementer_value = (BSP_bus_frequency/BSP_time_base_divisor)* |
---|
[8e13ca61] | 257 | (rtems_configuration_get_microseconds_per_tick()/1000); |
---|
[acc25ee] | 258 | |
---|
[c984fb3] | 259 | /* set the decrementer now, prior to installing the handler |
---|
| 260 | * so no interrupts will happen in a while. |
---|
| 261 | */ |
---|
| 262 | PPC_Set_decrementer( (unsigned)-1 ); |
---|
| 263 | |
---|
[3fa48ee] | 264 | /* On a bookE CPU the decrementer works differently. It doesn't |
---|
| 265 | * count past zero but we can enable auto-reload :-) |
---|
| 266 | */ |
---|
| 267 | if ( ppc_cpu_is_bookE() ) { |
---|
| 268 | |
---|
| 269 | rtems_interrupt_disable(l); |
---|
| 270 | |
---|
| 271 | tcr = _read_BOOKE_TCR(); |
---|
[9c325fb2] | 272 | tcr |= BOOKE_TCR_ARE; |
---|
| 273 | tcr &= ~BOOKE_TCR_DIE; |
---|
[3fa48ee] | 274 | _write_BOOKE_TCR(tcr); |
---|
| 275 | |
---|
| 276 | rtems_interrupt_enable(l); |
---|
| 277 | |
---|
| 278 | } |
---|
| 279 | |
---|
[f7588eba] | 280 | /* |
---|
| 281 | * Set the nanoseconds since last tick handler |
---|
| 282 | */ |
---|
| 283 | rtems_clock_set_nanoseconds_extension( |
---|
| 284 | Clock_driver_nanoseconds_since_last_tick |
---|
| 285 | ); |
---|
| 286 | |
---|
[c984fb3] | 287 | /* if a decrementer exception was pending, it is cleared by |
---|
| 288 | * executing the default (nop) handler at this point; |
---|
| 289 | * The next exception will then be taken by our clock handler. |
---|
| 290 | * Clock handler installation initializes the decrementer to |
---|
| 291 | * the correct value. |
---|
| 292 | */ |
---|
| 293 | |
---|
[56758965] | 294 | clock_handler = clockHandler; |
---|
[acc25ee] | 295 | if (!BSP_connect_clock_handler ()) { |
---|
| 296 | printk("Unable to initialize system clock\n"); |
---|
| 297 | rtems_fatal_error_occurred(1); |
---|
| 298 | } |
---|
| 299 | /* make major/minor avail to others such as shared memory driver */ |
---|
| 300 | |
---|
| 301 | rtems_clock_major = major; |
---|
| 302 | rtems_clock_minor = minor; |
---|
| 303 | |
---|
| 304 | return RTEMS_SUCCESSFUL; |
---|
| 305 | } /* Clock_initialize */ |
---|
| 306 | |
---|
| 307 | /* |
---|
| 308 | * Clock_control |
---|
| 309 | * |
---|
| 310 | * This routine is the clock device driver control entry point. |
---|
| 311 | * |
---|
| 312 | * Input parameters: |
---|
| 313 | * major - clock device major number |
---|
| 314 | * minor - clock device minor number |
---|
| 315 | * parg - pointer to optional device driver arguments |
---|
| 316 | * |
---|
| 317 | * Output parameters: NONE |
---|
| 318 | * |
---|
| 319 | * Return values: |
---|
| 320 | * rtems_device_driver status code |
---|
| 321 | */ |
---|
| 322 | |
---|
| 323 | rtems_device_driver Clock_control( |
---|
| 324 | rtems_device_major_number major, |
---|
| 325 | rtems_device_minor_number minor, |
---|
| 326 | void *pargp |
---|
| 327 | ) |
---|
| 328 | { |
---|
| 329 | rtems_libio_ioctl_args_t *args = pargp; |
---|
| 330 | |
---|
| 331 | if (args == 0) |
---|
| 332 | goto done; |
---|
| 333 | |
---|
[95273a6] | 334 | Clock_Decrementer_value = (BSP_bus_frequency/BSP_time_base_divisor)* |
---|
[8e13ca61] | 335 | (rtems_configuration_get_microseconds_per_tick()/1000); |
---|
[acc25ee] | 336 | |
---|
| 337 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) |
---|
[56758965] | 338 | clockHandler(); |
---|
[acc25ee] | 339 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) |
---|
| 340 | { |
---|
[56758965] | 341 | if (clock_handler == NULL) |
---|
| 342 | Clock_initialize(major, minor, 0); |
---|
| 343 | clock_handler = args->buffer; |
---|
[acc25ee] | 344 | } |
---|
| 345 | done: |
---|
| 346 | return RTEMS_SUCCESSFUL; |
---|
| 347 | } |
---|